header.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright 1990, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. * Copyright 1990 Network Computing Devices;
  20. * Portions Copyright 1987 by Digital Equipment Corporation
  21. *
  22. * Permission to use, copy, modify, distribute, and sell this software and
  23. * its documentation for any purpose is hereby granted without fee, provided
  24. * that the above copyright notice appear in all copies and that both that
  25. * copyright notice and this permission notice appear in supporting
  26. * documentation, and that the names of Network Computing Devices, or Digital
  27. * not be used in advertising or publicity pertaining to distribution
  28. * of the software without specific, written prior permission.
  29. *
  30. * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
  31. * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
  33. * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  34. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  35. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  36. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  37. * THIS SOFTWARE.
  38. */
  39. #include <stdio.h>
  40. #include <X11/Xosdefs.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "fstobdf.h"
  44. unsigned long pointSize;
  45. unsigned long yResolution;
  46. static const char *warning[] =
  47. {
  48. "COMMENT ",
  49. "COMMENT WARNING: This bdf file was generated from a font server using",
  50. "COMMENT fstobdf. The resulting font is subject to the same copyright,",
  51. "COMMENT license, and trademark restrictions as the original font. The",
  52. "COMMENT authors and distributors of fstobdf disclaim all liability for",
  53. "COMMENT misuse of the program or its output.",
  54. "COMMENT ",
  55. NULL
  56. };
  57. static char *
  58. FindStringProperty(const char *propName,
  59. unsigned int *propLength,
  60. FSPropInfo *propInfo,
  61. FSPropOffset *propOffsets,
  62. unsigned char *propData)
  63. {
  64. FSPropOffset *propOffset;
  65. unsigned int length;
  66. unsigned int i;
  67. propOffset = &propOffsets[0];
  68. length = (unsigned int) strlen(propName);
  69. for (i = propInfo->num_offsets; i--; propOffset++) {
  70. if (propOffset->type == PropTypeString) {
  71. #ifdef DEBUG
  72. char pname[256];
  73. memmove( pname, propData + propOffset->name.position,
  74. propOffset->name.length);
  75. pname[propOffset->name.length] = '\0';
  76. fprintf(stderr, "prop name: %s (len %d)\n",
  77. pname, propOffset->name.length);
  78. #endif
  79. if ((propOffset->name.length == length) &&
  80. !strncmp((char*)propData + propOffset->name.position, propName, length)) {
  81. *propLength = propOffset->value.length;
  82. return (char *)(propData + propOffset->value.position);
  83. }
  84. }
  85. }
  86. *propLength = 0;
  87. return (NULL);
  88. }
  89. static int
  90. FindNumberProperty(const char *propName,
  91. unsigned long *propValue,
  92. FSPropInfo *propInfo,
  93. FSPropOffset *propOffsets,
  94. unsigned char *propData)
  95. {
  96. FSPropOffset *propOffset;
  97. unsigned int i;
  98. unsigned int length;
  99. propOffset = &propOffsets[0];
  100. length = (unsigned int) strlen(propName);
  101. for (i = propInfo->num_offsets; i--; propOffset++) {
  102. if ((propOffset->type == PropTypeSigned) ||
  103. (propOffset->type == PropTypeUnsigned)) {
  104. if ((propOffset->name.length == length) &&
  105. !strncmp((char*)propData + propOffset->name.position, propName, length)) {
  106. *propValue = propOffset->value.position;
  107. return (propOffset->type);
  108. }
  109. }
  110. }
  111. return (-1);
  112. }
  113. /*
  114. * EmitHeader - print STARTFONT, COMMENT lines, FONT, SIZE, and
  115. * FONTBOUNDINGBOX lines
  116. */
  117. Bool
  118. EmitHeader(FILE *outFile,
  119. FSXFontInfoHeader *fontHeader,
  120. FSPropInfo *propInfo,
  121. FSPropOffset *propOffsets,
  122. unsigned char *propData)
  123. {
  124. unsigned int len;
  125. int type;
  126. char *cp;
  127. const char **cpp;
  128. unsigned long xResolution;
  129. fprintf(outFile, "STARTFONT 2.1\n");
  130. /*
  131. * find COPYRIGHT message and print it first, followed by warning
  132. */
  133. cp = FindStringProperty("COPYRIGHT", &len, propInfo, propOffsets,
  134. propData);
  135. if (cp) {
  136. fprintf(outFile, "COMMENT \nCOMMENT ");
  137. fwrite(cp, 1, len, outFile);
  138. fputc('\n', outFile);
  139. }
  140. for (cpp = warning; *cpp; cpp++)
  141. fprintf(outFile, "%s\n", *cpp);
  142. /*
  143. * FONT name
  144. */
  145. cp = FindStringProperty("FONT", &len, propInfo, propOffsets, propData);
  146. if (cp) {
  147. fprintf(outFile, "FONT ");
  148. fwrite(cp, 1, len, outFile);
  149. fputc('\n', outFile);
  150. } else {
  151. fprintf(stderr, "unable to find FONT property\n");
  152. return (False);
  153. }
  154. /*
  155. * SIZE point xres yres
  156. *
  157. * Get XLFD values if possible, else fake it
  158. */
  159. type = FindNumberProperty("RESOLUTION_X", &xResolution, propInfo,
  160. propOffsets, propData);
  161. if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
  162. xResolution = 72;
  163. type = FindNumberProperty("RESOLUTION_Y", &yResolution, propInfo,
  164. propOffsets, propData);
  165. if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
  166. yResolution = 72;
  167. type = FindNumberProperty("POINT_SIZE", &pointSize, propInfo,
  168. propOffsets, propData);
  169. if ((type == PropTypeUnsigned) || (type == PropTypeSigned))
  170. pointSize = (pointSize + 5) / 10;
  171. else
  172. pointSize = ((fontHeader->font_ascent + fontHeader->font_descent)
  173. * 72) / yResolution;
  174. fprintf(outFile, "SIZE %lu %lu %lu\n", pointSize, xResolution,
  175. yResolution);
  176. /*
  177. * FONTBOUNDINGBOX width height xoff yoff
  178. *
  179. */
  180. fprintf(outFile, "FONTBOUNDINGBOX %d %d %d %d\n",
  181. fontHeader->max_bounds.right - fontHeader->min_bounds.left,
  182. fontHeader->max_bounds.ascent + fontHeader->max_bounds.descent,
  183. fontHeader->min_bounds.left,
  184. -fontHeader->max_bounds.descent);
  185. return (True);
  186. }