Text Rendering

Text rendering using OpPainter

  1. Allocate an OpBitmap and get a painter from it (SVGCanvasVega::GetPainter)
  2. Render text using the painter (SVGCanvasVega::Draw*StringPainter)
  3. Copy the contents of the bitmap to a memory buffer (SVGCanvasVega::ReleasePainter)
  4. Create a VEGAFill from the memory buffer (SVGCanvasVega::ReleasePainter)
  5. Draw a rectangle filled with this fill onto the vega rendertarget (SVGCanvasVega::ReleasePainter)

Text rendering using libvega

  1. Acquire glyph data (OpFont::GetOutline)
  2. Render glyph data to the SVGCanvas (directly) using the DrawPath method

TWEAK_SVG_FIX_BROKEN_PAINTER_ALPHA

The purpose of the mentioned tweak is to allow platforms where the painters don't produce alpha to get antialiased text (or text at all for that matter). This has initially been used on desktop for Windows and Linux/Unix. It works by producing a greyscale mask (by drawing with white on black) which is then used to draw through to produce the text in the selected color (although the code does not do it exactly like that).

With the tweak TWEAK_SVG_FIX_BROKEN_PAINTER_ALPHA set to YES, the OpPainter-case (see above) changes into:

  1. ...
  2. Render white text onto black background using the painter
  3. Copy the contents of the bitmap to a memory buffer, and apply color
  4. ...

The most interresting part of code related to the above tweak is the SVGCanvasVega::FixupPainterAlpha function.

Notes for platform implementors

Check that the text rendered with the painter has a valid alpha channel - if it doesn't (you'll probably either see a colored square or nothing), try setting the tweak to YES.

Below are some simple testcases. The first should render text with the painter, and the second should render text without the painter (by using the hint text-rendering="geometricPrecision").

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 360"> <g font-size="30" text-anchor="middle"> <text x="240" y="200">Test text</text> </g> <rect x="1" y="1" width="478" height="358" fill="none" stroke="blue"/> </svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 360"> <g font-size="30" text-anchor="middle" text-rendering="geometricPrecision"> <text x="240" y="200">Test vector-text</text> </g> <rect x="1" y="1" width="478" height="358" fill="none" stroke="blue"/> </svg>