2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 #ifdef VERBOSE_REV_INFO
25 static char rcs_id[] = "$XConsortium: lineToData.c /main/1 1996/04/21 19:20:58 drk $";
26 #endif /* VERBOSE_REV_INFO */
30 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
31 * (c) Copyright 1993, 1994 International Business Machines Corp. *
32 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
33 * (c) Copyright 1993, 1994 Novell, Inc. *
41 #include "TermPrimLineFontP.h"
43 int ParseChar(char **str);
44 int parseCoord(char **str, char *val, signed char *offset);
45 char * parseToken(char **str);
46 void displayErrorString(FILE *f, char *orig, char *remain);
47 static void DumpChar(FILE *f, int charNum, charType *charList, int charListMax,
48 rect *rectList, int rectListMax, line *lineList, int lineListMax,
49 line *stippleList, int stippleListMax);
50 static void DumpGlyphs(FILE *f, char *prefix,
51 GlyphInfo glyphList, int glyphListMax);
52 static int parseLine(char **bufPtr, line *line);
53 static int parseRectangle(char **bufPtr, rect *rectangle);
54 static void order(char *p1, char *p2, signed char *offset1, signed char *offset2);
55 static char *vis(char val);
74 int resolution = 75; /* dots per inch */
76 char *prefix = "_Term";
78 charType *charList = (charType *) 0;
82 rect *rectList = (rect *) 0;
86 line *stippleList = (line *) 0;
87 int stippleListSize = 0;
88 int stippleListMax = 0;
90 line *lineList = (line *) 0;
94 GlyphInfo glyphList = (GlyphInfo) 0;
95 int glyphListSize = 0;
98 int charCount = 0; /* total number of chars in font*/
100 char *datafilename = "linegen.data";
104 while (EOF != (i = getopt(argc, argv, "p:f:"))) {
107 datafilename = optarg;
116 if (NULL == (f = fopen(datafilename, "r"))) {
117 (void) perror(datafilename);
121 /* read through the file and generate each character...
123 while (fgets(buffer, sizeof(buffer), f)) {
124 /* null out '\n'... */
125 buffer[strlen(buffer) - 1] = '\0';
127 /* if the line begins with '#', ignore it... */
131 /* if the line begins with a '!', just print it out (with the
134 if (*buffer == '!') {
135 (void) fputs(buffer + 1, stdout);
136 (void) putc('\n', stdout);
140 /* back up buffer... */
141 (void) strcpy(orig, buffer);
145 /* pull off token... */
146 c = parseToken(&bufPtr);
148 /* ignore blank lines... */
152 /* process the token... */
153 if (!strcmp(c, "char")) {
154 /* dump the previous character... */
155 if (charListMax > 0) {
156 (void) DumpChar(stdout, charCount++, charList, charListMax,
157 rectList, rectListMax, lineList, lineListMax,
158 stippleList, stippleListMax);
164 /* grow the glyph list if necessary... */
165 if (glyphListMax + 1 > glyphListSize) {
167 glyphList = (GlyphInfo) realloc(glyphList,
168 glyphListSize * sizeof(GlyphInfoRec));
170 /* bump count. We need to remember that this is one high
171 * when we use it as an index...
173 glyphList[glyphListMax].numRects = 0;
174 glyphList[glyphListMax].numLines = 0;
175 glyphList[glyphListMax].numStipples = 0;
176 (void) glyphListMax++;
178 /* this is a list of characters... */
181 charList = (charType *)
182 malloc((unsigned) (charListSize * sizeof(charType)));
185 /* extend the charList if we need to (the 2 is 1 for this
186 * entry and 1 for the terminating 0...
188 if (charListMax + 2 > charListSize) {
190 charList = (charType *) realloc(charList,
191 charListSize * sizeof(charType));
194 if (0 == (charList[charListMax++] = ParseChar(&bufPtr))) {
195 (void) fprintf(stderr, "invalid \"char\" specification:\n");
196 (void) displayErrorString(stderr, orig, bufPtr);
200 if (bufPtr && *bufPtr) {
201 if (*bufPtr == ',') {
202 /* additional stuff... */
204 /* skip whitespace... */
205 while (*bufPtr && strchr(" \t", *bufPtr))
208 /* error -- invalid character... */
209 (void) fprintf(stderr,
210 "invalid \"char\" specification:\n");
211 (void) displayErrorString(stderr, orig, bufPtr);
217 /* null term the list... */
218 charList[charListMax] = 0;
220 /* save away the char list... */
221 glyphList[glyphListMax - 1].chars = charList;
223 } else if (!strcmp(c, "rect")) {
224 if (rectListMax + 1 > rectListSize) {
226 rectList = (rect *) realloc(rectList,
227 rectListSize * sizeof(rect));
230 if (parseRectangle(&bufPtr, &rectList[rectListMax])) {
231 (void) fprintf(stderr, "invalid \"rect\" specification:\n");
232 (void) displayErrorString(stderr, orig, bufPtr);
236 /* increment line count... */
237 (void) rectListMax++;
239 /* save away the rect list... */
240 glyphList[glyphListMax - 1].rects = rectList;
241 glyphList[glyphListMax - 1].numRects = rectListMax;
243 } else if (!strcmp(c, "line")) {
244 if (lineListMax + 1 > lineListSize) {
246 lineList = (line *) realloc(lineList,
247 lineListSize * sizeof(line));
250 /* did we error?... */
251 if (parseLine(&bufPtr, &lineList[lineListMax])) {
252 (void) fprintf(stderr, "invalid \"line\" specification:\n");
253 (void) displayErrorString(stderr, orig, bufPtr);
257 /* increment line count... */
258 (void) lineListMax++;
260 /* save away the line list... */
261 glyphList[glyphListMax - 1].lines = lineList;
262 glyphList[glyphListMax - 1].numLines = lineListMax;
264 } else if (!strcmp(c, "stipple")) {
265 if (stippleListMax + 1 > stippleListSize) {
266 stippleListSize += 10;
267 stippleList = (line *) realloc(stippleList,
268 stippleListSize * sizeof(line));
271 if (parseLine(&bufPtr, &stippleList[stippleListMax])) {
272 (void) fprintf(stderr, "invalid \"stipple\" specification:\n");
273 (void) displayErrorString(stderr, orig, bufPtr);
278 /* increment line count... */
279 (void) stippleListMax++;
281 /* save away the line list... */
282 glyphList[glyphListMax - 1].stipples = stippleList;
283 glyphList[glyphListMax - 1].numStipples = stippleListMax;
286 /* unknown token... */
287 (void) fprintf(stderr, "invalid token:\n");
288 (void) displayErrorString(stderr, orig, orig);
293 /* dump out the last character(s)... */
294 if (charListMax > 0) {
295 (void) DumpChar(stdout, charCount++, charList, charListMax,
296 rectList, rectListMax, lineList, lineListMax,
297 stippleList, stippleListMax);
304 /* build the final structure... */
305 (void) DumpGlyphs(stdout, prefix, glyphList, glyphListMax);
311 ParseChar(char **str)
313 register char *c = *str;
317 while (*c && strchr(" \t", *c))
321 /* char format (i.e., 'c')...
324 /* skip over '\''... */
327 /* quoted character... */
328 /* skip over '\\'... */
362 ret = (int) strtol(c, &ptr, 8);
372 /* unquoted char... */
376 /* need to finish parsing the closing quote... */
381 } else if ((*c >= '0') || (*c <= '9')) {
384 ret = (int) strtol(c, &ptr, 0);
387 /* skip over whitespace... */
388 while (*c && strchr(" \t", *c))
391 /* update the pointer... */
397 parseCoord(char **str, char *val, signed char *offset)
399 register char *c = *str;
403 /* skip over whitespace... */
404 while (*c && strchr(" \t", *c))
407 /* end of line?... */
413 /* parse off number... */
414 *val = (char) strtol(c, &ptr, 0);
417 /* skip over whitespace... */
418 while (c && *c && strchr(" \t", *c))
421 /* parse off offset... */
422 if (c && ((*c == '-') || (*c == '+'))) {
425 /* skip over sign... */
427 /* skip over shitespace... */
428 while (*c && strchr(" \t", *c))
431 /* parse off offset... */
432 *offset = (char) strtol(c, &ptr, 0);
435 /* set sign of offset... */
438 /* skip over whitespace... */
439 while (c && *c && strchr(" \t", *c))
445 /* set up return ptr... */
452 parseToken(char **str)
454 register char *c = *str;
457 /* skip white space... */
458 while (*c && strchr(" \t", *c))
461 /* we hit start of token... */
463 while (*c && !strchr(" \t", *c))
466 /* null out the first whitespace... */
470 /* skip white space... */
471 while (*c && strchr(" \t", *c))
479 displayErrorString(FILE *f, char *orig, char *remain)
483 (void) fprintf(f, " %s\n", orig);
484 if (!remain || !*remain) {
487 col = strlen(orig) - strlen(remain);
489 /* this should not happen... */
493 /* add 8 for the indent... */
496 /* output space... */
499 /* output an '^'... */
501 (void) putc('\n', f);
505 parseLine(char **bufPtr, line *line)
507 signed char dummyChar;
510 /* set the bits that correspond to this line... */
511 /* parse off x1... */
512 error = parseCoord(bufPtr, &(line->x1),
515 /* parse off y1... */
517 error = parseCoord(bufPtr, &(line->y1),
520 /* parse off x2... */
522 error = parseCoord(bufPtr, &(line->x2),
525 /* parse off y2... */
527 error = parseCoord(bufPtr, &(line->y2),
530 /* parse off lineWidth... */
532 error = parseCoord(bufPtr, &(line->width),
535 /* did we error?... */
540 /* order x and y coords... */
541 (void) order(&(line->x1),
545 (void) order(&(line->y1),
554 parseRectangle(char **bufPtr, rect *rectangle)
558 /* parse off x1... */
559 error = parseCoord(bufPtr, &(rectangle->x1),
560 &(rectangle->x1Offset));
562 /* parse off y1... */
564 error = parseCoord(bufPtr, &(rectangle->y1),
565 &(rectangle->y1Offset));
567 /* parse off x2... */
569 error = parseCoord(bufPtr, &(rectangle->x2),
570 &(rectangle->x2Offset));
572 /* parse off y2... */
574 error = parseCoord(bufPtr, &(rectangle->y2),
575 &(rectangle->y2Offset));
577 /* did we error?... */
582 /* order x and y coords... */
583 (void) order(&(rectangle->x1),
585 &(rectangle->x1Offset),
586 &(rectangle->x2Offset));
587 (void) order(&(rectangle->y1),
589 &(rectangle->y1Offset),
590 &(rectangle->y2Offset));
596 order(char *p1, char *p2, signed char *offset1, signed char *offset2)
600 /* order coords... */
614 DumpGlyphs(FILE *f, char *prefix, GlyphInfo glyphList, int glyphListMax)
619 /* print the full glyph array... */
621 (void) fprintf(f, "int %sNumGlyphs = %d;\n", prefix, glyphListMax);
622 (void) fprintf(f, "GlyphInfoRec %sGlyphs[] = {\n", prefix);
623 for (i1 = 0; i1 < glyphListMax; i1++) {
624 /* print the characters this is for... */
625 (void) fputs(" /* data for ", f);
626 for (i2 = 0; glyphList[i1].chars[i2] != 0; i2++) {
630 (void) fputs(vis(glyphList[i1].chars[i2]), f);
633 (void) fputs("*/\n", f);
634 (void) fprintf(f, " {\n");
636 (void) fprintf(f, " chars%03d, /* char list */\n",
639 if (glyphList[i1].numRects > 0) {
640 (void) fprintf(f, " rects%03d, %2d,", i1,
641 glyphList[i1].numRects);
643 (void) fprintf(f, " (rect *) 0, 0,");
645 (void) fprintf(f, " /* rectangle list */\n");
647 if (glyphList[i1].numLines > 0) {
648 (void) fprintf(f, " lines%03d, %2d,", i1,
649 glyphList[i1].numLines);
651 (void) fprintf(f, " (line *) 0, 0,");
653 (void) fprintf(f, " /* line list */\n");
655 if (glyphList[i1].numStipples > 0) {
656 (void) fprintf(f, " stipples%03d, %2d,", i1,
657 glyphList[i1].numStipples);
659 (void) fprintf(f, " (line *) 0, 0,");
661 (void) fprintf(f, " /* stipple list */\n");
662 (void) printf(" },\n\n");
664 (void) printf("};\n");
668 DumpChar(FILE *f, int charCount, charType *charList, int charListMax,
669 rect *rectList, int rectListMax, line *lineList, int lineListMax,
670 line *stippleList, int stippleListMax)
674 /* print the characters this is for... */
675 (void) fputs("/* data for ", f);
676 for (i = 0; i < charListMax; i++) {
680 (void) fputs(vis(charList[i]), f);
683 (void) fputs("*/\n", f);
685 (void) fprintf(f, "static charType chars%03d[] = {", charCount);
686 for (i = 0; i < charListMax; i++) {
687 (void) fprintf(f, "0%03o, ", (int) charList[i]);
689 (void) fprintf(f, "0};\n");
691 if (rectListMax > 0) {
692 (void) fprintf(f, "static rect rects%03d[] = {\n", charCount);
693 for (i = 0; i < rectListMax; i++) {
694 (void) fprintf(f, " {%d, %d, %d, %d, %d, %d, %d, %d},\n",
695 rectList[i].x1, rectList[i].x1Offset,
696 rectList[i].y1, rectList[i].y1Offset,
697 rectList[i].x2, rectList[i].x2Offset,
698 rectList[i].y2, rectList[i].y2Offset);
700 (void) fprintf(f, "};\n");
703 if (lineListMax > 0) {
704 (void) fprintf(f, "static line lines%03d[] = {\n", charCount);
705 for (i = 0; i < lineListMax; i++) {
706 (void) fprintf(f, " {%d, %d, %d, %d, %d, %d, %d, %d, %d},\n",
707 lineList[i].x1, lineList[i].x1Offset,
708 lineList[i].y1, lineList[i].y1Offset,
709 lineList[i].x2, lineList[i].x2Offset,
710 lineList[i].y2, lineList[i].y2Offset,
713 (void) fprintf(f, "};\n");
716 if (stippleListMax > 0) {
717 (void) fprintf(f, "static line stipples%03d[] = {\n", charCount);
718 for (i = 0; i < stippleListMax; i++) {
719 (void) fprintf(f, " {%d, %d, %d, %d, %d, %d, %d, %d, %d},\n",
720 stippleList[i].x1, stippleList[i].x1Offset,
721 stippleList[i].y1, stippleList[i].y1Offset,
722 stippleList[i].x2, stippleList[i].x2Offset,
723 stippleList[i].y2, stippleList[i].y2Offset,
724 stippleList[i].width);
726 (void) fprintf(f, "};\n");
728 (void) fprintf(f, "\n");
738 (void) strcpy(buffer, "'^^'");
739 } else if (val == '\\') {
740 (void) strcpy(buffer, "'\\\\'");
741 } else if (val == '\'') {
742 (void) strcpy(buffer, "'\\\''");
744 (void) sprintf(buffer, "'%c'", val);
746 } else if (iscntrl(val)) {
747 (void) sprintf(buffer, "'^%c'", val);
749 (void) sprintf(buffer, "'\\%03o'", val);
752 return(strdup(buffer));