ssfn.h 64 KB

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