#9540

Add a DirectWrite font loader

Openlheckerwants to merge intofrom

Windows has a builtin text rasterizer. Let's use it!

I recommend checking out the images below at 1:1 pixel scale and switch between them. DirectWrite looks quite nice. Otherwise, it also features the ability to load system fonts and font fallback.

To make the former possible, this PR introduces the AddFontFromFamily API.

stb_truetype, 100% scale:

DirectWrite, 100% scale:

stb_truetype, 150% scale:

DirectWrite, 150% scale:

Activity

6
lheckerreviewed2mo ago
ocornut1mo ago

Hello,

I have rebased this over latest (fixed minor conflicts) but due to a recent change in ImFontLoader signature it doesn't work anymore. I thought I'd easily fix it but I wasn't sure how to adapt some details map_codepoint_with_fallback(), maybe you'd more easily adapt it.

The new design introduced for #9559 allows loading glyphs from codepoint or font index. See commits: 7263a23f1beab16c72b4a72a036fd822313cfc4e, dfa3c0c0bb7d694348855b3a67ff95d0319a2d61, 09d740ba3292a8609481a17e5b20f16df3f34377, 46ff5a7aab3c091fe8e4e427fea2cabffcf0478d

Old signatures:

static bool font_src_contains_glyph(ImFontAtlas*, ImFontConfig* src, ImWchar codepoint);
static bool font_baked_load_glyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint, ImFontGlyph* out_glyph, float* out_advance_x);

New signatures:

static int  font_src_get_glyph_index_from_codepoint(ImFontAtlas*, ImFontConfig* src, ImWchar codepoint);
static bool font_src_contains_glyph(ImFontAtlas*, ImFontConfig* src, int glyph_index;
static bool font_baked_load_glyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, int glyph_index, ImFontGlyph* out_glyph, float* out_advance_x);
lhecker1mo ago

@ocornut I believe this unfortunately prevents the font fallback that this PR introduces from working. Would you be open to revising the API again?

My PR permits you to request the font "Segoe UI" but render e.g. "木" anyway. map_codepoint_with_fallback() performs font fallback and resolves to a tuple of ("SimSun", 12345) for "木". The new font_src_get_glyph_index_from_codepoint complicates this, since it only returns a glyph index, but no font face to associate it with.

It's possible to work around this by returning a fake (tagged) glyph index. But I'm not sure if that's something you'd like to see merged.

ocornut1mo ago

Would you be open to revising the API again?

yes