1 /***************************************************************************/
5 /* FreeType convenience functions to handle glyphs (body). */
7 /* Copyright 1996-2005, 2007, 2008, 2010, 2012-2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
18 /*************************************************************************/
20 /* This file contains the definition of several convenience functions */
21 /* that can be used by client applications to easily retrieve glyph */
22 /* bitmaps and outlines from a given face. */
24 /* These functions should be optional if you are writing a font server */
25 /* or text layout engine on top of FreeType. However, they are pretty */
26 /* handy for many other simple uses of the library. */
28 /*************************************************************************/
32 #include FT_INTERNAL_DEBUG_H
37 #include FT_INTERNAL_OBJECTS_H
41 /*************************************************************************/
43 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
44 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
45 /* messages during execution. */
48 #define FT_COMPONENT trace_glyph
51 /*************************************************************************/
52 /*************************************************************************/
54 /**** FT_BitmapGlyph support ****/
56 /*************************************************************************/
57 /*************************************************************************/
59 FT_CALLBACK_DEF( FT_Error )
60 ft_bitmap_glyph_init( FT_Glyph bitmap_glyph,
63 FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
64 FT_Error error = FT_Err_Ok;
65 FT_Library library = FT_GLYPH( glyph )->library;
68 if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
70 error = FT_THROW( Invalid_Glyph_Format );
74 glyph->left = slot->bitmap_left;
75 glyph->top = slot->bitmap_top;
77 /* do lazy copying whenever possible */
78 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
80 glyph->bitmap = slot->bitmap;
81 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
85 FT_Bitmap_New( &glyph->bitmap );
86 error = FT_Bitmap_Copy( library, &slot->bitmap, &glyph->bitmap );
94 FT_CALLBACK_DEF( FT_Error )
95 ft_bitmap_glyph_copy( FT_Glyph bitmap_source,
96 FT_Glyph bitmap_target )
98 FT_Library library = bitmap_source->library;
99 FT_BitmapGlyph source = (FT_BitmapGlyph)bitmap_source;
100 FT_BitmapGlyph target = (FT_BitmapGlyph)bitmap_target;
103 target->left = source->left;
104 target->top = source->top;
106 return FT_Bitmap_Copy( library, &source->bitmap, &target->bitmap );
110 FT_CALLBACK_DEF( void )
111 ft_bitmap_glyph_done( FT_Glyph bitmap_glyph )
113 FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
114 FT_Library library = FT_GLYPH( glyph )->library;
117 FT_Bitmap_Done( library, &glyph->bitmap );
121 FT_CALLBACK_DEF( void )
122 ft_bitmap_glyph_bbox( FT_Glyph bitmap_glyph,
125 FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
128 cbox->xMin = glyph->left << 6;
129 cbox->xMax = cbox->xMin + ( glyph->bitmap.width << 6 );
130 cbox->yMax = glyph->top << 6;
131 cbox->yMin = cbox->yMax - ( glyph->bitmap.rows << 6 );
135 FT_DEFINE_GLYPH(ft_bitmap_glyph_class,
136 sizeof ( FT_BitmapGlyphRec ),
137 FT_GLYPH_FORMAT_BITMAP,
139 ft_bitmap_glyph_init,
140 ft_bitmap_glyph_done,
141 ft_bitmap_glyph_copy,
142 0, /* FT_Glyph_TransformFunc */
143 ft_bitmap_glyph_bbox,
144 0 /* FT_Glyph_PrepareFunc */
148 /*************************************************************************/
149 /*************************************************************************/
151 /**** FT_OutlineGlyph support ****/
153 /*************************************************************************/
154 /*************************************************************************/
157 FT_CALLBACK_DEF( FT_Error )
158 ft_outline_glyph_init( FT_Glyph outline_glyph,
161 FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
162 FT_Error error = FT_Err_Ok;
163 FT_Library library = FT_GLYPH( glyph )->library;
164 FT_Outline* source = &slot->outline;
165 FT_Outline* target = &glyph->outline;
168 /* check format in glyph slot */
169 if ( slot->format != FT_GLYPH_FORMAT_OUTLINE )
171 error = FT_THROW( Invalid_Glyph_Format );
175 /* allocate new outline */
176 error = FT_Outline_New( library, source->n_points, source->n_contours,
181 FT_Outline_Copy( source, target );
188 FT_CALLBACK_DEF( void )
189 ft_outline_glyph_done( FT_Glyph outline_glyph )
191 FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
194 FT_Outline_Done( FT_GLYPH( glyph )->library, &glyph->outline );
198 FT_CALLBACK_DEF( FT_Error )
199 ft_outline_glyph_copy( FT_Glyph outline_source,
200 FT_Glyph outline_target )
202 FT_OutlineGlyph source = (FT_OutlineGlyph)outline_source;
203 FT_OutlineGlyph target = (FT_OutlineGlyph)outline_target;
205 FT_Library library = FT_GLYPH( source )->library;
208 error = FT_Outline_New( library, source->outline.n_points,
209 source->outline.n_contours, &target->outline );
211 FT_Outline_Copy( &source->outline, &target->outline );
217 FT_CALLBACK_DEF( void )
218 ft_outline_glyph_transform( FT_Glyph outline_glyph,
219 const FT_Matrix* matrix,
220 const FT_Vector* delta )
222 FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
226 FT_Outline_Transform( &glyph->outline, matrix );
229 FT_Outline_Translate( &glyph->outline, delta->x, delta->y );
233 FT_CALLBACK_DEF( void )
234 ft_outline_glyph_bbox( FT_Glyph outline_glyph,
237 FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
240 FT_Outline_Get_CBox( &glyph->outline, bbox );
244 FT_CALLBACK_DEF( FT_Error )
245 ft_outline_glyph_prepare( FT_Glyph outline_glyph,
248 FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
251 slot->format = FT_GLYPH_FORMAT_OUTLINE;
252 slot->outline = glyph->outline;
253 slot->outline.flags &= ~FT_OUTLINE_OWNER;
259 FT_DEFINE_GLYPH( ft_outline_glyph_class,
260 sizeof ( FT_OutlineGlyphRec ),
261 FT_GLYPH_FORMAT_OUTLINE,
263 ft_outline_glyph_init,
264 ft_outline_glyph_done,
265 ft_outline_glyph_copy,
266 ft_outline_glyph_transform,
267 ft_outline_glyph_bbox,
268 ft_outline_glyph_prepare
272 /*************************************************************************/
273 /*************************************************************************/
275 /**** FT_Glyph class and API ****/
277 /*************************************************************************/
278 /*************************************************************************/
281 ft_new_glyph( FT_Library library,
282 const FT_Glyph_Class* clazz,
285 FT_Memory memory = library->memory;
287 FT_Glyph glyph = NULL;
292 if ( !FT_ALLOC( glyph, clazz->glyph_size ) )
294 glyph->library = library;
295 glyph->clazz = clazz;
296 glyph->format = clazz->glyph_format;
305 /* documentation is in ftglyph.h */
307 FT_EXPORT_DEF( FT_Error )
308 FT_Glyph_Copy( FT_Glyph source,
313 const FT_Glyph_Class* clazz;
316 /* check arguments */
317 if ( !target || !source || !source->clazz )
319 error = FT_THROW( Invalid_Argument );
325 if ( !source || !source->clazz )
327 error = FT_THROW( Invalid_Argument );
331 clazz = source->clazz;
332 error = ft_new_glyph( source->library, clazz, © );
336 copy->advance = source->advance;
337 copy->format = source->format;
339 if ( clazz->glyph_copy )
340 error = clazz->glyph_copy( source, copy );
343 FT_Done_Glyph( copy );
352 /* documentation is in ftglyph.h */
354 FT_EXPORT_DEF( FT_Error )
355 FT_Get_Glyph( FT_GlyphSlot slot,
362 const FT_Glyph_Class* clazz = NULL;
366 return FT_THROW( Invalid_Slot_Handle );
368 library = slot->library;
371 return FT_THROW( Invalid_Argument );
373 /* if it is a bitmap, that's easy :-) */
374 if ( slot->format == FT_GLYPH_FORMAT_BITMAP )
375 clazz = FT_BITMAP_GLYPH_CLASS_GET;
377 /* if it is an outline */
378 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
379 clazz = FT_OUTLINE_GLYPH_CLASS_GET;
383 /* try to find a renderer that supports the glyph image format */
384 FT_Renderer render = FT_Lookup_Renderer( library, slot->format, 0 );
388 clazz = &render->glyph_class;
393 error = FT_THROW( Invalid_Glyph_Format );
397 /* create FT_Glyph object */
398 error = ft_new_glyph( library, clazz, &glyph );
402 /* copy advance while converting it to 16.16 format */
403 glyph->advance.x = slot->advance.x << 10;
404 glyph->advance.y = slot->advance.y << 10;
406 /* now import the image from the glyph slot */
407 error = clazz->glyph_init( glyph, slot );
409 /* if an error occurred, destroy the glyph */
411 FT_Done_Glyph( glyph );
420 /* documentation is in ftglyph.h */
422 FT_EXPORT_DEF( FT_Error )
423 FT_Glyph_Transform( FT_Glyph glyph,
427 FT_Error error = FT_Err_Ok;
430 if ( !glyph || !glyph->clazz )
431 error = FT_THROW( Invalid_Argument );
434 const FT_Glyph_Class* clazz = glyph->clazz;
437 if ( clazz->glyph_transform )
439 /* transform glyph image */
440 clazz->glyph_transform( glyph, matrix, delta );
442 /* transform advance vector */
444 FT_Vector_Transform( &glyph->advance, matrix );
447 error = FT_THROW( Invalid_Glyph_Format );
453 /* documentation is in ftglyph.h */
455 FT_EXPORT_DEF( void )
456 FT_Glyph_Get_CBox( FT_Glyph glyph,
460 const FT_Glyph_Class* clazz;
466 acbox->xMin = acbox->yMin = acbox->xMax = acbox->yMax = 0;
468 if ( !glyph || !glyph->clazz )
471 clazz = glyph->clazz;
472 if ( !clazz->glyph_bbox )
475 /* retrieve bbox in 26.6 coordinates */
476 clazz->glyph_bbox( glyph, acbox );
478 /* perform grid fitting if needed */
479 if ( bbox_mode == FT_GLYPH_BBOX_GRIDFIT ||
480 bbox_mode == FT_GLYPH_BBOX_PIXELS )
482 acbox->xMin = FT_PIX_FLOOR( acbox->xMin );
483 acbox->yMin = FT_PIX_FLOOR( acbox->yMin );
484 acbox->xMax = FT_PIX_CEIL( acbox->xMax );
485 acbox->yMax = FT_PIX_CEIL( acbox->yMax );
488 /* convert to integer pixels if needed */
489 if ( bbox_mode == FT_GLYPH_BBOX_TRUNCATE ||
490 bbox_mode == FT_GLYPH_BBOX_PIXELS )
500 /* documentation is in ftglyph.h */
502 FT_EXPORT_DEF( FT_Error )
503 FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
504 FT_Render_Mode render_mode,
508 FT_GlyphSlotRec dummy;
509 FT_GlyphSlot_InternalRec dummy_internal;
510 FT_Error error = FT_Err_Ok;
512 FT_BitmapGlyph bitmap = NULL;
513 const FT_Glyph_Class* clazz;
515 /* FT_BITMAP_GLYPH_CLASS_GET dereferences `library' in PIC mode */
526 clazz = glyph->clazz;
527 library = glyph->library;
528 if ( !library || !clazz )
531 /* when called with a bitmap glyph, do nothing and return successfully */
532 if ( clazz == FT_BITMAP_GLYPH_CLASS_GET )
535 if ( !clazz->glyph_prepare )
538 /* we render the glyph into a glyph bitmap using a `dummy' glyph slot */
539 /* then calling FT_Render_Glyph_Internal() */
541 FT_MEM_ZERO( &dummy, sizeof ( dummy ) );
542 FT_MEM_ZERO( &dummy_internal, sizeof ( dummy_internal ) );
543 dummy.internal = &dummy_internal;
544 dummy.library = library;
545 dummy.format = clazz->glyph_format;
547 /* create result bitmap glyph */
548 error = ft_new_glyph( library, FT_BITMAP_GLYPH_CLASS_GET, &b );
551 bitmap = (FT_BitmapGlyph)b;
554 /* if `origin' is set, translate the glyph image */
556 FT_Glyph_Transform( glyph, 0, origin );
561 /* prepare dummy slot for rendering */
562 error = clazz->glyph_prepare( glyph, &dummy );
564 error = FT_Render_Glyph_Internal( glyph->library, &dummy, render_mode );
567 if ( !destroy && origin )
574 FT_Glyph_Transform( glyph, 0, &v );
581 /* in case of success, copy the bitmap to the glyph bitmap */
582 error = ft_bitmap_glyph_init( (FT_Glyph)bitmap, &dummy );
587 bitmap->root.advance = glyph->advance;
590 FT_Done_Glyph( glyph );
592 *the_glyph = FT_GLYPH( bitmap );
595 if ( error && bitmap )
596 FT_Done_Glyph( FT_GLYPH( bitmap ) );
601 error = FT_THROW( Invalid_Argument );
606 /* documentation is in ftglyph.h */
608 FT_EXPORT_DEF( void )
609 FT_Done_Glyph( FT_Glyph glyph )
613 FT_Memory memory = glyph->library->memory;
614 const FT_Glyph_Class* clazz = glyph->clazz;
617 if ( clazz->glyph_done )
618 clazz->glyph_done( glyph );