1 /***************************************************************************/
5 /* FreeType PFR object methods (body). */
7 /* Copyright 2002-2008, 2010-2011, 2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
25 #include FT_INTERNAL_DEBUG_H
26 #include FT_TRUETYPE_IDS_H
31 #define FT_COMPONENT trace_pfr
34 /*************************************************************************/
35 /*************************************************************************/
37 /***** FACE OBJECT METHODS *****/
39 /*************************************************************************/
40 /*************************************************************************/
43 pfr_face_done( FT_Face pfrface
) /* PFR_Face */
45 PFR_Face face
= (PFR_Face
)pfrface
;
52 memory
= pfrface
->driver
->root
.memory
;
54 /* we don't want dangling pointers */
55 pfrface
->family_name
= NULL
;
56 pfrface
->style_name
= NULL
;
58 /* finalize the physical font record */
59 pfr_phy_font_done( &face
->phy_font
, FT_FACE_MEMORY( face
) );
61 /* no need to finalize the logical font or the header */
62 FT_FREE( pfrface
->available_sizes
);
66 FT_LOCAL_DEF( FT_Error
)
67 pfr_face_init( FT_Stream stream
,
71 FT_Parameter
* params
)
73 PFR_Face face
= (PFR_Face
)pfrface
;
76 FT_UNUSED( num_params
);
80 FT_TRACE2(( "PFR driver\n" ));
82 /* load the header and check it */
83 error
= pfr_header_load( &face
->header
, stream
);
87 if ( !pfr_header_check( &face
->header
) )
89 FT_TRACE2(( " not a PFR font\n" ));
90 error
= FT_THROW( Unknown_File_Format
);
94 /* check face index */
99 error
= pfr_log_font_count( stream
,
100 face
->header
.log_dir_offset
,
105 pfrface
->num_faces
= num_faces
;
108 if ( face_index
< 0 )
111 if ( face_index
>= pfrface
->num_faces
)
113 FT_ERROR(( "pfr_face_init: invalid face index\n" ));
114 error
= FT_THROW( Invalid_Argument
);
119 error
= pfr_log_font_load(
120 &face
->log_font
, stream
, face_index
,
121 face
->header
.log_dir_offset
,
122 FT_BOOL( face
->header
.phy_font_max_size_high
!= 0 ) );
126 /* now load the physical font descriptor */
127 error
= pfr_phy_font_load( &face
->phy_font
, stream
,
128 face
->log_font
.phys_offset
,
129 face
->log_font
.phys_size
);
133 /* now set up all root face fields */
135 PFR_PhyFont phy_font
= &face
->phy_font
;
138 pfrface
->face_index
= face_index
;
139 pfrface
->num_glyphs
= phy_font
->num_chars
+ 1;
140 pfrface
->face_flags
= FT_FACE_FLAG_SCALABLE
;
142 /* if all characters point to the same gps_offset 0, we */
143 /* assume that the font only contains bitmaps */
148 for ( nn
= 0; nn
< phy_font
->num_chars
; nn
++ )
149 if ( phy_font
->chars
[nn
].gps_offset
!= 0 )
152 if ( nn
== phy_font
->num_chars
)
154 if ( phy_font
->num_strikes
> 0 )
155 pfrface
->face_flags
= 0; /* not scalable */
158 FT_ERROR(( "pfr_face_init: font doesn't contain glyphs\n" ));
159 error
= FT_THROW( Invalid_File_Format
);
165 if ( (phy_font
->flags
& PFR_PHY_PROPORTIONAL
) == 0 )
166 pfrface
->face_flags
|= FT_FACE_FLAG_FIXED_WIDTH
;
168 if ( phy_font
->flags
& PFR_PHY_VERTICAL
)
169 pfrface
->face_flags
|= FT_FACE_FLAG_VERTICAL
;
171 pfrface
->face_flags
|= FT_FACE_FLAG_HORIZONTAL
;
173 if ( phy_font
->num_strikes
> 0 )
174 pfrface
->face_flags
|= FT_FACE_FLAG_FIXED_SIZES
;
176 if ( phy_font
->num_kern_pairs
> 0 )
177 pfrface
->face_flags
|= FT_FACE_FLAG_KERNING
;
179 /* If no family name was found in the "undocumented" auxiliary
180 * data, use the font ID instead. This sucks but is better than
183 pfrface
->family_name
= phy_font
->family_name
;
184 if ( pfrface
->family_name
== NULL
)
185 pfrface
->family_name
= phy_font
->font_id
;
187 /* note that the style name can be NULL in certain PFR fonts,
188 * probably meaning "Regular"
190 pfrface
->style_name
= phy_font
->style_name
;
192 pfrface
->num_fixed_sizes
= 0;
193 pfrface
->available_sizes
= 0;
195 pfrface
->bbox
= phy_font
->bbox
;
196 pfrface
->units_per_EM
= (FT_UShort
)phy_font
->outline_resolution
;
197 pfrface
->ascender
= (FT_Short
) phy_font
->bbox
.yMax
;
198 pfrface
->descender
= (FT_Short
) phy_font
->bbox
.yMin
;
200 pfrface
->height
= (FT_Short
)( ( pfrface
->units_per_EM
* 12 ) / 10 );
201 if ( pfrface
->height
< pfrface
->ascender
- pfrface
->descender
)
202 pfrface
->height
= (FT_Short
)(pfrface
->ascender
- pfrface
->descender
);
204 if ( phy_font
->num_strikes
> 0 )
206 FT_UInt n
, count
= phy_font
->num_strikes
;
207 FT_Bitmap_Size
* size
;
209 FT_Memory memory
= pfrface
->stream
->memory
;
212 if ( FT_NEW_ARRAY( pfrface
->available_sizes
, count
) )
215 size
= pfrface
->available_sizes
;
216 strike
= phy_font
->strikes
;
217 for ( n
= 0; n
< count
; n
++, size
++, strike
++ )
219 size
->height
= (FT_UShort
)strike
->y_ppm
;
220 size
->width
= (FT_UShort
)strike
->x_ppm
;
221 size
->size
= strike
->y_ppm
<< 6;
222 size
->x_ppem
= strike
->x_ppm
<< 6;
223 size
->y_ppem
= strike
->y_ppm
<< 6;
225 pfrface
->num_fixed_sizes
= count
;
228 /* now compute maximum advance width */
229 if ( ( phy_font
->flags
& PFR_PHY_PROPORTIONAL
) == 0 )
230 pfrface
->max_advance_width
= (FT_Short
)phy_font
->standard_advance
;
234 FT_UInt count
= phy_font
->num_chars
;
235 PFR_Char gchar
= phy_font
->chars
;
238 for ( ; count
> 0; count
--, gchar
++ )
240 if ( max
< gchar
->advance
)
241 max
= gchar
->advance
;
244 pfrface
->max_advance_width
= (FT_Short
)max
;
247 pfrface
->max_advance_height
= pfrface
->height
;
249 pfrface
->underline_position
= (FT_Short
)( -pfrface
->units_per_EM
/ 10 );
250 pfrface
->underline_thickness
= (FT_Short
)( pfrface
->units_per_EM
/ 30 );
254 FT_CharMapRec charmap
;
257 charmap
.face
= pfrface
;
258 charmap
.platform_id
= TT_PLATFORM_MICROSOFT
;
259 charmap
.encoding_id
= TT_MS_ID_UNICODE_CS
;
260 charmap
.encoding
= FT_ENCODING_UNICODE
;
262 error
= FT_CMap_New( &pfr_cmap_class_rec
, NULL
, &charmap
, NULL
);
265 /* Select default charmap */
266 if ( pfrface
->num_charmaps
)
267 pfrface
->charmap
= pfrface
->charmaps
[0];
271 /* check whether we've loaded any kerning pairs */
272 if ( phy_font
->num_kern_pairs
)
273 pfrface
->face_flags
|= FT_FACE_FLAG_KERNING
;
281 /*************************************************************************/
282 /*************************************************************************/
284 /***** SLOT OBJECT METHOD *****/
286 /*************************************************************************/
287 /*************************************************************************/
289 FT_LOCAL_DEF( FT_Error
)
290 pfr_slot_init( FT_GlyphSlot pfrslot
) /* PFR_Slot */
292 PFR_Slot slot
= (PFR_Slot
)pfrslot
;
293 FT_GlyphLoader loader
= pfrslot
->internal
->loader
;
296 pfr_glyph_init( &slot
->glyph
, loader
);
303 pfr_slot_done( FT_GlyphSlot pfrslot
) /* PFR_Slot */
305 PFR_Slot slot
= (PFR_Slot
)pfrslot
;
308 pfr_glyph_done( &slot
->glyph
);
312 FT_LOCAL_DEF( FT_Error
)
313 pfr_slot_load( FT_GlyphSlot pfrslot
, /* PFR_Slot */
314 FT_Size pfrsize
, /* PFR_Size */
316 FT_Int32 load_flags
)
318 PFR_Slot slot
= (PFR_Slot
)pfrslot
;
319 PFR_Size size
= (PFR_Size
)pfrsize
;
321 PFR_Face face
= (PFR_Face
)pfrslot
->face
;
323 FT_Outline
* outline
= &pfrslot
->outline
;
330 if ( !face
|| gindex
>= face
->phy_font
.num_chars
)
332 error
= FT_THROW( Invalid_Argument
);
336 /* try to load an embedded bitmap */
337 if ( ( load_flags
& ( FT_LOAD_NO_SCALE
| FT_LOAD_NO_BITMAP
) ) == 0 )
339 error
= pfr_slot_load_bitmap( slot
, size
, gindex
);
344 if ( load_flags
& FT_LOAD_SBITS_ONLY
)
346 error
= FT_THROW( Invalid_Argument
);
350 gchar
= face
->phy_font
.chars
+ gindex
;
351 pfrslot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
352 outline
->n_points
= 0;
353 outline
->n_contours
= 0;
354 gps_offset
= face
->header
.gps_section_offset
;
356 /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */
357 error
= pfr_glyph_load( &slot
->glyph
, face
->root
.stream
,
358 gps_offset
, gchar
->gps_offset
, gchar
->gps_size
);
363 FT_Glyph_Metrics
* metrics
= &pfrslot
->metrics
;
365 FT_Int em_metrics
, em_outline
;
369 scaling
= FT_BOOL( ( load_flags
& FT_LOAD_NO_SCALE
) == 0 );
371 /* copy outline data */
372 *outline
= slot
->glyph
.loader
->base
.outline
;
374 outline
->flags
&= ~FT_OUTLINE_OWNER
;
375 outline
->flags
|= FT_OUTLINE_REVERSE_FILL
;
377 if ( size
&& pfrsize
->metrics
.y_ppem
< 24 )
378 outline
->flags
|= FT_OUTLINE_HIGH_PRECISION
;
380 /* compute the advance vector */
381 metrics
->horiAdvance
= 0;
382 metrics
->vertAdvance
= 0;
384 advance
= gchar
->advance
;
385 em_metrics
= face
->phy_font
.metrics_resolution
;
386 em_outline
= face
->phy_font
.outline_resolution
;
388 if ( em_metrics
!= em_outline
)
389 advance
= FT_MulDiv( advance
, em_outline
, em_metrics
);
391 if ( face
->phy_font
.flags
& PFR_PHY_VERTICAL
)
392 metrics
->vertAdvance
= advance
;
394 metrics
->horiAdvance
= advance
;
396 pfrslot
->linearHoriAdvance
= metrics
->horiAdvance
;
397 pfrslot
->linearVertAdvance
= metrics
->vertAdvance
;
399 /* make-up vertical metrics(?) */
400 metrics
->vertBearingX
= 0;
401 metrics
->vertBearingY
= 0;
403 #if 0 /* some fonts seem to be broken here! */
405 /* Apply the font matrix, if any. */
406 /* TODO: Test existing fonts with unusual matrix */
407 /* whether we have to adjust Units per EM. */
409 FT_Matrix font_matrix
;
412 font_matrix
.xx
= face
->log_font
.matrix
[0] << 8;
413 font_matrix
.yx
= face
->log_font
.matrix
[1] << 8;
414 font_matrix
.xy
= face
->log_font
.matrix
[2] << 8;
415 font_matrix
.yy
= face
->log_font
.matrix
[3] << 8;
417 FT_Outline_Transform( outline
, &font_matrix
);
421 /* scale when needed */
425 FT_Fixed x_scale
= pfrsize
->metrics
.x_scale
;
426 FT_Fixed y_scale
= pfrsize
->metrics
.y_scale
;
427 FT_Vector
* vec
= outline
->points
;
430 /* scale outline points */
431 for ( n
= 0; n
< outline
->n_points
; n
++, vec
++ )
433 vec
->x
= FT_MulFix( vec
->x
, x_scale
);
434 vec
->y
= FT_MulFix( vec
->y
, y_scale
);
437 /* scale the advance */
438 metrics
->horiAdvance
= FT_MulFix( metrics
->horiAdvance
, x_scale
);
439 metrics
->vertAdvance
= FT_MulFix( metrics
->vertAdvance
, y_scale
);
442 /* compute the rest of the metrics */
443 FT_Outline_Get_CBox( outline
, &cbox
);
445 metrics
->width
= cbox
.xMax
- cbox
.xMin
;
446 metrics
->height
= cbox
.yMax
- cbox
.yMin
;
447 metrics
->horiBearingX
= cbox
.xMin
;
448 metrics
->horiBearingY
= cbox
.yMax
- metrics
->height
;
456 /*************************************************************************/
457 /*************************************************************************/
459 /***** KERNING METHOD *****/
461 /*************************************************************************/
462 /*************************************************************************/
464 FT_LOCAL_DEF( FT_Error
)
465 pfr_face_get_kerning( FT_Face pfrface
, /* PFR_Face */
470 PFR_Face face
= (PFR_Face
)pfrface
;
471 FT_Error error
= FT_Err_Ok
;
472 PFR_PhyFont phy_font
= &face
->phy_font
;
473 FT_UInt32 code1
, code2
, pair
;
485 /* convert glyph indices to character codes */
486 if ( glyph1
> phy_font
->num_chars
||
487 glyph2
> phy_font
->num_chars
)
490 code1
= phy_font
->chars
[glyph1
].char_code
;
491 code2
= phy_font
->chars
[glyph2
].char_code
;
492 pair
= PFR_KERN_INDEX( code1
, code2
);
494 /* now search the list of kerning items */
496 PFR_KernItem item
= phy_font
->kern_items
;
497 FT_Stream stream
= pfrface
->stream
;
500 for ( ; item
; item
= item
->next
)
502 if ( pair
>= item
->pair1
&& pair
<= item
->pair2
)
507 FoundPair
: /* we found an item, now parse it and find the value if any */
508 if ( FT_STREAM_SEEK( item
->offset
) ||
509 FT_FRAME_ENTER( item
->pair_count
* item
->pair_size
) )
513 FT_UInt count
= item
->pair_count
;
514 FT_UInt size
= item
->pair_size
;
515 FT_UInt power
= (FT_UInt
)ft_highpow2( (FT_UInt32
)count
);
516 FT_UInt probe
= power
* size
;
517 FT_UInt extra
= count
- power
;
518 FT_Byte
* base
= stream
->cursor
;
519 FT_Bool twobytes
= FT_BOOL( item
->flags
& 1 );
520 FT_Bool twobyte_adj
= FT_BOOL( item
->flags
& 2 );
527 p
= base
+ extra
* size
;
530 cpair
= FT_NEXT_ULONG( p
);
532 cpair
= PFR_NEXT_KPAIR( p
);
547 while ( probe
> size
)
553 cpair
= FT_NEXT_ULONG( p
);
555 cpair
= PFR_NEXT_KPAIR( p
);
567 cpair
= FT_NEXT_ULONG( p
);
569 cpair
= PFR_NEXT_KPAIR( p
);
578 value
= FT_PEEK_SHORT( p
);
582 kerning
->x
= item
->base_adj
+ value
;