Skip to content

upipe_freetype: styled text pages - #1271

Open
kierank wants to merge 6 commits into
Upipe:masterfrom
kierank:freetype-upstream
Open

kierank wants to merge 6 commits into
Upipe:masterfrom
kierank:freetype-upstream

Conversation

@kierank

@kierank kierank commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

upipe_freetype could only render one string in one colour and size. This series lets it render subtitle and caption pages: regions positioned on a canvas, each holding styled runs (colour, background, size, bold, italic, underline, character edges, one of eight fonts) that the pipe measures, wraps and aligns. The page is described with the new uref_text attributes, and the output picture is cropped to what is drawn, with the distance to each edge of the canvas given as pic paddings, as the DVB subtitle decoder already does. The single string path is unchanged.

  • Patches 1-3 are fixes to the existing pipe found on the way: glyph bitmaps indexed by width instead of pitch, a wrong bounding box position, and reads of uninitialised values.
  • Patch 4 adds the page renderer.
  • Patch 5 makes compositing several times faster without changing its output (byte identical against the previous code over 130k random draws in six plane layouts).
  • Patch 6 adds character edges and a font table, enough to render CTA-708 pens.

The renderer read the coverage of a glyph at buffer[j * width + i], which is
only where it is when the bitmap has no padding between its lines.  Both the
small bitmap cache and a rendered glyph say what the distance between two
lines is, so use it: with a padded bitmap the glyph was sheared by a pixel per
line and read past the end of the buffer on the last ones.
The left of the box was taken from the first glyph under "if (!i)", but the
string index is advanced before the test, so it was never zero and the box
always started at zero however far into the line the first glyph sat.  Take it
from the first glyph that is actually looked up.

The height was assigned twice, the second time without the guard the first one
had, which made the guard do nothing; drop the second one.
The scaler handed to the cache manager is a stack structure with two fields
left unset; the manager keeps a copy of it.  They are the resolutions, which
it only reads when the sizes are not in pixels, so nothing came of it, but
zero them rather than hash and store whatever was on the stack.

FT_Get_Advance leaves its output untouched when it fails, and the result was
added to the total either way.
The pipe rendered one string, in one colour, at one size, on a baseline the
application picked.  A subtitle page is a handful of regions, each holding
runs with their own colour, size and background, wrapped and aligned inside
the region: describe one with the uref_text attributes and let the renderer
measure the runs, break them into lines and lay them out.

The picture is cropped to what is drawn and the distance to each edge of the
canvas goes on the flow definition as a pic padding, the convention the DVB
subtitle decoder already uses: downstream rebuilds the canvas from the size
and the paddings, so two lines of subtitle are not rescaled and alpha blended
over a whole frame-sized picture on every frame.  Nothing is advertised until
there is something to describe: a first negotiation at the size of the canvas
settles on a chain that does not scale, and no later crop gets a chance to
undo it.

Compositing divides by the resulting alpha rather than mixing the source
colour straight in.  On the opaque background of the single string path the
two are the same expression, but a page starts out transparent, and mixing in
the colour of a transparent pixel darkens the edge of every glyph.  Neither of
the two ends of that needs the division: nothing underneath leaves the source
colour as it stands, and an opaque result is a mix by 255, which is a pair of
shifts.  Subsampling is a shift too, so a pixel costs no division at all
unless it is translucent over translucent.

Only one font file is configured, so bold and italic are synthesized by
emboldening and shearing a copy of the cached outline; the plain style still
comes from the small bitmap cache.  Glyph blitting honours the pitch of the
bitmap, which is not its width for a glyph the small bitmap cache would not
hold.

A page is bounded, so the scratch it is laid out in comes with the pipe and
rendering one allocates nothing.  The smallest picture is the floor the DVB
subtitle decoder uses for an empty subtitle: a couple of pixels is not a legal
size once the chroma is subsampled.
Every pixel of every glyph and background went through a call that checked
it against the picture and worked out the offset of each plane again, and
whenever the canvas underneath was neither transparent nor opaque, which is
the case for text on a translucent background, it divided three times to
scale the blend back by the alpha it came to.

Rectangles and glyphs are now clipped once and blended a line at a time from
line pointers worked out once.  The division by the resulting alpha is a
multiply by a 2^24 reciprocal from a table, exact for every value a blend can
come to, and an opaque rectangle just sets the samples.  The 4:2:2 planar
layout with alpha the pages are rendered in gets a loop of its own without
the per plane checks.

The output is unchanged: against the per pixel version, 130738 random rects
and glyphs over 20000 random canvases in six plane layouts (yuva422p,
yuva420p, y + u8v8 + alpha, yuv422p, y only, yuva444p), clipped at every
edge, came out byte identical, and the reciprocal matches the division for
every value up to 255 times every alpha.  Four lines of 60 glyphs on a
1920x200 yuva422p crop take 0.68 ms instead of 1.56 ms on translucent line
backgrounds and 0.49 ms instead of 1.63 ms on opaque ones (laptop, gcc -O2).
CTA-708 pens carry a character edge (raised, depressed, uniform, left or
right drop shadow) with its colour, and one of eight font styles, none of
which a page could describe.  Add the run_edge, run_edge_color and run_font
attributes.

upipe_freetype draws an edge as offset copies of the glyph, after the
backgrounds and before the glyphs: eight around it for a uniform edge, one
for a drop shadow, a light and a dark one for raised and depressed.  The run
font index selects one of the fonts set with the font0 to font7 options,
falling back on font.  Each font file is a face id of its own in the cache,
and a face is removed from the cache before its path is freed, as a new path
may be allocated at the same address.

A page that sets none of the new attributes renders as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant