TrueType font that just contains a bitmap font

Options
Is it possible to create a TrueType font that just contains a bitmap font and no vector outlines? I have been attempting to create one using a bdf font in FontForge on Ubuntu, but it doesn't seem to be working. The generated TTF file gives an illegible preview. The bdf font is here. I am hoping to find a way to somehow use it in Visual Studio Code on Ubuntu.
«1

Comments

  • Jens Kutilek
    Jens Kutilek Posts: 346
    Options
    It should be possible. Is the display of embedded bitmaps enabled in your Linux environment?
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    but it doesn't seem to be working. The generated TTF file gives an illegible preview

    I imagine the preview is going to be at some arbitrary size… which might not be the size at which the bitmap(s) are optimized for.

    Worse, many modern rendering environments don’t even support embedded bitmaps in TTF, these days. In some cases the embedded bitmaps get removed before rasterizing, for example with Chrome and Firefox, they run the OpenType Sanitizer which specifically removes the EBDT and EBLC tables (that is, the bitmaps).

    VS Code uses Electron and therefore Chromium (not Chrome) for rendering, and Chromium uses Blink. I expect Blink uses either FreeType (old!) or Harfbuzz (newer evolution of the same rendering code). Both FreeType and Harfbuzz supported bitmap-only TrueType fonts last time I checked, but that does not mean that every client environment does.
  • pthomas505
    pthomas505 Posts: 54
    Options
    I tried generating an OTB font from the BDF using "fonttosfnt -b -c -g 2 -m 2 -o mono_size1.otb mono_size1.bdf" according to https://askubuntu.com/questions/1244175/how-can-i-activate-bitmap-fonts-on-20-04. I used the version of fonttosfnt compiled from the source in https://gitlab.freedesktop.org/xorg/app/fonttosfnt.git.

    I think the letter spacing is being set incorrectly. This is the preview of the OTB font:



  • Simon Cozens
    Simon Cozens Posts: 727
    Options
    The OpenType spec does not (sadly) say so explicitly, but it is heavily implied that there must be at least one outline format:

    Other data provide descriptions of glyphs as TrueType or Compact Font Format (CFF) outlines. Still other data can provide monochromatic or color bitmaps or SVG documents as alternate glyph descriptions. [my emphasis]

    OpenType fonts that contain TrueType outlines should use the value of 0x00010000 for the sfntVersion. OpenType fonts containing CFF data (version 1 or 2) should use 0x4F54544F ('OTTO', when re-interpreted as a Tag) for sfntVersion. [So which magic number would you use for one which contains neither?]

    Whether TrueType or CFF outlines are used in an OpenType font...

    OpenType fonts may also contain bitmaps of glyphs, in addition to outlines. [my emphasis]

    All from https://learn.microsoft.com/en-us/typography/opentype/spec/otff

  • pthomas505
    pthomas505 Posts: 54
    Options
    Does the same hold true for an OTB font? It is too bad I can't seem to use the BDF font directly.
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    “.otb” is an “OpenType bitmap font.” Good description here: https://fontforge.org/docs/techref/bitmaponlysfnt.html along with some other info on bitmap fonts in SFNT wrappers.

    “.otb” is an invention of the X Consortium; you could call it an unofficial extension as it is not part of the OpenType spec. I would not expect non-Linux environments to support it, but it might work for you?
  • pthomas505
    pthomas505 Posts: 54
    Options
    Yes, I am primarily interested in using the font on Linux.
  • Jens Kutilek
    Jens Kutilek Posts: 346
    Options
    I've tried to convert your BDF font with the system-installed version of fonttosfnt on Debian Bookworm, but the resulting OTB looks severely broken (from a technical standpoint, I can't even get a preview of the font). It has no horizontal glyph metrics, no cmap entries, weird vertical metrics. The bitmap tables are also empty. Maybe I'm doing something wrong.

    Maybe you could share your own OTB here to see if it is any different? It seems the distorted shapes you get in the preview at least have a relation to the letters in the original font ;)
  • pthomas505
    pthomas505 Posts: 54
    Options
    I just changed the extension to .txt in order to be able to upload it, it just needs to be renamed back to .otb.
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    Sadly, you can’t do that with binary files.
    (OK, I mean, you can DO it, but the file content will not survive the experience intact.)
  • pthomas505
    pthomas505 Posts: 54
    edited March 3
    Options
    I'm not sure I understand. I didn't open it and save it as a text file. I just renamed it in the filesystem. I tried downloading it and changing the extension back and was still able to open it.
  • pthomas505
    pthomas505 Posts: 54
    edited March 3
    Options
    I just tried regenerating the .otb file, making a copy, changing the extension of the copy to .txt, and doing a byte comparison of the two using 'cmp'. It didn't report any change.
  • pthomas505
    pthomas505 Posts: 54
    edited March 3
    Options
    *deleted*
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    I stand corrected, then! Either the OTB format isn’t binary file, or file handling has become much more robust than it used to be.  :)
  • pthomas505
    pthomas505 Posts: 54
    Options
    I think it is a binary file, so must have become more robust. :smile:
  • pthomas505
    pthomas505 Posts: 54
    Options
    I "cheated" and wrote a script to translate a bdf to a ufo by creating a square vector contour for each black pixel. I then used FontForge to remove the overlaps and generate a ttf. Is there a tool I could use to autohint the ttf in both the horizontal and vertical directions? It seems that Visual TrueType only autohints in the vertical direction?
  • pthomas505
    pthomas505 Posts: 54
    Options
    One issue is that it has self intersecting contours on the corners of diagonally adjacent "pixels". I'm not sure of a good way to generate it that avoids that.
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    Seems like a problem in your overlap removal? It is at least possible that some other tool will do better. If you posted the UFO, pre–overlap-removal, people could try out other tools on it?
  • pthomas505
    pthomas505 Posts: 54
    Options
    I think it is just a result of the geometry of tracing a staircase pattern. Each pixel at the connected corners of adjacent "steps" are going to overlap. One option might be to remove a portion of each corner in the outline and put it back in hinting?
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    I guess it depends on your definition of “overlap”—two pixels that are corner-adjacent will share a corner coordinate. There are multiple ways to trace that outline, depending on whether the algorithm prefers continuous outlines vs more separate contours.

    In the rather specialized case of pixel fonts, having separate contours when two pixels only touch at a corner is almost certainly preferable, IMO. I am suggesting one could try different apps to see whether one or another does that.
  • pthomas505
    pthomas505 Posts: 54
    Options
    I see.
  • pthomas505
    pthomas505 Posts: 54
    Options
    This should be the ufo before removing the overlap.
  • pthomas505
    pthomas505 Posts: 54
    edited March 9
    Options
    I was able to create a script to generate the separate contours. It doesn't handle fully enclosed interior points like in the hash symbol, but that seems to be a rare issue.

  • pthomas505
    pthomas505 Posts: 54
    Options
    The generated fonts are simple enough that I might be able to create a script to autohint them. It would be nice to find an existing tool though, if anyone knows of one? I tried ttfautohint, but I wasn't able to get it to do this, probably because I don't understand the options. I think I just need to link each stem in sequence starting from the character origin and align the remaining points? This seemed to work when I did it manually for the 'x' glyph.


  • pthomas505
    pthomas505 Posts: 54
    Options
    I guess the hinting might not be quite as simple as that.
  • Thomas Phinney
    Thomas Phinney Posts: 2,800
    Options
    Not sure it matters, but FontLab wasn’t able to import the UFO.
  • pthomas505
    pthomas505 Posts: 54
    Options
    :(
  • pthomas505
    pthomas505 Posts: 54
    Options
    Is there a way to hint this so that every "pixel" scales in size uniformly, like the hinted "x" does above?
  • pthomas505
    pthomas505 Posts: 54
    edited March 9
    Options
    I think I need to scale every coordinate dependent on the ppem, but I'm not certain of the exact formula.