a simple textfile based psf font editor suite
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.
 
 
 

307 lines
7.3 KiB

  1. /* psf.h
  2. *
  3. * a simple psf handling library.
  4. *
  5. * Gunnar Zötl <gz@tset.de> 2016
  6. * Released under the terms of the MIT license. See file LICENSE for details.
  7. *
  8. * info about the psf font file format(s) from
  9. * http://www.win.tue.nl/~aeb/linux/kbd/font-formats-1.html
  10. */
  11. #ifndef psf_h
  12. #define psf_h
  13. /* this first part is copied more or less verbatim from th above source */
  14. #define PSF1_MAGIC0 0x36
  15. #define PSF1_MAGIC1 0x04
  16. #define PSF1_MODE512 0x01
  17. #define PSF1_MODEHASTAB 0x02
  18. #define PSF1_MODEHASSEQ 0x04
  19. #define PSF1_MAXMODE 0x05
  20. #define PSF1_SEPARATOR 0xFFFF
  21. #define PSF1_STARTSEQ 0xFFFE
  22. struct psf1_header {
  23. unsigned char magic[2]; /* Magic number */
  24. unsigned char mode; /* PSF font mode */
  25. unsigned char charsize; /* Character size */
  26. };
  27. #define PSF2_MAGIC0 0x72
  28. #define PSF2_MAGIC1 0xb5
  29. #define PSF2_MAGIC2 0x4a
  30. #define PSF2_MAGIC3 0x86
  31. /* bits used in flags */
  32. #define PSF2_HAS_UNICODE_TABLE 0x01
  33. /* max version recognized so far */
  34. #define PSF2_MAXVERSION 0
  35. /* UTF8 separators */
  36. #define PSF2_SEPARATOR 0xFF
  37. #define PSF2_STARTSEQ 0xFE
  38. struct psf2_header {
  39. unsigned char magic[4];
  40. unsigned int version;
  41. unsigned int headersize; /* offset of bitmaps in file */
  42. unsigned int flags;
  43. unsigned int length; /* number of glyphs */
  44. unsigned int charsize; /* number of bytes for each character */
  45. unsigned int height, width; /* max dimensions of glyphs */
  46. /* charsize = height * ((width + 7) / 8) */
  47. };
  48. /* representation of a single glyph, including unicode mapping information */
  49. struct psf_glyph {
  50. unsigned char* data;
  51. unsigned int nucvals;
  52. unsigned int* ucvals;
  53. };
  54. /* representation of a complete psf font. */
  55. struct psf_font {
  56. int version; /* 1 or 2 */
  57. union {
  58. struct psf1_header psf1;
  59. struct psf2_header psf2;
  60. } header;
  61. struct psf_glyph *glyph;
  62. };
  63. /* psf_width (macro)
  64. *
  65. * return the width of a psf font
  66. *
  67. * Arguments:
  68. * psf the psf font
  69. *
  70. * Returns:
  71. * the width of the font
  72. */
  73. #define psf_width(psf) (((psf)->version == 1) ? 8 : (psf)->header.psf2.width)
  74. /* psf_height (macro)
  75. *
  76. * return the height of a psf font
  77. *
  78. * Arguments:
  79. * psf the psf font
  80. *
  81. * Returns:
  82. * the height of the font
  83. */
  84. #define psf_height(psf) (((psf)->version == 1) ? (psf)->header.psf1.charsize : (psf)->header.psf2.height)
  85. /* psf_new
  86. *
  87. * allocates and intializes a new psf_font structure. Based upon the version,
  88. * the following applies:
  89. * if version==1:
  90. * width must be 8
  91. * charsize is precomputed as height
  92. * space for 256 glyphs is preallocated
  93. * if version==2:
  94. * charsize is precomputed as ((width + 7) / 8) * height
  95. * headersize and version fields are set to constant values (32 and 0)
  96. * no space for glyphs is preallocated
  97. *
  98. * Arguments:
  99. * version psf version to create font file for
  100. * width width of char. Must be 8 for version 1 fonts
  101. * height height of char
  102. *
  103. * Returns:
  104. * a pointer to the allocated and initialized psf_font structure, or 0 on
  105. * error.
  106. */
  107. struct psf_font *psf_new(unsigned int version, unsigned int width, unsigned int height);
  108. /* psf_load_fromfile
  109. *
  110. * loads a psf font from a file handle
  111. *
  112. * Arguments:
  113. * file the file handle to load the font from
  114. *
  115. * Returns:
  116. * a pointer to a psf_font structure containing the loaded font, or 0 on
  117. * error.
  118. */
  119. struct psf_font *psf_load_fromfile(FILE *file);
  120. /* psf_load
  121. *
  122. * load a psf font from a file
  123. *
  124. * Arguments:
  125. * filename the name of the file to load the font from
  126. *
  127. * Returns:
  128. * a pointer to a psf_font structure containing the loaded font, or 0 on
  129. * error.
  130. */
  131. struct psf_font *psf_load(const char *filename);
  132. /* psf_save_tofile
  133. *
  134. * saves a psf_font structure to a psf font file handle
  135. *
  136. * Arguments:
  137. * file the file handle to save to
  138. * psf the psf_font structure to save
  139. *
  140. * Returns:
  141. * 1 on success, 0 on failure.
  142. */
  143. int psf_save_tofile(FILE *file, struct psf_font *psf);
  144. /* psf_save
  145. *
  146. * saves a psf_font structure to a psf font file
  147. *
  148. * Arguments:
  149. * filename the name of the file to save to
  150. * psf the psf_font structure to save
  151. *
  152. * Returns:
  153. * 1 on success, 0 on failure.
  154. */
  155. int psf_save(const char *filename, struct psf_font *psf);
  156. /* psf_delete
  157. *
  158. * clean up and delete a psf_font structure. This also calls free() on the
  159. * pointer. Use this to deallocate what you get from psf_new or the psf_load*
  160. * functions.
  161. *
  162. * Arguments:
  163. * psf the psf font
  164. *
  165. * Returns:
  166. * -
  167. */
  168. void psf_delete(struct psf_font *psf);
  169. /* psf_getglyph
  170. *
  171. * returns a pointer to a glyph within a psf font
  172. *
  173. * Arguments:
  174. * psf the psf font
  175. *
  176. * Returns:
  177. * a pointer to a psf_glyph structure representing the <no>th glyph in the
  178. * font, or 0 on failure. You must not call free() on this pointer.
  179. */
  180. struct psf_glyph *psf_getglyph(struct psf_font *psf, unsigned int no);
  181. /* psf_addglyph
  182. *
  183. * initializes glyph number <no>, sees that it is available (if within the
  184. * valid range for glyph numbers for the font) and returns a pointer to it.
  185. * If the glyph was in use prior to this call, it will be newly initialized.
  186. *
  187. * Arguments:
  188. * psf the psf font
  189. * no the number of the glyph to initialize and return
  190. *
  191. * Returns:
  192. * a pointer to a psf_glyph structure representing the <no>th glyph in the
  193. * font, or 0 on failure. You must not call free() on this pointer.
  194. */
  195. struct psf_glyph *psf_addglyph(struct psf_font *psf, unsigned int no);
  196. /* psf_glyph_init
  197. *
  198. * (re-)initializes a glyph.
  199. *
  200. * Arguments:
  201. * psf the psf font
  202. * glyph a pointer to the glyph to initialize
  203. *
  204. * Returns:
  205. * 1 on success, 0 on failure.
  206. */
  207. int psf_glyph_init(struct psf_font *psf, struct psf_glyph *glyph);
  208. /* psf_glyph_setpx
  209. *
  210. * sets a pixel in the glyph
  211. *
  212. * Arguments:
  213. * psf the psf font
  214. * glyph a pointer to the glyph to change
  215. * x, y coordinates of the pixel to set
  216. * val value to set the pixel to, 0 or 1
  217. *
  218. * Returns:
  219. * 1 on success, 0 on failure.
  220. */
  221. int psf_glyph_setpx(struct psf_font *psf, struct psf_glyph *glyph, unsigned int x, unsigned int y, unsigned int val);
  222. /* psf_glyph_getpx
  223. *
  224. * gets a pixel value from a glyph
  225. *
  226. * Arguments:
  227. * psf the psf font
  228. * glyph the glyph to get the pixel value from
  229. * x, y coordinates of pixel to read
  230. *
  231. * Returns:
  232. * 1 if the pixel is set, 0 if not. Any pixels outside of the glyph
  233. * dimensions are treated as unset.
  234. */
  235. int psf_glyph_getpx(struct psf_font *psf, struct psf_glyph *glyph, unsigned int x, unsigned int y);
  236. /* psf_glyph_adducval
  237. *
  238. * adds a unicode value to a glyph. For a sequence, add PSF1_STARTSEQ and
  239. * then the unicode chars that make up the sequence. Note that if you add
  240. * a sequence, you can add more sequences but not more single unicode values.
  241. *
  242. * Arguments:
  243. * psf the psf font
  244. * glyph glyph to add unicode value to
  245. * uni unicode value to add
  246. *
  247. * Returns:
  248. * 1 on success, 0 on failure.
  249. */
  250. int psf_glyph_adducval(struct psf_font *psf, struct psf_glyph *glyph, unsigned int uni);
  251. /* psf_numglyphs
  252. *
  253. * return the amount of glyphs in a psf font. Note that for psf1 fonts, this
  254. * number is always either 256 or 512.
  255. *
  256. * Arguments:
  257. * psf the psf font
  258. *
  259. * Returns:
  260. * the number of glyphs in the font.
  261. */
  262. unsigned int psf_numglyphs(struct psf_font *psf);
  263. /* psf_hasunicodetable
  264. *
  265. * checks whether a font has an unicode table
  266. *
  267. * Arguments:
  268. * psf the psf font
  269. *
  270. * Returns:
  271. * 1 if the font has an unicode table, 0 if not.
  272. */
  273. unsigned int psf_hasunicodetable(struct psf_font *psf);
  274. #endif /* psf_h */