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