freetype2/src/base/ftglyph.c

811 lines
27 KiB
C
Raw Normal View History

/***************************************************************************/
/* */
/* ftglyph.c */
/* */
2000-05-30 18:49:14 +02:00
/* FreeType convenience functions to handle glyphs (body). */
/* */
2000-05-30 18:49:14 +02:00
/* Copyright 1996-2000 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
/* modified, and distributed under the terms of the FreeType project */
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
/* this file you indicate that you have read the license and */
/* understand and accept it fully. */
/* */
/***************************************************************************/
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* This file contains the definition of several convenience functions */
/* that can be used by client applications to easily retrieve glyph */
/* bitmaps and outlines from a given face. */
/* */
/* These functions should be optional if you are writing a font server */
/* or text layout engine on top of FreeType. However, they are pretty */
/* handy for many other simple uses of the library. */
/* */
/*************************************************************************/
#include <freetype/ftglyph.h>
#include <freetype/internal/ftobjs.h>
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
/* messages during execution. */
/* */
#undef FT_COMPONENT
#define FT_COMPONENT trace_glyph
/* a helper function to avoid duplication of code */
static
2000-05-30 18:49:14 +02:00
void ft_prepare_glyph( FT_Glyph glyph,
FT_Face face,
FT_Bool vertical )
{
FT_Glyph_Metrics* metrics = &face->glyph->metrics;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
glyph->memory = face->memory;
glyph->width = metrics->width;
glyph->height = metrics->height;
if ( vertical )
{
glyph->bearingX = metrics->vertBearingX;
glyph->bearingY = metrics->vertBearingY;
glyph->advance = metrics->vertAdvance;
}
else
{
glyph->bearingX = metrics->horiBearingX;
glyph->bearingY = metrics->horiBearingY;
glyph->advance = metrics->horiAdvance;
}
}
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* <Function> */
/* FT_Get_Glyph_Bitmap */
/* */
/* <Description> */
/* A function used to directly return a monochrome bitmap glyph image */
/* from a face. */
/* */
/* <Input> */
/* face :: A handle to source face object. */
/* glyph_index :: A glyph index into the face. */
/* load_flags :: Load flags (see FT_LOAD_FLAG_XXXX constants). */
/* grays :: The number of gray levels for anti-aliased bitmaps. */
/* Set it to 0 if you want to render a monochrome */
/* bitmap. */
/* origin :: A pointer to the origin's position. Set it to 0 */
/* if the current transform is the identity. */
/* */
/* <Output> */
/* abitglyph :: A pointer to the new bitmap glyph. */
/* */
/* <Return> */
/* FreeType error code. 0 means success. */
/* */
/* <Note> */
/* If the font contains glyph outlines, these will be automatically */
/* converted to a bitmap according to the value of `grays'. */
2000-05-30 18:49:14 +02:00
/* */
/* If `grays' is set to 0, the result is a 1-bit monochrome bitmap */
/* otherwise, it is an 8-bit gray-level bitmap. */
/* */
/* The number of gray levels in the result anti-aliased bitmap might */
/* not be `grays', depending on the current scan-converter */
/* implementation. */
/* */
/* Note that it is not possible to generate 8-bit monochrome bitmaps */
/* with this function. Rather, use FT_Get_Glyph_Outline(), then */
/* FT_Glyph_Render_Outline(), and provide your own span callbacks. */
/* */
/* If the face doesn't contain scalable outlines, this function will */
/* fail if the current transformation is not the identity, or if the */
/* glyph origin's phase to the pixel grid is not 0 in both */
/* directions! */
/* */
FT_EXPORT_FUNC( FT_Error ) FT_Get_Glyph_Bitmap(
FT_Face face,
FT_UInt glyph_index,
FT_UInt load_flags,
FT_Int grays,
FT_Vector* origin,
FT_BitmapGlyph* abitglyph )
{
FT_Error error;
FT_Memory memory;
2000-05-30 18:49:14 +02:00
FT_BitmapGlyph bitglyph;
FT_Glyph glyph;
FT_Pos origin_x = 0;
FT_Pos origin_y = 0;
2000-05-30 18:49:14 +02:00
if ( !face )
return FT_Err_Invalid_Face_Handle;
if ( !abitglyph )
return FT_Err_Invalid_Argument;
*abitglyph = 0;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
if ( origin )
{
origin_x = origin->x & 63;
origin_y = origin->y & 63;
}
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
/* check arguments whether the face's format is not scalable */
if ( !( face->face_flags & FT_FACE_FLAG_SCALABLE ) &&
face->transform_flags )
{
/* we can't transform bitmaps, so return an error */
error = FT_Err_Unimplemented_Feature;
goto Exit;
}
/* check that NO_SCALE and NO_RECURSE are not set */
2000-05-30 18:49:14 +02:00
if ( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_RECURSE ) )
{
error = FT_Err_Invalid_Argument;
goto Exit;
}
/* disable embedded bitmaps for transformed images */
if ( face->face_flags & FT_FACE_FLAG_SCALABLE && face->transform_flags )
load_flags |= FT_LOAD_NO_BITMAP;
2000-05-17 01:44:38 +02:00
error = FT_Load_Glyph( face, glyph_index, load_flags );
2000-05-30 18:49:14 +02:00
if ( error )
goto Exit;
2000-05-17 01:44:38 +02:00
/* now, handle bitmap and outline glyph images */
memory = face->memory;
switch ( face->glyph->format )
{
2000-05-30 18:49:14 +02:00
case ft_glyph_format_bitmap:
{
FT_Long size;
FT_Bitmap* source;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
if ( ALLOC( bitglyph, sizeof ( *bitglyph ) ) )
goto Exit;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
glyph = (FT_Glyph)bitglyph;
glyph->glyph_type = ft_glyph_type_bitmap;
ft_prepare_glyph( glyph, face, 0 );
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
source = &face->glyph->bitmap;
size = source->rows * source->pitch;
if ( size < 0 )
size = -size;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
bitglyph->bitmap = *source;
if ( ALLOC( bitglyph->bitmap.buffer, size ) )
goto Fail;
2000-05-30 18:49:14 +02:00
/* copy the content of the source glyph */
MEM_Copy( bitglyph->bitmap.buffer, source->buffer, size );
}
break;
case ft_glyph_format_outline:
{
FT_BBox cbox;
FT_Int width, height, pitch;
FT_Long size;
/* transform the outline -- note that the original metrics are NOT */
/* transformed by this, only the outline points themselves... */
FT_Outline_Translate( &face->glyph->outline,
2000-06-25 08:47:11 +02:00
origin_x,
origin_y );
2000-05-30 18:49:14 +02:00
/* compute the size in pixels of the outline */
FT_Outline_Get_CBox( &face->glyph->outline, &cbox );
cbox.xMin &= -64;
cbox.yMin &= -64;
cbox.xMax = ( cbox.xMax + 63 ) & -64;
cbox.yMax = ( cbox.yMax + 63 ) & -64;
width = ( cbox.xMax - cbox.xMin ) >> 6;
height = ( cbox.yMax - cbox.yMin ) >> 6;
/* allocate the pixel buffer for the glyph bitmap */
if ( grays )
/* some raster implementation need this */
pitch = ( width + 3 ) & -4;
else
pitch = ( width + 7 ) >> 3;
size = pitch * height;
2000-05-30 18:49:14 +02:00
if ( ALLOC( bitglyph, sizeof ( *bitglyph ) ) )
goto Exit;
glyph = (FT_Glyph)bitglyph;
glyph->glyph_type = ft_glyph_type_bitmap;
ft_prepare_glyph( glyph, face, 0 );
if ( ALLOC( bitglyph->bitmap.buffer, size ) )
goto Fail;
bitglyph->bitmap.width = width;
bitglyph->bitmap.rows = height;
bitglyph->bitmap.pitch = pitch;
bitglyph->bitmap.pixel_mode = grays ? ft_pixel_mode_grays
: ft_pixel_mode_mono;
bitglyph->bitmap.num_grays = (short)grays;
bitglyph->left = cbox.xMin >> 6;
bitglyph->top = cbox.yMax >> 6;
/* render the monochrome outline into the target buffer */
FT_Outline_Translate( &face->glyph->outline,
-cbox.xMin,
-cbox.yMin );
- MAJOR INTERNAL REDESIGN: A lot of internal modifications have been performed lately on the source in order to provide the following enhancements: - more generic module support: The FT_Module type is now defined to represent a handle to a given module. The file <freetype/ftmodule.h> contains the FT_Module_Class definition, as well as the module-loading public API The FT_Driver type is still defined, and still represents a pointer to a font driver. Note that FT_Add_Driver is replaced by FT_Add_Module, FT_Get_Driver by FT_Get_Module, etc.. - support for generic glyph image types: The FT_Renderer type is a pointer to a module used to perform various operations on glyph image. Each renderer is capable of handling images in a single format (e.g. ft_glyph_format_outline). Its functions are used to: - transform an glyph image - render a glyph image into a bitmap - return the control box (dimensions) of a given glyph image The scan converters "ftraster.c" and "ftgrays.c" have been moved to the new directory "src/renderer", and are used to provide two default renderer modules. One corresponds to the "standard" scan-converter, the other to the "smooth" one. The current renderer can be set through the new function FT_Set_Renderer. The old raster-related function FT_Set_Raster, FT_Get_Raster and FT_Set_Raster_Mode have now disappeared, in favor of the new: FT_Get_Renderer FT_Set_Renderer see the file <freetype/ftrender.h> for more details.. These changes were necessary to properly support different scalable formats in the future, like bi-color glyphs, etc.. - glyph loader object: A new internal object, called a 'glyph loader' has been introduced in the base layer. It is used by all scalable format font drivers to load glyphs and composites. This object has been created to reduce the code size of each driver, as each one of them basically re-implemented its functionality. See <freetype/internal/ftobjs.h> and the FT_GlyphLoader type for more information.. - FT_GlyphSlot had new fields: In order to support extended features (see below), the FT_GlyphSlot structure has a few new fields: linearHoriAdvance: this field gives the linearly scaled (i.e. scaled but unhinted) advance width for the glyph, expressed as a 16.16 fixed pixel value. This is useful to perform WYSIWYG text. linearVertAdvance: this field gives the linearly scaled advance height for the glyph (relevant in vertical glyph layouts only). This is useful to perform WYSIWYG text. Note that the two above field replace the removed "metrics2" field in the glyph slot. advance: this field is a vector that gives the transformed advance for the glyph. By default, it corresponds to the advance width, unless FT_LOAD_VERTICAL_LAYOUT was specified when calling FT_Load_Glyph or FT_Load_Char bitmap_left: this field gives the distance in integer pixels from the current pen position to the left-most pixel of a glyph image WHEN IT IS A BITMAP. It is only valid when the "format" field is set to "ft_glyph_format_bitmap", for example, after calling the new function FT_Render_Glyph. bitmap_top: this field gives the distance in integer pixels from the current pen position (located on the baseline) to the top-most pixel of the glyph image WHEN IT IS A BITMAP. Positive values correspond to upwards Y. loader: this is a new private field for the glyph slot. Client applications should not touch it.. - support for transforms and direct rendering in FT_Load_Glyph: Most of the functionality found in <freetype/ftglyph.h> has been moved to the core library. Hence, the following: - a transform can be specified for a face through FT_Set_Transform. this transform is applied by FT_Load_Glyph to scalable glyph images (i.e. NOT TO BITMAPS) before the function returns, unless the bit flag FT_LOAD_IGNORE_TRANSFORM was set in the load flags.. - once a glyph image has been loaded, it can be directly converted to a bitmap by using the new FT_Render_Glyph function. Note that this function takes the glyph image from the glyph slot, and converts it to a bitmap whose properties are returned in "face.glyph.bitmap", "face.glyph.bitmap_left" and "face.glyph.bitmap_top". The original native image might be lost after the conversion. - when using the new bit flag FT_LOAD_RENDER, the FT_Load_Glyph and FT_Load_Char functions will call FT_Render_Glyph automatically when needed.
2000-06-22 02:17:42 +02:00
error = FT_Outline_Get_Bitmap( face->driver->root.library,
2000-05-30 18:49:14 +02:00
&face->glyph->outline,
&bitglyph->bitmap );
if ( error )
{
2000-05-30 18:49:14 +02:00
FREE( bitglyph->bitmap.buffer );
goto Fail;
}
2000-05-30 18:49:14 +02:00
}
break;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
default:
error = FT_Err_Invalid_Glyph_Index;
goto Exit;
}
2000-05-17 01:44:38 +02:00
*abitglyph = bitglyph;
2000-05-30 18:49:14 +02:00
Exit:
return error;
2000-05-17 01:44:38 +02:00
Fail:
FREE( glyph );
goto Exit;
}
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* <Function> */
/* FT_Get_Glyph_Outline */
/* */
/* <Description> */
/* A function used to directly return a bitmap glyph image from a */
/* face. This is faster than calling FT_Load_Glyph() + */
/* FT_Get_Outline_Bitmap(). */
/* */
/* <Input> */
/* face :: A handle to the source face object. */
/* glyph_index :: A glyph index into face */
/* load_flags :: Load flags (see FT_LOAD_FLAG_XXXX constants). */
/* */
/* <Output> */
/* vecglyph :: A pointer to the new outline glyph. */
/* */
/* <Return> */
/* FreeType error code. 0 means success. */
/* */
/* <Note> */
/* This function will fail if the load flags FT_LOAD_NO_OUTLINE and */
/* FT_LOAD_NO_RECURSE are set. */
/* */
FT_EXPORT_FUNC( FT_Error ) FT_Get_Glyph_Outline(
FT_Face face,
FT_UInt glyph_index,
FT_UInt load_flags,
FT_OutlineGlyph* vecglyph )
{
FT_Error error;
FT_Memory memory;
FT_OutlineGlyph glyph;
2000-05-30 18:49:14 +02:00
/* test for valid `face' delayed to FT_Load_Glyph() */
if ( !vecglyph )
return FT_Err_Invalid_Argument;
*vecglyph = 0;
2000-05-17 01:44:38 +02:00
- MAJOR INTERNAL REDESIGN: A lot of internal modifications have been performed lately on the source in order to provide the following enhancements: - more generic module support: The FT_Module type is now defined to represent a handle to a given module. The file <freetype/ftmodule.h> contains the FT_Module_Class definition, as well as the module-loading public API The FT_Driver type is still defined, and still represents a pointer to a font driver. Note that FT_Add_Driver is replaced by FT_Add_Module, FT_Get_Driver by FT_Get_Module, etc.. - support for generic glyph image types: The FT_Renderer type is a pointer to a module used to perform various operations on glyph image. Each renderer is capable of handling images in a single format (e.g. ft_glyph_format_outline). Its functions are used to: - transform an glyph image - render a glyph image into a bitmap - return the control box (dimensions) of a given glyph image The scan converters "ftraster.c" and "ftgrays.c" have been moved to the new directory "src/renderer", and are used to provide two default renderer modules. One corresponds to the "standard" scan-converter, the other to the "smooth" one. The current renderer can be set through the new function FT_Set_Renderer. The old raster-related function FT_Set_Raster, FT_Get_Raster and FT_Set_Raster_Mode have now disappeared, in favor of the new: FT_Get_Renderer FT_Set_Renderer see the file <freetype/ftrender.h> for more details.. These changes were necessary to properly support different scalable formats in the future, like bi-color glyphs, etc.. - glyph loader object: A new internal object, called a 'glyph loader' has been introduced in the base layer. It is used by all scalable format font drivers to load glyphs and composites. This object has been created to reduce the code size of each driver, as each one of them basically re-implemented its functionality. See <freetype/internal/ftobjs.h> and the FT_GlyphLoader type for more information.. - FT_GlyphSlot had new fields: In order to support extended features (see below), the FT_GlyphSlot structure has a few new fields: linearHoriAdvance: this field gives the linearly scaled (i.e. scaled but unhinted) advance width for the glyph, expressed as a 16.16 fixed pixel value. This is useful to perform WYSIWYG text. linearVertAdvance: this field gives the linearly scaled advance height for the glyph (relevant in vertical glyph layouts only). This is useful to perform WYSIWYG text. Note that the two above field replace the removed "metrics2" field in the glyph slot. advance: this field is a vector that gives the transformed advance for the glyph. By default, it corresponds to the advance width, unless FT_LOAD_VERTICAL_LAYOUT was specified when calling FT_Load_Glyph or FT_Load_Char bitmap_left: this field gives the distance in integer pixels from the current pen position to the left-most pixel of a glyph image WHEN IT IS A BITMAP. It is only valid when the "format" field is set to "ft_glyph_format_bitmap", for example, after calling the new function FT_Render_Glyph. bitmap_top: this field gives the distance in integer pixels from the current pen position (located on the baseline) to the top-most pixel of the glyph image WHEN IT IS A BITMAP. Positive values correspond to upwards Y. loader: this is a new private field for the glyph slot. Client applications should not touch it.. - support for transforms and direct rendering in FT_Load_Glyph: Most of the functionality found in <freetype/ftglyph.h> has been moved to the core library. Hence, the following: - a transform can be specified for a face through FT_Set_Transform. this transform is applied by FT_Load_Glyph to scalable glyph images (i.e. NOT TO BITMAPS) before the function returns, unless the bit flag FT_LOAD_IGNORE_TRANSFORM was set in the load flags.. - once a glyph image has been loaded, it can be directly converted to a bitmap by using the new FT_Render_Glyph function. Note that this function takes the glyph image from the glyph slot, and converts it to a bitmap whose properties are returned in "face.glyph.bitmap", "face.glyph.bitmap_left" and "face.glyph.bitmap_top". The original native image might be lost after the conversion. - when using the new bit flag FT_LOAD_RENDER, the FT_Load_Glyph and FT_Load_Char functions will call FT_Render_Glyph automatically when needed.
2000-06-22 02:17:42 +02:00
/* check that RENDER and NO_RECURSE are not set */
if ( load_flags & ( FT_LOAD_RENDER | FT_LOAD_NO_RECURSE ) )
{
error = FT_Err_Invalid_Argument;
goto Exit;
}
2000-05-17 01:44:38 +02:00
/* disable the loading of embedded bitmaps */
load_flags |= FT_LOAD_NO_BITMAP;
2000-05-17 01:44:38 +02:00
error = FT_Load_Glyph( face, glyph_index, load_flags );
2000-05-30 18:49:14 +02:00
if ( error )
goto Exit;
2000-05-17 01:44:38 +02:00
/* check that we really loaded an outline */
if ( face->glyph->format != ft_glyph_format_outline )
{
error = FT_Err_Invalid_Glyph_Index;
goto Exit;
}
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
/* now, create a new outline glyph and copy everything */
memory = face->memory;
2000-05-30 18:49:14 +02:00
if ( ALLOC( glyph, sizeof ( *glyph ) ) )
goto Exit;
ft_prepare_glyph( (FT_Glyph)glyph, face, 0 );
glyph->metrics.glyph_type = ft_glyph_type_outline;
2000-05-17 01:44:38 +02:00
- MAJOR INTERNAL REDESIGN: A lot of internal modifications have been performed lately on the source in order to provide the following enhancements: - more generic module support: The FT_Module type is now defined to represent a handle to a given module. The file <freetype/ftmodule.h> contains the FT_Module_Class definition, as well as the module-loading public API The FT_Driver type is still defined, and still represents a pointer to a font driver. Note that FT_Add_Driver is replaced by FT_Add_Module, FT_Get_Driver by FT_Get_Module, etc.. - support for generic glyph image types: The FT_Renderer type is a pointer to a module used to perform various operations on glyph image. Each renderer is capable of handling images in a single format (e.g. ft_glyph_format_outline). Its functions are used to: - transform an glyph image - render a glyph image into a bitmap - return the control box (dimensions) of a given glyph image The scan converters "ftraster.c" and "ftgrays.c" have been moved to the new directory "src/renderer", and are used to provide two default renderer modules. One corresponds to the "standard" scan-converter, the other to the "smooth" one. The current renderer can be set through the new function FT_Set_Renderer. The old raster-related function FT_Set_Raster, FT_Get_Raster and FT_Set_Raster_Mode have now disappeared, in favor of the new: FT_Get_Renderer FT_Set_Renderer see the file <freetype/ftrender.h> for more details.. These changes were necessary to properly support different scalable formats in the future, like bi-color glyphs, etc.. - glyph loader object: A new internal object, called a 'glyph loader' has been introduced in the base layer. It is used by all scalable format font drivers to load glyphs and composites. This object has been created to reduce the code size of each driver, as each one of them basically re-implemented its functionality. See <freetype/internal/ftobjs.h> and the FT_GlyphLoader type for more information.. - FT_GlyphSlot had new fields: In order to support extended features (see below), the FT_GlyphSlot structure has a few new fields: linearHoriAdvance: this field gives the linearly scaled (i.e. scaled but unhinted) advance width for the glyph, expressed as a 16.16 fixed pixel value. This is useful to perform WYSIWYG text. linearVertAdvance: this field gives the linearly scaled advance height for the glyph (relevant in vertical glyph layouts only). This is useful to perform WYSIWYG text. Note that the two above field replace the removed "metrics2" field in the glyph slot. advance: this field is a vector that gives the transformed advance for the glyph. By default, it corresponds to the advance width, unless FT_LOAD_VERTICAL_LAYOUT was specified when calling FT_Load_Glyph or FT_Load_Char bitmap_left: this field gives the distance in integer pixels from the current pen position to the left-most pixel of a glyph image WHEN IT IS A BITMAP. It is only valid when the "format" field is set to "ft_glyph_format_bitmap", for example, after calling the new function FT_Render_Glyph. bitmap_top: this field gives the distance in integer pixels from the current pen position (located on the baseline) to the top-most pixel of the glyph image WHEN IT IS A BITMAP. Positive values correspond to upwards Y. loader: this is a new private field for the glyph slot. Client applications should not touch it.. - support for transforms and direct rendering in FT_Load_Glyph: Most of the functionality found in <freetype/ftglyph.h> has been moved to the core library. Hence, the following: - a transform can be specified for a face through FT_Set_Transform. this transform is applied by FT_Load_Glyph to scalable glyph images (i.e. NOT TO BITMAPS) before the function returns, unless the bit flag FT_LOAD_IGNORE_TRANSFORM was set in the load flags.. - once a glyph image has been loaded, it can be directly converted to a bitmap by using the new FT_Render_Glyph function. Note that this function takes the glyph image from the glyph slot, and converts it to a bitmap whose properties are returned in "face.glyph.bitmap", "face.glyph.bitmap_left" and "face.glyph.bitmap_top". The original native image might be lost after the conversion. - when using the new bit flag FT_LOAD_RENDER, the FT_Load_Glyph and FT_Load_Char functions will call FT_Render_Glyph automatically when needed.
2000-06-22 02:17:42 +02:00
error = FT_Outline_New( face->driver->root.library,
face->glyph->outline.n_points,
face->glyph->outline.n_contours,
&glyph->outline );
2000-05-30 18:49:14 +02:00
if ( !error )
error = FT_Outline_Copy( &face->glyph->outline, &glyph->outline );
2000-05-30 18:49:14 +02:00
if ( error )
goto Fail;
2000-05-17 01:44:38 +02:00
*vecglyph = glyph;
2000-05-30 18:49:14 +02:00
Exit:
return error;
2000-05-17 01:44:38 +02:00
Fail:
FREE( glyph );
goto Exit;
}
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* <Function> */
/* FT_Done_Glyph */
/* */
/* <Description> */
/* Destroys a given glyph. */
/* */
/* <Input> */
/* glyph :: A handle to the target glyph object. */
/* */
FT_EXPORT_FUNC( void ) FT_Done_Glyph( FT_Glyph glyph )
{
2000-05-30 18:49:14 +02:00
if ( glyph )
{
FT_Memory memory = glyph->memory;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
if ( glyph->glyph_type == ft_glyph_type_bitmap )
{
FT_BitmapGlyph bit = (FT_BitmapGlyph)glyph;
2000-05-30 18:49:14 +02:00
FREE( bit->bitmap.buffer );
}
else if ( glyph->glyph_type == ft_glyph_type_outline )
{
FT_OutlineGlyph out = (FT_OutlineGlyph)glyph;
2000-05-30 18:49:14 +02:00
if ( out->outline.flags & ft_outline_owner )
{
FREE( out->outline.points );
FREE( out->outline.contours );
FREE( out->outline.tags );
}
}
2000-05-17 01:44:38 +02:00
FREE( glyph );
}
}
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/* */
/* <Function> */
/* FT_Glyph_Get_Box */
/* */
/* <Description> */
/* Returns the glyph image's bounding box in pixels. */
/* */
/* <Input> */
/* glyph :: A handle to the target glyph object. */
/* */
/* <Output> */
/* box :: The glyph bounding box. Coordinates are expressed in */
/* _integer_ pixels, with exclusive maximal bounding values. */
/* */
/* <Note> */
/* Coordinates are relative to the glyph origin, using the Y-upwards */
/* convention. */
/* */
/* The width of the box in pixels is `box.xMax-box.xMin'; the height */
/* is `box.yMax-box.yMin'. */
/* */
FT_EXPORT_FUNC( void ) FT_Glyph_Get_Box( FT_Glyph glyph,
FT_BBox* box )
{
if ( !box )
return;
box->xMin = box->xMax = 0;
box->yMin = box->yMax = 0;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
if ( glyph )
switch ( glyph->glyph_type )
{
case ft_glyph_type_bitmap:
{
FT_BitmapGlyph bit = (FT_BitmapGlyph)glyph;
box->xMin = bit->left;
box->xMax = box->xMin + bit->bitmap.width;
box->yMax = bit->top;
box->yMin = box->yMax - bit->bitmap.rows;
}
break;
2000-05-17 01:44:38 +02:00
case ft_glyph_type_outline:
{
FT_OutlineGlyph out = (FT_OutlineGlyph)glyph;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
FT_Outline_Get_CBox( &out->outline, box );
box->xMin >>= 6;
box->yMin >>= 6;
2000-05-30 18:49:14 +02:00
box->xMax = ( box->xMax + 63 ) >> 6;
box->yMax = ( box->yMax + 63 ) >> 6;
}
break;
2000-05-17 01:44:38 +02:00
default:
;
}
}
2000-05-30 18:49:14 +02:00
/*************************************************************************/
/*************************************************************************/
/**** ****/
/**** EXPERIMENTAL EMBOLDENING/OUTLINING SUPPORT ****/
/**** ****/
/*************************************************************************/
/*************************************************************************/
2000-05-17 01:44:38 +02:00
#if 0
2000-05-30 18:49:14 +02:00
/* Compute the norm of a vector */
#ifdef FT_CONFIG_OPTION_OLD_CALCS
2000-05-30 18:49:14 +02:00
static
2000-05-30 18:49:14 +02:00
FT_Pos ft_norm( FT_Vector* vec )
{
2000-05-30 18:49:14 +02:00
FT_Int64 t1, t2;
2000-05-30 18:49:14 +02:00
MUL_64( vec->x, vec->x, t1 );
MUL_64( vec->y, vec->y, t2 );
ADD_64( t1, t2, t1 );
return (FT_Pos)SQRT_64( t1 );
}
2000-05-30 18:49:14 +02:00
#else /* FT_CONFIG_OPTION_OLD_CALCS */
static
FT_Pos ft_norm( FT_Vector* vec )
{
FT_F26Dot6 u, v, d;
FT_Int shift;
FT_ULong H, L, L2, hi, lo, med;
2000-05-30 18:49:14 +02:00
u = vec->x; if ( u < 0 ) u = -u;
v = vec->y; if ( v < 0 ) v = -v;
if ( u < v )
{
d = u;
u = v;
v = d;
}
/* check that we are not trying to normalize zero! */
2000-05-30 18:49:14 +02:00
if ( u == 0 )
return 0;
2000-05-30 18:49:14 +02:00
/* compute (u*u + v*v) on 64 bits with two 32-bit registers [H:L] */
hi = (FT_ULong)u >> 16;
lo = (FT_ULong)u & 0xFFFF;
2000-05-30 18:49:14 +02:00
med = hi * lo;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
H = hi * hi + ( med >> 15 );
med <<= 17;
2000-05-30 18:49:14 +02:00
L = lo * lo + med;
if ( L < med )
H++;
2000-05-17 01:44:38 +02:00
hi = (FT_ULong)v >> 16;
lo = (FT_ULong)v & 0xFFFF;
2000-05-30 18:49:14 +02:00
med = hi * lo;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
H += hi * hi + ( med >> 15 );
med <<= 17;
2000-05-30 18:49:14 +02:00
L2 = lo * lo + med;
if ( L2 < med )
H++;
2000-05-17 01:44:38 +02:00
L += L2;
2000-05-30 18:49:14 +02:00
if ( L < L2 )
H++;
2000-05-30 18:49:14 +02:00
/* if the value is smaller than 32 bits */
shift = 0;
2000-05-30 18:49:14 +02:00
if ( H == 0 )
{
2000-05-30 18:49:14 +02:00
while ( ( L & 0xC0000000UL ) == 0 )
{
L <<= 2;
shift++;
}
2000-05-30 18:49:14 +02:00
return ( FT_Sqrt32( L ) >> shift );
}
else
{
2000-05-30 18:49:14 +02:00
while ( H )
{
2000-05-30 18:49:14 +02:00
L = ( L >> 2 ) | ( H << 30 );
H >>= 2;
shift++;
}
2000-05-30 18:49:14 +02:00
return ( FT_Sqrt32( L ) << shift );
}
}
2000-05-30 18:49:14 +02:00
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
static
int ft_test_extrema( FT_Outline* outline,
int n )
{
2000-05-30 18:49:14 +02:00
FT_Vector *prev, *cur, *next;
FT_Pos product;
FT_Int first, last;
2000-05-30 18:49:14 +02:00
/* we need to compute the `previous' and `next' point */
/* for these extrema. */
cur = outline->points + n;
prev = cur - 1;
next = cur + 1;
2000-05-17 01:44:38 +02:00
first = 0;
for ( c = 0; c < outline->n_contours; c++ )
{
last = outline->contours[c];
if ( n == first )
prev = outline->points + last;
2000-05-17 01:44:38 +02:00
if ( n == last )
next = outline->points + first;
2000-05-17 01:44:38 +02:00
first = last + 1;
}
2000-05-30 18:49:14 +02:00
product = FT_MulDiv( cur->x - prev->x, /* in.x */
next->y - cur->y, /* out.y */
2000-05-30 18:49:14 +02:00
0x40 )
-
FT_MulDiv( cur->y - prev->y, /* in.y */
next->x - cur->x, /* out.x */
0x40 );
2000-05-30 18:49:14 +02:00
if ( product )
product = product > 0 ? 1 : -1;
2000-05-30 18:49:14 +02:00
return product;
}
2000-05-30 18:49:14 +02:00
/* Compute the orientation of path filling. It differs between TrueType */
/* and Type1 formats. We could use the `ft_outline_reverse_fill' flag, */
/* but it is better to re-compute it directly (it seems that this flag */
/* isn't correctly set for some weird composite glyphs currently). */
2000-05-30 18:49:14 +02:00
/* */
/* We do this by computing bounding box points, and computing their */
/* curvature. */
/* */
/* The function returns either 1 or -1. */
/* */
static
int ft_get_orientation( FT_Outline* outline )
{
2000-05-30 18:49:14 +02:00
FT_BBox box;
FT_BBox indices;
int n, last;
2000-05-30 18:49:14 +02:00
indices.xMin = -1;
indices.yMin = -1;
indices.xMax = -1;
indices.yMax = -1;
box.xMin = box.yMin = 32767;
box.xMax = box.yMax = -32768;
/* is it empty ? */
if ( outline->n_contours < 1 )
return 1;
last = outline->contours[outline->n_contours - 1];
for ( n = 0; n <= last; n++ )
{
FT_Pos x, y;
x = outline->points[n].x;
if ( x < box.xMin )
{
box.xMin = x;
indices.xMin = n;
}
if ( x > box.xMax )
{
box.xMax = x;
indices.xMax = n;
}
y = outline->points[n].y;
if ( y < box.yMin )
{
box.yMin = y;
indices.yMin = n;
}
if ( y > box.yMax )
{
box.yMax = y;
indices.yMax = n;
}
}
/* test orientation of the xmin */
return ft_test_extrema( outline, indices.xMin ) ||
ft_test_extrema( outline, indices.yMin ) ||
ft_test_extrema( outline, indices.xMax ) ||
ft_test_extrema( outline, indices.yMax ) ||
1; /* this is an empty glyph? */
}
static
2000-05-30 18:49:14 +02:00
FT_Error ft_embolden( FT_Face original,
FT_Outline* outline,
FT_Pos* advance )
{
2000-05-30 18:49:14 +02:00
FT_Vector u, v;
FT_Vector* points;
FT_Vector cur, prev, next;
FT_Pos distance;
int c, n, first, orientation;
2000-05-30 18:49:14 +02:00
UNUSED( advance );
2000-05-30 18:49:14 +02:00
/* compute control distance */
distance = FT_MulFix( original->em_size / 60,
original->size->metrics.y_scale );
2000-05-30 18:49:14 +02:00
orientation = ft_get_orientation( &original->glyph->outline );
2000-05-30 18:49:14 +02:00
points = original->glyph->outline.points;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
first = 0;
for ( c = 0; c < outline->n_contours; c++ )
{
int last = outline->contours[c];
2000-05-30 18:49:14 +02:00
prev = points[last];
2000-05-30 18:49:14 +02:00
for ( n = first; n <= last; n++ )
{
FT_Pos norm, delta, d;
FT_Vector in, out;
2000-05-30 18:49:14 +02:00
cur = points[n];
if ( n < last ) next = points[n + 1];
else next = points[first];
2000-05-30 18:49:14 +02:00
/* compute the in and out vectors */
in.x = cur.x - prev.x;
in.y = cur.y - prev.y;
2000-05-30 18:49:14 +02:00
out.x = next.x - cur.x;
out.y = next.y - cur.y;
2000-05-30 18:49:14 +02:00
/* compute U and V */
norm = ft_norm( &in );
u.x = orientation * FT_DivFix( in.y, norm );
u.y = orientation * -FT_DivFix( in.x, norm );
2000-05-30 18:49:14 +02:00
norm = ft_norm( &out );
v.x = orientation * FT_DivFix( out.y, norm );
v.y = orientation * -FT_DivFix( out.x, norm );
2000-05-30 18:49:14 +02:00
d = distance;
if ( ( outline->flags[n] & FT_Curve_Tag_On ) == 0 )
2000-05-30 18:49:14 +02:00
d *= 2;
2000-05-30 18:49:14 +02:00
/* Check discriminant for parallel vectors */
delta = FT_MulFix( u.x, v.y ) - FT_MulFix( u.y, v.x );
if ( delta > FT_BOLD_THRESHOLD || delta < -FT_BOLD_THRESHOLD )
{
/* Move point -- compute A and B */
FT_Pos x, y, A, B;
2000-05-17 01:44:38 +02:00
2000-05-30 18:49:14 +02:00
A = d + FT_MulFix( cur.x, u.x ) + FT_MulFix( cur.y, u.y );
B = d + FT_MulFix( cur.x, v.x ) + FT_MulFix( cur.y, v.y );
2000-05-30 18:49:14 +02:00
x = FT_MulFix( A, v.y ) - FT_MulFix( B, u.y );
y = FT_MulFix( B, u.x ) - FT_MulFix( A, v.x );
2000-05-30 18:49:14 +02:00
outline->points[n].x = distance + FT_DivFix( x, delta );
outline->points[n].y = distance + FT_DivFix( y, delta );
}
else
{
/* Vectors are nearly parallel */
FT_Pos x, y;
2000-05-30 18:49:14 +02:00
x = distance + cur.x + FT_MulFix( d, u.x + v.x ) / 2;
y = distance + cur.y + FT_MulFix( d, u.y + v.y ) / 2;
2000-05-30 18:49:14 +02:00
outline->points[n].x = x;
outline->points[n].y = y;
}
prev = cur;
}
first = last + 1;
}
if ( advance )
*advance = ( *advance + distance * 4 ) & -64;
return 0;
}
2000-05-30 18:49:14 +02:00
#endif /* 0 -- EXPERIMENTAL STUFF! */
/* END */