- Sync with trunk up to r46941.
[reactos.git] / lib / 3rdparty / freetype / src / pfr / pfrobjs.c
1 /***************************************************************************/
2 /* */
3 /* pfrobjs.c */
4 /* */
5 /* FreeType PFR object methods (body). */
6 /* */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007 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 "pfrobjs.h"
20 #include "pfrload.h"
21 #include "pfrgload.h"
22 #include "pfrcmap.h"
23 #include "pfrsbit.h"
24 #include FT_OUTLINE_H
25 #include FT_INTERNAL_DEBUG_H
26
27 #include "pfrerror.h"
28
29 #undef FT_COMPONENT
30 #define FT_COMPONENT trace_pfr
31
32
33 /*************************************************************************/
34 /*************************************************************************/
35 /***** *****/
36 /***** FACE OBJECT METHODS *****/
37 /***** *****/
38 /*************************************************************************/
39 /*************************************************************************/
40
41 FT_LOCAL_DEF( void )
42 pfr_face_done( FT_Face pfrface ) /* PFR_Face */
43 {
44 PFR_Face face = (PFR_Face)pfrface;
45 FT_Memory memory = pfrface->driver->root.memory;
46
47
48 /* we don't want dangling pointers */
49 pfrface->family_name = NULL;
50 pfrface->style_name = NULL;
51
52 /* finalize the physical font record */
53 pfr_phy_font_done( &face->phy_font, FT_FACE_MEMORY( face ) );
54
55 /* no need to finalize the logical font or the header */
56 FT_FREE( pfrface->available_sizes );
57 }
58
59
60 FT_LOCAL_DEF( FT_Error )
61 pfr_face_init( FT_Stream stream,
62 FT_Face pfrface,
63 FT_Int face_index,
64 FT_Int num_params,
65 FT_Parameter* params )
66 {
67 PFR_Face face = (PFR_Face)pfrface;
68 FT_Error error;
69
70 FT_UNUSED( num_params );
71 FT_UNUSED( params );
72
73
74 /* load the header and check it */
75 error = pfr_header_load( &face->header, stream );
76 if ( error )
77 goto Exit;
78
79 if ( !pfr_header_check( &face->header ) )
80 {
81 FT_TRACE4(( "pfr_face_init: not a valid PFR font\n" ));
82 error = PFR_Err_Unknown_File_Format;
83 goto Exit;
84 }
85
86 /* check face index */
87 {
88 FT_UInt num_faces;
89
90
91 error = pfr_log_font_count( stream,
92 face->header.log_dir_offset,
93 &num_faces );
94 if ( error )
95 goto Exit;
96
97 pfrface->num_faces = num_faces;
98 }
99
100 if ( face_index < 0 )
101 goto Exit;
102
103 if ( face_index >= pfrface->num_faces )
104 {
105 FT_ERROR(( "pfr_face_init: invalid face index\n" ));
106 error = PFR_Err_Invalid_Argument;
107 goto Exit;
108 }
109
110 /* load the face */
111 error = pfr_log_font_load(
112 &face->log_font, stream, face_index,
113 face->header.log_dir_offset,
114 FT_BOOL( face->header.phy_font_max_size_high != 0 ) );
115 if ( error )
116 goto Exit;
117
118 /* now load the physical font descriptor */
119 error = pfr_phy_font_load( &face->phy_font, stream,
120 face->log_font.phys_offset,
121 face->log_font.phys_size );
122 if ( error )
123 goto Exit;
124
125 /* now set up all root face fields */
126 {
127 PFR_PhyFont phy_font = &face->phy_font;
128
129
130 pfrface->face_index = face_index;
131 pfrface->num_glyphs = phy_font->num_chars + 1;
132 pfrface->face_flags = FT_FACE_FLAG_SCALABLE;
133
134 /* if all characters point to the same gps_offset 0, we */
135 /* assume that the font only contains bitmaps */
136 {
137 FT_UInt nn;
138
139
140 for ( nn = 0; nn < phy_font->num_chars; nn++ )
141 if ( phy_font->chars[nn].gps_offset != 0 )
142 break;
143
144 if ( nn == phy_font->num_chars )
145 pfrface->face_flags = 0; /* not scalable */
146 }
147
148 if ( (phy_font->flags & PFR_PHY_PROPORTIONAL) == 0 )
149 pfrface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
150
151 if ( phy_font->flags & PFR_PHY_VERTICAL )
152 pfrface->face_flags |= FT_FACE_FLAG_VERTICAL;
153 else
154 pfrface->face_flags |= FT_FACE_FLAG_HORIZONTAL;
155
156 if ( phy_font->num_strikes > 0 )
157 pfrface->face_flags |= FT_FACE_FLAG_FIXED_SIZES;
158
159 if ( phy_font->num_kern_pairs > 0 )
160 pfrface->face_flags |= FT_FACE_FLAG_KERNING;
161
162 /* If no family name was found in the "undocumented" auxiliary
163 * data, use the font ID instead. This sucks but is better than
164 * nothing.
165 */
166 pfrface->family_name = phy_font->family_name;
167 if ( pfrface->family_name == NULL )
168 pfrface->family_name = phy_font->font_id;
169
170 /* note that the style name can be NULL in certain PFR fonts,
171 * probably meaning "Regular"
172 */
173 pfrface->style_name = phy_font->style_name;
174
175 pfrface->num_fixed_sizes = 0;
176 pfrface->available_sizes = 0;
177
178 pfrface->bbox = phy_font->bbox;
179 pfrface->units_per_EM = (FT_UShort)phy_font->outline_resolution;
180 pfrface->ascender = (FT_Short) phy_font->bbox.yMax;
181 pfrface->descender = (FT_Short) phy_font->bbox.yMin;
182
183 pfrface->height = (FT_Short)( ( pfrface->units_per_EM * 12 ) / 10 );
184 if ( pfrface->height < pfrface->ascender - pfrface->descender )
185 pfrface->height = (FT_Short)(pfrface->ascender - pfrface->descender);
186
187 if ( phy_font->num_strikes > 0 )
188 {
189 FT_UInt n, count = phy_font->num_strikes;
190 FT_Bitmap_Size* size;
191 PFR_Strike strike;
192 FT_Memory memory = pfrface->stream->memory;
193
194
195 if ( FT_NEW_ARRAY( pfrface->available_sizes, count ) )
196 goto Exit;
197
198 size = pfrface->available_sizes;
199 strike = phy_font->strikes;
200 for ( n = 0; n < count; n++, size++, strike++ )
201 {
202 size->height = (FT_UShort)strike->y_ppm;
203 size->width = (FT_UShort)strike->x_ppm;
204 size->size = strike->y_ppm << 6;
205 size->x_ppem = strike->x_ppm << 6;
206 size->y_ppem = strike->y_ppm << 6;
207 }
208 pfrface->num_fixed_sizes = count;
209 }
210
211 /* now compute maximum advance width */
212 if ( ( phy_font->flags & PFR_PHY_PROPORTIONAL ) == 0 )
213 pfrface->max_advance_width = (FT_Short)phy_font->standard_advance;
214 else
215 {
216 FT_Int max = 0;
217 FT_UInt count = phy_font->num_chars;
218 PFR_Char gchar = phy_font->chars;
219
220
221 for ( ; count > 0; count--, gchar++ )
222 {
223 if ( max < gchar->advance )
224 max = gchar->advance;
225 }
226
227 pfrface->max_advance_width = (FT_Short)max;
228 }
229
230 pfrface->max_advance_height = pfrface->height;
231
232 pfrface->underline_position = (FT_Short)( -pfrface->units_per_EM / 10 );
233 pfrface->underline_thickness = (FT_Short)( pfrface->units_per_EM / 30 );
234
235 /* create charmap */
236 {
237 FT_CharMapRec charmap;
238
239
240 charmap.face = pfrface;
241 charmap.platform_id = 3;
242 charmap.encoding_id = 1;
243 charmap.encoding = FT_ENCODING_UNICODE;
244
245 FT_CMap_New( &pfr_cmap_class_rec, NULL, &charmap, NULL );
246
247 #if 0
248 /* Select default charmap */
249 if ( pfrface->num_charmaps )
250 pfrface->charmap = pfrface->charmaps[0];
251 #endif
252 }
253
254 /* check whether we've loaded any kerning pairs */
255 if ( phy_font->num_kern_pairs )
256 pfrface->face_flags |= FT_FACE_FLAG_KERNING;
257 }
258
259 Exit:
260 return error;
261 }
262
263
264 /*************************************************************************/
265 /*************************************************************************/
266 /***** *****/
267 /***** SLOT OBJECT METHOD *****/
268 /***** *****/
269 /*************************************************************************/
270 /*************************************************************************/
271
272 FT_LOCAL_DEF( FT_Error )
273 pfr_slot_init( FT_GlyphSlot pfrslot ) /* PFR_Slot */
274 {
275 PFR_Slot slot = (PFR_Slot)pfrslot;
276 FT_GlyphLoader loader = pfrslot->internal->loader;
277
278
279 pfr_glyph_init( &slot->glyph, loader );
280
281 return 0;
282 }
283
284
285 FT_LOCAL_DEF( void )
286 pfr_slot_done( FT_GlyphSlot pfrslot ) /* PFR_Slot */
287 {
288 PFR_Slot slot = (PFR_Slot)pfrslot;
289
290
291 pfr_glyph_done( &slot->glyph );
292 }
293
294
295 FT_LOCAL_DEF( FT_Error )
296 pfr_slot_load( FT_GlyphSlot pfrslot, /* PFR_Slot */
297 FT_Size pfrsize, /* PFR_Size */
298 FT_UInt gindex,
299 FT_Int32 load_flags )
300 {
301 PFR_Slot slot = (PFR_Slot)pfrslot;
302 PFR_Size size = (PFR_Size)pfrsize;
303 FT_Error error;
304 PFR_Face face = (PFR_Face)pfrslot->face;
305 PFR_Char gchar;
306 FT_Outline* outline = &pfrslot->outline;
307 FT_ULong gps_offset;
308
309
310 if ( gindex > 0 )
311 gindex--;
312
313 if ( !face || gindex >= face->phy_font.num_chars )
314 {
315 error = PFR_Err_Invalid_Argument;
316 goto Exit;
317 }
318
319 /* try to load an embedded bitmap */
320 if ( ( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP ) ) == 0 )
321 {
322 error = pfr_slot_load_bitmap( slot, size, gindex );
323 if ( error == 0 )
324 goto Exit;
325 }
326
327 if ( load_flags & FT_LOAD_SBITS_ONLY )
328 {
329 error = PFR_Err_Invalid_Argument;
330 goto Exit;
331 }
332
333 gchar = face->phy_font.chars + gindex;
334 pfrslot->format = FT_GLYPH_FORMAT_OUTLINE;
335 outline->n_points = 0;
336 outline->n_contours = 0;
337 gps_offset = face->header.gps_section_offset;
338
339 /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */
340 error = pfr_glyph_load( &slot->glyph, face->root.stream,
341 gps_offset, gchar->gps_offset, gchar->gps_size );
342
343 if ( !error )
344 {
345 FT_BBox cbox;
346 FT_Glyph_Metrics* metrics = &pfrslot->metrics;
347 FT_Pos advance;
348 FT_Int em_metrics, em_outline;
349 FT_Bool scaling;
350
351
352 scaling = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
353
354 /* copy outline data */
355 *outline = slot->glyph.loader->base.outline;
356
357 outline->flags &= ~FT_OUTLINE_OWNER;
358 outline->flags |= FT_OUTLINE_REVERSE_FILL;
359
360 if ( size && pfrsize->metrics.y_ppem < 24 )
361 outline->flags |= FT_OUTLINE_HIGH_PRECISION;
362
363 /* compute the advance vector */
364 metrics->horiAdvance = 0;
365 metrics->vertAdvance = 0;
366
367 advance = gchar->advance;
368 em_metrics = face->phy_font.metrics_resolution;
369 em_outline = face->phy_font.outline_resolution;
370
371 if ( em_metrics != em_outline )
372 advance = FT_MulDiv( advance, em_outline, em_metrics );
373
374 if ( face->phy_font.flags & PFR_PHY_VERTICAL )
375 metrics->vertAdvance = advance;
376 else
377 metrics->horiAdvance = advance;
378
379 pfrslot->linearHoriAdvance = metrics->horiAdvance;
380 pfrslot->linearVertAdvance = metrics->vertAdvance;
381
382 /* make-up vertical metrics(?) */
383 metrics->vertBearingX = 0;
384 metrics->vertBearingY = 0;
385
386 #if 0 /* some fonts seem to be broken here! */
387
388 /* Apply the font matrix, if any. */
389 /* TODO: Test existing fonts with unusual matrix */
390 /* whether we have to adjust Units per EM. */
391 {
392 FT_Matrix font_matrix;
393
394
395 font_matrix.xx = face->log_font.matrix[0] << 8;
396 font_matrix.yx = face->log_font.matrix[1] << 8;
397 font_matrix.xy = face->log_font.matrix[2] << 8;
398 font_matrix.yy = face->log_font.matrix[3] << 8;
399
400 FT_Outline_Transform( outline, &font_matrix );
401 }
402 #endif
403
404 /* scale when needed */
405 if ( scaling )
406 {
407 FT_Int n;
408 FT_Fixed x_scale = pfrsize->metrics.x_scale;
409 FT_Fixed y_scale = pfrsize->metrics.y_scale;
410 FT_Vector* vec = outline->points;
411
412
413 /* scale outline points */
414 for ( n = 0; n < outline->n_points; n++, vec++ )
415 {
416 vec->x = FT_MulFix( vec->x, x_scale );
417 vec->y = FT_MulFix( vec->y, y_scale );
418 }
419
420 /* scale the advance */
421 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
422 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
423 }
424
425 /* compute the rest of the metrics */
426 FT_Outline_Get_CBox( outline, &cbox );
427
428 metrics->width = cbox.xMax - cbox.xMin;
429 metrics->height = cbox.yMax - cbox.yMin;
430 metrics->horiBearingX = cbox.xMin;
431 metrics->horiBearingY = cbox.yMax - metrics->height;
432 }
433
434 Exit:
435 return error;
436 }
437
438
439 /*************************************************************************/
440 /*************************************************************************/
441 /***** *****/
442 /***** KERNING METHOD *****/
443 /***** *****/
444 /*************************************************************************/
445 /*************************************************************************/
446
447 FT_LOCAL_DEF( FT_Error )
448 pfr_face_get_kerning( FT_Face pfrface, /* PFR_Face */
449 FT_UInt glyph1,
450 FT_UInt glyph2,
451 FT_Vector* kerning )
452 {
453 PFR_Face face = (PFR_Face)pfrface;
454 FT_Error error = PFR_Err_Ok;
455 PFR_PhyFont phy_font = &face->phy_font;
456 FT_UInt32 code1, code2, pair;
457
458
459 kerning->x = 0;
460 kerning->y = 0;
461
462 if ( glyph1 > 0 )
463 glyph1--;
464
465 if ( glyph2 > 0 )
466 glyph2--;
467
468 /* convert glyph indices to character codes */
469 if ( glyph1 > phy_font->num_chars ||
470 glyph2 > phy_font->num_chars )
471 goto Exit;
472
473 code1 = phy_font->chars[glyph1].char_code;
474 code2 = phy_font->chars[glyph2].char_code;
475 pair = PFR_KERN_INDEX( code1, code2 );
476
477 /* now search the list of kerning items */
478 {
479 PFR_KernItem item = phy_font->kern_items;
480 FT_Stream stream = pfrface->stream;
481
482
483 for ( ; item; item = item->next )
484 {
485 if ( pair >= item->pair1 && pair <= item->pair2 )
486 goto FoundPair;
487 }
488 goto Exit;
489
490 FoundPair: /* we found an item, now parse it and find the value if any */
491 if ( FT_STREAM_SEEK( item->offset ) ||
492 FT_FRAME_ENTER( item->pair_count * item->pair_size ) )
493 goto Exit;
494
495 {
496 FT_UInt count = item->pair_count;
497 FT_UInt size = item->pair_size;
498 FT_UInt power = (FT_UInt)ft_highpow2( (FT_UInt32)count );
499 FT_UInt probe = power * size;
500 FT_UInt extra = count - power;
501 FT_Byte* base = stream->cursor;
502 FT_Bool twobytes = FT_BOOL( item->flags & 1 );
503 FT_Bool twobyte_adj = FT_BOOL( item->flags & 2 );
504 FT_Byte* p;
505 FT_UInt32 cpair;
506
507
508 if ( extra > 0 )
509 {
510 p = base + extra * size;
511
512 if ( twobytes )
513 cpair = FT_NEXT_ULONG( p );
514 else
515 cpair = PFR_NEXT_KPAIR( p );
516
517 if ( cpair == pair )
518 goto Found;
519
520 if ( cpair < pair )
521 {
522 if ( twobyte_adj )
523 p += 2;
524 else
525 p++;
526 base = p;
527 }
528 }
529
530 while ( probe > size )
531 {
532 probe >>= 1;
533 p = base + probe;
534
535 if ( twobytes )
536 cpair = FT_NEXT_ULONG( p );
537 else
538 cpair = PFR_NEXT_KPAIR( p );
539
540 if ( cpair == pair )
541 goto Found;
542
543 if ( cpair < pair )
544 base += probe;
545 }
546
547 p = base;
548
549 if ( twobytes )
550 cpair = FT_NEXT_ULONG( p );
551 else
552 cpair = PFR_NEXT_KPAIR( p );
553
554 if ( cpair == pair )
555 {
556 FT_Int value;
557
558
559 Found:
560 if ( twobyte_adj )
561 value = FT_PEEK_SHORT( p );
562 else
563 value = p[0];
564
565 kerning->x = item->base_adj + value;
566 }
567 }
568
569 FT_FRAME_EXIT();
570 }
571
572 Exit:
573 return error;
574 }
575
576 /* END */