3

For a webapp I need a way to prevent that a browser falls back to another font if my web font doesn't include a character. It seems the only way to do this is to add another font to the fontstack which includes "all" possible characters 1.

There are already existing fallback fonts, but those are more debug helpers as they show the codepoint as number, therefore they are much to heavy (>2MB).

The fallback font for my usecase should just show something like a box to signal a missing character.

My idea was to generate a simple font with only one glyph and apply a feature file which will replace all glyphs with this one.

My script for fontforge:

import fontforge
import fontTools.feaLib.builder as feaLibBuilder
from fontTools.ttLib import TTFont

font_name = 'maeh.ttf'
font = fontforge.font()
glyph = font.createChar(33, "theone")
pen = glyph.glyphPen()
pen.moveTo((100,100))
pen.lineTo((100,500))
pen.lineTo((500,500))
pen.lineTo((500,100))
pen.closePath()

for i in range(34, 99):
    glyph = font.createChar(i)
    glyph.width=10

font.cidConvertTo('Adobe', 'Identity', 0)  # doesn't make a difference

font.generate(font_name)

font = TTFont(font_name)
feaLibBuilder.addOpenTypeFeatures(font, 'fallback.fea')
font.save("fea_"+font_name)

My feature file:

languagesystem DFLT dflt;

@all=[\00035-\00039];
#@all=[A-Z]   this works 

feature liga {
    sub @all by theone;
} liga;

But the above results in a

KeyError: ('cid00037', 'SingleSubst[0]', 'Lookup[0]', 'LookupList')

with changing numbers for cid00037.

If I use the out commented A-Z from the Feature file it works, so this approach doesn't seem to be completely wrong.

Why can't fonttools find the glyphs if I specify the range in CID notation? Is there another way to crate a class for the OpenType feature file which includes all glyphs?

Hooked
  • 84,485
  • 43
  • 192
  • 261
tobltobs
  • 2,782
  • 1
  • 27
  • 33
  • 1
    before we continue: why do you need to prevent font-fallback? Because that's what websites are *supposed* to do, so why are you trying to fight how CSS is supposed to work, rather than writing an offline validator for your code that tells you whether or not your chosen font has gaps? – Mike 'Pomax' Kamermans Dec 12 '16 at 23:56
  • @Mike'Pomax'Kamermans I need this for a website which is used to test fonts. If a glyph isn't part of the font it should be obvious that it missing. – tobltobs Dec 13 '16 at 08:23
  • so... why not *exploit* font fallback, and fall back to [Adobe's blank font](https://github.com/adobe-fonts/adobe-blank) or [Adobe's notdef font](https://github.com/adobe-fonts/adobe-notdef)? That's literally what they were made for (typesetting validation). That said, testing glyph presence is a thing that you would reasonably do *without* any kind of front end: you just run your page code through a shaper and see if at point it goes "you fed me string X and this font I'm supposed to use has no glyph index/indices for it", no need to do that in a client. But if you must: use a blank. – Mike 'Pomax' Kamermans Dec 13 '16 at 16:38

1 Answers1

0

While working on the above problem, somebody hinted me to the Adobe NotDef font, which is pretty much what I was looking for. For some reason I wasn't able to convert the .otf of the Adobe NotDef to woff or woff2 with fontforge. Also all the online tools to create the web font files like fontsquirrel failed. To create the woff file I used sfnt2woff from the woff-tools package. For the woff2 file I used https://github.com/google/woff2.

tobltobs
  • 2,782
  • 1
  • 27
  • 33
  • 1
    Which version of Font Forge? Both Adobe blank and Adobe notdef rely on a new CMAP table, so if you're still using an old version it's a good bet it simply doesn't understand what it should be doing with that CMAP data. – Mike 'Pomax' Kamermans Dec 13 '16 at 16:42
  • FontForge has had WOFF support since 2010 so that shouldn't have been a problem, but WOFF2 support was only added June 15, 2018. – Yay295 Aug 13 '18 at 00:46