blob: 617c7868757a7c0b74819c5c0758cf2badd0210a [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 );
471 if ( !error )
472 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
473 FREE( stream ); /* FT_Open_Face() made a copy */
474 return error;
475 }
476
477
478 /* Create a new FT_Face from a file spec to an LWFN file. */
479 static
480 FT_Error FT_New_Face_From_LWFN( FT_Library library,
481 FSSpec* spec,
482 FT_Long face_index,
483 FT_Face* aface )
484 {
485 FT_Byte* pfb_data;
486 FT_ULong pfb_size;
487 FT_Error error;
488 FT_Memory memory = library->memory;
489
490
491 error = read_lwfn( library->memory, spec, &pfb_data, &pfb_size );
492 if ( error )
493 return error;
494
495#if 0
496 {
497 FILE* f;
498 char * path;
499
500 path = p2c_str( spec->name );
501 strcat( path, ".PFB" );
502 f = fopen(path, "wb");
503 if ( f )
504 {
505 fwrite( pfb_data, 1, pfb_size, f );
506 fclose( f );
507 }
508 }
509#endif
510
511 return open_face_from_buffer( library,
512 pfb_data,
513 pfb_size,
514 face_index,
515 "type1",
516 aface );
517 }
518
519
520 /* Create a new FT_Face from an SFNT resource, specified by res ID. */
521 static
522 FT_Error FT_New_Face_From_SFNT( FT_Library library,
523 short sfnt_id,
524 FT_Long face_index,
525 FT_Face* aface )
526 {
527 Handle sfnt = NULL;
528 FT_Byte* sfnt_data;
529 size_t sfnt_size;
530 FT_Stream stream = NULL;
531 FT_Error error = 0;
532 FT_Memory memory = library->memory;
533
534
535 sfnt = GetResource( 'sfnt', sfnt_id );
536 if ( ResError() )
537 return FT_Err_Invalid_Handle;
538
539 sfnt_size = (FT_ULong)GetHandleSize( sfnt );
540 if ( ALLOC( sfnt_data, (FT_Long)sfnt_size ) )
541 {
542 ReleaseResource( sfnt );
543 return error;
544 }
545
546 HLock( sfnt );
547 memcpy( sfnt_data, *sfnt, sfnt_size);
548 HUnlock( sfnt );
549 ReleaseResource( sfnt );
550
551 return open_face_from_buffer( library,
552 sfnt_data,
553 sfnt_size,
554 face_index,
555 "truetype",
556 aface );
557 }
558
559
560 /* Create a new FT_Face from a file spec to a suitcase file. */
561 static
562 FT_Error FT_New_Face_From_Suitcase( FT_Library library,
563 FSSpec* spec,
564 FT_Long face_index,
565 FT_Face* aface )
566 {
567 FT_Error error = FT_Err_Ok;
568 short res_ref, res_index;
569 Handle fond;
570
571
572 res_ref = FSpOpenResFile( spec, fsRdPerm );
573 if ( ResError() )
574 return FT_Err_Cannot_Open_Resource;
575 UseResFile( res_ref );
576
577 /* face_index may be -1, in which case we
578 just need to do a sanity check */
579 if ( face_index < 0)
580 res_index = 1;
581 else
582 {
583 res_index = (short)(face_index + 1);
584 face_index = 0;
585 }
586 fond = Get1IndResource( 'FOND', res_index );
587 if ( ResError() )
588 {
589 error = FT_Err_Cannot_Open_Resource;
590 goto Error;
591 }
592
593 error = FT_New_Face_From_FOND( library, fond, face_index, aface );
594
595Error:
596 CloseResFile( res_ref );
597 return error;
598 }
599
600
601 /*************************************************************************/
602 /* */
603 /* <Function> */
604 /* FT_New_Face_From_FOND */
605 /* */
606 /* <Description> */
607 /* Creates a new face object from an FOND resource. */
608 /* */
609 /* <InOut> */
610 /* library :: A handle to the library resource. */
611 /* */
612 /* <Input> */
613 /* fond :: An FOND resource. */
614 /* */
615 /* face_index :: only supported for the -1 `sanity check' */
616 /* special case. */
617 /* */
618 /* <Output> */
619 /* aface :: A handle to a new face object. */
620 /* */
621 /* <Return> */
622 /* FreeType error code. 0 means success. */
623 /* */
624 /* <Notes> */
625 /* This function can be used to create FT_Face abjects from fonts */
626 /* that are installed in the system like so: */
627 /* fond = GetResource( 'FOND', fontName ); */
628 /* error = FT_New_Face_From_FOND( library, fond, 0, &face ); */
629 /* */
630 FT_EXPORT_FUNC( FT_Error ) FT_New_Face_From_FOND(
631 FT_Library library,
632 Handle fond,
633 FT_Long face_index,
634 FT_Face* aface )
635 {
636 short sfnt_id, have_sfnt, have_lwfn = 0;
637 Str255 lwfn_file_name;
638 short fond_id;
639 OSType fond_type;
640 Str255 fond_name;
641 FSSpec lwfn_spec;
642 FT_Error error = FT_Err_Unknown_File_Format;
643
644
645 GetResInfo(fond, &fond_id, &fond_type, fond_name);
646 if ( ResError() != noErr || fond_type != 'FOND' )
647 return FT_Err_Invalid_File_Format;
648
649 HLock( fond );
650 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name );
651 HUnlock( fond );
652
653 if ( lwfn_file_name[0] )
654 {
655 if ( make_lwfn_spec( fond, lwfn_file_name, &lwfn_spec ) == FT_Err_Ok )
656 have_lwfn = 1; /* yeah, we got one! */
657 else
658 have_lwfn = 0; /* no LWFN file found */
659 }
660
661 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
662 return FT_New_Face_From_LWFN( library,
663 &lwfn_spec,
664 face_index,
665 aface );
666 else if ( have_sfnt )
667 return FT_New_Face_From_SFNT( library,
668 sfnt_id,
669 face_index,
670 aface );
671
672 return FT_Err_Unknown_File_Format;
673 }
674
675
676 /*************************************************************************/
677 /* */
678 /* <Function> */
679 /* FT_New_Face */
680 /* */
681 /* <Description> */
682 /* This is the Mac-specific implementation of FT_New_Face. In */
683 /* addition to the standard FT_New_Face functionality, it also */
684 /* accepts pathnames to Mac suitcase files. For further */
685 /* documentation see the original FT_New_Face in ftobjs.c. */
686 /* */
687 FT_EXPORT_FUNC( FT_Error ) FT_New_Face( FT_Library library,
688 const char* pathname,
689 FT_Long face_index,
690 FT_Face* aface )
691 {
692 FT_Open_Args args;
693 FSSpec spec;
694 OSType file_type;
695
696
697 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
698 if ( !pathname )
699 return FT_Err_Invalid_Argument;
700
701 if ( file_spec_from_path( pathname, &spec ) )
702 return FT_Err_Invalid_Argument;
703
704 file_type = get_file_type( &spec );
705 if ( file_type == 'FFIL' || file_type == 'tfil' )
706 return FT_New_Face_From_Suitcase( library, &spec, face_index, aface );
707 else if ( file_type == 'LWFN' )
708 return FT_New_Face_From_LWFN( library, &spec, face_index, aface );
709 else
710 {
711 args.flags = ft_open_pathname;
712 args.pathname = (char*)pathname;
713
714 return FT_Open_Face( library, &args, face_index, aface );
715 }
716 }
717