cffload.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  1. /***************************************************************************/
  2. /* */
  3. /* cffload.c */
  4. /* */
  5. /* OpenType and CFF data/program tables loader (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002 by */
  8. /* 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. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include FT_INTERNAL_POSTSCRIPT_NAMES_H
  22. #include FT_TRUETYPE_TAGS_H
  23. #include "cffload.h"
  24. #include "cffparse.h"
  25. #include "cfferrs.h"
  26. #if 1
  27. static const FT_UShort cff_isoadobe_charset[229] =
  28. {
  29. 0,
  30. 1,
  31. 2,
  32. 3,
  33. 4,
  34. 5,
  35. 6,
  36. 7,
  37. 8,
  38. 9,
  39. 10,
  40. 11,
  41. 12,
  42. 13,
  43. 14,
  44. 15,
  45. 16,
  46. 17,
  47. 18,
  48. 19,
  49. 20,
  50. 21,
  51. 22,
  52. 23,
  53. 24,
  54. 25,
  55. 26,
  56. 27,
  57. 28,
  58. 29,
  59. 30,
  60. 31,
  61. 32,
  62. 33,
  63. 34,
  64. 35,
  65. 36,
  66. 37,
  67. 38,
  68. 39,
  69. 40,
  70. 41,
  71. 42,
  72. 43,
  73. 44,
  74. 45,
  75. 46,
  76. 47,
  77. 48,
  78. 49,
  79. 50,
  80. 51,
  81. 52,
  82. 53,
  83. 54,
  84. 55,
  85. 56,
  86. 57,
  87. 58,
  88. 59,
  89. 60,
  90. 61,
  91. 62,
  92. 63,
  93. 64,
  94. 65,
  95. 66,
  96. 67,
  97. 68,
  98. 69,
  99. 70,
  100. 71,
  101. 72,
  102. 73,
  103. 74,
  104. 75,
  105. 76,
  106. 77,
  107. 78,
  108. 79,
  109. 80,
  110. 81,
  111. 82,
  112. 83,
  113. 84,
  114. 85,
  115. 86,
  116. 87,
  117. 88,
  118. 89,
  119. 90,
  120. 91,
  121. 92,
  122. 93,
  123. 94,
  124. 95,
  125. 96,
  126. 97,
  127. 98,
  128. 99,
  129. 100,
  130. 101,
  131. 102,
  132. 103,
  133. 104,
  134. 105,
  135. 106,
  136. 107,
  137. 108,
  138. 109,
  139. 110,
  140. 111,
  141. 112,
  142. 113,
  143. 114,
  144. 115,
  145. 116,
  146. 117,
  147. 118,
  148. 119,
  149. 120,
  150. 121,
  151. 122,
  152. 123,
  153. 124,
  154. 125,
  155. 126,
  156. 127,
  157. 128,
  158. 129,
  159. 130,
  160. 131,
  161. 132,
  162. 133,
  163. 134,
  164. 135,
  165. 136,
  166. 137,
  167. 138,
  168. 139,
  169. 140,
  170. 141,
  171. 142,
  172. 143,
  173. 144,
  174. 145,
  175. 146,
  176. 147,
  177. 148,
  178. 149,
  179. 150,
  180. 151,
  181. 152,
  182. 153,
  183. 154,
  184. 155,
  185. 156,
  186. 157,
  187. 158,
  188. 159,
  189. 160,
  190. 161,
  191. 162,
  192. 163,
  193. 164,
  194. 165,
  195. 166,
  196. 167,
  197. 168,
  198. 169,
  199. 170,
  200. 171,
  201. 172,
  202. 173,
  203. 174,
  204. 175,
  205. 176,
  206. 177,
  207. 178,
  208. 179,
  209. 180,
  210. 181,
  211. 182,
  212. 183,
  213. 184,
  214. 185,
  215. 186,
  216. 187,
  217. 188,
  218. 189,
  219. 190,
  220. 191,
  221. 192,
  222. 193,
  223. 194,
  224. 195,
  225. 196,
  226. 197,
  227. 198,
  228. 199,
  229. 200,
  230. 201,
  231. 202,
  232. 203,
  233. 204,
  234. 205,
  235. 206,
  236. 207,
  237. 208,
  238. 209,
  239. 210,
  240. 211,
  241. 212,
  242. 213,
  243. 214,
  244. 215,
  245. 216,
  246. 217,
  247. 218,
  248. 219,
  249. 220,
  250. 221,
  251. 222,
  252. 223,
  253. 224,
  254. 225,
  255. 226,
  256. 227,
  257. 228
  258. };
  259. static const FT_UShort cff_expert_charset[166] =
  260. {
  261. 0,
  262. 1,
  263. 229,
  264. 230,
  265. 231,
  266. 232,
  267. 233,
  268. 234,
  269. 235,
  270. 236,
  271. 237,
  272. 238,
  273. 13,
  274. 14,
  275. 15,
  276. 99,
  277. 239,
  278. 240,
  279. 241,
  280. 242,
  281. 243,
  282. 244,
  283. 245,
  284. 246,
  285. 247,
  286. 248,
  287. 27,
  288. 28,
  289. 249,
  290. 250,
  291. 251,
  292. 252,
  293. 253,
  294. 254,
  295. 255,
  296. 256,
  297. 257,
  298. 258,
  299. 259,
  300. 260,
  301. 261,
  302. 262,
  303. 263,
  304. 264,
  305. 265,
  306. 266,
  307. 109,
  308. 110,
  309. 267,
  310. 268,
  311. 269,
  312. 270,
  313. 271,
  314. 272,
  315. 273,
  316. 274,
  317. 275,
  318. 276,
  319. 277,
  320. 278,
  321. 279,
  322. 280,
  323. 281,
  324. 282,
  325. 283,
  326. 284,
  327. 285,
  328. 286,
  329. 287,
  330. 288,
  331. 289,
  332. 290,
  333. 291,
  334. 292,
  335. 293,
  336. 294,
  337. 295,
  338. 296,
  339. 297,
  340. 298,
  341. 299,
  342. 300,
  343. 301,
  344. 302,
  345. 303,
  346. 304,
  347. 305,
  348. 306,
  349. 307,
  350. 308,
  351. 309,
  352. 310,
  353. 311,
  354. 312,
  355. 313,
  356. 314,
  357. 315,
  358. 316,
  359. 317,
  360. 318,
  361. 158,
  362. 155,
  363. 163,
  364. 319,
  365. 320,
  366. 321,
  367. 322,
  368. 323,
  369. 324,
  370. 325,
  371. 326,
  372. 150,
  373. 164,
  374. 169,
  375. 327,
  376. 328,
  377. 329,
  378. 330,
  379. 331,
  380. 332,
  381. 333,
  382. 334,
  383. 335,
  384. 336,
  385. 337,
  386. 338,
  387. 339,
  388. 340,
  389. 341,
  390. 342,
  391. 343,
  392. 344,
  393. 345,
  394. 346,
  395. 347,
  396. 348,
  397. 349,
  398. 350,
  399. 351,
  400. 352,
  401. 353,
  402. 354,
  403. 355,
  404. 356,
  405. 357,
  406. 358,
  407. 359,
  408. 360,
  409. 361,
  410. 362,
  411. 363,
  412. 364,
  413. 365,
  414. 366,
  415. 367,
  416. 368,
  417. 369,
  418. 370,
  419. 371,
  420. 372,
  421. 373,
  422. 374,
  423. 375,
  424. 376,
  425. 377,
  426. 378
  427. };
  428. static const FT_UShort cff_expertsubset_charset[87] =
  429. {
  430. 0,
  431. 1,
  432. 231,
  433. 232,
  434. 235,
  435. 236,
  436. 237,
  437. 238,
  438. 13,
  439. 14,
  440. 15,
  441. 99,
  442. 239,
  443. 240,
  444. 241,
  445. 242,
  446. 243,
  447. 244,
  448. 245,
  449. 246,
  450. 247,
  451. 248,
  452. 27,
  453. 28,
  454. 249,
  455. 250,
  456. 251,
  457. 253,
  458. 254,
  459. 255,
  460. 256,
  461. 257,
  462. 258,
  463. 259,
  464. 260,
  465. 261,
  466. 262,
  467. 263,
  468. 264,
  469. 265,
  470. 266,
  471. 109,
  472. 110,
  473. 267,
  474. 268,
  475. 269,
  476. 270,
  477. 272,
  478. 300,
  479. 301,
  480. 302,
  481. 305,
  482. 314,
  483. 315,
  484. 158,
  485. 155,
  486. 163,
  487. 320,
  488. 321,
  489. 322,
  490. 323,
  491. 324,
  492. 325,
  493. 326,
  494. 150,
  495. 164,
  496. 169,
  497. 327,
  498. 328,
  499. 329,
  500. 330,
  501. 331,
  502. 332,
  503. 333,
  504. 334,
  505. 335,
  506. 336,
  507. 337,
  508. 338,
  509. 339,
  510. 340,
  511. 341,
  512. 342,
  513. 343,
  514. 344,
  515. 345,
  516. 346
  517. };
  518. static const FT_UShort cff_standard_encoding[256] =
  519. {
  520. 0,
  521. 0,
  522. 0,
  523. 0,
  524. 0,
  525. 0,
  526. 0,
  527. 0,
  528. 0,
  529. 0,
  530. 0,
  531. 0,
  532. 0,
  533. 0,
  534. 0,
  535. 0,
  536. 0,
  537. 0,
  538. 0,
  539. 0,
  540. 0,
  541. 0,
  542. 0,
  543. 0,
  544. 0,
  545. 0,
  546. 0,
  547. 0,
  548. 0,
  549. 0,
  550. 0,
  551. 0,
  552. 1,
  553. 2,
  554. 3,
  555. 4,
  556. 5,
  557. 6,
  558. 7,
  559. 8,
  560. 9,
  561. 10,
  562. 11,
  563. 12,
  564. 13,
  565. 14,
  566. 15,
  567. 16,
  568. 17,
  569. 18,
  570. 19,
  571. 20,
  572. 21,
  573. 22,
  574. 23,
  575. 24,
  576. 25,
  577. 26,
  578. 27,
  579. 28,
  580. 29,
  581. 30,
  582. 31,
  583. 32,
  584. 33,
  585. 34,
  586. 35,
  587. 36,
  588. 37,
  589. 38,
  590. 39,
  591. 40,
  592. 41,
  593. 42,
  594. 43,
  595. 44,
  596. 45,
  597. 46,
  598. 47,
  599. 48,
  600. 49,
  601. 50,
  602. 51,
  603. 52,
  604. 53,
  605. 54,
  606. 55,
  607. 56,
  608. 57,
  609. 58,
  610. 59,
  611. 60,
  612. 61,
  613. 62,
  614. 63,
  615. 64,
  616. 65,
  617. 66,
  618. 67,
  619. 68,
  620. 69,
  621. 70,
  622. 71,
  623. 72,
  624. 73,
  625. 74,
  626. 75,
  627. 76,
  628. 77,
  629. 78,
  630. 79,
  631. 80,
  632. 81,
  633. 82,
  634. 83,
  635. 84,
  636. 85,
  637. 86,
  638. 87,
  639. 88,
  640. 89,
  641. 90,
  642. 91,
  643. 92,
  644. 93,
  645. 94,
  646. 95,
  647. 0,
  648. 0,
  649. 0,
  650. 0,
  651. 0,
  652. 0,
  653. 0,
  654. 0,
  655. 0,
  656. 0,
  657. 0,
  658. 0,
  659. 0,
  660. 0,
  661. 0,
  662. 0,
  663. 0,
  664. 0,
  665. 0,
  666. 0,
  667. 0,
  668. 0,
  669. 0,
  670. 0,
  671. 0,
  672. 0,
  673. 0,
  674. 0,
  675. 0,
  676. 0,
  677. 0,
  678. 0,
  679. 0,
  680. 0,
  681. 96,
  682. 97,
  683. 98,
  684. 99,
  685. 100,
  686. 101,
  687. 102,
  688. 103,
  689. 104,
  690. 105,
  691. 106,
  692. 107,
  693. 108,
  694. 109,
  695. 110,
  696. 0,
  697. 111,
  698. 112,
  699. 113,
  700. 114,
  701. 0,
  702. 115,
  703. 116,
  704. 117,
  705. 118,
  706. 119,
  707. 120,
  708. 121,
  709. 122,
  710. 0,
  711. 123,
  712. 0,
  713. 124,
  714. 125,
  715. 126,
  716. 127,
  717. 128,
  718. 129,
  719. 130,
  720. 131,
  721. 0,
  722. 132,
  723. 133,
  724. 0,
  725. 134,
  726. 135,
  727. 136,
  728. 137,
  729. 0,
  730. 0,
  731. 0,
  732. 0,
  733. 0,
  734. 0,
  735. 0,
  736. 0,
  737. 0,
  738. 0,
  739. 0,
  740. 0,
  741. 0,
  742. 0,
  743. 0,
  744. 0,
  745. 138,
  746. 0,
  747. 139,
  748. 0,
  749. 0,
  750. 0,
  751. 0,
  752. 140,
  753. 141,
  754. 142,
  755. 143,
  756. 0,
  757. 0,
  758. 0,
  759. 0,
  760. 0,
  761. 144,
  762. 0,
  763. 0,
  764. 0,
  765. 145,
  766. 0,
  767. 0,
  768. 146,
  769. 147,
  770. 148,
  771. 149,
  772. 0,
  773. 0,
  774. 0,
  775. 0
  776. };
  777. static const FT_UShort cff_expert_encoding[256] =
  778. {
  779. 0,
  780. 0,
  781. 0,
  782. 0,
  783. 0,
  784. 0,
  785. 0,
  786. 0,
  787. 0,
  788. 0,
  789. 0,
  790. 0,
  791. 0,
  792. 0,
  793. 0,
  794. 0,
  795. 0,
  796. 0,
  797. 0,
  798. 0,
  799. 0,
  800. 0,
  801. 0,
  802. 0,
  803. 0,
  804. 0,
  805. 0,
  806. 0,
  807. 0,
  808. 0,
  809. 0,
  810. 0,
  811. 1,
  812. 229,
  813. 230,
  814. 0,
  815. 231,
  816. 232,
  817. 233,
  818. 234,
  819. 235,
  820. 236,
  821. 237,
  822. 238,
  823. 13,
  824. 14,
  825. 15,
  826. 99,
  827. 239,
  828. 240,
  829. 241,
  830. 242,
  831. 243,
  832. 244,
  833. 245,
  834. 246,
  835. 247,
  836. 248,
  837. 27,
  838. 28,
  839. 249,
  840. 250,
  841. 251,
  842. 252,
  843. 0,
  844. 253,
  845. 254,
  846. 255,
  847. 256,
  848. 257,
  849. 0,
  850. 0,
  851. 0,
  852. 258,
  853. 0,
  854. 0,
  855. 259,
  856. 260,
  857. 261,
  858. 262,
  859. 0,
  860. 0,
  861. 263,
  862. 264,
  863. 265,
  864. 0,
  865. 266,
  866. 109,
  867. 110,
  868. 267,
  869. 268,
  870. 269,
  871. 0,
  872. 270,
  873. 271,
  874. 272,
  875. 273,
  876. 274,
  877. 275,
  878. 276,
  879. 277,
  880. 278,
  881. 279,
  882. 280,
  883. 281,
  884. 282,
  885. 283,
  886. 284,
  887. 285,
  888. 286,
  889. 287,
  890. 288,
  891. 289,
  892. 290,
  893. 291,
  894. 292,
  895. 293,
  896. 294,
  897. 295,
  898. 296,
  899. 297,
  900. 298,
  901. 299,
  902. 300,
  903. 301,
  904. 302,
  905. 303,
  906. 0,
  907. 0,
  908. 0,
  909. 0,
  910. 0,
  911. 0,
  912. 0,
  913. 0,
  914. 0,
  915. 0,
  916. 0,
  917. 0,
  918. 0,
  919. 0,
  920. 0,
  921. 0,
  922. 0,
  923. 0,
  924. 0,
  925. 0,
  926. 0,
  927. 0,
  928. 0,
  929. 0,
  930. 0,
  931. 0,
  932. 0,
  933. 0,
  934. 0,
  935. 0,
  936. 0,
  937. 0,
  938. 0,
  939. 0,
  940. 304,
  941. 305,
  942. 306,
  943. 0,
  944. 0,
  945. 307,
  946. 308,
  947. 309,
  948. 310,
  949. 311,
  950. 0,
  951. 312,
  952. 0,
  953. 0,
  954. 312,
  955. 0,
  956. 0,
  957. 314,
  958. 315,
  959. 0,
  960. 0,
  961. 316,
  962. 317,
  963. 318,
  964. 0,
  965. 0,
  966. 0,
  967. 158,
  968. 155,
  969. 163,
  970. 319,
  971. 320,
  972. 321,
  973. 322,
  974. 323,
  975. 324,
  976. 325,
  977. 0,
  978. 0,
  979. 326,
  980. 150,
  981. 164,
  982. 169,
  983. 327,
  984. 328,
  985. 329,
  986. 330,
  987. 331,
  988. 332,
  989. 333,
  990. 334,
  991. 335,
  992. 336,
  993. 337,
  994. 338,
  995. 339,
  996. 340,
  997. 341,
  998. 342,
  999. 343,
  1000. 344,
  1001. 345,
  1002. 346,
  1003. 347,
  1004. 348,
  1005. 349,
  1006. 350,
  1007. 351,
  1008. 352,
  1009. 353,
  1010. 354,
  1011. 355,
  1012. 356,
  1013. 357,
  1014. 358,
  1015. 359,
  1016. 360,
  1017. 361,
  1018. 362,
  1019. 363,
  1020. 364,
  1021. 365,
  1022. 366,
  1023. 367,
  1024. 368,
  1025. 369,
  1026. 370,
  1027. 371,
  1028. 372,
  1029. 373,
  1030. 374,
  1031. 375,
  1032. 376,
  1033. 377,
  1034. 378
  1035. };
  1036. #endif
  1037. FT_LOCAL_DEF( FT_UShort )
  1038. cff_get_standard_encoding( FT_UInt charcode )
  1039. {
  1040. return (FT_UShort)(charcode < 256 ? cff_standard_encoding[charcode] : 0);
  1041. }
  1042. /*************************************************************************/
  1043. /* */
  1044. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  1045. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  1046. /* messages during execution. */
  1047. /* */
  1048. #undef FT_COMPONENT
  1049. #define FT_COMPONENT trace_cffload
  1050. /* read a CFF offset from memory */
  1051. static FT_ULong
  1052. cff_get_offset( FT_Byte* p,
  1053. FT_Byte off_size )
  1054. {
  1055. FT_ULong result;
  1056. for ( result = 0; off_size > 0; off_size-- )
  1057. {
  1058. result <<= 8;
  1059. result |= *p++;
  1060. }
  1061. return result;
  1062. }
  1063. static FT_Error
  1064. cff_new_index( CFF_Index idx,
  1065. FT_Stream stream,
  1066. FT_Bool load )
  1067. {
  1068. FT_Error error;
  1069. FT_Memory memory = stream->memory;
  1070. FT_UShort count;
  1071. FT_MEM_ZERO( idx, sizeof ( *idx ) );
  1072. idx->stream = stream;
  1073. if ( !FT_READ_USHORT( count ) &&
  1074. count > 0 )
  1075. {
  1076. FT_Byte* p;
  1077. FT_Byte offsize;
  1078. FT_ULong data_size;
  1079. FT_ULong* poff;
  1080. /* there is at least one element; read the offset size, */
  1081. /* then access the offset table to compute the index's total size */
  1082. if ( FT_READ_BYTE( offsize ) )
  1083. goto Exit;
  1084. idx->stream = stream;
  1085. idx->count = count;
  1086. idx->off_size = offsize;
  1087. data_size = (FT_ULong)( count + 1 ) * offsize;
  1088. if ( FT_NEW_ARRAY( idx->offsets, count + 1 ) ||
  1089. FT_FRAME_ENTER( data_size ) )
  1090. goto Exit;
  1091. poff = idx->offsets;
  1092. p = (FT_Byte*)stream->cursor;
  1093. for ( ; (FT_Short)count >= 0; count-- )
  1094. {
  1095. poff[0] = cff_get_offset( p, offsize );
  1096. poff++;
  1097. p += offsize;
  1098. }
  1099. FT_FRAME_EXIT();
  1100. idx->data_offset = FT_STREAM_POS();
  1101. data_size = poff[-1] - 1;
  1102. if ( load )
  1103. {
  1104. /* load the data */
  1105. if ( FT_FRAME_EXTRACT( data_size, idx->bytes ) )
  1106. goto Exit;
  1107. }
  1108. else
  1109. {
  1110. /* skip the data */
  1111. if ( FT_STREAM_SKIP( data_size ) )
  1112. goto Exit;
  1113. }
  1114. }
  1115. Exit:
  1116. if ( error )
  1117. FT_FREE( idx->offsets );
  1118. return error;
  1119. }
  1120. static void
  1121. cff_done_index( CFF_Index idx )
  1122. {
  1123. if ( idx->stream )
  1124. {
  1125. FT_Stream stream = idx->stream;
  1126. FT_Memory memory = stream->memory;
  1127. if ( idx->bytes )
  1128. FT_FRAME_RELEASE( idx->bytes );
  1129. FT_FREE( idx->offsets );
  1130. FT_MEM_ZERO( idx, sizeof ( *idx ) );
  1131. }
  1132. }
  1133. /* allocate a table containing pointers to an index's elements */
  1134. static FT_Error
  1135. cff_index_get_pointers( CFF_Index idx,
  1136. FT_Byte*** table )
  1137. {
  1138. FT_Error error = 0;
  1139. FT_Memory memory = idx->stream->memory;
  1140. FT_ULong n, offset, old_offset;
  1141. FT_Byte** t;
  1142. *table = 0;
  1143. if ( idx->count > 0 && !FT_NEW_ARRAY( t, idx->count + 1 ) )
  1144. {
  1145. old_offset = 1;
  1146. for ( n = 0; n <= idx->count; n++ )
  1147. {
  1148. offset = idx->offsets[n];
  1149. if ( !offset )
  1150. offset = old_offset;
  1151. t[n] = idx->bytes + offset - 1;
  1152. old_offset = offset;
  1153. }
  1154. *table = t;
  1155. }
  1156. return error;
  1157. }
  1158. FT_LOCAL_DEF( FT_Error )
  1159. cff_index_access_element( CFF_Index idx,
  1160. FT_UInt element,
  1161. FT_Byte** pbytes,
  1162. FT_ULong* pbyte_len )
  1163. {
  1164. FT_Error error = 0;
  1165. if ( idx && idx->count > element )
  1166. {
  1167. /* compute start and end offsets */
  1168. FT_ULong off1, off2 = 0;
  1169. off1 = idx->offsets[element];
  1170. if ( off1 )
  1171. {
  1172. do
  1173. {
  1174. element++;
  1175. off2 = idx->offsets[element];
  1176. } while ( off2 == 0 && element < idx->count );
  1177. if ( !off2 )
  1178. off1 = 0;
  1179. }
  1180. /* access element */
  1181. if ( off1 )
  1182. {
  1183. *pbyte_len = off2 - off1;
  1184. if ( idx->bytes )
  1185. {
  1186. /* this index was completely loaded in memory, that's easy */
  1187. *pbytes = idx->bytes + off1 - 1;
  1188. }
  1189. else
  1190. {
  1191. /* this index is still on disk/file, access it through a frame */
  1192. FT_Stream stream = idx->stream;
  1193. if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
  1194. FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
  1195. goto Exit;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. /* empty index element */
  1201. *pbytes = 0;
  1202. *pbyte_len = 0;
  1203. }
  1204. }
  1205. else
  1206. error = CFF_Err_Invalid_Argument;
  1207. Exit:
  1208. return error;
  1209. }
  1210. FT_LOCAL_DEF( void )
  1211. cff_index_forget_element( CFF_Index idx,
  1212. FT_Byte** pbytes )
  1213. {
  1214. if ( idx->bytes == 0 )
  1215. {
  1216. FT_Stream stream = idx->stream;
  1217. FT_FRAME_RELEASE( *pbytes );
  1218. }
  1219. }
  1220. FT_LOCAL_DEF( FT_String* )
  1221. cff_index_get_name( CFF_Index idx,
  1222. FT_UInt element )
  1223. {
  1224. FT_Memory memory = idx->stream->memory;
  1225. FT_Byte* bytes;
  1226. FT_ULong byte_len;
  1227. FT_Error error;
  1228. FT_String* name = 0;
  1229. error = cff_index_access_element( idx, element, &bytes, &byte_len );
  1230. if ( error )
  1231. goto Exit;
  1232. if ( !FT_ALLOC( name, byte_len + 1 ) )
  1233. {
  1234. FT_MEM_COPY( name, bytes, byte_len );
  1235. name[byte_len] = 0;
  1236. }
  1237. cff_index_forget_element( idx, &bytes );
  1238. Exit:
  1239. return name;
  1240. }
  1241. FT_LOCAL_DEF( FT_String* )
  1242. cff_index_get_sid_string( CFF_Index idx,
  1243. FT_UInt sid,
  1244. PSNames_Service psnames_service )
  1245. {
  1246. /* if it is not a standard string, return it */
  1247. if ( sid > 390 )
  1248. return cff_index_get_name( idx, sid - 391 );
  1249. /* that's a standard string, fetch a copy from the PSName module */
  1250. {
  1251. FT_String* name = 0;
  1252. const char* adobe_name = psnames_service->adobe_std_strings( sid );
  1253. FT_UInt len;
  1254. if ( adobe_name )
  1255. {
  1256. FT_Memory memory = idx->stream->memory;
  1257. FT_Error error;
  1258. len = (FT_UInt)ft_strlen( adobe_name );
  1259. if ( !FT_ALLOC( name, len + 1 ) )
  1260. {
  1261. FT_MEM_COPY( name, adobe_name, len );
  1262. name[len] = 0;
  1263. }
  1264. FT_UNUSED( error );
  1265. }
  1266. return name;
  1267. }
  1268. }
  1269. /*************************************************************************/
  1270. /*************************************************************************/
  1271. /*** ***/
  1272. /*** FD Select table support ***/
  1273. /*** ***/
  1274. /*************************************************************************/
  1275. /*************************************************************************/
  1276. static void
  1277. CFF_Done_FD_Select( CFF_FDSelect fdselect,
  1278. FT_Stream stream )
  1279. {
  1280. if ( fdselect->data )
  1281. FT_FRAME_RELEASE( fdselect->data );
  1282. fdselect->data_size = 0;
  1283. fdselect->format = 0;
  1284. fdselect->range_count = 0;
  1285. }
  1286. static FT_Error
  1287. CFF_Load_FD_Select( CFF_FDSelect fdselect,
  1288. FT_UInt num_glyphs,
  1289. FT_Stream stream,
  1290. FT_ULong offset )
  1291. {
  1292. FT_Error error;
  1293. FT_Byte format;
  1294. FT_UInt num_ranges;
  1295. /* read format */
  1296. if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) )
  1297. goto Exit;
  1298. fdselect->format = format;
  1299. fdselect->cache_count = 0; /* clear cache */
  1300. switch ( format )
  1301. {
  1302. case 0: /* format 0, that's simple */
  1303. fdselect->data_size = num_glyphs;
  1304. goto Load_Data;
  1305. case 3: /* format 3, a tad more complex */
  1306. if ( FT_READ_USHORT( num_ranges ) )
  1307. goto Exit;
  1308. fdselect->data_size = num_ranges * 3 + 2;
  1309. Load_Data:
  1310. if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) )
  1311. goto Exit;
  1312. break;
  1313. default: /* hmm... that's wrong */
  1314. error = CFF_Err_Invalid_File_Format;
  1315. }
  1316. Exit:
  1317. return error;
  1318. }
  1319. FT_LOCAL_DEF( FT_Byte )
  1320. cff_fd_select_get( CFF_FDSelect fdselect,
  1321. FT_UInt glyph_index )
  1322. {
  1323. FT_Byte fd = 0;
  1324. switch ( fdselect->format )
  1325. {
  1326. case 0:
  1327. fd = fdselect->data[glyph_index];
  1328. break;
  1329. case 3:
  1330. /* first, compare to cache */
  1331. if ( (FT_UInt)( glyph_index - fdselect->cache_first ) <
  1332. fdselect->cache_count )
  1333. {
  1334. fd = fdselect->cache_fd;
  1335. break;
  1336. }
  1337. /* then, lookup the ranges array */
  1338. {
  1339. FT_Byte* p = fdselect->data;
  1340. FT_Byte* p_limit = p + fdselect->data_size;
  1341. FT_Byte fd2;
  1342. FT_UInt first, limit;
  1343. first = FT_NEXT_USHORT( p );
  1344. do
  1345. {
  1346. if ( glyph_index < first )
  1347. break;
  1348. fd2 = *p++;
  1349. limit = FT_NEXT_USHORT( p );
  1350. if ( glyph_index < limit )
  1351. {
  1352. fd = fd2;
  1353. /* update cache */
  1354. fdselect->cache_first = first;
  1355. fdselect->cache_count = limit-first;
  1356. fdselect->cache_fd = fd2;
  1357. break;
  1358. }
  1359. first = limit;
  1360. } while ( p < p_limit );
  1361. }
  1362. break;
  1363. default:
  1364. ;
  1365. }
  1366. return fd;
  1367. }
  1368. /*************************************************************************/
  1369. /*************************************************************************/
  1370. /*** ***/
  1371. /*** CFF font support ***/
  1372. /*** ***/
  1373. /*************************************************************************/
  1374. /*************************************************************************/
  1375. static void
  1376. cff_charset_done( CFF_Charset charset,
  1377. FT_Stream stream )
  1378. {
  1379. FT_Memory memory = stream->memory;
  1380. FT_FREE( charset->sids );
  1381. charset->format = 0;
  1382. charset->offset = 0;
  1383. }
  1384. static FT_Error
  1385. cff_charset_load( CFF_Charset charset,
  1386. FT_UInt num_glyphs,
  1387. FT_Stream stream,
  1388. FT_ULong base_offset,
  1389. FT_ULong offset )
  1390. {
  1391. FT_Memory memory = stream->memory;
  1392. FT_Error error = 0;
  1393. FT_UShort glyph_sid;
  1394. /* If the the offset is greater than 2, we have to parse the */
  1395. /* charset table. */
  1396. if ( offset > 2 )
  1397. {
  1398. FT_UInt j;
  1399. charset->offset = base_offset + offset;
  1400. /* Get the format of the table. */
  1401. if ( FT_STREAM_SEEK( charset->offset ) ||
  1402. FT_READ_BYTE( charset->format ) )
  1403. goto Exit;
  1404. /* Allocate memory for sids. */
  1405. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  1406. goto Exit;
  1407. /* assign the .notdef glyph */
  1408. charset->sids[0] = 0;
  1409. switch ( charset->format )
  1410. {
  1411. case 0:
  1412. if ( num_glyphs > 0 )
  1413. {
  1414. if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) )
  1415. goto Exit;
  1416. for ( j = 1; j < num_glyphs; j++ )
  1417. charset->sids[j] = FT_GET_USHORT();
  1418. FT_FRAME_EXIT();
  1419. }
  1420. break;
  1421. case 1:
  1422. case 2:
  1423. {
  1424. FT_UInt nleft;
  1425. FT_UInt i;
  1426. j = 1;
  1427. while ( j < num_glyphs )
  1428. {
  1429. /* Read the first glyph sid of the range. */
  1430. if ( FT_READ_USHORT( glyph_sid ) )
  1431. goto Exit;
  1432. /* Read the number of glyphs in the range. */
  1433. if ( charset->format == 2 )
  1434. {
  1435. if ( FT_READ_USHORT( nleft ) )
  1436. goto Exit;
  1437. }
  1438. else
  1439. {
  1440. if ( FT_READ_BYTE( nleft ) )
  1441. goto Exit;
  1442. }
  1443. /* Fill in the range of sids -- `nleft + 1' glyphs. */
  1444. for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ )
  1445. charset->sids[j] = glyph_sid;
  1446. }
  1447. }
  1448. break;
  1449. default:
  1450. FT_ERROR(( "cff_charset_load: invalid table format!\n" ));
  1451. error = CFF_Err_Invalid_File_Format;
  1452. goto Exit;
  1453. }
  1454. }
  1455. else
  1456. {
  1457. /* Parse default tables corresponding to offset == 0, 1, or 2. */
  1458. /* CFF specification intimates the following: */
  1459. /* */
  1460. /* In order to use a predefined charset, the following must be */
  1461. /* true: The charset constructed for the glyphs in the font's */
  1462. /* charstrings dictionary must match the predefined charset in */
  1463. /* the first num_glyphs, and hence must match the predefined */
  1464. /* charset *exactly*. */
  1465. charset->offset = offset; /* record charset type */
  1466. switch ( (FT_UInt)offset )
  1467. {
  1468. case 0:
  1469. if ( num_glyphs != 229 )
  1470. {
  1471. FT_ERROR(("cff_charset_load: implicit charset not equal to\n"
  1472. "predefined charset (Adobe ISO-Latin)!\n" ));
  1473. error = CFF_Err_Invalid_File_Format;
  1474. goto Exit;
  1475. }
  1476. /* Allocate memory for sids. */
  1477. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  1478. goto Exit;
  1479. /* Copy the predefined charset into the allocated memory. */
  1480. FT_MEM_COPY( charset->sids, cff_isoadobe_charset,
  1481. num_glyphs * sizeof ( FT_UShort ) );
  1482. break;
  1483. case 1:
  1484. if ( num_glyphs != 166 )
  1485. {
  1486. FT_ERROR(( "cff_charset_load: implicit charset not equal to\n"
  1487. "predefined charset (Adobe Expert)!\n" ));
  1488. error = CFF_Err_Invalid_File_Format;
  1489. goto Exit;
  1490. }
  1491. /* Allocate memory for sids. */
  1492. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  1493. goto Exit;
  1494. /* Copy the predefined charset into the allocated memory. */
  1495. FT_MEM_COPY( charset->sids, cff_expert_charset,
  1496. num_glyphs * sizeof ( FT_UShort ) );
  1497. break;
  1498. case 2:
  1499. if ( num_glyphs != 87 )
  1500. {
  1501. FT_ERROR(( "cff_charset_load: implicit charset not equal to\n"
  1502. "predefined charset (Adobe Expert Subset)!\n" ));
  1503. error = CFF_Err_Invalid_File_Format;
  1504. goto Exit;
  1505. }
  1506. /* Allocate memory for sids. */
  1507. if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) )
  1508. goto Exit;
  1509. /* Copy the predefined charset into the allocated memory. */
  1510. FT_MEM_COPY( charset->sids, cff_expertsubset_charset,
  1511. num_glyphs * sizeof ( FT_UShort ) );
  1512. break;
  1513. default:
  1514. error = CFF_Err_Invalid_File_Format;
  1515. goto Exit;
  1516. }
  1517. }
  1518. Exit:
  1519. /* Clean up if there was an error. */
  1520. if ( error )
  1521. if ( charset->sids )
  1522. {
  1523. FT_FREE( charset->sids );
  1524. charset->format = 0;
  1525. charset->offset = 0;
  1526. charset->sids = 0;
  1527. }
  1528. return error;
  1529. }
  1530. static void
  1531. cff_encoding_done( CFF_Encoding encoding )
  1532. {
  1533. encoding->format = 0;
  1534. encoding->offset = 0;
  1535. encoding->count = 0;
  1536. }
  1537. static FT_Error
  1538. cff_encoding_load( CFF_Encoding encoding,
  1539. CFF_Charset charset,
  1540. FT_UInt num_glyphs,
  1541. FT_Stream stream,
  1542. FT_ULong base_offset,
  1543. FT_ULong offset )
  1544. {
  1545. FT_Error error = 0;
  1546. FT_UInt count;
  1547. FT_UInt j;
  1548. FT_UShort glyph_sid;
  1549. FT_UInt glyph_code;
  1550. /* Check for charset->sids. If we do not have this, we fail. */
  1551. if ( !charset->sids )
  1552. {
  1553. error = CFF_Err_Invalid_File_Format;
  1554. goto Exit;
  1555. }
  1556. /* Zero out the code to gid/sid mappings. */
  1557. for ( j = 0; j < 256; j++ )
  1558. {
  1559. encoding->sids [j] = 0;
  1560. encoding->codes[j] = 0;
  1561. }
  1562. /* Note: The encoding table in a CFF font is indexed by glyph index, */
  1563. /* where the first encoded glyph index is 1. Hence, we read the char */
  1564. /* code (`glyph_code') at index j and make the assignment: */
  1565. /* */
  1566. /* encoding->codes[glyph_code] = j + 1 */
  1567. /* */
  1568. /* We also make the assignment: */
  1569. /* */
  1570. /* encoding->sids[glyph_code] = charset->sids[j + 1] */
  1571. /* */
  1572. /* This gives us both a code to GID and a code to SID mapping. */
  1573. if ( offset > 1 )
  1574. {
  1575. encoding->offset = base_offset + offset;
  1576. /* we need to parse the table to determine its size */
  1577. if ( FT_STREAM_SEEK( encoding->offset ) ||
  1578. FT_READ_BYTE( encoding->format ) ||
  1579. FT_READ_BYTE( count ) )
  1580. goto Exit;
  1581. encoding->count = count + 1;
  1582. switch ( encoding->format & 0x7F )
  1583. {
  1584. case 0:
  1585. {
  1586. FT_Byte* p;
  1587. if ( FT_FRAME_ENTER( count ) )
  1588. goto Exit;
  1589. p = (FT_Byte*)stream->cursor;
  1590. for ( j = 1; j <= count; j++ )
  1591. {
  1592. glyph_code = *p++;
  1593. /* Make sure j is not too big. */
  1594. if ( (FT_UInt) glyph_code < num_glyphs )
  1595. {
  1596. /* Assign code to GID mapping. */
  1597. encoding->codes[glyph_code] = (FT_UShort)j;
  1598. /* Assign code to SID mapping. */
  1599. encoding->sids[glyph_code] = charset->sids[j];
  1600. }
  1601. }
  1602. FT_FRAME_EXIT();
  1603. }
  1604. break;
  1605. case 1:
  1606. {
  1607. FT_Byte nleft;
  1608. FT_UInt i = 1;
  1609. FT_UInt k;
  1610. /* Parse the Format1 ranges. */
  1611. for ( j = 0; j < count; j++, i += nleft )
  1612. {
  1613. /* Read the first glyph code of the range. */
  1614. if ( FT_READ_BYTE( glyph_code ) )
  1615. goto Exit;
  1616. /* Read the number of codes in the range. */
  1617. if ( FT_READ_BYTE( nleft ) )
  1618. goto Exit;
  1619. /* Increment nleft, so we read `nleft + 1' codes/sids. */
  1620. nleft++;
  1621. /* Fill in the range of codes/sids. */
  1622. for ( k = i; k < nleft + i; k++, glyph_code++ )
  1623. {
  1624. /* Make sure k is not too big. */
  1625. if ( k < num_glyphs && glyph_code < 256 )
  1626. {
  1627. /* Assign code to GID mapping. */
  1628. encoding->codes[glyph_code] = (FT_UShort)k;
  1629. /* Assign code to SID mapping. */
  1630. encoding->sids[glyph_code] = charset->sids[k];
  1631. }
  1632. }
  1633. }
  1634. }
  1635. break;
  1636. default:
  1637. FT_ERROR(( "cff_encoding_load: invalid table format!\n" ));
  1638. error = CFF_Err_Invalid_File_Format;
  1639. goto Exit;
  1640. }
  1641. /* Parse supplemental encodings, if any. */
  1642. if ( encoding->format & 0x80 )
  1643. {
  1644. FT_UInt gindex;
  1645. /* count supplements */
  1646. if ( FT_READ_BYTE( count ) )
  1647. goto Exit;
  1648. for ( j = 0; j < count; j++ )
  1649. {
  1650. /* Read supplemental glyph code. */
  1651. if ( FT_READ_BYTE( glyph_code ) )
  1652. goto Exit;
  1653. /* Read the SID associated with this glyph code. */
  1654. if ( FT_READ_USHORT( glyph_sid ) )
  1655. goto Exit;
  1656. /* Assign code to SID mapping. */
  1657. encoding->sids[glyph_code] = glyph_sid;
  1658. /* First, lookup GID which has been assigned to */
  1659. /* SID glyph_sid. */
  1660. for ( gindex = 0; gindex < num_glyphs; gindex++ )
  1661. {
  1662. if ( charset->sids[gindex] == glyph_sid )
  1663. {
  1664. encoding->codes[glyph_code] = (FT_UShort) gindex;
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. }
  1670. }
  1671. else
  1672. {
  1673. FT_UInt i;
  1674. /* We take into account the fact a CFF font can use a predefined */
  1675. /* encoding without containing all of the glyphs encoded by this */
  1676. /* encoding (see the note at the end of section 12 in the CFF */
  1677. /* specification). */
  1678. encoding->count = 256;
  1679. switch ( (FT_UInt)offset )
  1680. {
  1681. case 0:
  1682. /* First, copy the code to SID mapping. */
  1683. FT_MEM_COPY( encoding->sids, cff_standard_encoding,
  1684. 256 * sizeof ( FT_UShort ) );
  1685. goto Populate;
  1686. case 1:
  1687. /* First, copy the code to SID mapping. */
  1688. FT_MEM_COPY( encoding->sids, cff_expert_encoding,
  1689. 256 * sizeof ( FT_UShort ) );
  1690. Populate:
  1691. /* Construct code to GID mapping from code to SID mapping */
  1692. /* and charset. */
  1693. for ( j = 0; j < 256; j++ )
  1694. {
  1695. /* If j is encoded, find the GID for it. */
  1696. if ( encoding->sids[j] )
  1697. {
  1698. for ( i = 1; i < num_glyphs; i++ )
  1699. /* We matched, so break. */
  1700. if ( charset->sids[i] == encoding->sids[j] )
  1701. break;
  1702. /* i will be equal to num_glyphs if we exited the above */
  1703. /* loop without a match. In this case, we also have to */
  1704. /* fix the code to SID mapping. */
  1705. if ( i == num_glyphs )
  1706. {
  1707. encoding->codes[j] = 0;
  1708. encoding->sids [j] = 0;
  1709. }
  1710. else
  1711. encoding->codes[j] = (FT_UShort)i;
  1712. }
  1713. }
  1714. break;
  1715. default:
  1716. FT_ERROR(( "cff_encoding_load: invalid table format!\n" ));
  1717. error = CFF_Err_Invalid_File_Format;
  1718. goto Exit;
  1719. }
  1720. }
  1721. Exit:
  1722. /* Clean up if there was an error. */
  1723. return error;
  1724. }
  1725. static FT_Error
  1726. cff_subfont_load( CFF_SubFont font,
  1727. CFF_Index idx,
  1728. FT_UInt font_index,
  1729. FT_Stream stream,
  1730. FT_ULong base_offset )
  1731. {
  1732. FT_Error error;
  1733. CFF_ParserRec parser;
  1734. FT_Byte* dict;
  1735. FT_ULong dict_len;
  1736. CFF_FontRecDict top = &font->font_dict;
  1737. CFF_Private priv = &font->private_dict;
  1738. cff_parser_init( &parser, CFF_CODE_TOPDICT, &font->font_dict );
  1739. /* set defaults */
  1740. FT_MEM_ZERO( top, sizeof ( *top ) );
  1741. top->underline_position = -100;
  1742. top->underline_thickness = 50;
  1743. top->charstring_type = 2;
  1744. top->font_matrix.xx = 0x10000L;
  1745. top->font_matrix.yy = 0x10000L;
  1746. top->cid_count = 8720;
  1747. error = cff_index_access_element( idx, font_index, &dict, &dict_len ) ||
  1748. cff_parser_run( &parser, dict, dict + dict_len );
  1749. cff_index_forget_element( idx, &dict );
  1750. if ( error )
  1751. goto Exit;
  1752. /* if it is a CID font, we stop there */
  1753. if ( top->cid_registry )
  1754. goto Exit;
  1755. /* parse the private dictionary, if any */
  1756. if ( top->private_offset && top->private_size )
  1757. {
  1758. /* set defaults */
  1759. FT_MEM_ZERO( priv, sizeof ( *priv ) );
  1760. priv->blue_shift = 7;
  1761. priv->blue_fuzz = 1;
  1762. priv->lenIV = -1;
  1763. priv->expansion_factor = (FT_Fixed)0.06 * 0x10000L;
  1764. priv->blue_scale = (FT_Fixed)0.039625 * 0x10000L;
  1765. cff_parser_init( &parser, CFF_CODE_PRIVATE, priv );
  1766. if ( FT_STREAM_SEEK( base_offset + font->font_dict.private_offset ) ||
  1767. FT_FRAME_ENTER( font->font_dict.private_size ) )
  1768. goto Exit;
  1769. error = cff_parser_run( &parser,
  1770. (FT_Byte*)stream->cursor,
  1771. (FT_Byte*)stream->limit );
  1772. FT_FRAME_EXIT();
  1773. if ( error )
  1774. goto Exit;
  1775. }
  1776. /* read the local subrs, if any */
  1777. if ( priv->local_subrs_offset )
  1778. {
  1779. if ( FT_STREAM_SEEK( base_offset + top->private_offset +
  1780. priv->local_subrs_offset ) )
  1781. goto Exit;
  1782. error = cff_new_index( &font->local_subrs_index, stream, 1 );
  1783. if ( error )
  1784. goto Exit;
  1785. font->num_local_subrs = font->local_subrs_index.count;
  1786. error = cff_index_get_pointers( &font->local_subrs_index,
  1787. &font->local_subrs );
  1788. if ( error )
  1789. goto Exit;
  1790. }
  1791. Exit:
  1792. return error;
  1793. }
  1794. static void
  1795. cff_subfont_done( FT_Memory memory,
  1796. CFF_SubFont subfont )
  1797. {
  1798. if ( subfont )
  1799. {
  1800. cff_done_index( &subfont->local_subrs_index );
  1801. FT_FREE( subfont->local_subrs );
  1802. }
  1803. }
  1804. FT_LOCAL_DEF( FT_Error )
  1805. cff_font_load( FT_Stream stream,
  1806. FT_Int face_index,
  1807. CFF_Font font )
  1808. {
  1809. static const FT_Frame_Field cff_header_fields[] =
  1810. {
  1811. #undef FT_STRUCTURE
  1812. #define FT_STRUCTURE CFF_FontRec
  1813. FT_FRAME_START( 4 ),
  1814. FT_FRAME_BYTE( version_major ),
  1815. FT_FRAME_BYTE( version_minor ),
  1816. FT_FRAME_BYTE( header_size ),
  1817. FT_FRAME_BYTE( absolute_offsize ),
  1818. FT_FRAME_END
  1819. };
  1820. FT_Error error;
  1821. FT_Memory memory = stream->memory;
  1822. FT_ULong base_offset;
  1823. CFF_FontRecDict dict;
  1824. FT_ZERO( font );
  1825. font->stream = stream;
  1826. font->memory = memory;
  1827. dict = &font->top_font.font_dict;
  1828. base_offset = FT_STREAM_POS();
  1829. /* read CFF font header */
  1830. if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) )
  1831. goto Exit;
  1832. /* check format */
  1833. if ( font->version_major != 1 ||
  1834. font->header_size < 4 ||
  1835. font->absolute_offsize > 4 )
  1836. {
  1837. FT_TRACE2(( "[not a CFF font header!]\n" ));
  1838. error = CFF_Err_Unknown_File_Format;
  1839. goto Exit;
  1840. }
  1841. /* skip the rest of the header */
  1842. if ( FT_STREAM_SKIP( font->header_size - 4 ) )
  1843. goto Exit;
  1844. /* read the name, top dict, string and global subrs index */
  1845. if ( FT_SET_ERROR( cff_new_index( &font->name_index, stream, 0 )) ||
  1846. FT_SET_ERROR( cff_new_index( &font->font_dict_index, stream, 0 )) ||
  1847. FT_SET_ERROR( cff_new_index( &font->string_index, stream, 0 )) ||
  1848. FT_SET_ERROR( cff_new_index( &font->global_subrs_index, stream, 1 )) )
  1849. goto Exit;
  1850. /* well, we don't really forget the `disabled' fonts... */
  1851. font->num_faces = font->name_index.count;
  1852. if ( face_index >= (FT_Int)font->num_faces )
  1853. {
  1854. FT_ERROR(( "cff_font_load: incorrect face index = %d\n",
  1855. face_index ));
  1856. error = CFF_Err_Invalid_Argument;
  1857. }
  1858. /* in case of a font format check, simply exit now */
  1859. if ( face_index < 0 )
  1860. goto Exit;
  1861. /* now, parse the top-level font dictionary */
  1862. error = cff_subfont_load( &font->top_font,
  1863. &font->font_dict_index,
  1864. face_index,
  1865. stream,
  1866. base_offset );
  1867. if ( error )
  1868. goto Exit;
  1869. /* now, check for a CID font */
  1870. if ( dict->cid_registry )
  1871. {
  1872. CFF_IndexRec fd_index;
  1873. CFF_SubFont sub;
  1874. FT_UInt idx;
  1875. /* this is a CID-keyed font, we must now allocate a table of */
  1876. /* sub-fonts, then load each of them separately */
  1877. if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
  1878. goto Exit;
  1879. error = cff_new_index( &fd_index, stream, 0 );
  1880. if ( error )
  1881. goto Exit;
  1882. if ( fd_index.count > CFF_MAX_CID_FONTS )
  1883. {
  1884. FT_ERROR(( "cff_font_load: FD array too large in CID font\n" ));
  1885. goto Fail_CID;
  1886. }
  1887. /* allocate & read each font dict independently */
  1888. font->num_subfonts = fd_index.count;
  1889. if ( FT_NEW_ARRAY( sub, fd_index.count ) )
  1890. goto Fail_CID;
  1891. /* setup pointer table */
  1892. for ( idx = 0; idx < fd_index.count; idx++ )
  1893. font->subfonts[idx] = sub + idx;
  1894. /* now load each sub font independently */
  1895. for ( idx = 0; idx < fd_index.count; idx++ )
  1896. {
  1897. sub = font->subfonts[idx];
  1898. error = cff_subfont_load( sub, &fd_index, idx,
  1899. stream, base_offset );
  1900. if ( error )
  1901. goto Fail_CID;
  1902. }
  1903. /* now load the FD Select array */
  1904. error = CFF_Load_FD_Select( &font->fd_select,
  1905. (FT_UInt)dict->cid_count,
  1906. stream,
  1907. base_offset + dict->cid_fd_select_offset );
  1908. Fail_CID:
  1909. cff_done_index( &fd_index );
  1910. if ( error )
  1911. goto Exit;
  1912. }
  1913. else
  1914. font->num_subfonts = 0;
  1915. /* read the charstrings index now */
  1916. if ( dict->charstrings_offset == 0 )
  1917. {
  1918. FT_ERROR(( "cff_font_load: no charstrings offset!\n" ));
  1919. error = CFF_Err_Unknown_File_Format;
  1920. goto Exit;
  1921. }
  1922. if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
  1923. goto Exit;
  1924. error = cff_new_index( &font->charstrings_index, stream, 0 );
  1925. if ( error )
  1926. goto Exit;
  1927. /* explicit the global subrs */
  1928. font->num_global_subrs = font->global_subrs_index.count;
  1929. font->num_glyphs = font->charstrings_index.count;
  1930. error = cff_index_get_pointers( &font->global_subrs_index,
  1931. &font->global_subrs ) ;
  1932. if ( error )
  1933. goto Exit;
  1934. /* read the Charset and Encoding tables when available */
  1935. if ( font->num_glyphs > 0 )
  1936. {
  1937. error = cff_charset_load( &font->charset, font->num_glyphs, stream,
  1938. base_offset, dict->charset_offset );
  1939. if ( error )
  1940. goto Exit;
  1941. error = cff_encoding_load( &font->encoding,
  1942. &font->charset,
  1943. font->num_glyphs,
  1944. stream,
  1945. base_offset,
  1946. dict->encoding_offset );
  1947. if ( error )
  1948. goto Exit;
  1949. }
  1950. /* get the font name */
  1951. font->font_name = cff_index_get_name( &font->name_index, face_index );
  1952. Exit:
  1953. return error;
  1954. }
  1955. FT_LOCAL_DEF( void )
  1956. cff_font_done( CFF_Font font )
  1957. {
  1958. FT_Memory memory = font->memory;
  1959. FT_UInt idx;
  1960. cff_done_index( &font->global_subrs_index );
  1961. cff_done_index( &font->string_index );
  1962. cff_done_index( &font->font_dict_index );
  1963. cff_done_index( &font->name_index );
  1964. cff_done_index( &font->charstrings_index );
  1965. /* release font dictionaries, but only if working with */
  1966. /* a CID keyed CFF font */
  1967. if ( font->num_subfonts > 0 )
  1968. {
  1969. for ( idx = 0; idx < font->num_subfonts; idx++ )
  1970. cff_subfont_done( memory, font->subfonts[idx] );
  1971. FT_FREE( font->subfonts );
  1972. }
  1973. cff_encoding_done( &font->encoding );
  1974. cff_charset_done( &font->charset, font->stream );
  1975. cff_subfont_done( memory, &font->top_font );
  1976. CFF_Done_FD_Select( &font->fd_select, font->stream );
  1977. FT_FREE( font->global_subrs );
  1978. FT_FREE( font->font_name );
  1979. }
  1980. /* END */