* Sync up to trunk HEAD (r62502).
[reactos.git] / lib / 3rdparty / freetype / src / base / ftobjs.c
1 /***************************************************************************/
2 /* */
3 /* ftobjs.c */
4 /* */
5 /* The FreeType private base classes (body). */
6 /* */
7 /* Copyright 1996-2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_LIST_H
21 #include FT_OUTLINE_H
22 #include FT_INTERNAL_VALIDATE_H
23 #include FT_INTERNAL_OBJECTS_H
24 #include FT_INTERNAL_DEBUG_H
25 #include FT_INTERNAL_RFORK_H
26 #include FT_INTERNAL_STREAM_H
27 #include FT_INTERNAL_SFNT_H /* for SFNT_Load_Table_Func */
28 #include FT_TRUETYPE_TABLES_H
29 #include FT_TRUETYPE_TAGS_H
30 #include FT_TRUETYPE_IDS_H
31
32 #include FT_SERVICE_PROPERTIES_H
33 #include FT_SERVICE_SFNT_H
34 #include FT_SERVICE_POSTSCRIPT_NAME_H
35 #include FT_SERVICE_GLYPH_DICT_H
36 #include FT_SERVICE_TT_CMAP_H
37 #include FT_SERVICE_KERNING_H
38 #include FT_SERVICE_TRUETYPE_ENGINE_H
39
40 #ifdef FT_CONFIG_OPTION_MAC_FONTS
41 #include "ftbase.h"
42 #endif
43
44
45 #ifdef FT_DEBUG_LEVEL_TRACE
46
47 #include FT_BITMAP_H
48
49 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
50 /* We disable the warning `conversion from XXX to YYY, */
51 /* possible loss of data' in order to compile cleanly with */
52 /* the maximum level of warnings: `md5.c' is non-FreeType */
53 /* code, and it gets used during development builds only. */
54 #pragma warning( push )
55 #pragma warning( disable : 4244 )
56 #endif /* _MSC_VER */
57
58 /* it's easiest to include `md5.c' directly */
59 #include "md5.c"
60
61 #if defined( _MSC_VER )
62 #pragma warning( pop )
63 #endif
64
65 #endif /* FT_DEBUG_LEVEL_TRACE */
66
67
68 #define GRID_FIT_METRICS
69
70
71 FT_BASE_DEF( FT_Pointer )
72 ft_service_list_lookup( FT_ServiceDesc service_descriptors,
73 const char* service_id )
74 {
75 FT_Pointer result = NULL;
76 FT_ServiceDesc desc = service_descriptors;
77
78
79 if ( desc && service_id )
80 {
81 for ( ; desc->serv_id != NULL; desc++ )
82 {
83 if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
84 {
85 result = (FT_Pointer)desc->serv_data;
86 break;
87 }
88 }
89 }
90
91 return result;
92 }
93
94
95 FT_BASE_DEF( void )
96 ft_validator_init( FT_Validator valid,
97 const FT_Byte* base,
98 const FT_Byte* limit,
99 FT_ValidationLevel level )
100 {
101 valid->base = base;
102 valid->limit = limit;
103 valid->level = level;
104 valid->error = FT_Err_Ok;
105 }
106
107
108 FT_BASE_DEF( FT_Int )
109 ft_validator_run( FT_Validator valid )
110 {
111 /* This function doesn't work! None should call it. */
112 FT_UNUSED( valid );
113
114 return -1;
115 }
116
117
118 FT_BASE_DEF( void )
119 ft_validator_error( FT_Validator valid,
120 FT_Error error )
121 {
122 /* since the cast below also disables the compiler's */
123 /* type check, we introduce a dummy variable, which */
124 /* will be optimized away */
125 volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
126
127
128 valid->error = error;
129
130 /* throw away volatileness; use `jump_buffer' or the */
131 /* compiler may warn about an unused local variable */
132 ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
133 }
134
135
136 /*************************************************************************/
137 /*************************************************************************/
138 /*************************************************************************/
139 /**** ****/
140 /**** ****/
141 /**** S T R E A M ****/
142 /**** ****/
143 /**** ****/
144 /*************************************************************************/
145 /*************************************************************************/
146 /*************************************************************************/
147
148
149 /* create a new input stream from an FT_Open_Args structure */
150 /* */
151 FT_BASE_DEF( FT_Error )
152 FT_Stream_New( FT_Library library,
153 const FT_Open_Args* args,
154 FT_Stream *astream )
155 {
156 FT_Error error;
157 FT_Memory memory;
158 FT_Stream stream = NULL;
159
160
161 *astream = 0;
162
163 if ( !library )
164 return FT_THROW( Invalid_Library_Handle );
165
166 if ( !args )
167 return FT_THROW( Invalid_Argument );
168
169 memory = library->memory;
170
171 if ( FT_NEW( stream ) )
172 goto Exit;
173
174 stream->memory = memory;
175
176 if ( args->flags & FT_OPEN_MEMORY )
177 {
178 /* create a memory-based stream */
179 FT_Stream_OpenMemory( stream,
180 (const FT_Byte*)args->memory_base,
181 args->memory_size );
182 }
183
184 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
185
186 else if ( args->flags & FT_OPEN_PATHNAME )
187 {
188 /* create a normal system stream */
189 error = FT_Stream_Open( stream, args->pathname );
190 stream->pathname.pointer = args->pathname;
191 }
192 else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
193 {
194 /* use an existing, user-provided stream */
195
196 /* in this case, we do not need to allocate a new stream object */
197 /* since the caller is responsible for closing it himself */
198 FT_FREE( stream );
199 stream = args->stream;
200 }
201
202 #endif
203
204 else
205 error = FT_THROW( Invalid_Argument );
206
207 if ( error )
208 FT_FREE( stream );
209 else
210 stream->memory = memory; /* just to be certain */
211
212 *astream = stream;
213
214 Exit:
215 return error;
216 }
217
218
219 FT_BASE_DEF( void )
220 FT_Stream_Free( FT_Stream stream,
221 FT_Int external )
222 {
223 if ( stream )
224 {
225 FT_Memory memory = stream->memory;
226
227
228 FT_Stream_Close( stream );
229
230 if ( !external )
231 FT_FREE( stream );
232 }
233 }
234
235
236 /*************************************************************************/
237 /* */
238 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
239 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
240 /* messages during execution. */
241 /* */
242 #undef FT_COMPONENT
243 #define FT_COMPONENT trace_objs
244
245
246 /*************************************************************************/
247 /*************************************************************************/
248 /*************************************************************************/
249 /**** ****/
250 /**** ****/
251 /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
252 /**** ****/
253 /**** ****/
254 /*************************************************************************/
255 /*************************************************************************/
256 /*************************************************************************/
257
258
259 static FT_Error
260 ft_glyphslot_init( FT_GlyphSlot slot )
261 {
262 FT_Driver driver = slot->face->driver;
263 FT_Driver_Class clazz = driver->clazz;
264 FT_Memory memory = driver->root.memory;
265 FT_Error error = FT_Err_Ok;
266 FT_Slot_Internal internal = NULL;
267
268
269 slot->library = driver->root.library;
270
271 if ( FT_NEW( internal ) )
272 goto Exit;
273
274 slot->internal = internal;
275
276 if ( FT_DRIVER_USES_OUTLINES( driver ) )
277 error = FT_GlyphLoader_New( memory, &internal->loader );
278
279 if ( !error && clazz->init_slot )
280 error = clazz->init_slot( slot );
281
282 Exit:
283 return error;
284 }
285
286
287 FT_BASE_DEF( void )
288 ft_glyphslot_free_bitmap( FT_GlyphSlot slot )
289 {
290 if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
291 {
292 FT_Memory memory = FT_FACE_MEMORY( slot->face );
293
294
295 FT_FREE( slot->bitmap.buffer );
296 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
297 }
298 else
299 {
300 /* assume that the bitmap buffer was stolen or not */
301 /* allocated from the heap */
302 slot->bitmap.buffer = NULL;
303 }
304 }
305
306
307 FT_BASE_DEF( void )
308 ft_glyphslot_set_bitmap( FT_GlyphSlot slot,
309 FT_Byte* buffer )
310 {
311 ft_glyphslot_free_bitmap( slot );
312
313 slot->bitmap.buffer = buffer;
314
315 FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
316 }
317
318
319 FT_BASE_DEF( FT_Error )
320 ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot,
321 FT_ULong size )
322 {
323 FT_Memory memory = FT_FACE_MEMORY( slot->face );
324 FT_Error error;
325
326
327 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
328 FT_FREE( slot->bitmap.buffer );
329 else
330 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
331
332 (void)FT_ALLOC( slot->bitmap.buffer, size );
333 return error;
334 }
335
336
337 static void
338 ft_glyphslot_clear( FT_GlyphSlot slot )
339 {
340 /* free bitmap if needed */
341 ft_glyphslot_free_bitmap( slot );
342
343 /* clear all public fields in the glyph slot */
344 FT_ZERO( &slot->metrics );
345 FT_ZERO( &slot->outline );
346
347 slot->bitmap.width = 0;
348 slot->bitmap.rows = 0;
349 slot->bitmap.pitch = 0;
350 slot->bitmap.pixel_mode = 0;
351 /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
352
353 slot->bitmap_left = 0;
354 slot->bitmap_top = 0;
355 slot->num_subglyphs = 0;
356 slot->subglyphs = 0;
357 slot->control_data = 0;
358 slot->control_len = 0;
359 slot->other = 0;
360 slot->format = FT_GLYPH_FORMAT_NONE;
361
362 slot->linearHoriAdvance = 0;
363 slot->linearVertAdvance = 0;
364 slot->lsb_delta = 0;
365 slot->rsb_delta = 0;
366 }
367
368
369 static void
370 ft_glyphslot_done( FT_GlyphSlot slot )
371 {
372 FT_Driver driver = slot->face->driver;
373 FT_Driver_Class clazz = driver->clazz;
374 FT_Memory memory = driver->root.memory;
375
376
377 if ( clazz->done_slot )
378 clazz->done_slot( slot );
379
380 /* free bitmap buffer if needed */
381 ft_glyphslot_free_bitmap( slot );
382
383 /* slot->internal might be NULL in out-of-memory situations */
384 if ( slot->internal )
385 {
386 /* free glyph loader */
387 if ( FT_DRIVER_USES_OUTLINES( driver ) )
388 {
389 FT_GlyphLoader_Done( slot->internal->loader );
390 slot->internal->loader = 0;
391 }
392
393 FT_FREE( slot->internal );
394 }
395 }
396
397
398 /* documentation is in ftobjs.h */
399
400 FT_BASE_DEF( FT_Error )
401 FT_New_GlyphSlot( FT_Face face,
402 FT_GlyphSlot *aslot )
403 {
404 FT_Error error;
405 FT_Driver driver;
406 FT_Driver_Class clazz;
407 FT_Memory memory;
408 FT_GlyphSlot slot = NULL;
409
410
411 if ( !face || !face->driver )
412 return FT_THROW( Invalid_Argument );
413
414 driver = face->driver;
415 clazz = driver->clazz;
416 memory = driver->root.memory;
417
418 FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
419 if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
420 {
421 slot->face = face;
422
423 error = ft_glyphslot_init( slot );
424 if ( error )
425 {
426 ft_glyphslot_done( slot );
427 FT_FREE( slot );
428 goto Exit;
429 }
430
431 slot->next = face->glyph;
432 face->glyph = slot;
433
434 if ( aslot )
435 *aslot = slot;
436 }
437 else if ( aslot )
438 *aslot = 0;
439
440
441 Exit:
442 FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
443 return error;
444 }
445
446
447 /* documentation is in ftobjs.h */
448
449 FT_BASE_DEF( void )
450 FT_Done_GlyphSlot( FT_GlyphSlot slot )
451 {
452 if ( slot )
453 {
454 FT_Driver driver = slot->face->driver;
455 FT_Memory memory = driver->root.memory;
456 FT_GlyphSlot prev;
457 FT_GlyphSlot cur;
458
459
460 /* Remove slot from its parent face's list */
461 prev = NULL;
462 cur = slot->face->glyph;
463
464 while ( cur )
465 {
466 if ( cur == slot )
467 {
468 if ( !prev )
469 slot->face->glyph = cur->next;
470 else
471 prev->next = cur->next;
472
473 /* finalize client-specific data */
474 if ( slot->generic.finalizer )
475 slot->generic.finalizer( slot );
476
477 ft_glyphslot_done( slot );
478 FT_FREE( slot );
479 break;
480 }
481 prev = cur;
482 cur = cur->next;
483 }
484 }
485 }
486
487
488 /* documentation is in freetype.h */
489
490 FT_EXPORT_DEF( void )
491 FT_Set_Transform( FT_Face face,
492 FT_Matrix* matrix,
493 FT_Vector* delta )
494 {
495 FT_Face_Internal internal;
496
497
498 if ( !face )
499 return;
500
501 internal = face->internal;
502
503 internal->transform_flags = 0;
504
505 if ( !matrix )
506 {
507 internal->transform_matrix.xx = 0x10000L;
508 internal->transform_matrix.xy = 0;
509 internal->transform_matrix.yx = 0;
510 internal->transform_matrix.yy = 0x10000L;
511 matrix = &internal->transform_matrix;
512 }
513 else
514 internal->transform_matrix = *matrix;
515
516 /* set transform_flags bit flag 0 if `matrix' isn't the identity */
517 if ( ( matrix->xy | matrix->yx ) ||
518 matrix->xx != 0x10000L ||
519 matrix->yy != 0x10000L )
520 internal->transform_flags |= 1;
521
522 if ( !delta )
523 {
524 internal->transform_delta.x = 0;
525 internal->transform_delta.y = 0;
526 delta = &internal->transform_delta;
527 }
528 else
529 internal->transform_delta = *delta;
530
531 /* set transform_flags bit flag 1 if `delta' isn't the null vector */
532 if ( delta->x | delta->y )
533 internal->transform_flags |= 2;
534 }
535
536
537 static FT_Renderer
538 ft_lookup_glyph_renderer( FT_GlyphSlot slot );
539
540
541 #ifdef GRID_FIT_METRICS
542 static void
543 ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot,
544 FT_Bool vertical )
545 {
546 FT_Glyph_Metrics* metrics = &slot->metrics;
547 FT_Pos right, bottom;
548
549
550 if ( vertical )
551 {
552 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
553 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
554
555 right = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
556 bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
557
558 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
559 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
560
561 metrics->width = right - metrics->vertBearingX;
562 metrics->height = bottom - metrics->vertBearingY;
563 }
564 else
565 {
566 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
567 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
568
569 right = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
570 bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
571
572 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
573 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
574
575 metrics->width = right - metrics->horiBearingX;
576 metrics->height = metrics->horiBearingY - bottom;
577 }
578
579 metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
580 metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
581 }
582 #endif /* GRID_FIT_METRICS */
583
584
585 /* documentation is in freetype.h */
586
587 FT_EXPORT_DEF( FT_Error )
588 FT_Load_Glyph( FT_Face face,
589 FT_UInt glyph_index,
590 FT_Int32 load_flags )
591 {
592 FT_Error error;
593 FT_Driver driver;
594 FT_GlyphSlot slot;
595 FT_Library library;
596 FT_Bool autohint = FALSE;
597 FT_Module hinter;
598 TT_Face ttface = (TT_Face)face;
599
600
601 if ( !face || !face->size || !face->glyph )
602 return FT_THROW( Invalid_Face_Handle );
603
604 /* The validity test for `glyph_index' is performed by the */
605 /* font drivers. */
606
607 slot = face->glyph;
608 ft_glyphslot_clear( slot );
609
610 driver = face->driver;
611 library = driver->root.library;
612 hinter = library->auto_hinter;
613
614 /* resolve load flags dependencies */
615
616 if ( load_flags & FT_LOAD_NO_RECURSE )
617 load_flags |= FT_LOAD_NO_SCALE |
618 FT_LOAD_IGNORE_TRANSFORM;
619
620 if ( load_flags & FT_LOAD_NO_SCALE )
621 {
622 load_flags |= FT_LOAD_NO_HINTING |
623 FT_LOAD_NO_BITMAP;
624
625 load_flags &= ~FT_LOAD_RENDER;
626 }
627
628 /*
629 * Determine whether we need to auto-hint or not.
630 * The general rules are:
631 *
632 * - Do only auto-hinting if we have a hinter module, a scalable font
633 * format dealing with outlines, and no transforms except simple
634 * slants and/or rotations by integer multiples of 90 degrees.
635 *
636 * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
637 * have a native font hinter.
638 *
639 * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
640 * any hinting bytecode in the TrueType/OpenType font.
641 *
642 * - Exception: The font is `tricky' and requires the native hinter to
643 * load properly.
644 */
645
646 if ( hinter &&
647 !( load_flags & FT_LOAD_NO_HINTING ) &&
648 !( load_flags & FT_LOAD_NO_AUTOHINT ) &&
649 FT_DRIVER_IS_SCALABLE( driver ) &&
650 FT_DRIVER_USES_OUTLINES( driver ) &&
651 !FT_IS_TRICKY( face ) &&
652 ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) ||
653 ( face->internal->transform_matrix.yx == 0 &&
654 face->internal->transform_matrix.xx != 0 ) ||
655 ( face->internal->transform_matrix.xx == 0 &&
656 face->internal->transform_matrix.yx != 0 ) ) )
657 {
658 if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
659 !FT_DRIVER_HAS_HINTER( driver ) )
660 autohint = TRUE;
661 else
662 {
663 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
664
665
666 /* the check for `num_locations' assures that we actually */
667 /* test for instructions in a TTF and not in a CFF-based OTF */
668 /* */
669 /* since `maxSizeOfInstructions' might be unreliable, we */
670 /* check the size of the `fpgm' and `prep' tables, too -- */
671 /* the assumption is that there don't exist real TTFs where */
672 /* both `fpgm' and `prep' tables are missing */
673 if ( mode == FT_RENDER_MODE_LIGHT ||
674 face->internal->ignore_unpatented_hinter ||
675 ( FT_IS_SFNT( face ) &&
676 ttface->num_locations &&
677 ttface->max_profile.maxSizeOfInstructions == 0 &&
678 ttface->font_program_size == 0 &&
679 ttface->cvt_program_size == 0 ) )
680 autohint = TRUE;
681 }
682 }
683
684 if ( autohint )
685 {
686 FT_AutoHinter_Interface hinting;
687
688
689 /* try to load embedded bitmaps first if available */
690 /* */
691 /* XXX: This is really a temporary hack that should disappear */
692 /* promptly with FreeType 2.1! */
693 /* */
694 if ( FT_HAS_FIXED_SIZES( face ) &&
695 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
696 {
697 error = driver->clazz->load_glyph( slot, face->size,
698 glyph_index,
699 load_flags | FT_LOAD_SBITS_ONLY );
700
701 if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
702 goto Load_Ok;
703 }
704
705 {
706 FT_Face_Internal internal = face->internal;
707 FT_Int transform_flags = internal->transform_flags;
708
709
710 /* since the auto-hinter calls FT_Load_Glyph by itself, */
711 /* make sure that glyphs aren't transformed */
712 internal->transform_flags = 0;
713
714 /* load auto-hinted outline */
715 hinting = (FT_AutoHinter_Interface)hinter->clazz->module_interface;
716
717 error = hinting->load_glyph( (FT_AutoHinter)hinter,
718 slot, face->size,
719 glyph_index, load_flags );
720
721 internal->transform_flags = transform_flags;
722 }
723 }
724 else
725 {
726 error = driver->clazz->load_glyph( slot,
727 face->size,
728 glyph_index,
729 load_flags );
730 if ( error )
731 goto Exit;
732
733 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
734 {
735 /* check that the loaded outline is correct */
736 error = FT_Outline_Check( &slot->outline );
737 if ( error )
738 goto Exit;
739
740 #ifdef GRID_FIT_METRICS
741 if ( !( load_flags & FT_LOAD_NO_HINTING ) )
742 ft_glyphslot_grid_fit_metrics( slot,
743 FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
744 #endif
745 }
746 }
747
748 Load_Ok:
749 /* compute the advance */
750 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
751 {
752 slot->advance.x = 0;
753 slot->advance.y = slot->metrics.vertAdvance;
754 }
755 else
756 {
757 slot->advance.x = slot->metrics.horiAdvance;
758 slot->advance.y = 0;
759 }
760
761 /* compute the linear advance in 16.16 pixels */
762 if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
763 ( FT_IS_SCALABLE( face ) ) )
764 {
765 FT_Size_Metrics* metrics = &face->size->metrics;
766
767
768 /* it's tricky! */
769 slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
770 metrics->x_scale, 64 );
771
772 slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
773 metrics->y_scale, 64 );
774 }
775
776 if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
777 {
778 FT_Face_Internal internal = face->internal;
779
780
781 /* now, transform the glyph image if needed */
782 if ( internal->transform_flags )
783 {
784 /* get renderer */
785 FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
786
787
788 if ( renderer )
789 error = renderer->clazz->transform_glyph(
790 renderer, slot,
791 &internal->transform_matrix,
792 &internal->transform_delta );
793 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
794 {
795 /* apply `standard' transformation if no renderer is available */
796 if ( internal->transform_flags & 1 )
797 FT_Outline_Transform( &slot->outline,
798 &internal->transform_matrix );
799
800 if ( internal->transform_flags & 2 )
801 FT_Outline_Translate( &slot->outline,
802 internal->transform_delta.x,
803 internal->transform_delta.y );
804 }
805
806 /* transform advance */
807 FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
808 }
809 }
810
811 FT_TRACE5(( " x advance: %d\n" , slot->advance.x ));
812 FT_TRACE5(( " y advance: %d\n" , slot->advance.y ));
813
814 FT_TRACE5(( " linear x advance: %d\n" , slot->linearHoriAdvance ));
815 FT_TRACE5(( " linear y advance: %d\n" , slot->linearVertAdvance ));
816
817 /* do we need to render the image now? */
818 if ( !error &&
819 slot->format != FT_GLYPH_FORMAT_BITMAP &&
820 slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
821 load_flags & FT_LOAD_RENDER )
822 {
823 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
824
825
826 if ( mode == FT_RENDER_MODE_NORMAL &&
827 (load_flags & FT_LOAD_MONOCHROME ) )
828 mode = FT_RENDER_MODE_MONO;
829
830 error = FT_Render_Glyph( slot, mode );
831 }
832
833 Exit:
834 return error;
835 }
836
837
838 /* documentation is in freetype.h */
839
840 FT_EXPORT_DEF( FT_Error )
841 FT_Load_Char( FT_Face face,
842 FT_ULong char_code,
843 FT_Int32 load_flags )
844 {
845 FT_UInt glyph_index;
846
847
848 if ( !face )
849 return FT_THROW( Invalid_Face_Handle );
850
851 glyph_index = (FT_UInt)char_code;
852 if ( face->charmap )
853 glyph_index = FT_Get_Char_Index( face, char_code );
854
855 return FT_Load_Glyph( face, glyph_index, load_flags );
856 }
857
858
859 /* destructor for sizes list */
860 static void
861 destroy_size( FT_Memory memory,
862 FT_Size size,
863 FT_Driver driver )
864 {
865 /* finalize client-specific data */
866 if ( size->generic.finalizer )
867 size->generic.finalizer( size );
868
869 /* finalize format-specific stuff */
870 if ( driver->clazz->done_size )
871 driver->clazz->done_size( size );
872
873 FT_FREE( size->internal );
874 FT_FREE( size );
875 }
876
877
878 static void
879 ft_cmap_done_internal( FT_CMap cmap );
880
881
882 static void
883 destroy_charmaps( FT_Face face,
884 FT_Memory memory )
885 {
886 FT_Int n;
887
888
889 if ( !face )
890 return;
891
892 for ( n = 0; n < face->num_charmaps; n++ )
893 {
894 FT_CMap cmap = FT_CMAP( face->charmaps[n] );
895
896
897 ft_cmap_done_internal( cmap );
898
899 face->charmaps[n] = NULL;
900 }
901
902 FT_FREE( face->charmaps );
903 face->num_charmaps = 0;
904 }
905
906
907 /* destructor for faces list */
908 static void
909 destroy_face( FT_Memory memory,
910 FT_Face face,
911 FT_Driver driver )
912 {
913 FT_Driver_Class clazz = driver->clazz;
914
915
916 /* discard auto-hinting data */
917 if ( face->autohint.finalizer )
918 face->autohint.finalizer( face->autohint.data );
919
920 /* Discard glyph slots for this face. */
921 /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
922 while ( face->glyph )
923 FT_Done_GlyphSlot( face->glyph );
924
925 /* discard all sizes for this face */
926 FT_List_Finalize( &face->sizes_list,
927 (FT_List_Destructor)destroy_size,
928 memory,
929 driver );
930 face->size = 0;
931
932 /* now discard client data */
933 if ( face->generic.finalizer )
934 face->generic.finalizer( face );
935
936 /* discard charmaps */
937 destroy_charmaps( face, memory );
938
939 /* finalize format-specific stuff */
940 if ( clazz->done_face )
941 clazz->done_face( face );
942
943 /* close the stream for this face if needed */
944 FT_Stream_Free(
945 face->stream,
946 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
947
948 face->stream = 0;
949
950 /* get rid of it */
951 if ( face->internal )
952 {
953 FT_FREE( face->internal );
954 }
955 FT_FREE( face );
956 }
957
958
959 static void
960 Destroy_Driver( FT_Driver driver )
961 {
962 FT_List_Finalize( &driver->faces_list,
963 (FT_List_Destructor)destroy_face,
964 driver->root.memory,
965 driver );
966
967 /* check whether we need to drop the driver's glyph loader */
968 if ( FT_DRIVER_USES_OUTLINES( driver ) )
969 FT_GlyphLoader_Done( driver->glyph_loader );
970 }
971
972
973 /*************************************************************************/
974 /* */
975 /* <Function> */
976 /* find_unicode_charmap */
977 /* */
978 /* <Description> */
979 /* This function finds a Unicode charmap, if there is one. */
980 /* And if there is more than one, it tries to favour the more */
981 /* extensive one, i.e., one that supports UCS-4 against those which */
982 /* are limited to the BMP (said UCS-2 encoding.) */
983 /* */
984 /* This function is called from open_face() (just below), and also */
985 /* from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ). */
986 /* */
987 static FT_Error
988 find_unicode_charmap( FT_Face face )
989 {
990 FT_CharMap* first;
991 FT_CharMap* cur;
992
993
994 /* caller should have already checked that `face' is valid */
995 FT_ASSERT( face );
996
997 first = face->charmaps;
998
999 if ( !first )
1000 return FT_THROW( Invalid_CharMap_Handle );
1001
1002 /*
1003 * The original TrueType specification(s) only specified charmap
1004 * formats that are capable of mapping 8 or 16 bit character codes to
1005 * glyph indices.
1006 *
1007 * However, recent updates to the Apple and OpenType specifications
1008 * introduced new formats that are capable of mapping 32-bit character
1009 * codes as well. And these are already used on some fonts, mainly to
1010 * map non-BMP Asian ideographs as defined in Unicode.
1011 *
1012 * For compatibility purposes, these fonts generally come with
1013 * *several* Unicode charmaps:
1014 *
1015 * - One of them in the "old" 16-bit format, that cannot access
1016 * all glyphs in the font.
1017 *
1018 * - Another one in the "new" 32-bit format, that can access all
1019 * the glyphs.
1020 *
1021 * This function has been written to always favor a 32-bit charmap
1022 * when found. Otherwise, a 16-bit one is returned when found.
1023 */
1024
1025 /* Since the `interesting' table, with IDs (3,10), is normally the */
1026 /* last one, we loop backwards. This loses with type1 fonts with */
1027 /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
1028 /* chars (.01% ?), and this is the same about 99.99% of the time! */
1029
1030 cur = first + face->num_charmaps; /* points after the last one */
1031
1032 for ( ; --cur >= first; )
1033 {
1034 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1035 {
1036 /* XXX If some new encodings to represent UCS-4 are added, */
1037 /* they should be added here. */
1038 if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
1039 cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
1040 ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1041 cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
1042 {
1043 #ifdef FT_MAX_CHARMAP_CACHEABLE
1044 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1045 {
1046 FT_ERROR(( "find_unicode_charmap: UCS-4 cmap is found "
1047 "at too late position (%d)\n", cur - first ));
1048 continue;
1049 }
1050 #endif
1051 face->charmap = cur[0];
1052 return FT_Err_Ok;
1053 }
1054 }
1055 }
1056
1057 /* We do not have any UCS-4 charmap. */
1058 /* Do the loop again and search for UCS-2 charmaps. */
1059 cur = first + face->num_charmaps;
1060
1061 for ( ; --cur >= first; )
1062 {
1063 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1064 {
1065 #ifdef FT_MAX_CHARMAP_CACHEABLE
1066 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1067 {
1068 FT_ERROR(( "find_unicode_charmap: UCS-2 cmap is found "
1069 "at too late position (%d)\n", cur - first ));
1070 continue;
1071 }
1072 #endif
1073 face->charmap = cur[0];
1074 return FT_Err_Ok;
1075 }
1076 }
1077
1078 return FT_THROW( Invalid_CharMap_Handle );
1079 }
1080
1081
1082 /*************************************************************************/
1083 /* */
1084 /* <Function> */
1085 /* find_variant_selector_charmap */
1086 /* */
1087 /* <Description> */
1088 /* This function finds the variant selector charmap, if there is one. */
1089 /* There can only be one (platform=0, specific=5, format=14). */
1090 /* */
1091 static FT_CharMap
1092 find_variant_selector_charmap( FT_Face face )
1093 {
1094 FT_CharMap* first;
1095 FT_CharMap* end;
1096 FT_CharMap* cur;
1097
1098
1099 /* caller should have already checked that `face' is valid */
1100 FT_ASSERT( face );
1101
1102 first = face->charmaps;
1103
1104 if ( !first )
1105 return NULL;
1106
1107 end = first + face->num_charmaps; /* points after the last one */
1108
1109 for ( cur = first; cur < end; ++cur )
1110 {
1111 if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1112 cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1113 FT_Get_CMap_Format( cur[0] ) == 14 )
1114 {
1115 #ifdef FT_MAX_CHARMAP_CACHEABLE
1116 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1117 {
1118 FT_ERROR(( "find_unicode_charmap: UVS cmap is found "
1119 "at too late position (%d)\n", cur - first ));
1120 continue;
1121 }
1122 #endif
1123 return cur[0];
1124 }
1125 }
1126
1127 return NULL;
1128 }
1129
1130
1131 /*************************************************************************/
1132 /* */
1133 /* <Function> */
1134 /* open_face */
1135 /* */
1136 /* <Description> */
1137 /* This function does some work for FT_Open_Face(). */
1138 /* */
1139 static FT_Error
1140 open_face( FT_Driver driver,
1141 FT_Stream *astream,
1142 FT_Bool external_stream,
1143 FT_Long face_index,
1144 FT_Int num_params,
1145 FT_Parameter* params,
1146 FT_Face *aface )
1147 {
1148 FT_Memory memory;
1149 FT_Driver_Class clazz;
1150 FT_Face face = NULL;
1151 FT_Face_Internal internal = NULL;
1152
1153 FT_Error error, error2;
1154
1155
1156 clazz = driver->clazz;
1157 memory = driver->root.memory;
1158
1159 /* allocate the face object and perform basic initialization */
1160 if ( FT_ALLOC( face, clazz->face_object_size ) )
1161 goto Fail;
1162
1163 face->driver = driver;
1164 face->memory = memory;
1165 face->stream = *astream;
1166
1167 /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
1168 if ( external_stream )
1169 face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
1170
1171 if ( FT_NEW( internal ) )
1172 goto Fail;
1173
1174 face->internal = internal;
1175
1176 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1177 {
1178 int i;
1179
1180
1181 face->internal->incremental_interface = 0;
1182 for ( i = 0; i < num_params && !face->internal->incremental_interface;
1183 i++ )
1184 if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1185 face->internal->incremental_interface =
1186 (FT_Incremental_Interface)params[i].data;
1187 }
1188 #endif
1189
1190 if ( clazz->init_face )
1191 error = clazz->init_face( *astream,
1192 face,
1193 (FT_Int)face_index,
1194 num_params,
1195 params );
1196 *astream = face->stream; /* Stream may have been changed. */
1197 if ( error )
1198 goto Fail;
1199
1200 /* select Unicode charmap by default */
1201 error2 = find_unicode_charmap( face );
1202
1203 /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1204 /* is returned. */
1205
1206 /* no error should happen, but we want to play safe */
1207 if ( error2 && FT_ERR_NEQ( error2, Invalid_CharMap_Handle ) )
1208 {
1209 error = error2;
1210 goto Fail;
1211 }
1212
1213 *aface = face;
1214
1215 Fail:
1216 if ( error )
1217 {
1218 destroy_charmaps( face, memory );
1219 if ( clazz->done_face )
1220 clazz->done_face( face );
1221 FT_FREE( internal );
1222 FT_FREE( face );
1223 *aface = 0;
1224 }
1225
1226 return error;
1227 }
1228
1229
1230 /* there's a Mac-specific extended implementation of FT_New_Face() */
1231 /* in src/base/ftmac.c */
1232
1233 #ifndef FT_MACINTOSH
1234
1235 /* documentation is in freetype.h */
1236
1237 FT_EXPORT_DEF( FT_Error )
1238 FT_New_Face( FT_Library library,
1239 const char* pathname,
1240 FT_Long face_index,
1241 FT_Face *aface )
1242 {
1243 FT_Open_Args args;
1244
1245
1246 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1247 if ( !pathname )
1248 return FT_THROW( Invalid_Argument );
1249
1250 args.flags = FT_OPEN_PATHNAME;
1251 args.pathname = (char*)pathname;
1252 args.stream = NULL;
1253
1254 return FT_Open_Face( library, &args, face_index, aface );
1255 }
1256
1257 #endif
1258
1259
1260 /* documentation is in freetype.h */
1261
1262 FT_EXPORT_DEF( FT_Error )
1263 FT_New_Memory_Face( FT_Library library,
1264 const FT_Byte* file_base,
1265 FT_Long file_size,
1266 FT_Long face_index,
1267 FT_Face *aface )
1268 {
1269 FT_Open_Args args;
1270
1271
1272 /* test for valid `library' and `face' delayed to FT_Open_Face() */
1273 if ( !file_base )
1274 return FT_THROW( Invalid_Argument );
1275
1276 args.flags = FT_OPEN_MEMORY;
1277 args.memory_base = file_base;
1278 args.memory_size = file_size;
1279 args.stream = NULL;
1280
1281 return FT_Open_Face( library, &args, face_index, aface );
1282 }
1283
1284
1285 #ifdef FT_CONFIG_OPTION_MAC_FONTS
1286
1287 /* The behavior here is very similar to that in base/ftmac.c, but it */
1288 /* is designed to work on non-mac systems, so no mac specific calls. */
1289 /* */
1290 /* We look at the file and determine if it is a mac dfont file or a mac */
1291 /* resource file, or a macbinary file containing a mac resource file. */
1292 /* */
1293 /* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */
1294 /* the point, especially since there may be multiple `FOND' resources. */
1295 /* Instead I'll just look for `sfnt' and `POST' resources, ordered as */
1296 /* they occur in the file. */
1297 /* */
1298 /* Note that multiple `POST' resources do not mean multiple postscript */
1299 /* fonts; they all get jammed together to make what is essentially a */
1300 /* pfb file. */
1301 /* */
1302 /* We aren't interested in `NFNT' or `FONT' bitmap resources. */
1303 /* */
1304 /* As soon as we get an `sfnt' load it into memory and pass it off to */
1305 /* FT_Open_Face. */
1306 /* */
1307 /* If we have a (set of) `POST' resources, massage them into a (memory) */
1308 /* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */
1309 /* going to try to save the kerning info. After all that lives in the */
1310 /* `FOND' which isn't in the file containing the `POST' resources so */
1311 /* we don't really have access to it. */
1312
1313
1314 /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1315 /* It frees the memory it uses. */
1316 /* From ftmac.c. */
1317 static void
1318 memory_stream_close( FT_Stream stream )
1319 {
1320 FT_Memory memory = stream->memory;
1321
1322
1323 FT_FREE( stream->base );
1324
1325 stream->size = 0;
1326 stream->base = 0;
1327 stream->close = 0;
1328 }
1329
1330
1331 /* Create a new memory stream from a buffer and a size. */
1332 /* From ftmac.c. */
1333 static FT_Error
1334 new_memory_stream( FT_Library library,
1335 FT_Byte* base,
1336 FT_ULong size,
1337 FT_Stream_CloseFunc close,
1338 FT_Stream *astream )
1339 {
1340 FT_Error error;
1341 FT_Memory memory;
1342 FT_Stream stream = NULL;
1343
1344
1345 if ( !library )
1346 return FT_THROW( Invalid_Library_Handle );
1347
1348 if ( !base )
1349 return FT_THROW( Invalid_Argument );
1350
1351 *astream = 0;
1352 memory = library->memory;
1353 if ( FT_NEW( stream ) )
1354 goto Exit;
1355
1356 FT_Stream_OpenMemory( stream, base, size );
1357
1358 stream->close = close;
1359
1360 *astream = stream;
1361
1362 Exit:
1363 return error;
1364 }
1365
1366
1367 /* Create a new FT_Face given a buffer and a driver name. */
1368 /* from ftmac.c */
1369 FT_LOCAL_DEF( FT_Error )
1370 open_face_from_buffer( FT_Library library,
1371 FT_Byte* base,
1372 FT_ULong size,
1373 FT_Long face_index,
1374 const char* driver_name,
1375 FT_Face *aface )
1376 {
1377 FT_Open_Args args;
1378 FT_Error error;
1379 FT_Stream stream = NULL;
1380 FT_Memory memory = library->memory;
1381
1382
1383 error = new_memory_stream( library,
1384 base,
1385 size,
1386 memory_stream_close,
1387 &stream );
1388 if ( error )
1389 {
1390 FT_FREE( base );
1391 return error;
1392 }
1393
1394 args.flags = FT_OPEN_STREAM;
1395 args.stream = stream;
1396 if ( driver_name )
1397 {
1398 args.flags = args.flags | FT_OPEN_DRIVER;
1399 args.driver = FT_Get_Module( library, driver_name );
1400 }
1401
1402 #ifdef FT_MACINTOSH
1403 /* At this point, face_index has served its purpose; */
1404 /* whoever calls this function has already used it to */
1405 /* locate the correct font data. We should not propagate */
1406 /* this index to FT_Open_Face() (unless it is negative). */
1407
1408 if ( face_index > 0 )
1409 face_index = 0;
1410 #endif
1411
1412 error = FT_Open_Face( library, &args, face_index, aface );
1413
1414 if ( error == FT_Err_Ok )
1415 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
1416 else
1417 #ifdef FT_MACINTOSH
1418 FT_Stream_Free( stream, 0 );
1419 #else
1420 {
1421 FT_Stream_Close( stream );
1422 FT_FREE( stream );
1423 }
1424 #endif
1425
1426 return error;
1427 }
1428
1429
1430 /* Look up `TYP1' or `CID ' table from sfnt table directory. */
1431 /* `offset' and `length' must exclude the binary header in tables. */
1432
1433 /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1434 /* format too. Here, since we can't expect that the TrueType font */
1435 /* driver is loaded unconditially, we must parse the font by */
1436 /* ourselves. We are only interested in the name of the table and */
1437 /* the offset. */
1438
1439 static FT_Error
1440 ft_lookup_PS_in_sfnt_stream( FT_Stream stream,
1441 FT_Long face_index,
1442 FT_ULong* offset,
1443 FT_ULong* length,
1444 FT_Bool* is_sfnt_cid )
1445 {
1446 FT_Error error;
1447 FT_UShort numTables;
1448 FT_Long pstable_index;
1449 FT_ULong tag;
1450 int i;
1451
1452
1453 *offset = 0;
1454 *length = 0;
1455 *is_sfnt_cid = FALSE;
1456
1457 /* TODO: support for sfnt-wrapped PS/CID in TTC format */
1458
1459 /* version check for 'typ1' (should be ignored?) */
1460 if ( FT_READ_ULONG( tag ) )
1461 return error;
1462 if ( tag != TTAG_typ1 )
1463 return FT_THROW( Unknown_File_Format );
1464
1465 if ( FT_READ_USHORT( numTables ) )
1466 return error;
1467 if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1468 return error;
1469
1470 pstable_index = -1;
1471 *is_sfnt_cid = FALSE;
1472
1473 for ( i = 0; i < numTables; i++ )
1474 {
1475 if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) ||
1476 FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1477 return error;
1478
1479 if ( tag == TTAG_CID )
1480 {
1481 pstable_index++;
1482 *offset += 22;
1483 *length -= 22;
1484 *is_sfnt_cid = TRUE;
1485 if ( face_index < 0 )
1486 return FT_Err_Ok;
1487 }
1488 else if ( tag == TTAG_TYP1 )
1489 {
1490 pstable_index++;
1491 *offset += 24;
1492 *length -= 24;
1493 *is_sfnt_cid = FALSE;
1494 if ( face_index < 0 )
1495 return FT_Err_Ok;
1496 }
1497 if ( face_index >= 0 && pstable_index == face_index )
1498 return FT_Err_Ok;
1499 }
1500 return FT_THROW( Table_Missing );
1501 }
1502
1503
1504 FT_LOCAL_DEF( FT_Error )
1505 open_face_PS_from_sfnt_stream( FT_Library library,
1506 FT_Stream stream,
1507 FT_Long face_index,
1508 FT_Int num_params,
1509 FT_Parameter *params,
1510 FT_Face *aface )
1511 {
1512 FT_Error error;
1513 FT_Memory memory = library->memory;
1514 FT_ULong offset, length;
1515 FT_Long pos;
1516 FT_Bool is_sfnt_cid;
1517 FT_Byte* sfnt_ps = NULL;
1518
1519 FT_UNUSED( num_params );
1520 FT_UNUSED( params );
1521
1522
1523 pos = FT_Stream_Pos( stream );
1524
1525 error = ft_lookup_PS_in_sfnt_stream( stream,
1526 face_index,
1527 &offset,
1528 &length,
1529 &is_sfnt_cid );
1530 if ( error )
1531 goto Exit;
1532
1533 if ( FT_Stream_Seek( stream, pos + offset ) )
1534 goto Exit;
1535
1536 if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
1537 goto Exit;
1538
1539 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1540 if ( error )
1541 goto Exit;
1542
1543 error = open_face_from_buffer( library,
1544 sfnt_ps,
1545 length,
1546 FT_MIN( face_index, 0 ),
1547 is_sfnt_cid ? "cid" : "type1",
1548 aface );
1549 Exit:
1550 {
1551 FT_Error error1;
1552
1553
1554 if ( FT_ERR_EQ( error, Unknown_File_Format ) )
1555 {
1556 error1 = FT_Stream_Seek( stream, pos );
1557 if ( error1 )
1558 return error1;
1559 }
1560
1561 return error;
1562 }
1563 }
1564
1565
1566 #ifndef FT_MACINTOSH
1567
1568 /* The resource header says we've got resource_cnt `POST' (type1) */
1569 /* resources in this file. They all need to be coalesced into */
1570 /* one lump which gets passed on to the type1 driver. */
1571 /* Here can be only one PostScript font in a file so face_index */
1572 /* must be 0 (or -1). */
1573 /* */
1574 static FT_Error
1575 Mac_Read_POST_Resource( FT_Library library,
1576 FT_Stream stream,
1577 FT_Long *offsets,
1578 FT_Long resource_cnt,
1579 FT_Long face_index,
1580 FT_Face *aface )
1581 {
1582 FT_Error error = FT_ERR( Cannot_Open_Resource );
1583 FT_Memory memory = library->memory;
1584 FT_Byte* pfb_data = NULL;
1585 int i, type, flags;
1586 FT_Long len;
1587 FT_Long pfb_len, pfb_pos, pfb_lenpos;
1588 FT_Long rlen, temp;
1589
1590
1591 if ( face_index == -1 )
1592 face_index = 0;
1593 if ( face_index != 0 )
1594 return error;
1595
1596 /* Find the length of all the POST resources, concatenated. Assume */
1597 /* worst case (each resource in its own section). */
1598 pfb_len = 0;
1599 for ( i = 0; i < resource_cnt; ++i )
1600 {
1601 error = FT_Stream_Seek( stream, offsets[i] );
1602 if ( error )
1603 goto Exit;
1604 if ( FT_READ_LONG( temp ) )
1605 goto Exit;
1606 pfb_len += temp + 6;
1607 }
1608
1609 if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
1610 goto Exit;
1611
1612 pfb_data[0] = 0x80;
1613 pfb_data[1] = 1; /* Ascii section */
1614 pfb_data[2] = 0; /* 4-byte length, fill in later */
1615 pfb_data[3] = 0;
1616 pfb_data[4] = 0;
1617 pfb_data[5] = 0;
1618 pfb_pos = 6;
1619 pfb_lenpos = 2;
1620
1621 len = 0;
1622 type = 1;
1623 for ( i = 0; i < resource_cnt; ++i )
1624 {
1625 error = FT_Stream_Seek( stream, offsets[i] );
1626 if ( error )
1627 goto Exit2;
1628 if ( FT_READ_LONG( rlen ) )
1629 goto Exit;
1630 if ( FT_READ_USHORT( flags ) )
1631 goto Exit;
1632 FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
1633 i, offsets[i], rlen, flags ));
1634
1635 /* postpone the check of rlen longer than buffer until FT_Stream_Read() */
1636 if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */
1637 continue;
1638
1639 /* the flags are part of the resource, so rlen >= 2. */
1640 /* but some fonts declare rlen = 0 for empty fragment */
1641 if ( rlen > 2 )
1642 rlen -= 2;
1643 else
1644 rlen = 0;
1645
1646 if ( ( flags >> 8 ) == type )
1647 len += rlen;
1648 else
1649 {
1650 if ( pfb_lenpos + 3 > pfb_len + 2 )
1651 goto Exit2;
1652 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1653 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1654 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1655 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1656
1657 if ( ( flags >> 8 ) == 5 ) /* End of font mark */
1658 break;
1659
1660 if ( pfb_pos + 6 > pfb_len + 2 )
1661 goto Exit2;
1662 pfb_data[pfb_pos++] = 0x80;
1663
1664 type = flags >> 8;
1665 len = rlen;
1666
1667 pfb_data[pfb_pos++] = (FT_Byte)type;
1668 pfb_lenpos = pfb_pos;
1669 pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */
1670 pfb_data[pfb_pos++] = 0;
1671 pfb_data[pfb_pos++] = 0;
1672 pfb_data[pfb_pos++] = 0;
1673 }
1674
1675 error = FT_ERR( Cannot_Open_Resource );
1676 if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
1677 goto Exit2;
1678
1679 error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
1680 if ( error )
1681 goto Exit2;
1682 pfb_pos += rlen;
1683 }
1684
1685 if ( pfb_pos + 2 > pfb_len + 2 )
1686 goto Exit2;
1687 pfb_data[pfb_pos++] = 0x80;
1688 pfb_data[pfb_pos++] = 3;
1689
1690 if ( pfb_lenpos + 3 > pfb_len + 2 )
1691 goto Exit2;
1692 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1693 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1694 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1695 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1696
1697 return open_face_from_buffer( library,
1698 pfb_data,
1699 pfb_pos,
1700 face_index,
1701 "type1",
1702 aface );
1703
1704 Exit2:
1705 FT_FREE( pfb_data );
1706
1707 Exit:
1708 return error;
1709 }
1710
1711
1712 /* The resource header says we've got resource_cnt `sfnt' */
1713 /* (TrueType/OpenType) resources in this file. Look through */
1714 /* them for the one indicated by face_index, load it into mem, */
1715 /* pass it on the the truetype driver and return it. */
1716 /* */
1717 static FT_Error
1718 Mac_Read_sfnt_Resource( FT_Library library,
1719 FT_Stream stream,
1720 FT_Long *offsets,
1721 FT_Long resource_cnt,
1722 FT_Long face_index,
1723 FT_Face *aface )
1724 {
1725 FT_Memory memory = library->memory;
1726 FT_Byte* sfnt_data = NULL;
1727 FT_Error error;
1728 FT_Long flag_offset;
1729 FT_Long rlen;
1730 int is_cff;
1731 FT_Long face_index_in_resource = 0;
1732
1733
1734 if ( face_index == -1 )
1735 face_index = 0;
1736 if ( face_index >= resource_cnt )
1737 return FT_THROW( Cannot_Open_Resource );
1738
1739 flag_offset = offsets[face_index];
1740 error = FT_Stream_Seek( stream, flag_offset );
1741 if ( error )
1742 goto Exit;
1743
1744 if ( FT_READ_LONG( rlen ) )
1745 goto Exit;
1746 if ( rlen == -1 )
1747 return FT_THROW( Cannot_Open_Resource );
1748
1749 error = open_face_PS_from_sfnt_stream( library,
1750 stream,
1751 face_index,
1752 0, NULL,
1753 aface );
1754 if ( !error )
1755 goto Exit;
1756
1757 /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
1758 if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
1759 goto Exit;
1760
1761 if ( FT_ALLOC( sfnt_data, (FT_Long)rlen ) )
1762 return error;
1763 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
1764 if ( error )
1765 goto Exit;
1766
1767 is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1768 error = open_face_from_buffer( library,
1769 sfnt_data,
1770 rlen,
1771 face_index_in_resource,
1772 is_cff ? "cff" : "truetype",
1773 aface );
1774
1775 Exit:
1776 return error;
1777 }
1778
1779
1780 /* Check for a valid resource fork header, or a valid dfont */
1781 /* header. In a resource fork the first 16 bytes are repeated */
1782 /* at the location specified by bytes 4-7. In a dfont bytes */
1783 /* 4-7 point to 16 bytes of zeroes instead. */
1784 /* */
1785 static FT_Error
1786 IsMacResource( FT_Library library,
1787 FT_Stream stream,
1788 FT_Long resource_offset,
1789 FT_Long face_index,
1790 FT_Face *aface )
1791 {
1792 FT_Memory memory = library->memory;
1793 FT_Error error;
1794 FT_Long map_offset, rdara_pos;
1795 FT_Long *data_offsets;
1796 FT_Long count;
1797
1798
1799 error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
1800 &map_offset, &rdara_pos );
1801 if ( error )
1802 return error;
1803
1804 error = FT_Raccess_Get_DataOffsets( library, stream,
1805 map_offset, rdara_pos,
1806 TTAG_POST,
1807 &data_offsets, &count );
1808 if ( !error )
1809 {
1810 error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
1811 face_index, aface );
1812 FT_FREE( data_offsets );
1813 /* POST exists in an LWFN providing a single face */
1814 if ( !error )
1815 (*aface)->num_faces = 1;
1816 return error;
1817 }
1818
1819 error = FT_Raccess_Get_DataOffsets( library, stream,
1820 map_offset, rdara_pos,
1821 TTAG_sfnt,
1822 &data_offsets, &count );
1823 if ( !error )
1824 {
1825 FT_Long face_index_internal = face_index % count;
1826
1827
1828 error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
1829 face_index_internal, aface );
1830 FT_FREE( data_offsets );
1831 if ( !error )
1832 (*aface)->num_faces = count;
1833 }
1834
1835 return error;
1836 }
1837
1838
1839 /* Check for a valid macbinary header, and if we find one */
1840 /* check that the (flattened) resource fork in it is valid. */
1841 /* */
1842 static FT_Error
1843 IsMacBinary( FT_Library library,
1844 FT_Stream stream,
1845 FT_Long face_index,
1846 FT_Face *aface )
1847 {
1848 unsigned char header[128];
1849 FT_Error error;
1850 FT_Long dlen, offset;
1851
1852
1853 if ( NULL == stream )
1854 return FT_THROW( Invalid_Stream_Operation );
1855
1856 error = FT_Stream_Seek( stream, 0 );
1857 if ( error )
1858 goto Exit;
1859
1860 error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
1861 if ( error )
1862 goto Exit;
1863
1864 if ( header[ 0] != 0 ||
1865 header[74] != 0 ||
1866 header[82] != 0 ||
1867 header[ 1] == 0 ||
1868 header[ 1] > 33 ||
1869 header[63] != 0 ||
1870 header[2 + header[1]] != 0 )
1871 return FT_THROW( Unknown_File_Format );
1872
1873 dlen = ( header[0x53] << 24 ) |
1874 ( header[0x54] << 16 ) |
1875 ( header[0x55] << 8 ) |
1876 header[0x56];
1877 #if 0
1878 rlen = ( header[0x57] << 24 ) |
1879 ( header[0x58] << 16 ) |
1880 ( header[0x59] << 8 ) |
1881 header[0x5a];
1882 #endif /* 0 */
1883 offset = 128 + ( ( dlen + 127 ) & ~127 );
1884
1885 return IsMacResource( library, stream, offset, face_index, aface );
1886
1887 Exit:
1888 return error;
1889 }
1890
1891
1892 static FT_Error
1893 load_face_in_embedded_rfork( FT_Library library,
1894 FT_Stream stream,
1895 FT_Long face_index,
1896 FT_Face *aface,
1897 const FT_Open_Args *args )
1898 {
1899
1900 #undef FT_COMPONENT
1901 #define FT_COMPONENT trace_raccess
1902
1903 FT_Memory memory = library->memory;
1904 FT_Error error = FT_ERR( Unknown_File_Format );
1905 int i;
1906
1907 char * file_names[FT_RACCESS_N_RULES];
1908 FT_Long offsets[FT_RACCESS_N_RULES];
1909 FT_Error errors[FT_RACCESS_N_RULES];
1910 FT_Bool is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
1911
1912 FT_Open_Args args2;
1913 FT_Stream stream2 = 0;
1914
1915
1916 FT_Raccess_Guess( library, stream,
1917 args->pathname, file_names, offsets, errors );
1918
1919 for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
1920 {
1921 is_darwin_vfs = ft_raccess_rule_by_darwin_vfs( library, i );
1922 if ( is_darwin_vfs && vfs_rfork_has_no_font )
1923 {
1924 FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
1925 " is already checked and"
1926 " no font is found\n", i ));
1927 continue;
1928 }
1929
1930 if ( errors[i] )
1931 {
1932 FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
1933 continue;
1934 }
1935
1936 args2.flags = FT_OPEN_PATHNAME;
1937 args2.pathname = file_names[i] ? file_names[i] : args->pathname;
1938
1939 FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
1940 i, args2.pathname, offsets[i] ));
1941
1942 error = FT_Stream_New( library, &args2, &stream2 );
1943 if ( is_darwin_vfs && FT_ERR_EQ( error, Cannot_Open_Stream ) )
1944 vfs_rfork_has_no_font = TRUE;
1945
1946 if ( error )
1947 {
1948 FT_TRACE3(( "failed\n" ));
1949 continue;
1950 }
1951
1952 error = IsMacResource( library, stream2, offsets[i],
1953 face_index, aface );
1954 FT_Stream_Free( stream2, 0 );
1955
1956 FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
1957
1958 if ( !error )
1959 break;
1960 else if ( is_darwin_vfs )
1961 vfs_rfork_has_no_font = TRUE;
1962 }
1963
1964 for (i = 0; i < FT_RACCESS_N_RULES; i++)
1965 {
1966 if ( file_names[i] )
1967 FT_FREE( file_names[i] );
1968 }
1969
1970 /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
1971 if ( error )
1972 error = FT_ERR( Unknown_File_Format );
1973
1974 return error;
1975
1976 #undef FT_COMPONENT
1977 #define FT_COMPONENT trace_objs
1978
1979 }
1980
1981
1982 /* Check for some macintosh formats without Carbon framework. */
1983 /* Is this a macbinary file? If so look at the resource fork. */
1984 /* Is this a mac dfont file? */
1985 /* Is this an old style resource fork? (in data) */
1986 /* Else call load_face_in_embedded_rfork to try extra rules */
1987 /* (defined in `ftrfork.c'). */
1988 /* */
1989 static FT_Error
1990 load_mac_face( FT_Library library,
1991 FT_Stream stream,
1992 FT_Long face_index,
1993 FT_Face *aface,
1994 const FT_Open_Args *args )
1995 {
1996 FT_Error error;
1997 FT_UNUSED( args );
1998
1999
2000 error = IsMacBinary( library, stream, face_index, aface );
2001 if ( FT_ERR_EQ( error, Unknown_File_Format ) )
2002 {
2003
2004 #undef FT_COMPONENT
2005 #define FT_COMPONENT trace_raccess
2006
2007 FT_TRACE3(( "Try as dfont: %s ...", args->pathname ));
2008
2009 error = IsMacResource( library, stream, 0, face_index, aface );
2010
2011 FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
2012
2013 #undef FT_COMPONENT
2014 #define FT_COMPONENT trace_objs
2015
2016 }
2017
2018 if ( ( FT_ERR_EQ( error, Unknown_File_Format ) ||
2019 FT_ERR_EQ( error, Invalid_Stream_Operation ) ) &&
2020 ( args->flags & FT_OPEN_PATHNAME ) )
2021 error = load_face_in_embedded_rfork( library, stream,
2022 face_index, aface, args );
2023 return error;
2024 }
2025 #endif
2026
2027 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2028
2029
2030 /* documentation is in freetype.h */
2031
2032 FT_EXPORT_DEF( FT_Error )
2033 FT_Open_Face( FT_Library library,
2034 const FT_Open_Args* args,
2035 FT_Long face_index,
2036 FT_Face *aface )
2037 {
2038 FT_Error error;
2039 FT_Driver driver = NULL;
2040 FT_Memory memory = NULL;
2041 FT_Stream stream = NULL;
2042 FT_Face face = NULL;
2043 FT_ListNode node = NULL;
2044 FT_Bool external_stream;
2045 FT_Module* cur;
2046 FT_Module* limit;
2047
2048
2049 /* test for valid `library' delayed to */
2050 /* FT_Stream_New() */
2051
2052 if ( ( !aface && face_index >= 0 ) || !args )
2053 return FT_THROW( Invalid_Argument );
2054
2055 external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2056 args->stream );
2057
2058 /* create input stream */
2059 error = FT_Stream_New( library, args, &stream );
2060 if ( error )
2061 goto Fail3;
2062
2063 memory = library->memory;
2064
2065 /* If the font driver is specified in the `args' structure, use */
2066 /* it. Otherwise, we scan the list of registered drivers. */
2067 if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2068 {
2069 driver = FT_DRIVER( args->driver );
2070
2071 /* not all modules are drivers, so check... */
2072 if ( FT_MODULE_IS_DRIVER( driver ) )
2073 {
2074 FT_Int num_params = 0;
2075 FT_Parameter* params = 0;
2076
2077
2078 if ( args->flags & FT_OPEN_PARAMS )
2079 {
2080 num_params = args->num_params;
2081 params = args->params;
2082 }
2083
2084 error = open_face( driver, &stream, external_stream, face_index,
2085 num_params, params, &face );
2086 if ( !error )
2087 goto Success;
2088 }
2089 else
2090 error = FT_THROW( Invalid_Handle );
2091
2092 FT_Stream_Free( stream, external_stream );
2093 goto Fail;
2094 }
2095 else
2096 {
2097 error = FT_ERR( Missing_Module );
2098
2099 /* check each font driver for an appropriate format */
2100 cur = library->modules;
2101 limit = cur + library->num_modules;
2102
2103 for ( ; cur < limit; cur++ )
2104 {
2105 /* not all modules are font drivers, so check... */
2106 if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2107 {
2108 FT_Int num_params = 0;
2109 FT_Parameter* params = 0;
2110
2111
2112 driver = FT_DRIVER( cur[0] );
2113
2114 if ( args->flags & FT_OPEN_PARAMS )
2115 {
2116 num_params = args->num_params;
2117 params = args->params;
2118 }
2119
2120 error = open_face( driver, &stream, external_stream, face_index,
2121 num_params, params, &face );
2122 if ( !error )
2123 goto Success;
2124
2125 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2126 if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2127 FT_ERR_EQ( error, Table_Missing ) )
2128 {
2129 /* TrueType but essential tables are missing */
2130 if ( FT_Stream_Seek( stream, 0 ) )
2131 break;
2132
2133 error = open_face_PS_from_sfnt_stream( library,
2134 stream,
2135 face_index,
2136 num_params,
2137 params,
2138 aface );
2139 if ( !error )
2140 {
2141 FT_Stream_Free( stream, external_stream );
2142 return error;
2143 }
2144 }
2145 #endif
2146
2147 if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2148 goto Fail3;
2149 }
2150 }
2151
2152 Fail3:
2153 /* If we are on the mac, and we get an */
2154 /* FT_Err_Invalid_Stream_Operation it may be because we have an */
2155 /* empty data fork, so we need to check the resource fork. */
2156 if ( FT_ERR_NEQ( error, Cannot_Open_Stream ) &&
2157 FT_ERR_NEQ( error, Unknown_File_Format ) &&
2158 FT_ERR_NEQ( error, Invalid_Stream_Operation ) )
2159 goto Fail2;
2160
2161 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2162 error = load_mac_face( library, stream, face_index, aface, args );
2163 if ( !error )
2164 {
2165 /* We don't want to go to Success here. We've already done that. */
2166 /* On the other hand, if we succeeded we still need to close this */
2167 /* stream (we opened a different stream which extracted the */
2168 /* interesting information out of this stream here. That stream */
2169 /* will still be open and the face will point to it). */
2170 FT_Stream_Free( stream, external_stream );
2171 return error;
2172 }
2173
2174 if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2175 goto Fail2;
2176 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2177
2178 /* no driver is able to handle this format */
2179 error = FT_THROW( Unknown_File_Format );
2180
2181 Fail2:
2182 FT_Stream_Free( stream, external_stream );
2183 goto Fail;
2184 }
2185
2186 Success:
2187 FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2188
2189 /* add the face object to its driver's list */
2190 if ( FT_NEW( node ) )
2191 goto Fail;
2192
2193 node->data = face;
2194 /* don't assume driver is the same as face->driver, so use */
2195 /* face->driver instead. */
2196 FT_List_Add( &face->driver->faces_list, node );
2197
2198 /* now allocate a glyph slot object for the face */
2199 FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2200
2201 if ( face_index >= 0 )
2202 {
2203 error = FT_New_GlyphSlot( face, NULL );
2204 if ( error )
2205 goto Fail;
2206
2207 /* finally, allocate a size object for the face */
2208 {
2209 FT_Size size;
2210
2211
2212 FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2213
2214 error = FT_New_Size( face, &size );
2215 if ( error )
2216 goto Fail;
2217
2218 face->size = size;
2219 }
2220 }
2221
2222 /* some checks */
2223
2224 if ( FT_IS_SCALABLE( face ) )
2225 {
2226 if ( face->height < 0 )
2227 face->height = (FT_Short)-face->height;
2228
2229 if ( !FT_HAS_VERTICAL( face ) )
2230 face->max_advance_height = (FT_Short)face->height;
2231 }
2232
2233 if ( FT_HAS_FIXED_SIZES( face ) )
2234 {
2235 FT_Int i;
2236
2237
2238 for ( i = 0; i < face->num_fixed_sizes; i++ )
2239 {
2240 FT_Bitmap_Size* bsize = face->available_sizes + i;
2241
2242
2243 if ( bsize->height < 0 )
2244 bsize->height = (FT_Short)-bsize->height;
2245 if ( bsize->x_ppem < 0 )
2246 bsize->x_ppem = (FT_Short)-bsize->x_ppem;
2247 if ( bsize->y_ppem < 0 )
2248 bsize->y_ppem = -bsize->y_ppem;
2249 }
2250 }
2251
2252 /* initialize internal face data */
2253 {
2254 FT_Face_Internal internal = face->internal;
2255
2256
2257 internal->transform_matrix.xx = 0x10000L;
2258 internal->transform_matrix.xy = 0;
2259 internal->transform_matrix.yx = 0;
2260 internal->transform_matrix.yy = 0x10000L;
2261
2262 internal->transform_delta.x = 0;
2263 internal->transform_delta.y = 0;
2264
2265 internal->refcount = 1;
2266 }
2267
2268 if ( aface )
2269 *aface = face;
2270 else
2271 FT_Done_Face( face );
2272
2273 goto Exit;
2274
2275 Fail:
2276 if ( node )
2277 FT_Done_Face( face ); /* face must be in the driver's list */
2278 else if ( face )
2279 destroy_face( memory, face, driver );
2280
2281 Exit:
2282 FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
2283
2284 return error;
2285 }
2286
2287
2288 /* documentation is in freetype.h */
2289
2290 FT_EXPORT_DEF( FT_Error )
2291 FT_Attach_File( FT_Face face,
2292 const char* filepathname )
2293 {
2294 FT_Open_Args open;
2295
2296
2297 /* test for valid `face' delayed to FT_Attach_Stream() */
2298
2299 if ( !filepathname )
2300 return FT_THROW( Invalid_Argument );
2301
2302 open.stream = NULL;
2303 open.flags = FT_OPEN_PATHNAME;
2304 open.pathname = (char*)filepathname;
2305
2306 return FT_Attach_Stream( face, &open );
2307 }
2308
2309
2310 /* documentation is in freetype.h */
2311
2312 FT_EXPORT_DEF( FT_Error )
2313 FT_Attach_Stream( FT_Face face,
2314 FT_Open_Args* parameters )
2315 {
2316 FT_Stream stream;
2317 FT_Error error;
2318 FT_Driver driver;
2319
2320 FT_Driver_Class clazz;
2321
2322
2323 /* test for valid `parameters' delayed to FT_Stream_New() */
2324
2325 if ( !face )
2326 return FT_THROW( Invalid_Face_Handle );
2327
2328 driver = face->driver;
2329 if ( !driver )
2330 return FT_THROW( Invalid_Driver_Handle );
2331
2332 error = FT_Stream_New( driver->root.library, parameters, &stream );
2333 if ( error )
2334 goto Exit;
2335
2336 /* we implement FT_Attach_Stream in each driver through the */
2337 /* `attach_file' interface */
2338
2339 error = FT_ERR( Unimplemented_Feature );
2340 clazz = driver->clazz;
2341 if ( clazz->attach_file )
2342 error = clazz->attach_file( face, stream );
2343
2344 /* close the attached stream */
2345 FT_Stream_Free( stream,
2346 (FT_Bool)( parameters->stream &&
2347 ( parameters->flags & FT_OPEN_STREAM ) ) );
2348
2349 Exit:
2350 return error;
2351 }
2352
2353
2354 /* documentation is in freetype.h */
2355
2356 FT_EXPORT_DEF( FT_Error )
2357 FT_Reference_Face( FT_Face face )
2358 {
2359 face->internal->refcount++;
2360
2361 return FT_Err_Ok;
2362 }
2363
2364
2365 /* documentation is in freetype.h */
2366
2367 FT_EXPORT_DEF( FT_Error )
2368 FT_Done_Face( FT_Face face )
2369 {
2370 FT_Error error;
2371 FT_Driver driver;
2372 FT_Memory memory;
2373 FT_ListNode node;
2374
2375
2376 error = FT_ERR( Invalid_Face_Handle );
2377 if ( face && face->driver )
2378 {
2379 face->internal->refcount--;
2380 if ( face->internal->refcount > 0 )
2381 error = FT_Err_Ok;
2382 else
2383 {
2384 driver = face->driver;
2385 memory = driver->root.memory;
2386
2387 /* find face in driver's list */
2388 node = FT_List_Find( &driver->faces_list, face );
2389 if ( node )
2390 {
2391 /* remove face object from the driver's list */
2392 FT_List_Remove( &driver->faces_list, node );
2393 FT_FREE( node );
2394
2395 /* now destroy the object proper */
2396 destroy_face( memory, face, driver );
2397 error = FT_Err_Ok;
2398 }
2399 }
2400 }
2401
2402 return error;
2403 }
2404
2405
2406 /* documentation is in ftobjs.h */
2407
2408 FT_EXPORT_DEF( FT_Error )
2409 FT_New_Size( FT_Face face,
2410 FT_Size *asize )
2411 {
2412 FT_Error error;
2413 FT_Memory memory;
2414 FT_Driver driver;
2415 FT_Driver_Class clazz;
2416
2417 FT_Size size = 0;
2418 FT_ListNode node = 0;
2419
2420
2421 if ( !face )
2422 return FT_THROW( Invalid_Face_Handle );
2423
2424 if ( !asize )
2425 return FT_THROW( Invalid_Size_Handle );
2426
2427 if ( !face->driver )
2428 return FT_THROW( Invalid_Driver_Handle );
2429
2430 *asize = 0;
2431
2432 driver = face->driver;
2433 clazz = driver->clazz;
2434 memory = face->memory;
2435
2436 /* Allocate new size object and perform basic initialisation */
2437 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
2438 goto Exit;
2439
2440 size->face = face;
2441
2442 /* for now, do not use any internal fields in size objects */
2443 size->internal = 0;
2444
2445 if ( clazz->init_size )
2446 error = clazz->init_size( size );
2447
2448 /* in case of success, add to the face's list */
2449 if ( !error )
2450 {
2451 *asize = size;
2452 node->data = size;
2453 FT_List_Add( &face->sizes_list, node );
2454 }
2455
2456 Exit:
2457 if ( error )
2458 {
2459 FT_FREE( node );
2460 FT_FREE( size );
2461 }
2462
2463 return error;
2464 }
2465
2466
2467 /* documentation is in ftobjs.h */
2468
2469 FT_EXPORT_DEF( FT_Error )
2470 FT_Done_Size( FT_Size size )
2471 {
2472 FT_Error error;
2473 FT_Driver driver;
2474 FT_Memory memory;
2475 FT_Face face;
2476 FT_ListNode node;
2477
2478
2479 if ( !size )
2480 return FT_THROW( Invalid_Size_Handle );
2481
2482 face = size->face;
2483 if ( !face )
2484 return FT_THROW( Invalid_Face_Handle );
2485
2486 driver = face->driver;
2487 if ( !driver )
2488 return FT_THROW( Invalid_Driver_Handle );
2489
2490 memory = driver->root.memory;
2491
2492 error = FT_Err_Ok;
2493 node = FT_List_Find( &face->sizes_list, size );
2494 if ( node )
2495 {
2496 FT_List_Remove( &face->sizes_list, node );
2497 FT_FREE( node );
2498
2499 if ( face->size == size )
2500 {
2501 face->size = 0;
2502 if ( face->sizes_list.head )
2503 face->size = (FT_Size)(face->sizes_list.head->data);
2504 }
2505
2506 destroy_size( memory, size, driver );
2507 }
2508 else
2509 error = FT_THROW( Invalid_Size_Handle );
2510
2511 return error;
2512 }
2513
2514
2515 /* documentation is in ftobjs.h */
2516
2517 FT_BASE_DEF( FT_Error )
2518 FT_Match_Size( FT_Face face,
2519 FT_Size_Request req,
2520 FT_Bool ignore_width,
2521 FT_ULong* size_index )
2522 {
2523 FT_Int i;
2524 FT_Long w, h;
2525
2526
2527 if ( !FT_HAS_FIXED_SIZES( face ) )
2528 return FT_THROW( Invalid_Face_Handle );
2529
2530 /* FT_Bitmap_Size doesn't provide enough info... */
2531 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2532 return FT_THROW( Unimplemented_Feature );
2533
2534 w = FT_REQUEST_WIDTH ( req );
2535 h = FT_REQUEST_HEIGHT( req );
2536
2537 if ( req->width && !req->height )
2538 h = w;
2539 else if ( !req->width && req->height )
2540 w = h;
2541
2542 w = FT_PIX_ROUND( w );
2543 h = FT_PIX_ROUND( h );
2544
2545 for ( i = 0; i < face->num_fixed_sizes; i++ )
2546 {
2547 FT_Bitmap_Size* bsize = face->available_sizes + i;
2548
2549
2550 if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
2551 continue;
2552
2553 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
2554 {
2555 FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i ));
2556
2557 if ( size_index )
2558 *size_index = (FT_ULong)i;
2559
2560 return FT_Err_Ok;
2561 }
2562 }
2563
2564 return FT_THROW( Invalid_Pixel_Size );
2565 }
2566
2567
2568 /* documentation is in ftobjs.h */
2569
2570 FT_BASE_DEF( void )
2571 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics,
2572 FT_Pos advance )
2573 {
2574 FT_Pos height = metrics->height;
2575
2576
2577 /* compensate for glyph with bbox above/below the baseline */
2578 if ( metrics->horiBearingY < 0 )
2579 {
2580 if ( height < metrics->horiBearingY )
2581 height = metrics->horiBearingY;
2582 }
2583 else if ( metrics->horiBearingY > 0 )
2584 height -= metrics->horiBearingY;
2585
2586 /* the factor 1.2 is a heuristical value */
2587 if ( !advance )
2588 advance = height * 12 / 10;
2589
2590 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
2591 metrics->vertBearingY = ( advance - height ) / 2;
2592 metrics->vertAdvance = advance;
2593 }
2594
2595
2596 static void
2597 ft_recompute_scaled_metrics( FT_Face face,
2598 FT_Size_Metrics* metrics )
2599 {
2600 /* Compute root ascender, descender, test height, and max_advance */
2601
2602 #ifdef GRID_FIT_METRICS
2603 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender,
2604 metrics->y_scale ) );
2605
2606 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender,
2607 metrics->y_scale ) );
2608
2609 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height,
2610 metrics->y_scale ) );
2611
2612 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
2613 metrics->x_scale ) );
2614 #else /* !GRID_FIT_METRICS */
2615 metrics->ascender = FT_MulFix( face->ascender,
2616 metrics->y_scale );
2617
2618 metrics->descender = FT_MulFix( face->descender,
2619 metrics->y_scale );
2620
2621 metrics->height = FT_MulFix( face->height,
2622 metrics->y_scale );
2623
2624 metrics->max_advance = FT_MulFix( face->max_advance_width,
2625 metrics->x_scale );
2626 #endif /* !GRID_FIT_METRICS */
2627 }
2628
2629
2630 FT_BASE_DEF( void )
2631 FT_Select_Metrics( FT_Face face,
2632 FT_ULong strike_index )
2633 {
2634 FT_Size_Metrics* metrics;
2635 FT_Bitmap_Size* bsize;
2636
2637
2638 metrics = &face->size->metrics;
2639 bsize = face->available_sizes + strike_index;
2640
2641 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
2642 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
2643
2644 if ( FT_IS_SCALABLE( face ) )
2645 {
2646 metrics->x_scale = FT_DivFix( bsize->x_ppem,
2647 face->units_per_EM );
2648 metrics->y_scale = FT_DivFix( bsize->y_ppem,
2649 face->units_per_EM );
2650
2651 ft_recompute_scaled_metrics( face, metrics );
2652 }
2653 else
2654 {
2655 metrics->x_scale = 1L << 16;
2656 metrics->y_scale = 1L << 16;
2657 metrics->ascender = bsize->y_ppem;
2658 metrics->descender = 0;
2659 metrics->height = bsize->height << 6;
2660 metrics->max_advance = bsize->x_ppem;
2661 }
2662
2663 FT_TRACE5(( "FT_Select_Metrics:\n" ));
2664 FT_TRACE5(( " x scale: %d (%f)\n",
2665 metrics->x_scale, metrics->x_scale / 65536.0 ));
2666 FT_TRACE5(( " y scale: %d (%f)\n",
2667 metrics->y_scale, metrics->y_scale / 65536.0 ));
2668 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2669 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2670 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2671 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2672 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2673 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2674 }
2675
2676
2677 FT_BASE_DEF( void )
2678 FT_Request_Metrics( FT_Face face,
2679 FT_Size_Request req )
2680 {
2681 FT_Size_Metrics* metrics;
2682
2683
2684 metrics = &face->size->metrics;
2685
2686 if ( FT_IS_SCALABLE( face ) )
2687 {
2688 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0;
2689
2690
2691 switch ( req->type )
2692 {
2693 case FT_SIZE_REQUEST_TYPE_NOMINAL:
2694 w = h = face->units_per_EM;
2695 break;
2696
2697 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
2698 w = h = face->ascender - face->descender;
2699 break;
2700
2701 case FT_SIZE_REQUEST_TYPE_BBOX:
2702 w = face->bbox.xMax - face->bbox.xMin;
2703 h = face->bbox.yMax - face->bbox.yMin;
2704 break;
2705
2706 case FT_SIZE_REQUEST_TYPE_CELL:
2707 w = face->max_advance_width;
2708 h = face->ascender - face->descender;
2709 break;
2710
2711 case FT_SIZE_REQUEST_TYPE_SCALES:
2712 metrics->x_scale = (FT_Fixed)req->width;
2713 metrics->y_scale = (FT_Fixed)req->height;
2714 if ( !metrics->x_scale )
2715 metrics->x_scale = metrics->y_scale;
2716 else if ( !metrics->y_scale )
2717 metrics->y_scale = metrics->x_scale;
2718 goto Calculate_Ppem;
2719
2720 case FT_SIZE_REQUEST_TYPE_MAX:
2721 break;
2722 }
2723
2724 /* to be on the safe side */
2725 if ( w < 0 )
2726 w = -w;
2727
2728 if ( h < 0 )
2729 h = -h;
2730
2731 scaled_w = FT_REQUEST_WIDTH ( req );
2732 scaled_h = FT_REQUEST_HEIGHT( req );
2733
2734 /* determine scales */
2735 if ( req->width )
2736 {
2737 metrics->x_scale = FT_DivFix( scaled_w, w );
2738
2739 if ( req->height )
2740 {
2741 metrics->y_scale = FT_DivFix( scaled_h, h );
2742
2743 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
2744 {
2745 if ( metrics->y_scale > metrics->x_scale )
2746 metrics->y_scale = metrics->x_scale;
2747 else
2748 metrics->x_scale = metrics->y_scale;
2749 }
2750 }
2751 else
2752 {
2753 metrics->y_scale = metrics->x_scale;
2754 scaled_h = FT_MulDiv( scaled_w, h, w );
2755 }
2756 }
2757 else
2758 {
2759 metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
2760 scaled_w = FT_MulDiv( scaled_h, w, h );
2761 }
2762
2763 Calculate_Ppem:
2764 /* calculate the ppems */
2765 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2766 {
2767 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
2768 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
2769 }
2770
2771 metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
2772 metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
2773
2774 ft_recompute_scaled_metrics( face, metrics );
2775 }
2776 else
2777 {
2778 FT_ZERO( metrics );
2779 metrics->x_scale = 1L << 16;
2780 metrics->y_scale = 1L << 16;
2781 }
2782
2783 FT_TRACE5(( "FT_Request_Metrics:\n" ));
2784 FT_TRACE5(( " x scale: %d (%f)\n",
2785 metrics->x_scale, metrics->x_scale / 65536.0 ));
2786 FT_TRACE5(( " y scale: %d (%f)\n",
2787 metrics->y_scale, metrics->y_scale / 65536.0 ));
2788 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2789 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2790 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2791 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2792 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2793 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2794 }
2795
2796
2797 /* documentation is in freetype.h */
2798
2799 FT_EXPORT_DEF( FT_Error )
2800 FT_Select_Size( FT_Face face,
2801 FT_Int strike_index )
2802 {
2803 FT_Driver_Class clazz;
2804
2805
2806 if ( !face || !FT_HAS_FIXED_SIZES( face ) )
2807 return FT_THROW( Invalid_Face_Handle );
2808
2809 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
2810 return FT_THROW( Invalid_Argument );
2811
2812 clazz = face->driver->clazz;
2813
2814 if ( clazz->select_size )
2815 {
2816 FT_Error error;
2817
2818
2819 error = clazz->select_size( face->size, (FT_ULong)strike_index );
2820
2821 #ifdef FT_DEBUG_LEVEL_TRACE
2822 {
2823 FT_Size_Metrics* metrics = &face->size->metrics;
2824
2825
2826 FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" ));
2827 FT_TRACE5(( " x scale: %d (%f)\n",
2828 metrics->x_scale, metrics->x_scale / 65536.0 ));
2829 FT_TRACE5(( " y scale: %d (%f)\n",
2830 metrics->y_scale, metrics->y_scale / 65536.0 ));
2831 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2832 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2833 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2834 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2835 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2836 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2837 }
2838 #endif
2839
2840 return error;
2841 }
2842
2843 FT_Select_Metrics( face, (FT_ULong)strike_index );
2844
2845 return FT_Err_Ok;
2846 }
2847
2848
2849 /* documentation is in freetype.h */
2850
2851 FT_EXPORT_DEF( FT_Error )
2852 FT_Request_Size( FT_Face face,
2853 FT_Size_Request req )
2854 {
2855 FT_Driver_Class clazz;
2856 FT_ULong strike_index;
2857
2858
2859 if ( !face )
2860 return FT_THROW( Invalid_Face_Handle );
2861
2862 if ( !req || req->width < 0 || req->height < 0 ||
2863 req->type >= FT_SIZE_REQUEST_TYPE_MAX )
2864 return FT_THROW( Invalid_Argument );
2865
2866 clazz = face->driver->clazz;
2867
2868 if ( clazz->request_size )
2869 {
2870 FT_Error error;
2871
2872
2873 error = clazz->request_size( face->size, req );
2874
2875 #ifdef FT_DEBUG_LEVEL_TRACE
2876 {
2877 FT_Size_Metrics* metrics = &face->size->metrics;
2878
2879
2880 FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" ));
2881 FT_TRACE5(( " x scale: %d (%f)\n",
2882 metrics->x_scale, metrics->x_scale / 65536.0 ));
2883 FT_TRACE5(( " y scale: %d (%f)\n",
2884 metrics->y_scale, metrics->y_scale / 65536.0 ));
2885 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2886 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2887 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2888 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2889 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2890 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2891 }
2892 #endif
2893
2894 return error;
2895 }
2896
2897 /*
2898 * The reason that a driver doesn't have `request_size' defined is
2899 * either that the scaling here suffices or that the supported formats
2900 * are bitmap-only and size matching is not implemented.
2901 *
2902 * In the latter case, a simple size matching is done.
2903 */
2904 if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
2905 {
2906 FT_Error error;
2907
2908
2909 error = FT_Match_Size( face, req, 0, &strike_index );
2910 if ( error )
2911 return error;
2912
2913 return FT_Select_Size( face, (FT_Int)strike_index );
2914 }
2915
2916 FT_Request_Metrics( face, req );
2917
2918 return FT_Err_Ok;
2919 }
2920
2921
2922 /* documentation is in freetype.h */
2923
2924 FT_EXPORT_DEF( FT_Error )
2925 FT_Set_Char_Size( FT_Face face,
2926 FT_F26Dot6 char_width,
2927 FT_F26Dot6 char_height,
2928 FT_UInt horz_resolution,
2929 FT_UInt vert_resolution )
2930 {
2931 FT_Size_RequestRec req;
2932
2933
2934 if ( !char_width )
2935 char_width = char_height;
2936 else if ( !char_height )
2937 char_height = char_width;
2938
2939 if ( !horz_resolution )
2940 horz_resolution = vert_resolution;
2941 else if ( !vert_resolution )
2942 vert_resolution = horz_resolution;
2943
2944 if ( char_width < 1 * 64 )
2945 char_width = 1 * 64;
2946 if ( char_height < 1 * 64 )
2947 char_height = 1 * 64;
2948
2949 if ( !horz_resolution )
2950 horz_resolution = vert_resolution = 72;
2951
2952 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2953 req.width = char_width;
2954 req.height = char_height;
2955 req.horiResolution = horz_resolution;
2956 req.vertResolution = vert_resolution;
2957
2958 return FT_Request_Size( face, &req );
2959 }
2960
2961
2962 /* documentation is in freetype.h */
2963
2964 FT_EXPORT_DEF( FT_Error )
2965 FT_Set_Pixel_Sizes( FT_Face face,
2966 FT_UInt pixel_width,
2967 FT_UInt pixel_height )
2968 {
2969 FT_Size_RequestRec req;
2970
2971
2972 if ( pixel_width == 0 )
2973 pixel_width = pixel_height;
2974 else if ( pixel_height == 0 )
2975 pixel_height = pixel_width;
2976
2977 if ( pixel_width < 1 )
2978 pixel_width = 1;
2979 if ( pixel_height < 1 )
2980 pixel_height = 1;
2981
2982 /* use `>=' to avoid potential compiler warning on 16bit platforms */
2983 if ( pixel_width >= 0xFFFFU )
2984 pixel_width = 0xFFFFU;
2985 if ( pixel_height >= 0xFFFFU )
2986 pixel_height = 0xFFFFU;
2987
2988 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2989 req.width = pixel_width << 6;
2990 req.height = pixel_height << 6;
2991 req.horiResolution = 0;
2992 req.vertResolution = 0;
2993
2994 return FT_Request_Size( face, &req );
2995 }
2996
2997
2998 /* documentation is in freetype.h */
2999
3000 FT_EXPORT_DEF( FT_Error )
3001 FT_Get_Kerning( FT_Face face,
3002 FT_UInt left_glyph,
3003 FT_UInt right_glyph,
3004 FT_UInt kern_mode,
3005 FT_Vector *akerning )
3006 {
3007 FT_Error error = FT_Err_Ok;
3008 FT_Driver driver;
3009
3010
3011 if ( !face )
3012 return FT_THROW( Invalid_Face_Handle );
3013
3014 if ( !akerning )
3015 return FT_THROW( Invalid_Argument );
3016
3017 driver = face->driver;
3018
3019 akerning->x = 0;
3020 akerning->y = 0;
3021
3022 if ( driver->clazz->get_kerning )
3023 {
3024 error = driver->clazz->get_kerning( face,
3025 left_glyph,
3026 right_glyph,
3027 akerning );
3028 if ( !error )
3029 {
3030 if ( kern_mode != FT_KERNING_UNSCALED )
3031 {
3032 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
3033 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
3034
3035 if ( kern_mode != FT_KERNING_UNFITTED )
3036 {
3037 /* we scale down kerning values for small ppem values */
3038 /* to avoid that rounding makes them too big. */
3039 /* `25' has been determined heuristically. */
3040 if ( face->size->metrics.x_ppem < 25 )
3041 akerning->x = FT_MulDiv( akerning->x,
3042 face->size->metrics.x_ppem, 25 );
3043 if ( face->size->metrics.y_ppem < 25 )
3044 akerning->y = FT_MulDiv( akerning->y,
3045 face->size->metrics.y_ppem, 25 );
3046
3047 akerning->x = FT_PIX_ROUND( akerning->x );
3048 akerning->y = FT_PIX_ROUND( akerning->y );
3049 }
3050 }
3051 }
3052 }
3053
3054 return error;
3055 }
3056
3057
3058 /* documentation is in freetype.h */
3059
3060 FT_EXPORT_DEF( FT_Error )
3061 FT_Get_Track_Kerning( FT_Face face,
3062 FT_Fixed point_size,
3063 FT_Int degree,
3064 FT_Fixed* akerning )
3065 {
3066 FT_Service_Kerning service;
3067 FT_Error error = FT_Err_Ok;
3068
3069
3070 if ( !face )
3071 return FT_THROW( Invalid_Face_Handle );
3072
3073 if ( !akerning )
3074 return FT_THROW( Invalid_Argument );
3075
3076 FT_FACE_FIND_SERVICE( face, service, KERNING );
3077 if ( !service )
3078 return FT_THROW( Unimplemented_Feature );
3079
3080 error = service->get_track( face,
3081 point_size,
3082 degree,
3083 akerning );
3084
3085 return error;
3086 }
3087
3088
3089 /* documentation is in freetype.h */
3090
3091 FT_EXPORT_DEF( FT_Error )
3092 FT_Select_Charmap( FT_Face face,
3093 FT_Encoding encoding )
3094 {
3095 FT_CharMap* cur;
3096 FT_CharMap* limit;
3097
3098
3099 if ( !face )
3100 return FT_THROW( Invalid_Face_Handle );
3101
3102 if ( encoding == FT_ENCODING_NONE )
3103 return FT_THROW( Invalid_Argument );
3104
3105 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */
3106 /* charmap available, i.e., one with UCS-4 characters, if possible. */
3107 /* */
3108 /* This is done by find_unicode_charmap() above, to share code. */
3109 if ( encoding == FT_ENCODING_UNICODE )
3110 return find_unicode_charmap( face );
3111
3112 cur = face->charmaps;
3113 if ( !cur )
3114 return FT_THROW( Invalid_CharMap_Handle );
3115
3116 limit = cur + face->num_charmaps;
3117
3118 for ( ; cur < limit; cur++ )
3119 {
3120 if ( cur[0]->encoding == encoding )
3121 {
3122 #ifdef FT_MAX_CHARMAP_CACHEABLE
3123 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3124 {
3125 FT_ERROR(( "FT_Select_Charmap: requested charmap is found (%d), "
3126 "but in too late position to cache\n",
3127 cur - face->charmaps ));
3128 continue;
3129 }
3130 #endif
3131 face->charmap = cur[0];
3132 return 0;
3133 }
3134 }
3135
3136 return FT_THROW( Invalid_Argument );
3137 }
3138
3139
3140 /* documentation is in freetype.h */
3141
3142 FT_EXPORT_DEF( FT_Error )
3143 FT_Set_Charmap( FT_Face face,
3144 FT_CharMap charmap )
3145 {
3146 FT_CharMap* cur;
3147 FT_CharMap* limit;
3148
3149
3150 if ( !face )
3151 return FT_THROW( Invalid_Face_Handle );
3152
3153 cur = face->charmaps;
3154 if ( !cur )
3155 return FT_THROW( Invalid_CharMap_Handle );
3156 if ( FT_Get_CMap_Format( charmap ) == 14 )
3157 return FT_THROW( Invalid_Argument );
3158
3159 limit = cur + face->num_charmaps;
3160
3161 for ( ; cur < limit; cur++ )
3162 {
3163 if ( cur[0] == charmap )
3164 {
3165 #ifdef FT_MAX_CHARMAP_CACHEABLE
3166 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3167 {
3168 FT_ERROR(( "FT_Set_Charmap: requested charmap is found (%d), "
3169 "but in too late position to cache\n",
3170 cur - face->charmaps ));
3171 continue;
3172 }
3173 #endif
3174 face->charmap = cur[0];
3175 return 0;
3176 }
3177 }
3178 return FT_THROW( Invalid_Argument );
3179 }
3180
3181
3182 /* documentation is in freetype.h */
3183
3184 FT_EXPORT_DEF( FT_Int )
3185 FT_Get_Charmap_Index( FT_CharMap charmap )
3186 {
3187 FT_Int i;
3188
3189
3190 if ( !charmap || !charmap->face )
3191 return -1;
3192
3193 for ( i = 0; i < charmap->face->num_charmaps; i++ )
3194 if ( charmap->face->charmaps[i] == charmap )
3195 break;
3196
3197 FT_ASSERT( i < charmap->face->num_charmaps );
3198
3199 #ifdef FT_MAX_CHARMAP_CACHEABLE
3200 if ( i > FT_MAX_CHARMAP_CACHEABLE )
3201 {
3202 FT_ERROR(( "FT_Get_Charmap_Index: requested charmap is found (%d), "
3203 "but in too late position to cache\n",
3204 i ));
3205 return -i;
3206 }
3207 #endif
3208 return i;
3209 }
3210
3211
3212 static void
3213 ft_cmap_done_internal( FT_CMap cmap )
3214 {
3215 FT_CMap_Class clazz = cmap->clazz;
3216 FT_Face face = cmap->charmap.face;
3217 FT_Memory memory = FT_FACE_MEMORY( face );
3218
3219
3220 if ( clazz->done )
3221 clazz->done( cmap );
3222
3223 FT_FREE( cmap );
3224 }
3225
3226
3227 FT_BASE_DEF( void )
3228 FT_CMap_Done( FT_CMap cmap )
3229 {
3230 if ( cmap )
3231 {
3232 FT_Face face = cmap->charmap.face;
3233 FT_Memory memory = FT_FACE_MEMORY( face );
3234 FT_Error error;
3235 FT_Int i, j;
3236
3237
3238 for ( i = 0; i < face->num_charmaps; i++ )
3239 {
3240 if ( (FT_CMap)face->charmaps[i] == cmap )
3241 {
3242 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1];
3243
3244
3245 if ( FT_RENEW_ARRAY( face->charmaps,
3246 face->num_charmaps,
3247 face->num_charmaps - 1 ) )
3248 return;
3249
3250 /* remove it from our list of charmaps */
3251 for ( j = i + 1; j < face->num_charmaps; j++ )
3252 {
3253 if ( j == face->num_charmaps - 1 )
3254 face->charmaps[j - 1] = last_charmap;
3255 else
3256 face->charmaps[j - 1] = face->charmaps[j];
3257 }
3258
3259 face->num_charmaps--;
3260
3261 if ( (FT_CMap)face->charmap == cmap )
3262 face->charmap = NULL;
3263
3264 ft_cmap_done_internal( cmap );
3265
3266 break;
3267 }
3268 }
3269 }
3270 }
3271
3272
3273 FT_BASE_DEF( FT_Error )
3274 FT_CMap_New( FT_CMap_Class clazz,
3275 FT_Pointer init_data,
3276 FT_CharMap charmap,
3277 FT_CMap *acmap )
3278 {
3279 FT_Error error = FT_Err_Ok;
3280 FT_Face face;
3281 FT_Memory memory;
3282 FT_CMap cmap = NULL;
3283
3284
3285 if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
3286 return FT_THROW( Invalid_Argument );
3287
3288 face = charmap->face;
3289 memory = FT_FACE_MEMORY( face );
3290
3291 if ( !FT_ALLOC( cmap, clazz->size ) )
3292 {
3293 cmap->charmap = *charmap;
3294 cmap->clazz = clazz;
3295
3296 if ( clazz->init )
3297 {
3298 error = clazz->init( cmap, init_data );
3299 if ( error )
3300 goto Fail;
3301 }
3302
3303 /* add it to our list of charmaps */
3304 if ( FT_RENEW_ARRAY( face->charmaps,
3305 face->num_charmaps,
3306 face->num_charmaps + 1 ) )
3307 goto Fail;
3308
3309 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3310 }
3311
3312 Exit:
3313 if ( acmap )
3314 *acmap = cmap;
3315
3316 return error;
3317
3318 Fail:
3319 ft_cmap_done_internal( cmap );
3320 cmap = NULL;
3321 goto Exit;
3322 }
3323
3324
3325 /* documentation is in freetype.h */
3326
3327 FT_EXPORT_DEF( FT_UInt )
3328 FT_Get_Char_Index( FT_Face face,
3329 FT_ULong charcode )
3330 {
3331 FT_UInt result = 0;
3332
3333
3334 if ( face && face->charmap )
3335 {
3336 FT_CMap cmap = FT_CMAP( face->charmap );
3337
3338
3339 if ( charcode > 0xFFFFFFFFUL )
3340 {
3341 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3342 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3343 }
3344 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3345 }
3346 return result;
3347 }
3348
3349
3350 /* documentation is in freetype.h */
3351
3352 FT_EXPORT_DEF( FT_ULong )
3353 FT_Get_First_Char( FT_Face face,
3354 FT_UInt *agindex )
3355 {
3356 FT_ULong result = 0;
3357 FT_UInt gindex = 0;
3358
3359
3360 if ( face && face->charmap && face->num_glyphs )
3361 {
3362 gindex = FT_Get_Char_Index( face, 0 );
3363 if ( gindex == 0 || gindex >= (FT_UInt)face->num_glyphs )
3364 result = FT_Get_Next_Char( face, 0, &gindex );
3365 }
3366
3367 if ( agindex )
3368 *agindex = gindex;
3369
3370 return result;
3371 }
3372
3373
3374 /* documentation is in freetype.h */
3375
3376 FT_EXPORT_DEF( FT_ULong )
3377 FT_Get_Next_Char( FT_Face face,
3378 FT_ULong charcode,
3379 FT_UInt *agindex )
3380 {
3381 FT_ULong result = 0;
3382 FT_UInt gindex = 0;
3383
3384
3385 if ( face && face->charmap && face->num_glyphs )
3386 {
3387 FT_UInt32 code = (FT_UInt32)charcode;
3388 FT_CMap cmap = FT_CMAP( face->charmap );
3389
3390
3391 do
3392 {
3393 gindex = cmap->clazz->char_next( cmap, &code );
3394
3395 } while ( gindex >= (FT_UInt)face->num_glyphs );
3396
3397 result = ( gindex == 0 ) ? 0 : code;
3398 }
3399
3400 if ( agindex )
3401 *agindex = gindex;
3402
3403 return result;
3404 }
3405
3406
3407 /* documentation is in freetype.h */
3408
3409 FT_EXPORT_DEF( FT_UInt )
3410 FT_Face_GetCharVariantIndex( FT_Face face,
3411 FT_ULong charcode,
3412 FT_ULong variantSelector )
3413 {
3414 FT_UInt result = 0;
3415
3416
3417 if ( face && face->charmap &&
3418 face->charmap->encoding == FT_ENCODING_UNICODE )
3419 {
3420 FT_CharMap charmap = find_variant_selector_charmap( face );
3421 FT_CMap ucmap = FT_CMAP( face->charmap );
3422
3423
3424 if ( charmap != NULL )
3425 {
3426 FT_CMap vcmap = FT_CMAP( charmap );
3427
3428
3429 if ( charcode > 0xFFFFFFFFUL )
3430 {
3431 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3432 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3433 }
3434 if ( variantSelector > 0xFFFFFFFFUL )
3435 {
3436 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3437 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3438 }
3439
3440 result = vcmap->clazz->char_var_index( vcmap, ucmap,
3441 (FT_UInt32)charcode,
3442 (FT_UInt32)variantSelector );
3443 }
3444 }
3445
3446 return result;
3447 }
3448
3449
3450 /* documentation is in freetype.h */
3451
3452 FT_EXPORT_DEF( FT_Int )
3453 FT_Face_GetCharVariantIsDefault( FT_Face face,
3454 FT_ULong charcode,
3455 FT_ULong variantSelector )
3456 {
3457 FT_Int result = -1;
3458
3459
3460 if ( face )
3461 {
3462 FT_CharMap charmap = find_variant_selector_charmap( face );
3463
3464
3465 if ( charmap != NULL )
3466 {
3467 FT_CMap vcmap = FT_CMAP( charmap );
3468
3469
3470 if ( charcode > 0xFFFFFFFFUL )
3471 {
3472 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3473 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3474 }
3475 if ( variantSelector > 0xFFFFFFFFUL )
3476 {
3477 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3478 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3479 }
3480
3481 result = vcmap->clazz->char_var_default( vcmap,
3482 (FT_UInt32)charcode,
3483 (FT_UInt32)variantSelector );
3484 }
3485 }
3486
3487 return result;
3488 }
3489
3490
3491 /* documentation is in freetype.h */
3492
3493 FT_EXPORT_DEF( FT_UInt32* )
3494 FT_Face_GetVariantSelectors( FT_Face face )
3495 {
3496 FT_UInt32 *result = NULL;
3497
3498
3499 if ( face )
3500 {
3501 FT_CharMap charmap = find_variant_selector_charmap( face );
3502
3503
3504 if ( charmap != NULL )
3505 {
3506 FT_CMap vcmap = FT_CMAP( charmap );
3507 FT_Memory memory = FT_FACE_MEMORY( face );
3508
3509
3510 result = vcmap->clazz->variant_list( vcmap, memory );
3511 }
3512 }
3513
3514 return result;
3515 }
3516
3517
3518 /* documentation is in freetype.h */
3519
3520 FT_EXPORT_DEF( FT_UInt32* )
3521 FT_Face_GetVariantsOfChar( FT_Face face,
3522 FT_ULong charcode )
3523 {
3524 FT_UInt32 *result = NULL;
3525
3526
3527 if ( face )
3528 {
3529 FT_CharMap charmap = find_variant_selector_charmap( face );
3530
3531
3532 if ( charmap != NULL )
3533 {
3534 FT_CMap vcmap = FT_CMAP( charmap );
3535 FT_Memory memory = FT_FACE_MEMORY( face );
3536
3537
3538 if ( charcode > 0xFFFFFFFFUL )
3539 {
3540 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3541 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3542 }
3543
3544 result = vcmap->clazz->charvariant_list( vcmap, memory,
3545 (FT_UInt32)charcode );
3546 }
3547 }
3548 return result;
3549 }
3550
3551
3552 /* documentation is in freetype.h */
3553
3554 FT_EXPORT_DEF( FT_UInt32* )
3555 FT_Face_GetCharsOfVariant( FT_Face face,
3556 FT_ULong variantSelector )
3557 {
3558 FT_UInt32 *result = NULL;
3559
3560
3561 if ( face )
3562 {
3563 FT_CharMap charmap = find_variant_selector_charmap( face );
3564
3565
3566 if ( charmap != NULL )
3567 {
3568 FT_CMap vcmap = FT_CMAP( charmap );
3569 FT_Memory memory = FT_FACE_MEMORY( face );
3570
3571
3572 if ( variantSelector > 0xFFFFFFFFUL )
3573 {
3574 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3575 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3576 }
3577
3578 result = vcmap->clazz->variantchar_list( vcmap, memory,
3579 (FT_UInt32)variantSelector );
3580 }
3581 }
3582
3583 return result;
3584 }
3585
3586
3587 /* documentation is in freetype.h */
3588
3589 FT_EXPORT_DEF( FT_UInt )
3590 FT_Get_Name_Index( FT_Face face,
3591 FT_String* glyph_name )
3592 {
3593 FT_UInt result = 0;
3594
3595
3596 if ( face && FT_HAS_GLYPH_NAMES( face ) )
3597 {
3598 FT_Service_GlyphDict service;
3599
3600
3601 FT_FACE_LOOKUP_SERVICE( face,
3602 service,
3603 GLYPH_DICT );
3604
3605 if ( service && service->name_index )
3606 result = service->name_index( face, glyph_name );
3607 }
3608
3609 return result;
3610 }
3611
3612
3613 /* documentation is in freetype.h */
3614
3615 FT_EXPORT_DEF( FT_Error )
3616 FT_Get_Glyph_Name( FT_Face face,
3617 FT_UInt glyph_index,
3618 FT_Pointer buffer,
3619 FT_UInt buffer_max )
3620 {
3621 FT_Error error = FT_ERR( Invalid_Argument );
3622
3623
3624 /* clean up buffer */
3625 if ( buffer && buffer_max > 0 )
3626 ((FT_Byte*)buffer)[0] = 0;
3627
3628 if ( face &&
3629 (FT_Long)glyph_index <= face->num_glyphs &&
3630 FT_HAS_GLYPH_NAMES( face ) )
3631 {
3632 FT_Service_GlyphDict service;
3633
3634
3635 FT_FACE_LOOKUP_SERVICE( face,
3636 service,
3637 GLYPH_DICT );
3638
3639 if ( service && service->get_name )
3640 error = service->get_name( face, glyph_index, buffer, buffer_max );
3641 }
3642
3643 return error;
3644 }
3645
3646
3647 /* documentation is in freetype.h */
3648
3649 FT_EXPORT_DEF( const char* )
3650 FT_Get_Postscript_Name( FT_Face face )
3651 {
3652 const char* result = NULL;
3653
3654
3655 if ( !face )
3656 goto Exit;
3657
3658 if ( !result )
3659 {
3660 FT_Service_PsFontName service;
3661
3662
3663 FT_FACE_LOOKUP_SERVICE( face,
3664 service,
3665 POSTSCRIPT_FONT_NAME );
3666
3667 if ( service && service->get_ps_font_name )
3668 result = service->get_ps_font_name( face );
3669 }
3670
3671 Exit:
3672 return result;
3673 }
3674
3675
3676 /* documentation is in tttables.h */
3677
3678 FT_EXPORT_DEF( void* )
3679 FT_Get_Sfnt_Table( FT_Face face,
3680 FT_Sfnt_Tag tag )
3681 {
3682 void* table = 0;
3683 FT_Service_SFNT_Table service;
3684
3685
3686 if ( face && FT_IS_SFNT( face ) )
3687 {
3688 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3689 if ( service != NULL )
3690 table = service->get_table( face, tag );
3691 }
3692
3693 return table;
3694 }
3695
3696
3697 /* documentation is in tttables.h */
3698
3699 FT_EXPORT_DEF( FT_Error )
3700 FT_Load_Sfnt_Table( FT_Face face,
3701 FT_ULong tag,
3702 FT_Long offset,
3703 FT_Byte* buffer,
3704 FT_ULong* length )
3705 {
3706 FT_Service_SFNT_Table service;
3707
3708
3709 if ( !face || !FT_IS_SFNT( face ) )
3710 return FT_THROW( Invalid_Face_Handle );
3711
3712 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3713 if ( service == NULL )
3714 return FT_THROW( Unimplemented_Feature );
3715
3716 return service->load_table( face, tag, offset, buffer, length );
3717 }
3718
3719
3720 /* documentation is in tttables.h */
3721
3722 FT_EXPORT_DEF( FT_Error )
3723 FT_Sfnt_Table_Info( FT_Face face,
3724 FT_UInt table_index,
3725 FT_ULong *tag,
3726 FT_ULong *length )
3727 {
3728 FT_Service_SFNT_Table service;
3729 FT_ULong offset;
3730
3731
3732 if ( !face || !FT_IS_SFNT( face ) )
3733 return FT_THROW( Invalid_Face_Handle );
3734
3735 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3736 if ( service == NULL )
3737 return FT_THROW( Unimplemented_Feature );
3738
3739 return service->table_info( face, table_index, tag, &offset, length );
3740 }
3741
3742
3743 /* documentation is in tttables.h */
3744
3745 FT_EXPORT_DEF( FT_ULong )
3746 FT_Get_CMap_Language_ID( FT_CharMap charmap )
3747 {
3748 FT_Service_TTCMaps service;
3749 FT_Face face;
3750 TT_CMapInfo cmap_info;
3751
3752
3753 if ( !charmap || !charmap->face )
3754 return 0;
3755
3756 face = charmap->face;
3757 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3758 if ( service == NULL )
3759 return 0;
3760 if ( service->get_cmap_info( charmap, &cmap_info ))
3761 return 0;
3762
3763 return cmap_info.language;
3764 }
3765
3766
3767 /* documentation is in tttables.h */
3768
3769 FT_EXPORT_DEF( FT_Long )
3770 FT_Get_CMap_Format( FT_CharMap charmap )
3771 {
3772 FT_Service_TTCMaps service;
3773 FT_Face face;
3774 TT_CMapInfo cmap_info;
3775
3776
3777 if ( !charmap || !charmap->face )
3778 return -1;
3779
3780 face = charmap->face;
3781 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3782 if ( service == NULL )
3783 return -1;
3784 if ( service->get_cmap_info( charmap, &cmap_info ))
3785 return -1;
3786
3787 return cmap_info.format;
3788 }
3789
3790
3791 /* documentation is in ftsizes.h */
3792
3793 FT_EXPORT_DEF( FT_Error )
3794 FT_Activate_Size( FT_Size size )
3795 {
3796 FT_Face face;
3797
3798
3799 if ( size == NULL )
3800 return FT_THROW( Invalid_Argument );
3801
3802 face = size->face;
3803 if ( face == NULL || face->driver == NULL )
3804 return FT_THROW( Invalid_Argument );
3805
3806 /* we don't need anything more complex than that; all size objects */
3807 /* are already listed by the face */
3808 face->size = size;
3809
3810 return FT_Err_Ok;
3811 }
3812
3813
3814 /*************************************************************************/
3815 /*************************************************************************/
3816 /*************************************************************************/
3817 /**** ****/
3818 /**** ****/
3819 /**** R E N D E R E R S ****/
3820 /**** ****/
3821 /**** ****/
3822 /*************************************************************************/
3823 /*************************************************************************/
3824 /*************************************************************************/
3825
3826 /* lookup a renderer by glyph format in the library's list */
3827 FT_BASE_DEF( FT_Renderer )
3828 FT_Lookup_Renderer( FT_Library library,
3829 FT_Glyph_Format format,
3830 FT_ListNode* node )
3831 {
3832 FT_ListNode cur;
3833 FT_Renderer result = 0;
3834
3835
3836 if ( !library )
3837 goto Exit;
3838
3839 cur = library->renderers.head;
3840
3841 if ( node )
3842 {
3843 if ( *node )
3844 cur = (*node)->next;
3845 *node = 0;
3846 }
3847
3848 while ( cur )
3849 {
3850 FT_Renderer renderer = FT_RENDERER( cur->data );
3851
3852
3853 if ( renderer->glyph_format == format )
3854 {
3855 if ( node )
3856 *node = cur;
3857
3858 result = renderer;
3859 break;
3860 }
3861 cur = cur->next;
3862 }
3863
3864 Exit:
3865 return result;
3866 }
3867
3868
3869 static FT_Renderer
3870 ft_lookup_glyph_renderer( FT_GlyphSlot slot )
3871 {
3872 FT_Face face = slot->face;
3873 FT_Library library = FT_FACE_LIBRARY( face );
3874 FT_Renderer result = library->cur_renderer;
3875
3876
3877 if ( !result || result->glyph_format != slot->format )
3878 result = FT_Lookup_Renderer( library, slot->format, 0 );
3879
3880 return result;
3881 }
3882
3883
3884 static void
3885 ft_set_current_renderer( FT_Library library )
3886 {
3887 FT_Renderer renderer;
3888
3889
3890 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
3891 library->cur_renderer = renderer;
3892 }
3893
3894
3895 static FT_Error
3896 ft_add_renderer( FT_Module module )
3897 {
3898 FT_Library library = module->library;
3899 FT_Memory memory = library->memory;
3900 FT_Error error;
3901 FT_ListNode node = NULL;
3902
3903
3904 if ( FT_NEW( node ) )
3905 goto Exit;
3906
3907 {
3908 FT_Renderer render = FT_RENDERER( module );
3909 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
3910
3911
3912 render->clazz = clazz;
3913 render->glyph_format = clazz->glyph_format;
3914
3915 /* allocate raster object if needed */
3916 if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3917 clazz->raster_class->raster_new )
3918 {
3919 error = clazz->raster_class->raster_new( memory, &render->raster );
3920 if ( error )
3921 goto Fail;
3922
3923 render->raster_render = clazz->raster_class->raster_render;
3924 render->render = clazz->render_glyph;
3925 }
3926
3927 /* add to list */
3928 node->data = module;
3929 FT_List_Add( &library->renderers, node );
3930
3931 ft_set_current_renderer( library );
3932 }
3933
3934 Fail:
3935 if ( error )
3936 FT_FREE( node );
3937
3938 Exit:
3939 return error;
3940 }
3941
3942
3943 static void
3944 ft_remove_renderer( FT_Module module )
3945 {
3946 FT_Library library = module->library;
3947 FT_Memory memory = library->memory;
3948 FT_ListNode node;
3949
3950
3951 node = FT_List_Find( &library->renderers, module );
3952 if ( node )
3953 {
3954 FT_Renderer render = FT_RENDERER( module );
3955
3956
3957 /* release raster object, if any */
3958 if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3959 render->raster )
3960 render->clazz->raster_class->raster_done( render->raster );
3961
3962 /* remove from list */
3963 FT_List_Remove( &library->renderers, node );
3964 FT_FREE( node );
3965
3966 ft_set_current_renderer( library );
3967 }
3968 }
3969
3970
3971 /* documentation is in ftrender.h */
3972
3973 FT_EXPORT_DEF( FT_Renderer )
3974 FT_Get_Renderer( FT_Library library,
3975 FT_Glyph_Format format )
3976 {
3977 /* test for valid `library' delayed to FT_Lookup_Renderer() */
3978
3979 return FT_Lookup_Renderer( library, format, 0 );
3980 }
3981
3982
3983 /* documentation is in ftrender.h */
3984
3985 FT_EXPORT_DEF( FT_Error )
3986 FT_Set_Renderer( FT_Library library,
3987 FT_Renderer renderer,
3988 FT_UInt num_params,
3989 FT_Parameter* parameters )
3990 {
3991 FT_ListNode node;
3992 FT_Error error = FT_Err_Ok;
3993
3994
3995 if ( !library )
3996 return FT_THROW( Invalid_Library_Handle );
3997
3998 if ( !renderer )
3999 return FT_THROW( Invalid_Argument );
4000
4001 node = FT_List_Find( &library->renderers, renderer );
4002 if ( !node )
4003 {
4004 error = FT_THROW( Invalid_Argument );
4005 goto Exit;
4006 }
4007
4008 FT_List_Up( &library->renderers, node );
4009
4010 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
4011 library->cur_renderer = renderer;
4012
4013 if ( num_params > 0 )
4014 {
4015 FT_Renderer_SetModeFunc set_mode = renderer->clazz->set_mode;
4016
4017
4018 for ( ; num_params > 0; num_params-- )
4019 {
4020 error = set_mode( renderer, parameters->tag, parameters->data );
4021 if ( error )
4022 break;
4023 parameters++;
4024 }
4025 }
4026
4027 Exit:
4028 return error;
4029 }
4030
4031
4032 FT_BASE_DEF( FT_Error )
4033 FT_Render_Glyph_Internal( FT_Library library,
4034 FT_GlyphSlot slot,
4035 FT_Render_Mode render_mode )
4036 {
4037 FT_Error error = FT_Err_Ok;
4038 FT_Renderer renderer;
4039
4040
4041 /* if it is already a bitmap, no need to do anything */
4042 switch ( slot->format )
4043 {
4044 case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */
4045 break;
4046
4047 default:
4048 {
4049 FT_ListNode node = 0;
4050 FT_Bool update = 0;
4051
4052
4053 /* small shortcut for the very common case */
4054 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4055 {
4056 renderer = library->cur_renderer;
4057 node = library->renderers.head;
4058 }
4059 else
4060 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4061
4062 error = FT_ERR( Unimplemented_Feature );
4063 while ( renderer )
4064 {
4065 error = renderer->render( renderer, slot, render_mode, NULL );
4066 if ( !error ||
4067 FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
4068 break;
4069
4070 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
4071 /* is unsupported by the current renderer for this glyph image */
4072 /* format. */
4073
4074 /* now, look for another renderer that supports the same */
4075 /* format. */
4076 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4077 update = 1;
4078 }
4079
4080 /* if we changed the current renderer for the glyph image format */
4081 /* we need to select it as the next current one */
4082 if ( !error && update && renderer )
4083 FT_Set_Renderer( library, renderer, 0, 0 );
4084 }
4085 }
4086
4087 #ifdef FT_DEBUG_LEVEL_TRACE
4088
4089 #undef FT_COMPONENT
4090 #define FT_COMPONENT trace_bitmap
4091
4092 /* we convert to a single bitmap format for computing the checksum */
4093 {
4094 FT_Bitmap bitmap;
4095 FT_Error err;
4096
4097
4098 FT_Bitmap_New( &bitmap );
4099
4100 err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
4101 if ( !err )
4102 {
4103 MD5_CTX ctx;
4104 unsigned char md5[16];
4105 int i;
4106
4107
4108 MD5_Init( &ctx);
4109 MD5_Update( &ctx, bitmap.buffer, bitmap.rows * bitmap.pitch );
4110 MD5_Final( md5, &ctx );
4111
4112 FT_TRACE3(( "MD5 checksum for %dx%d bitmap:\n"
4113 " ",
4114 bitmap.rows, bitmap.pitch ));
4115 for ( i = 0; i < 16; i++ )
4116 FT_TRACE3(( "%02X", md5[i] ));
4117 FT_TRACE3(( "\n" ));
4118 }
4119
4120 FT_Bitmap_Done( library, &bitmap );
4121 }
4122
4123 #undef FT_COMPONENT
4124 #define FT_COMPONENT trace_objs
4125
4126 #endif /* FT_DEBUG_LEVEL_TRACE */
4127
4128 return error;
4129 }
4130
4131
4132 /* documentation is in freetype.h */
4133
4134 FT_EXPORT_DEF( FT_Error )
4135 FT_Render_Glyph( FT_GlyphSlot slot,
4136 FT_Render_Mode render_mode )
4137 {
4138 FT_Library library;
4139
4140
4141 if ( !slot || !slot->face )
4142 return FT_THROW( Invalid_Argument );
4143
4144 library = FT_FACE_LIBRARY( slot->face );
4145
4146 return FT_Render_Glyph_Internal( library, slot, render_mode );
4147 }
4148
4149
4150 /*************************************************************************/
4151 /*************************************************************************/
4152 /*************************************************************************/
4153 /**** ****/
4154 /**** ****/
4155 /**** M O D U L E S ****/
4156 /**** ****/
4157 /**** ****/
4158 /*************************************************************************/
4159 /*************************************************************************/
4160 /*************************************************************************/
4161
4162
4163 /*************************************************************************/
4164 /* */
4165 /* <Function> */
4166 /* Destroy_Module */
4167 /* */
4168 /* <Description> */
4169 /* Destroys a given module object. For drivers, this also destroys */
4170 /* all child faces. */
4171 /* */
4172 /* <InOut> */
4173 /* module :: A handle to the target driver object. */
4174 /* */
4175 /* <Note> */
4176 /* The driver _must_ be LOCKED! */
4177 /* */
4178 static void
4179 Destroy_Module( FT_Module module )
4180 {
4181 FT_Memory memory = module->memory;
4182 FT_Module_Class* clazz = module->clazz;
4183 FT_Library library = module->library;
4184
4185
4186 if ( library && library->auto_hinter == module )
4187 library->auto_hinter = 0;
4188
4189 /* if the module is a renderer */
4190 if ( FT_MODULE_IS_RENDERER( module ) )
4191 ft_remove_renderer( module );
4192
4193 /* if the module is a font driver, add some steps */
4194 if ( FT_MODULE_IS_DRIVER( module ) )
4195 Destroy_Driver( FT_DRIVER( module ) );
4196
4197 /* finalize the module object */
4198 if ( clazz->module_done )
4199 clazz->module_done( module );
4200
4201 /* discard it */
4202 FT_FREE( module );
4203 }
4204
4205
4206 /* documentation is in ftmodapi.h */
4207
4208 FT_EXPORT_DEF( FT_Error )
4209 FT_Add_Module( FT_Library library,
4210 const FT_Module_Class* clazz )
4211 {
4212 FT_Error error;
4213 FT_Memory memory;
4214 FT_Module module;
4215 FT_UInt nn;
4216
4217
4218 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4219 FREETYPE_MINOR )
4220
4221 if ( !library )
4222 return FT_THROW( Invalid_Library_Handle );
4223
4224 if ( !clazz )
4225 return FT_THROW( Invalid_Argument );
4226
4227 /* check freetype version */
4228 if ( clazz->module_requires > FREETYPE_VER_FIXED )
4229 return FT_THROW( Invalid_Version );
4230
4231 /* look for a module with the same name in the library's table */
4232 for ( nn = 0; nn < library->num_modules; nn++ )
4233 {
4234 module = library->modules[nn];
4235 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4236 {
4237 /* this installed module has the same name, compare their versions */
4238 if ( clazz->module_version <= module->clazz->module_version )
4239 return FT_THROW( Lower_Module_Version );
4240
4241 /* remove the module from our list, then exit the loop to replace */
4242 /* it by our new version.. */
4243 FT_Remove_Module( library, module );
4244 break;
4245 }
4246 }
4247
4248 memory = library->memory;
4249 error = FT_Err_Ok;
4250
4251 if ( library->num_modules >= FT_MAX_MODULES )
4252 {
4253 error = FT_THROW( Too_Many_Drivers );
4254 goto Exit;
4255 }
4256
4257 /* allocate module object */
4258 if ( FT_ALLOC( module, clazz->module_size ) )
4259 goto Exit;
4260
4261 /* base initialization */
4262 module->library = library;
4263 module->memory = memory;
4264 module->clazz = (FT_Module_Class*)clazz;
4265
4266 /* check whether the module is a renderer - this must be performed */
4267 /* before the normal module initialization */
4268 if ( FT_MODULE_IS_RENDERER( module ) )
4269 {
4270 /* add to the renderers list */
4271 error = ft_add_renderer( module );
4272 if ( error )
4273 goto Fail;
4274 }
4275
4276 /* is the module a auto-hinter? */
4277 if ( FT_MODULE_IS_HINTER( module ) )
4278 library->auto_hinter = module;
4279
4280 /* if the module is a font driver */
4281 if ( FT_MODULE_IS_DRIVER( module ) )
4282 {
4283 /* allocate glyph loader if needed */
4284 FT_Driver driver = FT_DRIVER( module );
4285
4286
4287 driver->clazz = (FT_Driver_Class)module->clazz;
4288 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4289 {
4290 error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
4291 if ( error )
4292 goto Fail;
4293 }
4294 }
4295
4296 if ( clazz->module_init )
4297 {
4298 error = clazz->module_init( module );
4299 if ( error )
4300 goto Fail;
4301 }
4302
4303 /* add module to the library's table */
4304 library->modules[library->num_modules++] = module;
4305
4306 Exit:
4307 return error;
4308
4309 Fail:
4310 if ( FT_MODULE_IS_DRIVER( module ) )
4311 {
4312 FT_Driver driver = FT_DRIVER( module );
4313
4314
4315 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4316 FT_GlyphLoader_Done( driver->glyph_loader );
4317 }
4318
4319 if ( FT_MODULE_IS_RENDERER( module ) )
4320 {
4321 FT_Renderer renderer = FT_RENDERER( module );
4322
4323
4324 if ( renderer->clazz &&
4325 renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
4326 renderer->raster )
4327 renderer->clazz->raster_class->raster_done( renderer->raster );
4328 }
4329
4330 FT_FREE( module );
4331 goto Exit;
4332 }
4333
4334
4335 /* documentation is in ftmodapi.h */
4336
4337 FT_EXPORT_DEF( FT_Module )
4338 FT_Get_Module( FT_Library library,
4339 const char* module_name )
4340 {
4341 FT_Module result = 0;
4342 FT_Module* cur;
4343 FT_Module* limit;
4344
4345
4346 if ( !library || !module_name )
4347 return result;
4348
4349 cur = library->modules;
4350 limit = cur + library->num_modules;
4351
4352 for ( ; cur < limit; cur++ )
4353 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
4354 {
4355 result = cur[0];
4356 break;
4357 }
4358
4359 return result;
4360 }
4361
4362
4363 /* documentation is in ftobjs.h */
4364
4365 FT_BASE_DEF( const void* )
4366 FT_Get_Module_Interface( FT_Library library,
4367 const char* mod_name )
4368 {
4369 FT_Module module;
4370
4371
4372 /* test for valid `library' delayed to FT_Get_Module() */
4373
4374 module = FT_Get_Module( library, mod_name );
4375
4376 return module ? module->clazz->module_interface : 0;
4377 }
4378
4379
4380 FT_BASE_DEF( FT_Pointer )
4381 ft_module_get_service( FT_Module module,
4382 const char* service_id )
4383 {
4384 FT_Pointer result = NULL;
4385
4386
4387 if ( module )
4388 {
4389 FT_ASSERT( module->clazz && module->clazz->get_interface );
4390
4391 /* first, look for the service in the module */
4392 if ( module->clazz->get_interface )
4393 result = module->clazz->get_interface( module, service_id );
4394
4395 if ( result == NULL )
4396 {
4397 /* we didn't find it, look in all other modules then */
4398 FT_Library library = module->library;
4399 FT_Module* cur = library->modules;
4400 FT_Module* limit = cur + library->num_modules;
4401
4402
4403 for ( ; cur < limit; cur++ )
4404 {
4405 if ( cur[0] != module )
4406 {
4407 FT_ASSERT( cur[0]->clazz );
4408
4409 if ( cur[0]->clazz->get_interface )
4410 {
4411 result = cur[0]->clazz->get_interface( cur[0], service_id );
4412 if ( result != NULL )
4413 break;
4414 }
4415 }
4416 }
4417 }
4418 }
4419
4420 return result;
4421 }
4422
4423
4424 /* documentation is in ftmodapi.h */
4425
4426 FT_EXPORT_DEF( FT_Error )
4427 FT_Remove_Module( FT_Library library,
4428 FT_Module module )
4429 {
4430 /* try to find the module from the table, then remove it from there */
4431
4432 if ( !library )
4433 return FT_THROW( Invalid_Library_Handle );
4434
4435 if ( module )
4436 {
4437 FT_Module* cur = library->modules;
4438 FT_Module* limit = cur + library->num_modules;
4439
4440
4441 for ( ; cur < limit; cur++ )
4442 {
4443 if ( cur[0] == module )
4444 {
4445 /* remove it from the table */
4446 library->num_modules--;
4447 limit--;
4448 while ( cur < limit )
4449 {
4450 cur[0] = cur[1];
4451 cur++;
4452 }
4453 limit[0] = 0;
4454
4455 /* destroy the module */
4456 Destroy_Module( module );
4457
4458 return FT_Err_Ok;
4459 }
4460 }
4461 }
4462 return FT_THROW( Invalid_Driver_Handle );
4463 }
4464
4465
4466 FT_Error
4467 ft_property_do( FT_Library library,
4468 const FT_String* module_name,
4469 const FT_String* property_name,
4470 void* value,
4471 FT_Bool set )
4472 {
4473 FT_Module* cur;
4474 FT_Module* limit;
4475 FT_Module_Interface interface;
4476
4477 FT_Service_Properties service;
4478
4479 #ifdef FT_DEBUG_LEVEL_ERROR
4480 const FT_String* set_name = "FT_Property_Set";
4481 const FT_String* get_name = "FT_Property_Get";
4482 const FT_String* func_name = set ? set_name : get_name;
4483 #endif
4484
4485 FT_Bool missing_func;
4486
4487
4488 if ( !library )
4489 return FT_THROW( Invalid_Library_Handle );
4490
4491 if ( !module_name || !property_name || !value )
4492 return FT_THROW( Invalid_Argument );
4493
4494 cur = library->modules;
4495 limit = cur + library->num_modules;
4496
4497 /* search module */
4498 for ( ; cur < limit; cur++ )
4499 if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) )
4500 break;
4501
4502 if ( cur == limit )
4503 {
4504 FT_ERROR(( "%s: can't find module `%s'\n",
4505 func_name, module_name ));
4506 return FT_THROW( Missing_Module );
4507 }
4508
4509 /* check whether we have a service interface */
4510 if ( !cur[0]->clazz->get_interface )
4511 {
4512 FT_ERROR(( "%s: module `%s' doesn't support properties\n",
4513 func_name, module_name ));
4514 return FT_THROW( Unimplemented_Feature );
4515 }
4516
4517 /* search property service */
4518 interface = cur[0]->clazz->get_interface( cur[0],
4519 FT_SERVICE_ID_PROPERTIES );
4520 if ( !interface )
4521 {
4522 FT_ERROR(( "%s: module `%s' doesn't support properties\n",
4523 func_name, module_name ));
4524 return FT_THROW( Unimplemented_Feature );
4525 }
4526
4527 service = (FT_Service_Properties)interface;
4528
4529 if ( set )
4530 missing_func = (FT_Bool)( !service->set_property );
4531 else
4532 missing_func = (FT_Bool)( !service->get_property );
4533
4534 if ( missing_func )
4535 {
4536 FT_ERROR(( "%s: property service of module `%s' is broken\n",
4537 func_name, module_name ));
4538 return FT_THROW( Unimplemented_Feature );
4539 }
4540
4541 return set ? service->set_property( cur[0], property_name, value )
4542 : service->get_property( cur[0], property_name, value );
4543 }
4544
4545
4546 /* documentation is in ftmodapi.h */
4547
4548 FT_EXPORT_DEF( FT_Error )
4549 FT_Property_Set( FT_Library library,
4550 const FT_String* module_name,
4551 const FT_String* property_name,
4552 const void* value )
4553 {
4554 return ft_property_do( library,
4555 module_name,
4556 property_name,
4557 (void*)value,
4558 TRUE );
4559 }
4560
4561
4562 /* documentation is in ftmodapi.h */
4563
4564 FT_EXPORT_DEF( FT_Error )
4565 FT_Property_Get( FT_Library library,
4566 const FT_String* module_name,
4567 const FT_String* property_name,
4568 void* value )
4569 {
4570 return ft_property_do( library,
4571 module_name,
4572 property_name,
4573 value,
4574 FALSE );
4575 }
4576
4577
4578 /*************************************************************************/
4579 /*************************************************************************/
4580 /*************************************************************************/
4581 /**** ****/
4582 /**** ****/
4583 /**** L I B R A R Y ****/
4584 /**** ****/
4585 /**** ****/
4586 /*************************************************************************/
4587 /*************************************************************************/
4588 /*************************************************************************/
4589
4590
4591 /* documentation is in ftmodapi.h */
4592
4593 FT_EXPORT_DEF( FT_Error )
4594 FT_Reference_Library( FT_Library library )
4595 {
4596 library->refcount++;
4597
4598 return FT_Err_Ok;
4599 }
4600
4601
4602 /* documentation is in ftmodapi.h */
4603
4604 FT_EXPORT_DEF( FT_Error )
4605 FT_New_Library( FT_Memory memory,
4606 FT_Library *alibrary )
4607 {
4608 FT_Library library = NULL;
4609 FT_Error error;
4610
4611
4612 if ( !memory )
4613 return FT_THROW( Invalid_Argument );
4614
4615 #ifdef FT_DEBUG_LEVEL_ERROR
4616 /* init debugging support */
4617 ft_debug_init();
4618 #endif
4619
4620 /* first of all, allocate the library object */
4621 if ( FT_NEW( library ) )
4622 return error;
4623
4624 library->memory = memory;
4625
4626 #ifdef FT_CONFIG_OPTION_PIC
4627 /* initialize position independent code containers */
4628 error = ft_pic_container_init( library );
4629 if ( error )
4630 goto Fail;
4631 #endif
4632
4633 /* allocate the render pool */
4634 library->raster_pool_size = FT_RENDER_POOL_SIZE;
4635 #if FT_RENDER_POOL_SIZE > 0
4636 if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
4637 goto Fail;
4638 #endif
4639
4640 library->version_major = FREETYPE_MAJOR;
4641 library->version_minor = FREETYPE_MINOR;
4642 library->version_patch = FREETYPE_PATCH;
4643
4644 library->refcount = 1;
4645
4646 /* That's ok now */
4647 *alibrary = library;
4648
4649 return FT_Err_Ok;
4650
4651 Fail:
4652 #ifdef FT_CONFIG_OPTION_PIC
4653 ft_pic_container_destroy( library );
4654 #endif
4655 FT_FREE( library );
4656 return error;
4657 }
4658
4659
4660 /* documentation is in freetype.h */
4661
4662 FT_EXPORT_DEF( void )
4663 FT_Library_Version( FT_Library library,
4664 FT_Int *amajor,
4665 FT_Int *aminor,
4666 FT_Int *apatch )
4667 {
4668 FT_Int major = 0;
4669 FT_Int minor = 0;
4670 FT_Int patch = 0;
4671
4672
4673 if ( library )
4674 {
4675 major = library->version_major;
4676 minor = library->version_minor;
4677 patch = library->version_patch;
4678 }
4679
4680 if ( amajor )
4681 *amajor = major;
4682
4683 if ( aminor )
4684 *aminor = minor;
4685
4686 if ( apatch )
4687 *apatch = patch;
4688 }
4689
4690
4691 /* documentation is in ftmodapi.h */
4692
4693 FT_EXPORT_DEF( FT_Error )
4694 FT_Done_Library( FT_Library library )
4695 {
4696 FT_Memory memory;
4697
4698
4699 if ( !library )
4700 return FT_THROW( Invalid_Library_Handle );
4701
4702 library->refcount--;
4703 if ( library->refcount > 0 )
4704 goto Exit;
4705
4706 memory = library->memory;
4707
4708 /*
4709 * Close all faces in the library. If we don't do this, we can have
4710 * some subtle memory leaks.
4711 *
4712 * Example:
4713 *
4714 * - the cff font driver uses the pshinter module in cff_size_done
4715 * - if the pshinter module is destroyed before the cff font driver,
4716 * opened FT_Face objects managed by the driver are not properly
4717 * destroyed, resulting in a memory leak
4718 *
4719 * Some faces are dependent on other faces, like Type42 faces that
4720 * depend on TrueType faces synthesized internally.
4721 *
4722 * The order of drivers should be specified in driver_name[].
4723 */
4724 {
4725 FT_UInt m, n;
4726 const char* driver_name[] = { "type42", NULL };
4727
4728
4729 for ( m = 0;
4730 m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
4731 m++ )
4732 {
4733 for ( n = 0; n < library->num_modules; n++ )
4734 {
4735 FT_Module module = library->modules[n];
4736 const char* module_name = module->clazz->module_name;
4737 FT_List faces;
4738
4739
4740 if ( driver_name[m] &&
4741 ft_strcmp( module_name, driver_name[m] ) != 0 )
4742 continue;
4743
4744 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
4745 continue;
4746
4747 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
4748
4749 faces = &FT_DRIVER( module )->faces_list;
4750 while ( faces->head )
4751 {
4752 FT_Done_Face( FT_FACE( faces->head->data ) );
4753 if ( faces->head )
4754 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
4755 }
4756 }
4757 }
4758 }
4759
4760 /* Close all other modules in the library */
4761 #if 1
4762 /* XXX Modules are removed in the reversed order so that */
4763 /* type42 module is removed before truetype module. This */
4764 /* avoids double free in some occasions. It is a hack. */
4765 while ( library->num_modules > 0 )
4766 FT_Remove_Module( library,
4767 library->modules[library->num_modules - 1] );
4768 #else
4769 {
4770 FT_UInt n;
4771
4772
4773 for ( n = 0; n < library->num_modules; n++ )
4774 {
4775 FT_Module module = library->modules[n];
4776
4777
4778 if ( module )
4779 {
4780 Destroy_Module( module );
4781 library->modules[n] = 0;
4782 }
4783 }
4784 }
4785 #endif
4786
4787 /* Destroy raster objects */
4788 FT_FREE( library->raster_pool );
4789 library->raster_pool_size = 0;
4790
4791 #ifdef FT_CONFIG_OPTION_PIC
4792 /* Destroy pic container contents */
4793 ft_pic_container_destroy( library );
4794 #endif
4795
4796 FT_FREE( library );
4797
4798 Exit:
4799 return FT_Err_Ok;
4800 }
4801
4802
4803 /* documentation is in ftmodapi.h */
4804
4805 FT_EXPORT_DEF( void )
4806 FT_Set_Debug_Hook( FT_Library library,
4807 FT_UInt hook_index,
4808 FT_DebugHook_Func debug_hook )
4809 {
4810 if ( library && debug_hook &&
4811 hook_index <
4812 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
4813 library->debug_hooks[hook_index] = debug_hook;
4814 }
4815
4816
4817 /* documentation is in ftmodapi.h */
4818
4819 FT_EXPORT_DEF( FT_TrueTypeEngineType )
4820 FT_Get_TrueType_Engine_Type( FT_Library library )
4821 {
4822 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE;
4823
4824
4825 if ( library )
4826 {
4827 FT_Module module = FT_Get_Module( library, "truetype" );
4828
4829
4830 if ( module )
4831 {
4832 FT_Service_TrueTypeEngine service;
4833
4834
4835 service = (FT_Service_TrueTypeEngine)
4836 ft_module_get_service( module,
4837 FT_SERVICE_ID_TRUETYPE_ENGINE );
4838 if ( service )
4839 result = service->engine_type;
4840 }
4841 }
4842
4843 return result;
4844 }
4845
4846
4847 /* documentation is in freetype.h */
4848
4849 FT_EXPORT_DEF( FT_Error )
4850 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph,
4851 FT_UInt sub_index,
4852 FT_Int *p_index,
4853 FT_UInt *p_flags,
4854 FT_Int *p_arg1,
4855 FT_Int *p_arg2,
4856 FT_Matrix *p_transform )
4857 {
4858 FT_Error error = FT_ERR( Invalid_Argument );
4859
4860
4861 if ( glyph &&
4862 glyph->subglyphs &&
4863 glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
4864 sub_index < glyph->num_subglyphs )
4865 {
4866 FT_SubGlyph subg = glyph->subglyphs + sub_index;
4867
4868
4869 *p_index = subg->index;
4870 *p_flags = subg->flags;
4871 *p_arg1 = subg->arg1;
4872 *p_arg2 = subg->arg2;
4873 *p_transform = subg->transform;
4874 }
4875
4876 return error;
4877 }
4878
4879
4880 /* END */