[FREETYPE]
[reactos.git] / reactos / lib / 3rdparty / freetype / src / truetype / ttobjs.c
1 /***************************************************************************/
2 /* */
3 /* ttobjs.c */
4 /* */
5 /* Objects manager (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
8 /* 2010 by */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
16 /* */
17 /***************************************************************************/
18
19
20 #include <ft2build.h>
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_TAGS_H
24 #include FT_INTERNAL_SFNT_H
25
26 #include "ttgload.h"
27 #include "ttpload.h"
28
29 #include "tterrors.h"
30
31 #ifdef TT_USE_BYTECODE_INTERPRETER
32 #include "ttinterp.h"
33 #endif
34
35 #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
36 #include FT_TRUETYPE_UNPATENTED_H
37 #endif
38
39 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
40 #include "ttgxvar.h"
41 #endif
42
43 /*************************************************************************/
44 /* */
45 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
46 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
47 /* messages during execution. */
48 /* */
49 #undef FT_COMPONENT
50 #define FT_COMPONENT trace_ttobjs
51
52
53 #ifdef TT_USE_BYTECODE_INTERPRETER
54
55 /*************************************************************************/
56 /* */
57 /* GLYPH ZONE FUNCTIONS */
58 /* */
59 /*************************************************************************/
60
61
62 /*************************************************************************/
63 /* */
64 /* <Function> */
65 /* tt_glyphzone_done */
66 /* */
67 /* <Description> */
68 /* Deallocate a glyph zone. */
69 /* */
70 /* <Input> */
71 /* zone :: A pointer to the target glyph zone. */
72 /* */
73 FT_LOCAL_DEF( void )
74 tt_glyphzone_done( TT_GlyphZone zone )
75 {
76 FT_Memory memory = zone->memory;
77
78
79 if ( memory )
80 {
81 FT_FREE( zone->contours );
82 FT_FREE( zone->tags );
83 FT_FREE( zone->cur );
84 FT_FREE( zone->org );
85 FT_FREE( zone->orus );
86
87 zone->max_points = zone->n_points = 0;
88 zone->max_contours = zone->n_contours = 0;
89 zone->memory = NULL;
90 }
91 }
92
93
94 /*************************************************************************/
95 /* */
96 /* <Function> */
97 /* tt_glyphzone_new */
98 /* */
99 /* <Description> */
100 /* Allocate a new glyph zone. */
101 /* */
102 /* <Input> */
103 /* memory :: A handle to the current memory object. */
104 /* */
105 /* maxPoints :: The capacity of glyph zone in points. */
106 /* */
107 /* maxContours :: The capacity of glyph zone in contours. */
108 /* */
109 /* <Output> */
110 /* zone :: A pointer to the target glyph zone record. */
111 /* */
112 /* <Return> */
113 /* FreeType error code. 0 means success. */
114 /* */
115 FT_LOCAL_DEF( FT_Error )
116 tt_glyphzone_new( FT_Memory memory,
117 FT_UShort maxPoints,
118 FT_Short maxContours,
119 TT_GlyphZone zone )
120 {
121 FT_Error error;
122
123
124 FT_MEM_ZERO( zone, sizeof ( *zone ) );
125 zone->memory = memory;
126
127 if ( FT_NEW_ARRAY( zone->org, maxPoints ) ||
128 FT_NEW_ARRAY( zone->cur, maxPoints ) ||
129 FT_NEW_ARRAY( zone->orus, maxPoints ) ||
130 FT_NEW_ARRAY( zone->tags, maxPoints ) ||
131 FT_NEW_ARRAY( zone->contours, maxContours ) )
132 {
133 tt_glyphzone_done( zone );
134 }
135 else
136 {
137 zone->max_points = maxPoints;
138 zone->max_contours = maxContours;
139 }
140
141 return error;
142 }
143 #endif /* TT_USE_BYTECODE_INTERPRETER */
144
145
146 /* Compare the face with a list of well-known `tricky' fonts. */
147 /* This list shall be expanded as we find more of them. */
148
149 static FT_Bool
150 tt_check_trickyness_family( FT_String* name )
151 {
152
153 #define TRICK_NAMES_MAX_CHARACTERS 16
154 #define TRICK_NAMES_COUNT 8
155
156 static const char trick_names[TRICK_NAMES_COUNT]
157 [TRICK_NAMES_MAX_CHARACTERS + 1] =
158 {
159 "DFKaiSho-SB", /* dfkaisb.ttf */
160 "DFKaiShu",
161 "DFKai-SB", /* kaiu.ttf */
162 "HuaTianKaiTi?", /* htkt2.ttf */
163 "HuaTianSongTi?", /* htst3.ttf */
164 "MingLiU", /* mingliu.ttf & mingliu.ttc */
165 "PMingLiU", /* mingliu.ttc */
166 "MingLi43", /* mingli.ttf */
167 };
168
169 int nn;
170
171
172 for ( nn = 0; nn < TRICK_NAMES_COUNT; nn++ )
173 if ( ft_strstr( name, trick_names[nn] ) )
174 return TRUE;
175
176 return FALSE;
177 }
178
179
180 /* XXX: This function should be in the `sfnt' module. */
181
182 /* Some PDF generators clear the checksums in the TrueType header table. */
183 /* For example, Quartz ContextPDF clears all entries, or Bullzip PDF */
184 /* Printer clears the entries for subsetted subtables. We thus have to */
185 /* recalculate the checksums where necessary. */
186
187 static FT_UInt32
188 tt_synth_sfnt_checksum( FT_Stream stream,
189 FT_ULong length )
190 {
191 FT_Error error;
192 FT_UInt32 checksum = 0;
193 int i;
194
195
196 if ( FT_FRAME_ENTER( length ) )
197 return 0;
198
199 for ( ; length > 3; length -= 4 )
200 checksum += (FT_UInt32)FT_GET_ULONG();
201
202 for ( i = 3; length > 0; length --, i-- )
203 checksum += (FT_UInt32)( FT_GET_BYTE() << ( i * 8 ) );
204
205 FT_FRAME_EXIT();
206
207 return checksum;
208 }
209
210
211 /* XXX: This function should be in the `sfnt' module. */
212
213 static FT_ULong
214 tt_get_sfnt_checksum( TT_Face face,
215 FT_UShort i )
216 {
217 if ( face->dir_tables[i].CheckSum )
218 return face->dir_tables[i].CheckSum;
219
220 else if ( !face->goto_table )
221 return 0;
222
223 else if ( !face->goto_table( face,
224 face->dir_tables[i].Tag,
225 face->root.stream,
226 NULL ) )
227 return 0;
228
229 return (FT_ULong)tt_synth_sfnt_checksum( face->root.stream,
230 face->dir_tables[i].Length );
231 }
232
233
234 typedef struct tt_sfnt_id_rec_
235 {
236 FT_ULong CheckSum;
237 FT_ULong Length;
238
239 } tt_sfnt_id_rec;
240
241
242 static FT_Bool
243 tt_check_trickyness_sfnt_ids( TT_Face face )
244 {
245 #define TRICK_SFNT_IDS_PER_FACE 3
246 #define TRICK_SFNT_IDS_NUM_FACES 5
247
248 static const tt_sfnt_id_rec sfnt_id[TRICK_SFNT_IDS_NUM_FACES]
249 [TRICK_SFNT_IDS_PER_FACE] = {
250
251 #define TRICK_SFNT_ID_cvt 0
252 #define TRICK_SFNT_ID_fpgm 1
253 #define TRICK_SFNT_ID_prep 2
254
255 { /* MingLiU 1995 */
256 { 0x05bcf058, 0x000002e4 }, /* cvt */
257 { 0x28233bf1, 0x000087c4 }, /* fpgm */
258 { 0xa344a1ea, 0x000001e1 } /* prep */
259 },
260 { /* MingLiU 1996- */
261 { 0x05bcf058, 0x000002e4 }, /* cvt */
262 { 0x28233bf1, 0x000087c4 }, /* fpgm */
263 { 0xa344a1eb, 0x000001e1 } /* prep */
264 },
265 { /* DFKaiShu */
266 { 0x11e5ead4, 0x00000350 }, /* cvt */
267 { 0x5a30ca3b, 0x00009063 }, /* fpgm */
268 { 0x13a42602, 0x0000007e } /* prep */
269 },
270 { /* HuaTianKaiTi */
271 { 0xfffbfffc, 0x00000008 }, /* cvt */
272 { 0x9c9e48b8, 0x0000bea2 }, /* fpgm */
273 { 0x70020112, 0x00000008 } /* prep */
274 },
275 { /* HuaTianSongTi */
276 { 0xfffbfffc, 0x00000008 }, /* cvt */
277 { 0x0a5a0483, 0x00017c39 }, /* fpgm */
278 { 0x70020112, 0x00000008 } /* prep */
279 }
280 };
281
282 FT_ULong checksum;
283 int num_matched_ids[TRICK_SFNT_IDS_NUM_FACES];
284 int i, j, k;
285
286
287 FT_MEM_SET( num_matched_ids, 0,
288 sizeof( int ) * TRICK_SFNT_IDS_NUM_FACES );
289
290 for ( i = 0; i < face->num_tables; i++ )
291 {
292 checksum = 0;
293
294 switch( face->dir_tables[i].Tag )
295 {
296 case TTAG_cvt:
297 k = TRICK_SFNT_ID_cvt;
298 break;
299
300 case TTAG_fpgm:
301 k = TRICK_SFNT_ID_fpgm;
302 break;
303
304 case TTAG_prep:
305 k = TRICK_SFNT_ID_prep;
306 break;
307
308 default:
309 continue;
310 }
311
312 for ( j = 0; j < TRICK_SFNT_IDS_NUM_FACES; j++ )
313 if ( face->dir_tables[i].Length == sfnt_id[j][k].Length )
314 {
315 if ( !checksum )
316 checksum = tt_get_sfnt_checksum( face, i );
317
318 if ( sfnt_id[j][k].CheckSum == checksum )
319 num_matched_ids[j]++;
320
321 if ( num_matched_ids[j] == TRICK_SFNT_IDS_PER_FACE )
322 return TRUE;
323 }
324 }
325
326 return FALSE;
327 }
328
329
330 static FT_Bool
331 tt_check_trickyness( FT_Face face )
332 {
333 if ( !face )
334 return FALSE;
335
336 /* First, check the face name. */
337 if ( face->family_name )
338 {
339 if ( tt_check_trickyness_family( face->family_name ) )
340 return TRUE;
341 else
342 return FALSE;
343 }
344
345 /* Type42 fonts may lack `name' tables, we thus try to identify */
346 /* tricky fonts by checking the checksums of Type42-persistent */
347 /* sfnt tables (`cvt', `fpgm', and `prep'). */
348 if ( tt_check_trickyness_sfnt_ids( (TT_Face)face ) )
349 return TRUE;
350
351 return FALSE;
352 }
353
354
355 /*************************************************************************/
356 /* */
357 /* <Function> */
358 /* tt_face_init */
359 /* */
360 /* <Description> */
361 /* Initialize a given TrueType face object. */
362 /* */
363 /* <Input> */
364 /* stream :: The source font stream. */
365 /* */
366 /* face_index :: The index of the font face in the resource. */
367 /* */
368 /* num_params :: Number of additional generic parameters. Ignored. */
369 /* */
370 /* params :: Additional generic parameters. Ignored. */
371 /* */
372 /* <InOut> */
373 /* face :: The newly built face object. */
374 /* */
375 /* <Return> */
376 /* FreeType error code. 0 means success. */
377 /* */
378 FT_LOCAL_DEF( FT_Error )
379 tt_face_init( FT_Stream stream,
380 FT_Face ttface, /* TT_Face */
381 FT_Int face_index,
382 FT_Int num_params,
383 FT_Parameter* params )
384 {
385 FT_Error error;
386 FT_Library library;
387 SFNT_Service sfnt;
388 TT_Face face = (TT_Face)ttface;
389
390
391 library = ttface->driver->root.library;
392 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );
393 if ( !sfnt )
394 goto Bad_Format;
395
396 /* create input stream from resource */
397 if ( FT_STREAM_SEEK( 0 ) )
398 goto Exit;
399
400 /* check that we have a valid TrueType file */
401 error = sfnt->init_face( stream, face, face_index, num_params, params );
402 if ( error )
403 goto Exit;
404
405 /* We must also be able to accept Mac/GX fonts, as well as OT ones. */
406 /* The 0x00020000 tag is completely undocumented; some fonts from */
407 /* Arphic made for Chinese Windows 3.1 have this. */
408 if ( face->format_tag != 0x00010000L && /* MS fonts */
409 face->format_tag != 0x00020000L && /* CJK fonts for Win 3.1 */
410 face->format_tag != TTAG_true ) /* Mac fonts */
411 {
412 FT_TRACE2(( "[not a valid TTF font]\n" ));
413 goto Bad_Format;
414 }
415
416 #ifdef TT_USE_BYTECODE_INTERPRETER
417 ttface->face_flags |= FT_FACE_FLAG_HINTER;
418 #endif
419
420 /* If we are performing a simple font format check, exit immediately. */
421 if ( face_index < 0 )
422 return TT_Err_Ok;
423
424 /* Load font directory */
425 error = sfnt->load_face( stream, face, face_index, num_params, params );
426 if ( error )
427 goto Exit;
428
429 if ( tt_check_trickyness( ttface ) )
430 ttface->face_flags |= FT_FACE_FLAG_TRICKY;
431
432 error = tt_face_load_hdmx( face, stream );
433 if ( error )
434 goto Exit;
435
436 if ( FT_IS_SCALABLE( ttface ) )
437 {
438
439 #ifdef FT_CONFIG_OPTION_INCREMENTAL
440
441 if ( !ttface->internal->incremental_interface )
442 error = tt_face_load_loca( face, stream );
443 if ( !error )
444 error = tt_face_load_cvt( face, stream );
445 if ( !error )
446 error = tt_face_load_fpgm( face, stream );
447 if ( !error )
448 error = tt_face_load_prep( face, stream );
449
450 #else
451
452 if ( !error )
453 error = tt_face_load_loca( face, stream );
454 if ( !error )
455 error = tt_face_load_cvt( face, stream );
456 if ( !error )
457 error = tt_face_load_fpgm( face, stream );
458 if ( !error )
459 error = tt_face_load_prep( face, stream );
460
461 #endif
462
463 }
464
465 #if defined( TT_CONFIG_OPTION_UNPATENTED_HINTING ) && \
466 !defined( TT_CONFIG_OPTION_BYTECODE_INTERPRETER )
467
468 {
469 FT_Bool unpatented_hinting;
470 int i;
471
472
473 /* Determine whether unpatented hinting is to be used for this face. */
474 unpatented_hinting = FT_BOOL
475 ( library->debug_hooks[FT_DEBUG_HOOK_UNPATENTED_HINTING] != NULL );
476
477 for ( i = 0; i < num_params && !face->unpatented_hinting; i++ )
478 if ( params[i].tag == FT_PARAM_TAG_UNPATENTED_HINTING )
479 unpatented_hinting = TRUE;
480
481 if ( !unpatented_hinting )
482 ttface->internal->ignore_unpatented_hinter = TRUE;
483 }
484
485 #endif /* TT_CONFIG_OPTION_UNPATENTED_HINTING &&
486 !TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
487
488 /* initialize standard glyph loading routines */
489 TT_Init_Glyph_Loading( face );
490
491 Exit:
492 return error;
493
494 Bad_Format:
495 error = TT_Err_Unknown_File_Format;
496 goto Exit;
497 }
498
499
500 /*************************************************************************/
501 /* */
502 /* <Function> */
503 /* tt_face_done */
504 /* */
505 /* <Description> */
506 /* Finalize a given face object. */
507 /* */
508 /* <Input> */
509 /* face :: A pointer to the face object to destroy. */
510 /* */
511 FT_LOCAL_DEF( void )
512 tt_face_done( FT_Face ttface ) /* TT_Face */
513 {
514 TT_Face face = (TT_Face)ttface;
515 FT_Memory memory;
516 FT_Stream stream;
517 SFNT_Service sfnt;
518
519
520 if ( !face )
521 return;
522
523 memory = ttface->memory;
524 stream = ttface->stream;
525 sfnt = (SFNT_Service)face->sfnt;
526
527 /* for `extended TrueType formats' (i.e. compressed versions) */
528 if ( face->extra.finalizer )
529 face->extra.finalizer( face->extra.data );
530
531 if ( sfnt )
532 sfnt->done_face( face );
533
534 /* freeing the locations table */
535 tt_face_done_loca( face );
536
537 tt_face_free_hdmx( face );
538
539 /* freeing the CVT */
540 FT_FREE( face->cvt );
541 face->cvt_size = 0;
542
543 /* freeing the programs */
544 FT_FRAME_RELEASE( face->font_program );
545 FT_FRAME_RELEASE( face->cvt_program );
546 face->font_program_size = 0;
547 face->cvt_program_size = 0;
548
549 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
550 tt_done_blend( memory, face->blend );
551 face->blend = NULL;
552 #endif
553 }
554
555
556 /*************************************************************************/
557 /* */
558 /* SIZE FUNCTIONS */
559 /* */
560 /*************************************************************************/
561
562 #ifdef TT_USE_BYTECODE_INTERPRETER
563
564 /*************************************************************************/
565 /* */
566 /* <Function> */
567 /* tt_size_run_fpgm */
568 /* */
569 /* <Description> */
570 /* Run the font program. */
571 /* */
572 /* <Input> */
573 /* size :: A handle to the size object. */
574 /* */
575 /* <Return> */
576 /* FreeType error code. 0 means success. */
577 /* */
578 FT_LOCAL_DEF( FT_Error )
579 tt_size_run_fpgm( TT_Size size )
580 {
581 TT_Face face = (TT_Face)size->root.face;
582 TT_ExecContext exec;
583 FT_Error error;
584
585
586 /* debugging instances have their own context */
587 if ( size->debug )
588 exec = size->context;
589 else
590 exec = ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
591
592 if ( !exec )
593 return TT_Err_Could_Not_Find_Context;
594
595 TT_Load_Context( exec, face, size );
596
597 exec->callTop = 0;
598 exec->top = 0;
599
600 exec->period = 64;
601 exec->phase = 0;
602 exec->threshold = 0;
603
604 exec->instruction_trap = FALSE;
605 exec->F_dot_P = 0x10000L;
606
607 {
608 FT_Size_Metrics* metrics = &exec->metrics;
609 TT_Size_Metrics* tt_metrics = &exec->tt_metrics;
610
611
612 metrics->x_ppem = 0;
613 metrics->y_ppem = 0;
614 metrics->x_scale = 0;
615 metrics->y_scale = 0;
616
617 tt_metrics->ppem = 0;
618 tt_metrics->scale = 0;
619 tt_metrics->ratio = 0x10000L;
620 }
621
622 /* allow font program execution */
623 TT_Set_CodeRange( exec,
624 tt_coderange_font,
625 face->font_program,
626 face->font_program_size );
627
628 /* disable CVT and glyph programs coderange */
629 TT_Clear_CodeRange( exec, tt_coderange_cvt );
630 TT_Clear_CodeRange( exec, tt_coderange_glyph );
631
632 if ( face->font_program_size > 0 )
633 {
634 error = TT_Goto_CodeRange( exec, tt_coderange_font, 0 );
635
636 if ( !error )
637 {
638 FT_TRACE4(( "Executing `fpgm' table.\n" ));
639
640 error = face->interpreter( exec );
641 }
642 }
643 else
644 error = TT_Err_Ok;
645
646 if ( !error )
647 TT_Save_Context( exec, size );
648
649 return error;
650 }
651
652
653 /*************************************************************************/
654 /* */
655 /* <Function> */
656 /* tt_size_run_prep */
657 /* */
658 /* <Description> */
659 /* Run the control value program. */
660 /* */
661 /* <Input> */
662 /* size :: A handle to the size object. */
663 /* */
664 /* <Return> */
665 /* FreeType error code. 0 means success. */
666 /* */
667 FT_LOCAL_DEF( FT_Error )
668 tt_size_run_prep( TT_Size size )
669 {
670 TT_Face face = (TT_Face)size->root.face;
671 TT_ExecContext exec;
672 FT_Error error;
673
674
675 /* debugging instances have their own context */
676 if ( size->debug )
677 exec = size->context;
678 else
679 exec = ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
680
681 if ( !exec )
682 return TT_Err_Could_Not_Find_Context;
683
684 TT_Load_Context( exec, face, size );
685
686 exec->callTop = 0;
687 exec->top = 0;
688
689 exec->instruction_trap = FALSE;
690
691 TT_Set_CodeRange( exec,
692 tt_coderange_cvt,
693 face->cvt_program,
694 face->cvt_program_size );
695
696 TT_Clear_CodeRange( exec, tt_coderange_glyph );
697
698 if ( face->cvt_program_size > 0 )
699 {
700 error = TT_Goto_CodeRange( exec, tt_coderange_cvt, 0 );
701
702 if ( !error && !size->debug )
703 {
704 FT_TRACE4(( "Executing `prep' table.\n" ));
705
706 error = face->interpreter( exec );
707 }
708 }
709 else
710 error = TT_Err_Ok;
711
712 /* save as default graphics state */
713 size->GS = exec->GS;
714
715 TT_Save_Context( exec, size );
716
717 return error;
718 }
719
720 #endif /* TT_USE_BYTECODE_INTERPRETER */
721
722
723 #ifdef TT_USE_BYTECODE_INTERPRETER
724
725 static void
726 tt_size_done_bytecode( FT_Size ftsize )
727 {
728 TT_Size size = (TT_Size)ftsize;
729 TT_Face face = (TT_Face)ftsize->face;
730 FT_Memory memory = face->root.memory;
731
732
733 if ( size->debug )
734 {
735 /* the debug context must be deleted by the debugger itself */
736 size->context = NULL;
737 size->debug = FALSE;
738 }
739
740 FT_FREE( size->cvt );
741 size->cvt_size = 0;
742
743 /* free storage area */
744 FT_FREE( size->storage );
745 size->storage_size = 0;
746
747 /* twilight zone */
748 tt_glyphzone_done( &size->twilight );
749
750 FT_FREE( size->function_defs );
751 FT_FREE( size->instruction_defs );
752
753 size->num_function_defs = 0;
754 size->max_function_defs = 0;
755 size->num_instruction_defs = 0;
756 size->max_instruction_defs = 0;
757
758 size->max_func = 0;
759 size->max_ins = 0;
760
761 size->bytecode_ready = 0;
762 size->cvt_ready = 0;
763 }
764
765
766 /* Initialize bytecode-related fields in the size object. */
767 /* We do this only if bytecode interpretation is really needed. */
768 static FT_Error
769 tt_size_init_bytecode( FT_Size ftsize )
770 {
771 FT_Error error;
772 TT_Size size = (TT_Size)ftsize;
773 TT_Face face = (TT_Face)ftsize->face;
774 FT_Memory memory = face->root.memory;
775 FT_Int i;
776
777 FT_UShort n_twilight;
778 TT_MaxProfile* maxp = &face->max_profile;
779
780
781 size->bytecode_ready = 1;
782 size->cvt_ready = 0;
783
784 size->max_function_defs = maxp->maxFunctionDefs;
785 size->max_instruction_defs = maxp->maxInstructionDefs;
786
787 size->num_function_defs = 0;
788 size->num_instruction_defs = 0;
789
790 size->max_func = 0;
791 size->max_ins = 0;
792
793 size->cvt_size = face->cvt_size;
794 size->storage_size = maxp->maxStorage;
795
796 /* Set default metrics */
797 {
798 TT_Size_Metrics* metrics = &size->ttmetrics;
799
800
801 metrics->rotated = FALSE;
802 metrics->stretched = FALSE;
803
804 /* set default compensation (all 0) */
805 for ( i = 0; i < 4; i++ )
806 metrics->compensations[i] = 0;
807 }
808
809 /* allocate function defs, instruction defs, cvt, and storage area */
810 if ( FT_NEW_ARRAY( size->function_defs, size->max_function_defs ) ||
811 FT_NEW_ARRAY( size->instruction_defs, size->max_instruction_defs ) ||
812 FT_NEW_ARRAY( size->cvt, size->cvt_size ) ||
813 FT_NEW_ARRAY( size->storage, size->storage_size ) )
814 goto Exit;
815
816 /* reserve twilight zone */
817 n_twilight = maxp->maxTwilightPoints;
818
819 /* there are 4 phantom points (do we need this?) */
820 n_twilight += 4;
821
822 error = tt_glyphzone_new( memory, n_twilight, 0, &size->twilight );
823 if ( error )
824 goto Exit;
825
826 size->twilight.n_points = n_twilight;
827
828 size->GS = tt_default_graphics_state;
829
830 /* set `face->interpreter' according to the debug hook present */
831 {
832 FT_Library library = face->root.driver->root.library;
833
834
835 face->interpreter = (TT_Interpreter)
836 library->debug_hooks[FT_DEBUG_HOOK_TRUETYPE];
837 if ( !face->interpreter )
838 face->interpreter = (TT_Interpreter)TT_RunIns;
839 }
840
841 /* Fine, now run the font program! */
842 error = tt_size_run_fpgm( size );
843
844 Exit:
845 if ( error )
846 tt_size_done_bytecode( ftsize );
847
848 return error;
849 }
850
851
852 FT_LOCAL_DEF( FT_Error )
853 tt_size_ready_bytecode( TT_Size size )
854 {
855 FT_Error error = TT_Err_Ok;
856
857
858 if ( !size->bytecode_ready )
859 {
860 error = tt_size_init_bytecode( (FT_Size)size );
861 if ( error )
862 goto Exit;
863 }
864
865 /* rescale CVT when needed */
866 if ( !size->cvt_ready )
867 {
868 FT_UInt i;
869 TT_Face face = (TT_Face)size->root.face;
870
871
872 /* Scale the cvt values to the new ppem. */
873 /* We use by default the y ppem to scale the CVT. */
874 for ( i = 0; i < size->cvt_size; i++ )
875 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
876
877 /* all twilight points are originally zero */
878 for ( i = 0; i < (FT_UInt)size->twilight.n_points; i++ )
879 {
880 size->twilight.org[i].x = 0;
881 size->twilight.org[i].y = 0;
882 size->twilight.cur[i].x = 0;
883 size->twilight.cur[i].y = 0;
884 }
885
886 /* clear storage area */
887 for ( i = 0; i < (FT_UInt)size->storage_size; i++ )
888 size->storage[i] = 0;
889
890 size->GS = tt_default_graphics_state;
891
892 error = tt_size_run_prep( size );
893 if ( !error )
894 size->cvt_ready = 1;
895 }
896
897 Exit:
898 return error;
899 }
900
901 #endif /* TT_USE_BYTECODE_INTERPRETER */
902
903
904 /*************************************************************************/
905 /* */
906 /* <Function> */
907 /* tt_size_init */
908 /* */
909 /* <Description> */
910 /* Initialize a new TrueType size object. */
911 /* */
912 /* <InOut> */
913 /* size :: A handle to the size object. */
914 /* */
915 /* <Return> */
916 /* FreeType error code. 0 means success. */
917 /* */
918 FT_LOCAL_DEF( FT_Error )
919 tt_size_init( FT_Size ttsize ) /* TT_Size */
920 {
921 TT_Size size = (TT_Size)ttsize;
922 FT_Error error = TT_Err_Ok;
923
924 #ifdef TT_USE_BYTECODE_INTERPRETER
925 size->bytecode_ready = 0;
926 size->cvt_ready = 0;
927 #endif
928
929 size->ttmetrics.valid = FALSE;
930 size->strike_index = 0xFFFFFFFFUL;
931
932 return error;
933 }
934
935
936 /*************************************************************************/
937 /* */
938 /* <Function> */
939 /* tt_size_done */
940 /* */
941 /* <Description> */
942 /* The TrueType size object finalizer. */
943 /* */
944 /* <Input> */
945 /* size :: A handle to the target size object. */
946 /* */
947 FT_LOCAL_DEF( void )
948 tt_size_done( FT_Size ttsize ) /* TT_Size */
949 {
950 TT_Size size = (TT_Size)ttsize;
951
952
953 #ifdef TT_USE_BYTECODE_INTERPRETER
954 if ( size->bytecode_ready )
955 tt_size_done_bytecode( ttsize );
956 #endif
957
958 size->ttmetrics.valid = FALSE;
959 }
960
961
962 /*************************************************************************/
963 /* */
964 /* <Function> */
965 /* tt_size_reset */
966 /* */
967 /* <Description> */
968 /* Reset a TrueType size when resolutions and character dimensions */
969 /* have been changed. */
970 /* */
971 /* <Input> */
972 /* size :: A handle to the target size object. */
973 /* */
974 FT_LOCAL_DEF( FT_Error )
975 tt_size_reset( TT_Size size )
976 {
977 TT_Face face;
978 FT_Error error = TT_Err_Ok;
979 FT_Size_Metrics* metrics;
980
981
982 size->ttmetrics.valid = FALSE;
983
984 face = (TT_Face)size->root.face;
985
986 metrics = &size->metrics;
987
988 /* copy the result from base layer */
989 *metrics = size->root.metrics;
990
991 if ( metrics->x_ppem < 1 || metrics->y_ppem < 1 )
992 return TT_Err_Invalid_PPem;
993
994 /* This bit flag, if set, indicates that the ppems must be */
995 /* rounded to integers. Nearly all TrueType fonts have this bit */
996 /* set, as hinting won't work really well otherwise. */
997 /* */
998 if ( face->header.Flags & 8 )
999 {
1000 metrics->x_scale = FT_DivFix( metrics->x_ppem << 6,
1001 face->root.units_per_EM );
1002 metrics->y_scale = FT_DivFix( metrics->y_ppem << 6,
1003 face->root.units_per_EM );
1004
1005 metrics->ascender =
1006 FT_PIX_ROUND( FT_MulFix( face->root.ascender, metrics->y_scale ) );
1007 metrics->descender =
1008 FT_PIX_ROUND( FT_MulFix( face->root.descender, metrics->y_scale ) );
1009 metrics->height =
1010 FT_PIX_ROUND( FT_MulFix( face->root.height, metrics->y_scale ) );
1011 metrics->max_advance =
1012 FT_PIX_ROUND( FT_MulFix( face->root.max_advance_width,
1013 metrics->x_scale ) );
1014 }
1015
1016 /* compute new transformation */
1017 if ( metrics->x_ppem >= metrics->y_ppem )
1018 {
1019 size->ttmetrics.scale = metrics->x_scale;
1020 size->ttmetrics.ppem = metrics->x_ppem;
1021 size->ttmetrics.x_ratio = 0x10000L;
1022 size->ttmetrics.y_ratio = FT_MulDiv( metrics->y_ppem,
1023 0x10000L,
1024 metrics->x_ppem );
1025 }
1026 else
1027 {
1028 size->ttmetrics.scale = metrics->y_scale;
1029 size->ttmetrics.ppem = metrics->y_ppem;
1030 size->ttmetrics.x_ratio = FT_MulDiv( metrics->x_ppem,
1031 0x10000L,
1032 metrics->y_ppem );
1033 size->ttmetrics.y_ratio = 0x10000L;
1034 }
1035
1036 #ifdef TT_USE_BYTECODE_INTERPRETER
1037 size->cvt_ready = 0;
1038 #endif /* TT_USE_BYTECODE_INTERPRETER */
1039
1040 if ( !error )
1041 size->ttmetrics.valid = TRUE;
1042
1043 return error;
1044 }
1045
1046
1047 /*************************************************************************/
1048 /* */
1049 /* <Function> */
1050 /* tt_driver_init */
1051 /* */
1052 /* <Description> */
1053 /* Initialize a given TrueType driver object. */
1054 /* */
1055 /* <Input> */
1056 /* driver :: A handle to the target driver object. */
1057 /* */
1058 /* <Return> */
1059 /* FreeType error code. 0 means success. */
1060 /* */
1061 FT_LOCAL_DEF( FT_Error )
1062 tt_driver_init( FT_Module ttdriver ) /* TT_Driver */
1063 {
1064
1065 #ifdef TT_USE_BYTECODE_INTERPRETER
1066
1067 TT_Driver driver = (TT_Driver)ttdriver;
1068
1069
1070 if ( !TT_New_Context( driver ) )
1071 return TT_Err_Could_Not_Find_Context;
1072
1073 #else
1074
1075 FT_UNUSED( ttdriver );
1076
1077 #endif
1078
1079 return TT_Err_Ok;
1080 }
1081
1082
1083 /*************************************************************************/
1084 /* */
1085 /* <Function> */
1086 /* tt_driver_done */
1087 /* */
1088 /* <Description> */
1089 /* Finalize a given TrueType driver. */
1090 /* */
1091 /* <Input> */
1092 /* driver :: A handle to the target TrueType driver. */
1093 /* */
1094 FT_LOCAL_DEF( void )
1095 tt_driver_done( FT_Module ttdriver ) /* TT_Driver */
1096 {
1097 #ifdef TT_USE_BYTECODE_INTERPRETER
1098 TT_Driver driver = (TT_Driver)ttdriver;
1099
1100
1101 /* destroy the execution context */
1102 if ( driver->context )
1103 {
1104 TT_Done_Context( driver->context );
1105 driver->context = NULL;
1106 }
1107 #else
1108 FT_UNUSED( ttdriver );
1109 #endif
1110
1111 }
1112
1113
1114 /*************************************************************************/
1115 /* */
1116 /* <Function> */
1117 /* tt_slot_init */
1118 /* */
1119 /* <Description> */
1120 /* Initialize a new slot object. */
1121 /* */
1122 /* <InOut> */
1123 /* slot :: A handle to the slot object. */
1124 /* */
1125 /* <Return> */
1126 /* FreeType error code. 0 means success. */
1127 /* */
1128 FT_LOCAL_DEF( FT_Error )
1129 tt_slot_init( FT_GlyphSlot slot )
1130 {
1131 return FT_GlyphLoader_CreateExtra( slot->internal->loader );
1132 }
1133
1134
1135 /* END */