ssfn.h 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  2. * ssfn.h
  3. * https://gitlab.com/bztsrc/scalable-font2
  4. *
  5. * Copyright (C) 2020 - 2022 bzt
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies
  12. * of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. *
  27. * @brief Scalable Screen Font renderers
  28. *
  29. */
  30. #ifndef _SSFN_H_
  31. #define _SSFN_H_
  32. #define SSFN_VERSION 0x0200
  33. #ifdef __cplusplus
  34. extern "C" {
  35. # ifndef __THROW
  36. # define __THROW throw()
  37. # endif
  38. #else
  39. # ifndef __THROW
  40. # define __THROW
  41. # endif
  42. #endif
  43. /* if stdint.h was not included before us */
  44. #ifndef _STDINT_H
  45. typedef unsigned char uint8_t;
  46. typedef unsigned short int uint16_t;
  47. typedef short int int16_t;
  48. typedef unsigned int uint32_t;
  49. #ifndef _UINT64_T
  50. typedef unsigned long int uint64_t;
  51. #endif
  52. #endif
  53. /***** file format *****/
  54. /* magic bytes */
  55. #define SSFN_MAGIC "SFN2"
  56. #define SSFN_COLLECTION "SFNC"
  57. #define SSFN_ENDMAGIC "2NFS"
  58. /* ligatures area */
  59. #define SSFN_LIG_FIRST 0xF000
  60. #define SSFN_LIG_LAST 0xF8FF
  61. /* font family group in font type byte */
  62. #define SSFN_TYPE_FAMILY(x) ((x)&15)
  63. #define SSFN_FAMILY_SERIF 0
  64. #define SSFN_FAMILY_SANS 1
  65. #define SSFN_FAMILY_DECOR 2
  66. #define SSFN_FAMILY_MONOSPACE 3
  67. #define SSFN_FAMILY_HAND 4
  68. /* font style flags in font type byte */
  69. #define SSFN_TYPE_STYLE(x) (((x)>>4)&15)
  70. #define SSFN_STYLE_REGULAR 0
  71. #define SSFN_STYLE_BOLD 1
  72. #define SSFN_STYLE_ITALIC 2
  73. #define SSFN_STYLE_USRDEF1 4 /* user defined variant 1 */
  74. #define SSFN_STYLE_USRDEF2 8 /* user defined variant 2 */
  75. /* contour commands */
  76. #define SSFN_CONTOUR_MOVE 0
  77. #define SSFN_CONTOUR_LINE 1
  78. #define SSFN_CONTOUR_QUAD 2
  79. #define SSFN_CONTOUR_CUBIC 3
  80. /* glyph fragments, kerning groups and hinting grid info */
  81. #define SSFN_FRAG_CONTOUR 0
  82. #define SSFN_FRAG_BITMAP 1
  83. #define SSFN_FRAG_PIXMAP 2
  84. #define SSFN_FRAG_KERNING 3
  85. #define SSFN_FRAG_HINTING 4
  86. /* main SSFN header, 32 bytes */
  87. #ifndef _MSC_VER
  88. #define _pack __attribute__((packed))
  89. #else
  90. #define _pack
  91. #pragma pack(push)
  92. #pragma pack(1)
  93. #endif
  94. typedef struct {
  95. uint8_t magic[4]; /* SSFN magic bytes */
  96. uint32_t size; /* total size in bytes */
  97. uint8_t type; /* font family and style */
  98. uint8_t features; /* format features and revision */
  99. uint8_t width; /* overall width of the font */
  100. uint8_t height; /* overall height of the font */
  101. uint8_t baseline; /* horizontal baseline in grid pixels */
  102. uint8_t underline; /* position of under line in grid pixels */
  103. uint16_t fragments_offs; /* offset of fragments table */
  104. uint32_t characters_offs; /* characters table offset */
  105. uint32_t ligature_offs; /* ligatures table offset */
  106. uint32_t kerning_offs; /* kerning table offset */
  107. uint32_t cmap_offs; /* color map offset */
  108. } _pack ssfn_font_t;
  109. #ifdef _MSC_VER
  110. #pragma pack(pop)
  111. #endif
  112. /***** renderer API *****/
  113. #define SSFN_FAMILY_ANY 0xff /* select the first loaded font */
  114. #define SSFN_FAMILY_BYNAME 0xfe /* select font by its unique name */
  115. /* additional styles not stored in fonts */
  116. #define SSFN_STYLE_UNDERLINE 16 /* under line glyph */
  117. #define SSFN_STYLE_STHROUGH 32 /* strike through glyph */
  118. #define SSFN_STYLE_NOAA 64 /* no anti-aliasing */
  119. #define SSFN_STYLE_NOKERN 128 /* no kerning */
  120. #define SSFN_STYLE_NODEFGLYPH 256 /* don't draw default glyph */
  121. #define SSFN_STYLE_NOCACHE 512 /* don't cache rasterized glyph */
  122. #define SSFN_STYLE_NOHINTING 1024 /* no auto hinting grid (not used as of now) */
  123. #define SSFN_STYLE_RTL 2048 /* render right-to-left */
  124. #define SSFN_STYLE_ABS_SIZE 4096 /* scale absoulte height */
  125. #define SSFN_STYLE_NOSMOOTH 8192 /* no edge-smoothing for bitmaps */
  126. #define SSFN_STYLE_A 16384 /* keep original alpha channel */
  127. /* error codes */
  128. #define SSFN_OK 0 /* success */
  129. #define SSFN_ERR_ALLOC -1 /* allocation error */
  130. #define SSFN_ERR_BADFILE -2 /* bad SSFN file format */
  131. #define SSFN_ERR_NOFACE -3 /* no font face selected */
  132. #define SSFN_ERR_INVINP -4 /* invalid input */
  133. #define SSFN_ERR_BADSTYLE -5 /* bad style */
  134. #define SSFN_ERR_BADSIZE -6 /* bad size */
  135. #define SSFN_ERR_NOGLYPH -7 /* glyph (or kerning info) not found */
  136. #define SSFN_SIZE_MAX 192 /* biggest size we can render */
  137. #define SSFN_ITALIC_DIV 4 /* italic angle divisor, glyph top side pushed width / this pixels */
  138. #define SSFN_PREC 4 /* precision in bits */
  139. /* destination frame buffer context */
  140. typedef struct {
  141. uint8_t *ptr; /* pointer to the buffer */
  142. int w; /* width (positive: ARGB, negative: ABGR pixels) */
  143. int h; /* height */
  144. uint16_t p; /* pitch, bytes per line */
  145. int x; /* cursor x */
  146. int y; /* cursor y */
  147. uint32_t fg; /* foreground color */
  148. uint32_t bg; /* background color */
  149. } ssfn_buf_t;
  150. /* cached bitmap struct */
  151. #define SSFN_DATA_MAX 65536
  152. typedef struct {
  153. uint16_t p; /* data buffer pitch, bytes per line */
  154. uint8_t h; /* data buffer height */
  155. uint8_t o; /* overlap of glyph, scaled to size */
  156. uint8_t x; /* advance x, scaled to size */
  157. uint8_t y; /* advance y, scaled to size */
  158. uint8_t a; /* ascender, scaled to size */
  159. uint8_t d; /* descender, scaled to size */
  160. uint8_t data[SSFN_DATA_MAX]; /* data buffer */
  161. } ssfn_glyph_t;
  162. /* character metrics */
  163. typedef struct {
  164. uint8_t t; /* type and overlap */
  165. uint8_t n; /* number of fragments */
  166. uint8_t w; /* width */
  167. uint8_t h; /* height */
  168. uint8_t x; /* advance x */
  169. uint8_t y; /* advance y */
  170. } ssfn_chr_t;
  171. #ifdef SSFN_PROFILING
  172. #include <string.h>
  173. #include <sys/time.h>
  174. #endif
  175. /* renderer context */
  176. typedef struct {
  177. #ifdef SSFN_MAXLINES
  178. const ssfn_font_t *fnt[5][16]; /* static font registry */
  179. #else
  180. const ssfn_font_t **fnt[5]; /* dynamic font registry */
  181. #endif
  182. const ssfn_font_t *s; /* explicitly selected font */
  183. const ssfn_font_t *f; /* font selected by best match */
  184. ssfn_glyph_t ga; /* glyph sketch area */
  185. ssfn_glyph_t *g; /* current glyph pointer */
  186. #ifdef SSFN_MAXLINES
  187. uint16_t p[SSFN_MAXLINES*2];
  188. #else
  189. ssfn_glyph_t ***c[17]; /* glyph cache */
  190. uint16_t *p;
  191. char **bufs; /* allocated extra buffers */
  192. #endif
  193. ssfn_chr_t *rc; /* pointer to current character */
  194. int numbuf, lenbuf, np, ap, ox, oy, ax;
  195. int mx, my, lx, ly; /* move to coordinates, last coordinates */
  196. int len[5]; /* number of fonts in registry */
  197. int family; /* required family */
  198. int style; /* required style */
  199. int size; /* required size */
  200. int line; /* calculate line height */
  201. #ifdef SSFN_PROFILING
  202. uint64_t lookup, raster, blit, kern;/* profiling accumulators */
  203. #endif
  204. } ssfn_t;
  205. /***** API function protoypes *****/
  206. uint32_t ssfn_utf8(char **str); /* decode UTF-8 sequence */
  207. /* normal renderer */
  208. int ssfn_load(ssfn_t *ctx, const void *data); /* add an SSFN to context */
  209. int ssfn_select(ssfn_t *ctx, int family, const char *name, int style, int size); /* select font to use */
  210. int ssfn_render(ssfn_t *ctx, ssfn_buf_t *dst, const char *str); /* render a glyph to a pixel buffer */
  211. int ssfn_bbox(ssfn_t *ctx, const char *str, int *w, int *h, int *left, int *top); /* get bounding box */
  212. ssfn_buf_t *ssfn_text(ssfn_t *ctx, const char *str, unsigned int fg); /* renders text to a newly allocated buffer */
  213. int ssfn_mem(ssfn_t *ctx); /* return how much memory is used */
  214. void ssfn_free(ssfn_t *ctx); /* free context */
  215. #define ssfn_error(err) (err<0&&err>=-7?ssfn_errstr[-err]:"Unknown error") /* return string for error code */
  216. extern const char *ssfn_errstr[];
  217. /* simple renderer */
  218. extern ssfn_font_t *ssfn_src; /* font buffer */
  219. extern ssfn_buf_t ssfn_dst; /* destination frame buffer */
  220. int ssfn_putc(uint32_t unicode); /* render console bitmap font */
  221. /***** renderer implementations *****/
  222. /*** these go for both renderers ***/
  223. #if (defined(SSFN_IMPLEMENTATION) || defined(SSFN_CONSOLEBITMAP_PALETTE) || \
  224. defined(SSFN_CONSOLEBITMAP_HICOLOR) || defined(SSFN_CONSOLEBITMAP_TRUECOLOR)) && !defined(SSFN_COMMON)
  225. #define SSFN_COMMON
  226. /**
  227. * Error code strings
  228. */
  229. const char *ssfn_errstr[] = { "",
  230. "Memory allocation error",
  231. "Bad file format",
  232. "No font face found",
  233. "Invalid input value",
  234. "Invalid style",
  235. "Invalid size",
  236. "Glyph not found"
  237. };
  238. /**
  239. * Decode an UTF-8 multibyte, advance string pointer and return UNICODE. Watch out, no input checks
  240. *
  241. * @param **s pointer to an UTF-8 string pointer
  242. * @return unicode, and *s moved to next multibyte sequence
  243. */
  244. uint32_t ssfn_utf8(char **s)
  245. {
  246. uint32_t c = **s;
  247. if((**s & 128) != 0) {
  248. if(!(**s & 32)) { c = ((**s & 0x1F)<<6)|(*(*s+1) & 0x3F); *s += 1; } else
  249. if(!(**s & 16)) { c = ((**s & 0xF)<<12)|((*(*s+1) & 0x3F)<<6)|(*(*s+2) & 0x3F); *s += 2; } else
  250. if(!(**s & 8)) { c = ((**s & 0x7)<<18)|((*(*s+1) & 0x3F)<<12)|((*(*s+2) & 0x3F)<<6)|(*(*s+3) & 0x3F); *s += 3; }
  251. else c = 0;
  252. }
  253. (*s)++;
  254. return c;
  255. }
  256. #endif
  257. #ifdef SSFN_IMPLEMENTATION
  258. /*** normal renderer (ca. 28k, fully featured with error checking) ***/
  259. # ifndef NULL
  260. # define NULL (void*)0
  261. # endif
  262. # ifndef size_t
  263. typedef __SIZE_TYPE__ size_t;
  264. # endif
  265. # ifndef inline
  266. # define inline __inline__
  267. # endif
  268. #ifndef _STRING_H_
  269. extern int memcmp (const void *__s1, const void *__s2, size_t __n) __THROW;
  270. extern void *memset (void *__s, int __c, size_t __n) __THROW;
  271. #endif
  272. /* Clang does not have built-ins */
  273. # ifndef SSFN_memcmp
  274. # ifdef __builtin_memcmp
  275. # define SSFN_memcmp __builtin_memcmp
  276. # else
  277. # ifndef SSFN_MAXLINES
  278. # define SSFN_memcmp memcmp
  279. # else
  280. static int SSFN_memcmp(const void *__s1, const void *__s2, size_t __n)
  281. { unsigned char *a = (unsigned char *)__s1, *b = (unsigned char *)__s2;
  282. if(__n > 0) { while(__n-- > 0) { if(*a != *b) { return *a - *b; } a++; b++; } } return 0; }
  283. # endif
  284. # endif
  285. # endif
  286. # ifndef SSFN_memset
  287. # ifdef __builtin_memset
  288. # define SSFN_memset __builtin_memset
  289. # else
  290. # ifndef SSFN_MAXLINES
  291. # define SSFN_memset memset
  292. # else
  293. static void *SSFN_memset(void *__s, int __c, size_t __n)
  294. { unsigned char *a = __s; if(__n > 0) { while(__n-- > 0) *a++ = __c; } return __s; }
  295. # endif
  296. # endif
  297. # endif
  298. # ifndef SSFN_MAXLINES
  299. # ifndef SSFN_realloc
  300. # ifdef __builtin_realloc
  301. # define SSFN_realloc __builtin_realloc
  302. # else
  303. # define SSFN_realloc realloc
  304. extern void *realloc (void *__ptr, size_t __size) __THROW;
  305. # endif
  306. # endif
  307. # ifndef SSFN_free
  308. # ifdef __builtin_free
  309. # define SSFN_free __builtin_free
  310. # else
  311. # define SSFN_free free
  312. extern void free (void *p) __THROW;
  313. # endif
  314. # endif
  315. # endif /* if !SSFN_MAXLINES */
  316. /*** Private functions ***/
  317. /* parse character table */
  318. static uint8_t *_ssfn_c(const ssfn_font_t *font, const char *str, int *len, uint32_t *unicode)
  319. {
  320. uint32_t i, j, u = -1U;
  321. uint16_t *l;
  322. uint8_t *ptr, *s;
  323. *len = 0; *unicode = 0;
  324. if(!font || !font->characters_offs || !str || !*str) return NULL;
  325. if(font->ligature_offs) {
  326. for(l = (uint16_t*)((uint8_t*)font + font->ligature_offs), i = 0; l[i] && u == -1U; i++) {
  327. for(ptr = (uint8_t*)font + l[i], s = (uint8_t*)str; *ptr && *ptr == *s; ptr++, s++);
  328. if(!*ptr) { u = SSFN_LIG_FIRST + i; break; }
  329. }
  330. }
  331. if(u == -1U) {
  332. /* inline ssfn_utf8 to workaround -O2 bug in gcc 11.1 */
  333. s = (uint8_t*)str; u = *s;
  334. if((*s & 128) != 0) {
  335. if(!(*s & 32)) { u = ((*s & 0x1F)<<6)|(*(s+1) & 0x3F); s++; } else
  336. if(!(*s & 16)) { u = ((*s & 0xF)<<12)|((*(s+1) & 0x3F)<<6)|(*(s+2) & 0x3F); s += 2; } else
  337. if(!(*s & 8)) { u = ((*s & 0x7)<<18)|((*(s+1) & 0x3F)<<12)|((*(s+2) & 0x3F)<<6)|(*(s+3) & 0x3F); s += 3; }
  338. else u = 0;
  339. }
  340. s++;
  341. }
  342. *len = (int)(s - (uint8_t*)str);
  343. *unicode = u;
  344. for(ptr = (uint8_t*)font + font->characters_offs, i = 0; i < 0x110000; i++) {
  345. if(ptr[0] == 0xFF) { i += 65535; ptr++; }
  346. else if((ptr[0] & 0xC0) == 0xC0) { j = (((ptr[0] & 0x3F) << 8) | ptr[1]); i += j; ptr += 2; }
  347. else if((ptr[0] & 0xC0) == 0x80) { j = (ptr[0] & 0x3F); i += j; ptr++; }
  348. else {
  349. if(i == u) return ptr;
  350. ptr += 6 + ptr[1] * (ptr[0] & 0x40 ? 6 : 5);
  351. }
  352. }
  353. return NULL;
  354. }
  355. /* add a line to contour */
  356. static void _ssfn_l(ssfn_t *ctx, int p, int h, int x, int y)
  357. {
  358. if(x < 0 || y < 0 || x >= p || y >= h || (
  359. ((ctx->lx + (1 << (SSFN_PREC-1))) >> SSFN_PREC) == ((x + (1 << (SSFN_PREC-1))) >> SSFN_PREC) &&
  360. ((ctx->ly + (1 << (SSFN_PREC-1))) >> SSFN_PREC) == ((y + (1 << (SSFN_PREC-1))) >> SSFN_PREC))) return;
  361. #ifdef SSFN_MAXLINES
  362. if(ctx->np >= SSFN_MAXLINES*2-2) return;
  363. #else
  364. if(ctx->ap <= ctx->np) {
  365. ctx->ap = ctx->np + 512;
  366. ctx->p = (uint16_t*)SSFN_realloc(ctx->p, ctx->ap * sizeof(uint16_t));
  367. if(!ctx->p) { ctx->ap = ctx->np = 0; return; }
  368. }
  369. #endif
  370. if(!ctx->np) {
  371. ctx->p[0] = ctx->mx;
  372. ctx->p[1] = ctx->my;
  373. ctx->np += 2;
  374. }
  375. ctx->p[ctx->np+0] = x;
  376. ctx->p[ctx->np+1] = y;
  377. ctx->np += 2;
  378. ctx->lx = x; ctx->ly = y;
  379. }
  380. /* add a Bezier curve to contour */
  381. static void _ssfn_b(ssfn_t *ctx, int p,int h, int x0,int y0, int x1,int y1, int x2,int y2, int x3,int y3, int l)
  382. {
  383. int m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, m4x, m4y,m5x, m5y;
  384. if(l<4 && (x0!=x3 || y0!=y3)) {
  385. m0x = ((x1-x0)/2) + x0; m0y = ((y1-y0)/2) + y0;
  386. m1x = ((x2-x1)/2) + x1; m1y = ((y2-y1)/2) + y1;
  387. m2x = ((x3-x2)/2) + x2; m2y = ((y3-y2)/2) + y2;
  388. m3x = ((m1x-m0x)/2) + m0x; m3y = ((m1y-m0y)/2) + m0y;
  389. m4x = ((m2x-m1x)/2) + m1x; m4y = ((m2y-m1y)/2) + m1y;
  390. m5x = ((m4x-m3x)/2) + m3x; m5y = ((m4y-m3y)/2) + m3y;
  391. _ssfn_b(ctx, p,h, x0,y0, m0x,m0y, m3x,m3y, m5x,m5y, l+1);
  392. _ssfn_b(ctx, p,h, m5x,m5y, m4x,m4y, m2x,m2y, x3,y3, l+1);
  393. }
  394. if(l) _ssfn_l(ctx, p,h, x3, y3);
  395. }
  396. #ifndef SSFN_MAXLINES
  397. static void _ssfn_fc(ssfn_t *ctx)
  398. {
  399. int i, j, k;
  400. if(!ctx) return;
  401. for(k = 0; k <= 16; k++)
  402. if(ctx->c[k]) {
  403. for(j = 0; j < 256; j++)
  404. if(ctx->c[k][j]) {
  405. for(i = 0; i < 256; i++)
  406. if(ctx->c[k][j][i]) SSFN_free(ctx->c[k][j][i]);
  407. SSFN_free(ctx->c[k][j]);
  408. }
  409. SSFN_free(ctx->c[k]);
  410. ctx->c[k] = NULL;
  411. }
  412. }
  413. /*
  414. * gzip deflate uncompressor from stb_image.h with minor modifications to reduce dependency
  415. * stb_image - v2.23 - public domain image loader - http://nothings.org/stb_image.h
  416. */
  417. #define SSFN__ZFAST_BITS 9
  418. #define SSFN__ZFAST_MASK ((1 << SSFN__ZFAST_BITS) - 1)
  419. typedef struct
  420. {
  421. uint16_t fast[1 << SSFN__ZFAST_BITS];
  422. uint16_t firstcode[16];
  423. int maxcode[17];
  424. uint16_t firstsymbol[16];
  425. unsigned char size[288];
  426. uint16_t value[288];
  427. } _ssfn__zhuffman;
  428. inline static int _ssfn__bitreverse16(int n)
  429. {
  430. n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
  431. n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
  432. n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
  433. n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
  434. return n;
  435. }
  436. inline static int _ssfn__bit_reverse(int v, int bits)
  437. {
  438. return _ssfn__bitreverse16(v) >> (16-bits);
  439. }
  440. static int _ssfn__zbuild_huffman(_ssfn__zhuffman *z, unsigned char *sizelist, int num)
  441. {
  442. int i,k=0;
  443. int code, next_code[16], sizes[17];
  444. SSFN_memset(sizes, 0, sizeof(sizes));
  445. SSFN_memset(z->fast, 0, sizeof(z->fast));
  446. for (i=0; i < num; ++i)
  447. ++sizes[sizelist[i]];
  448. sizes[0] = 0;
  449. for (i=1; i < 16; ++i)
  450. if (sizes[i] > (1 << i))
  451. return 0;
  452. code = 0;
  453. for (i=1; i < 16; ++i) {
  454. next_code[i] = code;
  455. z->firstcode[i] = (uint16_t) code;
  456. z->firstsymbol[i] = (uint16_t) k;
  457. code = (code + sizes[i]);
  458. if (sizes[i])
  459. if (code-1 >= (1 << i)) return 0;
  460. z->maxcode[i] = code << (16-i);
  461. code <<= 1;
  462. k += sizes[i];
  463. }
  464. z->maxcode[16] = 0x10000;
  465. for (i=0; i < num; ++i) {
  466. int s = sizelist[i];
  467. if (s) {
  468. int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  469. uint16_t fastv = (uint16_t) ((s << 9) | i);
  470. z->size [c] = (unsigned char) s;
  471. z->value[c] = (uint16_t) i;
  472. if (s <= SSFN__ZFAST_BITS) {
  473. int j = _ssfn__bit_reverse(next_code[s],s);
  474. while (j < (1 << SSFN__ZFAST_BITS)) {
  475. z->fast[j] = fastv;
  476. j += (1 << s);
  477. }
  478. }
  479. ++next_code[s];
  480. }
  481. }
  482. return 1;
  483. }
  484. typedef struct
  485. {
  486. unsigned char *zbuffer;
  487. int num_bits;
  488. uint32_t code_buffer;
  489. char *zout;
  490. char *zout_start;
  491. char *zout_end;
  492. _ssfn__zhuffman z_length, z_distance;
  493. } _ssfn__zbuf;
  494. inline static unsigned char _ssfn__zget8(_ssfn__zbuf *z)
  495. {
  496. return *z->zbuffer++;
  497. }
  498. static void _ssfn__fill_bits(_ssfn__zbuf *z)
  499. {
  500. do {
  501. z->code_buffer |= (unsigned int) _ssfn__zget8(z) << z->num_bits;
  502. z->num_bits += 8;
  503. } while (z->num_bits <= 24);
  504. }
  505. inline static unsigned int _ssfn__zreceive(_ssfn__zbuf *z, int n)
  506. {
  507. unsigned int k;
  508. if (z->num_bits < n) _ssfn__fill_bits(z);
  509. k = z->code_buffer & ((1 << n) - 1);
  510. z->code_buffer >>= n;
  511. z->num_bits -= n;
  512. return k;
  513. }
  514. static int _ssfn__zhuffman_decode_slowpath(_ssfn__zbuf *a, _ssfn__zhuffman *z)
  515. {
  516. int b,s,k;
  517. k = _ssfn__bit_reverse(a->code_buffer, 16);
  518. for (s=SSFN__ZFAST_BITS+1; ; ++s)
  519. if (k < z->maxcode[s])
  520. break;
  521. if (s == 16) return -1;
  522. b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  523. a->code_buffer >>= s;
  524. a->num_bits -= s;
  525. return z->value[b];
  526. }
  527. inline static int _ssfn__zhuffman_decode(_ssfn__zbuf *a, _ssfn__zhuffman *z)
  528. {
  529. int b,s;
  530. if (a->num_bits < 16) _ssfn__fill_bits(a);
  531. b = z->fast[a->code_buffer & SSFN__ZFAST_MASK];
  532. if (b) {
  533. s = b >> 9;
  534. a->code_buffer >>= s;
  535. a->num_bits -= s;
  536. return b & 511;
  537. }
  538. return _ssfn__zhuffman_decode_slowpath(a, z);
  539. }
  540. static int _ssfn__zexpand(_ssfn__zbuf *z, char *zout)
  541. {
  542. char *q;
  543. unsigned int cur, limit;
  544. #ifdef __GNUC__
  545. #pragma GCC diagnostic push
  546. #pragma GCC diagnostic ignored "-Wuse-after-free"
  547. #endif
  548. z->zout = zout; cur = (unsigned int) (z->zout - z->zout_start); limit = (unsigned int) (z->zout_end - z->zout_start);
  549. if(limit == 8) { if(z->zout_start[0] != 'S' || z->zout_start[1] != 'F' || z->zout_start[2] != 'N') return 0; limit = *((uint32_t*)&z->zout_start[4]); } else return 0;
  550. q = (char *) SSFN_realloc(z->zout_start, limit);
  551. if (q == NULL) return 0;
  552. z->zout_start = q; z->zout = q + cur; z->zout_end = q + limit;
  553. #ifdef __GNUC__
  554. #pragma GCC diagnostic pop
  555. #endif
  556. return 1;
  557. }
  558. static int _ssfn__zlength_base[31]={3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0};
  559. static int _ssfn__zlength_extra[31]={0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0};
  560. static int _ssfn__zdist_base[32]={1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  561. static int _ssfn__zdist_extra[32]={0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  562. static int _ssfn__parse_huffman_block(_ssfn__zbuf *a)
  563. {
  564. char *zout = a->zout;
  565. for(;;) {
  566. int z = _ssfn__zhuffman_decode(a, &a->z_length);
  567. if (z < 256) {
  568. if (z < 0) return 0;
  569. if (zout >= a->zout_end) { if (!_ssfn__zexpand(a, zout)) return 0; zout = a->zout; }
  570. *zout++ = (char) z;
  571. } else {
  572. unsigned char *p;
  573. int len,dist;
  574. if (z == 256) { a->zout = zout; return 1; }
  575. z -= 257;
  576. len = _ssfn__zlength_base[z];
  577. if (_ssfn__zlength_extra[z]) len += _ssfn__zreceive(a, _ssfn__zlength_extra[z]);
  578. z = _ssfn__zhuffman_decode(a, &a->z_distance);
  579. if (z < 0) return 0;
  580. dist = _ssfn__zdist_base[z];
  581. if (_ssfn__zdist_extra[z]) dist += _ssfn__zreceive(a, _ssfn__zdist_extra[z]);
  582. if (zout - a->zout_start < dist) return 0;
  583. if (zout + len > a->zout_end) {
  584. if (!_ssfn__zexpand(a, zout)) return 0;
  585. zout = a->zout;
  586. }
  587. p = (unsigned char *) (zout - dist);
  588. if (dist == 1) {unsigned char v = *p;if (len) { do *zout++ = v; while (--len); }
  589. } else { if (len) { do *zout++ = *p++; while (--len); } }
  590. }
  591. }
  592. }
  593. static int _ssfn__compute_huffman_codes(_ssfn__zbuf *a)
  594. {
  595. static unsigned char length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  596. _ssfn__zhuffman z_codelength;
  597. unsigned char lencodes[286+32+137];
  598. unsigned char codelength_sizes[19];
  599. int i,n;
  600. int hlit = _ssfn__zreceive(a,5) + 257;
  601. int hdist = _ssfn__zreceive(a,5) + 1;
  602. int hclen = _ssfn__zreceive(a,4) + 4;
  603. int ntot = hlit + hdist;
  604. SSFN_memset(codelength_sizes, 0, sizeof(codelength_sizes));
  605. for (i=0; i < hclen; ++i) {
  606. int s = _ssfn__zreceive(a,3);
  607. codelength_sizes[length_dezigzag[i]] = (unsigned char) s;
  608. }
  609. if (!_ssfn__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  610. n = 0;
  611. while (n < ntot) {
  612. int c = _ssfn__zhuffman_decode(a, &z_codelength);
  613. if (c < 0 || c >= 19) return 0;
  614. if (c < 16)
  615. lencodes[n++] = (unsigned char) c;
  616. else {
  617. unsigned char fill = 0;
  618. if (c == 16) {
  619. c = _ssfn__zreceive(a,2)+3;
  620. if (n == 0) return 0;
  621. fill = lencodes[n-1];
  622. } else
  623. if (c == 17) c = _ssfn__zreceive(a,3)+3;
  624. else { c = _ssfn__zreceive(a,7)+11; }
  625. if (ntot - n < c) return 0;
  626. SSFN_memset(lencodes+n, fill, c);
  627. n += c;
  628. }
  629. }
  630. if (n != ntot) return 0;
  631. if (!_ssfn__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  632. if (!_ssfn__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  633. return 1;
  634. }
  635. inline static int _ssfn__parse_uncompressed_block(_ssfn__zbuf *a)
  636. {
  637. unsigned char header[4];
  638. int len,nlen,k;
  639. if (a->num_bits & 7)
  640. _ssfn__zreceive(a, a->num_bits & 7);
  641. k = 0;
  642. while (a->num_bits > 0) {
  643. header[k++] = (unsigned char) (a->code_buffer & 255);
  644. a->code_buffer >>= 8;
  645. a->num_bits -= 8;
  646. }
  647. while (k < 4)
  648. header[k++] = _ssfn__zget8(a);
  649. len = header[1] * 256 + header[0];
  650. nlen = header[3] * 256 + header[2];
  651. if (nlen != (len ^ 0xffff)) return 0;
  652. if (a->zout + len > a->zout_end)
  653. if (!_ssfn__zexpand(a, a->zout)) return 0;
  654. for(k = 0; k < len; k++)
  655. a->zout[k] = a->zbuffer[k];
  656. a->zbuffer += len;
  657. a->zout += len;
  658. return 1;
  659. }
  660. static unsigned char _ssfn__zdefault_length[288], _ssfn__zdefault_distance[32];
  661. static void _ssfn__init_zdefaults(void)
  662. {
  663. int i;
  664. for (i=0; i <= 143; ++i) _ssfn__zdefault_length[i] = 8;
  665. for ( ; i <= 255; ++i) _ssfn__zdefault_length[i] = 9;
  666. for ( ; i <= 279; ++i) _ssfn__zdefault_length[i] = 7;
  667. for ( ; i <= 287; ++i) _ssfn__zdefault_length[i] = 8;
  668. for (i=0; i <= 31; ++i) _ssfn__zdefault_distance[i] = 5;
  669. }
  670. static int _ssfn__parse_zlib(_ssfn__zbuf *a)
  671. {
  672. int fin, type;
  673. a->num_bits = 0;
  674. a->code_buffer = 0;
  675. do {
  676. fin = _ssfn__zreceive(a,1);
  677. type = _ssfn__zreceive(a,2);
  678. if (type == 0) { if (!_ssfn__parse_uncompressed_block(a)) return 0; }
  679. else if (type == 3) { return 0; }
  680. else {
  681. if (type == 1) {
  682. if (!_ssfn__zbuild_huffman(&a->z_length , _ssfn__zdefault_length , 288)) return 0;
  683. if (!_ssfn__zbuild_huffman(&a->z_distance, _ssfn__zdefault_distance, 32)) return 0;
  684. } else {
  685. if (!_ssfn__compute_huffman_codes(a)) return 0;
  686. }
  687. if (!_ssfn__parse_huffman_block(a)) return 0;
  688. }
  689. } while (!fin);
  690. return 1;
  691. }
  692. char *_ssfn_zlib_decode(const char *buffer)
  693. {
  694. _ssfn__zbuf a;
  695. char *p = (char *) SSFN_realloc(NULL, 8);
  696. if (p == NULL) return NULL;
  697. a.zbuffer = (unsigned char *) buffer;
  698. a.zout_start = p; a.zout = p; a.zout_end = p + 8;
  699. _ssfn__init_zdefaults();
  700. if (_ssfn__parse_zlib(&a)) { return a.zout_start; } else { SSFN_free(a.zout_start); return NULL; }
  701. }
  702. #endif /* if !SSFN_MAXLINES */
  703. /*** Public API implementation ***/
  704. /**
  705. * Load a font or font collection into renderer context
  706. *
  707. * @param ctx rendering context
  708. * @param font SSFN font or font collection in memory
  709. * @return error code
  710. */
  711. int ssfn_load(ssfn_t *ctx, const void *data)
  712. {
  713. const ssfn_font_t *font = (const ssfn_font_t *)data;
  714. ssfn_font_t *fnt, *end;
  715. int family;
  716. #ifndef SSFN_MAXLINES
  717. uint8_t c, r, *ptr = (uint8_t *)font;
  718. #endif
  719. if(!ctx || !font)
  720. return SSFN_ERR_INVINP;
  721. if(((uint8_t *)font)[0] == 0x1f && ((uint8_t *)font)[1] == 0x8b) {
  722. #ifdef SSFN_MAXLINES
  723. return SSFN_ERR_BADFILE;
  724. #else
  725. ptr += 2;
  726. if(*ptr++ != 8) return SSFN_ERR_BADFILE;
  727. c = *ptr++; ptr += 6;
  728. if(c & 4) { r = *ptr++; r += (*ptr++ << 8); ptr += r; }
  729. if(c & 8) { while(*ptr++ != 0); }
  730. if(c & 16) { while(*ptr++ != 0); }
  731. font = (ssfn_font_t*)_ssfn_zlib_decode((const char*)ptr);
  732. if(!font) return SSFN_ERR_BADFILE;
  733. ctx->bufs = (char**)SSFN_realloc(ctx->bufs, (ctx->numbuf + 1) * sizeof(char*));
  734. if(!ctx->bufs) { ctx->numbuf = 0; return SSFN_ERR_ALLOC; }
  735. ctx->bufs[ctx->numbuf++] = (char*)font;
  736. ctx->lenbuf += font->size;
  737. #endif
  738. }
  739. if(!SSFN_memcmp(font->magic, SSFN_COLLECTION, 4)) {
  740. end = (ssfn_font_t*)((uint8_t*)font + font->size);
  741. for(fnt = (ssfn_font_t*)((uint8_t*)font + 8); fnt < end && !ssfn_load(ctx, (const void *)fnt);
  742. fnt = (ssfn_font_t*)((uint8_t*)fnt + fnt->size));
  743. } else {
  744. family = SSFN_TYPE_FAMILY(font->type);
  745. if(SSFN_memcmp(font->magic, SSFN_MAGIC, 4) || SSFN_memcmp((uint8_t*)font + font->size - 4, SSFN_ENDMAGIC, 4) ||
  746. family > SSFN_FAMILY_HAND || font->fragments_offs >= font->size || font->characters_offs >= font->size ||
  747. font->ligature_offs >= font->size || font->kerning_offs >= font->size || font->cmap_offs >= font->size ||
  748. font->fragments_offs >= font->characters_offs) {
  749. return SSFN_ERR_BADFILE;
  750. } else {
  751. ctx->len[family]++;
  752. #ifdef SSFN_MAXLINES
  753. if(ctx->len[family] > 15) return SSFN_ERR_ALLOC;
  754. #else
  755. ctx->fnt[family] = (const ssfn_font_t**)SSFN_realloc(ctx->fnt[family], ctx->len[family]*sizeof(void*));
  756. if(!ctx->fnt[family]) {
  757. ctx->len[family] = 0;
  758. return SSFN_ERR_ALLOC;
  759. } else
  760. #endif
  761. ctx->fnt[family][ctx->len[family]-1] = font;
  762. }
  763. #ifndef SSFN_MAXLINES
  764. _ssfn_fc(ctx);
  765. #endif
  766. }
  767. return SSFN_OK;
  768. }
  769. /**
  770. * Free renderer context
  771. *
  772. * @param ctx rendering context
  773. */
  774. void ssfn_free(ssfn_t *ctx)
  775. {
  776. #ifndef SSFN_MAXLINES
  777. int i;
  778. #endif
  779. if(!ctx) return;
  780. #ifndef SSFN_MAXLINES
  781. _ssfn_fc(ctx);
  782. if(ctx->bufs) {
  783. for(i = 0; i < ctx->numbuf; i++)
  784. if(ctx->bufs[i]) SSFN_free(ctx->bufs[i]);
  785. SSFN_free(ctx->bufs);
  786. }
  787. for(i = 0; i < 5; i++)
  788. if(ctx->fnt[i]) SSFN_free(ctx->fnt[i]);
  789. if(ctx->p) SSFN_free(ctx->p);
  790. #endif
  791. SSFN_memset(ctx, 0, sizeof(ssfn_t));
  792. }
  793. /**
  794. * Returns how much memory a context consumes
  795. *
  796. * @param ctx rendering context
  797. * @return total memory used by that context in bytes
  798. */
  799. int ssfn_mem(ssfn_t *ctx)
  800. {
  801. #ifdef SSFN_MAXLINES
  802. return ctx ? sizeof(ssfn_t) : 0;
  803. #else
  804. int i, j, k, ret = sizeof(ssfn_t);
  805. if(!ctx) return 0;
  806. for(i = 0; i < 5; i++) ret += ctx->len[i] * sizeof(ssfn_font_t*);
  807. ret += ctx->lenbuf;
  808. for(k = 0; k <= 16; k++) {
  809. if(ctx->c[k]) {
  810. for(j = 0; j < 256; j++)
  811. if(ctx->c[k][j]) {
  812. for(i = 0; i < 256; i++)
  813. if(ctx->c[k][j][i]) ret += 8 + ctx->c[k][j][i]->p * ctx->c[k][j][i]->h;
  814. ret += 256 * sizeof(void*);
  815. }
  816. ret += 256 * sizeof(void*);
  817. }
  818. }
  819. if(ctx->p) ret += ctx->ap * sizeof(uint16_t);
  820. return ret;
  821. #endif
  822. }
  823. /**
  824. * Set up rendering parameters
  825. *
  826. * @param ctx rendering context
  827. * @param family one of SSFN_FAMILY_*
  828. * @param name NULL or UTF-8 string if family is SSFN_FAMILY_BYNAME
  829. * @param style OR'd values of SSFN_STYLE_*
  830. * @param size how big glyph it should render, 8 - 192
  831. * @return error code
  832. */
  833. int ssfn_select(ssfn_t *ctx, int family, const char *name, int style, int size)
  834. {
  835. int i, j, l;
  836. if(!ctx) return SSFN_ERR_INVINP;
  837. #ifndef SSFN_MAXLINES
  838. _ssfn_fc(ctx);
  839. #endif
  840. if((style & ~0x5FFF)) return SSFN_ERR_BADSTYLE;
  841. if(size < 8 || size > SSFN_SIZE_MAX) return SSFN_ERR_BADSIZE;
  842. if(family == SSFN_FAMILY_BYNAME) {
  843. if(!name || !name[0]) return SSFN_ERR_INVINP;
  844. for(l=0; name[l]; l++);
  845. for(i=0; i < 5; i++) {
  846. for(j=0; j < ctx->len[i]; j++) {
  847. if(!SSFN_memcmp(name, (uint8_t*)&ctx->fnt[i][j]->magic + sizeof(ssfn_font_t), l)) {
  848. ctx->s = ctx->fnt[i][j];
  849. goto familyfound;
  850. }
  851. }
  852. }
  853. return SSFN_ERR_NOFACE;
  854. } else {
  855. if(family != SSFN_FAMILY_ANY && (family > SSFN_FAMILY_HAND || !ctx->len[family])) return SSFN_ERR_NOFACE;
  856. ctx->s = NULL;
  857. }
  858. familyfound:
  859. ctx->f = NULL;
  860. ctx->family = family;
  861. ctx->style = style;
  862. ctx->size = size;
  863. ctx->line = 0;
  864. return SSFN_OK;
  865. }
  866. /**
  867. * Render a glyph to a pixel buffer
  868. *
  869. * @param ctx rendering context
  870. * @param dst destination buffer
  871. * @param str pointer to an UTF-8 string
  872. * @return number of bytes parsed in str (zero means end of string) or error code
  873. */
  874. int ssfn_render(ssfn_t *ctx, ssfn_buf_t *dst, const char *str)
  875. {
  876. ssfn_font_t **fl;
  877. uint8_t *ptr = NULL, *frg, *end, *tmp, color, ci = 0, cb = 0, cs, dec[65536];
  878. uint16_t r[640];
  879. uint32_t unicode, P, O, *Op, *Ol;
  880. unsigned long int sR, sG, sB, sA;
  881. int ret = 0, i, j, k, l, p, m, n, o, s, x, y, w, h, H, a, A, b, B, nr, uix, uax;
  882. int ox, oy, y0, y1, Y0, Y1, x0, x1, X0, X1, X2, xs, ys, yp, pc, fB, fG, fR, fA, bB, bG, bR, dB, dG, dR, dA;
  883. #ifdef SSFN_PROFILING
  884. struct timeval tv0, tv1, tvd;
  885. gettimeofday(&tv0, NULL);
  886. #endif
  887. #define PUTPIXEL O = *Ol;bR = (O >> (16 - cs)) & 0xFF; bG = (O >> 8) & 0xFF; bB = (O >> cs) & 0xFF;\
  888. bB += ((fB - bB) * fA) >> 8; bG += ((fG - bG) * fA) >> 8; bR += ((fR - bR) * fA) >> 8;\
  889. *Ol = (ctx->style & SSFN_STYLE_A ? O & 0xFF000000 : ((uint32_t)fA << 24)) | (bR << (16 - cs)) | (bG << 8) | (bB << cs);
  890. if(!ctx || !str) return SSFN_ERR_INVINP;
  891. if(!*str) return 0;
  892. if(*str == '\r') { dst->x = 0; return 1; }
  893. if(*str == '\n') { dst->x = 0; dst->y += ctx->line ? ctx->line : ctx->size; return 1; }
  894. if(ctx->s) {
  895. ctx->f = ctx->s;
  896. ptr = _ssfn_c(ctx->f, str, &ret, &unicode);
  897. } else {
  898. /* find best match */
  899. p = ctx->family;
  900. ctx->f = NULL;
  901. again: if(p >= SSFN_FAMILY_BYNAME) { n = 0; m = 4; } else n = m = p;
  902. for(; n <= m; n++) {
  903. fl = (ssfn_font_t **)ctx->fnt[n];
  904. if(ctx->style & 3) {
  905. /* check if we have a specific ctx->f for the requested style and size */
  906. for(i=0;i<ctx->len[n];i++)
  907. if(((fl[i]->type>>4) & 3) == (ctx->style & 3) && fl[i]->height == ctx->size &&
  908. (ptr = _ssfn_c(fl[i], str, &ret, &unicode))) { ctx->f = fl[i]; break; }
  909. /* if not, check if we have the requested size (for bitmap fonts) */
  910. if(!ptr)
  911. for(i=0;i<ctx->len[n];i++)
  912. if(fl[i]->height == ctx->size && (ptr = _ssfn_c(fl[i], str, &ret, &unicode))) { ctx->f = fl[i]; break; }
  913. /* if neither size+style nor size matched, look for style match */
  914. if(!ptr)
  915. for(i=0;i<ctx->len[n];i++)
  916. if(((fl[i]->type>>4) & 3) == (ctx->style & 3) && (ptr = _ssfn_c(fl[i], str, &ret, &unicode)))
  917. { ctx->f = fl[i]; break; }
  918. /* if bold italic was requested, check if we have at least bold or italic */
  919. if(!ptr && (ctx->style & 3) == 3)
  920. for(i=0;i<ctx->len[n];i++)
  921. if(((fl[i]->type>>4) & 3) && (ptr = _ssfn_c(fl[i], str, &ret, &unicode))) { ctx->f = fl[i]; break; }
  922. }
  923. /* last resort, get the first ctx->f which has a glyph for this multibyte, no matter style */
  924. if(!ptr) {
  925. for(i=0;i<ctx->len[n];i++)
  926. if((ptr = _ssfn_c(fl[i], str, &ret, &unicode))) { ctx->f = fl[i]; break; }
  927. }
  928. }
  929. /* if glyph still not found, try any family group */
  930. if(!ptr && p != SSFN_FAMILY_ANY) { p = SSFN_FAMILY_ANY; goto again; }
  931. }
  932. if(!ptr) {
  933. if(ctx->style & SSFN_STYLE_NODEFGLYPH) return SSFN_ERR_NOGLYPH;
  934. else {
  935. unicode = 0;
  936. if(ctx->family >= SSFN_FAMILY_BYNAME) { n = 0; m = 4; } else n = m = ctx->family;
  937. for(; n <= m && !ptr; n++)
  938. if(ctx->len[n] && ctx->fnt[n][0] && !(*((uint8_t*)ctx->fnt[n][0] + ctx->fnt[n][0]->characters_offs) & 0x80))
  939. { ctx->f = ctx->fnt[n][0]; ptr = (uint8_t*)ctx->f + ctx->f->characters_offs; }
  940. }
  941. if(!ptr) return SSFN_ERR_NOGLYPH;
  942. }
  943. if(!ctx->f || !ctx->f->height || !ctx->size) return SSFN_ERR_NOFACE;
  944. if((unicode >> 16) > 0x10) return SSFN_ERR_INVINP;
  945. ctx->rc = (ssfn_chr_t*)ptr; ptr += sizeof(ssfn_chr_t);
  946. H = (ctx->style & SSFN_STYLE_ABS_SIZE) || SSFN_TYPE_FAMILY(ctx->f->type) == SSFN_FAMILY_MONOSPACE || !ctx->f->baseline ?
  947. ctx->size : ctx->size * ctx->f->height / ctx->f->baseline;
  948. #ifdef SSFN_PROFILING
  949. gettimeofday(&tv1, NULL); tvd.tv_sec = tv1.tv_sec - tv0.tv_sec; tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
  950. if(tvd.tv_usec < 0) { tvd.tv_sec--; tvd.tv_usec += 1000000L; }
  951. ctx->lookup += tvd.tv_sec * 1000000L + tvd.tv_usec;
  952. memcpy(&tv0, &tv1, sizeof(struct timeval));
  953. #endif
  954. /* render glyph into cache */
  955. #ifndef SSFN_MAXLINES
  956. if(!(ctx->style & SSFN_STYLE_NOCACHE) && ctx->c[unicode >> 16] && ctx->c[unicode >> 16][(unicode >> 8) & 0xFF] &&
  957. ctx->c[unicode >> 16][(unicode >> 8) & 0xFF][unicode & 0xFF]) {
  958. ctx->g = ctx->c[unicode >> 16][(unicode >> 8) & 0xFF][unicode & 0xFF];
  959. } else
  960. #endif
  961. {
  962. h = ctx->style & SSFN_STYLE_NOAA ? H : (ctx->size > ctx->f->height ? (ctx->size + 4) & ~3 : ctx->f->height);
  963. ci = (ctx->style & SSFN_STYLE_ITALIC) && !(SSFN_TYPE_STYLE(ctx->f->type) & SSFN_STYLE_ITALIC);
  964. cb = (ctx->style & SSFN_STYLE_BOLD) && !(SSFN_TYPE_STYLE(ctx->f->type) & SSFN_STYLE_BOLD) ? (ctx->f->height+64)>>6 : 0;
  965. w = (ctx->rc->w * h + ctx->f->height - 1) / ctx->f->height;
  966. if(w > SSFN_SIZE_MAX) { h = h * SSFN_SIZE_MAX / w; w = SSFN_SIZE_MAX; }
  967. p = w + (ci ? h / SSFN_ITALIC_DIV : 0) + cb;
  968. /* failsafe, should never happen */
  969. if(p * h >= SSFN_DATA_MAX) return SSFN_ERR_BADSIZE;
  970. #ifndef SSFN_MAXLINES
  971. if(!(ctx->style & SSFN_STYLE_NOCACHE)) {
  972. if(!ctx->c[unicode >> 16]) {
  973. ctx->c[unicode >> 16] = (ssfn_glyph_t***)SSFN_realloc(NULL, 256 * sizeof(void*));
  974. if(!ctx->c[unicode >> 16]) return SSFN_ERR_ALLOC;
  975. SSFN_memset(ctx->c[unicode >> 16], 0, 256 * sizeof(void*));
  976. }
  977. if(!ctx->c[unicode >> 16][(unicode >> 8) & 0xFF]) {
  978. ctx->c[unicode >> 16][(unicode >> 8) & 0xFF] = (ssfn_glyph_t**)SSFN_realloc(NULL, 256 * sizeof(void*));
  979. if(!ctx->c[unicode >> 16][(unicode >> 8) & 0xFF]) return SSFN_ERR_ALLOC;
  980. SSFN_memset(ctx->c[unicode >> 16][(unicode >> 8) & 0xFF], 0, 256 * sizeof(void*));
  981. }
  982. ctx->g = ctx->c[unicode >> 16][(unicode >> 8) & 0xFF][unicode & 0xFF] = (ssfn_glyph_t*)SSFN_realloc(NULL, p * h + 8);
  983. if(!ctx->c[unicode >> 16][(unicode >> 8) & 0xFF][unicode & 0xFF]) return SSFN_ERR_ALLOC;
  984. } else
  985. #endif
  986. ctx->g = &ctx->ga;
  987. x = (ctx->rc->x > 0 && ci ? (ctx->f->height - ctx->f->baseline) * h / SSFN_ITALIC_DIV / ctx->f->height : 0);
  988. ctx->g->p = p;
  989. ctx->g->h = h;
  990. ctx->g->x = (ctx->rc->x + x > 255 ? 255 : ctx->rc->x + x);
  991. ctx->g->y = ctx->rc->y;
  992. ctx->g->o = (ctx->rc->t & 0x3F) + x;
  993. SSFN_memset(&ctx->g->data, 0xFF, p * h);
  994. color = 0xFE; ctx->g->a = ctx->g->d = 0;
  995. for(n = 0; n < ctx->rc->n; n++) {
  996. if(ptr[0] == 255 && ptr[1] == 255) { color = ptr[2]; ptr += ctx->rc->t & 0x40 ? 6 : 5; continue; }
  997. x = ((ptr[0] + cb) << SSFN_PREC) * h / ctx->f->height; y = (ptr[1] << SSFN_PREC) * h / ctx->f->height;
  998. if(ctx->rc->t & 0x40) { m = (ptr[5] << 24) | (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]; ptr += 6; }
  999. else { m = (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]; ptr += 5; }
  1000. frg = (uint8_t*)ctx->f + m;
  1001. if(!(frg[0] & 0x80)) {
  1002. /* contour */
  1003. j = (frg[0] & 0x3F);
  1004. if(frg[0] & 0x40) { j <<= 8; j |= frg[1]; frg++; }
  1005. j++; frg++; tmp = frg; frg += (j+3)/4; ctx->np = 0;
  1006. for(i = 0; i < j; i++) {
  1007. k = (frg[0] << SSFN_PREC) * h / ctx->f->height + x; m = (frg[1] << SSFN_PREC) * h / ctx->f->height + y;
  1008. switch((tmp[i >> 2] >> ((i & 3) << 1)) & 3) {
  1009. case SSFN_CONTOUR_MOVE: ctx->mx = ctx->lx = k; ctx->my = ctx->ly = m; frg += 2; break;
  1010. case SSFN_CONTOUR_LINE: _ssfn_l(ctx, p << SSFN_PREC, h << SSFN_PREC, k, m); frg += 2; break;
  1011. case SSFN_CONTOUR_QUAD:
  1012. a = (frg[2] << SSFN_PREC) * h / ctx->f->height + x; A = (frg[3] << SSFN_PREC) * h / ctx->f->height + y;
  1013. _ssfn_b(ctx, p << SSFN_PREC,h << SSFN_PREC, ctx->lx,ctx->ly, ((a-ctx->lx)/2)+ctx->lx,
  1014. ((A-ctx->ly)/2)+ctx->ly, ((k-a)/2)+a,((A-m)/2)+m, k,m, 0);
  1015. frg += 4;
  1016. break;
  1017. case SSFN_CONTOUR_CUBIC:
  1018. a = (frg[2] << SSFN_PREC) * h / ctx->f->height + x; A = (frg[3] << SSFN_PREC) * h / ctx->f->height + y;
  1019. b = (frg[4] << SSFN_PREC) * h / ctx->f->height + x; B = (frg[5] << SSFN_PREC) * h / ctx->f->height + y;
  1020. _ssfn_b(ctx, p << SSFN_PREC,h << SSFN_PREC, ctx->lx,ctx->ly, a,A, b,B, k,m, 0);
  1021. frg += 6;
  1022. break;
  1023. }
  1024. }
  1025. /* close path */
  1026. if(ctx->mx != ctx->lx || ctx->my != ctx->ly) { ctx->p[ctx->np+0] = ctx->mx; ctx->p[ctx->np+1] = ctx->my; ctx->np += 2; }
  1027. /* add rasterized vector layers to cached glyph */
  1028. if(ctx->np > 4) {
  1029. for(b = A = B = o = 0; b < h; b++, B += p) {
  1030. a = b << SSFN_PREC;
  1031. for(nr = 0, i = 0; i < ctx->np - 3; i += 2) {
  1032. if( (ctx->p[i+1] < a && ctx->p[i+3] >= a) ||
  1033. (ctx->p[i+3] < a && ctx->p[i+1] >= a)) {
  1034. if((ctx->p[i+1] >> SSFN_PREC) == (ctx->p[i+3] >> SSFN_PREC))
  1035. x = (((int)ctx->p[i]+(int)ctx->p[i+2])>>1);
  1036. else
  1037. x = ((int)ctx->p[i]) + ((a - (int)ctx->p[i+1])*
  1038. ((int)ctx->p[i+2] - (int)ctx->p[i])/
  1039. ((int)ctx->p[i+3] - (int)ctx->p[i+1]));
  1040. x >>= SSFN_PREC;
  1041. if(ci) x += (h - b) / SSFN_ITALIC_DIV;
  1042. if(cb && !o) {
  1043. if(ctx->g->data[B + x] != color) { o = -cb; A = cb; }
  1044. else { o = cb; A = -cb; }
  1045. }
  1046. for(k = 0; k < nr && x > r[k]; k++);
  1047. for(l = nr; l > k; l--) r[l] = r[l-1];
  1048. r[k] = x;
  1049. nr++;
  1050. }
  1051. }
  1052. if(nr > 1 && nr & 1) { r[nr - 2] = r[nr - 1]; nr--; }
  1053. if(nr) {
  1054. if(ctx->g->d < y + b) ctx->g->d = y + b;
  1055. for(i = 0; i < nr - 1; i += 2) {
  1056. l = r[i] + o; m = r[i + 1] + A;
  1057. if(l < 0) l = 0;
  1058. if(m > p) m = p;
  1059. if(i > 0 && l < r[i - 1] + A) l = r[i - 1] + A;
  1060. for(; l < m; l++)
  1061. ctx->g->data[B + l] = ctx->g->data[B + l] == color ? 0xFF : color;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. } else if((frg[0] & 0x60) == 0x00) {
  1067. /* bitmap */
  1068. B = ((frg[0] & 0x1F) + 1) << 3; A = frg[1] + 1; x >>= SSFN_PREC; y >>= SSFN_PREC;
  1069. b = B * h / ctx->f->height; a = A * h / ctx->f->height;
  1070. if(ctx->g->d < y + a) ctx->g->d = y + a;
  1071. frg += 2;
  1072. for(j = 0; j < a; j++) {
  1073. k = j * A / a;
  1074. l = (y + j) * p + x + (ci ? (h - y - j) / SSFN_ITALIC_DIV : 0);
  1075. for(i = 0; i < b; i++) {
  1076. m = i * B / b;
  1077. if(frg[(k * B + m) >> 3] & (1 << (m & 7))) {
  1078. for(o = 0; o <= cb; o++)
  1079. ctx->g->data[l + i + o] = color;
  1080. }
  1081. }
  1082. }
  1083. if(!(ctx->style & (SSFN_STYLE_NOAA|SSFN_STYLE_NOSMOOTH))) {
  1084. m = color == 0xFD ? 0xFC : 0xFD; o = y * p + p + x;
  1085. for(k = h; k > ctx->f->height + 4; k -= 2*ctx->f->height) {
  1086. for(j = 1, l = o; j < a - 1; j++, l += p)
  1087. for(i = 1; i < b - 1; i++) {
  1088. if(ctx->g->data[l + i] == 0xFF && (ctx->g->data[l + i - p] == color ||
  1089. ctx->g->data[l + i + p] == color) && (ctx->g->data[l + i - 1] == color ||
  1090. ctx->g->data[l + i + 1] == color)) ctx->g->data[l + i] = m;
  1091. }
  1092. for(j = 1, l = o; j < a - 1; j++, l += p)
  1093. for(i = 1; i < b - 1; i++) {
  1094. if(ctx->g->data[l + i] == m) ctx->g->data[l + i] = color;
  1095. }
  1096. }
  1097. }
  1098. } else if((frg[0] & 0x60) == 0x20) {
  1099. /* pixmap */
  1100. k = (((frg[0] & 0x1F) << 8) | frg[1]) + 1; B = frg[2] + 1; A = frg[3] + 1; x >>= SSFN_PREC; y >>= SSFN_PREC;
  1101. b = B * h / ctx->f->height; a = A * h / ctx->f->height;
  1102. if(ctx->g->d < y + a) ctx->g->d = y + a;
  1103. frg += 4; end = frg + k; i = 0;
  1104. while(frg < end) {
  1105. l = ((*frg++) & 0x7F) + 1;
  1106. if(frg[-1] & 0x80) {
  1107. while(l--) dec[i++] = *frg;
  1108. frg++;
  1109. } else while(l--) dec[i++] = *frg++;
  1110. }
  1111. for(j = 0; j < a; j++) {
  1112. k = j * A / a * B;
  1113. l = (y + j) * p + x + (ci ? (h - y - j) / SSFN_ITALIC_DIV : 0);
  1114. for(i = 0; i < b; i++) {
  1115. m = dec[k + i * B / b];
  1116. if(m != 0xFF) ctx->g->data[l + i] = m;
  1117. }
  1118. }
  1119. }
  1120. color = 0xFE;
  1121. }
  1122. ctx->g->a = ctx->f->baseline;
  1123. if(ctx->g->d > ctx->g->a + 1) ctx->g->d -= ctx->g->a + 1; else ctx->g->d = 0;
  1124. #ifdef SSFN_DEBUGGLYPH
  1125. printf("\nU+%06X size %d p %d h %d base %d under %d overlap %d ascender %d descender %d advance x %d advance y %d cb %d\n",
  1126. unicode, ctx->size,p,h,ctx->f->baseline,ctx->f->underline,ctx->g->o,ctx->g->a,ctx->g->d,ctx->g->x,ctx->g->y,cb);
  1127. for(j = 0; j < h; j++) { printf("%3d: ", j); for(i = 0; i < p; i++) { if(ctx->g->data[j*p+i] == 0xFF) printf(j == ctx->g->a ? "_" : "."); else printf("%x", ctx->g->data[j*p+i] & 0xF); } printf("\n"); }
  1128. #endif
  1129. #ifdef SSFN_PROFILING
  1130. gettimeofday(&tv1, NULL); tvd.tv_sec = tv1.tv_sec - tv0.tv_sec; tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
  1131. if(tvd.tv_usec < 0) { tvd.tv_sec--; tvd.tv_usec += 1000000L; }
  1132. ctx->raster += tvd.tv_sec * 1000000L + tvd.tv_usec;
  1133. memcpy(&tv0, &tv1, sizeof(struct timeval));
  1134. #endif
  1135. }
  1136. if(dst) {
  1137. /* blit glyph from cache into buffer */
  1138. h = H;
  1139. if(h > ctx->line) ctx->line = h;
  1140. w = ctx->g->p * h / ctx->g->h;
  1141. s = ((ctx->g->x - ctx->g->o) * h + ctx->f->height - 1) / ctx->f->height;
  1142. n = ctx->size > 16 ? 2 : 1;
  1143. if(w < n) w = n;
  1144. if(s < n) s = n;
  1145. if(ctx->g->x) {
  1146. ctx->ox = ox = ((ctx->g->o * h + ctx->f->height - 1) / ctx->f->height) + (ctx->style & SSFN_STYLE_RTL ? w : 0);
  1147. ctx->oy = oy = (ctx->g->a * h + ctx->f->height - 1) / ctx->f->height;
  1148. } else { ctx->ox = ox = w / 2; ctx->oy = oy = 0; }
  1149. if(dst->ptr) {
  1150. j = dst->w < 0 ? -dst->w : dst->w;
  1151. cs = dst->w < 0 ? 16 : 0;
  1152. cb = (h + 64) >> 6; uix = w > s ? w : s; uax = 0;
  1153. n = (ctx->f->underline * h + ctx->f->height - 1) / ctx->f->height;
  1154. #ifdef SSFN_DEBUGGLYPH
  1155. printf("Scaling to w %d h %d (glyph %d %d, cache %d %d, font %d)\n",
  1156. w,h,ctx->rc->w,ctx->rc->h,ctx->g->p,ctx->g->h,ctx->f->height);
  1157. #endif
  1158. fR = (dst->fg >> 16) & 0xFF; fG = (dst->fg >> 8) & 0xFF; fB = (dst->fg >> 0) & 0xFF; fA = (dst->fg >> 24) & 0xFF;
  1159. bR = (dst->bg >> 16) & 0xFF; bG = (dst->bg >> 8) & 0xFF; bB = (dst->bg >> 0) & 0xFF; O = 0xFF000000;
  1160. Op = (uint32_t*)(dst->ptr + dst->p * (dst->y - oy) + ((dst->x - ox) << 2));
  1161. for (y = 0; y < h && dst->y + y - oy < dst->h; y++, Op += dst->p >> 2) {
  1162. if(dst->y + y - oy < 0) continue;
  1163. y0 = (y << 8) * ctx->g->h / h; Y0 = y0 >> 8; y1 = ((y + 1) << 8) * ctx->g->h / h; Y1 = y1 >> 8; Ol = Op;
  1164. for (x = 0; x < w && dst->x + x - ox < j; x++, Ol++) {
  1165. if(dst->x + x - ox < 0) continue;
  1166. m = 0; sR = sG = sB = sA = 0;
  1167. if(!dst->bg) {
  1168. /* real linear frame buffers should be accessed only as uint32_t on 32 bit boundary */
  1169. O = *Ol;
  1170. bR = (O >> (16 - cs)) & 0xFF;
  1171. bG = (O >> 8) & 0xFF;
  1172. bB = (O >> cs) & 0xFF;
  1173. }
  1174. x0 = (x << 8) * ctx->g->p / w; X0 = x0 >> 8; x1 = ((x + 1) << 8) * ctx->g->p / w; X1 = x1 >> 8;
  1175. for(ys = y0; ys < y1; ys += 256) {
  1176. if(ys >> 8 == Y0) { yp = 256 - (ys & 0xFF); ys &= ~0xFF; if(yp > y1 - y0) yp = y1 - y0; }
  1177. else if(ys >> 8 == Y1) yp = y1 & 0xFF; else yp = 256;
  1178. X2 = (ys >> 8) * ctx->g->p;
  1179. for(xs = x0; xs < x1; xs += 256) {
  1180. if (xs >> 8 == X0) {
  1181. k = 256 - (xs & 0xFF); xs &= ~0xFF; if(k > x1 - x0) k = x1 - x0; pc = k == 256 ? yp : (k * yp)>>8;
  1182. } else
  1183. if (xs >> 8 == X1) { k = x1 & 0xFF; pc = k == 256 ? yp : (k * yp) >> 8; }
  1184. else pc = yp;
  1185. m += pc;
  1186. k = ctx->g->data[X2 + (xs >> 8)];
  1187. if(k == 0xFF) {
  1188. sB += bB * pc; sG += bG * pc; sR += bR * pc; sA += 255;
  1189. } else {
  1190. if(k == 0xFE || !ctx->f->cmap_offs) {
  1191. dB = fB; dG = fG; dR = fR; dA = fA;
  1192. } else {
  1193. P = *((uint32_t*)((uint8_t*)ctx->f + ctx->f->cmap_offs + (k << 2)));
  1194. dR = (P >> 16) & 0xFF; dG = (P >> 8) & 0xFF; dB = (P >> 0) & 0xFF; dA = (P >> 24) & 0xFF;
  1195. }
  1196. if(dA == 255) {
  1197. sB += dB * pc; sG += dG * pc; sR += dR * pc; sA += dA * pc;
  1198. } else {
  1199. sB += (dB * dA + bB * (255 - dA)) * pc / 255; sG += (dG * dA + bG * (255 - dA)) * pc / 255;
  1200. sR += (dR * dA + bR * (255 - dA)) * pc / 255; sA += dA * pc;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. if(m) { sR /= m; sG /= m; sB /= m; sA /= m; } else { sR >>= 8; sG >>= 8; sB >>= 8; sA >>= 8; }
  1206. if(ctx->style & SSFN_STYLE_NOAA) sA = sA > 127 ? 255 : 0;
  1207. if(sA > 15) {
  1208. *Ol = (ctx->style & SSFN_STYLE_A ? O & 0xFF000000 : ((sA > 255 ? 255 : sA) << 24)) |
  1209. ((sR > 255 ? 255 : sR) << (16 - cs)) | ((sG > 255 ? 255 : sG) << 8) | ((sB > 255 ? 255 : sB) << cs);
  1210. if(y == n) { if(uix > x) { uix = x; } if(uax < x) { uax = x; } }
  1211. }
  1212. }
  1213. }
  1214. if(ctx->style & SSFN_STYLE_UNDERLINE) {
  1215. uix -= cb + 1; uax += cb + 2;
  1216. if(uax < uix) uax = uix + 1;
  1217. k = (w > s ? w : s);
  1218. Op = (uint32_t*)(dst->ptr + dst->p * (dst->y - oy + n) + ((dst->x - ox - 1) << 2));
  1219. for (y = n; y < n + cb && dst->y + y - oy < dst->h; y++, Op += dst->p >> 2) {
  1220. if(dst->y + y - oy < 0) continue;
  1221. for (Ol = Op, x = 0; x <= k && dst->x + x - ox < j; x++, Ol++) {
  1222. if(dst->x + x - ox < 0 || (x > uix && x < uax)) continue;
  1223. PUTPIXEL;
  1224. }
  1225. }
  1226. }
  1227. if(ctx->style & SSFN_STYLE_STHROUGH) {
  1228. n = (h >> 1); k = (w > s ? w : s) + 1;
  1229. Op = (uint32_t*)(dst->ptr + dst->p * (dst->y - oy + n) + ((dst->x - ox - 1) << 2));
  1230. for (y = n; y < n + cb && dst->y + y - oy < dst->h; y++, Op += dst->p >> 2) {
  1231. if(dst->y + y - oy < 0) continue;
  1232. for (Ol = Op, x = 0; x <= k && dst->x + x - ox < j; x++, Ol++) {
  1233. if(dst->x + x - ox < 0) continue;
  1234. PUTPIXEL;
  1235. }
  1236. }
  1237. }
  1238. #ifdef SSFN_PROFILING
  1239. gettimeofday(&tv1, NULL); tvd.tv_sec = tv1.tv_sec - tv0.tv_sec;tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
  1240. if(tvd.tv_usec < 0) { tvd.tv_sec--; tvd.tv_usec += 1000000L; }
  1241. ctx->blit += tvd.tv_sec * 1000000L + tvd.tv_usec;
  1242. memcpy(&tv0, &tv1, sizeof(struct timeval));
  1243. #endif
  1244. }
  1245. /* add advance and kerning */
  1246. ctx->ax = (ctx->style & SSFN_STYLE_RTL ? -s : s);
  1247. dst->x += ctx->ax;
  1248. dst->y += (ctx->g->y * h + ctx->f->height - 1) / ctx->f->height;
  1249. ptr = (uint8_t*)str + ret;
  1250. if(!(ctx->style & SSFN_STYLE_NOKERN) && ctx->f->kerning_offs && _ssfn_c(ctx->f, (const char*)ptr, &i, &P) && P > 32) {
  1251. ptr = (uint8_t*)ctx->rc + sizeof(ssfn_chr_t);
  1252. /* check all kerning fragments, because we might have both vertical and horizontal kerning offsets */
  1253. for(n = 0; n < ctx->rc->n; n++) {
  1254. if(ptr[0] == 255 && ptr[1] == 255) { ptr += ctx->rc->t & 0x40 ? 6 : 5; continue; }
  1255. x = ptr[0];
  1256. if(ctx->rc->t & 0x40) { m = (ptr[5] << 24) | (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]; ptr += 6; }
  1257. else { m = (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]; ptr += 5; }
  1258. frg = (uint8_t*)ctx->f + m;
  1259. if((frg[0] & 0xE0) == 0xC0) {
  1260. k = (((frg[0] & 0x1F) << 8) | frg[1]) + 1; frg += 2;
  1261. while(k--) {
  1262. m = ((frg[2] & 0xF) << 16) | (frg[1] << 8) | frg[0];
  1263. if(P >= (uint32_t)m && P <= (uint32_t)(((frg[5] & 0xF) << 16) | (frg[4] << 8) | frg[3])) {
  1264. P -= m;
  1265. m = ctx->f->kerning_offs + ((((frg[2] >> 4) & 0xF) << 24) | (((frg[5] >> 4) & 0xF) << 16) |
  1266. (frg[7] << 8) | frg[6]);
  1267. tmp = (uint8_t*)ctx->f + m;
  1268. while(tmp < (uint8_t*)ctx->f + ctx->f->size - 4) {
  1269. if((tmp[0] & 0x7F) < P) {
  1270. P -= (tmp[0] & 0x7F) + 1;
  1271. tmp += 2 + (tmp[0] & 0x80 ? 0 : tmp[0] & 0x7F);
  1272. } else {
  1273. y = (int)((signed char)tmp[1 + ((tmp[0] & 0x80) ? 0 : P)]) * h / ctx->f->height;
  1274. if(x) dst->x += y; else dst->y += y;
  1275. break;
  1276. }
  1277. }
  1278. break;
  1279. }
  1280. frg += 8;
  1281. }
  1282. }
  1283. }
  1284. #ifdef SSFN_PROFILING
  1285. gettimeofday(&tv1, NULL); tvd.tv_sec = tv1.tv_sec - tv0.tv_sec; tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
  1286. if(tvd.tv_usec < 0) { tvd.tv_sec--; tvd.tv_usec += 1000000L; }
  1287. ctx->kern += tvd.tv_sec * 1000000L + tvd.tv_usec;
  1288. #endif
  1289. }
  1290. }
  1291. return ret;
  1292. }
  1293. /**
  1294. * Get the bounding box for a string
  1295. * @param ctx rendering context
  1296. * @param str string to measure
  1297. * @param w returned width
  1298. * @param h returned height
  1299. * @param left returned left margin
  1300. * @param top returned ascender
  1301. * @return error code
  1302. */
  1303. int ssfn_bbox(ssfn_t *ctx, const char *str, int *w, int *h, int *left, int *top)
  1304. {
  1305. ssfn_buf_t buf;
  1306. int ret, f = 1, l = 0, t = 0;
  1307. if(!ctx || !str) return SSFN_ERR_INVINP;
  1308. if(w) {*w = 0;} if(h) {*h = 0;} if(top) {*top = 0;} if(left) {*left = 0;}
  1309. if(!*str) return SSFN_OK;
  1310. SSFN_memset(&buf, 0, sizeof(ssfn_buf_t)); ctx->line = 0;
  1311. while((ret = ssfn_render(ctx, &buf, str))) {
  1312. if(ret < 0 || !ctx->g) return ret;
  1313. if(f) { f = 0; l = ctx->ox; buf.x += l; }
  1314. if(ctx->g->x) {
  1315. if(ctx->oy > t) t = ctx->oy;
  1316. } else {
  1317. if(buf.w < ctx->g->p) buf.w = ctx->g->p;
  1318. buf.h += ctx->g->y ? ctx->g->y : ctx->g->h;
  1319. }
  1320. str += ret;
  1321. }
  1322. if((ctx->style & SSFN_STYLE_ITALIC) && !(SSFN_TYPE_STYLE(ctx->f->type) & SSFN_STYLE_ITALIC))
  1323. buf.x += ctx->size / SSFN_ITALIC_DIV - l;
  1324. if(ctx->g->x) { if(w) {*w = buf.x;} if(h) {*h = ctx->line;} if(left) {*left = l;} if(top) {*top = t;} }
  1325. else { if(w) {*w = buf.w;} if(h) {*h = buf.y;} if(top) {*top = 0;} if(left) {*left = 0;} }
  1326. return SSFN_OK;
  1327. }
  1328. /**
  1329. * Render text to a newly allocated pixel buffer
  1330. *
  1331. * @param ctx rendering context
  1332. * @param str string to measure
  1333. * @return a newly allocated pixel buffer or NULL
  1334. */
  1335. ssfn_buf_t *ssfn_text(ssfn_t *ctx, const char *str, unsigned int fg)
  1336. {
  1337. #ifndef SSFN_MAXLINES
  1338. ssfn_buf_t *buf;
  1339. int ret;
  1340. if(!ctx || !str) return NULL;
  1341. buf = (ssfn_buf_t*)SSFN_realloc(NULL, sizeof(ssfn_buf_t));
  1342. if(!buf) return NULL;
  1343. SSFN_memset(buf, 0, sizeof(ssfn_buf_t));
  1344. buf->fg = fg;
  1345. if(!*str || ssfn_bbox(ctx, str, (int*)&buf->w, (int*)&buf->h, (int*)&buf->x, (int*)&buf->y) != SSFN_OK)
  1346. return buf;
  1347. buf->p = buf->w * sizeof(uint32_t);
  1348. buf->ptr = (uint8_t*)SSFN_realloc(NULL, buf->p * buf->h);
  1349. SSFN_memset(buf->ptr, 0, buf->p * buf->h);
  1350. ctx->style &= ~SSFN_STYLE_A;
  1351. while((ret = ssfn_render(ctx, buf, str)) > 0)
  1352. str += ret;
  1353. if(ret != SSFN_OK) { SSFN_free(buf->ptr); SSFN_free(buf); return NULL; }
  1354. return buf;
  1355. #else
  1356. (void)ctx;
  1357. (void)str;
  1358. (void)fg;
  1359. return NULL;
  1360. #endif
  1361. }
  1362. #endif /* SSFN_IMPLEMENTATION */
  1363. #if defined(SSFN_CONSOLEBITMAP_PALETTE) || defined(SSFN_CONSOLEBITMAP_HICOLOR) || defined(SSFN_CONSOLEBITMAP_TRUECOLOR)
  1364. /*** special console bitmap font renderer (ca. 1.5k, no dependencies, no memory allocation and no error checking) ***/
  1365. /**
  1366. * public variables to configure
  1367. */
  1368. ssfn_font_t *ssfn_src; /* font buffer with an inflated bitmap font */
  1369. ssfn_buf_t ssfn_dst; /* destination frame buffer */
  1370. /**
  1371. * Minimal OS kernel console renderer
  1372. *
  1373. * @param unicode character
  1374. * @return error code
  1375. */
  1376. int ssfn_putc(uint32_t unicode)
  1377. {
  1378. # ifdef SSFN_CONSOLEBITMAP_PALETTE
  1379. # define SSFN_PIXEL uint8_t
  1380. # else
  1381. # ifdef SSFN_CONSOLEBITMAP_HICOLOR
  1382. # define SSFN_PIXEL uint16_t
  1383. # else
  1384. # define SSFN_PIXEL uint32_t
  1385. # endif
  1386. # endif
  1387. register SSFN_PIXEL *o, *p;
  1388. register uint8_t *ptr, *chr = NULL, *frg;
  1389. register int i, j, k, l, m, y = 0, w, s = ssfn_dst.p / sizeof(SSFN_PIXEL);
  1390. if(!ssfn_src || ssfn_src->magic[0] != 'S' || ssfn_src->magic[1] != 'F' || ssfn_src->magic[2] != 'N' ||
  1391. ssfn_src->magic[3] != '2' || !ssfn_dst.ptr || !ssfn_dst.p) return SSFN_ERR_INVINP;
  1392. w = ssfn_dst.w < 0 ? -ssfn_dst.w : ssfn_dst.w;
  1393. for(ptr = (uint8_t*)ssfn_src + ssfn_src->characters_offs, i = 0; i < 0x110000; i++) {
  1394. if(ptr[0] == 0xFF) { i += 65535; ptr++; }
  1395. else if((ptr[0] & 0xC0) == 0xC0) { j = (((ptr[0] & 0x3F) << 8) | ptr[1]); i += j; ptr += 2; }
  1396. else if((ptr[0] & 0xC0) == 0x80) { j = (ptr[0] & 0x3F); i += j; ptr++; }
  1397. else { if((uint32_t)i == unicode) { chr = ptr; break; } ptr += 6 + ptr[1] * (ptr[0] & 0x40 ? 6 : 5); }
  1398. }
  1399. #ifdef SSFN_CONSOLEBITMAP_CONTROL
  1400. i = ssfn_src->height; j = ssfn_dst.h - i - (ssfn_dst.h % i);
  1401. if(chr && w) {
  1402. if(unicode == '\t') ssfn_dst.x -= ssfn_dst.x % chr[4];
  1403. if(ssfn_dst.x + chr[4] > w) { ssfn_dst.x = 0; ssfn_dst.y += i; }
  1404. }
  1405. if(unicode == '\n') ssfn_dst.y += i;
  1406. if(j > 0 && ssfn_dst.y > j) {
  1407. ssfn_dst.y = j;
  1408. for(k = 0; k < j; k++)
  1409. for(l = 0; l < ssfn_dst.p; l++) ssfn_dst.ptr[k * ssfn_dst.p + l] = ssfn_dst.ptr[(k + i) * ssfn_dst.p + l];
  1410. }
  1411. if(unicode == '\r' || unicode == '\n') { ssfn_dst.x = 0; return SSFN_OK; }
  1412. #endif
  1413. if(!chr) return SSFN_ERR_NOGLYPH;
  1414. ptr = chr + 6; o = (SSFN_PIXEL*)(ssfn_dst.ptr + ssfn_dst.y * ssfn_dst.p + ssfn_dst.x * sizeof(SSFN_PIXEL));
  1415. for(i = 0; i < chr[1]; i++, ptr += chr[0] & 0x40 ? 6 : 5) {
  1416. if(ptr[0] == 255 && ptr[1] == 255) continue;
  1417. frg = (uint8_t*)ssfn_src + (chr[0] & 0x40 ? ((ptr[5] << 24) | (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]) :
  1418. ((ptr[4] << 16) | (ptr[3] << 8) | ptr[2]));
  1419. if((frg[0] & 0xE0) != 0x80) continue;
  1420. if(ssfn_dst.bg) {
  1421. for(; y < ptr[1] && (!ssfn_dst.h || ssfn_dst.y + y < ssfn_dst.h); y++, o += s) {
  1422. for(p = o, j = 0; j < chr[2] && (!w || ssfn_dst.x + j < w); j++, p++)
  1423. *p = ssfn_dst.bg;
  1424. }
  1425. } else { o += (int)(ptr[1] - y) * s; y = ptr[1]; }
  1426. k = ((frg[0] & 0x1F) + 1) << 3; j = frg[1] + 1; frg += 2;
  1427. for(m = 1; j && (!ssfn_dst.h || ssfn_dst.y + y < ssfn_dst.h); j--, y++, o += s)
  1428. for(p = o, l = 0; l < k; l++, p++, m <<= 1) {
  1429. if(m > 0x80) { frg++; m = 1; }
  1430. if(ssfn_dst.x + l >= 0 && (!w || ssfn_dst.x + l < w)) {
  1431. if(*frg & m) *p = ssfn_dst.fg; else
  1432. if(ssfn_dst.bg) *p = ssfn_dst.bg;
  1433. }
  1434. }
  1435. }
  1436. if(ssfn_dst.bg)
  1437. for(; y < chr[3] && (!ssfn_dst.h || ssfn_dst.y + y < ssfn_dst.h); y++, o += s) {
  1438. for(p = o, j = 0; j < chr[2] && (!w || ssfn_dst.x + j < w); j++, p++)
  1439. *p = ssfn_dst.bg;
  1440. }
  1441. ssfn_dst.x += chr[4]; ssfn_dst.y += chr[5];
  1442. return SSFN_OK;
  1443. }
  1444. #endif /* SSFN_CONSOLEBITMAP */
  1445. #ifdef __cplusplus
  1446. }
  1447. /*** SSFN C++ Wrapper Class ***/
  1448. #include <string>
  1449. namespace SSFN {
  1450. #ifndef SSFN_IMPLEMENTATION
  1451. class Font {
  1452. public:
  1453. Font();
  1454. ~Font();
  1455. public:
  1456. int Load(const std::string &data);
  1457. int Load(const void *data, int len);
  1458. int Select(int family, const std::string &name, int style, int size);
  1459. int Select(int family, const char *name, int style, int size);
  1460. int Render(ssfn_buf_t *dst, const std::string &str);
  1461. int Render(ssfn_buf_t *dst, const char *str);
  1462. int BBox(const std::string &str, int *w, int *h, int *left, int *top);
  1463. int BBox(const char *str, int *w, int *h, int *left, int *top);
  1464. ssfn_buf_t *Text(const std::string &str, unsigned int fg);
  1465. ssfn_buf_t *Text(const char *str, unsigned int fg);
  1466. int LineHeight();
  1467. int Mem();
  1468. const std::string ErrorStr(int err);
  1469. };
  1470. #else
  1471. class Font {
  1472. private:
  1473. ssfn_t ctx;
  1474. public:
  1475. Font() { SSFN_memset(&this->ctx, 0, sizeof(ssfn_t)); }
  1476. ~Font() { ssfn_free(&this->ctx); }
  1477. public:
  1478. int Load(const std::string &data) { return ssfn_load(&this->ctx,reinterpret_cast<const void*>(data.data())); }
  1479. int Load(const void *data) { return ssfn_load(&this->ctx, data); }
  1480. int Select(int family, const std::string &name, int style, int size)
  1481. { return ssfn_select(&this->ctx, family, (char*)name.data(), style, size); }
  1482. int Select(int family, char *name, int style, int size) { return ssfn_select(&this->ctx,family,name,style,size); }
  1483. int Render(ssfn_buf_t *dst, const std::string &str) { return ssfn_render(&this->ctx,dst,(const char *)str.data()); }
  1484. int Render(ssfn_buf_t *dst, const char *str) { return ssfn_render(&this->ctx, dst, str); }
  1485. int BBox(const std::string &str, int *w, int *h, int *left, int *top)
  1486. { return ssfn_bbox(&this->ctx, (const char *)str.data(), w, h, left, top); }
  1487. int BBox(const char *str, int *w, int *h, int *left, int *top) { return ssfn_bbox(&this->ctx,str,w,h,left,top); }
  1488. ssfn_buf_t *Text(const std::string &str, unsigned int fg)
  1489. { return ssfn_text(&this->ctx,(const char*)str.data(), fg); }
  1490. ssfn_buf_t *Text(const char *str, unsigned int fg) { return ssfn_text(&this->ctx, str, fg); }
  1491. int LineHeight() { return this->ctx.line ? this->ctx.line : this->ctx.size; }
  1492. int Mem() { return ssfn_mem(&this->ctx); }
  1493. const std::string ErrorStr(int err) { return std::string(ssfn_error(err)); }
  1494. };
  1495. #endif
  1496. }
  1497. #endif
  1498. /* */
  1499. #endif /* _SSFN_H_ */