1 /***************************************************************************/
5 /* Objects manager (body). */
7 /* Copyright 1996-2001, 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_CALC_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_IDS_H
24 #include FT_TRUETYPE_TAGS_H
25 #include FT_INTERNAL_SFNT_H
26 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
33 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
38 /*************************************************************************/
40 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
41 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
42 /* messages during execution. */
45 #define FT_COMPONENT trace_ttobjs
48 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
50 /*************************************************************************/
52 /* GLYPH ZONE FUNCTIONS */
54 /*************************************************************************/
57 /*************************************************************************/
60 /* tt_glyphzone_done */
63 /* Deallocates a glyph zone. */
66 /* zone :: A pointer to the target glyph zone. */
69 tt_glyphzone_done( TT_GlyphZone zone )
71 FT_Memory memory = zone->memory;
75 FT_FREE( zone->contours );
76 FT_FREE( zone->tags );
80 zone->max_points = zone->n_points = 0;
81 zone->max_contours = zone->n_contours = 0;
87 /*************************************************************************/
90 /* tt_glyphzone_new */
93 /* Allocates a new glyph zone. */
96 /* memory :: A handle to the current memory object. */
98 /* maxPoints :: The capacity of glyph zone in points. */
100 /* maxContours :: The capacity of glyph zone in contours. */
103 /* zone :: A pointer to the target glyph zone record. */
106 /* FreeType error code. 0 means success. */
108 FT_LOCAL_DEF( FT_Error )
109 tt_glyphzone_new( FT_Memory memory,
111 FT_Short maxContours,
120 FT_MEM_ZERO( zone, sizeof ( *zone ) );
121 zone->memory = memory;
123 if ( FT_NEW_ARRAY( zone->org, maxPoints * 2 ) ||
124 FT_NEW_ARRAY( zone->cur, maxPoints * 2 ) ||
125 FT_NEW_ARRAY( zone->tags, maxPoints ) ||
126 FT_NEW_ARRAY( zone->contours, maxContours ) )
128 tt_glyphzone_done( zone );
133 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
136 /*************************************************************************/
142 /* Initializes a given TrueType face object. */
145 /* stream :: The source font stream. */
147 /* face_index :: The index of the font face in the resource. */
149 /* num_params :: Number of additional generic parameters. Ignored. */
151 /* params :: Additional generic parameters. Ignored. */
154 /* face :: The newly built face object. */
157 /* FreeType error code. 0 means success. */
159 FT_LOCAL_DEF( FT_Error )
160 tt_face_init( FT_Stream stream,
164 FT_Parameter* params )
171 library = face->root.driver->root.library;
172 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );
176 /* create input stream from resource */
177 if ( FT_STREAM_SEEK( 0 ) )
180 /* check that we have a valid TrueType file */
181 error = sfnt->init_face( stream, face, face_index, num_params, params );
185 /* We must also be able to accept Mac/GX fonts, as well as OT ones */
186 if ( face->format_tag != 0x00010000L && /* MS fonts */
187 face->format_tag != TTAG_true ) /* Mac fonts */
189 FT_TRACE2(( "[not a valid TTF font]\n" ));
193 /* If we are performing a simple font format check, exit immediately */
194 if ( face_index < 0 )
197 /* Load font directory */
198 error = sfnt->load_face( stream, face, face_index, num_params, params );
202 if ( face->root.face_flags & FT_FACE_FLAG_SCALABLE )
205 #ifdef FT_CONFIG_OPTION_INCREMENTAL
207 if ( !face->root.internal->incremental_interface )
208 error = tt_face_load_loca( face, stream );
210 error = tt_face_load_cvt ( face, stream ) ||
211 tt_face_load_fpgm ( face, stream );
216 error = tt_face_load_loca( face, stream ) ||
217 tt_face_load_cvt ( face, stream ) ||
218 tt_face_load_fpgm ( face, stream );
224 /* initialize standard glyph loading routines */
225 TT_Init_Glyph_Loading( face );
231 error = TT_Err_Unknown_File_Format;
236 /*************************************************************************/
242 /* Finalizes a given face object. */
245 /* face :: A pointer to the face object to destroy. */
248 tt_face_done( TT_Face face )
250 FT_Memory memory = face->root.memory;
251 FT_Stream stream = face->root.stream;
253 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
256 /* for `extended TrueType formats' (i.e. compressed versions) */
257 if ( face->extra.finalizer )
258 face->extra.finalizer( face->extra.data );
261 sfnt->done_face( face );
263 /* freeing the locations table */
264 FT_FREE( face->glyph_locations );
265 face->num_locations = 0;
267 /* freeing the CVT */
268 FT_FREE( face->cvt );
271 /* freeing the programs */
272 FT_FRAME_RELEASE( face->font_program );
273 FT_FRAME_RELEASE( face->cvt_program );
274 face->font_program_size = 0;
275 face->cvt_program_size = 0;
279 /*************************************************************************/
283 /*************************************************************************/
286 /*************************************************************************/
292 /* Initializes a new TrueType size object. */
295 /* size :: A handle to the size object. */
298 /* FreeType error code. 0 means success. */
300 FT_LOCAL_DEF( FT_Error )
301 tt_size_init( TT_Size size )
303 FT_Error error = TT_Err_Ok;
306 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
308 TT_Face face = (TT_Face)size->root.face;
309 FT_Memory memory = face->root.memory;
313 FT_UShort n_twilight;
314 TT_MaxProfile* maxp = &face->max_profile;
317 size->ttmetrics.valid = FALSE;
319 size->max_function_defs = maxp->maxFunctionDefs;
320 size->max_instruction_defs = maxp->maxInstructionDefs;
322 size->num_function_defs = 0;
323 size->num_instruction_defs = 0;
328 size->cvt_size = face->cvt_size;
329 size->storage_size = maxp->maxStorage;
331 /* Set default metrics */
333 FT_Size_Metrics* metrics = &size->root.metrics;
334 TT_Size_Metrics* metrics2 = &size->ttmetrics;
340 metrics2->rotated = FALSE;
341 metrics2->stretched = FALSE;
343 /* set default compensation (all 0) */
344 for ( i = 0; i < 4; i++ )
345 metrics2->compensations[i] = 0;
348 /* allocate function defs, instruction defs, cvt, and storage area */
349 if ( FT_NEW_ARRAY( size->function_defs, size->max_function_defs ) ||
350 FT_NEW_ARRAY( size->instruction_defs, size->max_instruction_defs ) ||
351 FT_NEW_ARRAY( size->cvt, size->cvt_size ) ||
352 FT_NEW_ARRAY( size->storage, size->storage_size ) )
356 /* reserve twilight zone */
357 n_twilight = maxp->maxTwilightPoints;
358 error = tt_glyphzone_new( memory, n_twilight, 0, &size->twilight );
362 size->twilight.n_points = n_twilight;
364 /* set `face->interpreter' according to the debug hook present */
366 FT_Library library = face->root.driver->root.library;
369 face->interpreter = (TT_Interpreter)
370 library->debug_hooks[FT_DEBUG_HOOK_TRUETYPE];
371 if ( !face->interpreter )
372 face->interpreter = (TT_Interpreter)TT_RunIns;
375 /* Fine, now execute the font program! */
376 exec = size->context;
377 /* size objects used during debugging have their own context */
379 exec = TT_New_Context( face );
383 error = TT_Err_Could_Not_Find_Context;
387 size->GS = tt_default_graphics_state;
388 TT_Load_Context( exec, face, size );
398 FT_Size_Metrics* metrics = &exec->metrics;
399 TT_Size_Metrics* tt_metrics = &exec->tt_metrics;
404 metrics->x_scale = 0;
405 metrics->y_scale = 0;
407 tt_metrics->ppem = 0;
408 tt_metrics->scale = 0;
409 tt_metrics->ratio = 0x10000L;
412 exec->instruction_trap = FALSE;
414 exec->cvtSize = size->cvt_size;
415 exec->cvt = size->cvt;
417 exec->F_dot_P = 0x10000L;
419 /* allow font program execution */
420 TT_Set_CodeRange( exec,
423 face->font_program_size );
425 /* disable CVT and glyph programs coderange */
426 TT_Clear_CodeRange( exec, tt_coderange_cvt );
427 TT_Clear_CodeRange( exec, tt_coderange_glyph );
429 if ( face->font_program_size > 0 )
431 error = TT_Goto_CodeRange( exec, tt_coderange_font, 0 );
433 error = face->interpreter( exec );
441 TT_Save_Context( exec, size );
444 TT_Done_Context( exec );
446 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
448 size->ttmetrics.valid = FALSE;
451 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
455 TT_Done_Context( exec );
459 tt_size_done( size );
462 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
467 /*************************************************************************/
473 /* The TrueType size object finalizer. */
476 /* size :: A handle to the target size object. */
479 tt_size_done( TT_Size size )
482 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
484 FT_Memory memory = size->root.face->memory;
489 /* the debug context must be deleted by the debugger itself */
490 size->context = NULL;
494 FT_FREE( size->cvt );
497 /* free storage area */
498 FT_FREE( size->storage );
499 size->storage_size = 0;
502 tt_glyphzone_done( &size->twilight );
504 FT_FREE( size->function_defs );
505 FT_FREE( size->instruction_defs );
507 size->num_function_defs = 0;
508 size->max_function_defs = 0;
509 size->num_instruction_defs = 0;
510 size->max_instruction_defs = 0;
517 size->ttmetrics.valid = FALSE;
521 /*************************************************************************/
524 /* Reset_Outline_Size */
527 /* Resets a TrueType outline size when resolutions and character */
528 /* dimensions have been changed. */
531 /* size :: A handle to the target size object. */
534 Reset_Outline_Size( TT_Size size )
537 FT_Error error = TT_Err_Ok;
539 FT_Size_Metrics* metrics;
542 if ( size->ttmetrics.valid )
545 face = (TT_Face)size->root.face;
547 metrics = &size->metrics;
549 if ( metrics->x_ppem < 1 || metrics->y_ppem < 1 )
550 return TT_Err_Invalid_PPem;
552 /* compute new transformation */
553 if ( metrics->x_ppem >= metrics->y_ppem )
555 size->ttmetrics.scale = metrics->x_scale;
556 size->ttmetrics.ppem = metrics->x_ppem;
557 size->ttmetrics.x_ratio = 0x10000L;
558 size->ttmetrics.y_ratio = FT_MulDiv( metrics->y_ppem,
564 size->ttmetrics.scale = metrics->y_scale;
565 size->ttmetrics.ppem = metrics->y_ppem;
566 size->ttmetrics.x_ratio = FT_MulDiv( metrics->x_ppem,
569 size->ttmetrics.y_ratio = 0x10000L;
572 /* Compute root ascender, descender, test height, and max_advance */
573 metrics->ascender = ( FT_MulFix( face->root.ascender,
574 metrics->y_scale ) + 32 ) & -64;
575 metrics->descender = ( FT_MulFix( face->root.descender,
576 metrics->y_scale ) + 32 ) & -64;
577 metrics->height = ( FT_MulFix( face->root.height,
578 metrics->y_scale ) + 32 ) & -64;
579 metrics->max_advance = ( FT_MulFix( face->root.max_advance_width,
580 metrics->x_scale ) + 32 ) & -64;
583 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
584 /* set to `invalid' by default */
585 size->strike_index = 0xFFFFU;
588 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
595 /* Scale the cvt values to the new ppem. */
596 /* We use by default the y ppem to scale the CVT. */
597 for ( i = 0; i < size->cvt_size; i++ )
598 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
600 /* All twilight points are originally zero */
601 for ( j = 0; j < (FT_UInt)size->twilight.n_points; j++ )
603 size->twilight.org[j].x = 0;
604 size->twilight.org[j].y = 0;
605 size->twilight.cur[j].x = 0;
606 size->twilight.cur[j].y = 0;
609 /* clear storage area */
610 for ( i = 0; i < (FT_UInt)size->storage_size; i++ )
611 size->storage[i] = 0;
613 size->GS = tt_default_graphics_state;
615 /* get execution context and run prep program */
617 exec = size->context;
619 exec = TT_New_Context( face );
620 /* debugging instances have their own context */
623 return TT_Err_Could_Not_Find_Context;
625 TT_Load_Context( exec, face, size );
627 TT_Set_CodeRange( exec,
630 face->cvt_program_size );
632 TT_Clear_CodeRange( exec, tt_coderange_glyph );
634 exec->instruction_trap = FALSE;
639 if ( face->cvt_program_size > 0 )
641 error = TT_Goto_CodeRange( exec, tt_coderange_cvt, 0 );
646 error = face->interpreter( exec );
652 /* save default graphics state */
655 TT_Save_Context( exec, size );
658 TT_Done_Context( exec );
659 /* debugging instances keep their context */
662 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
665 size->ttmetrics.valid = TRUE;
671 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
673 /*************************************************************************/
676 /* Reset_SBit_Size */
679 /* Resets a TrueType sbit size when resolutions and character */
680 /* dimensions have been changed. */
683 /* size :: A handle to the target size object. */
686 Reset_SBit_Size( TT_Size size )
689 FT_Error error = TT_Err_Ok;
691 FT_ULong strike_index;
692 FT_Size_Metrics* metrics;
693 FT_Size_Metrics* sbit_metrics;
697 metrics = &size->metrics;
699 if ( size->strike_index != 0xFFFFU )
702 face = (TT_Face)size->root.face;
703 sfnt = (SFNT_Service)face->sfnt;
705 sbit_metrics = &size->strike_metrics;
707 error = sfnt->set_sbit_strike(face,
708 metrics->x_ppem, metrics->y_ppem,
713 TT_SBit_Strike strike = face->sbit_strikes + strike_index;
716 sbit_metrics->x_ppem = metrics->x_ppem;
717 sbit_metrics->y_ppem = metrics->y_ppem;
720 * sbit_metrics->?_scale
723 sbit_metrics->x_scale = 1 << 16;
724 sbit_metrics->y_scale = 1 << 16;
727 sbit_metrics->ascender = strike->hori.ascender << 6;
728 sbit_metrics->descender = strike->hori.descender << 6;
730 /* XXX: Is this correct? */
731 sbit_metrics->height = sbit_metrics->ascender -
732 sbit_metrics->descender;
734 /* XXX: Is this correct? */
735 sbit_metrics->max_advance = ( strike->hori.min_origin_SB +
736 strike->hori.max_width +
737 strike->hori.min_advance_SB ) << 6;
739 size->strike_index = strike_index;
743 size->strike_index = 0xFFFFU;
745 sbit_metrics->x_ppem = 0;
746 sbit_metrics->y_ppem = 0;
747 sbit_metrics->ascender = 0;
748 sbit_metrics->descender = 0;
749 sbit_metrics->height = 0;
750 sbit_metrics->max_advance = 0;
756 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
759 /*************************************************************************/
765 /* Resets a TrueType size when resolutions and character dimensions */
766 /* have been changed. */
769 /* size :: A handle to the target size object. */
771 FT_LOCAL_DEF( FT_Error )
772 tt_size_reset( TT_Size size )
775 FT_Error error = TT_Err_Ok;
778 face = size->root.face;
780 if ( face->face_flags & FT_FACE_FLAG_SCALABLE )
782 if ( !size->ttmetrics.valid )
783 error = Reset_Outline_Size( size );
789 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
791 if ( face->face_flags & FT_FACE_FLAG_FIXED_SIZES )
793 if ( size->strike_index == 0xFFFFU )
794 error = Reset_SBit_Size( size );
796 if ( !error && !( face->face_flags & FT_FACE_FLAG_SCALABLE ) )
797 size->root.metrics = size->strike_metrics;
800 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
802 if ( face->face_flags & FT_FACE_FLAG_SCALABLE )
809 /*************************************************************************/
815 /* Initializes a given TrueType driver object. */
818 /* driver :: A handle to the target driver object. */
821 /* FreeType error code. 0 means success. */
823 FT_LOCAL_DEF( FT_Error )
824 tt_driver_init( TT_Driver driver )
829 /* set `extra' in glyph loader */
830 error = FT_GlyphLoader_CreateExtra( FT_DRIVER( driver )->glyph_loader );
836 /*************************************************************************/
842 /* Finalizes a given TrueType driver. */
845 /* driver :: A handle to the target TrueType driver. */
848 tt_driver_done( TT_Driver driver )
850 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
852 /* destroy the execution context */
853 if ( driver->context )
855 TT_Destroy_Context( driver->context, driver->root.root.memory );
856 driver->context = NULL;