ReactOS 0.4.16-dev-2332-g4cba65d
t1afm.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * t1afm.c
4 *
5 * AFM support for Type 1 fonts (body).
6 *
7 * Copyright (C) 1996-2020 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 */
17
18
19#include "t1afm.h"
23#include "t1errors.h"
24
25
26#ifndef T1_CONFIG_OPTION_NO_AFM
27
28 /**************************************************************************
29 *
30 * The macro FT_COMPONENT is used in trace mode. It is an implicit
31 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
32 * messages during execution.
33 */
34#undef FT_COMPONENT
35#define FT_COMPONENT t1afm
36
37
38 FT_LOCAL_DEF( void )
40 AFM_FontInfo fi )
41 {
42 FT_FREE( fi->KernPairs );
43 fi->NumKernPair = 0;
44
45 FT_FREE( fi->TrackKerns );
46 fi->NumTrackKern = 0;
47
48 FT_FREE( fi );
49 }
50
51
52 /* read a glyph name and return the equivalent glyph index */
53 static FT_Int
54 t1_get_index( const char* name,
56 void* user_data )
57 {
58 T1_Font type1 = (T1_Font)user_data;
59 FT_Int n;
60
61
62 /* PS string/name length must be < 16-bit */
63 if ( len > 0xFFFFU )
64 return 0;
65
66 for ( n = 0; n < type1->num_glyphs; n++ )
67 {
68 char* gname = (char*)type1->glyph_names[n];
69
70
71 if ( gname && gname[0] == name[0] &&
72 ft_strlen( gname ) == len &&
73 ft_strncmp( gname, name, len ) == 0 )
74 return n;
75 }
76
77 return 0;
78 }
79
80
81#undef KERN_INDEX
82#define KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
83
84
85 /* compare two kerning pairs */
86 FT_CALLBACK_DEF( int )
88 const void* b )
89 {
92
93 FT_ULong index1 = KERN_INDEX( pair1->index1, pair1->index2 );
94 FT_ULong index2 = KERN_INDEX( pair2->index1, pair2->index2 );
95
96
97 if ( index1 > index2 )
98 return 1;
99 else if ( index1 < index2 )
100 return -1;
101 else
102 return 0;
103 }
104
105
106 /* parse a PFM file -- for now, only read the kerning pairs */
107 static FT_Error
110 AFM_FontInfo fi )
111 {
113 FT_Memory memory = stream->memory;
114 FT_Byte* start;
115 FT_Byte* limit;
116 FT_Byte* p;
117 AFM_KernPair kp;
118 FT_Int width_table_length;
119 FT_CharMap oldcharmap;
120 FT_CharMap charmap;
121 FT_Int n;
122
123
124 start = (FT_Byte*)stream->cursor;
125 limit = (FT_Byte*)stream->limit;
126
127 /* Figure out how long the width table is. */
128 /* This info is a little-endian short at offset 99. */
129 p = start + 99;
130 if ( p + 2 > limit )
131 {
132 error = FT_THROW( Unknown_File_Format );
133 goto Exit;
134 }
135 width_table_length = FT_PEEK_USHORT_LE( p );
136
137 p += 18 + width_table_length;
138 if ( p + 0x12 > limit || FT_PEEK_USHORT_LE( p ) < 0x12 )
139 /* extension table is probably optional */
140 goto Exit;
141
142 /* Kerning offset is 14 bytes from start of extensions table. */
143 p += 14;
144 p = start + FT_PEEK_ULONG_LE( p );
145
146 if ( p == start )
147 /* zero offset means no table */
148 goto Exit;
149
150 if ( p + 2 > limit )
151 {
152 error = FT_THROW( Unknown_File_Format );
153 goto Exit;
154 }
155
157 p += 2;
158 if ( p + 4 * fi->NumKernPair > limit )
159 {
160 error = FT_THROW( Unknown_File_Format );
161 goto Exit;
162 }
163
164 /* Actually, kerning pairs are simply optional! */
165 if ( fi->NumKernPair == 0 )
166 goto Exit;
167
168 /* allocate the pairs */
169 if ( FT_QNEW_ARRAY( fi->KernPairs, fi->NumKernPair ) )
170 goto Exit;
171
172 /* now, read each kern pair */
173 kp = fi->KernPairs;
174 limit = p + 4 * fi->NumKernPair;
175
176 /* PFM kerning data are stored by encoding rather than glyph index, */
177 /* so find the PostScript charmap of this font and install it */
178 /* temporarily. If we find no PostScript charmap, then just use */
179 /* the default and hope it is the right one. */
180 oldcharmap = t1_face->charmap;
181 charmap = NULL;
182
183 for ( n = 0; n < t1_face->num_charmaps; n++ )
184 {
185 charmap = t1_face->charmaps[n];
186 /* check against PostScript pseudo platform */
187 if ( charmap->platform_id == 7 )
188 {
189 error = FT_Set_Charmap( t1_face, charmap );
190 if ( error )
191 goto Exit;
192 break;
193 }
194 }
195
196 /* Kerning info is stored as: */
197 /* */
198 /* encoding of first glyph (1 byte) */
199 /* encoding of second glyph (1 byte) */
200 /* offset (little-endian short) */
201 for ( ; p < limit; p += 4 )
202 {
203 kp->index1 = FT_Get_Char_Index( t1_face, p[0] );
204 kp->index2 = FT_Get_Char_Index( t1_face, p[1] );
205
206 kp->x = (FT_Int)FT_PEEK_SHORT_LE(p + 2);
207 kp->y = 0;
208
209 kp++;
210 }
211
212 if ( oldcharmap )
213 error = FT_Set_Charmap( t1_face, oldcharmap );
214 if ( error )
215 goto Exit;
216
217 /* now, sort the kern pairs according to their glyph indices */
220
221 Exit:
222 if ( error )
223 {
224 FT_FREE( fi->KernPairs );
225 fi->NumKernPair = 0;
226 }
227
228 return error;
229 }
230
231
232 /* parse a metrics file -- either AFM or PFM depending on what */
233 /* it turns out to be */
237 {
238 PSAux_Service psaux;
239 FT_Memory memory = stream->memory;
241 AFM_FontInfo fi = NULL;
242 FT_Error error = FT_ERR( Unknown_File_Format );
243 T1_Face face = (T1_Face)t1_face;
244 T1_Font t1_font = &face->type1;
245
246
247 if ( face->afm_data )
248 {
249 FT_TRACE1(( "T1_Read_Metrics:"
250 " Freeing previously attached metrics data.\n" ));
252
253 face->afm_data = NULL;
254 }
255
256 if ( FT_NEW( fi ) ||
258 goto Exit;
259
260 fi->FontBBox = t1_font->font_bbox;
261 fi->Ascender = t1_font->font_bbox.yMax;
262 fi->Descender = t1_font->font_bbox.yMin;
263
264 psaux = (PSAux_Service)face->psaux;
265 if ( psaux->afm_parser_funcs )
266 {
267 error = psaux->afm_parser_funcs->init( &parser,
268 stream->memory,
269 stream->cursor,
270 stream->limit );
271
272 if ( !error )
273 {
274 parser.FontInfo = fi;
275 parser.get_index = t1_get_index;
276 parser.user_data = t1_font;
277
278 error = psaux->afm_parser_funcs->parse( &parser );
279 psaux->afm_parser_funcs->done( &parser );
280 }
281 }
282
283 if ( FT_ERR_EQ( error, Unknown_File_Format ) )
284 {
285 FT_Byte* start = stream->cursor;
286
287
288 /* MS Windows allows versions up to 0x3FF without complaining */
289 if ( stream->size > 6 &&
290 start[1] < 4 &&
291 FT_PEEK_ULONG_LE( start + 2 ) == stream->size )
292 error = T1_Read_PFM( t1_face, stream, fi );
293 }
294
295 if ( !error )
296 {
297 t1_font->font_bbox = fi->FontBBox;
298
299 t1_face->bbox.xMin = fi->FontBBox.xMin >> 16;
300 t1_face->bbox.yMin = fi->FontBBox.yMin >> 16;
301 /* no `U' suffix here to 0xFFFF! */
302 t1_face->bbox.xMax = ( fi->FontBBox.xMax + 0xFFFF ) >> 16;
303 t1_face->bbox.yMax = ( fi->FontBBox.yMax + 0xFFFF ) >> 16;
304
305 /* no `U' suffix here to 0x8000! */
306 t1_face->ascender = (FT_Short)( ( fi->Ascender + 0x8000 ) >> 16 );
307 t1_face->descender = (FT_Short)( ( fi->Descender + 0x8000 ) >> 16 );
308
309 if ( fi->NumKernPair )
310 {
311 t1_face->face_flags |= FT_FACE_FLAG_KERNING;
312 face->afm_data = fi;
313 fi = NULL;
314 }
315 }
316
318
319 Exit:
320 if ( fi )
321 T1_Done_Metrics( memory, fi );
322
323 return error;
324 }
325
326
327 /* find the kerning for a given glyph pair */
328 FT_LOCAL_DEF( void )
330 FT_UInt glyph1,
331 FT_UInt glyph2,
333 {
334 AFM_KernPair min, mid, max;
335 FT_ULong idx = KERN_INDEX( glyph1, glyph2 );
336
337
338 /* simple binary search */
339 min = fi->KernPairs;
340 max = min + fi->NumKernPair - 1;
341
342 while ( min <= max )
343 {
344 FT_ULong midi;
345
346
347 mid = min + ( max - min ) / 2;
348 midi = KERN_INDEX( mid->index1, mid->index2 );
349
350 if ( midi == idx )
351 {
352 kerning->x = mid->x;
353 kerning->y = mid->y;
354
355 return;
356 }
357
358 if ( midi < idx )
359 min = mid + 1;
360 else
361 max = mid - 1;
362 }
363
364 kerning->x = 0;
365 kerning->y = 0;
366 }
367
368
371 FT_Fixed ptsize,
372 FT_Int degree,
374 {
375 AFM_FontInfo fi = (AFM_FontInfo)( (T1_Face)face )->afm_data;
376 FT_UInt i;
377
378
379 if ( !fi )
380 return FT_THROW( Invalid_Argument );
381
382 for ( i = 0; i < fi->NumTrackKern; i++ )
383 {
384 AFM_TrackKern tk = fi->TrackKerns + i;
385
386
387 if ( tk->degree != degree )
388 continue;
389
390 if ( ptsize < tk->min_ptsize )
391 *kerning = tk->min_kern;
392 else if ( ptsize > tk->max_ptsize )
393 *kerning = tk->max_kern;
394 else
395 {
396 *kerning = FT_MulDiv( ptsize - tk->min_ptsize,
397 tk->max_kern - tk->min_kern,
398 tk->max_ptsize - tk->min_ptsize ) +
399 tk->min_kern;
400 }
401 }
402
403 return FT_Err_Ok;
404 }
405
406#else /* T1_CONFIG_OPTION_NO_AFM */
407
408 /* ANSI C doesn't like empty source files */
409 typedef int _t1_afm_dummy;
410
411#endif /* T1_CONFIG_OPTION_NO_AFM */
412
413
414/* END */
#define FT_CALLBACK_DEF(x)
#define FT_LOCAL_DEF(x)
#define NULL
Definition: types.h:112
WORD face[3]
Definition: mesh.c:4747
unsigned int idx
Definition: utils.c:41
static void * user_data
Definition: metahost.c:106
#define FT_FACE_FLAG_KERNING
Definition: freetype.h:1203
FT_Get_Char_Index(FT_Face face, FT_ULong charcode)
Definition: ftobjs.c:3731
FT_Set_Charmap(FT_Face face, FT_CharMap charmap)
Definition: ftobjs.c:3564
FT_MulDiv(FT_Long a, FT_Long b, FT_Long c)
Definition: ftcalc.c:415
return FT_Err_Ok
Definition: ftbbox.c:526
#define FT_THROW(e)
Definition: ftdebug.h:243
#define FT_TRACE1(varformat)
Definition: ftdebug.h:188
#define FT_NEW(ptr)
Definition: ftmemory.h:339
#define FT_FREE(ptr)
Definition: ftmemory.h:337
#define FT_QNEW_ARRAY(ptr, count)
Definition: ftmemory.h:350
#define ft_strncmp
Definition: ftstdlib.h:89
#define ft_strlen
Definition: ftstdlib.h:88
#define ft_qsort
Definition: ftstdlib.h:122
#define FT_FRAME_ENTER(size)
Definition: ftstream.h:548
#define FT_PEEK_SHORT_LE(p)
Definition: ftstream.h:207
#define FT_PEEK_USHORT_LE(p)
Definition: ftstream.h:210
#define FT_FRAME_EXIT()
Definition: ftstream.h:553
#define FT_PEEK_ULONG_LE(p)
Definition: ftstream.h:218
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:64
unsigned long FT_ULong
Definition: fttypes.h:253
unsigned char FT_Byte
Definition: fttypes.h:154
#define FT_ERR_EQ(x, e)
Definition: fttypes.h:604
signed long FT_Fixed
Definition: fttypes.h:287
int FT_Error
Definition: fttypes.h:299
#define FT_ERR(e)
Definition: fttypes.h:599
signed short FT_Short
Definition: fttypes.h:198
unsigned int FT_UInt
Definition: fttypes.h:231
size_t FT_Offset
Definition: fttypes.h:323
signed int FT_Int
Definition: fttypes.h:220
GLuint start
Definition: gl.h:1545
GLdouble n
Definition: glext.h:7729
GLenum GLuint GLint GLenum face
Definition: glext.h:7025
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLint limit
Definition: glext.h:10326
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
if(dx< 0)
Definition: linetemp.h:194
#define error(str)
Definition: mkdosfs.c:1605
static char memory[1024 *256]
Definition: process.c:122
#define min(a, b)
Definition: monoChain.cc:55
struct PSAux_ServiceRec_ * PSAux_Service
static void Exit(void)
Definition: sock.c:1330
FT_Fixed Ascender
Definition: t1types.h:175
AFM_KernPair KernPairs
Definition: t1types.h:179
FT_Fixed Descender
Definition: t1types.h:176
FT_UInt NumKernPair
Definition: t1types.h:180
AFM_TrackKern TrackKerns
Definition: t1types.h:177
FT_UInt NumTrackKern
Definition: t1types.h:178
FT_BBox FontBBox
Definition: t1types.h:174
FT_UInt index2
Definition: t1types.h:165
FT_UInt index1
Definition: t1types.h:164
FT_Error(* init)(AFM_Parser parser, FT_Memory memory, FT_Byte *base, FT_Byte *limit)
Definition: psaux.h:1261
FT_Error(* parse)(AFM_Parser parser)
Definition: psaux.h:1270
void(* done)(AFM_Parser parser)
Definition: psaux.h:1267
FT_Fixed max_ptsize
Definition: t1types.h:157
FT_Fixed min_ptsize
Definition: t1types.h:155
FT_Fixed max_kern
Definition: t1types.h:158
FT_Fixed min_kern
Definition: t1types.h:156
FT_Pos xMin
Definition: ftimage.h:120
FT_Pos yMax
Definition: ftimage.h:121
FT_Pos yMin
Definition: ftimage.h:120
FT_Pos xMax
Definition: ftimage.h:121
FT_UShort platform_id
Definition: freetype.h:821
FT_Int num_charmaps
Definition: freetype.h:1044
FT_CharMap * charmaps
Definition: freetype.h:1045
FT_CharMap charmap
Definition: freetype.h:1067
FT_Pos x
Definition: ftimage.h:77
FT_Pos y
Definition: ftimage.h:78
const AFM_Parser_FuncsRec * afm_parser_funcs
Definition: psaux.h:1373
FT_Int num_glyphs
Definition: t1types.h:115
FT_String ** glyph_names
Definition: t1types.h:116
FT_BBox font_bbox
Definition: t1types.h:124
Definition: name.c:39
Definition: import.c:81
Definition: parse.h:23
unsigned int size
Definition: parse.h:27
#define max(a, b)
Definition: svc.c:63
static FT_Error T1_Read_PFM(FT_Face t1_face, FT_Stream stream, AFM_FontInfo fi)
Definition: t1afm.c:108
T1_Get_Kerning(AFM_FontInfo fi, FT_UInt glyph1, FT_UInt glyph2, FT_Vector *kerning)
Definition: t1afm.c:329
T1_Read_Metrics(FT_Face t1_face, FT_Stream stream)
Definition: t1afm.c:235
compare_kern_pairs(const void *a, const void *b)
Definition: t1afm.c:87
static FT_Int t1_get_index(const char *name, FT_Offset len, void *user_data)
Definition: t1afm.c:54
T1_Done_Metrics(FT_Memory memory, AFM_FontInfo fi)
Definition: t1afm.c:39
#define KERN_INDEX(g1, g2)
Definition: t1afm.c:82
T1_Get_Track_Kerning(FT_Face face, FT_Fixed ptsize, FT_Int degree, FT_Fixed *kerning)
Definition: t1afm.c:370
struct T1_FontRec_ * T1_Font
struct AFM_FontInfoRec_ * AFM_FontInfo
struct T1_FaceRec_ * T1_Face
Definition: t1types.h:198
struct AFM_KernPairRec_ * AFM_KernPair
FT_UInt FT_UInt FT_Vector * kerning
Definition: ttdriver.c:206
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
#define const
Definition: zconf.h:233