Can I use FreeType to draw text on a pixmap with arbitrary depth?
Not directly, as FreeType is a font library, not a
general-purpose graphics library or text rendering service.
However, note that the anti-aliased renderer allows you to convert a
vectorial glyph outline into a list of ‘spans’ (i.e.,
horizontal pixel segments with the same coverage) that can be
rendered through user-provided callbacks.
By providing the appropriate span callback, you can render
anti-aliased text to any kind of surface. You can also use any
colour, fill pattern or fill image if you want to. This process is
called direct rendering.
FT_Raster_Params params;
FT_Outline outline;
... load vectorial glyph in ‘outline’ ...
params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
params.gray_spans = (FT_Raster_Span_Func)your_own_span_function;
params.user = your_own_data_pointer;
error = FT_Outline_Render( library, &outline, ¶ms );
Note that direct rendering is not available with
monochrome output, as the current renderer uses a two-pass algorithm
to generate glyphs with correct drop-out control.
How can I set the colour of text rendered by FreeType?
Basically, you can't do that, because FreeType is simply a font
library. In general, you need to use your favorite graphics library
to draw the FreeType glyphs with the appropriate colour.
Note that for anti-aliased glyphs, you can ‘set the
colour’ by using direct rendering as described in this answer.
I set the pixel size to 8×8, but the resulting glyphs are
larger (or smaller) than that. Why?
A lot of people have difficulties to understand this topic, because
they think of glyphs as fixed-width or fixed-height
‘cells’, like those of fonts used in terminals/consoles.
This assumption is not valid with most ‘modern’ font
formats, even for bitmapped-based ones like PCF or
BDF.
Be aware that the character size that is set either
through FT_Set_Char_Size() or FT_Set_Pixel_Sizes()
isn't directly related to the dimension of the generated glyph
bitmaps!
Rather, the character size is indeed the size of an abstract
square, called the EM, used by typographers to design
fonts. Scaling two distinct fonts to the same character size, be it
expressed in points or pixels, generally results in bitmaps with
distinct dimensions!
Note that historically, the EM corresponded to the width of a
capital ‘M’ in Latin typefaces. However, later
improvements in typography led to designs that greatly deviate from
this rule. Today, it is not possible to connect the EM size to a
specific font ‘feature’ in a reliable way.
How can I compute the bounding box of a text string without
loading its glyphs before?
This is not possible in general. Reason is that hinting distorts
the glyph shape for optimal rasterization, and this process
sometimes creates outlines which have considerably different
metrics. The TrueType format provides the (optional)
‘hdmx’ table which contains device horizontal metrics
for selected pixel sizes, but even here the vertical metrics are
missing.
It is probably best to use both a glyph and a metrics cache to
avoid recomputation.
Which anti-aliasing algorithm is used by FreeType 2?
The algorithm has been specifically designed for FreeType. It is
based on ideas that were originally found in the implementation of
the libArt graphics
library to compute the exact pixel coverage of a vector
image with no sub-sampling and filtering.
However, these two implementations are radically distinct and use
vastly different models. The FreeType 2 renderer is optimized
specifically for rendering small complex shapes, like glyphs, at
very high speed while using very few memory. On the other hand,
libArt has been designed for general shape and polygon processing,
especially large ones.
The FreeType 2 anti-aliasing renderer is indeed
faster than the monochrome renderer for small character
sizes (typically <20 pixels). The reason is that the
monochrome renderer must perform two passes on the outline in order
to perform drop-out control according to the TrueType
specification.
When will FreeType 2 support OpenType?
Well, the engine already reads OpenType/CFF files perfectly.
What it doesn't do is handling ‘OpenType Layout’
tables.
FreeType 1 comes with a set of extensions that are used to
load and manage OpenType Layout tables. It even has a demonstration
program named ftstrtto to show its capabilities. However,
this code is no longer maintained, and we strongly advise to not use
it.
For FreeType 2, we have decided that the layout operations
provided through these tables are better placed in a specific
text-layout library like Pango.
|