ftmac.c 25 KB

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