[FREETYPE]
[reactos.git] / reactos / lib / 3rdparty / freetype / src / type42 / t42objs.c
1 /***************************************************************************/
2 /* */
3 /* t42objs.c */
4 /* */
5 /* Type 42 objects manager (body). */
6 /* */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 */
8 /* by Roberto Alameda. */
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 "t42objs.h"
20 #include "t42parse.h"
21 #include "t42error.h"
22 #include FT_INTERNAL_DEBUG_H
23 #include FT_LIST_H
24 #include FT_TRUETYPE_IDS_H
25
26
27 #undef FT_COMPONENT
28 #define FT_COMPONENT trace_t42
29
30
31 static FT_Error
32 T42_Open_Face( T42_Face face )
33 {
34 T42_LoaderRec loader;
35 T42_Parser parser;
36 T1_Font type1 = &face->type1;
37 FT_Memory memory = face->root.memory;
38 FT_Error error;
39
40 PSAux_Service psaux = (PSAux_Service)face->psaux;
41
42
43 t42_loader_init( &loader, face );
44
45 parser = &loader.parser;
46
47 if ( FT_ALLOC( face->ttf_data, 12 ) )
48 goto Exit;
49
50 error = t42_parser_init( parser,
51 face->root.stream,
52 memory,
53 psaux);
54 if ( error )
55 goto Exit;
56
57 error = t42_parse_dict( face, &loader,
58 parser->base_dict, parser->base_len );
59 if ( error )
60 goto Exit;
61
62 if ( type1->font_type != 42 )
63 {
64 error = T42_Err_Unknown_File_Format;
65 goto Exit;
66 }
67
68 /* now, propagate the charstrings and glyphnames tables */
69 /* to the Type1 data */
70 type1->num_glyphs = loader.num_glyphs;
71
72 if ( !loader.charstrings.init )
73 {
74 FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
75 error = T42_Err_Invalid_File_Format;
76 }
77
78 loader.charstrings.init = 0;
79 type1->charstrings_block = loader.charstrings.block;
80 type1->charstrings = loader.charstrings.elements;
81 type1->charstrings_len = loader.charstrings.lengths;
82
83 /* we copy the glyph names `block' and `elements' fields; */
84 /* the `lengths' field must be released later */
85 type1->glyph_names_block = loader.glyph_names.block;
86 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
87 loader.glyph_names.block = 0;
88 loader.glyph_names.elements = 0;
89
90 /* we must now build type1.encoding when we have a custom array */
91 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
92 {
93 FT_Int charcode, idx, min_char, max_char;
94 FT_Byte* char_name;
95 FT_Byte* glyph_name;
96
97
98 /* OK, we do the following: for each element in the encoding */
99 /* table, look up the index of the glyph having the same name */
100 /* as defined in the CharStrings array. */
101 /* The index is then stored in type1.encoding.char_index, and */
102 /* the name in type1.encoding.char_name */
103
104 min_char = 0;
105 max_char = 0;
106
107 charcode = 0;
108 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
109 {
110 type1->encoding.char_index[charcode] = 0;
111 type1->encoding.char_name [charcode] = (char *)".notdef";
112
113 char_name = loader.encoding_table.elements[charcode];
114 if ( char_name )
115 for ( idx = 0; idx < type1->num_glyphs; idx++ )
116 {
117 glyph_name = (FT_Byte*)type1->glyph_names[idx];
118 if ( ft_strcmp( (const char*)char_name,
119 (const char*)glyph_name ) == 0 )
120 {
121 type1->encoding.char_index[charcode] = (FT_UShort)idx;
122 type1->encoding.char_name [charcode] = (char*)glyph_name;
123
124 /* Change min/max encoded char only if glyph name is */
125 /* not /.notdef */
126 if ( ft_strcmp( (const char*)".notdef",
127 (const char*)glyph_name ) != 0 )
128 {
129 if ( charcode < min_char )
130 min_char = charcode;
131 if ( charcode >= max_char )
132 max_char = charcode + 1;
133 }
134 break;
135 }
136 }
137 }
138
139 type1->encoding.code_first = min_char;
140 type1->encoding.code_last = max_char;
141 type1->encoding.num_chars = loader.num_chars;
142 }
143
144 Exit:
145 t42_loader_done( &loader );
146 return error;
147 }
148
149
150 /***************** Driver Functions *************/
151
152
153 FT_LOCAL_DEF( FT_Error )
154 T42_Face_Init( FT_Stream stream,
155 T42_Face face,
156 FT_Int face_index,
157 FT_Int num_params,
158 FT_Parameter* params )
159 {
160 FT_Error error;
161 FT_Service_PsCMaps psnames;
162 PSAux_Service psaux;
163 FT_Face root = (FT_Face)&face->root;
164 T1_Font type1 = &face->type1;
165 PS_FontInfo info = &type1->font_info;
166
167 FT_UNUSED( num_params );
168 FT_UNUSED( params );
169 FT_UNUSED( face_index );
170 FT_UNUSED( stream );
171
172
173 face->ttf_face = NULL;
174 face->root.num_faces = 1;
175
176 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
177 face->psnames = psnames;
178
179 face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
180 "psaux" );
181 psaux = (PSAux_Service)face->psaux;
182
183 /* open the tokenizer, this will also check the font format */
184 error = T42_Open_Face( face );
185 if ( error )
186 goto Exit;
187
188 /* if we just wanted to check the format, leave successfully now */
189 if ( face_index < 0 )
190 goto Exit;
191
192 /* check the face index */
193 if ( face_index > 0 )
194 {
195 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
196 error = T42_Err_Invalid_Argument;
197 goto Exit;
198 }
199
200 /* Now load the font program into the face object */
201
202 /* Init the face object fields */
203 /* Now set up root face fields */
204
205 root->num_glyphs = type1->num_glyphs;
206 root->num_charmaps = 0;
207 root->face_index = 0;
208
209 root->face_flags = FT_FACE_FLAG_SCALABLE |
210 FT_FACE_FLAG_HORIZONTAL |
211 FT_FACE_FLAG_GLYPH_NAMES;
212
213 if ( info->is_fixed_pitch )
214 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
215
216 /* We only set this flag if we have the patented bytecode interpreter. */
217 /* There are no known `tricky' Type42 fonts that could be loaded with */
218 /* the unpatented interpreter. */
219 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
220 root->face_flags |= FT_FACE_FLAG_HINTER;
221 #endif
222
223 /* XXX: TODO -- add kerning with .afm support */
224
225 /* get style name -- be careful, some broken fonts only */
226 /* have a `/FontName' dictionary entry! */
227 root->family_name = info->family_name;
228 /* assume "Regular" style if we don't know better */
229 root->style_name = (char *)"Regular";
230 if ( root->family_name )
231 {
232 char* full = info->full_name;
233 char* family = root->family_name;
234
235
236 if ( full )
237 {
238 while ( *full )
239 {
240 if ( *full == *family )
241 {
242 family++;
243 full++;
244 }
245 else
246 {
247 if ( *full == ' ' || *full == '-' )
248 full++;
249 else if ( *family == ' ' || *family == '-' )
250 family++;
251 else
252 {
253 if ( !*family )
254 root->style_name = full;
255 break;
256 }
257 }
258 }
259 }
260 }
261 else
262 {
263 /* do we have a `/FontName'? */
264 if ( type1->font_name )
265 root->family_name = type1->font_name;
266 }
267
268 /* no embedded bitmap support */
269 root->num_fixed_sizes = 0;
270 root->available_sizes = 0;
271
272 /* Load the TTF font embedded in the T42 font */
273 {
274 FT_Open_Args args;
275
276
277 args.flags = FT_OPEN_MEMORY;
278 args.memory_base = face->ttf_data;
279 args.memory_size = face->ttf_size;
280
281 if ( num_params )
282 {
283 args.flags |= FT_OPEN_PARAMS;
284 args.num_params = num_params;
285 args.params = params;
286 }
287
288 error = FT_Open_Face( FT_FACE_LIBRARY( face ),
289 &args, 0, &face->ttf_face );
290 }
291
292 if ( error )
293 goto Exit;
294
295 FT_Done_Size( face->ttf_face->size );
296
297 /* Ignore info in FontInfo dictionary and use the info from the */
298 /* loaded TTF font. The PostScript interpreter also ignores it. */
299 root->bbox = face->ttf_face->bbox;
300 root->units_per_EM = face->ttf_face->units_per_EM;
301
302 root->ascender = face->ttf_face->ascender;
303 root->descender = face->ttf_face->descender;
304 root->height = face->ttf_face->height;
305
306 root->max_advance_width = face->ttf_face->max_advance_width;
307 root->max_advance_height = face->ttf_face->max_advance_height;
308
309 root->underline_position = (FT_Short)info->underline_position;
310 root->underline_thickness = (FT_Short)info->underline_thickness;
311
312 /* compute style flags */
313 root->style_flags = 0;
314 if ( info->italic_angle )
315 root->style_flags |= FT_STYLE_FLAG_ITALIC;
316
317 if ( face->ttf_face->style_flags & FT_STYLE_FLAG_BOLD )
318 root->style_flags |= FT_STYLE_FLAG_BOLD;
319
320 if ( face->ttf_face->face_flags & FT_FACE_FLAG_VERTICAL )
321 root->face_flags |= FT_FACE_FLAG_VERTICAL;
322
323 {
324 if ( psnames && psaux )
325 {
326 FT_CharMapRec charmap;
327 T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
328 FT_CMap_Class clazz;
329
330
331 charmap.face = root;
332
333 /* first of all, try to synthesize a Unicode charmap */
334 charmap.platform_id = TT_PLATFORM_MICROSOFT;
335 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
336 charmap.encoding = FT_ENCODING_UNICODE;
337
338 error = FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
339 if ( error && FT_Err_No_Unicode_Glyph_Name != error )
340 goto Exit;
341 error = FT_Err_Ok;
342
343 /* now, generate an Adobe Standard encoding when appropriate */
344 charmap.platform_id = TT_PLATFORM_ADOBE;
345 clazz = NULL;
346
347 switch ( type1->encoding_type )
348 {
349 case T1_ENCODING_TYPE_STANDARD:
350 charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
351 charmap.encoding_id = TT_ADOBE_ID_STANDARD;
352 clazz = cmap_classes->standard;
353 break;
354
355 case T1_ENCODING_TYPE_EXPERT:
356 charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
357 charmap.encoding_id = TT_ADOBE_ID_EXPERT;
358 clazz = cmap_classes->expert;
359 break;
360
361 case T1_ENCODING_TYPE_ARRAY:
362 charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
363 charmap.encoding_id = TT_ADOBE_ID_CUSTOM;
364 clazz = cmap_classes->custom;
365 break;
366
367 case T1_ENCODING_TYPE_ISOLATIN1:
368 charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
369 charmap.encoding_id = TT_ADOBE_ID_LATIN_1;
370 clazz = cmap_classes->unicode;
371 break;
372
373 default:
374 ;
375 }
376
377 if ( clazz )
378 error = FT_CMap_New( clazz, NULL, &charmap, NULL );
379
380 #if 0
381 /* Select default charmap */
382 if ( root->num_charmaps )
383 root->charmap = root->charmaps[0];
384 #endif
385 }
386 }
387 Exit:
388 return error;
389 }
390
391
392 FT_LOCAL_DEF( void )
393 T42_Face_Done( T42_Face face )
394 {
395 T1_Font type1;
396 PS_FontInfo info;
397 FT_Memory memory;
398
399
400 if ( !face )
401 return;
402
403 type1 = &face->type1;
404 info = &type1->font_info;
405 memory = face->root.memory;
406
407 /* delete internal ttf face prior to freeing face->ttf_data */
408 if ( face->ttf_face )
409 FT_Done_Face( face->ttf_face );
410
411 /* release font info strings */
412 FT_FREE( info->version );
413 FT_FREE( info->notice );
414 FT_FREE( info->full_name );
415 FT_FREE( info->family_name );
416 FT_FREE( info->weight );
417
418 /* release top dictionary */
419 FT_FREE( type1->charstrings_len );
420 FT_FREE( type1->charstrings );
421 FT_FREE( type1->glyph_names );
422
423 FT_FREE( type1->charstrings_block );
424 FT_FREE( type1->glyph_names_block );
425
426 FT_FREE( type1->encoding.char_index );
427 FT_FREE( type1->encoding.char_name );
428 FT_FREE( type1->font_name );
429
430 FT_FREE( face->ttf_data );
431
432 #if 0
433 /* release afm data if present */
434 if ( face->afm_data )
435 T1_Done_AFM( memory, (T1_AFM*)face->afm_data );
436 #endif
437
438 /* release unicode map, if any */
439 FT_FREE( face->unicode_map.maps );
440 face->unicode_map.num_maps = 0;
441
442 face->root.family_name = 0;
443 face->root.style_name = 0;
444 }
445
446
447 /*************************************************************************/
448 /* */
449 /* <Function> */
450 /* T42_Driver_Init */
451 /* */
452 /* <Description> */
453 /* Initializes a given Type 42 driver object. */
454 /* */
455 /* <Input> */
456 /* driver :: A handle to the target driver object. */
457 /* */
458 /* <Return> */
459 /* FreeType error code. 0 means success. */
460 /* */
461 FT_LOCAL_DEF( FT_Error )
462 T42_Driver_Init( T42_Driver driver )
463 {
464 FT_Module ttmodule;
465
466
467 ttmodule = FT_Get_Module( FT_MODULE(driver)->library, "truetype" );
468 driver->ttclazz = (FT_Driver_Class)ttmodule->clazz;
469
470 return T42_Err_Ok;
471 }
472
473
474 FT_LOCAL_DEF( void )
475 T42_Driver_Done( T42_Driver driver )
476 {
477 FT_UNUSED( driver );
478 }
479
480
481 FT_LOCAL_DEF( FT_Error )
482 T42_Size_Init( T42_Size size )
483 {
484 FT_Face face = size->root.face;
485 T42_Face t42face = (T42_Face)face;
486 FT_Size ttsize;
487 FT_Error error = T42_Err_Ok;
488
489
490 error = FT_New_Size( t42face->ttf_face, &ttsize );
491 size->ttsize = ttsize;
492
493 FT_Activate_Size( ttsize );
494
495 return error;
496 }
497
498
499 FT_LOCAL_DEF( FT_Error )
500 T42_Size_Request( T42_Size size,
501 FT_Size_Request req )
502 {
503 T42_Face face = (T42_Face)size->root.face;
504 FT_Error error;
505
506
507 FT_Activate_Size( size->ttsize );
508
509 error = FT_Request_Size( face->ttf_face, req );
510 if ( !error )
511 ( (FT_Size)size )->metrics = face->ttf_face->size->metrics;
512
513 return error;
514 }
515
516
517 FT_LOCAL_DEF( FT_Error )
518 T42_Size_Select( T42_Size size,
519 FT_ULong strike_index )
520 {
521 T42_Face face = (T42_Face)size->root.face;
522 FT_Error error;
523
524
525 FT_Activate_Size( size->ttsize );
526
527 error = FT_Select_Size( face->ttf_face, (FT_Int)strike_index );
528 if ( !error )
529 ( (FT_Size)size )->metrics = face->ttf_face->size->metrics;
530
531 return error;
532
533 }
534
535
536 FT_LOCAL_DEF( void )
537 T42_Size_Done( T42_Size size )
538 {
539 FT_Face face = size->root.face;
540 T42_Face t42face = (T42_Face)face;
541 FT_ListNode node;
542
543
544 node = FT_List_Find( &t42face->ttf_face->sizes_list, size->ttsize );
545 if ( node )
546 {
547 FT_Done_Size( size->ttsize );
548 size->ttsize = NULL;
549 }
550 }
551
552
553 FT_LOCAL_DEF( FT_Error )
554 T42_GlyphSlot_Init( T42_GlyphSlot slot )
555 {
556 FT_Face face = slot->root.face;
557 T42_Face t42face = (T42_Face)face;
558 FT_GlyphSlot ttslot;
559 FT_Error error = T42_Err_Ok;
560
561
562 if ( face->glyph == NULL )
563 {
564 /* First glyph slot for this face */
565 slot->ttslot = t42face->ttf_face->glyph;
566 }
567 else
568 {
569 error = FT_New_GlyphSlot( t42face->ttf_face, &ttslot );
570 slot->ttslot = ttslot;
571 }
572
573 return error;
574 }
575
576
577 FT_LOCAL_DEF( void )
578 T42_GlyphSlot_Done( T42_GlyphSlot slot )
579 {
580 FT_Done_GlyphSlot( slot->ttslot );
581 }
582
583
584 static void
585 t42_glyphslot_clear( FT_GlyphSlot slot )
586 {
587 /* free bitmap if needed */
588 ft_glyphslot_free_bitmap( slot );
589
590 /* clear all public fields in the glyph slot */
591 FT_ZERO( &slot->metrics );
592 FT_ZERO( &slot->outline );
593 FT_ZERO( &slot->bitmap );
594
595 slot->bitmap_left = 0;
596 slot->bitmap_top = 0;
597 slot->num_subglyphs = 0;
598 slot->subglyphs = 0;
599 slot->control_data = 0;
600 slot->control_len = 0;
601 slot->other = 0;
602 slot->format = FT_GLYPH_FORMAT_NONE;
603
604 slot->linearHoriAdvance = 0;
605 slot->linearVertAdvance = 0;
606 }
607
608
609 FT_LOCAL_DEF( FT_Error )
610 T42_GlyphSlot_Load( FT_GlyphSlot glyph,
611 FT_Size size,
612 FT_UInt glyph_index,
613 FT_Int32 load_flags )
614 {
615 FT_Error error;
616 T42_GlyphSlot t42slot = (T42_GlyphSlot)glyph;
617 T42_Size t42size = (T42_Size)size;
618 FT_Driver_Class ttclazz = ((T42_Driver)glyph->face->driver)->ttclazz;
619
620
621 t42_glyphslot_clear( t42slot->ttslot );
622 error = ttclazz->load_glyph( t42slot->ttslot,
623 t42size->ttsize,
624 glyph_index,
625 load_flags | FT_LOAD_NO_BITMAP );
626
627 if ( !error )
628 {
629 glyph->metrics = t42slot->ttslot->metrics;
630
631 glyph->linearHoriAdvance = t42slot->ttslot->linearHoriAdvance;
632 glyph->linearVertAdvance = t42slot->ttslot->linearVertAdvance;
633
634 glyph->format = t42slot->ttslot->format;
635 glyph->outline = t42slot->ttslot->outline;
636
637 glyph->bitmap = t42slot->ttslot->bitmap;
638 glyph->bitmap_left = t42slot->ttslot->bitmap_left;
639 glyph->bitmap_top = t42slot->ttslot->bitmap_top;
640
641 glyph->num_subglyphs = t42slot->ttslot->num_subglyphs;
642 glyph->subglyphs = t42slot->ttslot->subglyphs;
643
644 glyph->control_data = t42slot->ttslot->control_data;
645 glyph->control_len = t42slot->ttslot->control_len;
646 }
647
648 return error;
649 }
650
651
652 /* END */