1 /***************************************************************************/
2 /* */
3 /* t1driver.c */
4 /* */
5 /* Type 1 driver interface (body). */
6 /* */
7 /* Copyright 1996-2001 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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 /***************************************************************************/
19 #include <ft2build.h>
20 #include "t1driver.h"
21 #include "t1gload.h"
22 #include "t1load.h"
24 #include "t1errors.h"
26 #ifndef T1_CONFIG_OPTION_NO_AFM
27 #include "t1afm.h"
28 #endif
30 #include FT_INTERNAL_DEBUG_H
31 #include FT_INTERNAL_STREAM_H
32 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
34 #include <string.h> /* for strcmp() */
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_t1driver
47 static FT_Error
48 t1_get_glyph_name( T1_Face face,
49 FT_UInt glyph_index,
50 FT_Pointer buffer,
51 FT_UInt buffer_max )
52 {
53 FT_String* gname;
56 gname = face->type1.glyph_names[glyph_index];
58 if ( buffer_max > 0 )
59 {
60 FT_UInt len = (FT_UInt)( strlen( gname ) );
63 if (len >= buffer_max)
64 len = buffer_max - 1;
66 MEM_Copy( buffer, gname, len );
67 ((FT_Byte*)buffer)[len] = 0;
68 }
70 return T1_Err_Ok;
71 }
74 /*************************************************************************/
75 /* */
76 /* <Function> */
77 /* t1_get_name_index */
78 /* */
79 /* <Description> */
80 /* Uses the Type 1 font's `glyph_names' table to find a given glyph */
81 /* name's glyph index. */
82 /* */
83 /* <Input> */
84 /* face :: A handle to the source face object. */
85 /* */
86 /* glyph_name :: The glyph name. */
87 /* */
88 /* <Return> */
89 /* Glyph index. 0 means `undefined character code'. */
90 /* */
91 static FT_UInt
92 t1_get_name_index( T1_Face face,
93 FT_String* glyph_name )
94 {
95 FT_Int i;
96 FT_String* gname;
99 for ( i = 0; i < face->type1.num_glyphs; i++ )
100 {
101 gname = face->type1.glyph_names[i];
103 if ( !strcmp( glyph_name, gname ) )
104 return (FT_UInt)i;
105 }
107 return 0;
108 }
111 static const char*
112 t1_get_ps_name( T1_Face face )
113 {
114 return (const char*) face->type1.font_name;
115 }
118 /*************************************************************************/
119 /* */
120 /* <Function> */
121 /* Get_Interface */
122 /* */
123 /* <Description> */
124 /* Each driver can provide one or more extensions to the base */
125 /* FreeType API. These can be used to access format specific */
126 /* features (e.g., all TrueType/OpenType resources share a common */
127 /* file structure and common tables which can be accessed through the */
128 /* `sfnt' interface), or more simply generic ones (e.g., the */
129 /* `postscript names' interface which can be used to retrieve the */
130 /* PostScript name of a given glyph index). */
131 /* */
132 /* <InOut> */
133 /* driver :: A handle to a driver object. */
134 /* */
135 /* <Input> */
136 /* interface :: A string designing the interface. Examples are */
137 /* `sfnt', `post_names', `charmaps', etc. */
138 /* */
139 /* <Return> */
140 /* A typeless pointer to the extension's interface (normally a table */
141 /* of function pointers). Returns NULL if the requested extension */
142 /* isn't available (i.e., wasn't compiled in the driver at build */
143 /* time). */
144 /* */
145 static FT_Module_Interface
146 Get_Interface( FT_Driver driver,
147 const FT_String* interface )
148 {
149 FT_UNUSED( driver );
150 FT_UNUSED( interface );
152 if ( strcmp( (const char*)interface, "glyph_name" ) == 0 )
153 return (FT_Module_Interface)t1_get_glyph_name;
155 if ( strcmp( (const char*)interface, "name_index" ) == 0 )
156 return (FT_Module_Interface)t1_get_name_index;
158 if ( strcmp( (const char*)interface, "postscript_name" ) == 0 )
159 return (FT_Module_Interface)t1_get_ps_name;
161 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
162 if ( strcmp( (const char*)interface, "get_mm" ) == 0 )
163 return (FT_Module_Interface)T1_Get_Multi_Master;
165 if ( strcmp( (const char*)interface, "set_mm_design") == 0 )
166 return (FT_Module_Interface)T1_Set_MM_Design;
168 if ( strcmp( (const char*)interface, "set_mm_blend") == 0 )
169 return (FT_Module_Interface)T1_Set_MM_Blend;
170 #endif
171 return 0;
172 }
175 #ifndef T1_CONFIG_OPTION_NO_AFM
177 /*************************************************************************/
178 /* */
179 /* <Function> */
180 /* Get_Kerning */
181 /* */
182 /* <Description> */
183 /* A driver method used to return the kerning vector between two */
184 /* glyphs of the same face. */
185 /* */
186 /* <Input> */
187 /* face :: A handle to the source face object. */
188 /* */
189 /* left_glyph :: The index of the left glyph in the kern pair. */
190 /* */
191 /* right_glyph :: The index of the right glyph in the kern pair. */
192 /* */
193 /* <Output> */
194 /* kerning :: The kerning vector. This is in font units for */
195 /* scalable formats, and in pixels for fixed-sizes */
196 /* formats. */
197 /* */
198 /* <Return> */
199 /* FreeType error code. 0 means success. */
200 /* */
201 /* <Note> */
202 /* Only horizontal layouts (left-to-right & right-to-left) are */
203 /* supported by this function. Other layouts, or more sophisticated */
204 /* kernings are out of scope of this method (the basic driver */
205 /* interface is meant to be simple). */
206 /* */
207 /* They can be implemented by format-specific interfaces. */
208 /* */
209 static FT_Error
210 Get_Kerning( T1_Face face,
211 FT_UInt left_glyph,
212 FT_UInt right_glyph,
213 FT_Vector* kerning )
214 {
215 T1_AFM* afm;
218 kerning->x = 0;
219 kerning->y = 0;
221 afm = (T1_AFM*)face->afm_data;
222 if ( afm )
223 T1_Get_Kerning( afm, left_glyph, right_glyph, kerning );
225 return T1_Err_Ok;
226 }
229 #endif /* T1_CONFIG_OPTION_NO_AFM */
232 /*************************************************************************/
233 /* */
234 /* <Function> */
235 /* Get_Char_Index */
236 /* */
237 /* <Description> */
238 /* Uses a charmap to return a given character code's glyph index. */
239 /* */
240 /* <Input> */
241 /* charmap :: A handle to the source charmap object. */
242 /* charcode :: The character code. */
243 /* */
244 /* <Return> */
245 /* Glyph index. 0 means `undefined character code'. */
246 /* */
247 static FT_UInt
248 Get_Char_Index( FT_CharMap charmap,
249 FT_Long charcode )
250 {
251 T1_Face face;
252 FT_UInt result = 0;
253 PSNames_Interface* psnames;
256 face = (T1_Face)charmap->face;
257 psnames = (PSNames_Interface*)face->psnames;
258 if ( psnames )
259 switch ( charmap->encoding )
260 {
261 /*******************************************************************/
262 /* */
263 /* Unicode encoding support */
264 /* */
265 case ft_encoding_unicode:
266 /* use the `PSNames' module to synthetize the Unicode charmap */
267 result = psnames->lookup_unicode( &face->unicode_map,
268 (FT_ULong)charcode );
270 /* the function returns 0xFFFF if the Unicode charcode has */
271 /* no corresponding glyph */
272 if ( result == 0xFFFF )
273 result = 0;
274 goto Exit;
276 /*******************************************************************/
277 /* */
278 /* Custom Type 1 encoding */
279 /* */
280 case ft_encoding_adobe_custom:
281 {
282 T1_Encoding* encoding = &face->type1.encoding;
285 if ( charcode >= encoding->code_first &&
286 charcode <= encoding->code_last )
287 result = encoding->char_index[charcode];
288 goto Exit;
289 }
291 /*******************************************************************/
292 /* */
293 /* Adobe Standard & Expert encoding support */
294 /* */
295 default:
296 if ( charcode < 256 )
297 {
298 FT_UInt code;
299 FT_Int n;
300 const char* glyph_name;
303 code = psnames->adobe_std_encoding[charcode];
304 if ( charmap->encoding == ft_encoding_adobe_expert )
305 code = psnames->adobe_expert_encoding[charcode];
307 glyph_name = psnames->adobe_std_strings( code );
308 if ( !glyph_name )
309 break;
311 for ( n = 0; n < face->type1.num_glyphs; n++ )
312 {
313 const char* gname = face->type1.glyph_names[n];
316 if ( gname && gname[0] == glyph_name[0] &&
317 strcmp( gname, glyph_name ) == 0 )
318 {
319 result = n;
320 break;
321 }
322 }
323 }
324 }
325 Exit:
326 return result;
327 }
330 FT_CALLBACK_TABLE_DEF
331 const FT_Driver_Class t1_driver_class =
332 {
333 {
334 ft_module_font_driver | ft_module_driver_scalable,
335 sizeof( FT_DriverRec ),
337 "type1",
338 0x10000L,
339 0x20000L,
341 0, /* format interface */
343 (FT_Module_Constructor)T1_Init_Driver,
344 (FT_Module_Destructor) T1_Done_Driver,
345 (FT_Module_Requester) Get_Interface,
346 },
348 sizeof( T1_FaceRec ),
349 sizeof( T1_SizeRec ),
350 sizeof( T1_GlyphSlotRec ),
352 (FTDriver_initFace) T1_Init_Face,
353 (FTDriver_doneFace) T1_Done_Face,
354 (FTDriver_initSize) 0,
355 (FTDriver_doneSize) 0,
356 (FTDriver_initGlyphSlot)0,
357 (FTDriver_doneGlyphSlot)0,
359 (FTDriver_setCharSizes) 0,
360 (FTDriver_setPixelSizes)0,
361 (FTDriver_loadGlyph) T1_Load_Glyph,
362 (FTDriver_getCharIndex) Get_Char_Index,
364 #ifdef T1_CONFIG_OPTION_NO_AFM
365 (FTDriver_getKerning) 0,
366 (FTDriver_attachFile) 0,
367 #else
368 (FTDriver_getKerning) Get_Kerning,
369 (FTDriver_attachFile) T1_Read_AFM,
370 #endif
371 (FTDriver_getAdvances) 0
372 };
375 #ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS
378 /*************************************************************************/
379 /* */
380 /* <Function> */
381 /* getDriverClass */
382 /* */
383 /* <Description> */
384 /* This function is used when compiling the TrueType driver as a */
385 /* shared library (`.DLL' or `.so'). It will be used by the */
386 /* high-level library of FreeType to retrieve the address of the */
387 /* driver's generic interface. */
388 /* */
389 /* It shouldn't be implemented in a static build, as each driver must */
390 /* have the same function as an exported entry point. */
391 /* */
392 /* <Return> */
393 /* The address of the TrueType's driver generic interface. The */
394 /* format-specific interface can then be retrieved through the method */
395 /* interface->get_format_interface. */
396 /* */
397 FT_EXPORT_DEF( const FT_Driver_Class* )
398 getDriverClass( void )
399 {
400 return &t1_driver_class;
401 }
404 #endif /* FT_CONFIG_OPTION_DYNAMIC_DRIVERS */
407 /* END */