Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1376 lines
42 KiB

  1. /***
  2. **
  3. ** Module: T1Parser
  4. **
  5. ** Description:
  6. ** This is a module of the T1 to TT font converter. The module
  7. ** will extract information from a T1 font file, by parsing
  8. ** the data/commands found in PFB, PFM and AFM files.
  9. **
  10. ** Author: Michael Jansson
  11. **
  12. ** Created: 5/26/93
  13. **
  14. ***/
  15. /**** INCLUDES */
  16. /* General types and definitions. */
  17. #include <string.h>
  18. #include "types.h"
  19. /* Special types and definitions. */
  20. #include "safemem.h"
  21. #include "encoding.h"
  22. #include "metrics.h"
  23. #include "t1msg.h"
  24. /* Module dependent types and prototypes. */
  25. #include "titott.h"
  26. #include "t1parser.h"
  27. #include "charstr.h"
  28. #include "freader.h"
  29. #include "mreader.h"
  30. /***** CONSTANTS */
  31. #define ONE (USHORT)1
  32. #define BUFLEN (USHORT)512
  33. #define PS_ANGLE "/ItalicAngle"
  34. #define PS_ARRAY "array"
  35. #define PS_BEGIN "begin"
  36. #define PS_BLUEFUZZ "/BlueFuzz"
  37. #define PS_BLUESCALE "/BlueScale"
  38. #define PS_BLUESHIFT "/BlueShift"
  39. #define PS_BLUEVALUES "/BlueValues"
  40. #define PS_CHARSTRINGS "/CharStrings"
  41. #define PS_COPYRIGHT "/Copyright"
  42. #define PS_DATE "%%CreationDate:"
  43. #define PS_DUP "dup"
  44. #define PS_ENCODING "/Encoding"
  45. #define PS_END "end"
  46. #define PS_FAMILY "/FamilyName"
  47. #define PS_FAMILYBLUES "/FamilyBlues"
  48. #define PS_FAMILYOTHERBLUES "/FamilyOtherBlues"
  49. #define PS_FONTMATRIX "/FontMatrix"
  50. #define PS_FORCEBOLD "/ForceBold"
  51. #define PS_FULLNAME "/FullName"
  52. #define PS_HYBRID "hires"
  53. #define PS_ISFIXED "/isFixedPitch"
  54. #define PS_LENIV "/lenIV"
  55. #define PS_NAME "/FontName"
  56. #define PS_NOACCESS "noaccess"
  57. #define PS_NOTICE "/Notice"
  58. #define PS_OTHERBLUES "/OtherBlues"
  59. #define PS_SNAPH "/StemSnapH"
  60. #define PS_SNAPV "/StemSnapV"
  61. #define PS_STDENCODING "StandardEncoding"
  62. #define PS_STDVW "/StdVW"
  63. #define PS_STDHW "/StdHW"
  64. #define PS_SUBRS "/Subrs"
  65. #define PS_UNDERLINE "/UnderlinePosition"
  66. #define PS_UTHICK "/UnderlineThickness"
  67. #define PS_ID "/UniqueID"
  68. #define PS_VERSION "/version"
  69. #define PS_WEIGHT "/Weight"
  70. /***** LOCAL TYPES */
  71. struct T1Handle {
  72. struct FontFile *ff;
  73. struct PSState *ps;
  74. struct Subrs stdenc[256];
  75. USHORT numsubrs;
  76. struct Subrs *subrs;
  77. USHORT leniv;
  78. struct T1Metrics t1m;
  79. };
  80. /***** MACROS */
  81. /*-none-*/
  82. /***** PROTOTYPES */
  83. /*-none-*/
  84. /***** STATIC FUNCTIONS */
  85. /***
  86. ** Function: StrToFix
  87. **
  88. ** Description:
  89. ** This is a "strtod" function, that converts from
  90. ** ascii to fixpoint numbers.
  91. ***/
  92. static long StrToFix(char *str, char **out, const long base)
  93. {
  94. char *fstr;
  95. long num = 0, frac = 0, exp = 0;
  96. if (out)
  97. (*out) = str;
  98. /* Skip white space. */
  99. while (*str && (*str==' ' || *str=='\t'))
  100. str++;
  101. /* A number? */
  102. if (*str && ((*str>='0' && *str<='9') || *str=='-') || *str=='.') {
  103. num = atoi(str)*base;
  104. /* Fraction? */
  105. fstr = strchr(str, '.');
  106. if (fstr!=NULL && (strchr(str, ' ')==NULL || fstr<strchr(str, ' '))) {
  107. do {
  108. fstr++;
  109. } while (*fstr>='0' && *fstr<='9');
  110. /* Exponent? */
  111. if (*fstr=='E')
  112. exp = atoi(fstr+1);
  113. else
  114. exp = 0;
  115. fstr--;
  116. while (*fstr!='.') {
  117. frac += ((*fstr)-'0')*base;
  118. frac /= 10;
  119. fstr--;
  120. }
  121. if (num<0)
  122. num -= frac;
  123. else
  124. num += frac;
  125. /* Handle exponent. */
  126. if (exp>0) {
  127. do {
  128. num *= 10;
  129. } while (--exp);
  130. } else if (exp<0) {
  131. do {
  132. num /= 10;
  133. } while (++exp);
  134. }
  135. }
  136. /* Skip digits. */
  137. while (*str && ((*str>='0' && *str<='9') ||
  138. *str=='.' || *str=='-' || *str=='E'))
  139. str++;
  140. if (out)
  141. (*out) = str;
  142. }
  143. return num;
  144. }
  145. /***
  146. ** Function: FreeT1Composite
  147. **
  148. ** Description:
  149. ** This function frees the memory used to represent
  150. ** a composite acented T1 glyph.
  151. ***/
  152. static void FreeT1Composite(Composite *comp)
  153. {
  154. if (comp) {
  155. if (comp->cchar)
  156. Free(comp->cchar);
  157. Free(comp);
  158. }
  159. }
  160. /***
  161. ** Function: UseGlyph
  162. **
  163. ** Description:
  164. ** This function determines whether a glyph should be
  165. ** converted or not, based on the name of the glyph
  166. ** and a specification of the desired glyphs.
  167. ***/
  168. static int CDECL compare(const void *arg1, const void *arg2)
  169. {
  170. return strcmp( *((const char **)arg1), *((const char **)arg2) );
  171. }
  172. static boolean UseGlyph(const struct GlyphFilter *filter,
  173. Composite *comp,
  174. const char *name)
  175. {
  176. boolean found = FALSE;
  177. char **result;
  178. /* Check if the glyph is explicitly specified. */
  179. if (filter) {
  180. result = (char **)bsearch((char *)&name,
  181. (char *)filter->name, filter->num,
  182. sizeof(char *),
  183. compare);
  184. found = (boolean)(result!=NULL);
  185. /* Check if the glyph is specified indirectly through an accented */
  186. /* composite glyph. */
  187. if (!found) {
  188. Composite *c;
  189. for (c=comp; c &&
  190. strcmp(name, c->achar) &&
  191. strcmp(name, c->bchar); c = c->next);
  192. found = (boolean)(c!=NULL);
  193. }
  194. } else {
  195. found = TRUE;
  196. }
  197. return found;
  198. }
  199. /***
  200. ** Function: ReadFontMatrix
  201. **
  202. ** Description:
  203. ** Read the command sequence "/FontMatrix[%d %d %d %d]" and
  204. ** record the transformation matrix in the T1 handle.
  205. ***/
  206. static errcode ReadFontMatrix(struct T1Handle *t1,
  207. char *str,
  208. const USHORT len)
  209. {
  210. errcode status=SUCCESS;
  211. f16d16 fmatrix[6];
  212. USHORT i;
  213. if (GetSeq(t1->ff, str, len)) {
  214. for (i=0; i<6; i++)
  215. fmatrix[i] = StrToFix(str, &str, F16D16BASE);
  216. /* Check if we have the default matrix. */ /*lint -e771 */
  217. if (fmatrix[2]!=0 ||
  218. fmatrix[4]!=0 ||
  219. fmatrix[1]!=0 ||
  220. fmatrix[5]!=0 ||
  221. fmatrix[0]!=F16D16PPM ||
  222. fmatrix[3]!=F16D16PPM ||
  223. t1->t1m.upem!=2048) { /*lint +e771 */ /* fmatrix[] IS initialized */
  224. if ((t1->t1m.fmatrix = Malloc(sizeof(f16d16)*6))==NULL) {
  225. SetError(status = NOMEM);
  226. } else {
  227. t1->t1m.fmatrix[0] = fmatrix[0];
  228. t1->t1m.fmatrix[1] = fmatrix[1];
  229. t1->t1m.fmatrix[2] = fmatrix[2];
  230. t1->t1m.fmatrix[3] = fmatrix[3];
  231. t1->t1m.fmatrix[4] = fmatrix[4];
  232. t1->t1m.fmatrix[5] = fmatrix[5];
  233. }
  234. } else {
  235. t1->t1m.fmatrix = NULL;
  236. }
  237. } else {
  238. SetError(status = BADINPUTFILE);
  239. }
  240. return status;
  241. }
  242. /***
  243. ** Function: ReadEncodingArray
  244. **
  245. ** Description:
  246. ** Read the command sequence "/Encoding %d array ..." and
  247. ** build an encoding table, or read "/Encoding StdEncoding def"
  248. ** and used the standard encoding table.
  249. ***/
  250. static errcode ReadEncodingArray(struct T1Handle *t1,
  251. char *str,
  252. const USHORT len)
  253. {
  254. errcode status = SUCCESS;
  255. USHORT codes[ENC_MAXCODES];
  256. char *glyph_name = NULL;
  257. USHORT i, index;
  258. if (Get_Token(t1->ff, str, len)==NULL) {
  259. SetError(status = BADINPUTFILE);
  260. } else {
  261. if (strcmp(str, PS_STDENCODING) &&
  262. ((t1->t1m.encSize=(USHORT)atoi(str))!=0)) {
  263. if ((t1->t1m.encoding = AllocEncodingTable(t1->t1m.encSize))==NULL) {
  264. SetError(status = NOMEM);
  265. } else {
  266. /* Skip leading proc. */
  267. while (Get_Token(t1->ff, str, len) && strcmp(str, PS_DUP));
  268. /* Read the encoding entries: "<n> <str> put <comment>\n dup" */
  269. for (i=0; i<t1->t1m.encSize; i++) {
  270. /* Get character code. */
  271. (void)Get_Token(t1->ff, str, len);
  272. if (str[0]=='8' && str[1]=='#') { /* Octal? */
  273. index = (USHORT)atoi(&str[2]);
  274. index = (USHORT)((index/10)*8 + (index%8));
  275. } else {
  276. index = (USHORT)atoi(str);
  277. }
  278. /* Get character name. */
  279. (void)Get_Token(t1->ff, str, len);
  280. codes[ENC_MSWINDOWS] = index;
  281. codes[ENC_UNICODE] = index;
  282. if (index<256) {
  283. codes[ENC_STANDARD] = index;
  284. codes[ENC_MACCODES] = index;
  285. } else {
  286. codes[ENC_STANDARD] = NOTDEFCODE;
  287. codes[ENC_MACCODES] = NOTDEFCODE;
  288. }
  289. if ((glyph_name = Strdup(&str[1]))!=NULL)
  290. SetEncodingEntry(t1->t1m.encoding, i,
  291. glyph_name,
  292. ENC_MAXCODES,
  293. codes);
  294. else {
  295. status = NOMEM;
  296. break;
  297. }
  298. (void)Get_Token(t1->ff, str, len); /* Pop "dup" */
  299. (void)Get_Token(t1->ff, str, len); /* Pop "put" or comment. */
  300. if (str[0]=='%') {
  301. (void)GetNewLine(t1->ff, str, len);
  302. (void)Get_Token(t1->ff, str, len); /* Pop "put". */
  303. }
  304. if (strcmp(str, PS_DUP))
  305. break;
  306. }
  307. t1->t1m.encSize = (USHORT)(i+1);
  308. /* Rehash the table. */
  309. RehashEncodingTable(t1->t1m.encoding, t1->t1m.encSize);
  310. }
  311. }
  312. }
  313. return status;
  314. }
  315. /***
  316. ** Function: ReadArray
  317. **
  318. ** Description:
  319. ** Read an array.
  320. ***/
  321. static errcode ReadArray(struct T1Handle *t1,
  322. char *str,
  323. const USHORT len,
  324. funit *array,
  325. USHORT maxarr,
  326. USHORT *cnt)
  327. {
  328. errcode status;
  329. char *nxt;
  330. if (GetSeq(t1->ff, str, len)) {
  331. (*cnt)=0;
  332. do {
  333. array[(*cnt)] = (funit)(((StrToFix(str, &nxt, 4L)+8002)>>2) - 2000);
  334. if (nxt==str)
  335. break;
  336. str = nxt;
  337. } while (++(*cnt)<maxarr);
  338. status=SUCCESS;
  339. } else {
  340. SetError(status = BADINPUTFILE);
  341. }
  342. return status;
  343. }
  344. /***
  345. ** Function: ReadFontSubrs
  346. **
  347. ** Description:
  348. ** Read the command sequence "/Subrs %d array dup %d %d RD %x ND ...",
  349. ** decode and decrypt the subroutines and store them in the T1
  350. ** handle.
  351. ***/
  352. static errcode ReadFontSubrs(struct T1Handle *t1,
  353. char *str, const USHORT len)
  354. {
  355. errcode status = SUCCESS;
  356. USHORT index,i,j;
  357. USHORT count = 0;
  358. USHORT r;
  359. short b;
  360. /* Get the number of subroutines. */
  361. if (Get_Token(t1->ff, str, len)==NULL) {
  362. SetError(status = BADINPUTFILE);
  363. } else {
  364. count = (USHORT)atoi(str);
  365. /* Get the "array" keyword". */
  366. if ((Get_Token(t1->ff, str, len)==NULL) || strcmp(str, PS_ARRAY)) {
  367. SetError(status = BADINPUTFILE);
  368. } else {
  369. if ((t1->subrs = Malloc((USHORT)sizeof(struct Subrs)*count))==NULL) {
  370. SetError(status = NOMEM);
  371. } else {
  372. memset(t1->subrs, '\0', sizeof(struct Subrs)*count);
  373. t1->numsubrs = count;
  374. for (i=0; i<count; i++) {
  375. if (Get_Token(t1->ff, str, len)==NULL) { /* Get "dup" */
  376. SetError(status = BADINPUTFILE);
  377. break;
  378. }
  379. if (strcmp(str, PS_DUP)) {
  380. SetError(status = BADT1HEADER);
  381. break;
  382. }
  383. if (Get_Token(t1->ff, str, len)==NULL) { /* Get Subr index. */
  384. SetError(status=BADINPUTFILE);
  385. break;
  386. }
  387. index = (USHORT)atoi(str);
  388. if (t1->subrs[index].code) {
  389. LogError(MSG_WARNING, MSG_DBLIDX, NULL);
  390. Free(t1->subrs[index].code);
  391. }
  392. if (Get_Token(t1->ff, str, len)==NULL) { /* Get length. */
  393. SetError(status=BADINPUTFILE);
  394. break;
  395. }
  396. t1->subrs[index].len = (USHORT)(atoi(str) - t1->leniv);
  397. if ((t1->subrs[index].code
  398. = Malloc(t1->subrs[index].len))==NULL) {
  399. SetError(status = NOMEM);
  400. break;
  401. }
  402. if (Get_Token(t1->ff, str, len)==NULL) { /* Get RD + space */
  403. SetError(status=BADINPUTFILE);
  404. break;
  405. }
  406. /* Skip space. */
  407. (void)GetByte(t1->ff);
  408. /* Skip lenIV */
  409. r = 4330;
  410. for (j=0; j<t1->leniv; j++) {
  411. b=GetByte(t1->ff);
  412. (void)Decrypt(&r, (UBYTE)b);
  413. }
  414. if (status!=SUCCESS)
  415. break;
  416. /* Get code. */
  417. for (j=0; j<t1->subrs[index].len; j++) {
  418. b=GetByte(t1->ff);
  419. t1->subrs[index].code[j] = Decrypt(&r, (UBYTE)b);
  420. }
  421. if (status!=SUCCESS)
  422. break;
  423. if (Get_Token(t1->ff, str, len)==NULL) { /* Get ND */
  424. SetError(status=BADINPUTFILE);
  425. break;
  426. }
  427. /* Check for non-ATM compatible equivalent to 'ND' */
  428. if (!strcmp(str, PS_NOACCESS)) {
  429. (void)Get_Token(t1->ff, str, len);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. return status;
  436. }
  437. /***** FUNCTIONS */
  438. /***
  439. ** Function: FlushWorkspace
  440. **
  441. ** Description:
  442. ** Free the resources allocated for the T1 handle.
  443. ***/
  444. void FlushWorkspace(struct T1Handle *t1)
  445. {
  446. USHORT i;
  447. /* Free /Subrs */
  448. if (t1->subrs) {
  449. for (i=0; i<t1->numsubrs; i++) {
  450. Free(t1->subrs[i].code);
  451. }
  452. Free(t1->subrs);
  453. }
  454. t1->subrs = NULL;
  455. }
  456. /***
  457. ** Function: CleanUpT1
  458. **
  459. ** Description:
  460. ** Free the resources allocated for the T1 handle.
  461. ***/
  462. errcode CleanUpT1(struct T1Handle *t1)
  463. {
  464. errcode status = SUCCESS;
  465. AlignmentControl *align;
  466. Composite *next;
  467. Blues *blues;
  468. USHORT i;
  469. if (t1) {
  470. /* Free the PSState */
  471. if (t1->ps)
  472. FreePSState(t1->ps);
  473. /* Free /Subrs */
  474. if (t1->subrs) {
  475. for (i=0; i<t1->numsubrs; i++) {
  476. Free(t1->subrs[i].code);
  477. }
  478. Free(t1->subrs);
  479. }
  480. /* Clean up font file reader. */
  481. status = FRCleanUp(t1->ff);
  482. /* Clean up font matrix. */
  483. if (t1->t1m.fmatrix)
  484. Free(t1->t1m.fmatrix);
  485. /* Clean up seac. */
  486. while (t1->t1m.used_seac) {
  487. next = t1->t1m.used_seac->next;
  488. FreeT1Composite(t1->t1m.used_seac);
  489. t1->t1m.used_seac = next;
  490. }
  491. while (t1->t1m.seac) {
  492. next = t1->t1m.seac->next;
  493. FreeT1Composite(t1->t1m.seac);
  494. t1->t1m.seac = next;
  495. }
  496. /* Clean up stdenc. */
  497. for (i=0; i<256; i++) {
  498. if (t1->stdenc[i].code) {
  499. Free(t1->stdenc[i].code);
  500. t1->stdenc[i].code = NULL;
  501. t1->stdenc[i].len = 0;
  502. }
  503. }
  504. /* Clean up encoding table. */
  505. if (t1->t1m.encoding)
  506. FreeEncoding(t1->t1m.encoding, t1->t1m.encSize);
  507. /* Free strings */
  508. if (t1->t1m.date)
  509. Free(t1->t1m.date);
  510. if (t1->t1m.copyright)
  511. Free(t1->t1m.copyright);
  512. if (t1->t1m.name)
  513. Free(t1->t1m.name);
  514. if (t1->t1m.id)
  515. Free(t1->t1m.id);
  516. if (t1->t1m.notice)
  517. Free(t1->t1m.notice);
  518. if (t1->t1m.fullname)
  519. Free(t1->t1m.fullname);
  520. if (t1->t1m.weight)
  521. Free(t1->t1m.weight);
  522. if (t1->t1m.family)
  523. Free(t1->t1m.family);
  524. if (t1->t1m.widths)
  525. Free(t1->t1m.widths);
  526. if (t1->t1m.kerns)
  527. Free(t1->t1m.kerns);
  528. if (t1->t1m.stems.vwidths)
  529. Free(t1->t1m.stems.vwidths);
  530. if (t1->t1m.stems.hwidths)
  531. Free(t1->t1m.stems.hwidths);
  532. blues = &(t1->t1m.blues);
  533. align = &(t1->t1m.blues.align);
  534. for (i=0; i<blues->blue_cnt/2; i++) {
  535. Free(align->top[i].pos);
  536. }
  537. for (i=0; i<blues->oblue_cnt/2; i++) {
  538. Free(align->bottom[i].pos);
  539. }
  540. /* Free handle. */
  541. Free(t1);
  542. }
  543. return status;
  544. }
  545. /***
  546. ** Function: InitT1Input
  547. **
  548. ** Description:
  549. ** Allocate and initiate a handle for a T1 font file, including
  550. ** extracting data from the font prolog that is needed to
  551. ** read the glyphs, such as /FontMatrix, /Subrs and /lenIV.
  552. ***/
  553. errcode InitT1Input(const struct T1Arg *arg,
  554. struct T1Handle **t1ref,
  555. struct T1Metrics **t1mref,
  556. const short (*check)(const char *,
  557. const char *,
  558. const char *))
  559. {
  560. errcode status = SUCCESS;
  561. struct T1Handle *t1;
  562. struct PSState *ps;
  563. Blues *blues;
  564. boolean hybrid = FALSE;
  565. struct T1Metrics *t1m = NULL;
  566. char str[BUFLEN];
  567. USHORT i;
  568. /* Allocate the handle. */
  569. if (((*t1ref)=Malloc((USHORT)sizeof(struct T1Handle)))==NULL ||
  570. (ps = AllocPSState())==NULL) {
  571. if ((*t1ref)) {
  572. Free((*t1ref));
  573. *t1ref = NULL;
  574. }
  575. SetError(status = NOMEM);
  576. } else {
  577. /* Initiate the T1 record. */
  578. t1 = (*t1ref);
  579. t1m = &t1->t1m;
  580. (*t1mref) = t1m;
  581. blues = GetBlues(t1m);
  582. memset(t1, '\0', sizeof(*t1));
  583. t1->ps = ps;
  584. t1->leniv = 4;
  585. t1m->upem = arg->upem;
  586. t1m->defstdhw = 70;
  587. t1m->defstdvw = 80;
  588. blues->blueScale = 39; /* Should really be 39.625 */
  589. blues->blueFuzz = 1;
  590. blues->blueShift = 7 * F8D8;
  591. blues->align.cvt = 3;
  592. t1m->stems.storage = 15;
  593. /* Initiate font file reader. */
  594. if ((status=FRInit(arg->name, pfb_file, &t1->ff))==SUCCESS) {
  595. /* Read /FontMatrix and /Subrs. */
  596. while (status==SUCCESS) {
  597. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  598. SetError(status=BADINPUTFILE);
  599. /**** /ForceBold true def ****/
  600. } else if (!strcmp(str, PS_FORCEBOLD)) {
  601. if (Get_Token(t1->ff, str, BUFLEN)) {
  602. if (!strcmp(str, "true") || !strcmp(str, "True"))
  603. t1m->forcebold = TRUE;
  604. else
  605. t1m->forcebold = FALSE;
  606. status = SUCCESS;
  607. } else {
  608. status = BADINPUTFILE;
  609. }
  610. /**** /BlueFuzz 1 def ****/
  611. } else if (!strcmp(str, PS_BLUEFUZZ)) {
  612. if (Get_Token(t1->ff, str, BUFLEN)) {
  613. blues->blueFuzz = (UBYTE)atoi(str);
  614. status = SUCCESS;
  615. } else {
  616. status = BADINPUTFILE;
  617. }
  618. /**** /BlueScale 0.043625 def ****/
  619. } else if (!strcmp(str, PS_BLUESCALE)) {
  620. if (Get_Token(t1->ff, str, BUFLEN)) {
  621. str[5] = '\0';
  622. blues->blueScale = (UBYTE)atoi(&str[2]);
  623. status = SUCCESS;
  624. } else {
  625. status = BADINPUTFILE;
  626. }
  627. /**** /BlueShift 7 def ****/
  628. } else if (!strcmp(str, PS_BLUESHIFT)) {
  629. if (Get_Token(t1->ff, str, BUFLEN)) {
  630. blues->blueShift = (short)StrToFix(str, NULL, (long)F8D8);
  631. status = SUCCESS;
  632. } else {
  633. status = BADINPUTFILE;
  634. }
  635. /**** /Encoding StandardEncodind def ****/
  636. } else if (!strcmp(str, PS_ENCODING)) {
  637. status = ReadEncodingArray(t1, str, BUFLEN);
  638. /**** /StdVW [118] def ****/
  639. } else if (!strcmp(str, PS_STDVW)) {
  640. USHORT dummy;
  641. status = ReadArray(t1, str, BUFLEN,
  642. &t1m->stdvw, ONE, &dummy);
  643. /**** /StdHW [118] def ****/
  644. } else if (!strcmp(str, PS_STDHW)) {
  645. USHORT dummy;
  646. status = ReadArray(t1, str, BUFLEN,
  647. &t1m->stdhw, ONE, &dummy);
  648. /**** /StemSnapV [118 120] def ****/
  649. } else if (!strcmp(str, PS_SNAPV)) {
  650. status = ReadArray(t1, str, BUFLEN,
  651. &t1m->stemsnapv[0],
  652. MAXSNAP, &t1m->snapv_cnt);
  653. /* Add space for the snap enties in the CV table. */
  654. if (status==SUCCESS)
  655. blues->align.cvt = (USHORT)(blues->align.cvt +
  656. t1m->snapv_cnt);
  657. /**** /StemSnapH [118 120] def ****/
  658. } else if (!strcmp(str, PS_SNAPH)) {
  659. status = ReadArray(t1, str, BUFLEN,
  660. &t1m->stemsnaph[0],
  661. MAXSNAP, &t1m->snaph_cnt);
  662. /* Add space for the snap enties in the CV table. */
  663. if (status==SUCCESS)
  664. blues->align.cvt = (USHORT)(blues->align.cvt +
  665. t1m->snaph_cnt);
  666. /**** /BlueValues [-15 0] def ****/
  667. } else if (!strcmp(str, PS_BLUEVALUES)) {
  668. status = ReadArray(t1, str, BUFLEN,
  669. &(blues->bluevalues[0]),
  670. MAXBLUE, &(blues->blue_cnt));
  671. if (blues->blue_cnt%2)
  672. SetError(status = BADINPUTFILE);
  673. /**** /OtherBlues [-15 0] def ****/
  674. } else if (!strcmp(str, PS_OTHERBLUES)) {
  675. status = ReadArray(t1, str, BUFLEN,
  676. &(blues->otherblues[0]),
  677. MAXBLUE, &(blues->oblue_cnt));
  678. if (blues->oblue_cnt%2)
  679. SetError(status = BADINPUTFILE);
  680. /**** /FamilyBlues [-15 0] def ****/
  681. } else if (!strcmp(str, PS_FAMILYBLUES)) {
  682. status = ReadArray(t1, str, BUFLEN,
  683. &(blues->familyblues[0]),
  684. MAXBLUE, &(blues->fblue_cnt));
  685. /**** /FamilyOtherBlues [-15 0] def ****/
  686. } else if (!strcmp(str, PS_FAMILYOTHERBLUES)) {
  687. status = ReadArray(t1, str, BUFLEN,
  688. &(blues->familyotherblues[0]),
  689. MAXBLUE, &(blues->foblue_cnt));
  690. /**** /CharString ... */
  691. } else if (!strcmp(str, PS_CHARSTRINGS)) {
  692. break;
  693. /**** /FontMatrix [0 0.001 0 0.001 0] def ****/
  694. } else if (GetFontMatrix(t1m)==NULL &&
  695. !strcmp(str, PS_FONTMATRIX)) {
  696. status = ReadFontMatrix(t1, str, BUFLEN);
  697. } else if (!strcmp(str, PS_SUBRS)) {
  698. /* Discard prior lores /Subrs. */
  699. FlushWorkspace(t1);
  700. /* Read new subrs. */
  701. status = ReadFontSubrs(t1,str, BUFLEN);
  702. /**** /lenIV 4 def ****/
  703. } else if (!strcmp(str, PS_LENIV)) {
  704. if (Get_Token(t1->ff, str, BUFLEN)) {
  705. t1->leniv = (USHORT)atoi(str);
  706. status = SUCCESS;
  707. } else {
  708. status = BADINPUTFILE;
  709. }
  710. } else if (t1m->date==NULL && !strcmp(str, PS_DATE)) {
  711. if ((GetNewLine(t1->ff, str, BUFLEN))==NULL) {
  712. SetError(status = BADINPUTFILE);
  713. } else if ((t1m->date=Strdup(str))==NULL) {
  714. SetError(status = NOMEM);
  715. }
  716. } else if (t1m->copyright==NULL &&
  717. !strcmp(str, PS_COPYRIGHT)) {
  718. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  719. SetError(status = BADINPUTFILE);
  720. } else if ((t1m->copyright=Strdup(str))==NULL) {
  721. SetError(status = NOMEM);
  722. }
  723. } else if (t1m->name==NULL && !strcmp(str, PS_NAME)) {
  724. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  725. SetError(status = BADINPUTFILE);
  726. } else if ((t1m->name=Strdup(&str[1]))==NULL) {
  727. SetError(status = NOMEM);
  728. }
  729. } else if (t1m->id==NULL && !strcmp(str, PS_ID)) {
  730. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  731. SetError(status = BADINPUTFILE);
  732. } else if ((t1m->id=Strdup(str))==NULL) {
  733. SetError(status = NOMEM);
  734. }
  735. } else if (t1m->version.ver==0 && !strcmp(str, PS_VERSION)) {
  736. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  737. SetError(status = BADINPUTFILE);
  738. } else {
  739. t1m->version.ver = (USHORT)atoi(str);
  740. if (strchr(str, '.'))
  741. t1m->version.rev = (USHORT)atoi(strchr(str, '.')+1);
  742. else
  743. t1m->version.rev = 0;
  744. }
  745. } else if (t1m->notice==NULL && !strcmp(str, PS_NOTICE)) {
  746. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  747. SetError(status = BADINPUTFILE);
  748. } else if ((t1m->notice=Strdup(str))==NULL) {
  749. SetError(status = NOMEM);
  750. }
  751. } else if (t1m->fullname==NULL && !strcmp(str, PS_FULLNAME)) {
  752. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  753. SetError(status = BADINPUTFILE);
  754. } else if ((t1m->fullname=Strdup(str))==NULL) {
  755. SetError(status = NOMEM);
  756. }
  757. } else if (t1m->family==NULL && !strcmp(str, PS_FAMILY)) {
  758. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  759. SetError(status = BADINPUTFILE);
  760. } else if ((t1m->family=Strdup(str))==NULL) {
  761. SetError(status = NOMEM);
  762. }
  763. } else if (t1m->weight==NULL && !strcmp(str, PS_WEIGHT)) {
  764. if ((GetSeq(t1->ff, str, BUFLEN))==NULL) {
  765. SetError(status = BADINPUTFILE);
  766. } else if ((t1m->weight=Strdup(str))==NULL) {
  767. SetError(status = NOMEM);
  768. }
  769. } else if (t1m->angle==0 && !strcmp(str, PS_ANGLE)) {
  770. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  771. SetError(status = BADINPUTFILE);
  772. } else
  773. t1m->angle = StrToFix(str, NULL, F16D16BASE);
  774. } else if (t1m->underline==0 && !strcmp(str, PS_UNDERLINE)) {
  775. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  776. SetError(status = BADINPUTFILE);
  777. } else
  778. t1m->underline = (funit)StrToFix(str, NULL, 1L);
  779. } else if (t1m->uthick==0 && !strcmp(str, PS_UTHICK)) {
  780. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  781. SetError(status = BADINPUTFILE);
  782. } else
  783. t1m->uthick = (funit)StrToFix(str, NULL, 1L);
  784. } else if (!strcmp(str, PS_ISFIXED)) {
  785. if ((Get_Token(t1->ff, str, BUFLEN))==NULL) {
  786. SetError(status = BADINPUTFILE);
  787. } else
  788. if (!strcmp(str, "true") ||
  789. !strcmp(str, "True") ||
  790. !strcmp(str, "TRUE"))
  791. t1m->fixedPitch = TRUE;
  792. } else if (!strcmp(str, PS_HYBRID)) {
  793. hybrid = TRUE;
  794. }
  795. }
  796. /* Change the baseline zone into an OtherBlues[] zone. */
  797. if (blues->blue_cnt) {
  798. blues->otherblues[blues->oblue_cnt++] = blues->bluevalues[0];
  799. blues->otherblues[blues->oblue_cnt++] = blues->bluevalues[1];
  800. for (i=2; i<blues->blue_cnt; i++)
  801. blues->bluevalues[i-2] = blues->bluevalues[i];
  802. blues->blue_cnt -= 2;
  803. }
  804. if (blues->fblue_cnt) {
  805. blues->familyotherblues[blues->foblue_cnt++]
  806. = blues->familyblues[0];
  807. blues->familyotherblues[blues->foblue_cnt++]
  808. = blues->familyblues[1];
  809. for (i=2; i<blues->fblue_cnt; i++)
  810. blues->familyblues[i-2] = blues->familyblues[i];
  811. blues->fblue_cnt -= 2;
  812. }
  813. /* Allocate the space for the blue buckets. */
  814. for (i=0; i<blues->blue_cnt; i+=2) {
  815. USHORT size = (USHORT)((ABS(blues->bluevalues[i+1] -
  816. blues->bluevalues[i]) +
  817. 1 + 2*blues->blueFuzz)*
  818. (USHORT)sizeof(struct CVTPos));
  819. if ((blues->align.top[i/2].pos = Malloc(size))==NULL) {
  820. SetError(status = NOMEM);
  821. break;
  822. }
  823. /* Make sure that first value is larger than second value. */
  824. if (blues->bluevalues[i] > blues->bluevalues[i+1]) {
  825. LogError(MSG_WARNING, MSG_INVBLUES, NULL);
  826. SWAPINT(blues->bluevalues[i], blues->bluevalues[i+1]);
  827. }
  828. }
  829. for (i=0; i<blues->oblue_cnt; i+=2) {
  830. USHORT size = (USHORT)((ABS(blues->otherblues[i+1] -
  831. blues->otherblues[i]) +
  832. 1 + 2*blues->blueFuzz)*
  833. (USHORT)sizeof(struct CVTPos));
  834. if ((blues->align.bottom[i/2].pos = Malloc(size))==NULL) {
  835. SetError(status = NOMEM);
  836. break;
  837. }
  838. /* Make sure that first value is larger than second value. */
  839. if (blues->otherblues[i] > blues->otherblues[i+1]) {
  840. LogError(MSG_WARNING, MSG_INVBLUES, NULL);
  841. SWAPINT(blues->otherblues[i], blues->otherblues[i+1]);
  842. }
  843. }
  844. /* Advance to the first glyph. */
  845. if (status==SUCCESS) {
  846. while (Get_Token(t1->ff, str, BUFLEN) &&
  847. strcmp(str, PS_BEGIN));
  848. if (strcmp(str, PS_BEGIN)) {
  849. SetError(status = BADT1HEADER);
  850. }
  851. /* Skip lores chars if hybrid font. */
  852. if (status==SUCCESS && hybrid) {
  853. USHORT count;
  854. /* Skip Charstring dictionary. */
  855. do {
  856. /* Glyph name, or end. */
  857. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  858. SetError(status = BADINPUTFILE);
  859. break;
  860. }
  861. if (!strcmp(str, PS_END))
  862. break;
  863. /* Charstring length. */
  864. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  865. SetError(status = BADINPUTFILE);
  866. break;
  867. }
  868. count = (USHORT)(atoi(str)+1);
  869. /* Delimiter. */
  870. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  871. SetError(status = BADINPUTFILE);
  872. break;
  873. }
  874. /* Charstring */
  875. for (i=0; i<count; i++)
  876. (void)GetByte(t1->ff);
  877. /* Delimiter */
  878. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  879. SetError(status = BADINPUTFILE);
  880. break;
  881. }
  882. } while (status==SUCCESS);
  883. /* Skip to the beginning of next charstring. */
  884. while (Get_Token(t1->ff, str, BUFLEN) &&
  885. strcmp(str, PS_BEGIN));
  886. if (strcmp(str, PS_BEGIN)) {
  887. SetError(status = BADT1HYBRID);
  888. }
  889. }
  890. }
  891. }
  892. }
  893. if ((status==SUCCESS) && t1m && check(t1m->name,
  894. t1m->copyright,
  895. t1m->notice)!=SUCCESS)
  896. status = NOCOPYRIGHT;
  897. return status;
  898. }
  899. /***
  900. ** Function: GetT1Glyph
  901. **
  902. ** Description:
  903. ** The current file position of the T1 font file must be
  904. ** at the begining of an entry in the /CharStrings dictionary.
  905. ** The function will decode the font commands, parse them, and
  906. ** finally build a representation of the glyph.
  907. ***/
  908. errcode GetT1Glyph(struct T1Handle *t1,
  909. struct T1Glyph *glyph,
  910. const struct GlyphFilter *filter)
  911. {
  912. errcode status = SUCCESS;
  913. /* struct encoding *enc; */
  914. char str[BUFLEN];
  915. UBYTE *code;
  916. USHORT len;
  917. USHORT i;
  918. USHORT r = 4330;
  919. short b;
  920. /* Get glyph name or end. */
  921. if (Get_Token(t1->ff, str, BUFLEN)==NULL) {
  922. SetError(status = BADINPUTFILE);
  923. } else if (!strcmp(str, PS_END)) {
  924. status = DONE;
  925. } else if (str[0]!='/') {
  926. SetError(status = BADCHARSTRING);
  927. } else {
  928. if ((glyph->name = Strdup(&str[1]))==NULL) {
  929. SetError(status = NOMEM);
  930. }
  931. else if (Get_Token(t1->ff, str, BUFLEN)==NULL ||
  932. (len = (USHORT)atoi(str))==0) /* Get length of charstring. */
  933. {
  934. SetError(status = BADINPUTFILE);
  935. }
  936. else {
  937. /* Get RD + space */
  938. (void)Get_Token(t1->ff, str, BUFLEN); (void)GetByte(t1->ff);
  939. /* Get commands. */
  940. if (len<BUFLEN)
  941. code = (UBYTE *)str;
  942. else
  943. if ((code = Malloc(len*sizeof(UBYTE)))==NULL) {
  944. SetError(status = NOMEM);
  945. }
  946. if (code) {
  947. for (i=0; i<len; i++) {
  948. b = GetByte(t1->ff);
  949. code[i] = (UBYTE)Decrypt(&r, (UBYTE)b);
  950. }
  951. /* Parse commands. */
  952. if (status==SUCCESS) {
  953. if (t1->t1m.encoding!=NULL ||
  954. UseGlyph(filter, t1->t1m.seac, glyph->name)) {
  955. InitPS(t1->ps);
  956. status = ParseCharString(glyph,
  957. &t1->t1m.seac,
  958. t1->ps,
  959. t1->subrs,
  960. &code[t1->leniv],
  961. (USHORT)(len-t1->leniv));
  962. /* Skip normal conversion for the ".notdef" glyph. */
  963. if (!strcmp(glyph->name, ".notdef"))
  964. status = SKIP;
  965. } else {
  966. status = SKIP;
  967. /***
  968. Two approaches are implemented for the management of
  969. composite glyphs:
  970. 1) It is up to the client to specify a GlyphFilter such
  971. that all 'seac' characters has their dependent base and
  972. accent character in the filter as well.
  973. 2) The converter manages a list of the dependent characters,
  974. which are converted when found.
  975. Approach 2) will typically cause the converter to use more
  976. memory than what is available in the small memory model,
  977. which is why the default is to disabled it.
  978. ***/
  979. #if 0
  980. /* Record StandardEncoding glyphs, for 'seac' */
  981. if ((enc = LookupPSName(t1->t1m.encoding,
  982. t1->t1m.encSize,
  983. glyph->name)) &&
  984. (i = LookupCharCode(enc, ENC_STANDARD))!=0) {
  985. if ((t1->stdenc[i].code
  986. = Malloc(len-t1->leniv))==NULL) {
  987. SetError(status = NOMEM);
  988. } else {
  989. memcpy(t1->stdenc[i].code,
  990. &code[t1->leniv],
  991. sizeof(UBYTE) * (len - t1->leniv));
  992. t1->stdenc[i].len = len - t1->leniv;
  993. }
  994. }
  995. #endif
  996. }
  997. if (code!=(UBYTE *)str)
  998. Free(code);
  999. /* Get ND */
  1000. (void)Get_Token(t1->ff, str, BUFLEN);
  1001. }
  1002. }
  1003. }
  1004. }
  1005. return status;
  1006. }
  1007. /***
  1008. ** Function: FreeT1Glyph
  1009. **
  1010. ** Description:
  1011. ** This function frees the memory used to represent
  1012. ** a glyph that has been translated.
  1013. ***/
  1014. void FreeT1Glyph(T1Glyph *glyph)
  1015. {
  1016. Flex *flex;
  1017. Stem *stem;
  1018. Stem3 *stem3;
  1019. if (glyph->name)
  1020. Free(glyph->name);
  1021. while (glyph->hints.vstems) {
  1022. stem = glyph->hints.vstems->next;
  1023. Free(glyph->hints.vstems);
  1024. glyph->hints.vstems = stem;
  1025. }
  1026. while (glyph->hints.hstems) {
  1027. stem = glyph->hints.hstems->next;
  1028. Free(glyph->hints.hstems);
  1029. glyph->hints.hstems = stem;
  1030. }
  1031. while (glyph->hints.vstems3) {
  1032. stem3 = glyph->hints.vstems3->next;
  1033. Free(glyph->hints.vstems3);
  1034. glyph->hints.vstems3 = stem3;
  1035. }
  1036. while (glyph->hints.hstems3) {
  1037. stem3 = glyph->hints.hstems3->next;
  1038. Free(glyph->hints.hstems3);
  1039. glyph->hints.hstems3 = stem3;
  1040. }
  1041. while (glyph->hints.flex) {
  1042. flex = glyph->hints.flex->next;
  1043. Free(glyph->hints.flex);
  1044. glyph->hints.flex = flex;
  1045. }
  1046. while (glyph->paths) {
  1047. Outline *path = glyph->paths;
  1048. glyph->paths = path->next;
  1049. if (path->count) {
  1050. Free(path->onoff);
  1051. Free(path->pts);
  1052. }
  1053. Free(path);
  1054. }
  1055. memset((void *)glyph, '\0', sizeof(T1Glyph));
  1056. }
  1057. /***
  1058. ** Function: GetT1Composite
  1059. **
  1060. ** Description:
  1061. ** This function unlinks the first composite glyph
  1062. ** from the list of recorded composite glyphs, which
  1063. ** is returned to the caller.
  1064. ***/
  1065. struct Composite *GetT1Composite(struct T1Handle *t1)
  1066. {
  1067. struct Composite *comp;
  1068. comp = t1->t1m.seac;
  1069. if (comp) {
  1070. t1->t1m.seac = comp->next;
  1071. comp->next = t1->t1m.used_seac;
  1072. t1->t1m.used_seac = comp;
  1073. }
  1074. return comp;
  1075. }
  1076. /***
  1077. ** Function: GetT1BaseGlyph
  1078. **
  1079. ** Description:
  1080. ** This function parses the charstring code associated to the
  1081. ** base character of a composite character, if that glyph
  1082. ** is not already converted.
  1083. ***/
  1084. errcode GetT1BaseGlyph(struct T1Handle *t1,
  1085. const struct Composite *comp,
  1086. struct T1Glyph *glyph)
  1087. {
  1088. struct encoding *enc;
  1089. struct Subrs *subr;
  1090. errcode status = SUCCESS;
  1091. if ((enc = LookupPSName(t1->t1m.encoding,
  1092. t1->t1m.encSize,
  1093. comp->bchar))==NULL) {
  1094. LogError(MSG_WARNING, MSG_BADENC, comp->bchar);
  1095. return SKIP;
  1096. }
  1097. subr = &t1->stdenc[LookupCharCode(enc, ENC_STANDARD)];
  1098. if (subr->len==0) {
  1099. status = SKIP; /* Missing or already done. */
  1100. } else {
  1101. InitPS(t1->ps);
  1102. if ((glyph->name = Strdup((char*)comp->achar))==NULL) {
  1103. SetError(status = NOMEM);
  1104. } else {
  1105. status = ParseCharString(glyph,
  1106. &t1->t1m.seac,
  1107. t1->ps,
  1108. t1->subrs,
  1109. subr->code,
  1110. subr->len);
  1111. }
  1112. Free(subr->code);
  1113. subr->code = NULL;
  1114. subr->len = 0;
  1115. }
  1116. return status;
  1117. }
  1118. /***
  1119. ** Function: GetT1AccentGlyph
  1120. **
  1121. ** Description:
  1122. ** This function parses the charstring code associated to the
  1123. ** accent character of a composite character, if that glyph
  1124. ** is not already converted.
  1125. ***/
  1126. errcode GetT1AccentGlyph(struct T1Handle *t1,
  1127. const struct Composite *comp,
  1128. struct T1Glyph *glyph)
  1129. {
  1130. struct encoding *enc;
  1131. struct Subrs *subr;
  1132. errcode status = SUCCESS;
  1133. if ((enc = LookupPSName(t1->t1m.encoding,
  1134. t1->t1m.encSize,
  1135. comp->achar))==NULL) {
  1136. LogError(MSG_WARNING, MSG_BADENC, comp->achar);
  1137. return SKIP;
  1138. }
  1139. subr = &t1->stdenc[LookupCharCode(enc, ENC_STANDARD)];
  1140. if (subr->len==0) {
  1141. status = SKIP; /* Missing or already done. */
  1142. } else {
  1143. InitPS(t1->ps);
  1144. if ((glyph->name = Strdup((char *)comp->achar))==NULL) {
  1145. SetError(status = NOMEM);
  1146. } else {
  1147. status = ParseCharString(glyph,
  1148. &t1->t1m.seac,
  1149. t1->ps,
  1150. t1->subrs,
  1151. subr->code,
  1152. subr->len);
  1153. }
  1154. Free(subr->code);
  1155. subr->code = NULL;
  1156. subr->len = 0;
  1157. }
  1158. return status;
  1159. }
  1160. /***
  1161. ** Function: ReadOtherMetrics
  1162. **
  1163. ** Description:
  1164. ** Return font level information about the T1 font (mostly
  1165. ** metrics).
  1166. ***/
  1167. errcode ReadOtherMetrics(struct T1Metrics *t1m,
  1168. const char *metrics)
  1169. {
  1170. errcode status = SUCCESS;
  1171. if ((status = ReadFontMetrics(metrics, t1m))==NOMETRICS) {
  1172. t1m->flags = DEFAULTMETRICS;
  1173. status = SUCCESS;
  1174. } else {
  1175. t1m->flags = USEMETRICS;
  1176. }
  1177. return status;
  1178. }