Skip to content

Font Rendering

opencode edited this page Nov 15, 2026 · 3 revisions

Font Rendering

JNode's dual font rendering system: anti-aliased BDF bitmaps and a TrueType outline renderer behind a plugin FontManager.

Overview

JNode provides two font pipelines:

  • BDF — bitmap fonts parsed by a JavaCC-generated parser, used for the default UI and console fonts.
  • TrueType (TTF) — scalable outlines rasterized through GlyphRenderer and composited to a Surface.

Both are coordinated by DefaultFontManager, which discovers FontProvider plugins through the org.jnode.font.providers extension point. The renderer is selected by the jnode.font.renderer property; the default remains bdf even though TTF is more feature-complete.

The 2026-11 font work added anti-aliased DejaVu BDF faces with style/size-aware selection and substantially fixed the TTF pipeline (cmap format 4, metrics, raster layout, glyph vector routing and CP437 box glyphs).

Key Components

Class / File Purpose
gui/src/awt/org/jnode/awt/font/FontManager.java Central interface coordinating providers
gui/src/awt/org/jnode/awt/font/def/DefaultFontManager.java Plugin-based provider discovery; reads jnode.font.renderer (default bdf)
gui/src/font/org/jnode/font/bdf/BDFParser.java JavaCC-generated BDF parser
gui/src/font/org/jnode/font/bdf/BDFFontContainer.java Glyphs, metadata and glyph-name → code mapping
gui/src/font/org/jnode/font/bdf/BDFGlyph.java BDF glyph bitmap and decode
gui/src/awt/org/jnode/awt/font/bdf/BDFFontProvider.java Loads system fonts; bestFont() style/size scoring
gui/src/awt/org/jnode/awt/font/bdf/BDFFontPeer.java Peer; delegates selection and metrics to bestFont()
gui/src/awt/org/jnode/awt/font/bdf/BDFTextRenderer.java Blits BDF glyphs with alpha
gui/src/awt/org/jnode/awt/font/truetype/TTFFontProvider.java Loads system TTF fonts; bold/mono matching
gui/src/awt/org/jnode/awt/font/truetype/TTFFontPeer.java Metrics, glyph vector, canDisplay
gui/src/awt/org/jnode/awt/font/truetype/TTFTextRenderer.java Glyph placement and box-glyph drawing
gui/src/awt/org/jnode/awt/font/truetype/BoxGlyphMapping.java CP437 → Unicode box glyph remap
gui/src/awt/org/jnode/awt/font/renderer/GlyphRenderer.java Outline → anti-aliased 8-bit raster
gui/src/awt/org/jnode/awt/font/truetype/tables/*.java Head, glyph, cmap, hhea, hmtx, loca, maxp, name, OS/2, post
gui/fonts/*.bdf, gui/fonts/*.ttf DejaVu Sans / Sans Mono, regular and bold

How It Works

Provider selection

DefaultFontManager enumerates providers and asks each for a TextRenderer/FontMetrics. Provider order comes from the plugin registry, not from the requested family. jnode.font.renderer=bdf (the default) chooses BDF; -Djnode.font.renderer=ttf chooses TrueType.

BDF rendering and selection

BDFFontProvider.SYSTEM_FONTS lists the anti-aliased DejaVu faces first:

DejaVuSans-{10,12,14}.bdf
DejaVuSans-Bold-{10,12,14}.bdf
DejaVuSansMono-{10,12,14}.bdf
DejaVuSansMono-Bold-{10,12,14}.bdf

These are 4-bit BDFs (SIZE n 72 72 4), so each pixel carries 16 alpha levels. The old Vera-*, VeraMono-12-8, 6x12_FixedMedium-12 and VGAMedium-19 faces remain as fallbacks.

BDFFontProvider.bestFont(Font) scores every loaded container:

  • base score: abs(container.getSize() - font.getSize())
  • 1 << 20 for each mismatch: bold style, italic style, mono family

The first score-0 candidate wins; otherwise the minimum score is returned. A request is treated as mono when the family contains mono, fixed or dialoginput. Style comes from the XLFD weight/slant decoded by BDFParser. BDFFontPeer uses bestFont() for canDisplay, getFontMetrics, getGlyphName, getLineMetrics, getMaxCharBounds, getNumGlyphs, getPostScriptName and getStringBounds.

BDFTextRenderer.render() skips glyphs with BBX height <= 0 but still advances by DWIDTH, preventing a divide-by-zero and preserving spacing for blank glyphs.

gui/src/font/org/jnode/font/bdf/mappings.properties adds box218…box254 mappings to Unicode box-drawing characters (U+00DA, U+00C0, …). BDFFontContainer.setGlyphs() keys the mapper by lookup(glyphName), so CP437 box codes resolve to those glyphs in BDF mode.

TTF rendering

Contrary to the commit title ("fixed point TTF font renderer"), the renderer still uses double scaling:

double scale = fontSize / headTable.unitsPerEm;

Glyph advances are rounded with (int)(scale * advance + 0.5), matching TTFFontMetrics.charWidth(). GlyphRenderer.MASTER_HEIGHT remains 128.0.

Key fixes in the 2026-11 pass:

  • CMapTable.TableFormat4.getGlyphIndex() implements segment search, idDelta, idRangeOffset indirection and the trailing glyph-id array.
  • HorizontalMetricsTable.getAdvanceWidth() clamps indices past the advance-width array to the last entry; getLeftSideBearing() falls through to leftSideBearing2.
  • TableClass.create() uses a direct switch instead of reflective getDeclaredConstructor/setAccessible.
  • TTFFontPeer.layoutGlyphVector() returns org.jnode.awt.font.TGlyphVector, routing text through the provider renderer instead of StandardGlyphVector.
  • GlyphRenderer.createRaster() builds a plain single-band 8-bit DataBufferByte raster (no GRAY ICC colorspace).
  • GlyphRenderer.createMaster() flattens PathIterator contours (quadratics in 4 steps, cubics in 7), closes them and fills with a scanline non-zero winding rule, top-down.
  • AbstractTextRenderer.createAlphaRaster() allocates fontSize * 2 in both dimensions for tall glyphs.
  • TTFTextRenderer.render() places the raster at GlyphRenderer.getMinLocation() (ink origin), not the font header bbox; destinations round with +0.5.
  • TTFFontMetrics rounds ascent/descent/advances; getLeading() is 1 and getHeight() is ascent+descent+leading.

TTFFontProvider.SYSTEM_FONTS is now DejaVuSans.ttf, DejaVuSans-Bold.ttf, DejaVuSansMono.ttf, DejaVuSansMono-Bold.ttf. bestFont() matches bold/mono/family but not size, because TTF is scalable.

TTF box glyphs

BoxGlyphMapping remaps CP437 codes 218, 192, 191, 217, 195, 180, 193, 194, 196, 179, 197, 178 and 254 to Unicode box characters (\u250C…\u25A0). TTFTextRenderer.drawBoxGlyph() draws them itself:

  • cell width max(2, advance)
  • height max(2, ascent + descent)
  • 178 is a checkerboard (value 8); 254 is solid 0xFF
  • corners and tees stroke only toward the joined panel

This gives the Swing console continuous box-drawing even when the TTF has no matching glyph.

Glyph caching

RenderCache holds GlyphRenderer instances keyed by glyph + size. Each renderer precomputes a master shape/SAT at MASTER_HEIGHT = 128, enabling scaling to any requested size.

Gotchas

  • Default is still BDF (jnode.font.renderer=bdf). Use -Djnode.font.renderer=ttf to exercise the TTF path.
  • BDF glyph abstraction is not unified with TTF. BDF uses org.jnode.font.bdf.BDFGlyph; TTF returns org.jnode.awt.font.spi.Glyph.
  • BDF peer creation uses match() (always true). Actual selection happens later in bestFont().
  • No 19 px DejaVu Mono. A 19 px mono request selects DejaVuSansMono-14 (size penalty 5) rather than VGAMedium-19 (mono penalty 1<<20). VGAMedium-19.bdf also declares SIZE 18.
  • BDF box mappings can shadow Latin-1. U+00DA/U+00C0 etc. resolve to box glyphs in the DejaVu BDFs.
  • TTF getMaxCharSize() returns raw font units, not scaled pixels, so getMaxCharBounds() looks huge relative to rendered text.
  • TTF peer stubs: canDisplay() always true, getNumGlyphs() 0, getGlyphName() empty.
  • TTF descriptors still grant accessDeclaredMembers although TableClass no longer reflects.
  • No hinting. Anti-aliasing relies on the summed-area raster, not grid fitting.

Related Pages

Clone this wiki locally