1 /****************************************************************************
5 * FreeType PFR object methods (body).
7 * Copyright (C) 2002-2023 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.
24 #include <freetype/ftoutln.h>
25 #include <freetype/internal/ftdebug.h>
26 #include <freetype/internal/ftcalc.h>
27 #include <freetype/ttnameid.h>
32 #define FT_COMPONENT pfr
35 /*************************************************************************/
36 /*************************************************************************/
38 /***** FACE OBJECT METHODS *****/
40 /*************************************************************************/
41 /*************************************************************************/
44 pfr_face_done( FT_Face pfrface ) /* PFR_Face */
46 PFR_Face face = (PFR_Face)pfrface;
53 memory = pfrface->memory;
55 /* we don't want dangling pointers */
56 pfrface->family_name = NULL;
57 pfrface->style_name = NULL;
59 /* finalize the physical font record */
60 pfr_phy_font_done( &face->phy_font, memory );
62 /* no need to finalize the logical font or the header */
63 FT_FREE( pfrface->available_sizes );
67 FT_LOCAL_DEF( FT_Error )
68 pfr_face_init( FT_Stream stream,
72 FT_Parameter* params )
74 PFR_Face face = (PFR_Face)pfrface;
77 FT_UNUSED( num_params );
81 FT_TRACE2(( "PFR driver\n" ));
83 /* load the header and check it */
84 error = pfr_header_load( &face->header, stream );
87 FT_TRACE2(( " not a PFR font\n" ));
88 error = FT_THROW( Unknown_File_Format );
92 if ( !pfr_header_check( &face->header ) )
94 FT_TRACE2(( " not a PFR font\n" ));
95 error = FT_THROW( Unknown_File_Format );
99 /* check face index */
104 error = pfr_log_font_count( stream,
105 face->header.log_dir_offset,
110 pfrface->num_faces = num_faces;
113 if ( face_index < 0 )
116 if ( ( face_index & 0xFFFF ) >= pfrface->num_faces )
118 FT_ERROR(( "pfr_face_init: invalid face index\n" ));
119 error = FT_THROW( Invalid_Argument );
124 error = pfr_log_font_load(
127 (FT_UInt)( face_index & 0xFFFF ),
128 face->header.log_dir_offset,
129 FT_BOOL( face->header.phy_font_max_size_high ) );
133 /* load the physical font descriptor */
134 error = pfr_phy_font_load( &face->phy_font, stream,
135 face->log_font.phys_offset,
136 face->log_font.phys_size );
140 /* set up all root face fields */
142 PFR_PhyFont phy_font = &face->phy_font;
145 pfrface->face_index = face_index & 0xFFFF;
146 pfrface->num_glyphs = (FT_Long)phy_font->num_chars + 1;
148 pfrface->face_flags |= FT_FACE_FLAG_SCALABLE;
150 /* if gps_offset == 0 for all characters, we */
151 /* assume that the font only contains bitmaps */
156 for ( nn = 0; nn < phy_font->num_chars; nn++ )
157 if ( phy_font->chars[nn].gps_offset != 0 )
160 if ( nn == phy_font->num_chars )
162 if ( phy_font->num_strikes > 0 )
163 pfrface->face_flags &= ~FT_FACE_FLAG_SCALABLE;
166 FT_ERROR(( "pfr_face_init: font doesn't contain glyphs\n" ));
167 error = FT_THROW( Invalid_File_Format );
173 if ( !( phy_font->flags & PFR_PHY_PROPORTIONAL ) )
174 pfrface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
176 if ( phy_font->flags & PFR_PHY_VERTICAL )
177 pfrface->face_flags |= FT_FACE_FLAG_VERTICAL;
179 pfrface->face_flags |= FT_FACE_FLAG_HORIZONTAL;
181 if ( phy_font->num_strikes > 0 )
182 pfrface->face_flags |= FT_FACE_FLAG_FIXED_SIZES;
184 if ( phy_font->num_kern_pairs > 0 )
185 pfrface->face_flags |= FT_FACE_FLAG_KERNING;
187 /* If no family name was found in the `undocumented' auxiliary
188 * data, use the font ID instead. This sucks but is better than
191 pfrface->family_name = phy_font->family_name;
192 if ( !pfrface->family_name )
193 pfrface->family_name = phy_font->font_id;
195 /* note that the style name can be NULL in certain PFR fonts,
196 * probably meaning `Regular'
198 pfrface->style_name = phy_font->style_name;
200 pfrface->num_fixed_sizes = 0;
201 pfrface->available_sizes = NULL;
203 pfrface->bbox = phy_font->bbox;
204 pfrface->units_per_EM = (FT_UShort)phy_font->outline_resolution;
205 pfrface->ascender = (FT_Short) phy_font->bbox.yMax;
206 pfrface->descender = (FT_Short) phy_font->bbox.yMin;
208 pfrface->height = (FT_Short)( ( pfrface->units_per_EM * 12 ) / 10 );
209 if ( pfrface->height < pfrface->ascender - pfrface->descender )
210 pfrface->height = (FT_Short)( pfrface->ascender - pfrface->descender );
212 if ( phy_font->num_strikes > 0 )
214 FT_UInt n, count = phy_font->num_strikes;
215 FT_Bitmap_Size* size;
217 FT_Memory memory = pfrface->memory;
220 if ( FT_QNEW_ARRAY( pfrface->available_sizes, count ) )
223 size = pfrface->available_sizes;
224 strike = phy_font->strikes;
225 for ( n = 0; n < count; n++, size++, strike++ )
227 size->height = (FT_Short)strike->y_ppm;
228 size->width = (FT_Short)strike->x_ppm;
229 size->size = (FT_Pos)( strike->y_ppm << 6 );
230 size->x_ppem = (FT_Pos)( strike->x_ppm << 6 );
231 size->y_ppem = (FT_Pos)( strike->y_ppm << 6 );
233 pfrface->num_fixed_sizes = (FT_Int)count;
236 /* now compute maximum advance width */
237 if ( ( phy_font->flags & PFR_PHY_PROPORTIONAL ) == 0 )
238 pfrface->max_advance_width = (FT_Short)phy_font->standard_advance;
242 FT_UInt count = phy_font->num_chars;
243 PFR_Char gchar = phy_font->chars;
246 for ( ; count > 0; count--, gchar++ )
248 if ( max < gchar->advance )
249 max = gchar->advance;
252 pfrface->max_advance_width = (FT_Short)max;
255 pfrface->max_advance_height = pfrface->height;
257 pfrface->underline_position = (FT_Short)( -pfrface->units_per_EM / 10 );
258 pfrface->underline_thickness = (FT_Short)( pfrface->units_per_EM / 30 );
262 FT_CharMapRec charmap;
265 charmap.face = pfrface;
266 charmap.platform_id = TT_PLATFORM_MICROSOFT;
267 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
268 charmap.encoding = FT_ENCODING_UNICODE;
270 error = FT_CMap_New( &pfr_cmap_class_rec, NULL, &charmap, NULL );
273 /* check whether we have loaded any kerning pairs */
274 if ( phy_font->num_kern_pairs )
275 pfrface->face_flags |= FT_FACE_FLAG_KERNING;
283 /*************************************************************************/
284 /*************************************************************************/
286 /***** SLOT OBJECT METHOD *****/
288 /*************************************************************************/
289 /*************************************************************************/
291 FT_LOCAL_DEF( FT_Error )
292 pfr_slot_init( FT_GlyphSlot pfrslot ) /* PFR_Slot */
294 PFR_Slot slot = (PFR_Slot)pfrslot;
295 FT_GlyphLoader loader = pfrslot->internal->loader;
298 pfr_glyph_init( &slot->glyph, loader );
305 pfr_slot_done( FT_GlyphSlot pfrslot ) /* PFR_Slot */
307 PFR_Slot slot = (PFR_Slot)pfrslot;
310 pfr_glyph_done( &slot->glyph );
314 FT_LOCAL_DEF( FT_Error )
315 pfr_slot_load( FT_GlyphSlot pfrslot, /* PFR_Slot */
316 FT_Size pfrsize, /* PFR_Size */
318 FT_Int32 load_flags )
320 PFR_Slot slot = (PFR_Slot)pfrslot;
321 PFR_Size size = (PFR_Size)pfrsize;
323 PFR_Face face = (PFR_Face)pfrslot->face;
325 FT_Outline* outline = &pfrslot->outline;
329 FT_TRACE1(( "pfr_slot_load: glyph index %d\n", gindex ));
334 if ( !face || gindex >= face->phy_font.num_chars )
336 error = FT_THROW( Invalid_Argument );
340 /* try to load an embedded bitmap */
341 if ( !( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP ) ) )
343 error = pfr_slot_load_bitmap(
347 ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 );
352 if ( load_flags & FT_LOAD_SBITS_ONLY )
354 error = FT_THROW( Invalid_Argument );
358 gchar = face->phy_font.chars + gindex;
359 pfrslot->format = FT_GLYPH_FORMAT_OUTLINE;
360 outline->n_points = 0;
361 outline->n_contours = 0;
362 gps_offset = face->header.gps_section_offset;
364 /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */
365 error = pfr_glyph_load( &slot->glyph, face->root.stream,
366 gps_offset, gchar->gps_offset, gchar->gps_size );
371 FT_Glyph_Metrics* metrics = &pfrslot->metrics;
373 FT_UInt em_metrics, em_outline;
377 scaling = FT_BOOL( !( load_flags & FT_LOAD_NO_SCALE ) );
379 /* copy outline data */
380 *outline = slot->glyph.loader->base.outline;
382 outline->flags &= ~FT_OUTLINE_OWNER;
383 outline->flags |= FT_OUTLINE_REVERSE_FILL;
385 if ( pfrsize->metrics.y_ppem < 24 )
386 outline->flags |= FT_OUTLINE_HIGH_PRECISION;
388 /* compute the advance vector */
389 metrics->horiAdvance = 0;
390 metrics->vertAdvance = 0;
392 advance = gchar->advance;
393 em_metrics = face->phy_font.metrics_resolution;
394 em_outline = face->phy_font.outline_resolution;
396 if ( em_metrics != em_outline )
397 advance = FT_MulDiv( advance,
399 (FT_Long)em_metrics );
401 if ( face->phy_font.flags & PFR_PHY_VERTICAL )
402 metrics->vertAdvance = advance;
404 metrics->horiAdvance = advance;
406 pfrslot->linearHoriAdvance = metrics->horiAdvance;
407 pfrslot->linearVertAdvance = metrics->vertAdvance;
409 /* make up vertical metrics(?) */
410 metrics->vertBearingX = 0;
411 metrics->vertBearingY = 0;
413 #if 0 /* some fonts seem to be broken here! */
415 /* Apply the font matrix, if any. */
416 /* TODO: Test existing fonts with unusual matrix */
417 /* whether we have to adjust Units per EM. */
419 FT_Matrix font_matrix;
422 font_matrix.xx = face->log_font.matrix[0] << 8;
423 font_matrix.yx = face->log_font.matrix[1] << 8;
424 font_matrix.xy = face->log_font.matrix[2] << 8;
425 font_matrix.yy = face->log_font.matrix[3] << 8;
427 FT_Outline_Transform( outline, &font_matrix );
431 /* scale when needed */
435 FT_Fixed x_scale = pfrsize->metrics.x_scale;
436 FT_Fixed y_scale = pfrsize->metrics.y_scale;
437 FT_Vector* vec = outline->points;
440 /* scale outline points */
441 for ( n = 0; n < outline->n_points; n++, vec++ )
443 vec->x = FT_MulFix( vec->x, x_scale );
444 vec->y = FT_MulFix( vec->y, y_scale );
447 /* scale the advance */
448 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
449 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
452 /* compute the rest of the metrics */
453 FT_Outline_Get_CBox( outline, &cbox );
455 metrics->width = cbox.xMax - cbox.xMin;
456 metrics->height = cbox.yMax - cbox.yMin;
457 metrics->horiBearingX = cbox.xMin;
458 metrics->horiBearingY = cbox.yMax - metrics->height;
466 /*************************************************************************/
467 /*************************************************************************/
469 /***** KERNING METHOD *****/
471 /*************************************************************************/
472 /*************************************************************************/
474 FT_LOCAL_DEF( FT_Error )
475 pfr_face_get_kerning( FT_Face pfrface, /* PFR_Face */
480 PFR_Face face = (PFR_Face)pfrface;
481 FT_Error error = FT_Err_Ok;
482 PFR_PhyFont phy_font = &face->phy_font;
483 FT_UInt32 code1, code2, pair;
489 /* PFR indexing skips .notdef, which becomes UINT_MAX */
493 /* check the array bounds, .notdef is automatically out */
494 if ( glyph1 >= phy_font->num_chars ||
495 glyph2 >= phy_font->num_chars )
498 /* convert glyph indices to character codes */
499 code1 = phy_font->chars[glyph1].char_code;
500 code2 = phy_font->chars[glyph2].char_code;
501 pair = PFR_KERN_INDEX( code1, code2 );
503 /* now search the list of kerning items */
505 PFR_KernItem item = phy_font->kern_items;
506 FT_Stream stream = pfrface->stream;
509 for ( ; item; item = item->next )
511 if ( pair >= item->pair1 && pair <= item->pair2 )
516 FoundPair: /* we found an item, now parse it and find the value if any */
517 if ( FT_STREAM_SEEK( item->offset ) ||
518 FT_FRAME_ENTER( item->pair_count * item->pair_size ) )
522 FT_UInt count = item->pair_count;
523 FT_UInt size = item->pair_size;
524 FT_UInt power = 1 << FT_MSB( count );
525 FT_UInt probe = power * size;
526 FT_UInt extra = count - power;
527 FT_Byte* base = stream->cursor;
528 FT_Bool twobytes = FT_BOOL( item->flags & PFR_KERN_2BYTE_CHAR );
529 FT_Bool twobyte_adj = FT_BOOL( item->flags & PFR_KERN_2BYTE_ADJ );
536 p = base + extra * size;
539 cpair = FT_NEXT_ULONG( p );
541 cpair = PFR_NEXT_KPAIR( p );
556 while ( probe > size )
562 cpair = FT_NEXT_ULONG( p );
564 cpair = PFR_NEXT_KPAIR( p );
576 cpair = FT_NEXT_ULONG( p );
578 cpair = PFR_NEXT_KPAIR( p );
587 value = FT_PEEK_SHORT( p );
591 kerning->x = item->base_adj + value;