blob: d24d83b0a0b23bb0bf0f0d6154fdf40baf021331 [file] [log] [blame]
Just van Rossumae4bcee2000-07-28 01:10:35 +00001/***************************************************************************/
2/* */
3/* ftmac.c */
4/* */
5/* Mac FOND support. Written by just@letterror.com. */
6/* */
7/* Copyright 1996-2000 by */
8/* Just van Rossum, David Turner, Robert Wilhelm, and Werner Lemberg. */
9/* */
10/* This file is part of the FreeType project, and may only be used, */
11/* modified, and distributed under the terms of the FreeType project */
12/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13/* this file you indicate that you have read the license and */
14/* understand and accept it fully. */
15/* */
16/***************************************************************************/
17
18
19 /*
20 Notes
21
22 Mac suitcase files can (and often do!) contain multiple fonts. To
23 support this I use the face_index argument of FT_(Open|New)_Face()
24 functions, and pretend the suitcase file is a collection.
25 Warning: although the FOND driver sets face->num_faces field to the
26 number of available fonts, but the Type 1 driver sets it to 1 anyway.
27 So this field is currently not reliable, and I don't see a clean way
28 to resolve that. The face_index argument translates to
29 Get1IndResource( 'FOND', face_index + 1 );
30 so clients should figure out the resource index of the FOND.
31 (I'll try to provide some example code for this at some point.)
32
33
34 The Mac FOND support works roughly like this:
35
36 - Check whether the offered stream points to a Mac suitcase file.
37 This is done by checking the file type: it has to be 'FFIL' or 'tfil'.
38 The stream that gets passed to our init_face() routine is a stdio
39 stream, which isn't usable for us, since the FOND resources live
40 in the resource fork. So we just grab the stream->pathname field.
41
42 - Read the FOND resource into memory, then check whether there is
43 a TrueType font and/or (!) a Type 1 font available.
44
45 - If there is a Type 1 font available (as a separate 'LWFN' file),
46 read its data into memory, massage it slightly so it becomes
47 PFB data, wrap it into a memory stream, load the Type 1 driver
48 and delegate the rest of the work to it by calling FT_Open_Face().
49 (XXX TODO: after this has been done, the kerning data from the FOND
50 resource should be appended to the face: on the Mac there are usually
51 no AFM files available. However, this is tricky since we need to map
52 Mac char codes to ps glyph names to glyph ID's...)
53
54 - If there is a TrueType font (an 'sfnt' resource), read it into
55 memory, wrap it into a memory stream, load the TrueType driver
56 and delegate the rest of the work to it, by calling FT_Open_Face().
57 */
58
59#include <freetype/freetype.h>
60#include <freetype/internal/ftstream.h>
61#include <truetype/ttobjs.h>
62#include <type1z/z1objs.h>
63
64#include <Resources.h>
65#include <Fonts.h>
66#include <Errors.h>
67
68#include <ctype.h> /* for isupper() and isalnum() */
69
70#include <ftmac.h>
71
72
73
74 /* Set PREFER_LWFN to 1 if LWFN (Type 1) is preferred over
75 TrueType in case *both* are available (this is not common,
76 but it *is* possible). */
77#ifndef PREFER_LWFN
78#define PREFER_LWFN 1
79#endif
80
81
82
83 /* Quick 'n' Dirty Pascal string to C string converter.
84 Warning: this call is not thread safe! Use with caution. */
85 static
86 char * p2c_str( unsigned char *pstr )
87 {
88 static char cstr[256];
89
90
91 strncpy( cstr, (char*)pstr+1, pstr[0] );
92 cstr[pstr[0]] = '\0';
93 return cstr;
94 }
95
96
97 /* Given a pathname, fill in a file spec. */
98 static
99 int file_spec_from_path( const char* pathname, FSSpec *spec )
100 {
101 Str255 p_path;
102 FT_ULong path_len;
103
104
105 /* convert path to a pascal string */
106 path_len = strlen( pathname );
107 if ( path_len > 255 )
108 return -1;
109 p_path[0] = (unsigned char)path_len;
110 strncpy( (char*)p_path+1, pathname, path_len );
111
112 if ( FSMakeFSSpec( 0, 0, p_path, spec ) != noErr )
113 return -1;
114 else
115 return 0;
116 }
117
118
119 /* Return the file type of the file specified by spec. */
120 static
121 OSType get_file_type( FSSpec *spec )
122 {
123 FInfo finfo;
124
125
126 if ( FSpGetFInfo( spec, &finfo ) != noErr )
127 return 0; /* file might not exist */
128 return finfo.fdType;
129 }
130
131
132 /* Given a PostScript font name, create the Macintosh LWFN file name. */
133 static
134 void create_lwfn_name( char* ps_name, Str255 lwfn_file_name )
135 {
136 int max = 5, count = 0;
137 FT_Byte* p = lwfn_file_name;
138 FT_Byte* q = (FT_Byte*)ps_name;
139
140
141 lwfn_file_name[0] = 0;
142
143 while ( *q )
144 {
145 if ( isupper(*q) )
146 {
147 if ( count )
148 max = 3;
149 count = 0;
150 }
151 if ( count < max && (isalnum(*q) || *q == '_' ) )
152 {
153 *++p = *q;
154 lwfn_file_name[0]++;
155 count++;
156 }
157 q++;
158 }
159 }
160
161
162 /* Given a file reference, answer its location as a vRefNum
163 and a dirID. */
164 static
165 FT_Error get_file_location( short ref_num,
166 short* v_ref_num,
167 long* dir_id,
168 unsigned char* file_name )
169 {
170 FCBPBRec pb;
171 OSErr error;
172
173
174 pb.ioNamePtr = file_name;
175 pb.ioVRefNum = 0;
176 pb.ioRefNum = ref_num;
177 pb.ioFCBIndx = 0;
178
179 error = PBGetFCBInfoSync( &pb );
180 if ( error == noErr )
181 {
182 *v_ref_num = pb.ioFCBVRefNum;
183 *dir_id = pb.ioFCBParID;
184 }
185 return error;
186 }
187
188
189 /* Make a file spec for an LWFN file from a FOND resource and
190 a file name. */
191 static
192 FT_Error make_lwfn_spec( Handle fond,
193 unsigned char* file_name,
194 FSSpec* spec )
195 {
196 FT_Error error;
197 short ref_num, v_ref_num;
198 long dir_id;
199 Str255 fond_file_name;
200
201
202 ref_num = HomeResFile( fond );
203 error = ResError();
204 if ( !error )
205 error = get_file_location( ref_num, &v_ref_num, &dir_id, fond_file_name );
206 if ( !error )
207 error = FSMakeFSSpec( v_ref_num, dir_id, file_name, spec );
208
209 return error;
210 }
211
212
213 /* Look inside the FOND data, answer whether there should be an SFNT
214 resource, and answer the name of a possible LWFN Type 1 file. */
215 static
216 void parse_fond( char* fond_data,
217 short *have_sfnt,
218 short *sfnt_id,
219 Str255 lwfn_file_name )
220 {
221 AsscEntry* assoc;
222 FamRec* fond;
223
224
225 *sfnt_id = 0;
226 *have_sfnt = 0;
227 lwfn_file_name[0] = 0;
228
229 fond = (FamRec*)fond_data;
230 assoc = (AsscEntry*)(fond_data + sizeof(FamRec) + 2);
231
232 if ( assoc->fontSize == 0 )
233 {
234 *have_sfnt = 1;
235 *sfnt_id = assoc->fontID;
236 }
237
238 if ( fond->ffStylOff )
239 {
240 unsigned char* p = (unsigned char*)fond_data;
241 StyleTable* style;
242 unsigned short string_count;
243 unsigned char* name_table = 0;
244 char ps_name[256];
245 unsigned char* names[64];
246 int i;
247
248 p += fond->ffStylOff;
249 style = (StyleTable*)p;
250 p += sizeof(StyleTable);
251 string_count = *(unsigned short*)(p);
252 p += sizeof(short);
253
254 for ( i = 0 ; i < string_count && i < 64; i++ )
255 {
256 names[i] = p;
257 p += names[i][0];
258 p++;
259 }
260 strcpy(ps_name, p2c_str(names[0])); /* Family name */
261
262 if ( style->indexes[0] > 1 )
263 {
264 unsigned char* suffixes = names[style->indexes[0]-1];
265 for ( i=1; i<=suffixes[0]; i++ )
266 strcat( ps_name, p2c_str(names[suffixes[i]-1]) );
267 }
268 create_lwfn_name( ps_name, lwfn_file_name );
269 }
270 }
271
272
273 /* Read Type 1 data from the POST resources inside the LWFN file,
274 return a PFB buffer. This is somewhat convoluted because the FT2
275 PFB parser wants the ASCII header as one chunk, and the LWFN
276 chunks are often not organized that way, so we'll glue chunks
277 of the same type together. */
278 static
279 FT_Error read_lwfn( FT_Memory memory,
280 FSSpec* lwfn_spec,
281 FT_Byte** pfb_data,
282 FT_ULong* size )
283 {
284 FT_Error error = FT_Err_Ok;
285 short res_ref, res_id;
286 unsigned char *buffer, *p, *size_p;
287 FT_ULong total_size = 0;
288 FT_ULong post_size, pfb_chunk_size;
289 Handle post_data;
290 char code, last_code;
291
292
293 res_ref = FSpOpenResFile( lwfn_spec, fsRdPerm );
294 if ( ResError() )
295 return FT_Err_Out_Of_Memory;
296 UseResFile( res_ref );
297
298 /* First pass: load all POST resources, and determine the size of
299 the output buffer. */
300 res_id = 501;
301 last_code = -1;
302 for (;;)
303 {
304 post_data = Get1Resource( 'POST', res_id++ );
305 if ( post_data == NULL )
306 break; /* we're done */
307
308 code = (*post_data)[0];
309
310 if ( code != last_code )
311 {
312 if ( code == 5 )
313 total_size += 2; /* just the end code */
314 else
315 total_size += 6; /* code + 4 bytes chunk length */
316 }
317
318 total_size += GetHandleSize( post_data ) - 2;
319 last_code = code;
320 }
321
322 if ( ALLOC( buffer, (FT_Long)total_size ) )
323 goto Error;
324
325 /* Second pass: append all POST data to the buffer, add PFB fields.
326 Glue all consequtive chunks of the same type together. */
327 p = buffer;
328 res_id = 501;
329 last_code = -1;
330 pfb_chunk_size = 0;
331
332 for (;;)
333 {
334 post_data = Get1Resource( 'POST', res_id++ );
335 if ( post_data == NULL )
336 break; /* we're done */
337
338 post_size = (FT_ULong)GetHandleSize( post_data ) - 2;
339 code = (*post_data)[0];
340
341 if ( code != last_code )
342 {
343
344 if ( last_code != -1 )
345 {
346 /* we're done adding a chunk, fill in the size field */
347 *size_p++ = (FT_Byte)(pfb_chunk_size & 0xFF);
348 *size_p++ = (FT_Byte)((pfb_chunk_size >> 8) & 0xFF);
349 *size_p++ = (FT_Byte)((pfb_chunk_size >> 16) & 0xFF);
350 *size_p++ = (FT_Byte)((pfb_chunk_size >> 24) & 0xFF);
351 pfb_chunk_size = 0;
352 }
353
354 *p++ = 0x80;
355 if ( code == 5 )
356 *p++ = 0x03; /* the end */
357 else if ( code == 2 )
358 *p++ = 0x02; /* binary segment */
359 else
360 *p++ = 0x01; /* ASCII segment */
361
362 if ( code != 5 )
363 {
364 size_p = p; /* save for later */
365 p += 4; /* make space for size field */
366 }
367 }
368
369 memcpy( p, *post_data + 2, post_size );
370 pfb_chunk_size += post_size;
371 p += post_size;
372 last_code = code;
373 }
374
375 *pfb_data = buffer;
376 *size = total_size;
377
378Error:
379 CloseResFile( res_ref );
380 return error;
381 }
382
383
384 /* Finalizer for a memory stream; gets called by FT_Done_Face().
385 It frees the memory it uses. */
386 static
387 void memory_stream_close( FT_Stream stream )
388 {
389 FT_Memory memory = stream->memory;
390
391
392 FREE( stream->base );
393 stream->size = 0;
394 stream->base = 0;
395 stream->close = 0;
396 }
397
398
399 /* Create a new memory stream from a buffer and a size. */
400 static
401 FT_Error new_memory_stream( FT_Library library,
402 FT_Byte* base,
403 FT_ULong size,
404 FT_Stream_Close close,
405 FT_Stream* astream )
406 {
407 FT_Error error;
408 FT_Memory memory;
409 FT_Stream stream;
410
411
412 if ( !library )
413 return FT_Err_Invalid_Library_Handle;
414
415 if ( !base )
416 return FT_Err_Invalid_Argument;
417
418 *astream = 0;
419 memory = library->memory;
420 if ( ALLOC( stream, sizeof(*stream) ) )
421 goto Exit;
422
423 FT_New_Memory_Stream( library,
424 base,
425 size,
426 stream );
427
428 stream->close = close;
429
430 *astream = stream;
431
432 Exit:
433 return error;
434 }
435
436
437 /* Create a new FT_Face given a buffer and a driver name. */
438 static
439 FT_Error open_face_from_buffer( FT_Library library,
440 FT_Byte* base,
441 FT_ULong size,
442 FT_Long face_index,
443 char* driver_name,
444 FT_Face* aface )
445 {
446 FT_Open_Args args;
447 FT_Error error;
448 FT_Stream stream;
449 FT_Memory memory = library->memory;
450
451
452 error = new_memory_stream( library,
453 base,
454 size,
455 memory_stream_close,
456 &stream );
457 if ( error )
458 {
459 FREE( base );
460 return error;
461 }
462
463 args.flags = ft_open_stream;
464 args.stream = stream;
465 if ( driver_name )
466 {
467 args.flags = args.flags | ft_open_driver;
468 args.driver = FT_Get_Module( library, driver_name );
469 }
470 error = FT_Open_Face( library, &args, face_index, aface );
Just van Rossum5fe94ff2000-07-28 02:25:23 +0000471 if ( error == FT_Err_Ok )
Just van Rossumae4bcee2000-07-28 01:10:35 +0000472 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
Just van Rossum5fe94ff2000-07-28 02:25:23 +0000473 else
474 {
475 FT_Done_Stream( stream );
476 FREE( stream );
477 }
Just van Rossumae4bcee2000-07-28 01:10:35 +0000478 return error;
479 }
480
481
482 /* Create a new FT_Face from a file spec to an LWFN file. */
483 static
484 FT_Error FT_New_Face_From_LWFN( FT_Library library,
485 FSSpec* spec,
486 FT_Long face_index,
487 FT_Face* aface )
488 {
489 FT_Byte* pfb_data;
490 FT_ULong pfb_size;
491 FT_Error error;
492 FT_Memory memory = library->memory;
493
494
495 error = read_lwfn( library->memory, spec, &pfb_data, &pfb_size );
496 if ( error )
497 return error;
498
499#if 0
500 {
501 FILE* f;
502 char * path;
503
504 path = p2c_str( spec->name );
505 strcat( path, ".PFB" );
506 f = fopen(path, "wb");
507 if ( f )
508 {
509 fwrite( pfb_data, 1, pfb_size, f );
510 fclose( f );
511 }
512 }
513#endif
514
515 return open_face_from_buffer( library,
516 pfb_data,
517 pfb_size,
518 face_index,
519 "type1",
520 aface );
521 }
522
523
524 /* Create a new FT_Face from an SFNT resource, specified by res ID. */
525 static
526 FT_Error FT_New_Face_From_SFNT( FT_Library library,
527 short sfnt_id,
528 FT_Long face_index,
529 FT_Face* aface )
530 {
531 Handle sfnt = NULL;
532 FT_Byte* sfnt_data;
533 size_t sfnt_size;
534 FT_Stream stream = NULL;
535 FT_Error error = 0;
536 FT_Memory memory = library->memory;
537
538
539 sfnt = GetResource( 'sfnt', sfnt_id );
540 if ( ResError() )
541 return FT_Err_Invalid_Handle;
542
543 sfnt_size = (FT_ULong)GetHandleSize( sfnt );
544 if ( ALLOC( sfnt_data, (FT_Long)sfnt_size ) )
545 {
546 ReleaseResource( sfnt );
547 return error;
548 }
549
550 HLock( sfnt );
551 memcpy( sfnt_data, *sfnt, sfnt_size);
552 HUnlock( sfnt );
553 ReleaseResource( sfnt );
554
555 return open_face_from_buffer( library,
556 sfnt_data,
557 sfnt_size,
558 face_index,
559 "truetype",
560 aface );
561 }
562
563
564 /* Create a new FT_Face from a file spec to a suitcase file. */
565 static
566 FT_Error FT_New_Face_From_Suitcase( FT_Library library,
567 FSSpec* spec,
568 FT_Long face_index,
569 FT_Face* aface )
570 {
571 FT_Error error = FT_Err_Ok;
572 short res_ref, res_index;
573 Handle fond;
574
575
576 res_ref = FSpOpenResFile( spec, fsRdPerm );
577 if ( ResError() )
578 return FT_Err_Cannot_Open_Resource;
579 UseResFile( res_ref );
580
581 /* face_index may be -1, in which case we
582 just need to do a sanity check */
583 if ( face_index < 0)
584 res_index = 1;
585 else
586 {
587 res_index = (short)(face_index + 1);
588 face_index = 0;
589 }
590 fond = Get1IndResource( 'FOND', res_index );
591 if ( ResError() )
592 {
593 error = FT_Err_Cannot_Open_Resource;
594 goto Error;
595 }
596
597 error = FT_New_Face_From_FOND( library, fond, face_index, aface );
598
599Error:
600 CloseResFile( res_ref );
601 return error;
602 }
603
604
605 /*************************************************************************/
606 /* */
607 /* <Function> */
608 /* FT_New_Face_From_FOND */
609 /* */
610 /* <Description> */
611 /* Creates a new face object from an FOND resource. */
612 /* */
613 /* <InOut> */
614 /* library :: A handle to the library resource. */
615 /* */
616 /* <Input> */
617 /* fond :: An FOND resource. */
618 /* */
619 /* face_index :: only supported for the -1 `sanity check' */
620 /* special case. */
621 /* */
622 /* <Output> */
623 /* aface :: A handle to a new face object. */
624 /* */
625 /* <Return> */
626 /* FreeType error code. 0 means success. */
627 /* */
628 /* <Notes> */
629 /* This function can be used to create FT_Face abjects from fonts */
630 /* that are installed in the system like so: */
631 /* fond = GetResource( 'FOND', fontName ); */
632 /* error = FT_New_Face_From_FOND( library, fond, 0, &face ); */
633 /* */
634 FT_EXPORT_FUNC( FT_Error ) FT_New_Face_From_FOND(
635 FT_Library library,
636 Handle fond,
637 FT_Long face_index,
638 FT_Face* aface )
639 {
640 short sfnt_id, have_sfnt, have_lwfn = 0;
641 Str255 lwfn_file_name;
642 short fond_id;
643 OSType fond_type;
644 Str255 fond_name;
645 FSSpec lwfn_spec;
646 FT_Error error = FT_Err_Unknown_File_Format;
647
648
649 GetResInfo(fond, &fond_id, &fond_type, fond_name);
650 if ( ResError() != noErr || fond_type != 'FOND' )
651 return FT_Err_Invalid_File_Format;
652
653 HLock( fond );
654 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name );
655 HUnlock( fond );
656
657 if ( lwfn_file_name[0] )
658 {
659 if ( make_lwfn_spec( fond, lwfn_file_name, &lwfn_spec ) == FT_Err_Ok )
660 have_lwfn = 1; /* yeah, we got one! */
661 else
662 have_lwfn = 0; /* no LWFN file found */
663 }
664
665 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
666 return FT_New_Face_From_LWFN( library,
667 &lwfn_spec,
668 face_index,
669 aface );
670 else if ( have_sfnt )
671 return FT_New_Face_From_SFNT( library,
672 sfnt_id,
673 face_index,
674 aface );
675
676 return FT_Err_Unknown_File_Format;
677 }
678
679
680 /*************************************************************************/
681 /* */
682 /* <Function> */
683 /* FT_New_Face */
684 /* */
685 /* <Description> */
686 /* This is the Mac-specific implementation of FT_New_Face. In */
687 /* addition to the standard FT_New_Face functionality, it also */
688 /* accepts pathnames to Mac suitcase files. For further */
689 /* documentation see the original FT_New_Face in ftobjs.c. */
690 /* */
691 FT_EXPORT_FUNC( FT_Error ) FT_New_Face( FT_Library library,
692 const char* pathname,
693 FT_Long face_index,
694 FT_Face* aface )
695 {
696 FT_Open_Args args;
697 FSSpec spec;
698 OSType file_type;
699
700
701 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
702 if ( !pathname )
703 return FT_Err_Invalid_Argument;
704
705 if ( file_spec_from_path( pathname, &spec ) )
706 return FT_Err_Invalid_Argument;
707
708 file_type = get_file_type( &spec );
709 if ( file_type == 'FFIL' || file_type == 'tfil' )
710 return FT_New_Face_From_Suitcase( library, &spec, face_index, aface );
711 else if ( file_type == 'LWFN' )
712 return FT_New_Face_From_LWFN( library, &spec, face_index, aface );
713 else
714 {
715 args.flags = ft_open_pathname;
716 args.pathname = (char*)pathname;
717
718 return FT_Open_Face( library, &args, face_index, aface );
719 }
720 }
721