1 /***************************************************************************/
5 /* FreeType convenience functions to handle glyphs (body). */
7 /* Copyright 1996-2001 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 /*************************************************************************/
34 #include FT_INTERNAL_OBJECTS_H
37 /*************************************************************************/
39 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
40 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
41 /* messages during execution. */
44 #define FT_COMPONENT trace_glyph
47 /*************************************************************************/
48 /*************************************************************************/
50 /**** Convenience functions ****/
52 /*************************************************************************/
53 /*************************************************************************/
56 /* documentation is in ftglyph.h */
59 FT_Matrix_Multiply( FT_Matrix* a,
62 FT_Fixed xx, xy, yx, yy;
68 xx = FT_MulFix( a->xx, b->xx ) + FT_MulFix( a->xy, b->yx );
69 xy = FT_MulFix( a->xx, b->xy ) + FT_MulFix( a->xy, b->yy );
70 yx = FT_MulFix( a->yx, b->xx ) + FT_MulFix( a->yy, b->yx );
71 yy = FT_MulFix( a->yx, b->xy ) + FT_MulFix( a->yy, b->yy );
73 b->xx = xx; b->xy = xy;
74 b->yx = yx; b->yy = yy;
78 /* documentation is in ftglyph.h */
80 FT_EXPORT_DEF( FT_Error )
81 FT_Matrix_Invert( FT_Matrix* matrix )
87 return FT_Err_Invalid_Argument;
89 /* compute discriminant */
90 delta = FT_MulFix( matrix->xx, matrix->yy ) -
91 FT_MulFix( matrix->xy, matrix->yx );
94 return FT_Err_Invalid_Argument; /* matrix can't be inverted */
96 matrix->xy = - FT_DivFix( matrix->xy, delta );
97 matrix->yx = - FT_DivFix( matrix->yx, delta );
102 matrix->xx = FT_DivFix( yy, delta );
103 matrix->yy = FT_DivFix( xx, delta );
109 /*************************************************************************/
110 /*************************************************************************/
112 /**** FT_BitmapGlyph support ****/
114 /*************************************************************************/
115 /*************************************************************************/
118 ft_bitmap_copy( FT_Memory memory,
123 FT_Int pitch = source->pitch;
132 size = (FT_ULong)( pitch * source->rows );
134 if ( !ALLOC( target->buffer, size ) )
135 MEM_Copy( target->buffer, source->buffer, size );
142 ft_bitmap_glyph_init( FT_BitmapGlyph glyph,
145 FT_Error error = FT_Err_Ok;
146 FT_Library library = FT_GLYPH(glyph)->library;
147 FT_Memory memory = library->memory;
150 if ( slot->format != ft_glyph_format_bitmap )
152 error = FT_Err_Invalid_Glyph_Format;
156 /* grab the bitmap in the slot - do lazy copying whenever possible */
157 glyph->bitmap = slot->bitmap;
158 glyph->left = slot->bitmap_left;
159 glyph->top = slot->bitmap_top;
161 if ( slot->flags & ft_glyph_own_bitmap )
162 slot->flags &= ~ft_glyph_own_bitmap;
165 /* copy the bitmap into a new buffer */
166 error = ft_bitmap_copy( memory, &slot->bitmap, &glyph->bitmap );
175 ft_bitmap_glyph_copy( FT_BitmapGlyph source,
176 FT_BitmapGlyph target )
178 FT_Memory memory = source->root.library->memory;
181 target->left = source->left;
182 target->top = source->top;
184 return ft_bitmap_copy( memory, &source->bitmap, &target->bitmap );
189 ft_bitmap_glyph_done( FT_BitmapGlyph glyph )
191 FT_Memory memory = FT_GLYPH(glyph)->library->memory;
194 FREE( glyph->bitmap.buffer );
199 ft_bitmap_glyph_bbox( FT_BitmapGlyph glyph,
202 cbox->xMin = glyph->left << 6;
203 cbox->xMax = cbox->xMin + ( glyph->bitmap.width << 6 );
204 cbox->yMax = glyph->top << 6;
205 cbox->yMin = cbox->yMax - ( glyph->bitmap.rows << 6 );
209 const FT_Glyph_Class ft_bitmap_glyph_class =
211 sizeof( FT_BitmapGlyphRec ),
212 ft_glyph_format_bitmap,
214 (FT_Glyph_Init_Func) ft_bitmap_glyph_init,
215 (FT_Glyph_Done_Func) ft_bitmap_glyph_done,
216 (FT_Glyph_Copy_Func) ft_bitmap_glyph_copy,
217 (FT_Glyph_Transform_Func)0,
218 (FT_Glyph_BBox_Func) ft_bitmap_glyph_bbox,
219 (FT_Glyph_Prepare_Func) 0
223 /*************************************************************************/
224 /*************************************************************************/
226 /**** FT_OutlineGlyph support ****/
228 /*************************************************************************/
229 /*************************************************************************/
233 ft_outline_glyph_init( FT_OutlineGlyph glyph,
236 FT_Error error = FT_Err_Ok;
237 FT_Library library = FT_GLYPH(glyph)->library;
238 FT_Outline* source = &slot->outline;
239 FT_Outline* target = &glyph->outline;
242 /* check format in glyph slot */
243 if ( slot->format != ft_glyph_format_outline )
245 error = FT_Err_Invalid_Glyph_Format;
249 /* allocate new outline */
250 error = FT_Outline_New( library, source->n_points, source->n_contours,
256 MEM_Copy( target->points, source->points,
257 source->n_points * sizeof ( FT_Vector ) );
259 MEM_Copy( target->tags, source->tags,
260 source->n_points * sizeof ( FT_Byte ) );
262 MEM_Copy( target->contours, source->contours,
263 source->n_contours * sizeof ( FT_Short ) );
265 /* copy all flags, except the `ft_outline_owner' one */
266 target->flags = source->flags | ft_outline_owner;
274 ft_outline_glyph_done( FT_OutlineGlyph glyph )
276 FT_Outline_Done( FT_GLYPH( glyph )->library, &glyph->outline );
281 ft_outline_glyph_copy( FT_OutlineGlyph source,
282 FT_OutlineGlyph target )
285 FT_Library library = FT_GLYPH( source )->library;
288 error = FT_Outline_New( library, source->outline.n_points,
289 source->outline.n_contours, &target->outline );
291 FT_Outline_Copy( &source->outline, &target->outline );
298 ft_outline_glyph_transform( FT_OutlineGlyph glyph,
303 FT_Outline_Transform( &glyph->outline, matrix );
306 FT_Outline_Translate( &glyph->outline, delta->x, delta->y );
311 ft_outline_glyph_bbox( FT_OutlineGlyph glyph,
314 FT_Outline_Get_CBox( &glyph->outline, bbox );
319 ft_outline_glyph_prepare( FT_OutlineGlyph glyph,
322 slot->format = ft_glyph_format_outline;
323 slot->outline = glyph->outline;
324 slot->outline.flags &= ~ft_outline_owner;
330 const FT_Glyph_Class ft_outline_glyph_class =
332 sizeof( FT_OutlineGlyphRec ),
333 ft_glyph_format_outline,
335 (FT_Glyph_Init_Func) ft_outline_glyph_init,
336 (FT_Glyph_Done_Func) ft_outline_glyph_done,
337 (FT_Glyph_Copy_Func) ft_outline_glyph_copy,
338 (FT_Glyph_Transform_Func)ft_outline_glyph_transform,
339 (FT_Glyph_BBox_Func) ft_outline_glyph_bbox,
340 (FT_Glyph_Prepare_Func) ft_outline_glyph_prepare
344 /*************************************************************************/
345 /*************************************************************************/
347 /**** FT_Glyph class and API ****/
349 /*************************************************************************/
350 /*************************************************************************/
353 ft_new_glyph( FT_Library library,
354 const FT_Glyph_Class* clazz,
357 FT_Memory memory = library->memory;
364 if ( !ALLOC( glyph, clazz->glyph_size ) )
366 glyph->library = library;
367 glyph->clazz = clazz;
368 glyph->format = clazz->glyph_format;
377 /* documentation is in ftglyph.h */
379 FT_EXPORT_DEF( FT_Error )
380 FT_Glyph_Copy( FT_Glyph source,
385 const FT_Glyph_Class* clazz;
388 /* check arguments */
389 if ( !target || !source || !source->clazz )
391 error = FT_Err_Invalid_Argument;
397 clazz = source->clazz;
398 error = ft_new_glyph( source->library, clazz, © );
402 copy->advance = source->advance;
403 copy->format = source->format;
405 if ( clazz->glyph_copy )
406 error = clazz->glyph_copy( source, copy );
409 FT_Done_Glyph( copy );
418 /* documentation is in ftglyph.h */
420 FT_EXPORT_DEF( FT_Error )
421 FT_Get_Glyph( FT_GlyphSlot slot,
424 FT_Library library = slot->library;
428 const FT_Glyph_Class* clazz = 0;
432 return FT_Err_Invalid_Slot_Handle;
435 return FT_Err_Invalid_Argument;
437 /* if it is a bitmap, that's easy :-) */
438 if ( slot->format == ft_glyph_format_bitmap )
439 clazz = &ft_bitmap_glyph_class;
441 /* it it is an outline too */
442 else if ( slot->format == ft_glyph_format_outline )
443 clazz = &ft_outline_glyph_class;
447 /* try to find a renderer that supports the glyph image format */
448 FT_Renderer render = FT_Lookup_Renderer( library, slot->format, 0 );
452 clazz = &render->glyph_class;
457 error = FT_Err_Invalid_Glyph_Format;
461 /* create FT_Glyph object */
462 error = ft_new_glyph( library, clazz, &glyph );
466 /* copy advance while converting it to 16.16 format */
467 glyph->advance.x = slot->advance.x << 10;
468 glyph->advance.y = slot->advance.y << 10;
470 /* now import the image from the glyph slot */
471 error = clazz->glyph_init( glyph, slot );
473 /* if an error occurred, destroy the glyph */
475 FT_Done_Glyph( glyph );
484 /* documentation is in ftglyph.h */
486 FT_EXPORT_DEF( FT_Error )
487 FT_Glyph_Transform( FT_Glyph glyph,
491 const FT_Glyph_Class* clazz;
492 FT_Error error = FT_Err_Ok;
495 if ( !glyph || !glyph->clazz )
496 error = FT_Err_Invalid_Argument;
499 clazz = glyph->clazz;
500 if ( clazz->glyph_transform )
502 /* transform glyph image */
503 clazz->glyph_transform( glyph, matrix, delta );
505 /* transform advance vector */
507 FT_Vector_Transform( &glyph->advance, matrix );
510 error = FT_Err_Invalid_Glyph_Format;
516 /* documentation is in ftglyph.h */
518 FT_EXPORT_DEF( void )
519 FT_Glyph_Get_CBox( FT_Glyph glyph,
523 const FT_Glyph_Class* clazz;
529 acbox->xMin = acbox->yMin = acbox->xMax = acbox->yMax = 0;
531 if ( !glyph || !glyph->clazz )
535 clazz = glyph->clazz;
536 if ( !clazz->glyph_bbox )
540 /* retrieve bbox in 26.6 coordinates */
541 clazz->glyph_bbox( glyph, acbox );
543 /* perform grid fitting if needed */
544 if ( bbox_mode & ft_glyph_bbox_gridfit )
548 acbox->xMax = ( acbox->xMax + 63 ) & -64;
549 acbox->yMax = ( acbox->yMax + 63 ) & -64;
552 /* convert to integer pixels if needed */
553 if ( bbox_mode & ft_glyph_bbox_truncate )
566 /* documentation is in ftglyph.h */
568 FT_EXPORT_DEF( FT_Error )
569 FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
570 FT_ULong render_mode,
574 FT_GlyphSlotRec dummy;
577 FT_BitmapGlyph bitmap;
579 const FT_Glyph_Class* clazz;
586 /* we render the glyph into a glyph bitmap using a `dummy' glyph slot */
587 /* then calling FT_Render_Glyph_Internal() */
593 clazz = glyph->clazz;
594 if ( !clazz || !clazz->glyph_prepare )
597 MEM_Set( &dummy, 0, sizeof ( dummy ) );
598 dummy.library = glyph->library;
599 dummy.format = clazz->glyph_format;
601 /* if `origin' is set, translate the glyph image */
603 FT_Glyph_Transform( glyph, 0, origin );
605 /* create result bitmap glyph */
606 error = ft_new_glyph( glyph->library, &ft_bitmap_glyph_class,
607 (FT_Glyph*)&bitmap );
611 /* prepare dummy slot for rendering */
612 error = clazz->glyph_prepare( glyph, &dummy );
614 error = FT_Render_Glyph_Internal( glyph->library, &dummy, render_mode );
618 FT_Done_Glyph( FT_GLYPH( bitmap ) );
622 if ( !destroy && origin )
629 FT_Glyph_Transform( glyph, 0, &v );
632 /* in case of success, copy the bitmap to the glyph bitmap */
635 error = ft_bitmap_glyph_init( bitmap, &dummy );
638 /* this should never happen, but let's be safe */
639 FT_Done_Glyph( FT_GLYPH( bitmap ) );
644 bitmap->root.advance = glyph->advance;
647 FT_Done_Glyph( glyph );
649 *the_glyph = FT_GLYPH( bitmap );
656 error = FT_Err_Invalid_Argument;
661 /* documentation is in ftglyph.h */
663 FT_EXPORT_DEF( void )
664 FT_Done_Glyph( FT_Glyph glyph )
668 FT_Memory memory = glyph->library->memory;
669 const FT_Glyph_Class* clazz = glyph->clazz;
672 if ( clazz->glyph_done )
673 clazz->glyph_done( glyph );