update for HEAD-2003050101
[reactos.git] / lib / freetype / src / sfnt / ttpost.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ttpost.c                                                               */
4 /*                                                                         */
5 /*    Postcript name table processing for TrueType and OpenType fonts      */
6 /*    (body).                                                              */
7 /*                                                                         */
8 /*  Copyright 1996-2001, 2002 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   /*                                                                       */
21   /* The post table is not completely loaded by the core engine.  This     */
22   /* file loads the missing PS glyph names and implements an API to access */
23   /* them.                                                                 */
24   /*                                                                       */
25   /*************************************************************************/
26
27
28 #include <ft2build.h>
29 #include FT_INTERNAL_STREAM_H
30 #include FT_TRUETYPE_TAGS_H
31 #include "ttpost.h"
32 #include "ttload.h"
33
34 #include "sferrors.h"
35
36
37   /*************************************************************************/
38   /*                                                                       */
39   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
40   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
41   /* messages during execution.                                            */
42   /*                                                                       */
43 #undef  FT_COMPONENT
44 #define FT_COMPONENT  trace_ttpost
45
46
47   /* If this configuration macro is defined, we rely on the `PSNames' */
48   /* module to grab the glyph names.                                  */
49
50 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
51
52
53 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
54
55 #define MAC_NAME( x )  ( (FT_String*)psnames->macintosh_name( x ) )
56
57
58 #else /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
59
60
61    /* Otherwise, we ignore the `PSNames' module, and provide our own  */
62    /* table of Mac names.  Thus, it is possible to build a version of */
63    /* FreeType without the Type 1 driver & PSNames module.            */
64
65 #define MAC_NAME( x )  tt_post_default_names[x]
66
67   /* the 258 default Mac PS glyph names */
68
69   static const FT_String*  tt_post_default_names[258] =
70   {
71     /*   0 */
72     ".notdef", ".null", "CR", "space", "exclam",
73     "quotedbl", "numbersign", "dollar", "percent", "ampersand",
74     /*  10 */
75     "quotesingle", "parenleft", "parenright", "asterisk", "plus",
76     "comma", "hyphen", "period", "slash", "zero",
77     /*  20 */
78     "one", "two", "three", "four", "five",
79     "six", "seven", "eight", "nine", "colon",
80     /*  30 */
81     "semicolon", "less", "equal", "greater", "question",
82     "at", "A", "B", "C", "D",
83     /*  40 */
84     "E", "F", "G", "H", "I",
85     "J", "K", "L", "M", "N",
86     /*  50 */
87     "O", "P", "Q", "R", "S",
88     "T", "U", "V", "W", "X",
89     /*  60 */
90     "Y", "Z", "bracketleft", "backslash", "bracketright",
91     "asciicircum", "underscore", "grave", "a", "b",
92     /*  70 */
93     "c", "d", "e", "f", "g",
94     "h", "i", "j", "k", "l",
95     /*  80 */
96     "m", "n", "o", "p", "q",
97     "r", "s", "t", "u", "v",
98     /*  90 */
99     "w", "x", "y", "z", "braceleft",
100     "bar", "braceright", "asciitilde", "Adieresis", "Aring",
101     /* 100 */
102     "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis",
103     "aacute", "agrave", "acircumflex", "adieresis", "atilde",
104     /* 110 */
105     "aring", "ccedilla", "eacute", "egrave", "ecircumflex",
106     "edieresis", "iacute", "igrave", "icircumflex", "idieresis",
107     /* 120 */
108     "ntilde", "oacute", "ograve", "ocircumflex", "odieresis",
109     "otilde", "uacute", "ugrave", "ucircumflex", "udieresis",
110     /* 130 */
111     "dagger", "degree", "cent", "sterling", "section",
112     "bullet", "paragraph", "germandbls", "registered", "copyright",
113     /* 140 */
114     "trademark", "acute", "dieresis", "notequal", "AE",
115     "Oslash", "infinity", "plusminus", "lessequal", "greaterequal",
116     /* 150 */
117     "yen", "mu", "partialdiff", "summation", "product",
118     "pi", "integral", "ordfeminine", "ordmasculine", "Omega",
119     /* 160 */
120     "ae", "oslash", "questiondown", "exclamdown", "logicalnot",
121     "radical", "florin", "approxequal", "Delta", "guillemotleft",
122     /* 170 */
123     "guillemotright", "ellipsis", "nbspace", "Agrave", "Atilde",
124     "Otilde", "OE", "oe", "endash", "emdash",
125     /* 180 */
126     "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide",
127     "lozenge", "ydieresis", "Ydieresis", "fraction", "currency",
128     /* 190 */
129     "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl",
130     "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
131     /* 200 */
132     "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute",
133     "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex",
134     /* 210 */
135     "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave",
136     "dotlessi", "circumflex", "tilde", "macron", "breve",
137     /* 220 */
138     "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek",
139     "caron", "Lslash", "lslash", "Scaron", "scaron",
140     /* 230 */
141     "Zcaron", "zcaron", "brokenbar", "Eth", "eth",
142     "Yacute", "yacute", "Thorn", "thorn", "minus",
143     /* 240 */
144     "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf",
145     "onequarter", "threequarters", "franc", "Gbreve", "gbreve",
146     /* 250 */
147     "Idot", "Scedilla", "scedilla", "Cacute", "cacute",
148     "Ccaron", "ccaron", "dmacron",
149   };
150
151
152 #endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
153
154
155   static FT_Error
156   load_format_20( TT_Face    face,
157                   FT_Stream  stream )
158   {
159     FT_Memory   memory = stream->memory;
160     FT_Error    error;
161
162     FT_Int      num_glyphs;
163     FT_UShort   num_names;
164
165     FT_UShort*  glyph_indices = 0;
166     FT_Char**   name_strings  = 0;
167
168
169     if ( FT_READ_USHORT( num_glyphs ) )
170       goto Exit;
171
172     /* UNDOCUMENTED!  The number of glyphs in this table can be smaller */
173     /* than the value in the maxp table (cf. cyberbit.ttf).             */
174
175     /* There already exist fonts which have more than 32768 glyph names */
176     /* in this table, so the test for this threshold has been dropped.  */
177
178     if ( num_glyphs > face->root.num_glyphs )
179     {
180       error = SFNT_Err_Invalid_File_Format;
181       goto Exit;
182     }
183
184     /* load the indices */
185     {
186       FT_Int  n;
187
188
189       if ( FT_NEW_ARRAY ( glyph_indices, num_glyphs ) ||
190            FT_FRAME_ENTER( num_glyphs * 2L )          )
191         goto Fail;
192
193       for ( n = 0; n < num_glyphs; n++ )
194         glyph_indices[n] = FT_GET_USHORT();
195
196       FT_FRAME_EXIT();
197     }
198
199     /* compute number of names stored in table */
200     {
201       FT_Int  n;
202
203
204       num_names = 0;
205
206       for ( n = 0; n < num_glyphs; n++ )
207       {
208         FT_Int  idx;
209
210
211         idx = glyph_indices[n];
212         if ( idx >= 258 )
213         {
214           idx -= 257;
215           if ( idx > num_names )
216             num_names = (FT_UShort)idx;
217         }
218       }
219     }
220
221     /* now load the name strings */
222     {
223       FT_UShort  n;
224
225
226       if ( FT_NEW_ARRAY( name_strings, num_names ) )
227         goto Fail;
228
229       for ( n = 0; n < num_names; n++ )
230       {
231         FT_UInt  len;
232
233
234         if ( FT_READ_BYTE  ( len )                    ||
235              FT_NEW_ARRAY( name_strings[n], len + 1 ) ||
236              FT_STREAM_READ  ( name_strings[n], len ) )
237           goto Fail1;
238
239         name_strings[n][len] = '\0';
240       }
241     }
242
243     /* all right, set table fields and exit successfuly */
244     {
245       TT_Post_20  table = &face->postscript_names.names.format_20;
246
247
248       table->num_glyphs    = (FT_UShort)num_glyphs;
249       table->num_names     = (FT_UShort)num_names;
250       table->glyph_indices = glyph_indices;
251       table->glyph_names   = name_strings;
252     }
253     return SFNT_Err_Ok;
254
255   Fail1:
256     {
257       FT_UShort  n;
258
259
260       for ( n = 0; n < num_names; n++ )
261         FT_FREE( name_strings[n] );
262     }
263
264   Fail:
265     FT_FREE( name_strings );
266     FT_FREE( glyph_indices );
267
268   Exit:
269     return error;
270   }
271
272
273   static FT_Error
274   load_format_25( TT_Face    face,
275                   FT_Stream  stream )
276   {
277     FT_Memory  memory = stream->memory;
278     FT_Error   error;
279
280     FT_Int     num_glyphs;
281     FT_Char*   offset_table = 0;
282
283
284     /* UNDOCUMENTED!  This value appears only in the Apple TT specs. */
285     if ( FT_READ_USHORT( num_glyphs ) )
286       goto Exit;
287
288     /* check the number of glyphs */
289     if ( num_glyphs > face->root.num_glyphs || num_glyphs > 258 )
290     {
291       error = SFNT_Err_Invalid_File_Format;
292       goto Exit;
293     }
294
295     if ( FT_ALLOC( offset_table, num_glyphs )       ||
296          FT_STREAM_READ( offset_table, num_glyphs ) )
297       goto Fail;
298
299     /* now check the offset table */
300     {
301       FT_Int  n;
302
303
304       for ( n = 0; n < num_glyphs; n++ )
305       {
306         FT_Long  idx = (FT_Long)n + offset_table[n];
307
308
309         if ( idx < 0 || idx > num_glyphs )
310         {
311           error = SFNT_Err_Invalid_File_Format;
312           goto Fail;
313         }
314       }
315     }
316
317     /* OK, set table fields and exit successfuly */
318     {
319       TT_Post_25  table = &face->postscript_names.names.format_25;
320
321
322       table->num_glyphs = (FT_UShort)num_glyphs;
323       table->offsets    = offset_table;
324     }
325
326     return SFNT_Err_Ok;
327
328   Fail:
329     FT_FREE( offset_table );
330
331   Exit:
332     return error;
333   }
334
335
336   static FT_Error
337   load_post_names( TT_Face  face )
338   {
339     FT_Stream  stream;
340     FT_Error   error;
341     FT_Fixed   format;
342
343
344     /* get a stream for the face's resource */
345     stream = face->root.stream;
346
347     /* seek to the beginning of the PS names table */
348     error = face->goto_table( face, TTAG_post, stream, 0 );
349     if ( error )
350       goto Exit;
351
352     format = face->postscript.FormatType;
353
354     /* go to beginning of subtable */
355     if ( FT_STREAM_SKIP( 32 ) )
356       goto Exit;
357
358     /* now read postscript table */
359     if ( format == 0x00020000L )
360       error = load_format_20( face, stream );
361     else if ( format == 0x00028000L )
362       error = load_format_25( face, stream );
363     else
364       error = SFNT_Err_Invalid_File_Format;
365
366     face->postscript_names.loaded = 1;
367
368   Exit:
369     return error;
370   }
371
372
373   FT_LOCAL_DEF( void )
374   tt_face_free_ps_names( TT_Face  face )
375   {
376     FT_Memory      memory = face->root.memory;
377     TT_Post_Names  names  = &face->postscript_names;
378     FT_Fixed       format;
379
380
381     if ( names->loaded )
382     {
383       format = face->postscript.FormatType;
384
385       if ( format == 0x00020000L )
386       {
387         TT_Post_20  table = &names->names.format_20;
388         FT_UShort   n;
389
390
391         FT_FREE( table->glyph_indices );
392         table->num_glyphs = 0;
393
394         for ( n = 0; n < table->num_names; n++ )
395           FT_FREE( table->glyph_names[n] );
396
397         FT_FREE( table->glyph_names );
398         table->num_names = 0;
399       }
400       else if ( format == 0x00028000L )
401       {
402         TT_Post_25  table = &names->names.format_25;
403
404
405         FT_FREE( table->offsets );
406         table->num_glyphs = 0;
407       }
408     }
409     names->loaded = 0;
410   }
411
412
413   /*************************************************************************/
414   /*                                                                       */
415   /* <Function>                                                            */
416   /*    tt_face_get_ps_name                                                */
417   /*                                                                       */
418   /* <Description>                                                         */
419   /*    Gets the PostScript glyph name of a glyph.                         */
420   /*                                                                       */
421   /* <Input>                                                               */
422   /*    face   :: A handle to the parent face.                             */
423   /*                                                                       */
424   /*    idx    :: The glyph index.                                         */
425   /*                                                                       */
426   /*    PSname :: The address of a string pointer.  Will be NULL in case   */
427   /*              of error, otherwise it is a pointer to the glyph name.   */
428   /*                                                                       */
429   /*              You must not modify the returned string!                 */
430   /*                                                                       */
431   /* <Output>                                                              */
432   /*    FreeType error code.  0 means success.                             */
433   /*                                                                       */
434   FT_LOCAL_DEF( FT_Error )
435   tt_face_get_ps_name( TT_Face      face,
436                        FT_UInt      idx,
437                        FT_String**  PSname )
438   {
439     FT_Error         error;
440     TT_Post_Names    names;
441     FT_Fixed         format;
442
443 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
444     PSNames_Service  psnames;
445 #endif
446
447
448     if ( !face )
449       return SFNT_Err_Invalid_Face_Handle;
450
451     if ( idx >= (FT_UInt)face->root.num_glyphs )
452       return SFNT_Err_Invalid_Glyph_Index;
453
454 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
455     psnames = (PSNames_Service)face->psnames;
456     if ( !psnames )
457       return SFNT_Err_Unimplemented_Feature;
458 #endif
459
460     names = &face->postscript_names;
461
462     /* `.notdef' by default */
463     *PSname = MAC_NAME( 0 );
464
465     format = face->postscript.FormatType;
466
467     if ( format == 0x00010000L )
468     {
469       if ( idx < 258 )                    /* paranoid checking */
470         *PSname = MAC_NAME( idx );
471     }
472     else if ( format == 0x00020000L )
473     {
474       TT_Post_20  table = &names->names.format_20;
475
476
477       if ( !names->loaded )
478       {
479         error = load_post_names( face );
480         if ( error )
481           goto End;
482       }
483
484       if ( idx < (FT_UInt)table->num_glyphs )
485       {
486         FT_UShort  name_index = table->glyph_indices[idx];
487
488
489         if ( name_index < 258 )
490           *PSname = MAC_NAME( name_index );
491         else
492           *PSname = (FT_String*)table->glyph_names[name_index - 258];
493       }
494     }
495     else if ( format == 0x00028000L )
496     {
497       TT_Post_25  table = &names->names.format_25;
498
499
500       if ( !names->loaded )
501       {
502         error = load_post_names( face );
503         if ( error )
504           goto End;
505       }
506
507       if ( idx < (FT_UInt)table->num_glyphs )    /* paranoid checking */
508       {
509         idx    += table->offsets[idx];
510         *PSname = MAC_NAME( idx );
511       }
512     }
513     
514     /* nothing to do for format == 0x00030000L */
515
516   End:
517     return SFNT_Err_Ok;
518   }
519
520
521 /* END */