/* FreeType version older than 2.11 does not have the FT_RENDER_MODE_SDF enum value in FT_Render_Mode */ #if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 11) #define HAVE_FT_RENDER_MODE_SDF 1 #endif
cairo_bool_t from_face; /* was the FT_Face provided by user? */
FT_Face face; /* provided or cached face */ void *face_context;
/* only set if from_face is false */ char *filename; int id;
/* We temporarily scale the unscaled font as needed */
cairo_bool_t have_scale;
cairo_matrix_t current_scale; double x_scale; /* Extracted X scale factor */ double y_scale; /* Extracted Y scale factor */
cairo_bool_t have_shape; /* true if the current scale has a non-scale component*/
cairo_matrix_t current_shape;
FT_Matrix Current_Shape;
unsignedint have_color_set : 1; unsignedint have_color : 1; /* true if the font contains color glyphs */
FT_Fixed *variations; /* variation settings that FT_Face came */ unsignedint num_palettes;
cairo_mutex_t mutex; int lock_count;
cairo_ft_font_face_t *faces; /* Linked list of faces for this font */
};
#if CAIRO_HAS_FC_FONT
FcPattern *pattern; /* if pattern is set, the above fields will be NULL */
cairo_font_face_t *resolved_font_face;
FcConfig *resolved_config; #endif
};
/* This function is only intended to be called from *_cairo_ft_unscaled_font_map_lock.Sowe'llcrashifwecan
* detect some other call path. */
assert (cairo_ft_unscaled_font_map == NULL);
/* Return existing unscaled font if it exists in the hash table. */
unscaled = _cairo_hash_table_lookup (font_map->hash_table,
&key.base.hash_entry); if (unscaled != NULL) {
_cairo_unscaled_font_reference (&unscaled->base); goto DONE;
}
/* Otherwise create it and insert into hash table. */
unscaled = _cairo_calloc (sizeof (cairo_ft_unscaled_font_t)); if (unlikely (unscaled == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY); goto UNWIND_FONT_MAP_LOCK;
}
status = _cairo_ft_unscaled_font_init (unscaled, from_face, filename, id, font_face, face_context); if (unlikely (status)) goto UNWIND_UNSCALED_MALLOC;
assert (unscaled->base.hash_entry.hash == key.base.hash_entry.hash);
status = _cairo_hash_table_insert (font_map->hash_table,
&unscaled->base.hash_entry); if (unlikely (status)) goto UNWIND_UNSCALED_FONT_INIT;
ret = FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &font_face); if (ret == FcResultMatch) goto DONE; if (ret == FcResultOutOfMemory) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
ret = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **) &filename); if (ret == FcResultOutOfMemory) return _cairo_error (CAIRO_STATUS_NO_MEMORY); if (ret == FcResultMatch) { if (access (filename, R_OK) == 0) { /* If FC_INDEX is not set, we just use 0 */
ret = FcPatternGetInteger (pattern, FC_INDEX, 0, &id); if (ret == FcResultOutOfMemory) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
font_map = _cairo_ft_unscaled_font_map_lock (); /* All created objects must have been mapped in the font map. */
assert (font_map != NULL);
if (! _cairo_reference_count_dec_and_test (&unscaled->base.ref_count)) { /* somebody recreated the font whilst we waited for the lock */
_cairo_ft_unscaled_font_map_unlock (); returnFALSE;
}
/* Ensures that an unscaled font has a face object. If we exceed *MAX_OPEN_FACES,trytoclosesome. * *Thisdiffersfrom_cairo_ft_scaled_font_lock_faceinthatitdoesn't *setthescaleontheface,butjustreturnsitatthelastscale.
*/ static cairo_warn FT_Face
_cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
{
cairo_ft_unscaled_font_map_t *font_map;
FT_Face face = NULL;
FT_Error error;
if (unscaled->face_context) { if (!mozilla_LockSharedFTFace (unscaled->face_context, unscaled)) {
unscaled->have_scale = FALSE;
}
} else {
CAIRO_FT_LOCK (unscaled);
}
unscaled->lock_count++;
if (unscaled->face) return unscaled->face;
/* If this unscaled font was created from an FT_Face then we just
* returned it above. */
assert (!unscaled->from_face);
/* The font matrix has x and y "scale" components which we extract and *useascharacterscalevalues.Theseinfluencethewayfreetype *chooseshints,aswellasselectingdifferentbitmapsin *hand-renderedfonts.Wealsocopythenormalizedmatrixto *freetype'stransformation.
*/
status = _cairo_matrix_compute_basis_scale_factors (scale,
&x_scale, &y_scale, 1); if (unlikely (status)) return status;
/* FreeType docs say this about x_scale and y_scale: *"Acharacterwidthorheightsmallerthan1ptissetto1pt;" *So,wecapthemfrombelowat1.0andlettheFTtransform
* take care of sub-1.0 scaling. */ if (x_scale < 1.0)
x_scale = 1.0; if (y_scale < 1.0)
y_scale = 1.0;
/* Temporarily scales an unscaled font to the give scale. We catch *scalingtothesamesize,sincechangingaFT_Faceisexpensive.
*/ static cairo_status_t
_cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
cairo_matrix_t *scale)
{
cairo_status_t status;
cairo_ft_font_transform_t sf;
FT_Matrix mat;
FT_Error error;
case FT_PIXEL_MODE_GRAY: if (mode == FT_RENDER_MODE_LCD ||
mode == FT_RENDER_MODE_LCD_V)
{ /* each pixel is replicated into a 32-bit ARGB value */
pitch = width * 4;
} break;
case FT_PIXEL_MODE_LCD: if (mode != FT_RENDER_MODE_LCD) return -1;
/* horz pixel triplets are packed into 32-bit ARGB values */
width /= 3;
pitch = width * 4; break;
case FT_PIXEL_MODE_LCD_V: if (mode != FT_RENDER_MODE_LCD_V) return -1;
/* vert pixel triplets are packed into 32-bit ARGB values */
height /= 3;
pitch = width * 4; break;
case FT_PIXEL_MODE_BGRA: /* each pixel is replicated into a 32-bit ARGB value */
pitch = width * 4; break;
default: /* unsupported source format */ return -1;
}
while (count--) {
*d = CAIRO_BITSWAP8 (*d);
d++;
}
} #endif
format = CAIRO_FORMAT_A1; break;
case FT_PIXEL_MODE_LCD: case FT_PIXEL_MODE_LCD_V: case FT_PIXEL_MODE_GRAY: if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL ||
bitmap->pixel_mode == FT_PIXEL_MODE_GRAY)
{
stride = bitmap->pitch;
/* We don't support stride not multiple of 4. */ if (stride & 3)
{
assert (!own_buffer); goto convert;
}
if (own_buffer) {
data = bitmap->buffer;
} else {
data = _cairo_malloc_ab (height, stride); if (!data) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (!_cairo_is_little_endian ())
{ /* Byteswap. */ unsignedint i, count = height * width;
uint32_t *p = (uint32_t *) data; for (i = 0; i < count; i++)
p[i] = bswap_32 (p[i]);
}
format = CAIRO_FORMAT_ARGB32; break; case FT_PIXEL_MODE_GRAY2: case FT_PIXEL_MODE_GRAY4:
convert: if (!own_buffer && library)
{ /* This is pretty much the only case that we can get in here. */ /* Convert to 8bit grayscale. */
stride = bitmap->pitch;
data = _cairo_malloc_ab (height, stride); if (!data) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (bitmap->num_grays != 256)
{ unsignedint x, y; unsignedint mul = 255 / (bitmap->num_grays - 1);
FT_Byte *p = bitmap->buffer; for (y = 0; y < height; y++) { for (x = 0; x < width; x++)
p[x] *= mul;
p += bitmap->pitch;
}
}
memcpy (data, bitmap->buffer, (size_t)stride * height); break;
} /* fall through */ /* These could be triggered by very rare types of TrueType fonts */ default: if (own_buffer)
free (bitmap->buffer); return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
}
switch (font_options->antialias) { case CAIRO_ANTIALIAS_NONE:
render_mode = FT_RENDER_MODE_MONO; break;
case CAIRO_ANTIALIAS_SUBPIXEL: case CAIRO_ANTIALIAS_BEST: switch (font_options->subpixel_order) { case CAIRO_SUBPIXEL_ORDER_DEFAULT: case CAIRO_SUBPIXEL_ORDER_RGB: case CAIRO_SUBPIXEL_ORDER_BGR:
render_mode = FT_RENDER_MODE_LCD; break;
case CAIRO_SUBPIXEL_ORDER_VRGB: case CAIRO_SUBPIXEL_ORDER_VBGR:
render_mode = FT_RENDER_MODE_LCD_V; break;
}
switch (font_options->lcd_filter) { case CAIRO_LCD_FILTER_NONE:
lcd_filter = FT_LCD_FILTER_NONE; break; case CAIRO_LCD_FILTER_INTRA_PIXEL:
lcd_filter = FT_LCD_FILTER_LEGACY; break; case CAIRO_LCD_FILTER_FIR3:
lcd_filter = FT_LCD_FILTER_LIGHT; break; case CAIRO_LCD_FILTER_DEFAULT: case CAIRO_LCD_FILTER_FIR5:
lcd_filter = FT_LCD_FILTER_DEFAULT; break;
}
break;
case CAIRO_ANTIALIAS_DEFAULT: case CAIRO_ANTIALIAS_GRAY: case CAIRO_ANTIALIAS_GOOD: case CAIRO_ANTIALIAS_FAST:
render_mode = FT_RENDER_MODE_NORMAL;
}
if (width * height == 0) {
cairo_format_t format; /* Looks like fb handles zero-sized images just fine */ switch (render_mode) { case FT_RENDER_MODE_MONO:
format = CAIRO_FORMAT_A1; break; case FT_RENDER_MODE_LCD: case FT_RENDER_MODE_LCD_V:
format= CAIRO_FORMAT_ARGB32; break; case FT_RENDER_MODE_LIGHT: case FT_RENDER_MODE_NORMAL: case FT_RENDER_MODE_MAX: #if HAVE_FT_RENDER_MODE_SDF case FT_RENDER_MODE_SDF: #endif default:
format = CAIRO_FORMAT_A8; break;
}
(*surface) = (cairo_image_surface_t *)
cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
pixman_image_set_component_alpha ((*surface)->pixman_image, TRUE); if ((*surface)->base.status) return (*surface)->base.status;
} else {
int bitmap_size;
switch (render_mode) { case FT_RENDER_MODE_LCD: if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
rgba = CAIRO_SUBPIXEL_ORDER_BGR; else
rgba = CAIRO_SUBPIXEL_ORDER_RGB; break;
case FT_RENDER_MODE_LCD_V: if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
rgba = CAIRO_SUBPIXEL_ORDER_VBGR; else
rgba = CAIRO_SUBPIXEL_ORDER_VRGB; break;
case FT_RENDER_MODE_MONO: case FT_RENDER_MODE_LIGHT: case FT_RENDER_MODE_NORMAL: case FT_RENDER_MODE_MAX: #if HAVE_FT_RENDER_MODE_SDF case FT_RENDER_MODE_SDF: #endif default: break;
}
/* Note: *_get_bitmap_surfacewillfreebitmap.bufferifthereisanerror
*/
status = _get_bitmap_surface (&bitmap, NULL, TRUE, font_options, surface); if (unlikely (status)) return status;
/* Note: the font's coordinate system is upside down from ours, so the *Ycoordinateofthecontrolboxneedstobenegated.Moreover,device *offsetsarepositionofglyphoriginrelativetotopleftwhilexMin *andyMaxareoffsetsoftopleftrelativetoorigin.Anothernegation.
*/
cairo_surface_set_device_offset (&(*surface)->base,
(double)-glyphslot->bitmap_left,
(double)+glyphslot->bitmap_top);
}
return CAIRO_STATUS_SUCCESS;
}
/* Converts a bitmap (or other) FT_GlyphSlot into an image */ static cairo_status_t
_render_glyph_bitmap (FT_Face face,
cairo_font_options_t *font_options,
cairo_image_surface_t **surface)
{
FT_GlyphSlot glyphslot = face->glyph;
cairo_status_t status;
FT_Error error;
/* According to the FreeType docs, glyphslot->format could be *somethingotherthanFT_GLYPH_FORMAT_OUTLINEor *FT_GLYPH_FORMAT_BITMAP.CallingFT_Render_GlyphgivesFreeType *theopportunitytoconvertsuchto *bitmap.FT_GLYPH_FORMAT_COMPOSITEwillnotbeencounteredsince *weavoidtheFT_LOAD_NO_RECURSEflag.
*/
error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL); /* XXX ignoring all other errors for now. They are not fatal, typically
* just a glyph-not-found. */ if (error == FT_Err_Out_Of_Memory) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
status = _get_bitmap_surface (&glyphslot->bitmap,
glyphslot->library, FALSE, font_options,
surface); if (unlikely (status)) return status;
/* We want to compute a transform that takes the origin *(device_x_offset,device_y_offset)to0,0,thenapplies *the"shape"portionofthefonttransform
*/
original_to_transformed = *shape;
for (i = 1; i < 4; i++) { if (x[i] < x_min)
x_min = floor (x[i]); elseif (x[i] > x_max)
x_max = ceil (x[i]); if (y[i] < y_min)
y_min = floor (y[i]); elseif (y[i] > y_max)
y_max = ceil (y[i]);
}
/* Adjust the transform so that the bounding box starts at 0,0 ... *thisgivesourfinaltransformfromoriginalbitmaptotransformed *bitmap.
*/
original_to_transformed.x0 -= x_min;
original_to_transformed.y0 -= y_min;
/* Draw the original bitmap transformed into the new bitmap
*/
_cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
status = _cairo_surface_paint (image,
CAIRO_OPERATOR_SOURCE,
&pattern.base,
NULL);
_cairo_pattern_fini (&pattern.base);
if (unlikely (status)) {
cairo_surface_destroy (image); return status;
}
/* Now update the cache entry for the new bitmap, recomputing *theoriginbasedonthefinaltransform.
*/
cairo_matrix_transform_point (&original_to_transformed,
&origin_x, &origin_y);
/* Check whether to force use of embedded bitmaps */ if (FcPatternGetBool (pattern,
FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
bitmap = FcFalse;
/* disable antialiasing if requested */ if (FcPatternGetBool (pattern,
FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
antialias = FcTrue;
if (antialias) {
cairo_subpixel_order_t subpixel_order; int lcd_filter;
/* disable hinting if requested */ if (FcPatternGetBool (pattern,
FC_HINTING, 0, &hinting) != FcResultMatch)
hinting = FcTrue;
status = _cairo_scaled_font_init (&scaled_font->base,
&font_face->base,
font_matrix, ctm, options,
&_cairo_ft_scaled_font_backend); if (unlikely (status)) goto CLEANUP_SCALED_FONT;
status = _cairo_ft_unscaled_font_set_scale (unscaled,
&scaled_font->base.scale); if (unlikely (status)) { /* This can only fail if we encounter an error with the underlying
* font, so propagate the error back to the font-face. */
_cairo_ft_unscaled_font_unlock_face (unscaled);
_cairo_unscaled_font_destroy (&unscaled->base);
free (scaled_font); return status;
}
path = _cairo_path_fixed_create (); if (!path) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
glyph = face->glyph;
/* Font glyphs have an inverted Y axis compared to cairo. */
FT_Outline_Transform (&glyph->outline, &invert_y); if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
_cairo_path_fixed_destroy (path); return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
status = _cairo_path_fixed_close_path (path); if (unlikely (status)) {
_cairo_path_fixed_destroy (path); return status;
}
if (getVarDesignCoords) {
ret = (*getVarDesignCoords) (face, ft_mm_var->num_axis, current_coords); if (ret == 0) { for (i = 0; i < ft_mm_var->num_axis; i++) { if (coords[i] != current_coords[i]) break;
} if (i == ft_mm_var->num_axis) goto done;
}
}
error = FT_Load_Glyph (face,
_cairo_scaled_glyph_index(scaled_glyph),
load_flags); /* XXX ignoring all other errors for now. They are not fatal, typically
* just a glyph-not-found. */ if (error == FT_Err_Out_Of_Memory) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* *synthesizeglyphsifrequested
*/ if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
FT_GlyphSlot_Embolden (face->glyph);
if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
FT_GlyphSlot_Oblique (face->glyph);
if (vertical_layout)
_cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, face->glyph);
if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
FT_Pos xshift, yshift;
/* Check if there is a layer that uses the foreground color */
iterator.p = NULL; while (FT_Get_Color_Glyph_Layer(face,
_cairo_scaled_glyph_index (scaled_glyph),
&layer_glyph_index,
&layer_color_index,
&iterator)) { if (layer_color_index == 0xFFFF) {
uses_foreground_color = TRUE; break;
}
}
/* Only one info type at a time handled in this function */
assert (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE || info == CAIRO_SCALED_GLYPH_INFO_SURFACE);
if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) { if (!unscaled->have_color) {
scaled_glyph->color_glyph = FALSE;
scaled_glyph->color_glyph_set = TRUE; return CAIRO_INT_STATUS_UNSUPPORTED;
}
status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
scaled_glyph,
face,
load_flags, FALSE,
vertical_layout); if (unlikely (status)) return status;
glyph = face->glyph;
if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_OUTLINE) {
status = _render_glyph_outline (face, &scaled_font->ft_options.base,
&surface);
} else {
status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
&surface); if (likely (status == CAIRO_STATUS_SUCCESS) && unscaled->have_shape) {
status = _transform_glyph_bitmap (&unscaled->current_shape,
&surface); if (unlikely (status))
cairo_surface_destroy (&surface->base);
}
}
if (unlikely (status)) return status;
if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) { /* We tried loading a color glyph and can now check if we got *acolorglyphandsetscaled_glyph->color_glyph
* accordingly */ if (pixman_image_get_format (surface->pixman_image) == PIXMAN_a8r8g8b8 &&
!pixman_image_get_component_alpha (surface->pixman_image))
{
_cairo_scaled_glyph_set_color_surface (scaled_glyph,
&scaled_font->base,
surface,
uses_foreground_color ? foreground_color : NULL);
scaled_glyph->color_glyph = TRUE;
} else { /* We didn't ask for a non-color surface, but store it
* anyway so we don't have to load it again. */
_cairo_scaled_glyph_set_surface (scaled_glyph,
&scaled_font->base,
surface);
scaled_glyph->color_glyph = FALSE;
status = CAIRO_INT_STATUS_UNSUPPORTED;
}
scaled_glyph->color_glyph_set = TRUE;
} else { /* info == CAIRO_SCALED_GLYPH_INFO_SURFACE */
_cairo_scaled_glyph_set_surface (scaled_glyph,
&scaled_font->base,
surface);
}
extent_scale = scaled_font->base.scale_inverse;
snap_x_scale = 1.0;
snap_y_scale = 1.0;
status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
&x_scale, &y_scale, 1); if (status == CAIRO_STATUS_SUCCESS) { if (x_scale == 0)
x_scale = 1; if (y_scale == 0)
y_scale = 1;
snap_x_scale = x_scale;
snap_y_scale = y_scale;
/* since glyphs are pretty much 1.0x1.0, we can reduce error by
* scaling to a larger square. say, 1024.x1024. */
fixed_scale = 1024;
x_scale /= fixed_scale;
y_scale /= fixed_scale;
/* Compute extents.x/y/width/height from recording_surface,
* in font space.
*/
status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
&bbox,
&extent_scale); if (unlikely (status)) return status;
extent_scale = scaled_font->base.scale_inverse;
snap_x_scale = 1.0;
snap_y_scale = 1.0;
status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
&x_scale, &y_scale, 1); if (status == CAIRO_STATUS_SUCCESS) { if (x_scale == 0)
x_scale = 1; if (y_scale == 0)
y_scale = 1;
snap_x_scale = x_scale;
snap_y_scale = y_scale;
/* since glyphs are pretty much 1.0x1.0, we can reduce error by
* scaling to a larger square. say, 1024.x1024. */
fixed_scale = 1024;
x_scale /= fixed_scale;
y_scale /= fixed_scale;
/* Compute extents.x/y/width/height from recording_surface,
* in font space.
*/
status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
&bbox,
&extent_scale); if (unlikely (status)) return status;
/*
* Note: Y coordinates of the horizontal bearing need to be negated.
*
* Scale metrics back to glyph space from the scaled glyph space returned
* by FreeType
*
* If we want hinted metrics but aren't asking for hinted glyphs from
* FreeType, then we need to do the metric hinting ourselves.
*/
/* _cairo_ft_scaled_glyph_init_metrics() is called once the first
* time a cairo_scaled_glyph_t is created. We first allocate the
* cairo_ft_glyph_private_t struct and determine the glyph type.
*/
/* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
* we have a special "zombie" state for the face when the unscaled font
* is still alive but there are no other references to a font face with
* the same FT_Face.
*
* We go from:
*
* font_face ------> unscaled
* <-....weak....../
*
* To:
*
* font_face <------- unscaled
*/
if (font_face->unscaled) {
CAIRO_FT_LOCK (font_face->unscaled);
static cairo_font_face_t *
_cairo_ft_font_face_get_implementation (void *abstract_face,
const cairo_matrix_t *font_matrix,
const cairo_matrix_t *ctm,
const cairo_font_options_t *options)
{
/* The handling of font options is different depending on how the
* font face was created. When the user creates a font face with
* cairo_ft_font_face_create_for_ft_face(), then the load flags
* passed in augment the load flags for the options. But for
* cairo_ft_font_face_create_for_pattern(), the load flags are
* derived from a patternwhere the user has called
* cairo_ft_font_options_substitute(), so *just* use those load
* flags and ignore the options.
*/
/* If we have an unresolved pattern, resolve it and create
* unscaled font. Otherwise, use the ones stored in font_face.
*/ if (font_face->pattern) {
cairo_font_face_t *resolved;
/* Cache the resolved font whilst the FcConfig remains consistent. */
resolved = font_face->resolved_font_face; if (resolved != NULL) { if (! FcInitBringUptoDate ()) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY); return (cairo_font_face_t *) &_cairo_font_face_nil;
}
if (font_face->resolved_config == FcConfigGetCurrent ()) return cairo_font_face_reference (resolved);
/* Looked for an existing matching font face */
for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
font_face;
prev_font_face = &font_face->next, font_face = font_face->next)
{ if (font_face->ft_options.load_flags == ft_options->load_flags &&
font_face->ft_options.synth_flags == ft_options->synth_flags &&
cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
{ if (font_face->base.status) {
/* The font_face has been left in an error state, abandon it. */
*prev_font_face = font_face->next;
break;
}
if (font_face->unscaled == NULL) {
/* Resurrect this "zombie" font_face (from
* _cairo_ft_font_face_destroy), switching its unscaled_font
* from owner to ownee. */
font_face->unscaled = unscaled;
_cairo_unscaled_font_reference (&unscaled->base);
} else {
cairo_font_face_reference (&font_face->base);
}
/* No match found, create a new one */
font_face = _cairo_calloc (sizeof (cairo_ft_font_face_t)); if (unlikely (!font_face)) {
CAIRO_FT_UNLOCK (unscaled);
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY); return (cairo_font_face_t *)&_cairo_font_face_nil;
}
/**
* cairo_ft_font_options_substitute:
* @options: a #cairo_font_options_t object
* @pattern: an existing #FcPattern
*
* Add options to a #FcPattern based on a #cairo_font_options_t font
* options object. Options that are already in the pattern, are not overridden,
* so you should call this function after calling FcConfigSubstitute() (the
* user's settings should override options based on the surface type), but
* before calling FcDefaultSubstitute().
*
* Since: 1.0
**/
void
cairo_ft_font_options_substitute (const cairo_font_options_t *options,
FcPattern *pattern)
{ if (cairo_font_options_status ((cairo_font_options_t *) options)) return;
status = _cairo_ft_font_options_substitute (font_options, pattern); if (status) {
font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
goto FREE_PATTERN;
}
FcDefaultSubstitute (pattern);
status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled); if (unlikely (status)) {
font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
goto FREE_PATTERN;
}
if (unscaled == NULL) {
resolved = FcFontMatch (NULL, pattern, &result); if (!resolved) {
/* We failed to find any font. Substitute twin so that the user can
* see something (and hopefully recognise that the font is missing)
* and not just receive a NO_MEMORY error during rendering.
*/
font_face = _cairo_font_face_twin_create_fallback ();
goto FREE_PATTERN;
}
FREE_RESOLVED: if (resolved != pattern)
FcPatternDestroy (resolved);
FREE_PATTERN:
FcPatternDestroy (pattern);
return font_face;
}
/**
* cairo_ft_font_face_create_for_pattern:
* @pattern: A fontconfig pattern. Cairo makes a copy of the pattern
* if it needs to. You are free to modify or free @pattern after this call.
*
* Creates a new font face for the FreeType font backend based on a
* fontconfig pattern. This font can then be used with
* cairo_set_font_face() or cairo_scaled_font_create(). The
* #cairo_scaled_font_t returned from cairo_scaled_font_create() is
* also for the FreeType backend and can be used with functions such
* as cairo_ft_scaled_font_lock_face().
*
* Font rendering options are represented both here and when you
* call cairo_scaled_font_create(). Font options that have a representation
* in a #FcPattern must be passed in here; to modify #FcPattern
* appropriately to reflect the options in a #cairo_font_options_t, call
* cairo_ft_font_options_substitute().
*
* The pattern's FC_FT_FACE element is inspected first and if that is set,
* that will be the FreeType font face associated with the returned cairo
* font face. Otherwise the FC_FILE element is checked. If it's set,
* that and the value of the FC_INDEX element (defaults to zero) of @pattern
* are used to load a font face from file.
*
* If both steps from the previous paragraph fails, @pattern will be passed
* to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
* and the resulting font pattern is used.
*
* If the FC_FT_FACE element of @pattern is set, the user is responsible
* for making sure that the referenced FT_Face remains valid for the life
* time of the returned #cairo_font_face_t. See
* cairo_ft_font_face_create_for_ft_face() for an example of how to couple
* the life time of the FT_Face to that of the cairo font-face.
*
* Return value: a newly created #cairo_font_face_t. Free with
* cairo_font_face_destroy() when you are done using it.
*
* Since: 1.0
**/
cairo_font_face_t *
cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
{
cairo_ft_unscaled_font_t *unscaled;
cairo_font_face_t *font_face;
cairo_ft_options_t ft_options;
cairo_status_t status;
status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled); if (unlikely (status)) { if (status == CAIRO_STATUS_FILE_NOT_FOUND) return (cairo_font_face_t *) &_cairo_font_face_nil_file_not_found; else return (cairo_font_face_t *) &_cairo_font_face_nil;
} if (unlikely (unscaled == NULL)) {
/* Store the pattern. We will resolve it and create unscaled
* font when creating scaled fonts */ return _cairo_ft_font_face_create_for_pattern (pattern);
}
/**
* cairo_ft_font_face_create_for_ft_face:
* @face: A FreeType face object, already opened. This must
* be kept around until the face's ref_count drops to
* zero and it is freed. Since the face may be referenced
* internally to Cairo, the best way to determine when it
* is safe to free the face is to pass a
* #cairo_destroy_func_t to cairo_font_face_set_user_data()
* @load_flags: flags to pass to FT_Load_Glyph when loading
* glyphs from the font. These flags are OR'ed together with
* the flags derived from the #cairo_font_options_t passed
* to cairo_scaled_font_create(), so only a few values such
* as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
* are useful. You should not pass any of the flags affecting
* the load target, such as %FT_LOAD_TARGET_LIGHT.
*
* Creates a new font face for the FreeType font backend from a
* pre-opened FreeType face. This font can then be used with
* cairo_set_font_face() or cairo_scaled_font_create(). The
* #cairo_scaled_font_t returned from cairo_scaled_font_create() is
* also for the FreeType backend and can be used with functions such
* as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
* to the FT_Face alive in a font-cache and the exact lifetime of the reference
* depends highly upon the exact usage pattern and is subject to external
* factors. You must not call FT_Done_Face() before the last reference to the
* #cairo_font_face_t has been dropped.
*
* As an example, below is how one might correctly couple the lifetime of
* the FreeType face object to the #cairo_font_face_t.
*
* <informalexample><programlisting>
* static const cairo_user_data_key_t key;
*
* font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
* status = cairo_font_face_set_user_data (font_face, &key,
* ft_face, (cairo_destroy_func_t) FT_Done_Face);
* if (status) {
* cairo_font_face_destroy (font_face);
* FT_Done_Face (ft_face);
* return ERROR;
* }
* </programlisting></informalexample>
*
* Return value: a newly created #cairo_font_face_t. Free with
* cairo_font_face_destroy() when you are done using it.
*
* Since: 1.0
**/
cairo_font_face_t *
cairo_ft_font_face_create_for_ft_face (FT_Face face,
int load_flags,
unsigned int synth_flags,
void *face_context)
{
cairo_ft_unscaled_font_t *unscaled;
cairo_font_face_t *font_face;
cairo_ft_options_t ft_options;
cairo_status_t status;
status = _cairo_ft_unscaled_font_create_from_face (face, face_context,
&unscaled); if (unlikely (status)) return (cairo_font_face_t *)&_cairo_font_face_nil;
/**
* cairo_ft_font_face_set_synthesize:
* @font_face: The #cairo_ft_font_face_t object to modify
* @synth_flags: the set of synthesis options to enable
*
* FreeType provides the ability to synthesize different glyphs from a base
* font, which is useful if you lack those glyphs from a true bold or oblique
* font. See also #cairo_ft_synthesize_t.
*
* Since: 1.12
**/
void
cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
unsigned int synth_flags)
{
cairo_ft_font_face_t *ft;
if (font_face->backend->type != CAIRO_FONT_TYPE_FT) return;
ft = (cairo_ft_font_face_t *) font_face;
ft->ft_options.synth_flags |= synth_flags;
}
/**
* cairo_ft_font_face_unset_synthesize:
* @font_face: The #cairo_ft_font_face_t object to modify
* @synth_flags: the set of synthesis options to disable
*
* See cairo_ft_font_face_set_synthesize().
*
* Since: 1.12
**/
void
cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
unsigned int synth_flags)
{
cairo_ft_font_face_t *ft;
if (font_face->backend->type != CAIRO_FONT_TYPE_FT) return;
ft = (cairo_ft_font_face_t *) font_face;
ft->ft_options.synth_flags &= ~synth_flags;
}
/**
* cairo_ft_font_face_get_synthesize:
* @font_face: The #cairo_ft_font_face_t object to query
*
* See #cairo_ft_synthesize_t.
*
* Returns: the current set of synthesis options.
*
* Since: 1.12
**/
unsigned int
cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
{
cairo_ft_font_face_t *ft;
if (font_face->backend->type != CAIRO_FONT_TYPE_FT) return0;
ft = (cairo_ft_font_face_t *) font_face; return ft->ft_options.synth_flags;
}
/**
* cairo_ft_scaled_font_lock_face:
* @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
* object can be created by calling cairo_scaled_font_create() on a
* FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
* cairo_ft_font_face_create_for_ft_face()).
*
* cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
* backend font and scales it appropriately for the font and applies OpenType
* font variations if applicable. You must
* release the face with cairo_ft_scaled_font_unlock_face()
* when you are done using it. Since the #FT_Face object can be
* shared between multiple #cairo_scaled_font_t objects, you must not
* lock any other font objects until you unlock this one. A count is
* kept of the number of times cairo_ft_scaled_font_lock_face() is
* called. cairo_ft_scaled_font_unlock_face() must be called the same number
* of times.
*
* You must be careful when using this function in a library or in a
* threaded application, because freetype's design makes it unsafe to
* call freetype functions simultaneously from multiple threads, (even
* if using distinct FT_Face objects). Because of this, application
* code that acquires an FT_Face object with this call must add its
* own locking to protect any use of that object, (and which also must
* protect any other calls into cairo as almost any cairo function
* might result in a call into the freetype library).
*
* Return value: The #FT_Face object for @font, scaled appropriately,
* or %NULL if @scaled_font is in an error state (see
* cairo_scaled_font_status()) or there is insufficient memory.
*
* Since: 1.0
**/
FT_Face
cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
{
cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
FT_Face face;
cairo_status_t status;
if (! _cairo_scaled_font_is_ft (abstract_font)) {
_cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH); return NULL;
}
if (scaled_font->base.status) return NULL;
face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled); if (unlikely (face == NULL)) {
status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY); return NULL;
}
status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
&scaled_font->base.scale); if (unlikely (status)) {
_cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
status = _cairo_scaled_font_set_error (&scaled_font->base, status); return NULL;
}
cairo_ft_apply_variations (face, scaled_font);
/* Note: We deliberately release the unscaled font's mutex here,
* so that we are not holding a lock across two separate calls to
* cairo function, (which would give the application some
* opportunity for creating deadlock. This is obviously unsafe,
* but as documented, the user must add manual locking when using
* this function. */
CAIRO_FT_UNLOCK (scaled_font->unscaled);
return face;
}
/**
* cairo_ft_scaled_font_unlock_face:
* @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
* object can be created by calling cairo_scaled_font_create() on a
* FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
* cairo_ft_font_face_create_for_ft_face()).
*
* Releases a face obtained with cairo_ft_scaled_font_lock_face().
*
* Since: 1.0
**/
void
cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
{
cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
if (! _cairo_scaled_font_is_ft (abstract_font)) {
_cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH); return;
}
if (scaled_font->base.status) return;
/* Note: We released the unscaled font's mutex at the end of
* cairo_ft_scaled_font_lock_face, so we have to acquire it again
* as _cairo_ft_unscaled_font_unlock_face expects it to be held
* when we call into it. */
CAIRO_FT_LOCK (scaled_font->unscaled);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.