MOZ_text icon indicating copy to clipboard operation
MOZ_text copied to clipboard

Font size and line height conversion

Open lojjic opened this issue 4 years ago • 1 comments

Investigating a better approach for matching font size and line spacing than the current SDF_TEXT_MAGIC_SIZE_FACTOR and SDF_TEXT_MAGIC_LINESPACING_FACTOR constants.

Starting with the good news: Blender's line spacing appears to be a simple baseline-to-baseline distance, as a multiple of its "size" attribute. This matches Troika's approach of using fontSize-relative units. The issue, then, is all in matching Blender's "size" to the fontSize.

Troika follows CSS and interprets fontSize as the em height. Blender doesn't follow that convention, however, and in fact for the same "size" in Blender you'll get wildly different em heights across fonts.

... To the source! ...

I think this is where Blender interprets is "size" attribute: https://developer.blender.org/diffusion/B/browse/master/source/blender/blenlib/intern/freetypefont.c$346

if (face->bbox.yMax != face->bbox.yMin) {
    vfd->scale = (float)(1.0 / (double)(face->bbox.yMax - face->bbox.yMin));

    if (complete_font) {
      vfd->em_height = (float)(face->ascender - face->descender) /
                       (face->bbox.yMax - face->bbox.yMin);
    }
  }

So it seems to map its "size" value to the yMin-to-yMax range (that's the min/max y coordinates out of all glyphs, iiuc) and the em height ends up being relative to that.

IMO for this MOZ_text glTF extension you'll want to define the font size the same way Troika and CSS do, as the em height. So the conversion should therefore be done in the Blender export extension, by using some inversion of that yMin/yMax logic above. However I don't know if you have access to those freetypefont metrics in the python script?

lojjic avatar Apr 16 '20 18:04 lojjic