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
29 #include <freetype/internal/ftdebug.h>
30 #include <freetype/internal/ftstream.h>
31 #include <freetype/internal/ftobjs.h>
32 #include <freetype/ftgzip.h>
33 #include <freetype/ftlzw.h>
34 #include <freetype/ftbzip2.h>
35 #include <freetype/fterrors.h>
36 #include <freetype/ftbdf.h>
37 #include <freetype/ttnameid.h>
47 #define FT_COMPONENT pcfread
49 #include <freetype/internal/services/svbdf.h>
50 #include <freetype/internal/services/svfntfmt.h>
51 #include <freetype/internal/services/svprop.h>
52 #include <freetype/ftdriver.h>
55 /**************************************************************************
57 * The macro FT_COMPONENT is used in trace mode. It is an implicit
58 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
59 * messages during execution.
62 #define FT_COMPONENT pcfdriver
66 * This file uses X11 terminology for PCF data; an `encoding' in X11 speak
67 * is the same as a `character code' in FreeType speak.
69 typedef struct PCF_CMapRec_
74 } PCF_CMapRec, *PCF_CMap;
77 FT_CALLBACK_DEF( FT_Error )
78 pcf_cmap_init( FT_CMap pcfcmap, /* PCF_CMap */
79 FT_Pointer init_data )
81 PCF_CMap cmap = (PCF_CMap)pcfcmap;
82 PCF_Face face = (PCF_Face)FT_CMAP_FACE( pcfcmap );
84 FT_UNUSED( init_data );
87 cmap->enc = &face->enc;
93 FT_CALLBACK_DEF( void )
94 pcf_cmap_done( FT_CMap pcfcmap ) /* PCF_CMap */
96 PCF_CMap cmap = (PCF_CMap)pcfcmap;
103 FT_CALLBACK_DEF( FT_UInt )
104 pcf_cmap_char_index( FT_CMap pcfcmap, /* PCF_CMap */
107 PCF_CMap cmap = (PCF_CMap)pcfcmap;
108 PCF_Enc enc = cmap->enc;
109 FT_UShort charcodeRow;
110 FT_UShort charcodeCol;
113 if ( charcode > (FT_UInt32)( enc->lastRow * 256 + enc->lastCol ) ||
114 charcode < (FT_UInt32)( enc->firstRow * 256 + enc->firstCol ) )
117 charcodeRow = (FT_UShort)( charcode >> 8 );
118 charcodeCol = (FT_UShort)( charcode & 0xFF );
120 if ( charcodeCol < enc->firstCol ||
121 charcodeCol > enc->lastCol )
124 return (FT_UInt)enc->offset[( charcodeRow - enc->firstRow ) *
125 ( enc->lastCol - enc->firstCol + 1 ) +
126 charcodeCol - enc->firstCol];
130 FT_CALLBACK_DEF( FT_UInt )
131 pcf_cmap_char_next( FT_CMap pcfcmap, /* PCF_CMap */
132 FT_UInt32 *acharcode )
134 PCF_CMap cmap = (PCF_CMap)pcfcmap;
135 PCF_Enc enc = cmap->enc;
136 FT_UInt32 charcode = *acharcode;
137 FT_UShort charcodeRow;
138 FT_UShort charcodeCol;
142 while ( charcode < (FT_UInt32)( enc->lastRow * 256 + enc->lastCol ) )
146 if ( charcode < (FT_UInt32)( enc->firstRow * 256 + enc->firstCol ) )
147 charcode = (FT_UInt32)( enc->firstRow * 256 + enc->firstCol );
149 charcodeRow = (FT_UShort)( charcode >> 8 );
150 charcodeCol = (FT_UShort)( charcode & 0xFF );
152 if ( charcodeCol < enc->firstCol )
153 charcodeCol = enc->firstCol;
154 else if ( charcodeCol > enc->lastCol )
157 charcodeCol = enc->firstCol;
160 charcode = (FT_UInt32)( charcodeRow * 256 + charcodeCol );
162 result = (FT_UInt)enc->offset[( charcodeRow - enc->firstRow ) *
163 ( enc->lastCol - enc->firstCol + 1 ) +
164 charcodeCol - enc->firstCol];
165 if ( result != 0xFFFFU )
169 *acharcode = charcode;
176 const FT_CMap_ClassRec pcf_cmap_class =
178 sizeof ( PCF_CMapRec ),
184 NULL, NULL, NULL, NULL, NULL
188 FT_CALLBACK_DEF( void )
189 PCF_Face_Done( FT_Face pcfface ) /* PCF_Face */
191 PCF_Face face = (PCF_Face)pcfface;
198 memory = FT_FACE_MEMORY( face );
200 FT_FREE( face->metrics );
201 FT_FREE( face->enc.offset );
203 /* free properties */
204 if ( face->properties )
209 for ( i = 0; i < face->nprops; i++ )
211 PCF_Property prop = &face->properties[i];
216 FT_FREE( prop->name );
217 if ( prop->isString )
218 FT_FREE( prop->value.atom );
222 FT_FREE( face->properties );
225 FT_FREE( face->toc.tables );
226 FT_FREE( pcfface->family_name );
227 FT_FREE( pcfface->style_name );
228 FT_FREE( pcfface->available_sizes );
229 FT_FREE( face->charset_encoding );
230 FT_FREE( face->charset_registry );
232 /* close compressed stream if any */
233 if ( pcfface->stream == &face->comp_stream )
235 FT_Stream_Close( &face->comp_stream );
236 pcfface->stream = face->comp_source;
241 FT_CALLBACK_DEF( FT_Error )
242 PCF_Face_Init( FT_Stream stream,
243 FT_Face pcfface, /* PCF_Face */
246 FT_Parameter* params )
248 PCF_Face face = (PCF_Face)pcfface;
251 FT_UNUSED( num_params );
255 FT_TRACE2(( "PCF driver\n" ));
257 error = pcf_load_font( stream, face, face_index );
260 PCF_Face_Done( pcfface );
262 #if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
263 defined( FT_CONFIG_OPTION_USE_LZW ) || \
264 defined( FT_CONFIG_OPTION_USE_BZIP2 )
266 #ifdef FT_CONFIG_OPTION_USE_ZLIB
271 /* this didn't work, try gzip support! */
272 FT_TRACE2(( " ... try gzip stream\n" ));
273 error2 = FT_Stream_OpenGzip( &face->comp_stream, stream );
274 if ( FT_ERR_EQ( error2, Unimplemented_Feature ) )
279 #endif /* FT_CONFIG_OPTION_USE_ZLIB */
281 #ifdef FT_CONFIG_OPTION_USE_LZW
287 /* this didn't work, try LZW support! */
288 FT_TRACE2(( " ... try LZW stream\n" ));
289 error3 = FT_Stream_OpenLZW( &face->comp_stream, stream );
290 if ( FT_ERR_EQ( error3, Unimplemented_Feature ) )
295 #endif /* FT_CONFIG_OPTION_USE_LZW */
297 #ifdef FT_CONFIG_OPTION_USE_BZIP2
303 /* this didn't work, try Bzip2 support! */
304 FT_TRACE2(( " ... try Bzip2 stream\n" ));
305 error4 = FT_Stream_OpenBzip2( &face->comp_stream, stream );
306 if ( FT_ERR_EQ( error4, Unimplemented_Feature ) )
311 #endif /* FT_CONFIG_OPTION_USE_BZIP2 */
316 face->comp_source = stream;
317 pcfface->stream = &face->comp_stream;
319 stream = pcfface->stream;
321 error = pcf_load_font( stream, face, face_index );
325 #else /* !(FT_CONFIG_OPTION_USE_ZLIB ||
326 FT_CONFIG_OPTION_USE_LZW ||
327 FT_CONFIG_OPTION_USE_BZIP2) */
334 /* PCF cannot have multiple faces in a single font file.
335 * XXX: A non-zero face_index is already an invalid argument, but
336 * Type1, Type42 drivers have a convention to return
337 * an invalid argument error when the font could be
338 * opened by the specified driver.
340 if ( face_index < 0 )
342 else if ( face_index > 0 && ( face_index & 0xFFFF ) > 0 )
344 FT_ERROR(( "PCF_Face_Init: invalid face index\n" ));
345 PCF_Face_Done( pcfface );
346 return FT_THROW( Invalid_Argument );
351 FT_String *charset_registry = face->charset_registry;
352 FT_String *charset_encoding = face->charset_encoding;
353 FT_Bool unicode_charmap = 0;
356 if ( charset_registry && charset_encoding )
358 char* s = charset_registry;
361 /* Uh, oh, compare first letters manually to avoid dependency
363 if ( ( s[0] == 'i' || s[0] == 'I' ) &&
364 ( s[1] == 's' || s[1] == 'S' ) &&
365 ( s[2] == 'o' || s[2] == 'O' ) )
368 if ( !ft_strcmp( s, "10646" ) ||
369 ( !ft_strcmp( s, "8859" ) &&
370 !ft_strcmp( face->charset_encoding, "1" ) ) )
372 /* another name for ASCII */
373 else if ( !ft_strcmp( s, "646.1991" ) &&
374 !ft_strcmp( face->charset_encoding, "IRV" ) )
380 FT_CharMapRec charmap;
383 charmap.face = FT_FACE( face );
384 charmap.encoding = FT_ENCODING_NONE;
385 /* initial platform/encoding should indicate unset status? */
386 charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
387 charmap.encoding_id = TT_APPLE_ID_DEFAULT;
389 if ( unicode_charmap )
391 charmap.encoding = FT_ENCODING_UNICODE;
392 charmap.platform_id = TT_PLATFORM_MICROSOFT;
393 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
396 error = FT_CMap_New( &pcf_cmap_class, NULL, &charmap, NULL );
404 FT_TRACE2(( " not a PCF file\n" ));
405 PCF_Face_Done( pcfface );
406 error = FT_THROW( Unknown_File_Format ); /* error */
411 FT_CALLBACK_DEF( FT_Error )
412 PCF_Size_Select( FT_Size size,
413 FT_ULong strike_index )
415 PCF_Accel accel = &( (PCF_Face)size->face )->accel;
418 FT_Select_Metrics( size->face, strike_index );
420 size->metrics.ascender = accel->fontAscent * 64;
421 size->metrics.descender = -accel->fontDescent * 64;
422 size->metrics.max_advance = accel->maxbounds.characterWidth * 64;
428 FT_CALLBACK_DEF( FT_Error )
429 PCF_Size_Request( FT_Size size,
430 FT_Size_Request req )
432 PCF_Face face = (PCF_Face)size->face;
433 FT_Bitmap_Size* bsize = size->face->available_sizes;
434 FT_Error error = FT_ERR( Invalid_Pixel_Size );
438 height = FT_REQUEST_HEIGHT( req );
439 height = ( height + 32 ) >> 6;
443 case FT_SIZE_REQUEST_TYPE_NOMINAL:
444 if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
448 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
449 if ( height == ( face->accel.fontAscent +
450 face->accel.fontDescent ) )
455 error = FT_THROW( Unimplemented_Feature );
462 return PCF_Size_Select( size, 0 );
466 FT_CALLBACK_DEF( FT_Error )
467 PCF_Glyph_Load( FT_GlyphSlot slot,
470 FT_Int32 load_flags )
472 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
474 FT_Error error = FT_Err_Ok;
475 FT_Bitmap* bitmap = &slot->bitmap;
480 FT_TRACE1(( "PCF_Glyph_Load: glyph index %d\n", glyph_index ));
484 error = FT_THROW( Invalid_Face_Handle );
488 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
490 error = FT_THROW( Invalid_Argument );
494 stream = face->root.stream;
496 metric = face->metrics + glyph_index;
498 bitmap->rows = (unsigned int)( metric->ascent +
500 bitmap->width = (unsigned int)( metric->rightSideBearing -
501 metric->leftSideBearing );
502 bitmap->num_grays = 1;
503 bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
505 switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
508 bitmap->pitch = (int)( ( bitmap->width + 7 ) >> 3 );
512 bitmap->pitch = (int)( ( ( bitmap->width + 15 ) >> 4 ) << 1 );
516 bitmap->pitch = (int)( ( ( bitmap->width + 31 ) >> 5 ) << 2 );
520 bitmap->pitch = (int)( ( ( bitmap->width + 63 ) >> 6 ) << 3 );
524 return FT_THROW( Invalid_File_Format );
527 slot->format = FT_GLYPH_FORMAT_BITMAP;
528 slot->bitmap_left = metric->leftSideBearing;
529 slot->bitmap_top = metric->ascent;
531 slot->metrics.horiAdvance = (FT_Pos)( metric->characterWidth * 64 );
532 slot->metrics.horiBearingX = (FT_Pos)( metric->leftSideBearing * 64 );
533 slot->metrics.horiBearingY = (FT_Pos)( metric->ascent * 64 );
534 slot->metrics.width = (FT_Pos)( ( metric->rightSideBearing -
535 metric->leftSideBearing ) * 64 );
536 slot->metrics.height = (FT_Pos)( bitmap->rows * 64 );
538 ft_synthesize_vertical_metrics( &slot->metrics,
539 ( face->accel.fontAscent +
540 face->accel.fontDescent ) * 64 );
542 if ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY )
545 /* XXX: to do: are there cases that need repadding the bitmap? */
546 bytes = (FT_ULong)bitmap->pitch * bitmap->rows;
548 error = ft_glyphslot_alloc_bitmap( slot, (FT_ULong)bytes );
552 if ( FT_STREAM_SEEK( metric->bits ) ||
553 FT_STREAM_READ( bitmap->buffer, bytes ) )
556 if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
557 BitOrderInvert( bitmap->buffer, bytes );
559 if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
560 PCF_BIT_ORDER( face->bitmapsFormat ) ) )
562 switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
568 TwoByteSwap( bitmap->buffer, bytes );
572 FourByteSwap( bitmap->buffer, bytes );
589 pcf_get_bdf_property( PCF_Face face,
590 const char* prop_name,
591 BDF_PropertyRec *aproperty )
596 prop = pcf_find_property( face, prop_name );
599 if ( prop->isString )
601 aproperty->type = BDF_PROPERTY_TYPE_ATOM;
602 aproperty->u.atom = prop->value.atom;
606 if ( prop->value.l > 0x7FFFFFFFL ||
607 prop->value.l < ( -1 - 0x7FFFFFFFL ) )
609 FT_TRACE1(( "pcf_get_bdf_property:"
610 " too large integer 0x%lx is truncated\n",
615 * 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;
626 return FT_THROW( Invalid_Argument );
631 pcf_get_charset_id( PCF_Face face,
632 const char* *acharset_encoding,
633 const char* *acharset_registry )
635 *acharset_encoding = face->charset_encoding;
636 *acharset_registry = face->charset_registry;
642 static const FT_Service_BDFRec pcf_service_bdf =
644 (FT_BDF_GetCharsetIdFunc)pcf_get_charset_id, /* get_charset_id */
645 (FT_BDF_GetPropertyFunc) pcf_get_bdf_property /* get_property */
654 pcf_property_set( FT_Module module, /* PCF_Driver */
655 const char* property_name,
657 FT_Bool value_is_string )
659 #ifdef PCF_CONFIG_OPTION_LONG_FAMILY_NAMES
661 FT_Error error = FT_Err_Ok;
662 PCF_Driver driver = (PCF_Driver)module;
664 #ifndef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
665 FT_UNUSED( value_is_string );
669 if ( !ft_strcmp( property_name, "no-long-family-names" ) )
671 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
672 if ( value_is_string )
674 const char* s = (const char*)value;
675 long lfn = ft_strtol( s, NULL, 10 );
679 driver->no_long_family_names = 0;
681 driver->no_long_family_names = 1;
683 return FT_THROW( Invalid_Argument );
688 FT_Bool* no_long_family_names = (FT_Bool*)value;
691 driver->no_long_family_names = *no_long_family_names;
697 #else /* !PCF_CONFIG_OPTION_LONG_FAMILY_NAMES */
701 FT_UNUSED( value_is_string );
702 #ifndef FT_DEBUG_LEVEL_TRACE
703 FT_UNUSED( property_name );
706 #endif /* !PCF_CONFIG_OPTION_LONG_FAMILY_NAMES */
708 FT_TRACE0(( "pcf_property_set: missing property `%s'\n",
710 return FT_THROW( Missing_Property );
715 pcf_property_get( FT_Module module, /* PCF_Driver */
716 const char* property_name,
719 #ifdef PCF_CONFIG_OPTION_LONG_FAMILY_NAMES
721 FT_Error error = FT_Err_Ok;
722 PCF_Driver driver = (PCF_Driver)module;
725 if ( !ft_strcmp( property_name, "no-long-family-names" ) )
727 FT_Bool no_long_family_names = driver->no_long_family_names;
728 FT_Bool* val = (FT_Bool*)value;
731 *val = no_long_family_names;
736 #else /* !PCF_CONFIG_OPTION_LONG_FAMILY_NAMES */
740 #ifndef FT_DEBUG_LEVEL_TRACE
741 FT_UNUSED( property_name );
744 #endif /* !PCF_CONFIG_OPTION_LONG_FAMILY_NAMES */
746 FT_TRACE0(( "pcf_property_get: missing property `%s'\n",
748 return FT_THROW( Missing_Property );
752 FT_DEFINE_SERVICE_PROPERTIESREC(
753 pcf_service_properties,
755 (FT_Properties_SetFunc)pcf_property_set, /* set_property */
756 (FT_Properties_GetFunc)pcf_property_get ) /* get_property */
765 static const FT_ServiceDescRec pcf_services[] =
767 { FT_SERVICE_ID_BDF, &pcf_service_bdf },
768 { FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_PCF },
769 { FT_SERVICE_ID_PROPERTIES, &pcf_service_properties },
774 FT_CALLBACK_DEF( FT_Module_Interface )
775 pcf_driver_requester( FT_Module module,
780 return ft_service_list_lookup( pcf_services, name );
784 FT_CALLBACK_DEF( FT_Error )
785 pcf_driver_init( FT_Module module ) /* PCF_Driver */
787 #ifdef PCF_CONFIG_OPTION_LONG_FAMILY_NAMES
788 PCF_Driver driver = (PCF_Driver)module;
791 driver->no_long_family_names = 0;
800 FT_CALLBACK_DEF( void )
801 pcf_driver_done( FT_Module module ) /* PCF_Driver */
807 FT_CALLBACK_TABLE_DEF
808 const FT_Driver_ClassRec pcf_driver_class =
811 FT_MODULE_FONT_DRIVER |
812 FT_MODULE_DRIVER_NO_OUTLINES,
814 sizeof ( PCF_DriverRec ),
819 NULL, /* module-specific interface */
821 pcf_driver_init, /* FT_Module_Constructor module_init */
822 pcf_driver_done, /* FT_Module_Destructor module_done */
823 pcf_driver_requester /* FT_Module_Requester get_interface */
826 sizeof ( PCF_FaceRec ),
827 sizeof ( FT_SizeRec ),
828 sizeof ( FT_GlyphSlotRec ),
830 PCF_Face_Init, /* FT_Face_InitFunc init_face */
831 PCF_Face_Done, /* FT_Face_DoneFunc done_face */
832 NULL, /* FT_Size_InitFunc init_size */
833 NULL, /* FT_Size_DoneFunc done_size */
834 NULL, /* FT_Slot_InitFunc init_slot */
835 NULL, /* FT_Slot_DoneFunc done_slot */
837 PCF_Glyph_Load, /* FT_Slot_LoadFunc load_glyph */
839 NULL, /* FT_Face_GetKerningFunc get_kerning */
840 NULL, /* FT_Face_AttachFunc attach_file */
841 NULL, /* FT_Face_GetAdvancesFunc get_advances */
843 PCF_Size_Request, /* FT_Size_RequestFunc request_size */
844 PCF_Size_Select /* FT_Size_SelectFunc select_size */