3 FreeType font driver for pcf files
5 Copyright (C) 2000-2004, 2006-2011 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
38 #include FT_TRUETYPE_IDS_H
48 #define FT_COMPONENT trace_pcfread
50 #include FT_SERVICE_BDF_H
51 #include FT_SERVICE_XFREE86_NAME_H
54 /*************************************************************************/
56 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
57 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
58 /* messages during execution. */
61 #define FT_COMPONENT trace_pcfdriver
64 typedef struct PCF_CMapRec_
67 FT_UInt num_encodings;
68 PCF_Encoding encodings;
70 } PCF_CMapRec, *PCF_CMap;
73 FT_CALLBACK_DEF( FT_Error )
74 pcf_cmap_init( FT_CMap pcfcmap, /* PCF_CMap */
75 FT_Pointer init_data )
77 PCF_CMap cmap = (PCF_CMap)pcfcmap;
78 PCF_Face face = (PCF_Face)FT_CMAP_FACE( pcfcmap );
80 FT_UNUSED( init_data );
83 cmap->num_encodings = (FT_UInt)face->nencodings;
84 cmap->encodings = face->encodings;
90 FT_CALLBACK_DEF( void )
91 pcf_cmap_done( FT_CMap pcfcmap ) /* PCF_CMap */
93 PCF_CMap cmap = (PCF_CMap)pcfcmap;
96 cmap->encodings = NULL;
97 cmap->num_encodings = 0;
101 FT_CALLBACK_DEF( FT_UInt )
102 pcf_cmap_char_index( FT_CMap pcfcmap, /* PCF_CMap */
105 PCF_CMap cmap = (PCF_CMap)pcfcmap;
106 PCF_Encoding encodings = cmap->encodings;
107 FT_UInt min, max, mid;
112 max = cmap->num_encodings;
119 mid = ( min + max ) >> 1;
120 code = encodings[mid].enc;
122 if ( charcode == code )
124 result = encodings[mid].glyph + 1;
128 if ( charcode < code )
138 FT_CALLBACK_DEF( FT_UInt )
139 pcf_cmap_char_next( FT_CMap pcfcmap, /* PCF_CMap */
140 FT_UInt32 *acharcode )
142 PCF_CMap cmap = (PCF_CMap)pcfcmap;
143 PCF_Encoding encodings = cmap->encodings;
144 FT_UInt min, max, mid;
145 FT_ULong charcode = *acharcode + 1;
150 max = cmap->num_encodings;
157 mid = ( min + max ) >> 1;
158 code = encodings[mid].enc;
160 if ( charcode == code )
162 result = encodings[mid].glyph + 1;
166 if ( charcode < code )
173 if ( min < cmap->num_encodings )
175 charcode = encodings[min].enc;
176 result = encodings[min].glyph + 1;
180 if ( charcode > 0xFFFFFFFFUL )
182 FT_TRACE1(( "pcf_cmap_char_next: charcode 0x%x > 32bit API" ));
184 /* XXX: result should be changed to indicate an overflow error */
187 *acharcode = (FT_UInt32)charcode;
192 FT_CALLBACK_TABLE_DEF
193 const FT_CMap_ClassRec pcf_cmap_class =
195 sizeof ( PCF_CMapRec ),
201 NULL, NULL, NULL, NULL, NULL
205 FT_CALLBACK_DEF( void )
206 PCF_Face_Done( FT_Face pcfface ) /* PCF_Face */
208 PCF_Face face = (PCF_Face)pcfface;
215 memory = FT_FACE_MEMORY( face );
217 FT_FREE( face->encodings );
218 FT_FREE( face->metrics );
220 /* free properties */
226 if ( face->properties )
228 for ( i = 0; i < face->nprops; i++ )
230 prop = &face->properties[i];
234 FT_FREE( prop->name );
235 if ( prop->isString )
236 FT_FREE( prop->value.atom );
240 FT_FREE( face->properties );
243 FT_FREE( face->toc.tables );
244 FT_FREE( pcfface->family_name );
245 FT_FREE( pcfface->style_name );
246 FT_FREE( pcfface->available_sizes );
247 FT_FREE( face->charset_encoding );
248 FT_FREE( face->charset_registry );
250 /* close compressed stream if any */
251 if ( pcfface->stream == &face->comp_stream )
253 FT_Stream_Close( &face->comp_stream );
254 pcfface->stream = face->comp_source;
259 FT_CALLBACK_DEF( FT_Error )
260 PCF_Face_Init( FT_Stream stream,
261 FT_Face pcfface, /* PCF_Face */
264 FT_Parameter* params )
266 PCF_Face face = (PCF_Face)pcfface;
267 FT_Error error = PCF_Err_Ok;
269 FT_UNUSED( num_params );
271 FT_UNUSED( face_index );
274 FT_TRACE2(( "PCF driver\n" ));
276 error = pcf_load_font( stream, face );
279 PCF_Face_Done( pcfface );
281 #if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
282 defined( FT_CONFIG_OPTION_USE_LZW ) || \
283 defined( FT_CONFIG_OPTION_USE_BZIP2 )
285 #ifdef FT_CONFIG_OPTION_USE_ZLIB
290 /* this didn't work, try gzip support! */
291 error2 = FT_Stream_OpenGzip( &face->comp_stream, stream );
292 if ( FT_ERROR_BASE( error2 ) == FT_Err_Unimplemented_Feature )
297 #endif /* FT_CONFIG_OPTION_USE_ZLIB */
299 #ifdef FT_CONFIG_OPTION_USE_LZW
305 /* this didn't work, try LZW support! */
306 error3 = FT_Stream_OpenLZW( &face->comp_stream, stream );
307 if ( FT_ERROR_BASE( error3 ) == FT_Err_Unimplemented_Feature )
312 #endif /* FT_CONFIG_OPTION_USE_LZW */
314 #ifdef FT_CONFIG_OPTION_USE_BZIP2
320 /* this didn't work, try Bzip2 support! */
321 error4 = FT_Stream_OpenBzip2( &face->comp_stream, stream );
322 if ( FT_ERROR_BASE( error4 ) == FT_Err_Unimplemented_Feature )
327 #endif /* FT_CONFIG_OPTION_USE_BZIP2 */
332 face->comp_source = stream;
333 pcfface->stream = &face->comp_stream;
335 stream = pcfface->stream;
337 error = pcf_load_font( stream, face );
341 #else /* !(FT_CONFIG_OPTION_USE_ZLIB ||
342 FT_CONFIG_OPTION_USE_LZW ||
343 FT_CONFIG_OPTION_USE_BZIP2) */
352 FT_String *charset_registry = face->charset_registry;
353 FT_String *charset_encoding = face->charset_encoding;
354 FT_Bool unicode_charmap = 0;
357 if ( charset_registry && charset_encoding )
359 char* s = charset_registry;
362 /* Uh, oh, compare first letters manually to avoid dependency
364 if ( ( s[0] == 'i' || s[0] == 'I' ) &&
365 ( s[1] == 's' || s[1] == 'S' ) &&
366 ( s[2] == 'o' || s[2] == 'O' ) )
369 if ( !ft_strcmp( s, "10646" ) ||
370 ( !ft_strcmp( s, "8859" ) &&
371 !ft_strcmp( face->charset_encoding, "1" ) ) )
377 FT_CharMapRec charmap;
380 charmap.face = FT_FACE( face );
381 charmap.encoding = FT_ENCODING_NONE;
382 /* initial platform/encoding should indicate unset status? */
383 charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
384 charmap.encoding_id = TT_APPLE_ID_DEFAULT;
386 if ( unicode_charmap )
388 charmap.encoding = FT_ENCODING_UNICODE;
389 charmap.platform_id = TT_PLATFORM_MICROSOFT;
390 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
393 error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
396 /* Select default charmap */
397 if ( pcfface->num_charmaps )
398 pcfface->charmap = pcfface->charmaps[0];
407 FT_TRACE2(( " not a PCF file\n" ));
408 PCF_Face_Done( pcfface );
409 error = PCF_Err_Unknown_File_Format; /* error */
414 FT_CALLBACK_DEF( FT_Error )
415 PCF_Size_Select( FT_Size size,
416 FT_ULong strike_index )
418 PCF_Accel accel = &( (PCF_Face)size->face )->accel;
421 FT_Select_Metrics( size->face, strike_index );
423 size->metrics.ascender = accel->fontAscent << 6;
424 size->metrics.descender = -accel->fontDescent << 6;
425 size->metrics.max_advance = accel->maxbounds.characterWidth << 6;
431 FT_CALLBACK_DEF( FT_Error )
432 PCF_Size_Request( FT_Size size,
433 FT_Size_Request req )
435 PCF_Face face = (PCF_Face)size->face;
436 FT_Bitmap_Size* bsize = size->face->available_sizes;
437 FT_Error error = PCF_Err_Invalid_Pixel_Size;
441 height = FT_REQUEST_HEIGHT( req );
442 height = ( height + 32 ) >> 6;
446 case FT_SIZE_REQUEST_TYPE_NOMINAL:
447 if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
451 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
452 if ( height == ( face->accel.fontAscent +
453 face->accel.fontDescent ) )
458 error = PCF_Err_Unimplemented_Feature;
465 return PCF_Size_Select( size, 0 );
469 FT_CALLBACK_DEF( FT_Error )
470 PCF_Glyph_Load( FT_GlyphSlot slot,
473 FT_Int32 load_flags )
475 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
477 FT_Error error = PCF_Err_Ok;
478 FT_Bitmap* bitmap = &slot->bitmap;
482 FT_UNUSED( load_flags );
485 FT_TRACE4(( "load_glyph %d ---", glyph_index ));
487 if ( !face || glyph_index >= (FT_UInt)face->root.num_glyphs )
489 error = PCF_Err_Invalid_Argument;
493 stream = face->root.stream;
495 if ( glyph_index > 0 )
498 metric = face->metrics + glyph_index;
500 bitmap->rows = metric->ascent + metric->descent;
501 bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
502 bitmap->num_grays = 1;
503 bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
505 FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
506 PCF_BIT_ORDER( face->bitmapsFormat ),
507 PCF_BYTE_ORDER( face->bitmapsFormat ),
508 PCF_GLYPH_PAD( face->bitmapsFormat ) ));
510 switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
513 bitmap->pitch = ( bitmap->width + 7 ) >> 3;
517 bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
521 bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
525 bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
529 return PCF_Err_Invalid_File_Format;
532 /* XXX: to do: are there cases that need repadding the bitmap? */
533 bytes = bitmap->pitch * bitmap->rows;
535 error = ft_glyphslot_alloc_bitmap( slot, bytes );
539 if ( FT_STREAM_SEEK( metric->bits ) ||
540 FT_STREAM_READ( bitmap->buffer, bytes ) )
543 if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
544 BitOrderInvert( bitmap->buffer, bytes );
546 if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
547 PCF_BIT_ORDER( face->bitmapsFormat ) ) )
549 switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
555 TwoByteSwap( bitmap->buffer, bytes );
559 FourByteSwap( bitmap->buffer, bytes );
564 slot->format = FT_GLYPH_FORMAT_BITMAP;
565 slot->bitmap_left = metric->leftSideBearing;
566 slot->bitmap_top = metric->ascent;
568 slot->metrics.horiAdvance = metric->characterWidth << 6;
569 slot->metrics.horiBearingX = metric->leftSideBearing << 6;
570 slot->metrics.horiBearingY = metric->ascent << 6;
571 slot->metrics.width = ( metric->rightSideBearing -
572 metric->leftSideBearing ) << 6;
573 slot->metrics.height = bitmap->rows << 6;
575 ft_synthesize_vertical_metrics( &slot->metrics,
576 ( face->accel.fontAscent +
577 face->accel.fontDescent ) << 6 );
579 FT_TRACE4(( " --- ok\n" ));
593 pcf_get_bdf_property( PCF_Face face,
594 const char* prop_name,
595 BDF_PropertyRec *aproperty )
600 prop = pcf_find_property( face, prop_name );
603 if ( prop->isString )
605 aproperty->type = BDF_PROPERTY_TYPE_ATOM;
606 aproperty->u.atom = prop->value.atom;
610 if ( prop->value.l > 0x7FFFFFFFL || prop->value.l < ( -1 - 0x7FFFFFFFL ) )
612 FT_TRACE1(( "pcf_get_bdf_property: " ));
613 FT_TRACE1(( "too large integer 0x%x is truncated\n" ));
615 /* Apparently, the PCF driver loads all properties as signed integers!
616 * This really doesn't seem to be a problem, because this is
617 * sufficient for any meaningful values.
619 aproperty->type = BDF_PROPERTY_TYPE_INTEGER;
620 aproperty->u.integer = (FT_Int32)prop->value.l;
625 return PCF_Err_Invalid_Argument;
630 pcf_get_charset_id( PCF_Face face,
631 const char* *acharset_encoding,
632 const char* *acharset_registry )
634 *acharset_encoding = face->charset_encoding;
635 *acharset_registry = face->charset_registry;
641 static const FT_Service_BDFRec pcf_service_bdf =
643 (FT_BDF_GetCharsetIdFunc)pcf_get_charset_id,
644 (FT_BDF_GetPropertyFunc) pcf_get_bdf_property
654 static const FT_ServiceDescRec pcf_services[] =
656 { FT_SERVICE_ID_BDF, &pcf_service_bdf },
657 { FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_PCF },
662 FT_CALLBACK_DEF( FT_Module_Interface )
663 pcf_driver_requester( FT_Module module,
668 return ft_service_list_lookup( pcf_services, name );
672 FT_CALLBACK_TABLE_DEF
673 const FT_Driver_ClassRec pcf_driver_class =
676 FT_MODULE_FONT_DRIVER |
677 FT_MODULE_DRIVER_NO_OUTLINES,
678 sizeof ( FT_DriverRec ),
686 0, /* FT_Module_Constructor */
687 0, /* FT_Module_Destructor */
691 sizeof ( PCF_FaceRec ),
692 sizeof ( FT_SizeRec ),
693 sizeof ( FT_GlyphSlotRec ),
697 0, /* FT_Size_InitFunc */
698 0, /* FT_Size_DoneFunc */
699 0, /* FT_Slot_InitFunc */
700 0, /* FT_Slot_DoneFunc */
702 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
703 ft_stub_set_char_sizes,
704 ft_stub_set_pixel_sizes,
708 0, /* FT_Face_GetKerningFunc */
709 0, /* FT_Face_AttachFunc */
710 0, /* FT_Face_GetAdvancesFunc */