PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Build/source/texk/ps2pkm/t1funcs.c

https://bitbucket.org/preining/tex-live
C | 537 lines | 393 code | 72 blank | 72 comment | 77 complexity | d04719bb323c6ab9892efd1924377a46 MD5 | raw file
  1. /* $XConsortium: t1funcs.c,v 1.10 92/05/12 18:07:55 gildea Exp $ */
  2. /* Copyright International Business Machines,Corp. 1991
  3. * All Rights Reserved
  4. *
  5. * License, subject to the license given below, to use,
  6. * copy, modify, and distribute this software * and its
  7. * documentation for any purpose and without fee is hereby
  8. * granted, provided that the above copyright notice appear
  9. * in all copies and that both that copyright notice and
  10. * this permission notice appear in supporting documentation,
  11. * and that the name of IBM not be used in advertising or
  12. * publicity pertaining to distribution of the software
  13. * without specific, written prior permission.
  14. *
  15. * IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES
  16. * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
  17. * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF
  19. * THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND
  20. * PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
  21. * OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF
  22. * THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES
  23. * THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN
  24. * NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  25. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  26. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  27. * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  28. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  29. * SOFTWARE.
  30. *
  31. * Author: Jeffrey B. Lotspiech, IBM Almaden Research Center
  32. * Modeled on spfuncs.c by Dave Lemke, Network Computing Devices, Inc
  33. * which contains the following copyright and permission notices:
  34. *
  35. * Copyright 1990, 1991 Network Computing Devices;
  36. * Portions Copyright 1987 by Digital Equipment Corporation and the
  37. * Massachusetts Institute of Technology
  38. *
  39. * Permission to use, copy, modify, and distribute this protoype software
  40. * and its documentation to Members and Affiliates of the MIT X Consortium
  41. * any purpose and without fee is hereby granted, provided
  42. * that the above copyright notice appear in all copies and that both that
  43. * copyright notice and this permission notice appear in supporting
  44. * documentation, and that the names of Network Computing Devices, Digital or
  45. * MIT not be used in advertising or publicity pertaining to distribution of
  46. * the software without specific, written prior permission.
  47. *
  48. * NETWORK COMPUTING DEVICES, DIGITAL AND MIT DISCLAIM ALL WARRANTIES WITH
  49. * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  50. * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, DIGITAL OR MIT BE
  51. * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  52. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  53. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  54. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  55. */
  56. #include "types.h"
  57. #include <string.h>
  58. #include "ffilest.h"
  59. #ifdef XSERVER
  60. #include <X11/Xfuncs.h>
  61. #include "FSproto.h"
  62. #endif
  63. #include "t1intf.h"
  64. #include "objects.h"
  65. #include "spaces.h"
  66. #include "regions.h"
  67. #include "t1stdio.h"
  68. #include "util.h"
  69. #include "fontfcn.h"
  70. typedef char *encoding[256];
  71. int Type1OpenScalable(char **ev, struct _Font **ppFont, int flags,
  72. struct _FontEntry *entry, char *fileName,
  73. struct _FontScalable *vals, fsBitmapFormat format,
  74. ULONG fmask, double efactor,double slant);
  75. static int Type1GetGlyphs(struct _Font *pFont, ULONG count,
  76. unsigned char *chars, FontEncoding charEncoding,
  77. ULONG *glyphCount, struct _CharInfo **glyphs );
  78. void Type1CloseFont(struct _Font *pFont);
  79. extern int Type1GetInfoScalable(struct _FontPathElement *fpe,
  80. struct _FontInfo *pInfo,
  81. struct _FontEntry *entry,
  82. struct _FontName *fontName,
  83. char *fileName,
  84. struct _FontScalable *Vals);
  85. static int Type1GetMetrics(struct _Font *pFont, ULONG count,
  86. unsigned char *chars, FontEncoding charEncoding,
  87. ULONG *glyphCount, xCharInfo **glyphs );
  88. static void fillrun(register char *p,pel x0,pel x1,int bit);
  89. extern char * Xalloc(int size);
  90. extern void Xfree(void*);
  91. static void fill(char *dest,int h,int w,struct region *area,
  92. int byte,int bit,int wordsize);
  93. extern Bool fontfcnA(char *env,int *mode);
  94. extern struct region *fontfcnB(struct XYspace *S, unsigned char *code,
  95. int *lenP, int *mode);
  96. extern int FontDefaultFormat(int *bit, int *byte,
  97. int *glyphs, int *scan);
  98. extern int CheckFSFormat(int format,int fmask,int *bit,int *byte,int *scan,int *glyph,int *image);
  99. extern void QueryFontLib(char *env,char *infoName,void *infoValue,int *rcodeP);
  100. extern void T1FillFontInfo(struct _Font *pFont,struct _FontScalable *Vals,char *Filename,char *Fontname);
  101. extern void T1InitStdProps(void);
  102. extern int FontFileRegisterRenderer(FontRendererRec *);
  103. extern psfont *FontP;
  104. extern psobj *ISOLatin1EncArrayP;
  105. #define DECIPOINTSPERINCH 722.7
  106. extern int currentchar; /* for error reporting */
  107. /*ARGSUSED*/
  108. int Type1OpenScalable (ev, ppFont, flags, entry, fileName, vals, format,
  109. fmask, efactor, slant)
  110. encoding ev;
  111. FontPtr *ppFont;
  112. int flags;
  113. FontEntryPtr entry;
  114. char *fileName;
  115. FontScalablePtr vals;
  116. fsBitmapFormat format;
  117. fsBitmapFormatMask fmask;
  118. DOUBLE efactor;
  119. DOUBLE slant;
  120. {
  121. extern struct XYspace *IDENTITY;
  122. FontPtr pFont;
  123. int bit,
  124. byte,
  125. glyph,
  126. scan,
  127. image;
  128. int pad,wordsize; /* scan & image in bits */
  129. int size; /* for memory size calculations */
  130. struct XYspace *S; /* coordinate space for character */
  131. struct region *area;
  132. DOUBLE scale; /* scale factor for font */
  133. DOUBLE txx, tyx, txy, tyy;
  134. CharInfoRec *glyphs;
  135. register int i;
  136. int len,rc;
  137. struct type1font *type1;
  138. char *p;
  139. /* set up default values */
  140. FontDefaultFormat(&bit, &byte, &glyph, &scan);
  141. /* get any changes made from above */
  142. rc = CheckFSFormat(format, fmask, &bit, &byte, &scan, &glyph, &image);
  143. if (rc != Successful)
  144. return rc;
  145. pad = glyph * 8;
  146. wordsize = scan * 8;
  147. #define PAD(bits, pad) (((bits)+(pad)-1)&-(pad))
  148. pFont = (FontPtr) Xalloc(sizeof(FontRec));
  149. if (pFont == NULL)
  150. return AllocError;
  151. type1 = (struct type1font *)Xalloc(sizeof(struct type1font));
  152. if (type1 == NULL) {
  153. Xfree(pFont);
  154. return AllocError;
  155. }
  156. (void) memset(type1, 0, sizeof(struct type1font));
  157. scale = (DOUBLE) vals->pixel;
  158. /* Code added by E. Schenk. July 25, 1992.
  159. * The following code reads the FontMatrix for the font,
  160. * and transforms the font using it. This is necessary
  161. * for fonts that are just an oblique, or extend of
  162. * a normal font.
  163. */
  164. { float fontmatrix[6];
  165. int rc;
  166. QueryFontLib(fileName,"FontMatrix",fontmatrix,&rc);
  167. if (!rc) {
  168. S = (struct XYspace *) Transform(IDENTITY,
  169. fontmatrix[0], fontmatrix[1], fontmatrix[2], fontmatrix[3]);
  170. }
  171. else {
  172. S = (struct XYspace *) Scale(IDENTITY,0.001,0.001);
  173. }
  174. }
  175. /* End of additional code */
  176. if (efactor == 1.0 && slant == 0.0)
  177. S = (struct XYspace *)Permanent(Scale(S, scale, - scale));
  178. else {
  179. txx = (DOUBLE)vals->x * efactor * vals->point / DECIPOINTSPERINCH;
  180. tyy = (DOUBLE)vals->y * vals->point / DECIPOINTSPERINCH;
  181. tyx = 0.0;
  182. txy = (DOUBLE)vals->x * slant * vals->point / DECIPOINTSPERINCH;
  183. S = (struct XYspace *) Permanent(Transform(S, txx, tyx, txy, -tyy));
  184. }
  185. glyphs = type1->glyphs;
  186. /* load font if not already loaded */
  187. if (!fontfcnA(fileName, &rc))
  188. return (rc);
  189. for (i=0; i < 256-FIRSTCOL; i++) {
  190. LONG h,w;
  191. LONG paddedW;
  192. if (ev[i] == NULL) continue;
  193. len = strlen(ev[i]);
  194. currentchar = i;
  195. rc = 0;
  196. area = fontfcnB(S, (unsigned char *)ev[i], &len, &rc);
  197. if (rc < 0) {
  198. rc = BadFontName;
  199. break;
  200. }
  201. else if (rc > 0)
  202. continue;
  203. if (area == NULL)
  204. continue;
  205. h = area->ymax - area->ymin;
  206. w = area->xmax - area->xmin;
  207. paddedW = PAD(w, pad);
  208. if (h > 0 && w > 0) {
  209. size = h * paddedW / 8;
  210. glyphs[i].bits = (char *)Xalloc(size);
  211. if (glyphs[i].bits == NULL) {
  212. rc = AllocError;
  213. break;
  214. }
  215. }
  216. else {
  217. h = w = 0;
  218. area->xmin = area->xmax = 0;
  219. area->ymax = area->ymax = 0;
  220. }
  221. glyphs[i].metrics.leftSideBearing = area->xmin;
  222. glyphs[i].metrics.characterWidth = NEARESTPEL(area->ending.x - area->origin.x);
  223. glyphs[i].metrics.rightSideBearing = w + area->xmin;
  224. glyphs[i].metrics.descent = area->ymax - NEARESTPEL(area->origin.y);
  225. glyphs[i].metrics.ascent = h - glyphs[i].metrics.descent;
  226. if (h > 0 && w > 0) {
  227. (void) memset(glyphs[i].bits, 0, size);
  228. fill(glyphs[i].bits, h, paddedW, area, byte, bit, wordsize );
  229. }
  230. Destroy(area);
  231. }
  232. if (i != 256 - FIRSTCOL) {
  233. for (i--; i >= 0; i--)
  234. if (glyphs[i].bits != NULL)
  235. Xfree(glyphs[i].bits);
  236. Xfree(type1);
  237. Xfree(pFont);
  238. return rc;
  239. }
  240. type1->pDefault = NULL;
  241. pFont->format = format;
  242. pFont->bit = bit;
  243. pFont->byte = byte;
  244. pFont->glyph = glyph;
  245. pFont->scan = scan;
  246. pFont->info.firstCol = FIRSTCOL;
  247. pFont->info.lastCol = 255;
  248. pFont->info.firstRow = 0;
  249. pFont->info.lastRow = 0;
  250. pFont->get_metrics = Type1GetMetrics;
  251. pFont->get_glyphs = Type1GetGlyphs;
  252. pFont->unload_font = Type1CloseFont;
  253. pFont->refcnt = 0;
  254. pFont->maxPrivate = -1;
  255. pFont->devPrivates = 0;
  256. pFont->fontPrivate = (unsigned char *) type1;
  257. T1FillFontInfo(pFont, vals, fileName, entry->name.name);
  258. *ppFont = pFont;
  259. return Successful;
  260. }
  261. /* NOTE: ILH removed tests ...->characterWidth != 0 below because zero-width
  262. does not mean the character is empty, simply that it has zero escapement. */
  263. static int
  264. Type1GetGlyphs(pFont, count, chars, charEncoding, glyphCount, glyphs)
  265. FontPtr pFont;
  266. ULONG count;
  267. register unsigned char *chars;
  268. FontEncoding charEncoding;
  269. ULONG *glyphCount; /* RETURN */
  270. CharInfoPtr *glyphs; /* RETURN */
  271. {
  272. unsigned int firstRow;
  273. unsigned int numRows;
  274. CharInfoPtr *glyphsBase;
  275. register unsigned int c;
  276. register CharInfoPtr pci;
  277. unsigned int r;
  278. CharInfoPtr pDefault;
  279. register struct type1font *type1Font;
  280. register int firstCol;
  281. type1Font = (struct type1font *) pFont->fontPrivate;
  282. firstCol = pFont->info.firstCol;
  283. pDefault = type1Font->pDefault;
  284. glyphsBase = glyphs;
  285. switch (charEncoding) {
  286. case Linear8Bit:
  287. case TwoD8Bit:
  288. if (pFont->info.firstRow > 0)
  289. break;
  290. while (count--) {
  291. c = (*chars++);
  292. if (c >= firstCol)
  293. *glyphs++ = &type1Font->glyphs[c-firstCol];
  294. else if (pDefault)
  295. *glyphs++ = pDefault;
  296. }
  297. break;
  298. case Linear16Bit:
  299. while (count--) {
  300. c = *chars++ << 8;
  301. c = (c | *chars++);
  302. if (c < 256 && c >= firstCol)
  303. *glyphs++ = &type1Font->glyphs[c-firstCol];
  304. else if (pDefault)
  305. *glyphs++ = pDefault;
  306. }
  307. break;
  308. case TwoD16Bit:
  309. firstRow = pFont->info.firstRow;
  310. numRows = pFont->info.lastRow - firstRow + 1;
  311. while (count--) {
  312. r = (*chars++) - firstRow;
  313. c = (*chars++);
  314. if (r < numRows && c < 256 && c >= firstCol)
  315. *glyphs++ = &type1Font->glyphs[(r << 8) + c - firstCol];
  316. else if (pDefault)
  317. *glyphs++ = pDefault;
  318. }
  319. break;
  320. }
  321. *glyphCount = glyphs - glyphsBase;
  322. return Successful;
  323. }
  324. static int
  325. Type1GetMetrics(pFont, count, chars, charEncoding, glyphCount, glyphs)
  326. FontPtr pFont;
  327. ULONG count;
  328. register unsigned char *chars;
  329. FontEncoding charEncoding;
  330. ULONG *glyphCount; /* RETURN */
  331. xCharInfo **glyphs; /* RETURN */
  332. {
  333. static CharInfoRec nonExistantChar;
  334. int ret;
  335. struct type1font *type1Font;
  336. CharInfoPtr oldDefault;
  337. type1Font = (struct type1font *) pFont->fontPrivate;
  338. oldDefault = type1Font->pDefault;
  339. type1Font->pDefault = &nonExistantChar;
  340. ret = Type1GetGlyphs(pFont, count, chars, charEncoding, glyphCount, (CharInfoPtr *) glyphs);
  341. type1Font->pDefault = oldDefault;
  342. return ret;
  343. }
  344. void Type1CloseFont(pFont)
  345. FontPtr pFont;
  346. {
  347. register int i;
  348. struct type1font *type1;
  349. type1 = (struct type1font *) pFont->fontPrivate;
  350. for (i=0; i < 256 - pFont->info.firstCol; i++)
  351. if (type1->glyphs[i].bits != NULL)
  352. Xfree(type1->glyphs[i].bits);
  353. Xfree(type1);
  354. if (pFont->info.props)
  355. Xfree(pFont->info.props);
  356. if (pFont->info.isStringProp)
  357. Xfree(pFont->info.isStringProp);
  358. Xfree(pFont);
  359. }
  360. static void fill(dest, h, w, area, Byte, bit, wordsize)
  361. register char *dest; /* destination bitmap */
  362. int h,w; /* dimensions of 'dest', w padded */
  363. register struct region *area; /* region to write to 'dest' */
  364. int Byte,bit; /* flags; LSBFirst or MSBFirst */
  365. int wordsize; /* number of bits per word for LSB/MSB purposes */
  366. {
  367. register struct edgelist *edge; /* for looping through edges */
  368. register char *p; /* current scan line in 'dest' */
  369. register int y; /* for looping through scans */
  370. register int wbytes = w / 8; /* number of bytes in width */
  371. register pel *leftP,*rightP; /* pointers to X values, left and right */
  372. int xmin = area->xmin; /* upper left X */
  373. int ymin = area->ymin; /* upper left Y */
  374. for (edge = area->anchor; VALIDEDGE(edge); edge = edge->link->link) {
  375. p = dest + (edge->ymin - ymin) * wbytes;
  376. leftP = edge->xvalues;
  377. rightP = edge->link->xvalues;
  378. for (y = edge->ymin; y < edge->ymax; y++) {
  379. fillrun(p, (pel)(*leftP++ - xmin), (pel)(*rightP++ - xmin), bit);
  380. p += wbytes;
  381. }
  382. }
  383. /*
  384. Now, as an afterthought, we'll go reorganize if odd byte order requires
  385. it:
  386. */
  387. if (Byte == LSBFirst && wordsize != 8) {
  388. register int i;
  389. switch (wordsize) {
  390. case 16:
  391. {
  392. register unsigned short data,*p;
  393. p = (unsigned short *) dest;
  394. for (i = h * w /16; --i >= 0;) {
  395. data = *p;
  396. *p++ = (data << 8) + (data >> 8);
  397. }
  398. break;
  399. }
  400. case 64:
  401. case 32:
  402. {
  403. register ULONG data,*p;
  404. p = (ULONG *) dest;
  405. for (i = h * w / 32; --i >= 0;) {
  406. data = *p;
  407. *p++ = (data << 24) + (data >> 24)
  408. + (0xFF00 & (data >> 8))
  409. + (0xFF0000 & (data << 8));
  410. }
  411. if (wordsize == 64) {
  412. p = (ULONG *) dest;
  413. for (i = h * w / 64; --i >= 0;) {
  414. data = *p++;
  415. p[-1] = p[0];
  416. *p++ = data;
  417. }
  418. }
  419. break;
  420. }
  421. default:
  422. t1_abort("xiFill: unknown format");
  423. }
  424. }
  425. }
  426. #define ALLONES 0xFF
  427. static void fillrun(register char *p, pel x0, pel x1, int bit)
  428. {
  429. register int startmask,endmask; /* bits to set in first and last char*/
  430. register int middle; /* number of chars between start and end + 1 */
  431. if (x1 <= x0)
  432. return;
  433. middle = x1/8 - x0/8;
  434. p += x0/8;
  435. x0 &= 7; x1 &= 7;
  436. if (bit == LSBFirst) {
  437. startmask = ALLONES << x0;
  438. endmask = ~(ALLONES << x1);
  439. }
  440. else {
  441. startmask = ALLONES >> x0;
  442. endmask = ~(ALLONES >> x1);
  443. }
  444. if (middle == 0)
  445. *p++ |= startmask & endmask;
  446. else {
  447. *p++ |= startmask;
  448. while (--middle > 0)
  449. *p++ = ALLONES;
  450. *p |= endmask;
  451. }
  452. }
  453. static FontRendererRec renderers[] = {
  454. { ".pfa", 4, (int (*)()) 0, Type1OpenScalable,
  455. (int (*)()) 0, Type1GetInfoScalable, 0 },
  456. { ".pfb", 4, (int (*)()) 0, Type1OpenScalable,
  457. (int (*)()) 0, Type1GetInfoScalable, 0 }
  458. };
  459. Type1RegisterFontFileFunctions()
  460. {
  461. int i;
  462. T1InitStdProps();
  463. for (i=0; i < sizeof(renderers) / sizeof(FontRendererRec); i++)
  464. FontFileRegisterRenderer(&renderers[i]);
  465. }