3 FreeType font driver for pcf files
5 Copyright (C) 2000-2004, 2006-2011, 2013, 2014 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;
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 */
221 if ( face->properties )
226 for ( i = 0; i < face->nprops; i++ )
228 PCF_Property 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 /* close compressed stream if any */
250 if ( pcfface->stream == &face->comp_stream )
252 FT_Stream_Close( &face->comp_stream );
253 pcfface->stream = face->comp_source;
258 FT_CALLBACK_DEF( FT_Error )
259 PCF_Face_Init( FT_Stream stream,
260 FT_Face pcfface, /* PCF_Face */
263 FT_Parameter* params )
265 PCF_Face face = (PCF_Face)pcfface;
268 FT_UNUSED( num_params );
272 FT_TRACE2(( "PCF driver\n" ));
274 error = pcf_load_font( stream, face );
277 PCF_Face_Done( pcfface );
279 #if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
280 defined( FT_CONFIG_OPTION_USE_LZW ) || \
281 defined( FT_CONFIG_OPTION_USE_BZIP2 )
283 #ifdef FT_CONFIG_OPTION_USE_ZLIB
288 /* this didn't work, try gzip support! */
289 error2 = FT_Stream_OpenGzip( &face->comp_stream, stream );
290 if ( FT_ERR_EQ( error2, 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->comp_stream, stream );
305 if ( FT_ERR_EQ( error3, Unimplemented_Feature ) )
310 #endif /* FT_CONFIG_OPTION_USE_LZW */
312 #ifdef FT_CONFIG_OPTION_USE_BZIP2
318 /* this didn't work, try Bzip2 support! */
319 error4 = FT_Stream_OpenBzip2( &face->comp_stream, stream );
320 if ( FT_ERR_EQ( error4, Unimplemented_Feature ) )
325 #endif /* FT_CONFIG_OPTION_USE_BZIP2 */
330 face->comp_source = stream;
331 pcfface->stream = &face->comp_stream;
333 stream = pcfface->stream;
335 error = pcf_load_font( stream, face );
339 #else /* !(FT_CONFIG_OPTION_USE_ZLIB ||
340 FT_CONFIG_OPTION_USE_LZW ||
341 FT_CONFIG_OPTION_USE_BZIP2) */
348 /* PCF could not have multiple face in single font file.
349 * XXX: non-zero face_index is already invalid argument, but
350 * Type1, Type42 driver has a convention to return
351 * an invalid argument error when the font could be
352 * opened by the specified driver.
354 if ( face_index > 0 ) {
355 FT_ERROR(( "PCF_Face_Init: invalid face index\n" ));
356 PCF_Face_Done( pcfface );
357 return FT_THROW( Invalid_Argument );
362 FT_String *charset_registry = face->charset_registry;
363 FT_String *charset_encoding = face->charset_encoding;
364 FT_Bool unicode_charmap = 0;
367 if ( charset_registry && charset_encoding )
369 char* s = charset_registry;
372 /* Uh, oh, compare first letters manually to avoid dependency
374 if ( ( s[0] == 'i' || s[0] == 'I' ) &&
375 ( s[1] == 's' || s[1] == 'S' ) &&
376 ( s[2] == 'o' || s[2] == 'O' ) )
379 if ( !ft_strcmp( s, "10646" ) ||
380 ( !ft_strcmp( s, "8859" ) &&
381 !ft_strcmp( face->charset_encoding, "1" ) ) )
387 FT_CharMapRec charmap;
390 charmap.face = FT_FACE( face );
391 charmap.encoding = FT_ENCODING_NONE;
392 /* initial platform/encoding should indicate unset status? */
393 charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
394 charmap.encoding_id = TT_APPLE_ID_DEFAULT;
396 if ( unicode_charmap )
398 charmap.encoding = FT_ENCODING_UNICODE;
399 charmap.platform_id = TT_PLATFORM_MICROSOFT;
400 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
403 error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
406 /* Select default charmap */
407 if ( pcfface->num_charmaps )
408 pcfface->charmap = pcfface->charmaps[0];
417 FT_TRACE2(( " not a PCF file\n" ));
418 PCF_Face_Done( pcfface );
419 error = FT_THROW( Unknown_File_Format ); /* error */
424 FT_CALLBACK_DEF( FT_Error )
425 PCF_Size_Select( FT_Size size,
426 FT_ULong strike_index )
428 PCF_Accel accel = &( (PCF_Face)size->face )->accel;
431 FT_Select_Metrics( size->face, strike_index );
433 size->metrics.ascender = accel->fontAscent << 6;
434 size->metrics.descender = -accel->fontDescent << 6;
435 size->metrics.max_advance = accel->maxbounds.characterWidth << 6;
441 FT_CALLBACK_DEF( FT_Error )
442 PCF_Size_Request( FT_Size size,
443 FT_Size_Request req )
445 PCF_Face face = (PCF_Face)size->face;
446 FT_Bitmap_Size* bsize = size->face->available_sizes;
447 FT_Error error = FT_ERR( Invalid_Pixel_Size );
451 height = FT_REQUEST_HEIGHT( req );
452 height = ( height + 32 ) >> 6;
456 case FT_SIZE_REQUEST_TYPE_NOMINAL:
457 if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
461 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
462 if ( height == ( face->accel.fontAscent +
463 face->accel.fontDescent ) )
468 error = FT_THROW( Unimplemented_Feature );
475 return PCF_Size_Select( size, 0 );
479 FT_CALLBACK_DEF( FT_Error )
480 PCF_Glyph_Load( FT_GlyphSlot slot,
483 FT_Int32 load_flags )
485 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
487 FT_Error error = FT_Err_Ok;
488 FT_Bitmap* bitmap = &slot->bitmap;
492 FT_UNUSED( load_flags );
495 FT_TRACE1(( "PCF_Glyph_Load: glyph index %d\n", glyph_index ));
499 error = FT_THROW( Invalid_Face_Handle );
503 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
505 error = FT_THROW( Invalid_Argument );
509 stream = face->root.stream;
511 if ( glyph_index > 0 )
514 metric = face->metrics + glyph_index;
516 bitmap->rows = metric->ascent + metric->descent;
517 bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
518 bitmap->num_grays = 1;
519 bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
521 FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
522 PCF_BIT_ORDER( face->bitmapsFormat ),
523 PCF_BYTE_ORDER( face->bitmapsFormat ),
524 PCF_GLYPH_PAD( face->bitmapsFormat ) ));
526 switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
529 bitmap->pitch = ( bitmap->width + 7 ) >> 3;
533 bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
537 bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
541 bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
545 return FT_THROW( Invalid_File_Format );
548 /* XXX: to do: are there cases that need repadding the bitmap? */
549 bytes = bitmap->pitch * bitmap->rows;
551 error = ft_glyphslot_alloc_bitmap( slot, (FT_ULong)bytes );
555 if ( FT_STREAM_SEEK( metric->bits ) ||
556 FT_STREAM_READ( bitmap->buffer, bytes ) )
559 if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
560 BitOrderInvert( bitmap->buffer, bytes );
562 if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
563 PCF_BIT_ORDER( face->bitmapsFormat ) ) )
565 switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
571 TwoByteSwap( bitmap->buffer, bytes );
575 FourByteSwap( bitmap->buffer, bytes );
580 slot->format = FT_GLYPH_FORMAT_BITMAP;
581 slot->bitmap_left = metric->leftSideBearing;
582 slot->bitmap_top = metric->ascent;
584 slot->metrics.horiAdvance = metric->characterWidth << 6;
585 slot->metrics.horiBearingX = metric->leftSideBearing << 6;
586 slot->metrics.horiBearingY = metric->ascent << 6;
587 slot->metrics.width = ( metric->rightSideBearing -
588 metric->leftSideBearing ) << 6;
589 slot->metrics.height = bitmap->rows << 6;
591 ft_synthesize_vertical_metrics( &slot->metrics,
592 ( face->accel.fontAscent +
593 face->accel.fontDescent ) << 6 );
607 pcf_get_bdf_property( PCF_Face face,
608 const char* prop_name,
609 BDF_PropertyRec *aproperty )
614 prop = pcf_find_property( face, prop_name );
617 if ( prop->isString )
619 aproperty->type = BDF_PROPERTY_TYPE_ATOM;
620 aproperty->u.atom = prop->value.atom;
624 if ( prop->value.l > 0x7FFFFFFFL || prop->value.l < ( -1 - 0x7FFFFFFFL ) )
626 FT_TRACE1(( "pcf_get_bdf_property: " ));
627 FT_TRACE1(( "too large integer 0x%x is truncated\n" ));
629 /* Apparently, the PCF driver loads all properties as signed integers!
630 * This really doesn't seem to be a problem, because this is
631 * sufficient for any meaningful values.
633 aproperty->type = BDF_PROPERTY_TYPE_INTEGER;
634 aproperty->u.integer = (FT_Int32)prop->value.l;
639 return FT_THROW( Invalid_Argument );
644 pcf_get_charset_id( PCF_Face face,
645 const char* *acharset_encoding,
646 const char* *acharset_registry )
648 *acharset_encoding = face->charset_encoding;
649 *acharset_registry = face->charset_registry;
655 static const FT_Service_BDFRec pcf_service_bdf =
657 (FT_BDF_GetCharsetIdFunc)pcf_get_charset_id,
658 (FT_BDF_GetPropertyFunc) pcf_get_bdf_property
668 static const FT_ServiceDescRec pcf_services[] =
670 { FT_SERVICE_ID_BDF, &pcf_service_bdf },
671 { FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_PCF },
676 FT_CALLBACK_DEF( FT_Module_Interface )
677 pcf_driver_requester( FT_Module module,
682 return ft_service_list_lookup( pcf_services, name );
686 FT_CALLBACK_TABLE_DEF
687 const FT_Driver_ClassRec pcf_driver_class =
690 FT_MODULE_FONT_DRIVER |
691 FT_MODULE_DRIVER_NO_OUTLINES,
692 sizeof ( FT_DriverRec ),
700 0, /* FT_Module_Constructor */
701 0, /* FT_Module_Destructor */
705 sizeof ( PCF_FaceRec ),
706 sizeof ( FT_SizeRec ),
707 sizeof ( FT_GlyphSlotRec ),
711 0, /* FT_Size_InitFunc */
712 0, /* FT_Size_DoneFunc */
713 0, /* FT_Slot_InitFunc */
714 0, /* FT_Slot_DoneFunc */
718 0, /* FT_Face_GetKerningFunc */
719 0, /* FT_Face_AttachFunc */
720 0, /* FT_Face_GetAdvancesFunc */