From b84ae3420a02cd86dc5b43860513193ea9119b2c Mon Sep 17 00:00:00 2001 From: Moazin Khatti Date: Sat, 24 Aug 2019 14:58:31 +0500 Subject: [PATCH] Minor doc and styling improvements. --- devel/ftoption.h | 8 +++-- include/freetype/config/ftoption.h | 9 ++--- include/freetype/ftglyph.h | 16 +++++---- include/freetype/internal/ftobjs.h | 1 + include/freetype/internal/fttrace.h | 2 +- include/freetype/internal/sfnt.h | 5 +-- include/freetype/internal/svginterface.h | 1 + include/freetype/internal/tttypes.h | 1 + include/freetype/otsvg.h | 41 +++++++++++++++------ src/base/ftglyph.c | 25 ++++++------- src/cff/cffgload.c | 3 ++ src/sfnt/sfdriver.c | 1 + src/sfnt/ttsvg.c | 25 ++++++------- src/svg/ftsvg.c | 46 ++++++++++++++---------- src/svg/svgtypes.h | 1 + src/truetype/ttgload.c | 3 ++ 16 files changed, 120 insertions(+), 68 deletions(-) diff --git a/devel/ftoption.h b/devel/ftoption.h index 727f8d1bf..8afa42349 100644 --- a/devel/ftoption.h +++ b/devel/ftoption.h @@ -497,11 +497,15 @@ FT_BEGIN_HEADER * * OpenType SVG Glyph Support * - * If this macro is set, OpenType SVG glyphs will be supported. + * If `FT_CONFIG_OPTION_SVG` is set, OpenType SVG glyphs will be + * supported. + * If `FT_CONFIG_OPTION_DEFAULT_SVG` is also set, a default library will + * also be integrated so no external hooks will be necessary. */ #define FT_CONFIG_OPTION_SVG - #define FT_CONFIG_OPTION_DEFAULT_SVG + + /************************************************************************** * * Error Strings diff --git a/include/freetype/config/ftoption.h b/include/freetype/config/ftoption.h index fb8d2363b..0463bf922 100644 --- a/include/freetype/config/ftoption.h +++ b/include/freetype/config/ftoption.h @@ -493,18 +493,19 @@ FT_BEGIN_HEADER #undef FT_CONFIG_OPTION_USE_MODULE_ERRORS - - /************************************************************************** * * OpenType SVG Glyph Support * - * If this macro is set, OpenType SVG glyphs will be supported. + * If `FT_CONFIG_OPTION_SVG` is set, OpenType SVG glyphs will be + * supported. + * If `FT_CONFIG_OPTION_DEFAULT_SVG` is also set, a default library will + * also be integrated so no external hooks will be necessary. */ #define FT_CONFIG_OPTION_SVG - /* #define FT_CONFIG_OPTION_DEFAULT_SVG */ + /************************************************************************** * * Error Strings diff --git a/include/freetype/ftglyph.h b/include/freetype/ftglyph.h index e34644409..e80bb4b64 100644 --- a/include/freetype/ftglyph.h +++ b/include/freetype/ftglyph.h @@ -240,7 +240,7 @@ FT_BEGIN_HEADER * FT_SvgGlyphRec * * @description: - * A structure used for SVG glyph images. This really is a 'sub-class' + * A structure used for OT-SVG glyph images. This really is a 'sub-class' * of @FT_GlyphRec. * * @fields: @@ -251,7 +251,7 @@ FT_BEGIN_HEADER * A pointer to the SVG document. * * svg_document_length :: - * The length of the svg_document. + * The length of `svg_document`. * * glyph_index :: * The index of the glyph to be rendered. @@ -263,13 +263,13 @@ FT_BEGIN_HEADER * The size of the EM square. * * start_glyph_id :: - * The starting glyph ID for the glyph range that this document has. + * The first glyph ID in the glyph range is covered by this document. * * end_glyph_id :: - * The ending glyph ID for the glyph range that this document has. + * The last glyph ID in the glyph range is covered by this document. * * transform :: - * Transformation matrix to apply on the glyph while rendering. + * A 2x2 transformation matrix to apply on the glyph while rendering it. * * delta :: * Translation to apply on the glyph while rendering. @@ -283,7 +283,10 @@ FT_BEGIN_HEADER * `units_per_EM` is needed because the SVG is to be scaled in case its * viewbox size differs from `units_per_EM`. For more info, refer to * the section _Coordinate Systems and Glyph Metrics_ of the OpenType - * SVG specs. + * SVG specs. The `transform` and `delta` are stored here because for an + * SVG document, there's no way to apply a transformation without parsing + * it. Thus, we just store the transform and later the renderer can apply + * it while rendering. */ typedef struct FT_SvgGlyphRec_ { @@ -297,6 +300,7 @@ FT_BEGIN_HEADER FT_UShort end_glyph_id; FT_Matrix transform; FT_Vector delta; + } FT_SvgGlyphRec; diff --git a/include/freetype/internal/ftobjs.h b/include/freetype/internal/ftobjs.h index a89caa7d4..ea964a410 100644 --- a/include/freetype/internal/ftobjs.h +++ b/include/freetype/internal/ftobjs.h @@ -924,6 +924,7 @@ FT_BEGIN_HEADER #ifdef FT_CONFIG_OPTION_SVG void* svg_renderer_state; #endif + } FT_LibraryRec; diff --git a/include/freetype/internal/fttrace.h b/include/freetype/internal/fttrace.h index 8e7a6d076..fa6437a9d 100644 --- a/include/freetype/internal/fttrace.h +++ b/include/freetype/internal/fttrace.h @@ -39,7 +39,7 @@ FT_TRACE_DEF( mm ) /* MM interface (ftmm.c) */ FT_TRACE_DEF( psprops ) /* PS driver properties (ftpsprop.c) */ FT_TRACE_DEF( raccess ) /* resource fork accessor (ftrfork.c) */ FT_TRACE_DEF( raster ) /* monochrome rasterizer (ftraster.c) */ -FT_TRACE_DEF( otsvg ) /* ot-svg renderer (ftsvg.c) */ +FT_TRACE_DEF( otsvg ) /* ot-svg renderer (ftsvg.c) */ FT_TRACE_DEF( smooth ) /* anti-aliasing raster (ftgrays.c) */ FT_TRACE_DEF( synth ) /* bold/slant synthesizer (ftsynth.c) */ diff --git a/include/freetype/internal/sfnt.h b/include/freetype/internal/sfnt.h index 821ee1a49..628796d04 100644 --- a/include/freetype/internal/sfnt.h +++ b/include/freetype/internal/sfnt.h @@ -318,12 +318,12 @@ FT_BEGIN_HEADER * TT_Load_Svg_Doc_Func * * @description: - * Scans the SVG documents list to find the document containing the glyph + * Scan the SVG document list to find the document containing the glyph * that has the id "glyph". * * @input: * glyph :: - * The glyph slot from which pointers to SVG documents list will be + * The glyph slot from which pointers to SVG document list will be * grabbed. The results will be stored back in the slot too. * * glyph_index :: @@ -807,6 +807,7 @@ FT_BEGIN_HEADER TT_Load_Table_Func load_svg; TT_Free_Table_Func free_svg; TT_Load_Svg_Doc_Func load_svg_doc; + } SFNT_Interface; diff --git a/include/freetype/internal/svginterface.h b/include/freetype/internal/svginterface.h index 6ff6db02f..b9241184a 100644 --- a/include/freetype/internal/svginterface.h +++ b/include/freetype/internal/svginterface.h @@ -33,6 +33,7 @@ FT_BEGIN_HEADER typedef struct SVG_Interface_ { Preset_Bitmap_Func preset_slot; + } SVG_Interface; typedef SVG_Interface* SVG_Service; diff --git a/include/freetype/internal/tttypes.h b/include/freetype/internal/tttypes.h index 42a921fe7..cf227e9d9 100644 --- a/include/freetype/internal/tttypes.h +++ b/include/freetype/internal/tttypes.h @@ -1647,6 +1647,7 @@ FT_BEGIN_HEADER /* OpenType SVG Glyph Support */ void* svg; + } TT_FaceRec; diff --git a/include/freetype/otsvg.h b/include/freetype/otsvg.h index 09039d528..8979d06a9 100644 --- a/include/freetype/otsvg.h +++ b/include/freetype/otsvg.h @@ -36,7 +36,7 @@ FT_BEGIN_HEADER * SVG_Lib_Init_Func * * @description: - * A callback used to initiate the SVG Rendering port + * A callback used to initiate the SVG Rendering port. * * @input: * library :: @@ -46,7 +46,6 @@ FT_BEGIN_HEADER * @return: * FreeType error code. 0 means success. */ - typedef FT_Error (*SVG_Lib_Init_Func)( FT_Library library ); @@ -65,7 +64,6 @@ FT_BEGIN_HEADER * A instance of library. This is required to free the renderer's * state which will be held in the library. */ - typedef void (*SVG_Lib_Free_Func)( FT_Library library ); @@ -85,7 +83,6 @@ FT_BEGIN_HEADER * @return: * FreeType error code. 0 means success. */ - typedef FT_Error (*SVG_Lib_Render_Func)( FT_GlyphSlot slot ); @@ -104,11 +101,33 @@ FT_BEGIN_HEADER * @return: * FreeType error code. 0 means success. */ - typedef FT_Error (*SVG_Lib_Preset_Slot_Func)( FT_GlyphSlot slot, FT_Bool cache); + /************************************************************************** + * + * @struct: + * SVG_RendererHooks + * + * @description: + * A structure that stores the four hooks needed to render OT-SVG glyphs + * properly. The structure is publicly used to set the hooks via driver + * properties. + * + * @fields: + * init_svg :: + * The initialization hook. + * + * free_svg :: + * The cleanup hook. + * + * render_hook :: + * The render hook. + * + * preset_slot :: + * The preset hook. + */ typedef struct SVG_RendererHooks_ { /* Api Hooks for OT-SVG Rendering */ @@ -117,6 +136,7 @@ FT_BEGIN_HEADER SVG_Lib_Render_Func render_svg; SVG_Lib_Preset_Slot_Func preset_slot; + } SVG_RendererHooks; @@ -130,10 +150,10 @@ FT_BEGIN_HEADER * * @fields: * svg_document :: - * A pointer to the SVG document string. + * A pointer to the SVG document. * * svg_document_length :: - * The length of the SVG document string. + * The length of `svg_document`. * * metrics :: * A metrics object storing the size information. @@ -142,13 +162,13 @@ FT_BEGIN_HEADER * The size of the EM square. * * start_glyph_id :: - * The starting glyph ID for the glyph range that this document has. + * The first glyph ID in the glyph range is covered by this document. * * end_glyph_id :: - * The ending glyph ID for the glyph range that this document has. + * The last glyph ID in the glyph range is covered by this document. * * transform :: - * Transformation matrix to apply on the glyph while rendering. + * A 2x2 transformation matrix to apply on the glyph while rendering it. * * delta :: * Translation to apply on the glyph while rendering. @@ -173,6 +193,7 @@ FT_BEGIN_HEADER FT_UShort end_glyph_id; FT_Matrix transform; FT_Vector delta; + } FT_SVG_DocumentRec; /************************************************************************** diff --git a/src/base/ftglyph.c b/src/base/ftglyph.c index 22720602b..ce7cf602f 100644 --- a/src/base/ftglyph.c +++ b/src/base/ftglyph.c @@ -343,6 +343,7 @@ FT_SvgGlyph glyph = (FT_SvgGlyph)svg_glyph; FT_Memory memory = svg_glyph->library->memory; + /* just free the memory */ memory->free( memory, glyph->svg_document ); } @@ -356,6 +357,7 @@ FT_Error error = FT_Err_Ok; FT_Memory memory = FT_GLYPH( source )->library->memory; + if ( svg_source->format != FT_GLYPH_FORMAT_SVG ) { error = FT_THROW( Invalid_Glyph_Format ); @@ -395,15 +397,14 @@ const FT_Matrix* _matrix, const FT_Vector* _delta ) { - FT_SvgGlyph glyph = (FT_SvgGlyph)svg_glyph; + FT_SvgGlyph glyph = (FT_SvgGlyph)svg_glyph; + FT_Matrix* matrix = (FT_Matrix*)_matrix; + FT_Vector* delta = (FT_Vector*)_delta; + FT_Matrix tmp_matrix; + FT_Vector tmp_delta; + FT_Matrix a, b; + FT_Pos x, y; - FT_Matrix* matrix = (FT_Matrix*)_matrix; - FT_Vector* delta = (FT_Vector*)_delta; - - FT_Matrix tmp_matrix; - FT_Vector tmp_delta; - FT_Matrix a, b; - FT_Pos x, y; if ( !matrix ) { @@ -443,12 +444,12 @@ ft_svg_glyph_prepare( FT_Glyph svg_glyph, FT_GlyphSlot slot ) { - FT_SvgGlyph glyph = (FT_SvgGlyph)svg_glyph; - FT_Error error = FT_Err_Ok; - FT_Memory memory = svg_glyph->library->memory; - + FT_SvgGlyph glyph = (FT_SvgGlyph)svg_glyph; + FT_Error error = FT_Err_Ok; + FT_Memory memory = svg_glyph->library->memory; FT_SVG_Document document; + if ( FT_NEW( document ) ) return error; diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c index 33733896a..3c4cfe334 100644 --- a/src/cff/cffgload.c +++ b/src/cff/cffgload.c @@ -358,6 +358,7 @@ FT_UShort advanceX; FT_UShort advanceY; + if ( ( size->root.metrics.x_ppem < 1 || size->root.metrics.y_ppem < 1 ) ) { @@ -367,6 +368,7 @@ FT_TRACE3(( "Attemping to load SVG glyph\n" )); sfnt = (SFNT_Service)((TT_Face)glyph->root.face)->sfnt; + error = sfnt->load_svg_doc( (FT_GlyphSlot)glyph, glyph_index ); if( error == FT_Err_Ok ) { @@ -388,6 +390,7 @@ ((float)glyph->root.face->units_per_EM) * 64.0; glyph->root.metrics.horiAdvance = advanceX; glyph->root.metrics.vertAdvance = advanceY; + return error; } FT_TRACE3(( "Failed to load SVG glyph\n" )); diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c index f16c96b6f..39cfb5ed8 100644 --- a/src/sfnt/sfdriver.c +++ b/src/sfnt/sfdriver.c @@ -1305,6 +1305,7 @@ tt_face_get_name, /* TT_Get_Name_Func get_name */ sfnt_get_name_id, /* TT_Get_Name_ID_Func get_name_id */ + PUT_SVG_SUPPORT( tt_face_load_svg ), /* TT_Load_Table_Func load_svg */ PUT_SVG_SUPPORT( tt_face_free_svg ), diff --git a/src/sfnt/ttsvg.c b/src/sfnt/ttsvg.c index df311d682..42e6010ec 100644 --- a/src/sfnt/ttsvg.c +++ b/src/sfnt/ttsvg.c @@ -46,6 +46,7 @@ /* Memory that backs up SVG */ void* table; FT_ULong table_size; + } Svg; #undef FT_COMPONENT @@ -57,14 +58,11 @@ { FT_Error error; FT_Memory memory = face->root.memory; - FT_ULong table_size; FT_Byte* table = NULL; FT_Byte* p = NULL; - Svg* svg = NULL; - - FT_ULong offsetToSVGDocumentList; + FT_ULong offsetToSVGDocumentList; error = face->goto_table( face, TTAG_SVG, stream, &table_size ); @@ -134,16 +132,19 @@ FT_UShort end_glyph_id; FT_ULong offset; FT_ULong length; + } Svg_doc; static Svg_doc extract_svg_doc( FT_Byte* stream ) { Svg_doc doc; + + doc.start_glyph_id = FT_NEXT_USHORT( stream ); doc.end_glyph_id = FT_NEXT_USHORT( stream ); - doc.offset = FT_NEXT_ULONG( stream ); - doc.length = FT_NEXT_ULONG( stream ); + doc.offset = FT_NEXT_ULONG( stream ); + doc.length = FT_NEXT_ULONG( stream ); return doc; } @@ -242,10 +243,11 @@ } else { - *doc_offset = mid_doc.offset; - *doc_length = mid_doc.length; + *doc_offset = mid_doc.offset; + *doc_length = mid_doc.length; *start_glyph = mid_doc.start_glyph_id; *end_glyph = mid_doc.end_glyph_id; + error = FT_Err_Ok; } return error; @@ -257,12 +259,10 @@ { FT_Byte* doc_list; /* Pointer to the Svg Document List */ FT_UShort num_entries; /* Total no of entires in doc list */ - FT_ULong doc_offset; FT_ULong doc_length; FT_UShort start_glyph_id; FT_UShort end_glyph_id; - FT_ULong uncomp_size; FT_Byte* uncomp_buffer; @@ -273,6 +273,7 @@ FT_SVG_Document svg_document = glyph->other; + FT_ASSERT( !( svg == NULL ) ); doc_list = svg->svg_doc_list; @@ -336,13 +337,13 @@ glyph->other = svg_document; -Exit: + Exit: return error; } #else /* !FT_CONFIG_OPTION_SVG */ /* ANSI C doesn't like empty source files */ - typedef int _tt_cpal_dummy; + typedef int _tt_svg_dummy; #endif /* !FT_CONFIG_OPTION_SVG */ diff --git a/src/svg/ftsvg.c b/src/svg/ftsvg.c index d0e593b56..09b3561c1 100644 --- a/src/svg/ftsvg.c +++ b/src/svg/ftsvg.c @@ -23,24 +23,25 @@ #include FT_INTERNAL_SVG_INTERFACE_H #include FT_BBOX_H +#include "ftsvg.h" #include "svgtypes.h" + #ifdef FT_CONFIG_OPTION_DEFAULT_SVG #include "rsvg_port.h" #endif -#include - -#include "ftsvg.h" - #undef FT_COMPONENT #define FT_COMPONENT otsvg #ifdef FT_CONFIG_OPTION_SVG + /* ft_svg_init */ static FT_Error ft_svg_init( SVG_Renderer svg_module ) { FT_Error error = FT_Err_Ok; + + svg_module->loaded = FALSE; #ifdef FT_CONFIG_OPTION_DEFAULT_SVG FT_TRACE3(( "ft_svg_init: Default hooks being set\n" )); @@ -53,6 +54,7 @@ FT_TRACE3(( "ft_svg_init: No default hooks set\n" )); svg_module->hooks_set = FALSE; #endif + return error; } @@ -60,6 +62,8 @@ ft_svg_done( SVG_Renderer svg_module ) { FT_Library library = svg_module->root.root.library; + + if ( svg_module->loaded == TRUE && svg_module->hooks_set == TRUE ) svg_module->hooks.free_svg( library ); @@ -71,8 +75,9 @@ FT_GlyphSlot slot, FT_Bool cache ) { - SVG_Renderer svg_renderer = (SVG_Renderer)module; - SVG_RendererHooks hooks = svg_renderer->hooks; + SVG_Renderer svg_renderer = (SVG_Renderer)module; + SVG_RendererHooks hooks = svg_renderer->hooks; + if ( svg_renderer->hooks_set == FALSE ) { @@ -97,6 +102,7 @@ SVG_RendererHooks hooks = svg_renderer->hooks; + if ( svg_renderer->hooks_set == FALSE ) { FT_TRACE1(( "Hooks are NOT set. Can't render OT-SVG glyphs\n" )); @@ -111,6 +117,7 @@ } ft_svg_preset_slot( (FT_Module)renderer, slot, TRUE); + size_image_buffer = slot->bitmap.pitch * slot->bitmap.rows; FT_MEM_ALLOC( slot->bitmap.buffer, size_image_buffer); if ( error ) @@ -138,6 +145,7 @@ FT_Error error = FT_Err_Ok; SVG_Renderer renderer = (SVG_Renderer)module; + if ( !ft_strcmp( property_name, "svg_hooks" ) ) { SVG_RendererHooks* hooks; @@ -151,9 +159,8 @@ renderer->hooks_set = TRUE; } else - { error = FT_THROW( Missing_Property ); - } + return error; } @@ -165,15 +172,15 @@ FT_Error error = FT_Err_Ok; SVG_Renderer renderer = (SVG_Renderer)module; + if ( !ft_strcmp( property_name, "svg_hooks" ) ) { SVG_RendererHooks* hooks = (SVG_RendererHooks*)value; *hooks = renderer->hooks; } else - { error = FT_THROW( Missing_Property ); - } + return error; } @@ -199,6 +206,7 @@ result = ft_service_list_lookup( ft_svg_services, ft_svg_interface ); if ( result ) return result; + return 0; } @@ -208,14 +216,14 @@ const FT_Matrix* _matrix, const FT_Vector* _delta ) { - FT_SVG_Document doc = (FT_SVG_Document)slot->other; + FT_SVG_Document doc = (FT_SVG_Document)slot->other; + FT_Matrix* matrix = (FT_Matrix*)_matrix; + FT_Vector* delta = (FT_Vector*)_delta; + FT_Matrix tmp_matrix; + FT_Vector tmp_delta; + FT_Matrix a, b; + FT_Pos x, y; - FT_Matrix* matrix = (FT_Matrix*)_matrix; - FT_Vector* delta = (FT_Vector*)_delta; - FT_Matrix tmp_matrix; - FT_Vector tmp_delta; - FT_Matrix a, b; - FT_Pos x, y; if ( !matrix ) { @@ -246,9 +254,9 @@ FT_MulFix(matrix->yx, doc->delta.x), FT_MulFix(matrix->yy, doc->delta.y)), delta->y); - doc->delta.x = x; - doc->delta.y = y; + doc->delta.x = x; + doc->delta.y = y; doc->transform = a; return FT_Err_Ok; diff --git a/src/svg/svgtypes.h b/src/svg/svgtypes.h index cc34942e9..2e76b7a1b 100644 --- a/src/svg/svgtypes.h +++ b/src/svg/svgtypes.h @@ -29,6 +29,7 @@ FT_Bool loaded; FT_Bool hooks_set; SVG_RendererHooks hooks; /* Holds out hooks to the outside library */ + } SVG_RendererRec; typedef struct SVG_RendererRec_* SVG_Renderer; diff --git a/src/truetype/ttgload.c b/src/truetype/ttgload.c index a6ec379ed..fbc8f5edb 100644 --- a/src/truetype/ttgload.c +++ b/src/truetype/ttgload.c @@ -2917,8 +2917,10 @@ FT_UShort advanceX; FT_UShort advanceY; + FT_TRACE3(( "Attemping to load SVG glyph\n" )); sfnt = (SFNT_Service)((TT_Face)glyph->face)->sfnt; + error = sfnt->load_svg_doc( glyph, glyph_index ); if( error == FT_Err_Ok ) { @@ -2940,6 +2942,7 @@ ((float)glyph->face->units_per_EM) * 64.0; glyph->metrics.horiAdvance = advanceX; glyph->metrics.vertAdvance = advanceY; + return error; } FT_TRACE3(( "Failed to load SVG glyph\n" ));