adding new file
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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
diff --git a/src/base/ftutil.c b/src/base/ftutil.c
new file mode 100644
index 0000000..8987297
--- /dev/null
+++ b/src/base/ftutil.c
@@ -0,0 +1,762 @@
+#include <ft2build.h>
+#include FT_INTERNAL_DEBUG_H
+#include FT_INTERNAL_UTIL_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_memory
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** *****/
+ /***** M E M O R Y M A N A G E M E N T *****/
+ /***** *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+
+ /* documentation is in ftmemory.h */
+
+ FT_BASE_DEF( FT_Error )
+ FT_Alloc( FT_Memory memory,
+ FT_Long size,
+ void* *P )
+ {
+ FT_Assert( P != 0 );
+
+ if ( size > 0 )
+ {
+ *P = memory->alloc( memory, size );
+ if ( !*P )
+ {
+ FT_ERROR(( "FT_Alloc:" ));
+ FT_ERROR(( " Out of memory? (%ld requested)\n",
+ size ));
+
+ return FT_Err_Out_Of_Memory;
+ }
+ MEM_Set( *P, 0, size );
+ }
+ else
+ *P = NULL;
+
+ FT_TRACE7(( "FT_Alloc:" ));
+ FT_TRACE7(( " size = %ld, block = 0x%08p, ref = 0x%08p\n",
+ size, *P, P ));
+
+ return FT_Err_Ok;
+ }
+
+
+ /* documentation is in ftmemory.h */
+
+ FT_BASE_DEF( FT_Error )
+ FT_Realloc( FT_Memory memory,
+ FT_Long current,
+ FT_Long size,
+ void** P )
+ {
+ void* Q;
+
+
+ FT_Assert( P != 0 );
+
+ /* if the original pointer is NULL, call FT_Alloc() */
+ if ( !*P )
+ return FT_Alloc( memory, size, P );
+
+ /* if the new block if zero-sized, clear the current one */
+ if ( size <= 0 )
+ {
+ FT_Free( memory, P );
+ return FT_Err_Ok;
+ }
+
+ Q = memory->realloc( memory, current, size, *P );
+ if ( !Q )
+ goto Fail;
+
+ if ( size > current )
+ memset( (char*)Q + current, 0, size - current );
+
+ *P = Q;
+ return FT_Err_Ok;
+
+ Fail:
+ FT_ERROR(( "FT_Realloc:" ));
+ FT_ERROR(( " Failed (current %ld, requested %ld)\n",
+ current, size ));
+ return FT_Err_Out_Of_Memory;
+ }
+
+
+ /* documentation is in ftmemory.h */
+
+ FT_BASE_DEF( void )
+ FT_Free( FT_Memory memory,
+ void** P )
+ {
+ FT_TRACE7(( "FT_Free:" ));
+ FT_TRACE7(( " Freeing block 0x%08p, ref 0x%08p\n",
+ P, P ? *P : (void*)0 ));
+
+ if ( P && *P )
+ {
+ memory->free( memory, *P );
+ *P = 0;
+ }
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** *****/
+ /***** D O U B L Y L I N K E D L I S T S *****/
+ /***** *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+
+#undef FT_COMPONENT
+#define FT_COMPONENT trace_list
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( FT_ListNode )
+ FT_List_Find( FT_List list,
+ void* data )
+ {
+ FT_ListNode cur;
+
+
+ cur = list->head;
+ while ( cur )
+ {
+ if ( cur->data == data )
+ return cur;
+
+ cur = cur->next;
+ }
+
+ return (FT_ListNode)0;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( void )
+ FT_List_Add( FT_List list,
+ FT_ListNode node )
+ {
+ FT_ListNode before = list->tail;
+
+
+ node->next = 0;
+ node->prev = before;
+
+ if ( before )
+ before->next = node;
+ else
+ list->head = node;
+
+ list->tail = node;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( void )
+ FT_List_Insert( FT_List list,
+ FT_ListNode node )
+ {
+ FT_ListNode after = list->head;
+
+
+ node->next = after;
+ node->prev = 0;
+
+ if ( !after )
+ list->tail = node;
+ else
+ after->prev = node;
+
+ list->head = node;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( void )
+ FT_List_Remove( FT_List list,
+ FT_ListNode node )
+ {
+ FT_ListNode before, after;
+
+
+ before = node->prev;
+ after = node->next;
+
+ if ( before )
+ before->next = after;
+ else
+ list->head = after;
+
+ if ( after )
+ after->prev = before;
+ else
+ list->tail = before;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( void )
+ FT_List_Up( FT_List list,
+ FT_ListNode node )
+ {
+ FT_ListNode before, after;
+
+
+ before = node->prev;
+ after = node->next;
+
+ /* check whether we are already on top of the list */
+ if ( !before )
+ return;
+
+ before->next = after;
+
+ if ( after )
+ after->prev = before;
+ else
+ list->tail = before;
+
+ node->prev = 0;
+ node->next = list->head;
+ list->head->prev = node;
+ list->head = node;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( FT_Error )
+ FT_List_Iterate( FT_List list,
+ FT_List_Iterator iterator,
+ void* user )
+ {
+ FT_ListNode cur = list->head;
+ FT_Error error = FT_Err_Ok;
+
+
+ while ( cur )
+ {
+ FT_ListNode next = cur->next;
+
+
+ error = iterator( cur, user );
+ if ( error )
+ break;
+
+ cur = next;
+ }
+
+ return error;
+ }
+
+
+ /* documentation is in ftlist.h */
+
+ FT_EXPORT_DEF( void )
+ FT_List_Finalize( FT_List list,
+ FT_List_Destructor destroy,
+ FT_Memory memory,
+ void* user )
+ {
+ FT_ListNode cur;
+
+
+ cur = list->head;
+ while ( cur )
+ {
+ FT_ListNode next = cur->next;
+ void* data = cur->data;
+
+
+ if ( destroy )
+ destroy( memory, data, user );
+
+ FREE( cur );
+ cur = next;
+ }
+
+ list->head = 0;
+ list->tail = 0;
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** *****/
+ /***** G L Y P H L O A D E R *****/
+ /***** *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+
+ /*************************************************************************/
+ /* */
+ /* The glyph loader is a simple object which is used to load a set of */
+ /* glyphs easily. It is critical for the correct loading of composites. */
+ /* */
+ /* Ideally, one can see it as a stack of abstract `glyph' objects. */
+ /* */
+ /* loader.base Is really the bottom of the stack. It describes a */
+ /* single glyph image made of the juxtaposition of */
+ /* several glyphs (those `in the stack'). */
+ /* */
+ /* loader.current Describes the top of the stack, on which a new */
+ /* glyph can be loaded. */
+ /* */
+ /* Rewind Clears the stack. */
+ /* Prepare Set up `loader.current' for addition of a new glyph */
+ /* image. */
+ /* Add Add the `current' glyph image to the `base' one, */
+ /* and prepare for another one. */
+ /* */
+ /* The glyph loader is now a base object. Each driver used to */
+ /* re-implement it in one way or the other, which wasted code and */
+ /* energy. */
+ /* */
+ /*************************************************************************/
+
+
+ /* create a new glyph loader */
+ FT_BASE_DEF( FT_Error )
+ FT_GlyphLoader _New( FT_Memory memory,
+ FT_GlyphLoader *aloader )
+ {
+ FT_GlyphLoader loader;
+ FT_Error error;
+
+
+ if ( !ALLOC( loader, sizeof ( *loader ) ) )
+ {
+ loader->memory = memory;
+ *aloader = loader;
+ }
+ return error;
+ }
+
+
+ /* rewind the glyph loader - reset counters to 0 */
+ FT_BASE_DEF( void )
+ FT_GlyphLoader _Rewind( FT_GlyphLoader loader )
+ {
+ FT_GlyphLoad* base = &loader->base;
+ FT_GlyphLoad* current = &loader->current;
+
+
+ base->outline.n_points = 0;
+ base->outline.n_contours = 0;
+ base->num_subglyphs = 0;
+
+ *current = *base;
+ }
+
+
+ /* reset the glyph loader, frees all allocated tables */
+ /* and starts from zero */
+ FT_BASE_DEF( void )
+ FT_GlyphLoader _Reset( FT_GlyphLoader loader )
+ {
+ FT_Memory memory = loader->memory;
+
+
+ FREE( loader->base.outline.points );
+ FREE( loader->base.outline.tags );
+ FREE( loader->base.outline.contours );
+ FREE( loader->base.extra_points );
+ FREE( loader->base.subglyphs );
+
+ loader->max_points = 0;
+ loader->max_contours = 0;
+ loader->max_subglyphs = 0;
+
+ FT_GlyphLoader _Rewind( loader );
+ }
+
+
+ /* delete a glyph loader */
+ FT_BASE_DEF( void )
+ FT_GlyphLoader _Done( FT_GlyphLoader loader )
+ {
+ if ( loader )
+ {
+ FT_Memory memory = loader->memory;
+
+
+ FT_GlyphLoader _Reset( loader );
+ FREE( loader );
+ }
+ }
+
+
+ /* re-adjust the `current' outline fields */
+ static void
+ FT_GlyphLoader _Adjust_Points( FT_GlyphLoader loader )
+ {
+ FT_Outline* base = &loader->base.outline;
+ FT_Outline* current = &loader->current.outline;
+
+
+ current->points = base->points + base->n_points;
+ current->tags = base->tags + base->n_points;
+ current->contours = base->contours + base->n_contours;
+
+ /* handle extra points table - if any */
+ if ( loader->use_extra )
+ loader->current.extra_points =
+ loader->base.extra_points + base->n_points;
+ }
+
+
+ FT_BASE_DEF( FT_Error )
+ FT_GlyphLoader _Create_Extra( FT_GlyphLoader loader )
+ {
+ FT_Error error;
+ FT_Memory memory = loader->memory;
+
+
+ if ( !ALLOC_ARRAY( loader->base.extra_points,
+ loader->max_points, FT_Vector ) )
+ {
+ loader->use_extra = 1;
+ FT_GlyphLoader _Adjust_Points( loader );
+ }
+ return error;
+ }
+
+
+ /* re-adjust the `current' subglyphs field */
+ static void
+ FT_GlyphLoader _Adjust_Subglyphs( FT_GlyphLoader loader )
+ {
+ FT_GlyphLoad* base = &loader->base;
+ FT_GlyphLoad* current = &loader->current;
+
+
+ current->subglyphs = base->subglyphs + base->num_subglyphs;
+ }
+
+
+ /* Ensure that we can add `n_points' and `n_contours' to our glyph. this */
+ /* function reallocates its outline tables if necessary. Note that it */
+ /* DOESN'T change the number of points within the loader! */
+ /* */
+ FT_BASE_DEF( FT_Error )
+ FT_GlyphLoader _Check_Points( FT_GlyphLoader loader,
+ FT_UInt n_points,
+ FT_UInt n_contours )
+ {
+ FT_Memory memory = loader->memory;
+ FT_Error error = FT_Err_Ok;
+ FT_Outline* base = &loader->base.outline;
+ FT_Outline* current = &loader->current.outline;
+ FT_Bool adjust = 1;
+
+ FT_UInt new_max, old_max;
+
+
+ /* check points & tags */
+ new_max = base->n_points + current->n_points + n_points;
+ old_max = loader->max_points;
+
+ if ( new_max > old_max )
+ {
+ new_max = ( new_max + 7 ) & -8;
+
+ if ( REALLOC_ARRAY( base->points, old_max, new_max, FT_Vector ) ||
+ REALLOC_ARRAY( base->tags, old_max, new_max, FT_Byte ) )
+ goto Exit;
+
+ if ( loader->use_extra &&
+ REALLOC_ARRAY( loader->base.extra_points, old_max,
+ new_max, FT_Vector ) )
+ goto Exit;
+
+ adjust = 1;
+ loader->max_points = new_max;
+ }
+
+ /* check contours */
+ old_max = loader->max_contours;
+ new_max = base->n_contours + current->n_contours +
+ n_contours;
+ if ( new_max > old_max )
+ {
+ new_max = ( new_max + 3 ) & -4;
+ if ( REALLOC_ARRAY( base->contours, old_max, new_max, FT_Short ) )
+ goto Exit;
+
+ adjust = 1;
+ loader->max_contours = new_max;
+ }
+
+ if ( adjust )
+ FT_GlyphLoader _Adjust_Points( loader );
+
+ Exit:
+ return error;
+ }
+
+
+ /* Ensure that we can add `n_subglyphs' to our glyph. this function */
+ /* reallocates its subglyphs table if necessary. Note that it DOES */
+ /* NOT change the number of subglyphs within the loader! */
+ /* */
+ FT_BASE_DEF( FT_Error )
+ FT_GlyphLoader _Check_Subglyphs( FT_GlyphLoader loader,
+ FT_UInt n_subs )
+ {
+ FT_Memory memory = loader->memory;
+ FT_Error error = FT_Err_Ok;
+ FT_UInt new_max, old_max;
+
+ FT_GlyphLoad* base = &loader->base;
+ FT_GlyphLoad* current = &loader->current;
+
+
+ new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
+ old_max = loader->max_subglyphs;
+ if ( new_max > old_max )
+ {
+ new_max = ( new_max + 1 ) & -2;
+ if ( REALLOC_ARRAY( base->subglyphs, old_max, new_max, FT_SubGlyph ) )
+ goto Exit;
+
+ loader->max_subglyphs = new_max;
+
+ FT_GlyphLoader _Adjust_Subglyphs( loader );
+ }
+
+ Exit:
+ return error;
+ }
+
+
+ /* prepare loader for the addition of a new glyph on top of the base one */
+ FT_BASE_DEF( void )
+ FT_GlyphLoader _Prepare( FT_GlyphLoader loader )
+ {
+ FT_GlyphLoad* current = &loader->current;
+
+
+ current->outline.n_points = 0;
+ current->outline.n_contours = 0;
+ current->num_subglyphs = 0;
+
+ FT_GlyphLoader _Adjust_Points ( loader );
+ FT_GlyphLoader _Adjust_Subglyphs( loader );
+ }
+
+
+ /* add current glyph to the base image - and prepare for another */
+ FT_BASE_DEF( void )
+ FT_GlyphLoader _Add( FT_GlyphLoader loader )
+ {
+ FT_GlyphLoad* base = &loader->base;
+ FT_GlyphLoad* current = &loader->current;
+
+ FT_UInt n_curr_contours = current->outline.n_contours;
+ FT_UInt n_base_points = base->outline.n_points;
+ FT_UInt n;
+
+
+ base->outline.n_points =
+ (short)( base->outline.n_points + current->outline.n_points );
+ base->outline.n_contours =
+ (short)( base->outline.n_contours + current->outline.n_contours );
+
+ base->num_subglyphs += current->num_subglyphs;
+
+ /* adjust contours count in newest outline */
+ for ( n = 0; n < n_curr_contours; n++ )
+ current->outline.contours[n] =
+ (short)( current->outline.contours[n] + n_base_points );
+
+ /* prepare for another new glyph image */
+ FT_GlyphLoader _Prepare( loader );
+ }
+
+
+ FT_BASE_DEF( FT_Error )
+ FT_GlyphLoader _Copy_Points( FT_GlyphLoader target,
+ FT_GlyphLoader source )
+ {
+ FT_Error error;
+ FT_UInt num_points = source->base.outline.n_points;
+ FT_UInt num_contours = source->base.outline.n_contours;
+
+
+ error = FT_GlyphLoader _Check_Points( target, num_points, num_contours );
+ if ( !error )
+ {
+ FT_Outline* out = &target->base.outline;
+ FT_Outline* in = &source->base.outline;
+
+
+ MEM_Copy( out->points, in->points,
+ num_points * sizeof ( FT_Vector ) );
+ MEM_Copy( out->tags, in->tags,
+ num_points * sizeof ( char ) );
+ MEM_Copy( out->contours, in->contours,
+ num_contours * sizeof ( short ) );
+
+ /* do we need to copy the extra points? */
+ if ( target->use_extra && source->use_extra )
+ MEM_Copy( target->base.extra_points, source->base.extra_points,
+ num_points * sizeof ( FT_Vector ) );
+
+ out->n_points = (short)num_points;
+ out->n_contours = (short)num_contours;
+
+ FT_GlyphLoader _Adjust_Points( target );
+ }
+
+ return error;
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** *****/
+ /***** S T R E A M S *****/
+ /***** *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+ /*************************************************************************/
+
+#undef FT_COMPONENT
+#define FT_COMPONENT trace_stream
+
+ /*************************************************************************/
+ /* */
+ /* <Function> */
+ /* ft_new_input_stream */
+ /* */
+ /* <Description> */
+ /* Creates a new input stream object from an FT_Open_Args structure. */
+ /* */
+ /* <Note> */
+ /* The function expects a valid `astream' parameter. */
+ /* */
+ FT_EXPORT_DEF( FT_Error )
+ FT_Stream_Open( FT_Library library,
+ FT_Open_Args* args,
+ FT_Stream* astream )
+ {
+ FT_Error error;
+ FT_Memory memory;
+ FT_Stream stream;
+
+
+ if ( !library )
+ return FT_Err_Invalid_Library_Handle;
+
+ if ( !args )
+ return FT_Err_Invalid_Argument;
+
+ *astream = 0;
+ memory = library->memory;
+ if ( ALLOC( stream, sizeof ( *stream ) ) )
+ goto Exit;
+
+ stream->memory = memory;
+
+ /* now, look at the stream flags */
+ if ( args->flags & ft_open_memory )
+ {
+ error = 0;
+ FT_New_Memory_Stream( library,
+ (FT_Byte*)args->memory_base,
+ args->memory_size,
+ stream );
+ }
+ else if ( args->flags & ft_open_pathname )
+ {
+ error = FT_Stream_New( args->pathname, stream );
+ stream->pathname.pointer = args->pathname;
+ }
+ else if ( ( args->flags & ft_open_stream ) && args->stream )
+ {
+ /* in this case, we do not need to allocate a new stream object */
+ /* since the caller is responsible for closing it himself */
+ FREE( stream );
+ stream = args->stream;
+ }
+ else
+ error = FT_Err_Invalid_Argument;
+
+ if ( error )
+ FREE( stream );
+
+ *astream = stream;
+
+ Exit:
+ return error;
+ }
+
+
+ /* documentation is in ftobjs.h */
+
+ FT_EXPORT_DEF( void )
+ FT_Stream_Done( FT_Stream stream )
+ {
+ if ( stream && stream->close )
+ {
+ stream->close( stream );
+ stream->close = 0;
+ }
+ }
+
+
+ static void
+ ft_done_stream( FT_Stream* astream,
+ FT_Int external )
+ {
+ FT_Stream stream = *astream;
+
+
+ if ( stream->close )
+ stream->close( stream );
+
+ if ( !external )
+ {
+ FT_Memory memory = stream->memory;
+
+
+ FREE( stream );
+ }
+ *astream = 0;
+ }
+