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