libsfn.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * libsfn/libsfn.h
  3. *
  4. * Copyright (C) 2020 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief SSFN Utility functions
  27. *
  28. */
  29. #include <stdint.h>
  30. #include "ssfn.h"
  31. #define iswhitespace(x) ((x)==0x20||(x)==0xA0)
  32. /**
  33. * Messages for progress bar and progress bar callback prototype
  34. */
  35. enum {
  36. PBAR_NONE = 0,
  37. PBAR_MEASURE,
  38. PBAR_OUTLINE,
  39. PBAR_GETKERN,
  40. PBAR_QUANT,
  41. PBAR_RDFILE,
  42. PBAR_BITMAP,
  43. PBAR_SQRPIX,
  44. PBAR_TALLPIX,
  45. PBAR_WIDEPIX,
  46. PBAR_GENFRAG,
  47. PBAR_COMPFRAG,
  48. PBAR_SERFRAG,
  49. PBAR_WRTCHARS,
  50. PBAR_WRTFILE,
  51. PBAR_RASTERIZE,
  52. PBAR_VECTORIZE,
  53. PBAR_LINES,
  54. PBAR_COPY
  55. };
  56. typedef void (*sfnprogressbar_t)(int step, int numstep, int curr, int total, int msg);
  57. /**
  58. * Glyph cache structure, private (same as ssfn_glyph_t but with larger bit widths)
  59. */
  60. typedef struct {
  61. uint16_t p; /* data buffer pitch, bytes per line */
  62. uint16_t h; /* data buffer height */
  63. uint16_t o; /* overlap of glyph, scaled to size */
  64. uint16_t x; /* advance x, scaled to size */
  65. uint16_t y; /* advance y, scaled to size */
  66. uint16_t a; /* ascender, scaled to size */
  67. uint16_t d; /* descender, scaled to size */
  68. uint8_t data[(260 + 260 / SSFN_ITALIC_DIV) << 8]; /* data buffer */
  69. } sfngc_t;
  70. /**
  71. * Fragments structure, private
  72. */
  73. typedef struct {
  74. unsigned char type;
  75. int w, h;
  76. int pos;
  77. int len;
  78. int cnt;
  79. int idx;
  80. unsigned char *data;
  81. } sfnfrag_t;
  82. /**
  83. * Kerning group, private
  84. */
  85. typedef struct {
  86. int first;
  87. int last;
  88. int idx;
  89. } sfnkgrp_t;
  90. /**
  91. * Kerning position, private
  92. */
  93. typedef struct {
  94. int idx;
  95. int pos;
  96. int len;
  97. unsigned char *data;
  98. } sfnkpos_t;
  99. /**
  100. * Contour command
  101. */
  102. typedef struct {
  103. unsigned char type; /* one of SSFN_CONTOUR_x defines */
  104. unsigned char px; /* on curve point coordinates */
  105. unsigned char py;
  106. unsigned char c1x; /* control point #1 coordinates */
  107. unsigned char c1y;
  108. unsigned char c2x; /* control point #2 coordinates */
  109. unsigned char c2y;
  110. } sfncont_t;
  111. /**
  112. * One glyph layer
  113. */
  114. typedef struct {
  115. unsigned char type; /* one of SSFN_FRAG_x defines */
  116. unsigned char color; /* color palette index, 0xFE foreground, 0xFF background */
  117. int len, miny, minx; /* private properties, len = sfncont_t array length */
  118. unsigned char *data; /* either color index map, or sfncont_t array */
  119. } sfnlayer_t;
  120. /**
  121. * One kerning relation
  122. */
  123. typedef struct {
  124. int n; /* next code point in relation */
  125. char x; /* relative horizontal offset */
  126. char y; /* relative vertical offset */
  127. } sfnkern_t;
  128. /**
  129. * One glyph
  130. */
  131. typedef struct {
  132. unsigned char width; /* glyph width */
  133. unsigned char height; /* glyph height */
  134. unsigned char ovl_x; /* overlay x */
  135. unsigned char adv_x; /* horizontal advance */
  136. unsigned char adv_y; /* vertical advance */
  137. unsigned char numlayer; /* number of layers */
  138. sfnlayer_t *layers; /* the layers */
  139. int numkern, kgrp; /* private, number of kerning relations and groups */
  140. sfnkern_t *kern; /* kerning relations array */
  141. unsigned char rtl, hintv[33], hinth[33]; /* right-toleft flag and hinting */
  142. int numfrag; /* private, used by sfn_save() */
  143. int *frags;
  144. } sfnglyph_t;
  145. /**
  146. * In-memory font structure
  147. */
  148. typedef struct {
  149. unsigned char family; /* font family type, see sfn_setfamilytype() */
  150. unsigned char style; /* ORd SSFN_STYLE_x defines */
  151. unsigned char width; /* overall font width */
  152. unsigned char height; /* overall font height */
  153. unsigned char baseline; /* baseline (ascent) */
  154. unsigned char underline; /* underline position */
  155. char *filename; /* filename */
  156. char *name; /* unique name of the font */
  157. char *familyname; /* font family name */
  158. char *subname; /* font family sub-name */
  159. char *revision; /* font revision / version string */
  160. char *manufacturer; /* font designer / creator / foundry */
  161. char *license; /* font license */
  162. sfnglyph_t glyphs[0x110000];/* glyphs array */
  163. int numcpal; /* number of color palette entries */
  164. unsigned char cpal[1024]; /* color palette */
  165. char *ligatures[SSFN_LIG_LAST-SSFN_LIG_FIRST+1]; /* ligatures, UTF-8 strings for U+F000 .. U+F8FF */
  166. uint8_t skip[0x110000/8]; /* code points to skip on load, see sfn_rangesample() and sfn_skipadd() */
  167. int numfrags; /* private, used by sfn_save() and sfn_glyph() */
  168. sfnfrag_t *frags;
  169. int numkpos;
  170. sfnkpos_t *kpos;
  171. long int total;
  172. uint16_t *p;
  173. int np, ap, mx, my, lx, ly;
  174. } sfnctx_t;
  175. /**
  176. * The global font context
  177. */
  178. extern sfnctx_t ctx;
  179. /**
  180. * Progress bar callback
  181. */
  182. extern sfnprogressbar_t pbar;
  183. /*** arguments ***/
  184. extern int rs, re, replace, skipundef, skipcode, hinting, adv, relul, rasterize, dump, origwh, px, py;
  185. extern int unicode, lastuni, quiet, dorounderr, monosize, advrecalc, propo;
  186. /*** unicode.c ***/
  187. #include "unicode.h"
  188. /*** util.c ***/
  189. #include "util.h"
  190. /*** sfn.c ***/
  191. void sfn_init(sfnprogressbar_t pb);
  192. void sfn_setfamilytype(int t);
  193. void sfn_setstr(char **s, char *n, int len);
  194. void sfn_chardel(int unicode);
  195. int sfn_charadd(int unicode, int w, int h, int ax, int ay, int ox);
  196. sfnlayer_t *sfn_layeradd(int unicode, int t, int x, int y, int w, int h, int c, unsigned char *data);
  197. void sfn_layerdel(int unicode, int idx);
  198. int sfn_contadd(sfnlayer_t *lyr, int t, int px, int py, int c1x, int c1y, int c2x, int c2y);
  199. int sfn_kernadd(int unicode, int next, int x, int y);
  200. void sfn_hintgen(int unicode);
  201. unsigned char sfn_cpaladd(int r, int g, int b, int a);
  202. void sfn_skipadd(int unicode);
  203. void sfn_skipdel(int unicode);
  204. void sfn_rangesample(char *filename);
  205. int sfn_load(char *filename, int dump);
  206. int sfn_save(char *filename, int ascii, int compress);
  207. void sfn_sanitize(int unicode);
  208. int sfn_glyph(int size, int unicode, int layer, int postproc, sfngc_t *g);
  209. void sfn_rasterize(int size);
  210. void sfn_vectorize();
  211. void sfn_lines();
  212. void sfn_coverage();
  213. void sfn_free();