Hash :
2ecf89b4
Author :
Date :
2016-09-28T19:06:21
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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
/***************************************************************************/
/* */
/* ftglyph.c */
/* */
/* FreeType convenience functions to handle glyphs (body). */
/* */
/* Copyright 1996-2016 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. */
/* */
/***************************************************************************/
/*************************************************************************/
/* */
/* 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 <ft2build.h>
#include FT_INTERNAL_DEBUG_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
#include FT_BITMAP_H
#include FT_INTERNAL_OBJECTS_H
#include "basepic.h"
/*************************************************************************/
/* */
/* 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
/*************************************************************************/
/*************************************************************************/
/**** ****/
/**** FT_BitmapGlyph support ****/
/**** ****/
/*************************************************************************/
/*************************************************************************/
FT_CALLBACK_DEF( FT_Error )
ft_bitmap_glyph_init( FT_Glyph bitmap_glyph,
FT_GlyphSlot slot )
{
FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
FT_Error error = FT_Err_Ok;
FT_Library library = FT_GLYPH( glyph )->library;
if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
{
error = FT_THROW( Invalid_Glyph_Format );
goto Exit;
}
glyph->left = slot->bitmap_left;
glyph->top = slot->bitmap_top;
/* do lazy copying whenever possible */
if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
{
glyph->bitmap = slot->bitmap;
slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
}
else
{
FT_Bitmap_Init( &glyph->bitmap );
error = FT_Bitmap_Copy( library, &slot->bitmap, &glyph->bitmap );
}
Exit:
return error;
}
FT_CALLBACK_DEF( FT_Error )
ft_bitmap_glyph_copy( FT_Glyph bitmap_source,
FT_Glyph bitmap_target )
{
FT_Library library = bitmap_source->library;
FT_BitmapGlyph source = (FT_BitmapGlyph)bitmap_source;
FT_BitmapGlyph target = (FT_BitmapGlyph)bitmap_target;
target->left = source->left;
target->top = source->top;
return FT_Bitmap_Copy( library, &source->bitmap, &target->bitmap );
}
FT_CALLBACK_DEF( void )
ft_bitmap_glyph_done( FT_Glyph bitmap_glyph )
{
FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
FT_Library library = FT_GLYPH( glyph )->library;
FT_Bitmap_Done( library, &glyph->bitmap );
}
FT_CALLBACK_DEF( void )
ft_bitmap_glyph_bbox( FT_Glyph bitmap_glyph,
FT_BBox* cbox )
{
FT_BitmapGlyph glyph = (FT_BitmapGlyph)bitmap_glyph;
cbox->xMin = glyph->left * 64;
cbox->xMax = cbox->xMin + (FT_Pos)( glyph->bitmap.width * 64 );
cbox->yMax = glyph->top * 64;
cbox->yMin = cbox->yMax - (FT_Pos)( glyph->bitmap.rows * 64 );
}
FT_DEFINE_GLYPH(
ft_bitmap_glyph_class,
sizeof ( FT_BitmapGlyphRec ),
FT_GLYPH_FORMAT_BITMAP,
ft_bitmap_glyph_init, /* FT_Glyph_InitFunc glyph_init */
ft_bitmap_glyph_done, /* FT_Glyph_DoneFunc glyph_done */
ft_bitmap_glyph_copy, /* FT_Glyph_CopyFunc glyph_copy */
NULL, /* FT_Glyph_TransformFunc glyph_transform */
ft_bitmap_glyph_bbox, /* FT_Glyph_GetBBoxFunc glyph_bbox */
NULL /* FT_Glyph_PrepareFunc glyph_prepare */
)
/*************************************************************************/
/*************************************************************************/
/**** ****/
/**** FT_OutlineGlyph support ****/
/**** ****/
/*************************************************************************/
/*************************************************************************/
FT_CALLBACK_DEF( FT_Error )
ft_outline_glyph_init( FT_Glyph outline_glyph,
FT_GlyphSlot slot )
{
FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
FT_Error error = FT_Err_Ok;
FT_Library library = FT_GLYPH( glyph )->library;
FT_Outline* source = &slot->outline;
FT_Outline* target = &glyph->outline;
/* check format in glyph slot */
if ( slot->format != FT_GLYPH_FORMAT_OUTLINE )
{
error = FT_THROW( Invalid_Glyph_Format );
goto Exit;
}
/* allocate new outline */
error = FT_Outline_New( library,
(FT_UInt)source->n_points,
source->n_contours,
&glyph->outline );
if ( error )
goto Exit;
FT_Outline_Copy( source, target );
Exit:
return error;
}
FT_CALLBACK_DEF( void )
ft_outline_glyph_done( FT_Glyph outline_glyph )
{
FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
FT_Outline_Done( FT_GLYPH( glyph )->library, &glyph->outline );
}
FT_CALLBACK_DEF( FT_Error )
ft_outline_glyph_copy( FT_Glyph outline_source,
FT_Glyph outline_target )
{
FT_OutlineGlyph source = (FT_OutlineGlyph)outline_source;
FT_OutlineGlyph target = (FT_OutlineGlyph)outline_target;
FT_Error error;
FT_Library library = FT_GLYPH( source )->library;
error = FT_Outline_New( library,
(FT_UInt)source->outline.n_points,
source->outline.n_contours,
&target->outline );
if ( !error )
FT_Outline_Copy( &source->outline, &target->outline );
return error;
}
FT_CALLBACK_DEF( void )
ft_outline_glyph_transform( FT_Glyph outline_glyph,
const FT_Matrix* matrix,
const FT_Vector* delta )
{
FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
if ( matrix )
FT_Outline_Transform( &glyph->outline, matrix );
if ( delta )
FT_Outline_Translate( &glyph->outline, delta->x, delta->y );
}
FT_CALLBACK_DEF( void )
ft_outline_glyph_bbox( FT_Glyph outline_glyph,
FT_BBox* bbox )
{
FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
FT_Outline_Get_CBox( &glyph->outline, bbox );
}
FT_CALLBACK_DEF( FT_Error )
ft_outline_glyph_prepare( FT_Glyph outline_glyph,
FT_GlyphSlot slot )
{
FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph;
slot->format = FT_GLYPH_FORMAT_OUTLINE;
slot->outline = glyph->outline;
slot->outline.flags &= ~FT_OUTLINE_OWNER;
return FT_Err_Ok;
}
FT_DEFINE_GLYPH(
ft_outline_glyph_class,
sizeof ( FT_OutlineGlyphRec ),
FT_GLYPH_FORMAT_OUTLINE,
ft_outline_glyph_init, /* FT_Glyph_InitFunc glyph_init */
ft_outline_glyph_done, /* FT_Glyph_DoneFunc glyph_done */
ft_outline_glyph_copy, /* FT_Glyph_CopyFunc glyph_copy */
ft_outline_glyph_transform, /* FT_Glyph_TransformFunc glyph_transform */
ft_outline_glyph_bbox, /* FT_Glyph_GetBBoxFunc glyph_bbox */
ft_outline_glyph_prepare /* FT_Glyph_PrepareFunc glyph_prepare */
)
/*************************************************************************/
/*************************************************************************/
/**** ****/
/**** FT_Glyph class and API ****/
/**** ****/
/*************************************************************************/
/*************************************************************************/
static FT_Error
ft_new_glyph( FT_Library library,
const FT_Glyph_Class* clazz,
FT_Glyph* aglyph )
{
FT_Memory memory = library->memory;
FT_Error error;
FT_Glyph glyph = NULL;
*aglyph = NULL;
if ( !FT_ALLOC( glyph, clazz->glyph_size ) )
{
glyph->library = library;
glyph->clazz = clazz;
glyph->format = clazz->glyph_format;
*aglyph = glyph;
}
return error;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( FT_Error )
FT_Glyph_Copy( FT_Glyph source,
FT_Glyph *target )
{
FT_Glyph copy;
FT_Error error;
const FT_Glyph_Class* clazz;
/* check arguments */
if ( !target || !source || !source->clazz )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
*target = NULL;
if ( !source || !source->clazz )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
clazz = source->clazz;
error = ft_new_glyph( source->library, clazz, © );
if ( error )
goto Exit;
copy->advance = source->advance;
copy->format = source->format;
if ( clazz->glyph_copy )
error = clazz->glyph_copy( source, copy );
if ( error )
FT_Done_Glyph( copy );
else
*target = copy;
Exit:
return error;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( FT_Error )
FT_Get_Glyph( FT_GlyphSlot slot,
FT_Glyph *aglyph )
{
FT_Library library;
FT_Error error;
FT_Glyph glyph;
const FT_Glyph_Class* clazz = NULL;
if ( !slot )
return FT_THROW( Invalid_Slot_Handle );
library = slot->library;
if ( !aglyph )
return FT_THROW( Invalid_Argument );
/* if it is a bitmap, that's easy :-) */
if ( slot->format == FT_GLYPH_FORMAT_BITMAP )
clazz = FT_BITMAP_GLYPH_CLASS_GET;
/* if it is an outline */
else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
clazz = FT_OUTLINE_GLYPH_CLASS_GET;
else
{
/* try to find a renderer that supports the glyph image format */
FT_Renderer render = FT_Lookup_Renderer( library, slot->format, 0 );
if ( render )
clazz = &render->glyph_class;
}
if ( !clazz )
{
error = FT_THROW( Invalid_Glyph_Format );
goto Exit;
}
/* create FT_Glyph object */
error = ft_new_glyph( library, clazz, &glyph );
if ( error )
goto Exit;
/* copy advance while converting 26.6 to 16.16 format */
glyph->advance.x = slot->advance.x * 1024;
glyph->advance.y = slot->advance.y * 1024;
/* now import the image from the glyph slot */
error = clazz->glyph_init( glyph, slot );
/* if an error occurred, destroy the glyph */
if ( error )
FT_Done_Glyph( glyph );
else
*aglyph = glyph;
Exit:
return error;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( FT_Error )
FT_Glyph_Transform( FT_Glyph glyph,
FT_Matrix* matrix,
FT_Vector* delta )
{
FT_Error error = FT_Err_Ok;
if ( !glyph || !glyph->clazz )
error = FT_THROW( Invalid_Argument );
else
{
const FT_Glyph_Class* clazz = glyph->clazz;
if ( clazz->glyph_transform )
{
/* transform glyph image */
clazz->glyph_transform( glyph, matrix, delta );
/* transform advance vector */
if ( matrix )
FT_Vector_Transform( &glyph->advance, matrix );
}
else
error = FT_THROW( Invalid_Glyph_Format );
}
return error;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( void )
FT_Glyph_Get_CBox( FT_Glyph glyph,
FT_UInt bbox_mode,
FT_BBox *acbox )
{
const FT_Glyph_Class* clazz;
if ( !acbox )
return;
acbox->xMin = acbox->yMin = acbox->xMax = acbox->yMax = 0;
if ( !glyph || !glyph->clazz )
return;
clazz = glyph->clazz;
if ( !clazz->glyph_bbox )
return;
/* retrieve bbox in 26.6 coordinates */
clazz->glyph_bbox( glyph, acbox );
/* perform grid fitting if needed */
if ( bbox_mode == FT_GLYPH_BBOX_GRIDFIT ||
bbox_mode == FT_GLYPH_BBOX_PIXELS )
{
acbox->xMin = FT_PIX_FLOOR( acbox->xMin );
acbox->yMin = FT_PIX_FLOOR( acbox->yMin );
acbox->xMax = FT_PIX_CEIL( acbox->xMax );
acbox->yMax = FT_PIX_CEIL( acbox->yMax );
}
/* convert to integer pixels if needed */
if ( bbox_mode == FT_GLYPH_BBOX_TRUNCATE ||
bbox_mode == FT_GLYPH_BBOX_PIXELS )
{
acbox->xMin >>= 6;
acbox->yMin >>= 6;
acbox->xMax >>= 6;
acbox->yMax >>= 6;
}
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( FT_Error )
FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
FT_Render_Mode render_mode,
FT_Vector* origin,
FT_Bool destroy )
{
FT_GlyphSlotRec dummy;
FT_GlyphSlot_InternalRec dummy_internal;
FT_Error error = FT_Err_Ok;
FT_Glyph b, glyph;
FT_BitmapGlyph bitmap = NULL;
const FT_Glyph_Class* clazz;
/* FT_BITMAP_GLYPH_CLASS_GET dereferences `library' in PIC mode */
FT_Library library;
/* check argument */
if ( !the_glyph )
goto Bad;
glyph = *the_glyph;
if ( !glyph )
goto Bad;
clazz = glyph->clazz;
library = glyph->library;
if ( !library || !clazz )
goto Bad;
/* when called with a bitmap glyph, do nothing and return successfully */
if ( clazz == FT_BITMAP_GLYPH_CLASS_GET )
goto Exit;
if ( !clazz->glyph_prepare )
goto Bad;
/* we render the glyph into a glyph bitmap using a `dummy' glyph slot */
/* then calling FT_Render_Glyph_Internal() */
FT_ZERO( &dummy );
FT_ZERO( &dummy_internal );
dummy.internal = &dummy_internal;
dummy.library = library;
dummy.format = clazz->glyph_format;
/* create result bitmap glyph */
error = ft_new_glyph( library, FT_BITMAP_GLYPH_CLASS_GET, &b );
if ( error )
goto Exit;
bitmap = (FT_BitmapGlyph)b;
#if 1
/* if `origin' is set, translate the glyph image */
if ( origin )
FT_Glyph_Transform( glyph, 0, origin );
#else
FT_UNUSED( origin );
#endif
/* prepare dummy slot for rendering */
error = clazz->glyph_prepare( glyph, &dummy );
if ( !error )
error = FT_Render_Glyph_Internal( glyph->library, &dummy, render_mode );
#if 1
if ( !destroy && origin )
{
FT_Vector v;
v.x = -origin->x;
v.y = -origin->y;
FT_Glyph_Transform( glyph, 0, &v );
}
#endif
if ( error )
goto Exit;
/* in case of success, copy the bitmap to the glyph bitmap */
error = ft_bitmap_glyph_init( (FT_Glyph)bitmap, &dummy );
if ( error )
goto Exit;
/* copy advance */
bitmap->root.advance = glyph->advance;
if ( destroy )
FT_Done_Glyph( glyph );
*the_glyph = FT_GLYPH( bitmap );
Exit:
if ( error && bitmap )
FT_Done_Glyph( FT_GLYPH( bitmap ) );
return error;
Bad:
error = FT_THROW( Invalid_Argument );
goto Exit;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( void )
FT_Done_Glyph( FT_Glyph glyph )
{
if ( glyph )
{
FT_Memory memory = glyph->library->memory;
const FT_Glyph_Class* clazz = glyph->clazz;
if ( clazz->glyph_done )
clazz->glyph_done( glyph );
FT_FREE( glyph );
}
}
/* END */