3 FreeType font driver for pcf files
5 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 by
6 Francesco Zappa Nardelli
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include FT_INTERNAL_DEBUG_H
31 #include FT_INTERNAL_STREAM_H
32 #include FT_INTERNAL_OBJECTS_H
37 #include FT_TRUETYPE_IDS_H
47 #define FT_COMPONENT trace_pcfread
49 #include FT_SERVICE_BDF_H
50 #include FT_SERVICE_XFREE86_NAME_H
53 /*************************************************************************/
55 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
56 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
57 /* messages during execution. */
60 #define FT_COMPONENT trace_pcfdriver
63 typedef struct PCF_CMapRec_
66 FT_UInt num_encodings;
67 PCF_Encoding encodings;
69 } PCF_CMapRec, *PCF_CMap;
72 FT_CALLBACK_DEF( FT_Error )
73 pcf_cmap_init( FT_CMap pcfcmap, /* PCF_CMap */
74 FT_Pointer init_data )
76 PCF_CMap cmap = (PCF_CMap)pcfcmap;
77 PCF_Face face = (PCF_Face)FT_CMAP_FACE( pcfcmap );
79 FT_UNUSED( init_data );
82 cmap->num_encodings = (FT_UInt)face->nencodings;
83 cmap->encodings = face->encodings;
89 FT_CALLBACK_DEF( void )
90 pcf_cmap_done( FT_CMap pcfcmap ) /* PCF_CMap */
92 PCF_CMap cmap = (PCF_CMap)pcfcmap;
95 cmap->encodings = NULL;
96 cmap->num_encodings = 0;
100 FT_CALLBACK_DEF( FT_UInt )
101 pcf_cmap_char_index( FT_CMap pcfcmap, /* PCF_CMap */
104 PCF_CMap cmap = (PCF_CMap)pcfcmap;
105 PCF_Encoding encodings = cmap->encodings;
106 FT_UInt min, max, mid;
111 max = cmap->num_encodings;
118 mid = ( min + max ) >> 1;
119 code = encodings[mid].enc;
121 if ( charcode == code )
123 result = encodings[mid].glyph + 1;
127 if ( charcode < code )
137 FT_CALLBACK_DEF( FT_UInt )
138 pcf_cmap_char_next( FT_CMap pcfcmap, /* PCF_CMap */
139 FT_UInt32 *acharcode )
141 PCF_CMap cmap = (PCF_CMap)pcfcmap;
142 PCF_Encoding encodings = cmap->encodings;
143 FT_UInt min, max, mid;
144 FT_ULong charcode = *acharcode + 1;
149 max = cmap->num_encodings;
156 mid = ( min + max ) >> 1;
157 code = encodings[mid].enc;
159 if ( charcode == code )
161 result = encodings[mid].glyph + 1;
165 if ( charcode < code )
172 if ( min < cmap->num_encodings )
174 charcode = encodings[min].enc;
175 result = encodings[min].glyph + 1;
179 if ( charcode > 0xFFFFFFFFUL )
181 FT_TRACE1(( "pcf_cmap_char_next: charcode 0x%x > 32bit API" ));
183 /* XXX: result should be changed to indicate an overflow error */
186 *acharcode = (FT_UInt32)charcode;
191 FT_CALLBACK_TABLE_DEF
192 const FT_CMap_ClassRec pcf_cmap_class =
194 sizeof ( PCF_CMapRec ),
200 NULL, NULL, NULL, NULL, NULL
204 FT_CALLBACK_DEF( void )
205 PCF_Face_Done( FT_Face pcfface ) /* PCF_Face */
207 PCF_Face face = (PCF_Face)pcfface;
214 memory = FT_FACE_MEMORY( face );
216 FT_FREE( face->encodings );
217 FT_FREE( face->metrics );
219 /* free properties */
225 if ( face->properties )
227 for ( i = 0; i < face->nprops; i++ )
229 prop = &face->properties[i];
233 FT_FREE( prop->name );
234 if ( prop->isString )
235 FT_FREE( prop->value.atom );
239 FT_FREE( face->properties );
242 FT_FREE( face->toc.tables );
243 FT_FREE( pcfface->family_name );
244 FT_FREE( pcfface->style_name );
245 FT_FREE( pcfface->available_sizes );
246 FT_FREE( face->charset_encoding );
247 FT_FREE( face->charset_registry );
249 FT_TRACE4(( "PCF_Face_Done: done face\n" ));
251 /* close gzip/LZW stream if any */
252 if ( pcfface->stream == &face->gzip_stream )
254 FT_Stream_Close( &face->gzip_stream );
255 pcfface->stream = face->gzip_source;
260 FT_CALLBACK_DEF( FT_Error )
261 PCF_Face_Init( FT_Stream stream,
262 FT_Face pcfface, /* PCF_Face */
265 FT_Parameter* params )
267 PCF_Face face = (PCF_Face)pcfface;
268 FT_Error error = PCF_Err_Ok;
270 FT_UNUSED( num_params );
272 FT_UNUSED( face_index );
275 error = pcf_load_font( stream, face );
278 PCF_Face_Done( pcfface );
280 #if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
281 defined( FT_CONFIG_OPTION_USE_LZW )
283 #ifdef FT_CONFIG_OPTION_USE_ZLIB
288 /* this didn't work, try gzip support! */
289 error2 = FT_Stream_OpenGzip( &face->gzip_stream, stream );
290 if ( FT_ERROR_BASE( error2 ) == FT_Err_Unimplemented_Feature )
295 #endif /* FT_CONFIG_OPTION_USE_ZLIB */
297 #ifdef FT_CONFIG_OPTION_USE_LZW
303 /* this didn't work, try LZW support! */
304 error3 = FT_Stream_OpenLZW( &face->gzip_stream, stream );
305 if ( FT_ERROR_BASE( error3 ) == FT_Err_Unimplemented_Feature )
310 #endif /* FT_CONFIG_OPTION_USE_LZW */
315 face->gzip_source = stream;
316 pcfface->stream = &face->gzip_stream;
318 stream = pcfface->stream;
320 error = pcf_load_font( stream, face );
324 #else /* !(FT_CONFIG_OPTION_USE_ZLIB || FT_CONFIG_OPTION_USE_LZW) */
333 FT_String *charset_registry = face->charset_registry;
334 FT_String *charset_encoding = face->charset_encoding;
335 FT_Bool unicode_charmap = 0;
338 if ( charset_registry && charset_encoding )
340 char* s = charset_registry;
343 /* Uh, oh, compare first letters manually to avoid dependency
345 if ( ( s[0] == 'i' || s[0] == 'I' ) &&
346 ( s[1] == 's' || s[1] == 'S' ) &&
347 ( s[2] == 'o' || s[2] == 'O' ) )
350 if ( !ft_strcmp( s, "10646" ) ||
351 ( !ft_strcmp( s, "8859" ) &&
352 !ft_strcmp( face->charset_encoding, "1" ) ) )
358 FT_CharMapRec charmap;
361 charmap.face = FT_FACE( face );
362 charmap.encoding = FT_ENCODING_NONE;
363 /* initial platform/encoding should indicate unset status? */
364 charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
365 charmap.encoding_id = TT_APPLE_ID_DEFAULT;
367 if ( unicode_charmap )
369 charmap.encoding = FT_ENCODING_UNICODE;
370 charmap.platform_id = TT_PLATFORM_MICROSOFT;
371 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
374 error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
377 /* Select default charmap */
378 if ( pcfface->num_charmaps )
379 pcfface->charmap = pcfface->charmaps[0];
388 FT_TRACE2(( "[not a valid PCF file]\n" ));
389 PCF_Face_Done( pcfface );
390 error = PCF_Err_Unknown_File_Format; /* error */
395 FT_CALLBACK_DEF( FT_Error )
396 PCF_Size_Select( FT_Size size,
397 FT_ULong strike_index )
399 PCF_Accel accel = &( (PCF_Face)size->face )->accel;
402 FT_Select_Metrics( size->face, strike_index );
404 size->metrics.ascender = accel->fontAscent << 6;
405 size->metrics.descender = -accel->fontDescent << 6;
406 size->metrics.max_advance = accel->maxbounds.characterWidth << 6;
412 FT_CALLBACK_DEF( FT_Error )
413 PCF_Size_Request( FT_Size size,
414 FT_Size_Request req )
416 PCF_Face face = (PCF_Face)size->face;
417 FT_Bitmap_Size* bsize = size->face->available_sizes;
418 FT_Error error = PCF_Err_Invalid_Pixel_Size;
422 height = FT_REQUEST_HEIGHT( req );
423 height = ( height + 32 ) >> 6;
427 case FT_SIZE_REQUEST_TYPE_NOMINAL:
428 if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
432 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
433 if ( height == ( face->accel.fontAscent +
434 face->accel.fontDescent ) )
439 error = PCF_Err_Unimplemented_Feature;
446 return PCF_Size_Select( size, 0 );
450 FT_CALLBACK_DEF( FT_Error )
451 PCF_Glyph_Load( FT_GlyphSlot slot,
454 FT_Int32 load_flags )
456 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
458 FT_Error error = PCF_Err_Ok;
459 FT_Bitmap* bitmap = &slot->bitmap;
463 FT_UNUSED( load_flags );
466 FT_TRACE4(( "load_glyph %d ---", glyph_index ));
468 if ( !face || glyph_index >= (FT_UInt)face->root.num_glyphs )
470 error = PCF_Err_Invalid_Argument;
474 stream = face->root.stream;
476 if ( glyph_index > 0 )
479 metric = face->metrics + glyph_index;
481 bitmap->rows = metric->ascent + metric->descent;
482 bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
483 bitmap->num_grays = 1;
484 bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
486 FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
487 PCF_BIT_ORDER( face->bitmapsFormat ),
488 PCF_BYTE_ORDER( face->bitmapsFormat ),
489 PCF_GLYPH_PAD( face->bitmapsFormat ) ));
491 switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
494 bitmap->pitch = ( bitmap->width + 7 ) >> 3;
498 bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
502 bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
506 bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
510 return PCF_Err_Invalid_File_Format;
513 /* XXX: to do: are there cases that need repadding the bitmap? */
514 bytes = bitmap->pitch * bitmap->rows;
516 error = ft_glyphslot_alloc_bitmap( slot, bytes );
520 if ( FT_STREAM_SEEK( metric->bits ) ||
521 FT_STREAM_READ( bitmap->buffer, bytes ) )
524 if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
525 BitOrderInvert( bitmap->buffer, bytes );
527 if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
528 PCF_BIT_ORDER( face->bitmapsFormat ) ) )
530 switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
536 TwoByteSwap( bitmap->buffer, bytes );
540 FourByteSwap( bitmap->buffer, bytes );
545 slot->format = FT_GLYPH_FORMAT_BITMAP;
546 slot->bitmap_left = metric->leftSideBearing;
547 slot->bitmap_top = metric->ascent;
549 slot->metrics.horiAdvance = metric->characterWidth << 6;
550 slot->metrics.horiBearingX = metric->leftSideBearing << 6;
551 slot->metrics.horiBearingY = metric->ascent << 6;
552 slot->metrics.width = ( metric->rightSideBearing -
553 metric->leftSideBearing ) << 6;
554 slot->metrics.height = bitmap->rows << 6;
556 ft_synthesize_vertical_metrics( &slot->metrics,
557 ( face->accel.fontAscent +
558 face->accel.fontDescent ) << 6 );
560 FT_TRACE4(( " --- ok\n" ));
574 pcf_get_bdf_property( PCF_Face face,
575 const char* prop_name,
576 BDF_PropertyRec *aproperty )
581 prop = pcf_find_property( face, prop_name );
584 if ( prop->isString )
586 aproperty->type = BDF_PROPERTY_TYPE_ATOM;
587 aproperty->u.atom = prop->value.atom;
591 if ( prop->value.l > 0x7FFFFFFFL || prop->value.l < ( -1 - 0x7FFFFFFFL ) )
593 FT_TRACE1(( "pcf_get_bdf_property: " ));
594 FT_TRACE1(( "too large integer 0x%x is truncated\n" ));
596 /* Apparently, the PCF driver loads all properties as signed integers!
597 * This really doesn't seem to be a problem, because this is
598 * sufficient for any meaningful values.
600 aproperty->type = BDF_PROPERTY_TYPE_INTEGER;
601 aproperty->u.integer = (FT_Int32)prop->value.l;
606 return PCF_Err_Invalid_Argument;
611 pcf_get_charset_id( PCF_Face face,
612 const char* *acharset_encoding,
613 const char* *acharset_registry )
615 *acharset_encoding = face->charset_encoding;
616 *acharset_registry = face->charset_registry;
622 static const FT_Service_BDFRec pcf_service_bdf =
624 (FT_BDF_GetCharsetIdFunc)pcf_get_charset_id,
625 (FT_BDF_GetPropertyFunc) pcf_get_bdf_property
635 static const FT_ServiceDescRec pcf_services[] =
637 { FT_SERVICE_ID_BDF, &pcf_service_bdf },
638 { FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_PCF },
643 FT_CALLBACK_DEF( FT_Module_Interface )
644 pcf_driver_requester( FT_Module module,
649 return ft_service_list_lookup( pcf_services, name );
653 FT_CALLBACK_TABLE_DEF
654 const FT_Driver_ClassRec pcf_driver_class =
657 FT_MODULE_FONT_DRIVER |
658 FT_MODULE_DRIVER_NO_OUTLINES,
659 sizeof ( FT_DriverRec ),
672 sizeof ( PCF_FaceRec ),
673 sizeof ( FT_SizeRec ),
674 sizeof ( FT_GlyphSlotRec ),
678 0, /* FT_Size_InitFunc */
679 0, /* FT_Size_DoneFunc */
680 0, /* FT_Slot_InitFunc */
681 0, /* FT_Slot_DoneFunc */
683 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
684 ft_stub_set_char_sizes,
685 ft_stub_set_pixel_sizes,
689 0, /* FT_Face_GetKerningFunc */
690 0, /* FT_Face_AttachFunc */
691 0, /* FT_Face_GetAdvancesFunc */