Hash :
2fb8eda5
Author :
Date :
2023-06-01T19:38:36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/****************************************************************************
*
* ftsvg.c
*
* The FreeType SVG renderer interface (body).
*
* Copyright (C) 2022-2023 by
* David Turner, Robert Wilhelm, Werner Lemberg, and Moazin Khatti.
*
* 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.
*
*/
#include <freetype/internal/ftdebug.h>
#include <freetype/internal/ftserv.h>
#include <freetype/internal/services/svprop.h>
#include <freetype/otsvg.h>
#include <freetype/internal/svginterface.h>
#include <freetype/ftbbox.h>
#include "ftsvg.h"
#include "svgtypes.h"
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, usued to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT otsvg
#ifdef FT_CONFIG_OPTION_SVG
/* ft_svg_init */
static FT_Error
ft_svg_init( FT_Module module )
{
SVG_Renderer render = (SVG_Renderer)module;
FT_Error error = FT_Err_Ok;
render->loaded = FALSE;
render->hooks_set = FALSE;
return error;
}
static void
ft_svg_done( FT_Module module )
{
SVG_Renderer render = (SVG_Renderer)module;
if ( render->loaded == TRUE &&
render->hooks_set == TRUE )
render->hooks.free_svg( &render->state );
render->loaded = FALSE;
}
static FT_Error
ft_svg_preset_slot( FT_Module module,
FT_GlyphSlot slot,
FT_Bool cache )
{
SVG_Renderer svg_renderer = (SVG_Renderer)module;
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" ));
return FT_THROW( Missing_SVG_Hooks );
}
if ( svg_renderer->loaded == FALSE )
{
FT_TRACE3(( "ft_svg_preset_slot: first presetting call,"
" calling init hook\n" ));
hooks.init_svg( &svg_renderer->state );
svg_renderer->loaded = TRUE;
}
return hooks.preset_slot( slot, cache, &svg_renderer->state );
}
static FT_Error
ft_svg_render( FT_Renderer renderer,
FT_GlyphSlot slot,
FT_Render_Mode mode,
const FT_Vector* origin )
{
SVG_Renderer svg_renderer = (SVG_Renderer)renderer;
FT_Library library = renderer->root.library;
FT_Memory memory = library->memory;
FT_Error error;
FT_ULong size_image_buffer;
SVG_RendererHooks hooks = svg_renderer->hooks;
FT_UNUSED( mode );
FT_UNUSED( origin );
if ( mode != FT_RENDER_MODE_NORMAL )
return FT_THROW( Bad_Argument );
if ( svg_renderer->hooks_set == FALSE )
{
FT_TRACE1(( "Hooks are NOT set. Can't render OT-SVG glyphs\n" ));
return FT_THROW( Missing_SVG_Hooks );
}
if ( svg_renderer->loaded == FALSE )
{
FT_TRACE3(( "ft_svg_render: first rendering, calling init hook\n" ));
error = hooks.init_svg( &svg_renderer->state );
svg_renderer->loaded = TRUE;
}
ft_svg_preset_slot( (FT_Module)renderer, slot, TRUE );
size_image_buffer = (FT_ULong)slot->bitmap.pitch * slot->bitmap.rows;
/* No `FT_QALLOC` here since we need a clean, empty canvas */
/* to start with. */
if ( FT_ALLOC( slot->bitmap.buffer, size_image_buffer ) )
return error;
error = hooks.render_svg( slot, &svg_renderer->state );
if ( error )
FT_FREE( slot->bitmap.buffer );
else
slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
return error;
}
static const SVG_Interface svg_interface =
{
ft_svg_preset_slot /* Preset_Bitmap_Func preset_slot */
};
static FT_Error
ft_svg_property_set( FT_Module module,
const char* property_name,
const void* value,
FT_Bool value_is_string )
{
FT_Error error = FT_Err_Ok;
SVG_Renderer renderer = (SVG_Renderer)module;
if ( !ft_strcmp( property_name, "svg-hooks" ) )
{
SVG_RendererHooks* hooks;
if ( value_is_string == TRUE )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
hooks = (SVG_RendererHooks*)value;
if ( !hooks->init_svg ||
!hooks->free_svg ||
!hooks->render_svg ||
!hooks->preset_slot )
{
FT_TRACE0(( "ft_svg_property_set:"
" SVG rendering hooks not set because\n" ));
FT_TRACE0(( " "
" at least one function pointer is NULL\n" ));
error = FT_THROW( Invalid_Argument );
goto Exit;
}
renderer->hooks = *hooks;
renderer->hooks_set = TRUE;
}
else
error = FT_THROW( Missing_Property );
Exit:
return error;
}
static FT_Error
ft_svg_property_get( FT_Module module,
const char* property_name,
void* value )
{
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;
}
FT_DEFINE_SERVICE_PROPERTIESREC(
ft_svg_service_properties,
ft_svg_property_set, /* FT_Properties_SetFunc set_property */
ft_svg_property_get /* FT_Properties_GetFunc get_property */
)
FT_DEFINE_SERVICEDESCREC1(
ft_svg_services,
FT_SERVICE_ID_PROPERTIES, &ft_svg_service_properties )
FT_CALLBACK_DEF( FT_Module_Interface )
ft_svg_get_interface( FT_Module module,
const char* ft_svg_interface )
{
FT_Module_Interface result;
FT_UNUSED( module );
result = ft_service_list_lookup( ft_svg_services, ft_svg_interface );
if ( result )
return result;
return 0;
}
static FT_Error
ft_svg_transform( FT_Renderer renderer,
FT_GlyphSlot slot,
const FT_Matrix* _matrix,
const FT_Vector* _delta )
{
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_UNUSED( renderer );
if ( !matrix )
{
tmp_matrix.xx = 0x10000;
tmp_matrix.xy = 0;
tmp_matrix.yx = 0;
tmp_matrix.yy = 0x10000;
matrix = &tmp_matrix;
}
if ( !delta )
{
tmp_delta.x = 0;
tmp_delta.y = 0;
delta = &tmp_delta;
}
a = doc->transform;
b = *matrix;
FT_Matrix_Multiply( &b, &a );
x = ADD_LONG( ADD_LONG( FT_MulFix( matrix->xx, doc->delta.x ),
FT_MulFix( matrix->xy, doc->delta.y ) ),
delta->x );
y = ADD_LONG( ADD_LONG( 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->transform = a;
return FT_Err_Ok;
}
#endif /* FT_CONFIG_OPTION_SVG */
#ifdef FT_CONFIG_OPTION_SVG
#define PUT_SVG_MODULE( a ) a
#define SVG_GLYPH_FORMAT FT_GLYPH_FORMAT_SVG
#else
#define PUT_SVG_MODULE( a ) NULL
#define SVG_GLYPH_FORMAT FT_GLYPH_FORMAT_NONE
#endif
FT_DEFINE_RENDERER(
ft_svg_renderer_class,
FT_MODULE_RENDERER,
sizeof ( SVG_RendererRec ),
"ot-svg",
0x10000L,
0x20000L,
(const void*)PUT_SVG_MODULE( &svg_interface ), /* module specific interface */
PUT_SVG_MODULE( ft_svg_init ), /* FT_Module_Constructor module_init */
PUT_SVG_MODULE( ft_svg_done ), /* FT_Module_Destructor module_done */
PUT_SVG_MODULE( ft_svg_get_interface ), /* FT_Module_Requester get_interface */
SVG_GLYPH_FORMAT,
PUT_SVG_MODULE( ft_svg_render ), /* FT_Renderer_RenderFunc render_glyph */
PUT_SVG_MODULE( ft_svg_transform ), /* FT_Renderer_TransformFunc transform_glyph */
NULL, /* FT_Renderer_GetCBoxFunc get_glyph_cbox */
NULL, /* FT_Renderer_SetModeFunc set_mode */
NULL /* FT_Raster_Funcs* raster_class */
)
/* END */