PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/nx-3.5.0/nx-X11/lib/font/Speedo/sptobdf.c

#
C | 678 lines | 523 code | 86 blank | 69 comment | 85 complexity | 6e24fb00a09c35676b3bcca4c26b974d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $Xorg: sptobdf.c,v 1.4 2001/02/09 02:04:00 xorgcvs Exp $ */
  2. /*
  3. * Copyright 1990, 1991 Network Computing Devices;
  4. * Portions Copyright 1987 by Digital Equipment Corporation
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and its
  7. * documentation for any purpose is hereby granted without fee, provided that
  8. * the above copyright notice appear in all copies and that both that
  9. * copyright notice and this permission notice appear in supporting
  10. * documentation, and that the names of Network Computing Devices and
  11. * Digital not be used in advertising or publicity pertaining to
  12. * distribution of the software without specific, written prior permission.
  13. * Network Computing Devices and Digital make no representations about the
  14. * suitability of this software for any purpose. It is provided "as is"
  15. * without express or implied warranty.
  16. *
  17. * NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
  18. * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  19. * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES OR DIGITAL BE
  20. * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  22. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  23. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * Dave Lemke
  26. */
  27. /*
  28. Copyright 1987, 1994, 1998 The Open Group
  29. Permission to use, copy, modify, distribute, and sell this software and its
  30. documentation for any purpose is hereby granted without fee, provided that
  31. the above copyright notice appear in all copies and that both that
  32. copyright notice and this permission notice appear in supporting
  33. documentation.
  34. The above copyright notice and this permission notice shall be included
  35. in all copies or substantial portions of the Software.
  36. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  37. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  38. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  39. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  40. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  41. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  42. OTHER DEALINGS IN THE SOFTWARE.
  43. Except as contained in this notice, the name of The Open Group shall
  44. not be used in advertising or otherwise to promote the sale, use or
  45. other dealings in this Software without prior written authorization
  46. from The Open Group.
  47. */
  48. /*
  49. * Speedo outline to BFD format converter
  50. */
  51. #include <stdio.h>
  52. #include "speedo.h"
  53. #ifdef EXTRAFONTS
  54. #include "ncdkeys.h"
  55. #else
  56. #include "keys.h"
  57. #endif
  58. #include "iso8859.h"
  59. #define MAX_BITS 1024
  60. #define BBOX_CLIP
  61. static char line_of_bits[MAX_BITS + 1];
  62. static FILE *fp;
  63. static ufix16 char_index,
  64. char_id;
  65. static buff_t font;
  66. static buff_t char_data;
  67. static ufix8 *f_buffer,
  68. *c_buffer;
  69. static ufix16 mincharsize;
  70. static fix15 cur_y;
  71. static fix15 bit_width,
  72. bit_height;
  73. static ufix8 key[] =
  74. {
  75. KEY0,
  76. KEY1,
  77. KEY2,
  78. KEY3,
  79. KEY4,
  80. KEY5,
  81. KEY6,
  82. KEY7,
  83. KEY8
  84. }; /* Font decryption key */
  85. static char *progname;
  86. static char *fontname = NULL;
  87. static char *fontfile = NULL;
  88. static int point_size = 120;
  89. static int x_res = 72;
  90. static int y_res = 72;
  91. static int quality = 0;
  92. static int iso_encoding = 1;
  93. static int num_props = 7;
  94. static int stretch = 120;
  95. static specs_t specs;
  96. static void dump_header();
  97. static void
  98. usage()
  99. {
  100. fprintf(stderr, "Usage: %s [-xres x resolution] [-yres y resolution]\n\t[-ptsize pointsize] [-fn fontname] [-q quality (0-1)] fontfile\n", progname);
  101. fprintf(stderr, "Where:\n");
  102. fprintf(stderr, "-xres specifies the X resolution (72)\n");
  103. fprintf(stderr, "-yres specifies the Y resolution (72)\n");
  104. fprintf(stderr, "-pts specifies the pointsize in decipoints (120)\n");
  105. fprintf(stderr, "-fn specifies the font name (full Bitstream name)\n");
  106. fprintf(stderr, "-q specifies the font quality [0-1] (0)\n");
  107. fprintf(stderr, "\n");
  108. exit(0);
  109. }
  110. static fix15
  111. read_2b(ptr)
  112. ufix8 *ptr;
  113. {
  114. fix15 tmp;
  115. tmp = *ptr++;
  116. tmp = (tmp << 8) + *ptr;
  117. return tmp;
  118. }
  119. static fix31
  120. read_4b(ptr)
  121. ufix8 *ptr;
  122. {
  123. fix31 tmp;
  124. tmp = *ptr++;
  125. tmp = (tmp << 8) + *ptr++;
  126. tmp = (tmp << 8) + *ptr++;
  127. tmp = (tmp << 8) + *ptr;
  128. return tmp;
  129. }
  130. static void
  131. process_args(ac, av)
  132. int ac;
  133. char **av;
  134. {
  135. int i;
  136. for (i = 1; i < ac; i++) {
  137. if (!strncmp(av[i], "-xr", 3)) {
  138. if (av[i + 1]) {
  139. x_res = atoi(av[++i]);
  140. } else
  141. usage();
  142. } else if (!strncmp(av[i], "-yr", 3)) {
  143. if (av[i + 1]) {
  144. y_res = atoi(av[++i]);
  145. } else
  146. usage();
  147. } else if (!strncmp(av[i], "-pt", 3)) {
  148. if (av[i + 1]) {
  149. point_size = atoi(av[++i]);
  150. } else
  151. usage();
  152. } else if (!strncmp(av[i], "-fn", 3)) {
  153. if (av[i + 1]) {
  154. fontname = av[++i];
  155. } else
  156. usage();
  157. } else if (!strncmp(av[i], "-q", 2)) {
  158. if (av[i + 1]) {
  159. quality = atoi(av[++i]);
  160. } else
  161. usage();
  162. } else if (!strncmp(av[i], "-st", 3)) {
  163. if (av[i + 1]) {
  164. stretch = atoi(av[++i]);
  165. } else
  166. usage();
  167. } else if (!strncmp(av[i], "-noni", 5)) {
  168. iso_encoding = 0;
  169. } else if (*av[i] == '-') {
  170. usage();
  171. } else
  172. fontfile = av[i];
  173. }
  174. if (!fontfile)
  175. usage();
  176. }
  177. void
  178. main(argc, argv)
  179. int argc;
  180. char **argv;
  181. {
  182. ufix32 i;
  183. ufix8 tmp[16];
  184. ufix32 minbufsize;
  185. ufix16 cust_no;
  186. int first_char_index,
  187. num_chars;
  188. progname = argv[0];
  189. process_args(argc, argv);
  190. fp = fopen(fontfile, "r");
  191. if (!fp) {
  192. fprintf(stderr, "No such font file, \"%s\"\n", fontfile);
  193. exit(-1);
  194. }
  195. if (fread(tmp, sizeof(ufix8), 16, fp) != 16) {
  196. fprintf(stderr, "error reading \"%s\"\n", fontfile);
  197. exit(-1);
  198. }
  199. minbufsize = (ufix32) read_4b(tmp + FH_FBFSZ);
  200. f_buffer = (ufix8 *) malloc(minbufsize);
  201. if (!f_buffer) {
  202. fprintf(stderr, "can't get %x bytes of memory\n", minbufsize);
  203. exit(-1);
  204. }
  205. fseek(fp, (ufix32) 0, 0);
  206. if (fread(f_buffer, sizeof(ufix8), (ufix16) minbufsize, fp) != minbufsize) {
  207. fprintf(stderr, "error reading file \"%s\"\n", fontfile);
  208. exit(-1);
  209. }
  210. mincharsize = read_2b(f_buffer + FH_CBFSZ);
  211. c_buffer = (ufix8 *) malloc(mincharsize);
  212. if (!c_buffer) {
  213. fprintf(stderr, "can't get %x bytes for char buffer\n", mincharsize);
  214. exit(-1);
  215. }
  216. /* init */
  217. sp_reset();
  218. font.org = f_buffer;
  219. font.no_bytes = minbufsize;
  220. if ((cust_no = sp_get_cust_no(font)) != CUS0) {
  221. fprintf(stderr, "Non-standard encryption for \"%s\"\n", fontfile);
  222. exit(-1);
  223. }
  224. sp_set_key(key);
  225. first_char_index = read_2b(f_buffer + FH_FCHRF);
  226. num_chars = read_2b(f_buffer + FH_NCHRL);
  227. /* set up specs */
  228. /* Note that point size is in decipoints */
  229. specs.pfont = &font;
  230. /* XXX beware of overflow */
  231. specs.xxmult = point_size * x_res / 720 * (1 << 16);
  232. specs.xymult = 0L << 16;
  233. specs.xoffset = 0L << 16;
  234. specs.yxmult = 0L << 16;
  235. specs.yymult = point_size * y_res / 720 * (1 << 16);
  236. specs.yoffset = 0L << 16;
  237. switch (quality) {
  238. case 0:
  239. specs.flags = 0;
  240. break;
  241. case 1:
  242. specs.flags = MODE_SCREEN;
  243. break;
  244. case 2:
  245. specs.flags = MODE_2D;
  246. break;
  247. default:
  248. fprintf(stderr, "bogus quality value %d\n", quality);
  249. break;
  250. }
  251. specs.out_info = NULL;
  252. if (!fontname) {
  253. fontname = (char *) (f_buffer + FH_FNTNM);
  254. }
  255. if (iso_encoding)
  256. num_chars = num_iso_chars;
  257. dump_header(num_chars);
  258. if (!sp_set_specs(&specs)) {
  259. fprintf(stderr, "can't set specs\n");
  260. } else {
  261. if (iso_encoding) {
  262. for (i = 0; i < num_iso_chars * 2; i += 2) {
  263. char_index = iso_map[i + 1];
  264. char_id = iso_map[i];
  265. if (!sp_make_char(char_index)) {
  266. fprintf(stderr, "can't make char %x\n", char_index);
  267. }
  268. }
  269. } else {
  270. for (i = 0; i < num_chars; i++) {
  271. char_index = i + first_char_index;
  272. char_id = sp_get_char_id(char_index);
  273. if (char_id) {
  274. if (!sp_make_char(char_index)) {
  275. fprintf(stderr, "can't make char %x\n", char_index);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. (void) fclose(fp);
  282. printf("ENDFONT\n");
  283. exit(0);
  284. }
  285. static void
  286. dump_header(num_chars)
  287. ufix32 num_chars;
  288. {
  289. fix15 xmin,
  290. ymin,
  291. xmax,
  292. ymax;
  293. fix15 ascent,
  294. descent;
  295. fix15 pixel_size;
  296. xmin = read_2b(f_buffer + FH_FXMIN);
  297. ymin = read_2b(f_buffer + FH_FYMIN);
  298. xmax = read_2b(f_buffer + FH_FXMAX);
  299. ymax = read_2b(f_buffer + FH_FYMAX);
  300. pixel_size = point_size * x_res / 720;
  301. printf("STARTFONT 2.1\n");
  302. printf("COMMENT\n");
  303. printf("COMMENT Generated from Bitstream Speedo outlines via sptobdf\n");
  304. printf("COMMENT\n");
  305. printf("FONT %s\n", fontname);
  306. printf("SIZE %d %d %d\n", pixel_size, x_res, y_res);
  307. printf("FONTBOUNDINGBOX %d %d %d %d\n", xmin, ymin, xmax, ymax);
  308. printf("STARTPROPERTIES %d\n", num_props);
  309. printf("RESOLUTION_X %d\n", x_res);
  310. printf("RESOLUTION_Y %d\n", y_res);
  311. printf("POINT_SIZE %d\n", point_size);
  312. printf("PIXEL_SIZE %d\n", pixel_size);
  313. printf("COPYRIGHT \"%s\"\n", f_buffer + FH_CPYRT);
  314. /* do some stretching here so that its isn't too tight */
  315. pixel_size = pixel_size * stretch / 100;
  316. ascent = pixel_size * 764 / 1000; /* 764 == EM_TOP */
  317. descent = pixel_size - ascent;
  318. printf("FONT_ASCENT %d\n", ascent);
  319. printf("FONT_DESCENT %d\n", descent);
  320. printf("ENDPROPERTIES\n");
  321. printf("CHARS %d\n", num_chars);
  322. }
  323. buff_t *
  324. sp_load_char_data(file_offset, num, cb_offset)
  325. fix31 file_offset;
  326. fix15 num;
  327. fix15 cb_offset;
  328. {
  329. if (fseek(fp, (long) file_offset, (int) 0)) {
  330. fprintf(stderr, "can't seek to char\n");
  331. (void) fclose(fp);
  332. exit(-1);
  333. }
  334. if ((num + cb_offset) > mincharsize) {
  335. fprintf(stderr, "char buf overflow\n");
  336. (void) fclose(fp);
  337. exit(-2);
  338. }
  339. if (fread((c_buffer + cb_offset), sizeof(ufix8), num, fp) != num) {
  340. fprintf(stderr, "can't get char data\n");
  341. exit(-1);
  342. }
  343. char_data.org = (ufix8 *) c_buffer + cb_offset;
  344. char_data.no_bytes = num;
  345. return &char_data;
  346. }
  347. /*
  348. * Called by Speedo character generator to report an error.
  349. *
  350. * Since character data not available is one of those errors
  351. * that happens many times, don't report it to user
  352. */
  353. void
  354. sp_report_error(n)
  355. fix15 n;
  356. {
  357. switch (n) {
  358. case 1:
  359. fprintf(stderr, "Insufficient font data loaded\n");
  360. break;
  361. case 3:
  362. fprintf(stderr, "Transformation matrix out of range\n");
  363. break;
  364. case 4:
  365. fprintf(stderr, "Font format error\n");
  366. break;
  367. case 5:
  368. fprintf(stderr, "Requested specs not compatible with output module\n");
  369. break;
  370. case 7:
  371. fprintf(stderr, "Intelligent transformation requested but not supported\n");
  372. break;
  373. case 8:
  374. fprintf(stderr, "Unsupported output mode requested\n");
  375. break;
  376. case 9:
  377. fprintf(stderr, "Extended font loaded but only compact fonts supported\n");
  378. break;
  379. case 10:
  380. fprintf(stderr, "Font specs not set prior to use of font\n");
  381. break;
  382. case 12:
  383. break;
  384. case 13:
  385. fprintf(stderr, "Track kerning data not available()\n");
  386. break;
  387. case 14:
  388. fprintf(stderr, "Pair kerning data not available()\n");
  389. break;
  390. default:
  391. fprintf(stderr, "report_error(%d)\n", n);
  392. break;
  393. }
  394. }
  395. void
  396. sp_open_bitmap(x_set_width, y_set_width, xorg, yorg, xsize, ysize)
  397. fix31 x_set_width;
  398. fix31 y_set_width;
  399. fix31 xorg;
  400. fix31 yorg;
  401. fix15 xsize;
  402. fix15 ysize;
  403. {
  404. fix15 i;
  405. fix15 off_horz;
  406. fix15 off_vert;
  407. fix31 width,
  408. pix_width;
  409. bbox_t bb;
  410. bit_width = xsize;
  411. bit_height = ysize;
  412. off_horz = (fix15) ((xorg + 32768L) >> 16);
  413. off_vert = (fix15) ((yorg + 32768L) >> 16);
  414. if (bit_width > MAX_BITS) {
  415. #ifdef DEBUG
  416. fprintf(stderr, "char wider than max bits -- truncated\n");
  417. #endif
  418. bit_width = MAX_BITS;
  419. }
  420. width = sp_get_char_width(char_index);
  421. pix_width = width * (specs.xxmult / 65536L) +
  422. ((ufix32) width * ((ufix32) specs.xxmult & 0xffff)) / 65536L;
  423. pix_width /= 65536L;
  424. width = (pix_width * 7200L) / (point_size * y_res);
  425. (void) sp_get_char_bbox(char_index, &bb);
  426. bb.xmin >>= 16;
  427. bb.ymin >>= 16;
  428. bb.xmax >>= 16;
  429. bb.ymax >>= 16;
  430. #ifdef DEBUG
  431. if ((bb.xmax - bb.xmin) != bit_width)
  432. fprintf(stderr, "bbox & width mismatch 0x%x (%d) (%d vs %d)\n",
  433. char_index, char_id, (bb.xmax - bb.xmin), bit_width);
  434. if ((bb.ymax - bb.ymin) != bit_height)
  435. fprintf(stderr, "bbox & height mismatch 0x%x (%d) (%d vs %d)\n",
  436. char_index, char_id, (bb.ymax - bb.ymin), bit_height);
  437. if (bb.xmin != off_horz)
  438. fprintf(stderr, "x min mismatch 0x%x (%d) (%d vs %d)\n",
  439. char_index, char_id, bb.xmin, off_horz);
  440. if (bb.ymin != off_vert)
  441. fprintf(stderr, "y min mismatch 0x%x (%d) (%d vs %d)\n",
  442. char_index, char_id, bb.ymin, off_vert);
  443. #endif
  444. #ifdef BBOX_CLIP
  445. bit_width = bb.xmax - bb.xmin;
  446. bit_height = bb.ymax - bb.ymin;
  447. off_horz = bb.xmin;
  448. off_vert = bb.ymin;
  449. #endif
  450. /* XXX kludge to handle space */
  451. if (bb.xmin == 0 && bb.ymin == 0 && bb.xmax == 0 && bb.ymax == 0 &&
  452. width) {
  453. bit_width = 1;
  454. bit_height = 1;
  455. }
  456. printf("STARTCHAR %d\n", char_id);
  457. printf("ENCODING %d\n", char_id);
  458. printf("SWIDTH %d 0\n", width);
  459. printf("DWIDTH %d 0\n", pix_width);
  460. printf("BBX %d %d %d %d\n", bit_width, bit_height, off_horz, off_vert);
  461. printf("BITMAP\n");
  462. for (i = 0; i < bit_width; i++) {
  463. line_of_bits[i] = '.';
  464. }
  465. line_of_bits[bit_width] = '\0';
  466. cur_y = 0;
  467. }
  468. static void
  469. dump_line(line)
  470. ufix8 *line;
  471. {
  472. int bit;
  473. unsigned byte;
  474. byte = 0;
  475. for (bit = 0; bit < bit_width; bit++) {
  476. if (line[bit] == 'X')
  477. byte |= (1 << (7 - (bit & 7)));
  478. if ((bit & 7) == 7) {
  479. printf("%02X", byte);
  480. byte = 0;
  481. }
  482. }
  483. if ((bit & 7) != 0)
  484. printf("%02X", byte);
  485. printf("\n");
  486. }
  487. #ifdef BBOX_CLIP
  488. static fix15 last_y;
  489. static int trunc = 0;
  490. #endif
  491. void
  492. sp_set_bitmap_bits(y, xbit1, xbit2)
  493. fix15 y;
  494. fix15 xbit1;
  495. fix15 xbit2;
  496. {
  497. fix15 i;
  498. if (xbit1 > MAX_BITS) {
  499. #ifdef DEBUG
  500. fprintf(stderr, "run wider than max bits -- truncated\n");
  501. #endif
  502. xbit1 = MAX_BITS;
  503. }
  504. if (xbit2 > MAX_BITS) {
  505. #ifdef DEBUG
  506. fprintf(stderr, "run wider than max bits -- truncated\n");
  507. #endif
  508. xbit2 = MAX_BITS;
  509. }
  510. while (cur_y != y) {
  511. dump_line(line_of_bits);
  512. for (i = 0; i < bit_width; i++) {
  513. line_of_bits[i] = '.';
  514. }
  515. cur_y++;
  516. }
  517. #ifdef BBOX_CLIP
  518. last_y = y;
  519. if (y >= bit_height) {
  520. #ifdef DEBUG
  521. fprintf(stderr,
  522. "y value is larger than height 0x%x (%d) -- truncated\n",
  523. char_index, char_id);
  524. #endif
  525. trunc = 1;
  526. return;
  527. }
  528. #endif /* BBOX_CLIP */
  529. for (i = xbit1; i < xbit2; i++) {
  530. line_of_bits[i] = 'X';
  531. }
  532. }
  533. void
  534. sp_close_bitmap()
  535. {
  536. #ifdef BBOX_CLIP
  537. int i;
  538. if (!trunc)
  539. dump_line(line_of_bits);
  540. trunc = 0;
  541. last_y++;
  542. while (last_y < bit_height) {
  543. #ifdef DEBUG
  544. fprintf(stderr, "padding out height for 0x%x (%d)\n",
  545. char_index, char_id);
  546. #endif
  547. for (i = 0; i < bit_width; i++) {
  548. line_of_bits[i] = '.';
  549. }
  550. dump_line(line_of_bits);
  551. last_y++;
  552. }
  553. #else
  554. dump_line(line_of_bits);
  555. #endif
  556. printf("ENDCHAR\n");
  557. }
  558. /* outline stubs */
  559. void
  560. sp_open_outline()
  561. {
  562. }
  563. void
  564. sp_start_new_char()
  565. {
  566. }
  567. void
  568. sp_start_contour()
  569. {
  570. }
  571. void
  572. sp_curve_to()
  573. {
  574. }
  575. void
  576. sp_line_to()
  577. {
  578. }
  579. void
  580. sp_close_contour()
  581. {
  582. }
  583. void
  584. sp_close_outline()
  585. {
  586. }