3 FreeType font driver for pcf files
5 Copyright (C) 2000-2001 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
35 #include "pcfdriver.h"
41 /*************************************************************************/
43 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
44 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
45 /* messages during execution. */
48 #define FT_COMPONENT trace_pcfdriver
52 PCF_Done_Face( PCF_Face face )
54 FT_Memory memory = FT_FACE_MEMORY( face );
55 PCF_Property tmp = face->properties;
59 FREE( face->encodings );
60 FREE( face->metrics );
62 for ( i = 0; i < face->nprops; i++ )
68 FREE( face->properties );
70 FT_TRACE4(( "DONE_FACE!!!\n" ));
77 PCF_Init_Face( FT_Stream stream,
81 FT_Parameter* params )
83 FT_Error error = PCF_Err_Ok;
85 FT_UNUSED( num_params );
87 FT_UNUSED( face_index );
90 error = pcf_load_font( stream, face );
97 FT_TRACE2(( "[not a valid PCF file]\n" ));
98 PCF_Done_Face( face );
100 return PCF_Err_Unknown_File_Format; /* error */
105 PCF_Set_Pixel_Size( FT_Size size )
107 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
110 FT_TRACE4(( "rec %d - pres %d\n", size->metrics.y_ppem,
111 face->root.available_sizes->height ));
113 if ( size->metrics.y_ppem == face->root.available_sizes->height )
115 size->metrics.ascender = face->accel.fontAscent << 6;
116 size->metrics.descender = face->accel.fontDescent * (-64);
118 size->metrics.height = face->accel.maxbounds.ascent << 6;
120 size->metrics.height = size->metrics.ascender -
121 size->metrics.descender;
127 FT_TRACE4(( "size WRONG\n" ));
128 return PCF_Err_Invalid_Pixel_Size;
134 PCF_Load_Glyph( FT_GlyphSlot slot,
139 PCF_Face face = (PCF_Face)FT_SIZE_FACE( size );
140 FT_Error error = PCF_Err_Ok;
141 FT_Memory memory = FT_FACE(face)->memory;
142 FT_Bitmap* bitmap = &slot->bitmap;
146 FT_Stream stream = face->root.stream;
148 FT_UNUSED( load_flags );
151 FT_TRACE4(( "load_glyph %d ---", glyph_index ));
155 error = PCF_Err_Invalid_Argument;
159 metric = face->metrics + glyph_index;
161 bitmap->rows = metric->ascent + metric->descent;
162 bitmap->width = metric->rightSideBearing - metric->leftSideBearing;
163 bitmap->num_grays = 1;
164 bitmap->pixel_mode = ft_pixel_mode_mono;
166 FT_TRACE6(( "BIT_ORDER %d ; BYTE_ORDER %d ; GLYPH_PAD %d\n",
167 PCF_BIT_ORDER( face->bitmapsFormat ),
168 PCF_BYTE_ORDER( face->bitmapsFormat ),
169 PCF_GLYPH_PAD( face->bitmapsFormat ) ));
171 switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
174 bitmap->pitch = ( bitmap->width + 7 ) >> 3;
178 bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
182 bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
186 bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
190 return PCF_Err_Invalid_File_Format;
193 /* XXX: to do: are there cases that need repadding the bitmap? */
194 bytes = bitmap->pitch * bitmap->rows;
196 if ( ALLOC( bitmap->buffer, bytes ) )
199 if ( FILE_Seek( metric->bits ) ||
200 FILE_Read( bitmap->buffer, bytes ) )
203 if ( PCF_BIT_ORDER( face->bitmapsFormat ) != MSBFirst )
204 BitOrderInvert( bitmap->buffer,bytes );
206 if ( ( PCF_BYTE_ORDER( face->bitmapsFormat ) !=
207 PCF_BIT_ORDER( face->bitmapsFormat ) ) )
209 switch ( PCF_SCAN_UNIT( face->bitmapsFormat ) )
215 TwoByteSwap( bitmap->buffer, bytes );
219 FourByteSwap( bitmap->buffer, bytes );
224 slot->bitmap_left = metric->leftSideBearing;
225 slot->bitmap_top = metric->ascent;
227 slot->metrics.horiAdvance = metric->characterWidth << 6 ;
228 slot->metrics.horiBearingX = metric->rightSideBearing << 6 ;
229 slot->metrics.horiBearingY = metric->ascent << 6 ;
230 slot->metrics.width = metric->characterWidth << 6 ;
231 slot->metrics.height = bitmap->rows << 6;
233 slot->linearHoriAdvance = (FT_Fixed)bitmap->width << 16;
234 slot->format = ft_glyph_format_bitmap;
235 slot->flags = ft_glyph_own_bitmap;
237 FT_TRACE4(( " --- ok\n" ));
245 PCF_Get_Char_Index( FT_CharMap charmap,
248 PCF_Face face = (PCF_Face)charmap->face;
249 PCF_Encoding en_table = face->encodings;
253 FT_TRACE4(( "get_char_index %ld\n", char_code ));
256 high = face->nencodings - 1;
257 while ( low <= high )
259 mid = ( low + high ) / 2;
260 if ( char_code < en_table[mid].enc )
262 else if ( char_code > en_table[mid].enc )
265 return en_table[mid].glyph;
272 FT_CALLBACK_TABLE_DEF
273 const FT_Driver_Class pcf_driver_class =
276 ft_module_font_driver,
277 sizeof ( FT_DriverRec ),
285 (FT_Module_Constructor)0,
286 (FT_Module_Destructor) 0,
287 (FT_Module_Requester) 0
290 sizeof( PCF_FaceRec ),
291 sizeof( FT_SizeRec ),
292 sizeof( FT_GlyphSlotRec ),
294 (FTDriver_initFace) PCF_Init_Face,
295 (FTDriver_doneFace) PCF_Done_Face,
296 (FTDriver_initSize) 0,
297 (FTDriver_doneSize) 0,
298 (FTDriver_initGlyphSlot)0,
299 (FTDriver_doneGlyphSlot)0,
301 (FTDriver_setCharSizes) PCF_Set_Pixel_Size,
302 (FTDriver_setPixelSizes)PCF_Set_Pixel_Size,
304 (FTDriver_loadGlyph) PCF_Load_Glyph,
305 (FTDriver_getCharIndex) PCF_Get_Char_Index,
307 (FTDriver_getKerning) 0,
308 (FTDriver_attachFile) 0,
309 (FTDriver_getAdvances) 0
313 #ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS
316 /*************************************************************************/
322 /* This function is used when compiling the TrueType driver as a */
323 /* shared library (`.DLL' or `.so'). It will be used by the */
324 /* high-level library of FreeType to retrieve the address of the */
325 /* driver's generic interface. */
327 /* It shouldn't be implemented in a static build, as each driver must */
328 /* have the same function as an exported entry point. */
331 /* The address of the TrueType's driver generic interface. The */
332 /* format-specific interface can then be retrieved through the method */
333 /* interface->get_format_interface. */
335 FT_EXPORT_DEF( const FT_Driver_Class* )
336 getDriverClass( void )
338 return &pcf_driver_class;
342 #endif /* FT_CONFIG_OPTION_DYNAMIC_DRIVERS */