278#define STB_TRUETYPE_IMPLEMENTATION
279#include "stb_truetype.h"
281unsigned char ttf_buffer[1<<20];
282unsigned char temp_bitmap[512*512];
284stbtt_bakedchar cdata[96];
287void my_stbtt_initfont(
void)
289 fread(ttf_buffer, 1, 1<<20, fopen(
"c:/windows/fonts/times.ttf",
"rb"));
290 stbtt_BakeFontBitmap(ttf_buffer, 0, 32.0, temp_bitmap, 512, 512, 32, 96, cdata);
292 glGenTextures(1, &ftex);
293 glBindTexture(GL_TEXTURE_2D, ftex);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512, 512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
299void my_stbtt_print(
float x,
float y,
char *text)
303 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304 glEnable(GL_TEXTURE_2D);
305 glBindTexture(GL_TEXTURE_2D, ftex);
308 if (*text >= 32 && *text < 128) {
309 stbtt_aligned_quad q;
310 stbtt_GetBakedQuad(cdata, 512, 512, *text-32, &x, &y, &q, 1);
311 glTexCoord2f(q.s0, q.t0); glVertex2f(q.x0, q.y0);
312 glTexCoord2f(q.s1, q.t0); glVertex2f(q.x1, q.y0);
313 glTexCoord2f(q.s1, q.t1); glVertex2f(q.x1, q.y1);
314 glTexCoord2f(q.s0, q.t1); glVertex2f(q.x0, q.y1);
329#define STB_TRUETYPE_IMPLEMENTATION
330#include "stb_truetype.h"
332char ttf_buffer[1<<25];
334int main(
int argc,
char **argv)
337 unsigned char *bitmap;
338 int w,
h,
i,
j, c = (argc > 1 ? atoi(argv[1]) :
'a'),
s = (argc > 2 ? atoi(argv[2]) : 20);
340 fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] :
"c:/windows/fonts/arialbd.ttf",
"rb"));
342 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0));
343 bitmap = stbtt_GetCodepointBitmap(&font, 0, stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0, 0);
345 for (j = 0;
j <
h; ++
j) {
346 for (i = 0;
i <
w; ++
i)
347 putchar(
" .:ioVM@"[bitmap[j*w+i]>>5]);
373unsigned char screen[20][79];
375int main(
int arg,
char **argv)
378 int i,
j, ascent, baseline, ch = 0;
379 float scale, xpos = 2;
380 char *text =
"Heljo World!";
382 fread(buffer, 1, 1000000, fopen(
"c:/windows/fonts/arialbd.ttf",
"rb"));
383 stbtt_InitFont(&font, buffer, 0);
385 scale = stbtt_ScaleForPixelHeight(&font, 15);
386 stbtt_GetFontVMetrics(&font, &ascent, 0, 0);
387 baseline =
static_cast<int>(ascent*scale);
390 int advance, lsb, x0, y0, x1, y1;
391 float x_shift = xpos - (float)floor(xpos);
392 stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
393 stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale, scale, x_shift, 0, &x0, &y0, &x1, &y1);
394 stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][
static_cast<int>(xpos) + x0], x1-x0, y1-y0, 79, scale, scale, x_shift, 0, text[ch]);
399 xpos += (advance * scale);
401 xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch], text[ch+1]);
405 for (j = 0;
j < 20; ++
j) {
406 for (i = 0;
i < 78; ++
i)
407 putchar(
" .:ioVM@"[screen[j][i]>>5]);
423#ifndef DOXYGEN_SHOULD_SKIP_THIS
425#ifdef STB_TRUETYPE_IMPLEMENTATION
428typedef unsigned char stbtt_uint8;
429typedef signed char stbtt_int8;
430typedef unsigned short stbtt_uint16;
431typedef signed short stbtt_int16;
432typedef unsigned int stbtt_uint32;
433typedef signed int stbtt_int32;
436typedef char stbtt__check_size32[
sizeof(stbtt_int32) == 4 ? 1 : -1];
437typedef char stbtt__check_size16[
sizeof(stbtt_int16) == 2 ? 1 : -1];
442#define STBTT_ifloor(x) ((int)floor(x))
443#define STBTT_iceil(x) ((int)ceil(x))
448#define STBTT_sqrt(x) sqrt(x)
449#define STBTT_pow(x, y) pow(x, y)
454#define STBTT_fmod(x, y) fmod(x, y)
459#define STBTT_cos(x) cos(x)
460#define STBTT_acos(x) acos(x)
465#define STBTT_fabs(x) fabs(x)
471#define STBTT_malloc(x, u) ((void)(u), malloc(x))
472#define STBTT_free(x, u) ((void)(u), free(x))
477#define STBTT_assert(x) assert(x)
482#define STBTT_strlen(x) strlen(x)
487#define STBTT_memcpy memcpy
488#define STBTT_memset memset
499#ifndef __STB_INCLUDE_STB_TRUETYPE_H__
500#define __STB_INCLUDE_STB_TRUETYPE_H__
503#define STBTT_DEF static
505#define STBTT_DEF extern
529 unsigned short x0, y0, x1, y1;
530 float xoff, yoff, xadvance;
533 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
535 unsigned char *pixels,
int pw,
int ph,
536 int first_char,
int num_chars,
537 stbtt_bakedchar *chardata);
545 float x0, y0, s0, t0;
546 float x1, y1, s1,
t1;
547 } stbtt_aligned_quad;
549 STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
551 float *xpos,
float *ypos,
552 stbtt_aligned_quad *q,
553 int opengl_fillrule);
564 STBTT_DEF
void stbtt_GetScaledFontVMetrics(
const unsigned char *fontdata,
int index,
float size,
float *ascent,
565 float *descent,
float *lineGap);
577 unsigned short x0, y0, x1, y1;
578 float xoff, yoff, xadvance;
582 typedef struct stbtt_pack_context stbtt_pack_context;
583 typedef struct stbtt_fontinfo stbtt_fontinfo;
584#ifndef STB_RECT_PACK_VERSION
585 typedef struct stbrp_rect stbrp_rect;
588 STBTT_DEF
int stbtt_PackBegin(stbtt_pack_context *spc,
unsigned char *pixels,
int width,
int height,
589 int stride_in_bytes,
int padding,
void *alloc_context);
600 STBTT_DEF
void stbtt_PackEnd(stbtt_pack_context *spc);
603#define STBTT_POINT_SIZE(x) (-(x))
605 STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
606 float font_size,
int first_unicode_char_in_range,
int num_chars_in_range,
607 stbtt_packedchar *chardata_for_range);
624 int first_unicode_codepoint_in_range;
625 int *array_of_unicode_codepoints;
627 stbtt_packedchar *chardata_for_range;
628 unsigned char h_oversample, v_oversample;
631 STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
632 stbtt_pack_range *ranges,
int num_ranges);
638 STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample);
654 STBTT_DEF
void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc,
int skip);
660 STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
662 float *xpos,
float *ypos,
663 stbtt_aligned_quad *q,
664 int align_to_integer);
666 STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
667 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
668 STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects);
669 STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
670 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
683 struct stbtt_pack_context
685 void *user_allocator_context;
692 unsigned int h_oversample, v_oversample;
693 unsigned char *pixels;
703 STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data);
710 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index);
719 struct stbtt_fontinfo
727 int loca, head, glyf, hhea, hmtx, kern, gpos, svg;
729 int indexToLocFormat;
732 stbtt__buf charstrings;
735 stbtt__buf fontdicts;
739 STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset);
750 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint);
762 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float pixels);
770 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels);
775 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap);
783 STBTT_DEF
int stbtt_GetFontVMetricsOS2(
const stbtt_fontinfo *info,
int *typoAscent,
int *typoDescent,
int *typoLineGap);
789 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1);
792 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
793 int *leftSideBearing);
798 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2);
801 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1);
804 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
805 int *leftSideBearing);
806 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2);
807 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
810 typedef struct stbtt_kerningentry
815 } stbtt_kerningentry;
817 STBTT_DEF
int stbtt_GetKerningTableLength(
const stbtt_fontinfo *info);
818 STBTT_DEF
int stbtt_GetKerningTable(
const stbtt_fontinfo *info, stbtt_kerningentry *table,
int table_length);
830 enum { STBTT_vmove = 1, STBTT_vline, STBTT_vcurve, STBTT_vcubic };
835#define stbtt_vertex_type short
838 stbtt_vertex_type
x,
y, cx, cy, cx1, cy1;
839 unsigned char type, padding;
843 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index);
846 STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices);
847 STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **vertices);
858 STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *vertices);
861 STBTT_DEF
unsigned char *stbtt_FindSVGDoc(
const stbtt_fontinfo *info,
int gl);
862 STBTT_DEF
int stbtt_GetCodepointSVG(
const stbtt_fontinfo *info,
int unicode_codepoint,
const char **svg);
863 STBTT_DEF
int stbtt_GetGlyphSVG(
const stbtt_fontinfo *info,
int gl,
const char **svg);
872 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata);
875 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
876 int codepoint,
int *width,
int *height,
int *xoff,
int *yoff);
885 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
886 float shift_x,
float shift_y,
int codepoint,
int *width,
887 int *height,
int *xoff,
int *yoff);
891 STBTT_DEF
void stbtt_MakeCodepointBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
892 int out_stride,
float scale_x,
float scale_y,
int codepoint);
898 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
899 int out_h,
int out_stride,
float scale_x,
float scale_y,
float shift_x,
900 float shift_y,
int codepoint);
904 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
905 int out_h,
int out_stride,
float scale_x,
float scale_y,
906 float shift_x,
float shift_y,
int oversample_x,
907 int oversample_y,
float *sub_x,
float *sub_y,
int codepoint);
911 STBTT_DEF
void stbtt_GetCodepointBitmapBox(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
float scale_y,
912 int *ix0,
int *iy0,
int *ix1,
int *iy1);
919 STBTT_DEF
void stbtt_GetCodepointBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
920 float scale_y,
float shift_x,
float shift_y,
int *ix0,
int *iy0,
927 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
int glyph,
928 int *width,
int *height,
int *xoff,
int *yoff);
929 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
930 float shift_x,
float shift_y,
int glyph,
int *width,
int *height,
931 int *xoff,
int *yoff);
932 STBTT_DEF
void stbtt_MakeGlyphBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
933 int out_stride,
float scale_x,
float scale_y,
int glyph);
934 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
935 int out_stride,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
937 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
938 int out_h,
int out_stride,
float scale_x,
float scale_y,
939 float shift_x,
float shift_y,
int oversample_x,
int oversample_y,
940 float *sub_x,
float *sub_y,
int glyph);
941 STBTT_DEF
void stbtt_GetGlyphBitmapBox(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
int *ix0,
942 int *iy0,
int *ix1,
int *iy1);
943 STBTT_DEF
void stbtt_GetGlyphBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
944 float shift_x,
float shift_y,
int *ix0,
int *iy0,
int *ix1,
int *iy1);
950 unsigned char *pixels;
954 STBTT_DEF
void stbtt_Rasterize(stbtt__bitmap *result,
955 float flatness_in_pixels,
956 stbtt_vertex *vertices,
958 float scale_x,
float scale_y,
959 float shift_x,
float shift_y,
960 int x_off,
int y_off,
968 STBTT_DEF
void stbtt_FreeSDF(
unsigned char *bitmap,
void *userdata);
971 STBTT_DEF
unsigned char *stbtt_GetGlyphSDF(
const stbtt_fontinfo *info,
float scale,
int glyph,
int padding,
972 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
int *height,
973 int *xoff,
int *yoff);
974 STBTT_DEF
unsigned char *stbtt_GetCodepointSDF(
const stbtt_fontinfo *info,
float scale,
int codepoint,
int padding,
975 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
976 int *height,
int *xoff,
int *yoff);
1046 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags);
1051#define STBTT_MACSTYLE_DONTCARE 0
1052#define STBTT_MACSTYLE_BOLD 1
1053#define STBTT_MACSTYLE_ITALIC 2
1054#define STBTT_MACSTYLE_UNDERSCORE 4
1055#define STBTT_MACSTYLE_NONE 8
1057 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2);
1061 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
1062 int languageID,
int nameID);
1072 STBTT_PLATFORM_ID_UNICODE = 0,
1073 STBTT_PLATFORM_ID_MAC = 1,
1074 STBTT_PLATFORM_ID_ISO = 2,
1075 STBTT_PLATFORM_ID_MICROSOFT = 3
1080 STBTT_UNICODE_EID_UNICODE_1_0 = 0,
1081 STBTT_UNICODE_EID_UNICODE_1_1 = 1,
1082 STBTT_UNICODE_EID_ISO_10646 = 2,
1083 STBTT_UNICODE_EID_UNICODE_2_0_BMP = 3,
1084 STBTT_UNICODE_EID_UNICODE_2_0_FULL = 4
1089 STBTT_MS_EID_SYMBOL = 0,
1090 STBTT_MS_EID_UNICODE_BMP = 1,
1091 STBTT_MS_EID_SHIFTJIS = 2,
1092 STBTT_MS_EID_UNICODE_FULL = 10
1097 STBTT_MAC_EID_ROMAN = 0,
1098 STBTT_MAC_EID_ARABIC = 4,
1099 STBTT_MAC_EID_JAPANESE = 1,
1100 STBTT_MAC_EID_HEBREW = 5,
1101 STBTT_MAC_EID_CHINESE_TRAD = 2,
1102 STBTT_MAC_EID_GREEK = 6,
1103 STBTT_MAC_EID_KOREAN = 3,
1104 STBTT_MAC_EID_RUSSIAN = 7
1110 STBTT_MS_LANG_ENGLISH = 0x0409,
1111 STBTT_MS_LANG_ITALIAN = 0x0410,
1112 STBTT_MS_LANG_CHINESE = 0x0804,
1113 STBTT_MS_LANG_JAPANESE = 0x0411,
1114 STBTT_MS_LANG_DUTCH = 0x0413,
1115 STBTT_MS_LANG_KOREAN = 0x0412,
1116 STBTT_MS_LANG_FRENCH = 0x040c,
1117 STBTT_MS_LANG_RUSSIAN = 0x0419,
1118 STBTT_MS_LANG_GERMAN = 0x0407,
1119 STBTT_MS_LANG_SPANISH = 0x0409,
1120 STBTT_MS_LANG_HEBREW = 0x040d,
1121 STBTT_MS_LANG_SWEDISH = 0x041D
1126 STBTT_MAC_LANG_ENGLISH = 0,
1127 STBTT_MAC_LANG_JAPANESE = 11,
1128 STBTT_MAC_LANG_ARABIC = 12,
1129 STBTT_MAC_LANG_KOREAN = 23,
1130 STBTT_MAC_LANG_DUTCH = 4,
1131 STBTT_MAC_LANG_RUSSIAN = 32,
1132 STBTT_MAC_LANG_FRENCH = 1,
1133 STBTT_MAC_LANG_SPANISH = 6,
1134 STBTT_MAC_LANG_GERMAN = 2,
1135 STBTT_MAC_LANG_SWEDISH = 5,
1136 STBTT_MAC_LANG_HEBREW = 10,
1137 STBTT_MAC_LANG_CHINESE_SIMPLIFIED = 33,
1138 STBTT_MAC_LANG_ITALIAN = 3,
1139 STBTT_MAC_LANG_CHINESE_TRAD = 19
1155#ifdef STB_TRUETYPE_IMPLEMENTATION
1157#ifndef STBTT_MAX_OVERSAMPLE
1158#define STBTT_MAX_OVERSAMPLE 8
1161#if STBTT_MAX_OVERSAMPLE > 255
1162#error "STBTT_MAX_OVERSAMPLE cannot be > 255"
1165typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE - 1)) == 0 ? 1 : -1];
1167#ifndef STBTT_RASTERIZER_VERSION
1168#define STBTT_RASTERIZER_VERSION 2
1172#define STBTT__NOTUSED(v) (void)(v)
1174#define STBTT__NOTUSED(v) (void)sizeof(v)
1182static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)
1184 if (b->cursor >= b->size)
1186 return b->data[b->cursor++];
1189static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)
1191 if (b->cursor >= b->size)
1193 return b->data[b->cursor];
1196static void stbtt__buf_seek(stbtt__buf *b,
int o)
1198 STBTT_assert(!(o > b->size || o < 0));
1199 b->cursor = (o > b->size || o < 0) ? b->size : o;
1202static void stbtt__buf_skip(stbtt__buf *b,
int o) { stbtt__buf_seek(b, b->cursor + o); }
1204static stbtt_uint32 stbtt__buf_get(stbtt__buf *b,
int n)
1208 STBTT_assert(n >= 1 && n <= 4);
1209 for (i = 0;
i < n;
i++)
1210 v = (v << 8) | stbtt__buf_get8(b);
1214static stbtt__buf stbtt__new_buf(
const void *p,
size_t size)
1217 STBTT_assert(size < 0x40000000);
1218 r.data = (stbtt_uint8 *)p;
1224#define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
1225#define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
1227static stbtt__buf stbtt__buf_range(
const stbtt__buf *b,
int o,
int s)
1229 stbtt__buf
r = stbtt__new_buf(
nullptr, 0);
1230 if (o < 0 || s < 0 || o > b->size || s > b->size - o)
1232 r.data = b->data + o;
1237static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)
1239 int count,
start, offsize;
1241 count = stbtt__buf_get16(b);
1243 offsize = stbtt__buf_get8(b);
1244 STBTT_assert(offsize >= 1 && offsize <= 4);
1245 stbtt__buf_skip(b, offsize * count);
1246 stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
1248 return stbtt__buf_range(b, start, b->cursor - start);
1251static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)
1253 int b0 = stbtt__buf_get8(b);
1254 if (b0 >= 32 && b0 <= 246)
1256 else if (b0 >= 247 && b0 <= 250)
1257 return (b0 - 247) * 256 + stbtt__buf_get8(b) + 108;
1258 else if (b0 >= 251 && b0 <= 254)
1259 return -(b0 - 251) * 256 - stbtt__buf_get8(b) - 108;
1261 return stbtt__buf_get16(b);
1263 return stbtt__buf_get32(b);
1268static void stbtt__cff_skip_operand(stbtt__buf *b)
1270 int v, b0 = stbtt__buf_peek8(b);
1271 STBTT_assert(b0 >= 28);
1273 stbtt__buf_skip(b, 1);
1274 while (b->cursor < b->size) {
1275 v = stbtt__buf_get8(b);
1276 if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
1285static stbtt__buf stbtt__dict_get(stbtt__buf *b,
int key)
1287 stbtt__buf_seek(b, 0);
1288 while (b->cursor < b->size) {
1289 int start = b->cursor, end, op;
1290 while (stbtt__buf_peek8(b) >= 28)
1291 stbtt__cff_skip_operand(b);
1293 op = stbtt__buf_get8(b);
1295 op = stbtt__buf_get8(b) | 0x100;
1297 return stbtt__buf_range(b, start, end - start);
1299 return stbtt__buf_range(b, 0, 0);
1302static void stbtt__dict_get_ints(stbtt__buf *b,
int key,
int outcount, stbtt_uint32 *out)
1305 stbtt__buf operands = stbtt__dict_get(b, key);
1306 for (i = 0;
i < outcount && operands.cursor < operands.size;
i++)
1307 out[i] = stbtt__cff_int(&operands);
1310static int stbtt__cff_index_count(stbtt__buf *b)
1312 stbtt__buf_seek(b, 0);
1313 return stbtt__buf_get16(b);
1316static stbtt__buf stbtt__cff_index_get(stbtt__buf b,
int i)
1318 int count, offsize,
start, end;
1319 stbtt__buf_seek(&b, 0);
1320 count = stbtt__buf_get16(&b);
1321 offsize = stbtt__buf_get8(&b);
1322 STBTT_assert(i >= 0 && i < count);
1323 STBTT_assert(offsize >= 1 && offsize <= 4);
1324 stbtt__buf_skip(&b, i * offsize);
1325 start = stbtt__buf_get(&b, offsize);
1326 end = stbtt__buf_get(&b, offsize);
1327 return stbtt__buf_range(&b, 2 + (count + 1) * offsize + start, end - start);
1338#define ttBYTE(p) (*(stbtt_uint8 *)(p))
1339#define ttCHAR(p) (*(stbtt_int8 *)(p))
1340#define ttFixed(p) ttLONG(p)
1342static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) {
return p[0] * 256 +
p[1]; }
1343static stbtt_int16 ttSHORT(stbtt_uint8 *p) {
return p[0] * 256 +
p[1]; }
1344static stbtt_uint32 ttULONG(stbtt_uint8 *p) {
return (p[0] << 24) + (
p[1] << 16) + (p[2] << 8) +
p[3]; }
1345static stbtt_int32 ttLONG(stbtt_uint8 *p) {
return (p[0] << 24) + (
p[1] << 16) + (p[2] << 8) +
p[3]; }
1347#define stbtt_tag4(p, c0, c1, c2, c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
1348#define stbtt_tag(p, str) stbtt_tag4(p, str[0], str[1], str[2], str[3])
1350static int stbtt__isfont(stbtt_uint8 *font)
1353 if (stbtt_tag4(font,
'1', 0, 0, 0))
1355 if (stbtt_tag(font,
"typ1"))
1357 if (stbtt_tag(font,
"OTTO"))
1359 if (stbtt_tag4(font, 0, 1, 0, 0))
1361 if (stbtt_tag(font,
"true"))
1367static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart,
const char *tag)
1369 stbtt_int32 num_tables = ttUSHORT(data + fontstart + 4);
1370 stbtt_uint32 tabledir = fontstart + 12;
1372 for (i = 0;
i < num_tables; ++
i) {
1373 stbtt_uint32 loc = tabledir + 16 *
i;
1374 if (stbtt_tag(data + loc + 0, tag))
1375 return ttULONG(data + loc + 8);
1380static int stbtt_GetFontOffsetForIndex_internal(
unsigned char *font_collection,
int index)
1383 if (stbtt__isfont(font_collection))
1384 return index == 0 ? 0 : -1;
1387 if (stbtt_tag(font_collection,
"ttcf")) {
1389 if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
1390 stbtt_int32 n = ttLONG(font_collection + 8);
1393 return ttULONG(font_collection + 12 + index * 4);
1399static int stbtt_GetNumberOfFonts_internal(
unsigned char *font_collection)
1402 if (stbtt__isfont(font_collection))
1406 if (stbtt_tag(font_collection,
"ttcf")) {
1408 if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
1409 return ttLONG(font_collection + 8);
1415static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)
1417 stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
1419 stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
1420 if (!private_loc[1] || !private_loc[0])
1421 return stbtt__new_buf(
nullptr, 0);
1422 pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
1423 stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
1425 return stbtt__new_buf(
nullptr, 0);
1426 stbtt__buf_seek(&cff, private_loc[1] + subrsoff);
1427 return stbtt__cff_get_index(&cff);
1431static int stbtt__get_svg(stbtt_fontinfo *info)
1434 if (info->svg < 0) {
1435 t = stbtt__find_table(info->data, info->fontstart,
"SVG ");
1437 stbtt_uint32 offset = ttULONG(info->data + t + 2);
1438 info->svg =
t + offset;
1447static int stbtt_InitFont_internal(stbtt_fontinfo *info,
unsigned char *data,
int fontstart)
1449 stbtt_uint32 cmap,
t;
1450 stbtt_int32
i, numTables;
1453 info->fontstart = fontstart;
1454 info->cff = stbtt__new_buf(
nullptr, 0);
1456 cmap = stbtt__find_table(data, fontstart,
"cmap");
1457 info->loca = stbtt__find_table(data, fontstart,
"loca");
1458 info->head = stbtt__find_table(data, fontstart,
"head");
1459 info->glyf = stbtt__find_table(data, fontstart,
"glyf");
1460 info->hhea = stbtt__find_table(data, fontstart,
"hhea");
1461 info->hmtx = stbtt__find_table(data, fontstart,
"hmtx");
1462 info->kern = stbtt__find_table(data, fontstart,
"kern");
1463 info->gpos = stbtt__find_table(data, fontstart,
"GPOS");
1465 if (!cmap || !info->head || !info->hhea || !info->hmtx)
1474 stbtt__buf b, topdict, topdictidx;
1475 stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
1478 cff = stbtt__find_table(data, fontstart,
"CFF ");
1482 info->fontdicts = stbtt__new_buf(
nullptr, 0);
1483 info->fdselect = stbtt__new_buf(
nullptr, 0);
1486 info->cff = stbtt__new_buf(data + cff, 512 * 1024 * 1024);
1490 stbtt__buf_skip(&b, 2);
1491 stbtt__buf_seek(&b, stbtt__buf_get8(&b));
1495 stbtt__cff_get_index(&b);
1496 topdictidx = stbtt__cff_get_index(&b);
1497 topdict = stbtt__cff_index_get(topdictidx, 0);
1498 stbtt__cff_get_index(&b);
1499 info->gsubrs = stbtt__cff_get_index(&b);
1501 stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
1502 stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
1503 stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
1504 stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
1505 info->subrs = stbtt__get_subrs(b, topdict);
1510 if (charstrings == 0)
1517 stbtt__buf_seek(&b, fdarrayoff);
1518 info->fontdicts = stbtt__cff_get_index(&b);
1519 info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size - fdselectoff);
1522 stbtt__buf_seek(&b, charstrings);
1523 info->charstrings = stbtt__cff_get_index(&b);
1526 t = stbtt__find_table(data, fontstart,
"maxp");
1528 info->numGlyphs = ttUSHORT(data + t + 4);
1530 info->numGlyphs = 0xffff;
1537 numTables = ttUSHORT(data + cmap + 2);
1538 info->index_map = 0;
1539 for (i = 0;
i < numTables; ++
i) {
1540 stbtt_uint32 encoding_record = cmap + 4 + 8 *
i;
1542 switch (ttUSHORT(data + encoding_record)) {
1543 case STBTT_PLATFORM_ID_MICROSOFT:
1544 switch (ttUSHORT(data + encoding_record + 2)) {
1545 case STBTT_MS_EID_UNICODE_BMP:
1546 case STBTT_MS_EID_UNICODE_FULL:
1548 info->index_map = cmap + ttULONG(data + encoding_record + 4);
1552 case STBTT_PLATFORM_ID_UNICODE:
1555 info->index_map = cmap + ttULONG(data + encoding_record + 4);
1559 if (info->index_map == 0)
1562 info->indexToLocFormat = ttUSHORT(data + info->head + 50);
1566STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint)
1568 stbtt_uint8 *
data = info->data;
1569 stbtt_uint32 index_map = info->index_map;
1571 stbtt_uint16
format = ttUSHORT(data + index_map + 0);
1573 stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1574 if (unicode_codepoint < bytes - 6)
1575 return ttBYTE(data + index_map + 6 + unicode_codepoint);
1578 else if (format == 6) {
1579 stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1580 stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1581 if ((stbtt_uint32)unicode_codepoint >= first && (stbtt_uint32)unicode_codepoint < first + count)
1582 return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first) * 2);
1585 else if (format == 2) {
1589 else if (format == 4) {
1590 stbtt_uint16 segcount = ttUSHORT(data + index_map + 6) >> 1;
1591 stbtt_uint16 searchRange = ttUSHORT(data + index_map + 8) >> 1;
1592 stbtt_uint16 entrySelector = ttUSHORT(data + index_map + 10);
1593 stbtt_uint16 rangeShift = ttUSHORT(data + index_map + 12) >> 1;
1596 stbtt_uint32 endCount = index_map + 14;
1597 stbtt_uint32 search = endCount;
1599 if (unicode_codepoint > 0xffff)
1604 if (unicode_codepoint >= ttUSHORT(data + search + rangeShift * 2))
1605 search += rangeShift * 2;
1609 while (entrySelector) {
1612 end = ttUSHORT(data + search + searchRange * 2);
1613 if (unicode_codepoint > end)
1614 search += searchRange * 2;
1620 stbtt_uint16 offset,
start, last;
1621 stbtt_uint16 item = (stbtt_uint16)((search - endCount) >> 1);
1623 start = ttUSHORT(data + index_map + 14 + segcount * 2 + 2 + 2 * item);
1624 last = ttUSHORT(data + endCount + 2 * item);
1625 if (unicode_codepoint < start || unicode_codepoint > last)
1628 offset = ttUSHORT(data + index_map + 14 + segcount * 6 + 2 + 2 * item);
1630 return (stbtt_uint16)(unicode_codepoint + ttSHORT(data + index_map + 14 + segcount * 4 + 2 + 2 * item));
1632 return ttUSHORT(data + offset + (unicode_codepoint - start) * 2 + index_map + 14 + segcount * 6 + 2 + 2 * item);
1635 else if (format == 12 || format == 13) {
1636 stbtt_uint32 ngroups = ttULONG(data + index_map + 12);
1637 stbtt_int32 low, high;
1639 high = (stbtt_int32)ngroups;
1641 while (low < high) {
1642 stbtt_int32 mid = low + ((high - low) >> 1);
1643 stbtt_uint32 start_char = ttULONG(data + index_map + 16 + mid * 12);
1644 stbtt_uint32 end_char = ttULONG(data + index_map + 16 + mid * 12 + 4);
1645 if ((stbtt_uint32)unicode_codepoint < start_char)
1647 else if ((stbtt_uint32)unicode_codepoint > end_char)
1650 stbtt_uint32 start_glyph = ttULONG(data + index_map + 16 + mid * 12 + 8);
1652 return start_glyph + unicode_codepoint - start_char;
1664STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices)
1666 return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
1669static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx,
1673 v->x = (stbtt_int16)x;
1674 v->y = (stbtt_int16)y;
1675 v->cx = (stbtt_int16)cx;
1676 v->cy = (stbtt_int16)cy;
1679static int stbtt__GetGlyfOffset(
const stbtt_fontinfo *info,
int glyph_index)
1683 STBTT_assert(!info->cff.size);
1685 if (glyph_index >= info->numGlyphs)
1687 if (info->indexToLocFormat >= 2)
1690 if (info->indexToLocFormat == 0) {
1691 g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
1692 g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
1695 g1 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4);
1696 g2 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4 + 4);
1699 return g1 == g2 ? -1 : g1;
1702static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
1704STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
1706 if (info->cff.size) {
1707 stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
1710 int g = stbtt__GetGlyfOffset(info, glyph_index);
1715 *x0 = ttSHORT(info->data + g + 2);
1717 *y0 = ttSHORT(info->data + g + 4);
1719 *x1 = ttSHORT(info->data + g + 6);
1721 *y1 = ttSHORT(info->data + g + 8);
1726STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1)
1728 return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info, codepoint), x0, y0, x1, y1);
1731STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index)
1733 stbtt_int16 numberOfContours;
1736 return stbtt__GetGlyphInfoT2(info, glyph_index,
nullptr,
nullptr,
nullptr,
nullptr) == 0;
1737 g = stbtt__GetGlyfOffset(info, glyph_index);
1740 numberOfContours = ttSHORT(info->data + g);
1741 return numberOfContours == 0;
1744static int stbtt__close_shape(stbtt_vertex *vertices,
int num_vertices,
int was_off,
int start_off, stbtt_int32 sx,
1745 stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1749 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + scx) >> 1, (cy + scy) >> 1, cx, cy);
1750 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, scx, scy);
1754 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, cx, cy);
1756 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, sx, sy, 0, 0);
1758 return num_vertices;
1761static int stbtt__GetGlyphShapeTT(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
1763 stbtt_int16 numberOfContours;
1764 stbtt_uint8 *endPtsOfContours;
1765 stbtt_uint8 *
data = info->data;
1766 stbtt_vertex *vertices = 0;
1767 int num_vertices = 0;
1768 int g = stbtt__GetGlyfOffset(info, glyph_index);
1770 *pvertices =
nullptr;
1775 numberOfContours = ttSHORT(data + g);
1777 if (numberOfContours > 0) {
1778 stbtt_uint8 flags = 0, flagcount;
1779 stbtt_int32 ins,
i,
j = 0, m, n, next_move, was_off = 0, off, start_off = 0;
1780 stbtt_int32
x,
y, cx, cy, sx, sy, scx, scy;
1781 stbtt_uint8 *points;
1782 endPtsOfContours = (
data + g + 10);
1783 ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1784 points =
data + g + 10 + numberOfContours * 2 + 2 + ins;
1786 n = 1 + ttUSHORT(endPtsOfContours + numberOfContours * 2 - 2);
1788 m = n + 2 * numberOfContours;
1789 vertices = (stbtt_vertex *)STBTT_malloc(m *
sizeof(vertices[0]), info->userdata);
1804 for (i = 0;
i < n; ++
i) {
1805 if (flagcount == 0) {
1808 flagcount = *points++;
1812 vertices[off +
i].type = flags;
1817 for (i = 0;
i < n; ++
i) {
1818 flags = vertices[off +
i].type;
1820 stbtt_int16 dx = *points++;
1821 x += (flags & 16) ? dx : -dx;
1824 if (!(flags & 16)) {
1825 x =
x + (stbtt_int16)(points[0] * 256 + points[1]);
1829 vertices[off +
i].x = (stbtt_int16)x;
1834 for (i = 0;
i < n; ++
i) {
1835 flags = vertices[off +
i].type;
1837 stbtt_int16 dy = *points++;
1838 y += (flags & 32) ? dy : -dy;
1841 if (!(flags & 32)) {
1842 y =
y + (stbtt_int16)(points[0] * 256 + points[1]);
1846 vertices[off +
i].y = (stbtt_int16)y;
1851 sx = sy = cx = cy = scx = scy = 0;
1852 for (i = 0;
i < n; ++
i) {
1853 flags = vertices[off +
i].type;
1854 x = (stbtt_int16)vertices[off + i].x;
1855 y = (stbtt_int16)vertices[off + i].y;
1857 if (next_move == i) {
1859 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
1862 start_off = !(flags & 1);
1868 if (!(vertices[off + i + 1].type & 1)) {
1870 sx = (
x + (stbtt_int32)vertices[off +
i + 1].x) >> 1;
1871 sy = (
y + (stbtt_int32)vertices[off +
i + 1].y) >> 1;
1875 sx = (stbtt_int32)vertices[off + i + 1].x;
1876 sy = (stbtt_int32)vertices[off + i + 1].y;
1884 stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove, sx, sy, 0, 0);
1886 next_move = 1 + ttUSHORT(endPtsOfContours + j * 2);
1892 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + x) >> 1, (cy + y) >> 1, cx, cy);
1899 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x, y, cx, cy);
1901 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x, y, 0, 0);
1906 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
1908 else if (numberOfContours < 0) {
1911 stbtt_uint8 *comp =
data + g + 10;
1915 stbtt_uint16 flags, gidx;
1916 int comp_num_verts = 0,
i;
1917 stbtt_vertex *comp_verts = 0, *tmp = 0;
1918 float mtx[6] = { 1, 0, 0, 1, 0, 0 }, m, n;
1920 flags = ttSHORT(comp);
1922 gidx = ttSHORT(comp);
1927 mtx[4] = ttSHORT(comp);
1929 mtx[5] = ttSHORT(comp);
1933 mtx[4] = ttCHAR(comp);
1935 mtx[5] = ttCHAR(comp);
1943 if (flags & (1 << 3)) {
1944 mtx[0] = mtx[3] = ttSHORT(comp) / 16384.0f;
1946 mtx[1] = mtx[2] = 0;
1948 else if (flags & (1 << 6)) {
1949 mtx[0] = ttSHORT(comp) / 16384.0f;
1951 mtx[1] = mtx[2] = 0;
1952 mtx[3] = ttSHORT(comp) / 16384.0f;
1955 else if (flags & (1 << 7)) {
1956 mtx[0] = ttSHORT(comp) / 16384.0f;
1958 mtx[1] = ttSHORT(comp) / 16384.0f;
1960 mtx[2] = ttSHORT(comp) / 16384.0f;
1962 mtx[3] = ttSHORT(comp) / 16384.0f;
1967 m = (float)STBTT_sqrt(mtx[0] * mtx[0] + mtx[1] * mtx[1]);
1968 n = (float)STBTT_sqrt(mtx[2] * mtx[2] + mtx[3] * mtx[3]);
1971 comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
1972 if (comp_num_verts > 0) {
1974 for (i = 0;
i < comp_num_verts; ++
i) {
1975 stbtt_vertex *
v = &comp_verts[
i];
1976 stbtt_vertex_type
x,
y;
1979 v->x = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
1980 v->y = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
1983 v->cx = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
1984 v->cy = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
1987 tmp = (stbtt_vertex *)STBTT_malloc((num_vertices + comp_num_verts) *
sizeof(stbtt_vertex), info->userdata);
1990 STBTT_free(vertices, info->userdata);
1992 STBTT_free(comp_verts, info->userdata);
1995 if (num_vertices > 0 && vertices)
1996 STBTT_memcpy(tmp, vertices, num_vertices *
sizeof(stbtt_vertex));
1997 STBTT_memcpy(tmp + num_vertices, comp_verts, comp_num_verts *
sizeof(stbtt_vertex));
1999 STBTT_free(vertices, info->userdata);
2001 STBTT_free(comp_verts, info->userdata);
2002 num_vertices += comp_num_verts;
2005 more = flags & (1 << 5);
2012 *pvertices = vertices;
2013 return num_vertices;
2020 float first_x, first_y;
2022 stbtt_int32 min_x, max_x, min_y, max_y;
2024 stbtt_vertex *pvertices;
2028#define STBTT__CSCTX_INIT(bounds) \
2030 bounds, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr, 0 \
2033static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)
2035 if (x > c->max_x || !c->started)
2037 if (y > c->max_y || !c->started)
2039 if (x < c->min_x || !c->started)
2041 if (y < c->min_y || !c->started)
2046static void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx,
2047 stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)
2050 stbtt__track_vertex(c, x, y);
2051 if (type == STBTT_vcubic) {
2052 stbtt__track_vertex(c, cx, cy);
2053 stbtt__track_vertex(c, cx1, cy1);
2057 stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
2058 c->pvertices[c->num_vertices].cx1 = (stbtt_int16)cx1;
2059 c->pvertices[c->num_vertices].cy1 = (stbtt_int16)cy1;
2064static void stbtt__csctx_close_shape(stbtt__csctx *ctx)
2066 if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
2067 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->first_x, (
int)ctx->first_y, 0, 0, 0, 0);
2070static void stbtt__csctx_rmove_to(stbtt__csctx *ctx,
float dx,
float dy)
2072 stbtt__csctx_close_shape(ctx);
2073 ctx->first_x = ctx->x = ctx->x + dx;
2074 ctx->first_y = ctx->y = ctx->y + dy;
2075 stbtt__csctx_v(ctx, STBTT_vmove, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
2078static void stbtt__csctx_rline_to(stbtt__csctx *ctx,
float dx,
float dy)
2082 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
2085static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx,
float dx1,
float dy1,
float dx2,
float dy2,
float dx3,
float dy3)
2087 float cx1 = ctx->x + dx1;
2088 float cy1 = ctx->y + dy1;
2089 float cx2 = cx1 + dx2;
2090 float cy2 = cy1 + dy2;
2093 stbtt__csctx_v(ctx, STBTT_vcubic, (
int)ctx->x, (
int)ctx->y, (
int)cx1, (
int)cy1, (
int)cx2, (
int)cy2);
2096static stbtt__buf stbtt__get_subr(stbtt__buf idx,
int n)
2098 int count = stbtt__cff_index_count(&idx);
2102 else if (count >= 1240)
2105 if (n < 0 || n >= count)
2106 return stbtt__new_buf(
nullptr, 0);
2107 return stbtt__cff_index_get(idx, n);
2110static stbtt__buf stbtt__cid_get_glyph_subrs(
const stbtt_fontinfo *info,
int glyph_index)
2112 stbtt__buf fdselect = info->fdselect;
2113 int nranges,
start, end,
v, fmt, fdselector = -1,
i;
2115 stbtt__buf_seek(&fdselect, 0);
2116 fmt = stbtt__buf_get8(&fdselect);
2119 stbtt__buf_skip(&fdselect, glyph_index);
2120 fdselector = stbtt__buf_get8(&fdselect);
2122 else if (fmt == 3) {
2123 nranges = stbtt__buf_get16(&fdselect);
2124 start = stbtt__buf_get16(&fdselect);
2125 for (i = 0;
i < nranges;
i++) {
2126 v = stbtt__buf_get8(&fdselect);
2127 end = stbtt__buf_get16(&fdselect);
2128 if (glyph_index >= start && glyph_index < end) {
2135 if (fdselector == -1)
2136 stbtt__new_buf(
nullptr, 0);
2137 return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
2140static int stbtt__run_charstring(
const stbtt_fontinfo *info,
int glyph_index, stbtt__csctx *c)
2142 int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0,
v,
i, b0;
2143 int has_subrs = 0, clear_stack;
2145 stbtt__buf subr_stack[10], subrs = info->subrs, b;
2148#define STBTT__CSERR(s) (0)
2151 b = stbtt__cff_index_get(info->charstrings, glyph_index);
2152 while (b.cursor < b.size) {
2155 b0 = stbtt__buf_get8(&b);
2161 maskbits += (sp / 2);
2163 stbtt__buf_skip(&b, (maskbits + 7) / 8);
2170 maskbits += (sp / 2);
2176 return STBTT__CSERR(
"rmoveto stack");
2177 stbtt__csctx_rmove_to(c, s[sp - 2], s[sp - 1]);
2182 return STBTT__CSERR(
"vmoveto stack");
2183 stbtt__csctx_rmove_to(c, 0, s[sp - 1]);
2188 return STBTT__CSERR(
"hmoveto stack");
2189 stbtt__csctx_rmove_to(c, s[sp - 1], 0);
2194 return STBTT__CSERR(
"rlineto stack");
2195 for (;
i + 1 < sp;
i += 2)
2196 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2204 return STBTT__CSERR(
"vlineto stack");
2208 return STBTT__CSERR(
"hlineto stack");
2212 stbtt__csctx_rline_to(c, s[i], 0);
2217 stbtt__csctx_rline_to(c, 0, s[i]);
2224 return STBTT__CSERR(
"hvcurveto stack");
2228 return STBTT__CSERR(
"vhcurveto stack");
2232 stbtt__csctx_rccurve_to(c, 0, s[i], s[i + 1], s[i + 2], s[i + 3], (sp - i == 5) ? s[i + 4] : 0.0f);
2237 stbtt__csctx_rccurve_to(c, s[i], 0, s[i + 1], s[i + 2], (sp - i == 5) ? s[i + 4] : 0.0f, s[i + 3]);
2244 return STBTT__CSERR(
"rcurveline stack");
2245 for (;
i + 5 < sp;
i += 6)
2246 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2251 return STBTT__CSERR(
"rcurveline stack");
2252 for (;
i + 5 < sp - 2;
i += 6)
2253 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2255 return STBTT__CSERR(
"rcurveline stack");
2256 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2261 return STBTT__CSERR(
"rlinecurve stack");
2262 for (;
i + 1 < sp - 6;
i += 2)
2263 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2265 return STBTT__CSERR(
"rlinecurve stack");
2266 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2272 return STBTT__CSERR(
"(vv|hh)curveto stack");
2278 for (;
i + 3 < sp;
i += 4) {
2280 stbtt__csctx_rccurve_to(c, s[i], f, s[i + 1], s[i + 2], s[i + 3], 0.0);
2282 stbtt__csctx_rccurve_to(c, f, s[i], s[i + 1], s[i + 2], 0.0, s[i + 3]);
2289 if (info->fdselect.size)
2290 subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
2296 return STBTT__CSERR(
"call(g|)subr stack");
2298 if (subr_stack_height >= 10)
2299 return STBTT__CSERR(
"recursion limit");
2300 subr_stack[subr_stack_height++] = b;
2301 b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);
2303 return STBTT__CSERR(
"subr not found");
2309 if (subr_stack_height <= 0)
2310 return STBTT__CSERR(
"return outside subr");
2311 b = subr_stack[--subr_stack_height];
2316 stbtt__csctx_close_shape(c);
2320 float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
2322 int b1 = stbtt__buf_get8(&b);
2328 return STBTT__CSERR(
"hflex stack");
2336 stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
2337 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
2342 return STBTT__CSERR(
"flex stack");
2356 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2357 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2362 return STBTT__CSERR(
"hflex1 stack");
2372 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
2373 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1 + dy2 + dy5));
2378 return STBTT__CSERR(
"flex1 stack");
2390 dx = dx1 + dx2 + dx3 + dx4 + dx5;
2391 dy = dy1 + dy2 + dy3 + dy4 + dy5;
2392 if (STBTT_fabs(dx) > STBTT_fabs(dy))
2396 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2397 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2401 return STBTT__CSERR(
"unimplemented");
2406 if (b0 != 255 && b0 != 28 && b0 < 32)
2407 return STBTT__CSERR(
"reserved operator");
2411 f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000;
2414 stbtt__buf_skip(&b, -1);
2415 f = (float)(stbtt_int16)stbtt__cff_int(&b);
2418 return STBTT__CSERR(
"push stack overflow");
2426 return STBTT__CSERR(
"no endchar");
2431static int stbtt__GetGlyphShapeT2(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2434 stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
2435 stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
2436 if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
2437 *pvertices = (stbtt_vertex *)STBTT_malloc(count_ctx.num_vertices *
sizeof(stbtt_vertex), info->userdata);
2438 output_ctx.pvertices = *pvertices;
2439 if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
2440 STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
2441 return output_ctx.num_vertices;
2444 *pvertices =
nullptr;
2448static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
2450 stbtt__csctx c = STBTT__CSCTX_INIT(1);
2451 int r = stbtt__run_charstring(info, glyph_index, &c);
2453 *x0 =
r ? c.min_x : 0;
2455 *y0 =
r ? c.min_y : 0;
2457 *x1 =
r ? c.max_x : 0;
2459 *y1 =
r ? c.max_y : 0;
2460 return r ? c.num_vertices : 0;
2463STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2465 if (!info->cff.size)
2466 return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
2468 return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
2471STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
2472 int *leftSideBearing)
2474 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data + info->hhea + 34);
2475 if (glyph_index < numOfLongHorMetrics) {
2477 *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * glyph_index);
2478 if (leftSideBearing)
2479 *leftSideBearing = ttSHORT(info->data + info->hmtx + 4 * glyph_index + 2);
2483 *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * (numOfLongHorMetrics - 1));
2484 if (leftSideBearing)
2486 ttSHORT(info->data + info->hmtx + 4 * numOfLongHorMetrics + 2 * (glyph_index - numOfLongHorMetrics));
2490STBTT_DEF
int stbtt_GetKerningTableLength(
const stbtt_fontinfo *info)
2492 stbtt_uint8 *
data = info->data + info->kern;
2497 if (ttUSHORT(data + 2) < 1)
2499 if (ttUSHORT(data + 8) != 1)
2502 return ttUSHORT(data + 10);
2505STBTT_DEF
int stbtt_GetKerningTable(
const stbtt_fontinfo *info, stbtt_kerningentry *table,
int table_length)
2507 stbtt_uint8 *
data = info->data + info->kern;
2513 if (ttUSHORT(data + 2) < 1)
2515 if (ttUSHORT(data + 8) != 1)
2518 length = ttUSHORT(data + 10);
2519 if (table_length < length)
2520 length = table_length;
2522 for (k = 0; k < length; k++) {
2523 table[k].glyph1 = ttUSHORT(data + 18 + (k * 6));
2524 table[k].glyph2 = ttUSHORT(data + 20 + (k * 6));
2525 table[k].advance = ttSHORT(data + 22 + (k * 6));
2531static int stbtt__GetGlyphKernInfoAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
2533 stbtt_uint8 *
data = info->data + info->kern;
2534 stbtt_uint32 needle, straw;
2540 if (ttUSHORT(data + 2) < 1)
2542 if (ttUSHORT(data + 8) != 1)
2546 r = ttUSHORT(data + 10) - 1;
2547 needle = glyph1 << 16 | glyph2;
2550 straw = ttULONG(data + 18 + (m * 6));
2553 else if (needle > straw)
2556 return ttSHORT(data + 22 + (m * 6));
2561static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable,
int glyph)
2563 stbtt_uint16 coverageFormat = ttUSHORT(coverageTable);
2564 switch (coverageFormat) {
2566 stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2);
2569 stbtt_int32 l = 0,
r = glyphCount - 1, m;
2570 int straw, needle = glyph;
2572 stbtt_uint8 *glyphArray = coverageTable + 4;
2573 stbtt_uint16 glyphID;
2575 glyphID = ttUSHORT(glyphArray + 2 * m);
2579 else if (needle > straw)
2589 stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2);
2590 stbtt_uint8 *rangeArray = coverageTable + 4;
2593 stbtt_int32 l = 0,
r = rangeCount - 1, m;
2594 int strawStart, strawEnd, needle = glyph;
2596 stbtt_uint8 *rangeRecord;
2598 rangeRecord = rangeArray + 6 * m;
2599 strawStart = ttUSHORT(rangeRecord);
2600 strawEnd = ttUSHORT(rangeRecord + 2);
2601 if (needle < strawStart)
2603 else if (needle > strawEnd)
2606 stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4);
2607 return startCoverageIndex + glyph - strawStart;
2620static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable,
int glyph)
2622 stbtt_uint16 classDefFormat = ttUSHORT(classDefTable);
2623 switch (classDefFormat) {
2625 stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2);
2626 stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4);
2627 stbtt_uint8 *classDef1ValueArray = classDefTable + 6;
2629 if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)
2630 return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));
2635 stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2);
2636 stbtt_uint8 *classRangeRecords = classDefTable + 4;
2639 stbtt_int32 l = 0,
r = classRangeCount - 1, m;
2640 int strawStart, strawEnd, needle = glyph;
2642 stbtt_uint8 *classRangeRecord;
2644 classRangeRecord = classRangeRecords + 6 * m;
2645 strawStart = ttUSHORT(classRangeRecord);
2646 strawEnd = ttUSHORT(classRangeRecord + 2);
2647 if (needle < strawStart)
2649 else if (needle > strawEnd)
2652 return (stbtt_int32)ttUSHORT(classRangeRecord + 4);
2666#define STBTT_GPOS_TODO_assert(x)
2668static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
2670 stbtt_uint16 lookupListOffset;
2671 stbtt_uint8 *lookupList;
2672 stbtt_uint16 lookupCount;
2679 data = info->data + info->gpos;
2681 if (ttUSHORT(data + 0) != 1)
2683 if (ttUSHORT(data + 2) != 0)
2686 lookupListOffset = ttUSHORT(data + 8);
2687 lookupList =
data + lookupListOffset;
2688 lookupCount = ttUSHORT(lookupList);
2690 for (i = 0;
i < lookupCount; ++
i) {
2691 stbtt_uint16 lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);
2692 stbtt_uint8 *lookupTable = lookupList + lookupOffset;
2694 stbtt_uint16 lookupType = ttUSHORT(lookupTable);
2695 stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);
2696 stbtt_uint8 *subTableOffsets = lookupTable + 6;
2697 if (lookupType != 2)
2700 for (sti = 0; sti < subTableCount; sti++) {
2701 stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
2702 stbtt_uint8 *table = lookupTable + subtableOffset;
2703 stbtt_uint16 posFormat = ttUSHORT(table);
2704 stbtt_uint16 coverageOffset = ttUSHORT(table + 2);
2705 stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
2706 if (coverageIndex == -1)
2709 switch (posFormat) {
2711 stbtt_int32 l,
r, m;
2713 stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
2714 stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
2715 if (valueFormat1 == 4 && valueFormat2 == 0) {
2716 stbtt_int32 valueRecordPairSizeInBytes = 2;
2717 stbtt_uint16 pairSetCount = ttUSHORT(table + 8);
2718 stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
2719 stbtt_uint8 *pairValueTable = table + pairPosOffset;
2720 stbtt_uint16 pairValueCount = ttUSHORT(pairValueTable);
2721 stbtt_uint8 *pairValueArray = pairValueTable + 2;
2723 if (coverageIndex >= pairSetCount)
2727 r = pairValueCount - 1;
2732 stbtt_uint16 secondGlyph;
2733 stbtt_uint8 *pairValue;
2735 pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;
2736 secondGlyph = ttUSHORT(pairValue);
2737 straw = secondGlyph;
2740 else if (needle > straw)
2743 stbtt_int16 xAdvance = ttSHORT(pairValue + 2);
2754 stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
2755 stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
2756 if (valueFormat1 == 4 && valueFormat2 == 0) {
2757 stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);
2758 stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);
2759 int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);
2760 int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2);
2762 stbtt_uint16 class1Count = ttUSHORT(table + 12);
2763 stbtt_uint16 class2Count = ttUSHORT(table + 14);
2764 stbtt_uint8 *class1Records, *class2Records;
2765 stbtt_int16 xAdvance;
2767 if (glyph1class < 0 || glyph1class >= class1Count)
2769 if (glyph2class < 0 || glyph2class >= class2Count)
2772 class1Records = table + 16;
2773 class2Records = class1Records + 2 * (glyph1class * class2Count);
2774 xAdvance = ttSHORT(class2Records + 2 * glyph2class);
2791STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int g1,
int g2)
2796 xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
2797 else if (info->kern)
2798 xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
2803STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2)
2805 if (!info->kern && !info->gpos)
2807 return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info, ch1), stbtt_FindGlyphIndex(info, ch2));
2810STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
2811 int *leftSideBearing)
2813 stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info, codepoint), advanceWidth, leftSideBearing);
2816STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap)
2819 *ascent = ttSHORT(info->data + info->hhea + 4);
2821 *descent = ttSHORT(info->data + info->hhea + 6);
2823 *lineGap = ttSHORT(info->data + info->hhea + 8);
2826STBTT_DEF
int stbtt_GetFontVMetricsOS2(
const stbtt_fontinfo *info,
int *typoAscent,
int *typoDescent,
int *typoLineGap)
2828 int tab = stbtt__find_table(info->data, info->fontstart,
"OS/2");
2832 *typoAscent = ttSHORT(info->data + tab + 68);
2834 *typoDescent = ttSHORT(info->data + tab + 70);
2836 *typoLineGap = ttSHORT(info->data + tab + 72);
2840STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1)
2842 *x0 = ttSHORT(info->data + info->head + 36);
2843 *y0 = ttSHORT(info->data + info->head + 38);
2844 *x1 = ttSHORT(info->data + info->head + 40);
2845 *y1 = ttSHORT(info->data + info->head + 42);
2848STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float height)
2850 int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
2851 return (
float)
height / fheight;
2854STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels)
2856 int unitsPerEm = ttUSHORT(info->data + info->head + 18);
2857 return pixels / unitsPerEm;
2860STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *v) { STBTT_free(v, info->userdata); }
2862STBTT_DEF stbtt_uint8 *stbtt_FindSVGDoc(
const stbtt_fontinfo *info,
int gl)
2865 stbtt_uint8 *
data = info->data;
2866 stbtt_uint8 *svg_doc_list =
data + stbtt__get_svg((stbtt_fontinfo *)info);
2868 int numEntries = ttUSHORT(svg_doc_list);
2869 stbtt_uint8 *svg_docs = svg_doc_list + 2;
2871 for (i = 0;
i < numEntries;
i++) {
2872 stbtt_uint8 *svg_doc = svg_docs + (12 *
i);
2873 if ((gl >= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2)))
2879STBTT_DEF
int stbtt_GetGlyphSVG(
const stbtt_fontinfo *info,
int gl,
const char **svg)
2881 stbtt_uint8 *
data = info->data;
2882 stbtt_uint8 *svg_doc;
2887 svg_doc = stbtt_FindSVGDoc(info, gl);
2888 if (svg_doc !=
nullptr) {
2889 *svg = (
char *)data + info->svg + ttULONG(svg_doc + 4);
2890 return ttULONG(svg_doc + 8);
2897STBTT_DEF
int stbtt_GetCodepointSVG(
const stbtt_fontinfo *info,
int unicode_codepoint,
const char **svg)
2899 return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svg);
2907STBTT_DEF
void stbtt_GetGlyphBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
2908 float shift_x,
float shift_y,
int *ix0,
int *iy0,
int *ix1,
int *iy1)
2910 int x0 = 0, y0 = 0, x1, y1;
2911 if (!stbtt_GetGlyphBox(font, glyph, &x0, &y0, &x1, &y1)) {
2925 *ix0 = STBTT_ifloor(x0 * scale_x + shift_x);
2927 *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
2929 *ix1 = STBTT_iceil(x1 * scale_x + shift_x);
2931 *iy1 = STBTT_iceil(-y0 * scale_y + shift_y);
2935STBTT_DEF
void stbtt_GetGlyphBitmapBox(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
int *ix0,
2936 int *iy0,
int *ix1,
int *iy1)
2938 stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
2941STBTT_DEF
void stbtt_GetCodepointBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
2942 float scale_y,
float shift_x,
float shift_y,
int *ix0,
int *iy0,
2945 stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font, codepoint), scale_x, scale_y, shift_x, shift_y, ix0,
2949STBTT_DEF
void stbtt_GetCodepointBitmapBox(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
float scale_y,
2950 int *ix0,
int *iy0,
int *ix1,
int *iy1)
2952 stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
2959typedef struct stbtt__hheap_chunk
2961 struct stbtt__hheap_chunk *next;
2962} stbtt__hheap_chunk;
2964typedef struct stbtt__hheap
2966 struct stbtt__hheap_chunk *head;
2968 int num_remaining_in_head_chunk;
2971static void *stbtt__hheap_alloc(stbtt__hheap *hh,
size_t size,
void *userdata)
2973 if (hh->first_free) {
2974 void *
p = hh->first_free;
2975 hh->first_free = *(
void **)p;
2979 if (hh->num_remaining_in_head_chunk == 0) {
2980 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
2981 stbtt__hheap_chunk *c = (stbtt__hheap_chunk *)STBTT_malloc(
sizeof(stbtt__hheap_chunk) + size * count, userdata);
2986 hh->num_remaining_in_head_chunk = count;
2988 --hh->num_remaining_in_head_chunk;
2989 return (
char *)(hh->head) +
sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk;
2993static void stbtt__hheap_free(stbtt__hheap *hh,
void *p)
2995 *(
void **)p = hh->first_free;
2999static void stbtt__hheap_cleanup(stbtt__hheap *hh,
void *userdata)
3001 stbtt__hheap_chunk *c = hh->head;
3003 stbtt__hheap_chunk *n = c->next;
3004 STBTT_free(c, userdata);
3009typedef struct stbtt__edge
3011 float x0, y0, x1, y1;
3015typedef struct stbtt__active_edge
3017 struct stbtt__active_edge *next;
3018#if STBTT_RASTERIZER_VERSION == 1
3022#elif STBTT_RASTERIZER_VERSION == 2
3028#error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3030} stbtt__active_edge;
3032#if STBTT_RASTERIZER_VERSION == 1
3033#define STBTT_FIXSHIFT 10
3034#define STBTT_FIX (1 << STBTT_FIXSHIFT)
3035#define STBTT_FIXMASK (STBTT_FIX - 1)
3037static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
3040 stbtt__active_edge *
z = (stbtt__active_edge *)stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
3041 float dxdy = (
e->x1 -
e->x0) / (
e->y1 -
e->y0);
3042 STBTT_assert(z !=
nullptr);
3048 z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
3050 z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
3052 z->x = STBTT_ifloor(STBTT_FIX *
e->x0 +
3053 z->dx * (start_point -
e->y0));
3054 z->x -= off_x * STBTT_FIX;
3058 z->direction =
e->invert ? 1 : -1;
3061#elif STBTT_RASTERIZER_VERSION == 2
3062static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
3065 stbtt__active_edge *
z = (stbtt__active_edge *)stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
3066 float dxdy = (
e->x1 -
e->x0) / (
e->y1 -
e->y0);
3067 STBTT_assert(z !=
nullptr);
3072 z->fdy = dxdy != 0.0f ? (1.0f / dxdy) : 0.0f;
3073 z->fx =
e->x0 + dxdy * (start_point -
e->y0);
3075 z->direction =
e->invert ? 1.0f : -1.0f;
3082#error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3085#if STBTT_RASTERIZER_VERSION == 1
3089static void stbtt__fill_active_edges(
unsigned char *scanline,
int len, stbtt__active_edge *e,
int max_weight)
3105 int i = x0 >> STBTT_FIXSHIFT;
3106 int j = x1 >> STBTT_FIXSHIFT;
3108 if (i < len && j >= 0) {
3111 scanline[
i] = scanline[
i] + (stbtt_uint8)((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
3116 scanline[
i] + (stbtt_uint8)(((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
3121 scanline[
j] = scanline[
j] + (stbtt_uint8)(((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
3125 for (++i;
i <
j; ++
i)
3126 scanline[i] = scanline[i] + (stbtt_uint8)max_weight;
3136static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
3137 int off_y,
void *userdata)
3139 stbtt__hheap hh = { 0, 0, 0 };
3140 stbtt__active_edge *active =
nullptr;
3142 int max_weight = (255 / vsubsample);
3144 unsigned char scanline_data[512], *scanline;
3146 if (result->w > 512)
3147 scanline = (
unsigned char *)STBTT_malloc(result->w, userdata);
3149 scanline = scanline_data;
3151 y = off_y * vsubsample;
3152 e[n].y0 = (off_y + result->h) * (
float)vsubsample + 1;
3154 while (j < result->h) {
3155 STBTT_memset(scanline, 0, result->w);
3156 for (s = 0;
s < vsubsample; ++
s) {
3158 float scan_y =
y + 0.5f;
3159 stbtt__active_edge **
step = &active;
3164 stbtt__active_edge *
z = *
step;
3165 if (
z->ey <= scan_y) {
3167 STBTT_assert(
z->direction);
3169 stbtt__hheap_free(&hh, z);
3173 step = &((*step)->next);
3181 while (*step && (*step)->next) {
3182 if ((*step)->x > (*step)->next->x) {
3183 stbtt__active_edge *
t = *
step;
3184 stbtt__active_edge *q =
t->next;
3191 step = &(*step)->next;
3198 while (
e->y0 <= scan_y) {
3199 if (
e->y1 > scan_y) {
3200 stbtt__active_edge *
z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
3203 if (active ==
nullptr)
3205 else if (
z->x < active->x) {
3212 stbtt__active_edge *
p = active;
3213 while (
p->next &&
p->next->x <
z->x)
3226 stbtt__fill_active_edges(scanline, result->w, active, max_weight);
3230 STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
3234 stbtt__hheap_cleanup(&hh, userdata);
3236 if (scanline != scanline_data)
3237 STBTT_free(scanline, userdata);
3240#elif STBTT_RASTERIZER_VERSION == 2
3244static void stbtt__handle_clipped_edge(
float *scanline,
int x, stbtt__active_edge *e,
float x0,
float y0,
float x1,
3249 STBTT_assert(y0 < y1);
3250 STBTT_assert(
e->sy <=
e->ey);
3256 x0 += (x1 - x0) * (
e->sy - y0) / (y1 - y0);
3260 x1 += (x1 - x0) * (
e->ey - y1) / (y1 - y0);
3265 STBTT_assert(x1 <= x + 1);
3266 else if (x0 == x + 1)
3267 STBTT_assert(x1 >= x);
3269 STBTT_assert(x1 <= x);
3270 else if (x0 >= x + 1)
3271 STBTT_assert(x1 >= x + 1);
3273 STBTT_assert(x1 >= x && x1 <= x + 1);
3275 if (x0 <= x && x1 <= x)
3276 scanline[
x] +=
e->direction * (y1 - y0);
3277 else if (x0 >= x + 1 && x1 >= x + 1)
3280 STBTT_assert(x0 >= x && x0 <= x + 1 && x1 >= x && x1 <= x + 1);
3281 scanline[
x] +=
e->direction * (y1 - y0) * (1 - ((x0 - x) + (x1 - x)) / 2);
3285static float stbtt__sized_trapezoid_area(
float height,
float top_width,
float bottom_width)
3287 STBTT_assert(top_width >= 0);
3288 STBTT_assert(bottom_width >= 0);
3289 return (top_width + bottom_width) / 2.0f *
height;
3292static float stbtt__position_trapezoid_area(
float height,
float tx0,
float tx1,
float bx0,
float bx1)
3294 return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0);
3297static float stbtt__sized_triangle_area(
float height,
float width) {
return height *
width / 2; }
3299static void stbtt__fill_active_edges_new(
float *scanline,
float *scanline_fill,
int len, stbtt__active_edge *e,
3302 float y_bottom = y_top + 1;
3308 STBTT_assert(
e->ey >= y_top);
3314 stbtt__handle_clipped_edge(scanline, (
int)x0, e, x0, y_top, x0, y_bottom);
3315 stbtt__handle_clipped_edge(scanline_fill - 1, (
int)x0 + 1, e, x0, y_top, x0, y_bottom);
3318 stbtt__handle_clipped_edge(scanline_fill - 1, 0, e, x0, y_top, x0, y_bottom);
3326 float x_top, x_bottom;
3329 STBTT_assert(
e->sy <= y_bottom &&
e->ey >= y_top);
3334 if (
e->sy > y_top) {
3335 x_top = x0 + dx * (
e->sy - y_top);
3342 if (
e->ey < y_bottom) {
3343 x_bottom = x0 + dx * (
e->ey - y_top);
3351 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
3354 if ((
int)x_top == (
int)x_bottom) {
3358 height = (sy1 - sy0) *
e->direction;
3359 STBTT_assert(x >= 0 && x < len);
3360 scanline[
x] += stbtt__position_trapezoid_area(height, x_top, x + 1.0f, x_bottom, x + 1.0f);
3365 float y_crossing, y_final,
step, sign, area;
3367 if (x_top > x_bottom) {
3370 sy0 = y_bottom - (sy0 - y_top);
3371 sy1 = y_bottom - (sy1 - y_top);
3372 t = sy0, sy0 = sy1, sy1 =
t;
3373 t = x_bottom, x_bottom = x_top, x_top =
t;
3376 t = x0, x0 = xb, xb =
t;
3378 STBTT_assert(dy >= 0);
3379 STBTT_assert(dx >= 0);
3384 y_crossing = y_top + dy * (x1 + 1 - x0);
3387 y_final = y_top + dy * (x2 - x0);
3408 if (y_crossing > y_bottom)
3409 y_crossing = y_bottom;
3411 sign =
e->direction;
3414 area = sign * (y_crossing - sy0);
3417 scanline[x1] += stbtt__sized_triangle_area(area, x1 + 1 - x_top);
3420 if (y_final > y_bottom) {
3422 dy = (y_final - y_crossing) / (x2 - (x1 + 1));
3435 step = sign * dy * 1;
3439 for (x = x1 + 1;
x < x2; ++
x) {
3440 scanline[
x] += area +
step / 2;
3443 STBTT_assert(STBTT_fabs(area) <= 1.01f);
3444 STBTT_assert(sy1 > y_final - 0.01f);
3449 area + sign * stbtt__position_trapezoid_area(sy1 - y_final, (
float)x2, x2 + 1.0f, x_bottom, x2 + 1.0f);
3452 scanline_fill[x2] += sign * (sy1 - sy0);
3464 for (x = 0;
x < len; ++
x) {
3480 float x1 = (float)(x);
3481 float x2 = (float)(x + 1);
3483 float y3 = y_bottom;
3488 float y1 = (
x - x0) / dx + y_top;
3489 float y2 = (
x + 1 - x0) / dx + y_top;
3491 if (x0 < x1 && x3 > x2) {
3492 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3493 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x2, y2);
3494 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3496 else if (x3 < x1 && x0 > x2) {
3497 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3498 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x1, y1);
3499 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3501 else if (x0 < x1 && x3 > x1) {
3502 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3503 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3505 else if (x3 < x1 && x0 > x1) {
3506 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3507 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3509 else if (x0 < x2 && x3 > x2) {
3510 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3511 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3513 else if (x3 < x2 && x0 > x2) {
3514 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3515 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3518 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x3, y3);
3528static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
3529 int off_y,
void *userdata)
3531 stbtt__hheap hh = { 0, 0, 0 };
3532 stbtt__active_edge *active =
nullptr;
3534 float scanline_data[129], *scanline, *scanline2;
3536 STBTT__NOTUSED(vsubsample);
3539 scanline = (
float *)STBTT_malloc((result->w * 2 + 1) *
sizeof(
float), userdata);
3541 scanline = scanline_data;
3543 scanline2 = scanline + result->w;
3546 e[n].y0 = (float)(off_y + result->h) + 1;
3548 while (j < result->h) {
3550 float scan_y_top =
y + 0.0f;
3551 float scan_y_bottom =
y + 1.0f;
3552 stbtt__active_edge **
step = &active;
3554 STBTT_memset(scanline, 0, result->w *
sizeof(scanline[0]));
3555 STBTT_memset(scanline2, 0, (result->w + 1) *
sizeof(scanline[0]));
3560 stbtt__active_edge *
z = *
step;
3561 if (
z->ey <= scan_y_top) {
3563 STBTT_assert(
z->direction);
3565 stbtt__hheap_free(&hh, z);
3568 step = &((*step)->next);
3573 while (
e->y0 <= scan_y_bottom) {
3574 if (
e->y0 !=
e->y1) {
3575 stbtt__active_edge *
z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
3577 if (j == 0 && off_y != 0) {
3578 if (
z->ey < scan_y_top) {
3583 STBTT_assert(
z->ey >= scan_y_top);
3594 stbtt__fill_active_edges_new(scanline, scanline2 + 1, result->w, active, scan_y_top);
3598 for (i = 0;
i < result->w; ++
i) {
3601 sum += scanline2[
i];
3602 k = scanline[
i] + sum;
3603 k = (float)STBTT_fabs(k) * 255 + 0.5f;
3607 result->pixels[
j * result->stride +
i] = (
unsigned char)m;
3613 stbtt__active_edge *
z = *
step;
3615 step = &((*step)->next);
3622 stbtt__hheap_cleanup(&hh, userdata);
3624 if (scanline != scanline_data)
3625 STBTT_free(scanline, userdata);
3628#error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3631#define STBTT__COMPARE(a, b) ((a)->y0 < (b)->y0)
3633static void stbtt__sort_edges_ins_sort(stbtt__edge *p,
int n)
3636 for (i = 1;
i < n; ++
i) {
3637 stbtt__edge
t =
p[
i], *a = &
t;
3640 stbtt__edge *b = &
p[
j - 1];
3641 int c = STBTT__COMPARE(a, b);
3652static void stbtt__sort_edges_quicksort(stbtt__edge *p,
int n)
3657 int c01, c12, c, m,
i,
j;
3661 c01 = STBTT__COMPARE(&p[0], &p[m]);
3662 c12 = STBTT__COMPARE(&p[m], &p[n - 1]);
3667 c = STBTT__COMPARE(&p[0], &p[n - 1]);
3670 z = (c == c12) ? 0 : n - 1;
3688 if (!STBTT__COMPARE(&p[i], &p[0]))
3692 if (!STBTT__COMPARE(&p[0], &p[j]))
3707 stbtt__sort_edges_quicksort(p, j);
3712 stbtt__sort_edges_quicksort(p + i, n - i);
3718static void stbtt__sort_edges(stbtt__edge *p,
int n)
3720 stbtt__sort_edges_quicksort(p, n);
3721 stbtt__sort_edges_ins_sort(p, n);
3729static void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts,
int *wcount,
int windings,
float scale_x,
3730 float scale_y,
float shift_x,
float shift_y,
int off_x,
int off_y,
int invert,
3733 float y_scale_inv = invert ? -scale_y : scale_y;
3736#if STBTT_RASTERIZER_VERSION == 1
3737 int vsubsample = result->h < 8 ? 15 : 5;
3738#elif STBTT_RASTERIZER_VERSION == 2
3741#error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3747 for (i = 0;
i < windings; ++
i)
3750 e = (stbtt__edge *)STBTT_malloc(
sizeof(*e) * (n + 1), userdata);
3756 for (i = 0;
i < windings; ++
i) {
3757 stbtt__point *
p = pts + m;
3760 for (k = 0; k < wcount[
i];
j = k++) {
3763 if (p[j].y == p[k].y)
3767 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
3771 e[n].x0 =
p[a].x * scale_x + shift_x;
3772 e[n].y0 = (
p[a].y * y_scale_inv + shift_y) * vsubsample;
3773 e[n].x1 =
p[b].x * scale_x + shift_x;
3774 e[n].y1 = (
p[b].y * y_scale_inv + shift_y) * vsubsample;
3781 stbtt__sort_edges(e, n);
3784 stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
3786 STBTT_free(e, userdata);
3789static void stbtt__add_point(stbtt__point *points,
int n,
float x,
float y)
3798static int stbtt__tesselate_curve(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
3799 float x2,
float y2,
float objspace_flatness_squared,
int n)
3802 float mx = (x0 + 2 * x1 + x2) / 4;
3803 float my = (y0 + 2 * y1 + y2) / 4;
3805 float dx = (x0 + x2) / 2 - mx;
3806 float dy = (y0 + y2) / 2 - my;
3809 if (dx * dx + dy * dy > objspace_flatness_squared) {
3810 stbtt__tesselate_curve(points, num_points, x0, y0, (x0 + x1) / 2.0f, (y0 + y1) / 2.0f, mx, my,
3811 objspace_flatness_squared, n + 1);
3812 stbtt__tesselate_curve(points, num_points, mx, my, (x1 + x2) / 2.0f, (y1 + y2) / 2.0f, x2, y2,
3813 objspace_flatness_squared, n + 1);
3816 stbtt__add_point(points, *num_points, x2, y2);
3817 *num_points = *num_points + 1;
3822static void stbtt__tesselate_cubic(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
3823 float x2,
float y2,
float x3,
float y3,
float objspace_flatness_squared,
int n)
3826 float dx0 = x1 - x0;
3827 float dy0 = y1 - y0;
3828 float dx1 = x2 - x1;
3829 float dy1 = y2 - y1;
3830 float dx2 = x3 - x2;
3831 float dy2 = y3 - y2;
3834 float longlen = (float)(STBTT_sqrt(dx0 * dx0 + dy0 * dy0) + STBTT_sqrt(dx1 * dx1 + dy1 * dy1) +
3835 STBTT_sqrt(dx2 * dx2 + dy2 * dy2));
3836 float shortlen = (float)STBTT_sqrt(dx * dx + dy * dy);
3837 float flatness_squared = longlen * longlen - shortlen * shortlen;
3842 if (flatness_squared > objspace_flatness_squared) {
3843 float x01 = (x0 + x1) / 2;
3844 float y01 = (y0 + y1) / 2;
3845 float x12 = (x1 + x2) / 2;
3846 float y12 = (y1 + y2) / 2;
3847 float x23 = (x2 + x3) / 2;
3848 float y23 = (y2 + y3) / 2;
3850 float xa = (x01 + x12) / 2;
3851 float ya = (y01 + y12) / 2;
3852 float xb = (x12 + x23) / 2;
3853 float yb = (y12 + y23) / 2;
3855 float mx = (xa + xb) / 2;
3856 float my = (ya + yb) / 2;
3858 stbtt__tesselate_cubic(points, num_points, x0, y0, x01, y01, xa, ya, mx, my, objspace_flatness_squared, n + 1);
3859 stbtt__tesselate_cubic(points, num_points, mx, my, xb, yb, x23, y23, x3, y3, objspace_flatness_squared, n + 1);
3862 stbtt__add_point(points, *num_points, x3, y3);
3863 *num_points = *num_points + 1;
3868static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices,
int num_verts,
float objspace_flatness,
3869 int **contour_lengths,
int *num_contours,
void *userdata)
3871 stbtt__point *points = 0;
3874 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
3875 int i, n = 0,
start = 0, pass;
3878 for (i = 0;
i < num_verts; ++
i)
3879 if (vertices[i].type == STBTT_vmove)
3886 *contour_lengths = (
int *)STBTT_malloc(
sizeof(**contour_lengths) * n, userdata);
3888 if (*contour_lengths == 0) {
3894 for (pass = 0; pass < 2; ++pass) {
3897 points = (stbtt__point *)STBTT_malloc(num_points *
sizeof(points[0]), userdata);
3898 if (points ==
nullptr)
3903 for (i = 0;
i < num_verts; ++
i) {
3904 switch (vertices[i].type) {
3908 (*contour_lengths)[n] = num_points -
start;
3912 x = vertices[
i].x,
y = vertices[
i].y;
3913 stbtt__add_point(points, num_points++, x, y);
3916 x = vertices[
i].x,
y = vertices[
i].y;
3917 stbtt__add_point(points, num_points++, x, y);
3920 stbtt__tesselate_curve(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].x, vertices[i].y,
3921 objspace_flatness_squared, 0);
3922 x = vertices[
i].x,
y = vertices[
i].y;
3925 stbtt__tesselate_cubic(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].cx1,
3926 vertices[i].cy1, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0);
3927 x = vertices[
i].x,
y = vertices[
i].y;
3931 (*contour_lengths)[n] = num_points -
start;
3936 STBTT_free(points, userdata);
3937 STBTT_free(*contour_lengths, userdata);
3938 *contour_lengths = 0;
3943STBTT_DEF
void stbtt_Rasterize(stbtt__bitmap *result,
float flatness_in_pixels, stbtt_vertex *vertices,
int num_verts,
3944 float scale_x,
float scale_y,
float shift_x,
float shift_y,
int x_off,
int y_off,
3945 int invert,
void *userdata)
3947 float scale = scale_x > scale_y ? scale_y : scale_x;
3948 int winding_count = 0;
3949 int *winding_lengths =
nullptr;
3950 stbtt__point *windings =
3951 stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
3953 stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off,
3955 STBTT_free(winding_lengths, userdata);
3956 STBTT_free(windings, userdata);
3960STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata) { STBTT_free(bitmap, userdata); }
3962STBTT_DEF
unsigned char *stbtt_GetGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
3963 float shift_x,
float shift_y,
int glyph,
int *width,
int *height,
3964 int *xoff,
int *yoff)
3966 int ix0, iy0, ix1, iy1;
3968 stbtt_vertex *vertices;
3969 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
3975 STBTT_free(vertices, info->userdata);
3981 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, &ix1, &iy1);
3984 gbm.w = (ix1 - ix0);
3985 gbm.h = (iy1 - iy0);
3986 gbm.pixels =
nullptr;
3997 if (gbm.w && gbm.h) {
3998 gbm.pixels = (
unsigned char *)STBTT_malloc(gbm.w * gbm.h, info->userdata);
4002 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1,
4006 STBTT_free(vertices, info->userdata);
4010STBTT_DEF
unsigned char *stbtt_GetGlyphBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
int glyph,
4011 int *width,
int *height,
int *xoff,
int *yoff)
4013 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
4016STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
4017 int out_stride,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
4021 stbtt_vertex *vertices;
4022 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
4025 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, 0, 0);
4026 gbm.pixels = output;
4029 gbm.stride = out_stride;
4032 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
4034 STBTT_free(vertices, info->userdata);
4037STBTT_DEF
void stbtt_MakeGlyphBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
4038 int out_stride,
float scale_x,
float scale_y,
int glyph)
4040 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, glyph);
4043STBTT_DEF
unsigned char *stbtt_GetCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
4044 float shift_x,
float shift_y,
int codepoint,
int *width,
4045 int *height,
int *xoff,
int *yoff)
4047 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info, codepoint),
4048 width, height, xoff, yoff);
4051STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
4052 int out_h,
int out_stride,
float scale_x,
float scale_y,
4053 float shift_x,
float shift_y,
int oversample_x,
4054 int oversample_y,
float *sub_x,
float *sub_y,
int codepoint)
4056 stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y,
4057 oversample_x, oversample_y, sub_x, sub_y,
4058 stbtt_FindGlyphIndex(info, codepoint));
4061STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
4062 int out_h,
int out_stride,
float scale_x,
float scale_y,
float shift_x,
4063 float shift_y,
int codepoint)
4065 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y,
4066 stbtt_FindGlyphIndex(info, codepoint));
4069STBTT_DEF
unsigned char *stbtt_GetCodepointBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
4070 int codepoint,
int *width,
int *height,
int *xoff,
int *yoff)
4072 return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, codepoint, width, height, xoff, yoff);
4075STBTT_DEF
void stbtt_MakeCodepointBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
4076 int out_stride,
float scale_x,
float scale_y,
int codepoint)
4078 stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, codepoint);
4087static int stbtt_BakeFontBitmap_internal(
unsigned char *data,
int offset,
4089 unsigned char *pixels,
int pw,
int ph,
4090 int first_char,
int num_chars,
4091 stbtt_bakedchar *chardata)
4094 int x,
y, bottom_y,
i;
4096 f.userdata =
nullptr;
4097 if (!stbtt_InitFont(&f, data, offset))
4099 STBTT_memset(pixels, 0, pw * ph);
4103 scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
4105 for (i = 0;
i < num_chars; ++
i) {
4106 int advance, lsb, x0, y0, x1, y1, gw, gh;
4107 int g = stbtt_FindGlyphIndex(&f, first_char + i);
4108 stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
4109 stbtt_GetGlyphBitmapBox(&f, g, scale, scale, &x0, &y0, &x1, &y1);
4112 if (x + gw + 1 >= pw)
4113 y = bottom_y,
x = 1;
4114 if (y + gh + 1 >= ph)
4116 STBTT_assert(x + gw < pw);
4117 STBTT_assert(y + gh < ph);
4118 stbtt_MakeGlyphBitmap(&f, pixels + x + y * pw, gw, gh, pw, scale, scale, g);
4119 chardata[
i].x0 = (stbtt_int16)x;
4120 chardata[
i].y0 = (stbtt_int16)y;
4121 chardata[
i].x1 = (stbtt_int16)(x + gw);
4122 chardata[
i].y1 = (stbtt_int16)(y + gh);
4123 chardata[
i].xadvance = scale * advance;
4124 chardata[
i].xoff = (float)x0;
4125 chardata[
i].yoff = (float)y0;
4127 if (y + gh + 1 > bottom_y)
4128 bottom_y =
y + gh + 1;
4133STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
4134 float *ypos, stbtt_aligned_quad *q,
int opengl_fillrule)
4136 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
4137 float ipw = 1.0f / pw, iph = 1.0f / ph;
4138 const stbtt_bakedchar *b = chardata + char_index;
4139 int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
4140 int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
4142 q->x0 = round_x + d3d_bias;
4143 q->y0 = round_y + d3d_bias;
4144 q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
4145 q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
4147 q->s0 = b->x0 * ipw;
4148 q->t0 = b->y0 * iph;
4149 q->s1 = b->x1 * ipw;
4150 q->t1 = b->y1 * iph;
4152 *xpos += b->xadvance;
4160#ifndef STB_RECT_PACK_VERSION
4162typedef int stbrp_coord;
4189 int id,
w,
h, was_packed;
4192static void stbrp_init_target(stbrp_context *con,
int pw,
int ph, stbrp_node *nodes,
int num_nodes)
4199 STBTT__NOTUSED(nodes);
4200 STBTT__NOTUSED(num_nodes);
4203static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects,
int num_rects)
4206 for (i = 0;
i < num_rects; ++
i) {
4207 if (con->x + rects[i].w > con->width) {
4209 con->y = con->bottom_y;
4211 if (con->y + rects[i].h > con->height)
4213 rects[
i].x = con->x;
4214 rects[
i].y = con->y;
4215 rects[
i].was_packed = 1;
4216 con->x += rects[
i].w;
4217 if (con->y + rects[i].h > con->bottom_y)
4218 con->bottom_y = con->y + rects[
i].h;
4220 for (;
i < num_rects; ++
i)
4221 rects[i].was_packed = 0;
4232STBTT_DEF
int stbtt_PackBegin(stbtt_pack_context *spc,
unsigned char *pixels,
int pw,
int ph,
int stride_in_bytes,
4233 int padding,
void *alloc_context)
4235 stbrp_context *context = (stbrp_context *)STBTT_malloc(
sizeof(*context), alloc_context);
4236 int num_nodes = pw - padding;
4237 stbrp_node *nodes = (stbrp_node *)STBTT_malloc(
sizeof(*nodes) * num_nodes, alloc_context);
4239 if (context ==
nullptr || nodes ==
nullptr) {
4240 if (context !=
nullptr)
4241 STBTT_free(context, alloc_context);
4242 if (nodes !=
nullptr)
4243 STBTT_free(nodes, alloc_context);
4247 spc->user_allocator_context = alloc_context;
4250 spc->pixels = pixels;
4251 spc->pack_info = context;
4253 spc->padding = padding;
4254 spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
4255 spc->h_oversample = 1;
4256 spc->v_oversample = 1;
4257 spc->skip_missing = 0;
4259 stbrp_init_target(context, pw - padding, ph - padding, nodes, num_nodes);
4262 STBTT_memset(pixels, 0, pw * ph);
4267STBTT_DEF
void stbtt_PackEnd(stbtt_pack_context *spc)
4269 STBTT_free(spc->nodes, spc->user_allocator_context);
4270 STBTT_free(spc->pack_info, spc->user_allocator_context);
4273STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample)
4275 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
4276 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
4277 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
4278 spc->h_oversample = h_oversample;
4279 if (v_oversample <= STBTT_MAX_OVERSAMPLE)
4280 spc->v_oversample = v_oversample;
4283STBTT_DEF
void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc,
int skip) { spc->skip_missing = skip; }
4285#define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE - 1)
4287static void stbtt__h_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
4289 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
4290 int safe_w =
w - kernel_width;
4292 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
4293 for (j = 0;
j <
h; ++
j) {
4296 STBTT_memset(buffer, 0, kernel_width);
4301 switch (kernel_width) {
4303 for (i = 0;
i <= safe_w; ++
i) {
4304 total += pixels[
i] - buffer[
i & STBTT__OVER_MASK];
4305 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4306 pixels[
i] = (
unsigned char)(total / 2);
4310 for (i = 0;
i <= safe_w; ++
i) {
4311 total += pixels[
i] - buffer[
i & STBTT__OVER_MASK];
4312 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4313 pixels[
i] = (
unsigned char)(total / 3);
4317 for (i = 0;
i <= safe_w; ++
i) {
4318 total += pixels[
i] - buffer[
i & STBTT__OVER_MASK];
4319 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4320 pixels[
i] = (
unsigned char)(total / 4);
4324 for (i = 0;
i <= safe_w; ++
i) {
4325 total += pixels[
i] - buffer[
i & STBTT__OVER_MASK];
4326 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4327 pixels[
i] = (
unsigned char)(total / 5);
4331 for (i = 0;
i <= safe_w; ++
i) {
4332 total += pixels[
i] - buffer[
i & STBTT__OVER_MASK];
4333 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4334 pixels[
i] = (
unsigned char)(total / kernel_width);
4339 for (;
i <
w; ++
i) {
4340 STBTT_assert(pixels[i] == 0);
4341 total -= buffer[
i & STBTT__OVER_MASK];
4342 pixels[
i] = (
unsigned char)(total / kernel_width);
4345 pixels += stride_in_bytes;
4349static void stbtt__v_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
4351 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
4352 int safe_h =
h - kernel_width;
4354 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
4355 for (j = 0;
j <
w; ++
j) {
4358 STBTT_memset(buffer, 0, kernel_width);
4363 switch (kernel_width) {
4365 for (i = 0;
i <= safe_h; ++
i) {
4366 total += pixels[
i * stride_in_bytes] - buffer[
i & STBTT__OVER_MASK];
4367 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4368 pixels[
i * stride_in_bytes] = (
unsigned char)(total / 2);
4372 for (i = 0;
i <= safe_h; ++
i) {
4373 total += pixels[
i * stride_in_bytes] - buffer[
i & STBTT__OVER_MASK];
4374 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4375 pixels[
i * stride_in_bytes] = (
unsigned char)(total / 3);
4379 for (i = 0;
i <= safe_h; ++
i) {
4380 total += pixels[
i * stride_in_bytes] - buffer[
i & STBTT__OVER_MASK];
4381 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4382 pixels[
i * stride_in_bytes] = (
unsigned char)(total / 4);
4386 for (i = 0;
i <= safe_h; ++
i) {
4387 total += pixels[
i * stride_in_bytes] - buffer[
i & STBTT__OVER_MASK];
4388 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4389 pixels[
i * stride_in_bytes] = (
unsigned char)(total / 5);
4393 for (i = 0;
i <= safe_h; ++
i) {
4394 total += pixels[
i * stride_in_bytes] - buffer[
i & STBTT__OVER_MASK];
4395 buffer[(
i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4396 pixels[
i * stride_in_bytes] = (
unsigned char)(total / kernel_width);
4401 for (;
i <
h; ++
i) {
4402 STBTT_assert(pixels[i * stride_in_bytes] == 0);
4403 total -= buffer[
i & STBTT__OVER_MASK];
4404 pixels[
i * stride_in_bytes] = (
unsigned char)(total / kernel_width);
4411static float stbtt__oversample_shift(
int oversample)
4420 return (
float)-(oversample - 1) / (2.0f * (
float)oversample);
4424STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
4425 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
4428 int missing_glyph_added = 0;
4431 for (i = 0;
i < num_ranges; ++
i) {
4432 float fh = ranges[
i].font_size;
4433 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
4434 ranges[
i].h_oversample = (
unsigned char)spc->h_oversample;
4435 ranges[
i].v_oversample = (
unsigned char)spc->v_oversample;
4436 for (j = 0;
j < ranges[
i].num_chars; ++
j) {
4438 int codepoint = ranges[
i].array_of_unicode_codepoints ==
nullptr ? ranges[
i].first_unicode_codepoint_in_range +
j
4439 : ranges[
i].array_of_unicode_codepoints[
j];
4440 int glyph = stbtt_FindGlyphIndex(info, codepoint);
4441 if (glyph == 0 && (spc->skip_missing || missing_glyph_added)) {
4442 rects[k].w = rects[k].h = 0;
4445 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale * spc->h_oversample, scale * spc->v_oversample, 0, 0, &x0,
4447 rects[k].w = (stbrp_coord)(x1 - x0 + spc->padding + spc->h_oversample - 1);
4448 rects[k].h = (stbrp_coord)(y1 - y0 + spc->padding + spc->v_oversample - 1);
4450 missing_glyph_added = 1;
4459STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
4460 int out_h,
int out_stride,
float scale_x,
float scale_y,
4461 float shift_x,
float shift_y,
int prefilter_x,
int prefilter_y,
4462 float *sub_x,
float *sub_y,
int glyph)
4464 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w - (prefilter_x - 1), out_h - (prefilter_y - 1), out_stride, scale_x,
4465 scale_y, shift_x, shift_y, glyph);
4467 if (prefilter_x > 1)
4468 stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x);
4470 if (prefilter_y > 1)
4471 stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y);
4473 *sub_x = stbtt__oversample_shift(prefilter_x);
4474 *sub_y = stbtt__oversample_shift(prefilter_y);
4478STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
4479 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
4481 int i,
j, k, missing_glyph = -1, return_value = 1;
4484 int old_h_over = spc->h_oversample;
4485 int old_v_over = spc->v_oversample;
4488 for (i = 0;
i < num_ranges; ++
i) {
4489 float fh = ranges[
i].font_size;
4490 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
4491 float recip_h, recip_v, sub_x, sub_y;
4492 spc->h_oversample = ranges[
i].h_oversample;
4493 spc->v_oversample = ranges[
i].v_oversample;
4494 recip_h = 1.0f / spc->h_oversample;
4495 recip_v = 1.0f / spc->v_oversample;
4496 sub_x = stbtt__oversample_shift(spc->h_oversample);
4497 sub_y = stbtt__oversample_shift(spc->v_oversample);
4498 for (j = 0;
j < ranges[
i].num_chars; ++
j) {
4499 stbrp_rect *
r = &rects[k];
4500 if (
r->was_packed &&
r->w != 0 &&
r->h != 0) {
4501 stbtt_packedchar *bc = &ranges[
i].chardata_for_range[
j];
4502 int advance, lsb, x0, y0, x1, y1;
4503 int codepoint = ranges[
i].array_of_unicode_codepoints ==
nullptr ? ranges[
i].first_unicode_codepoint_in_range +
j
4504 : ranges[
i].array_of_unicode_codepoints[
j];
4505 int glyph = stbtt_FindGlyphIndex(info, codepoint);
4506 stbrp_coord pad = (stbrp_coord)spc->padding;
4513 stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
4514 stbtt_GetGlyphBitmapBox(info, glyph, scale * spc->h_oversample, scale * spc->v_oversample, &x0, &y0, &x1, &y1);
4515 stbtt_MakeGlyphBitmapSubpixel(info, spc->pixels +
r->x +
r->y * spc->stride_in_bytes,
4516 r->w - spc->h_oversample + 1,
r->h - spc->v_oversample + 1, spc->stride_in_bytes,
4517 scale * spc->h_oversample, scale * spc->v_oversample, 0, 0, glyph);
4519 if (spc->h_oversample > 1)
4520 stbtt__h_prefilter(spc->pixels +
r->x +
r->y * spc->stride_in_bytes,
r->w,
r->h, spc->stride_in_bytes,
4523 if (spc->v_oversample > 1)
4524 stbtt__v_prefilter(spc->pixels +
r->x +
r->y * spc->stride_in_bytes,
r->w,
r->h, spc->stride_in_bytes,
4527 bc->x0 = (stbtt_int16)
r->x;
4528 bc->y0 = (stbtt_int16)
r->y;
4529 bc->x1 = (stbtt_int16)(
r->x +
r->w);
4530 bc->y1 = (stbtt_int16)(
r->y +
r->h);
4531 bc->xadvance = scale * advance;
4532 bc->xoff = (float)x0 * recip_h + sub_x;
4533 bc->yoff = (float)y0 * recip_v + sub_y;
4534 bc->xoff2 = (x0 +
r->w) * recip_h + sub_x;
4535 bc->yoff2 = (y0 +
r->h) * recip_v + sub_y;
4540 else if (spc->skip_missing) {
4543 else if (
r->was_packed &&
r->w == 0 &&
r->h == 0 && missing_glyph >= 0) {
4544 ranges[
i].chardata_for_range[
j] = ranges[
i].chardata_for_range[missing_glyph];
4555 spc->h_oversample = old_h_over;
4556 spc->v_oversample = old_v_over;
4558 return return_value;
4561STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects)
4563 stbrp_pack_rects((stbrp_context *)spc->pack_info, rects, num_rects);
4566STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
4567 stbtt_pack_range *ranges,
int num_ranges)
4569 stbtt_fontinfo info;
4570 int i,
j, n, return_value = 1;
4575 for (i = 0;
i < num_ranges; ++
i)
4576 for (j = 0;
j < ranges[
i].num_chars; ++
j)
4577 ranges[i].chardata_for_range[j].x0 = ranges[i].chardata_for_range[j].y0 = ranges[i].chardata_for_range[j].x1 =
4578 ranges[i].chardata_for_range[j].y1 = 0;
4581 for (i = 0;
i < num_ranges; ++
i)
4582 n += ranges[i].num_chars;
4584 rects = (stbrp_rect *)STBTT_malloc(
sizeof(*rects) * n, spc->user_allocator_context);
4585 if (rects ==
nullptr)
4588 info.userdata = spc->user_allocator_context;
4589 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, font_index));
4591 n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
4593 stbtt_PackFontRangesPackRects(spc, rects, n);
4595 return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
4597 STBTT_free(rects, spc->user_allocator_context);
4598 return return_value;
4601STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
4602 float font_size,
int first_unicode_codepoint_in_range,
int num_chars_in_range,
4603 stbtt_packedchar *chardata_for_range)
4605 stbtt_pack_range range;
4606 range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
4607 range.array_of_unicode_codepoints =
nullptr;
4608 range.num_chars = num_chars_in_range;
4609 range.chardata_for_range = chardata_for_range;
4610 range.font_size = font_size;
4611 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
4614STBTT_DEF
void stbtt_GetScaledFontVMetrics(
const unsigned char *fontdata,
int index,
float size,
float *ascent,
4615 float *descent,
float *lineGap)
4617 int i_ascent, i_descent, i_lineGap;
4619 stbtt_fontinfo info;
4620 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index));
4621 scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size);
4622 stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap);
4623 *ascent = (float)i_ascent * scale;
4624 *descent = (float)i_descent * scale;
4625 *lineGap = (float)i_lineGap * scale;
4628STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
4629 float *ypos, stbtt_aligned_quad *q,
int align_to_integer)
4631 float ipw = 1.0f / pw, iph = 1.0f / ph;
4632 const stbtt_packedchar *b = chardata + char_index;
4634 if (align_to_integer) {
4635 float x = (float)STBTT_ifloor((*xpos + b->xoff) + 0.5f);
4636 float y = (float)STBTT_ifloor((*ypos + b->yoff) + 0.5f);
4639 q->x1 =
x + b->xoff2 - b->xoff;
4640 q->y1 =
y + b->yoff2 - b->yoff;
4643 q->x0 = *xpos + b->xoff;
4644 q->y0 = *ypos + b->yoff;
4645 q->x1 = *xpos + b->xoff2;
4646 q->y1 = *ypos + b->yoff2;
4649 q->s0 = b->x0 * ipw;
4650 q->t0 = b->y0 * iph;
4651 q->s1 = b->x1 * ipw;
4652 q->t1 = b->y1 * iph;
4654 *xpos += b->xadvance;
4662#define STBTT_min(a, b) ((a) < (b) ? (a) : (b))
4663#define STBTT_max(a, b) ((a) < (b) ? (b) : (a))
4665static int stbtt__ray_intersect_bezier(
float orig[2],
float ray[2],
float q0[2],
float q1[2],
float q2[2],
4668 float q0perp = q0[1] * ray[0] - q0[0] * ray[1];
4669 float q1perp = q1[1] * ray[0] - q1[0] * ray[1];
4670 float q2perp = q2[1] * ray[0] - q2[0] * ray[1];
4671 float roperp = orig[1] * ray[0] - orig[0] * ray[1];
4673 float a = q0perp - 2 * q1perp + q2perp;
4674 float b = q1perp - q0perp;
4675 float c = q0perp - roperp;
4677 float s0 = 0., s1 = 0.;
4681 float discr = b * b - a * c;
4683 float rcpna = -1 / a;
4684 float d = (float)STBTT_sqrt(discr);
4685 s0 = (b +
d) * rcpna;
4686 s1 = (b -
d) * rcpna;
4687 if (s0 >= 0.0 && s0 <= 1.0)
4689 if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) {
4700 if (s0 >= 0.0 && s0 <= 1.0)
4707 float rcp_len2 = 1 / (ray[0] * ray[0] + ray[1] * ray[1]);
4708 float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2;
4710 float q0d = q0[0] * rayn_x + q0[1] * rayn_y;
4711 float q1d = q1[0] * rayn_x + q1[1] * rayn_y;
4712 float q2d = q2[0] * rayn_x + q2[1] * rayn_y;
4713 float rod = orig[0] * rayn_x + orig[1] * rayn_y;
4715 float q10d = q1d - q0d;
4716 float q20d = q2d - q0d;
4717 float q0rd = q0d - rod;
4719 hits[0][0] = q0rd + s0 * (2.0f - 2.0f * s0) * q10d + s0 * s0 * q20d;
4720 hits[0][1] = a * s0 + b;
4723 hits[1][0] = q0rd + s1 * (2.0f - 2.0f * s1) * q10d + s1 * s1 * q20d;
4724 hits[1][1] = a * s1 + b;
4733static int equal(
float *a,
float *b) {
return (a[0] == b[0] && a[1] == b[1]); }
4735static int stbtt__compute_crossings_x(
float x,
float y,
int nverts, stbtt_vertex *verts)
4738 float orig[2], ray[2] = { 1, 0 };
4743 y_frac = (float)STBTT_fmod(y, 1.0f);
4746 else if (y_frac > 0.99f)
4753 for (i = 0;
i < nverts; ++
i) {
4754 if (verts[i].type == STBTT_vline) {
4755 int x0 = (
int)verts[i - 1].x, y0 = (
int)verts[
i - 1].y;
4756 int x1 = (
int)verts[i].x, y1 = (
int)verts[
i].y;
4757 if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
4758 float x_inter = (
y - y0) / (y1 - y0) * (x1 - x0) + x0;
4760 winding += (y0 < y1) ? 1 : -1;
4763 if (verts[i].type == STBTT_vcurve) {
4764 int x0 = (
int)verts[i - 1].x, y0 = (
int)verts[
i - 1].y;
4765 int x1 = (
int)verts[i].cx, y1 = (
int)verts[
i].cy;
4766 int x2 = (
int)verts[i].x, y2 = (
int)verts[
i].y;
4767 int ax = STBTT_min(x0, STBTT_min(x1, x2)), ay = STBTT_min(y0, STBTT_min(y1, y2));
4768 int by = STBTT_max(y0, STBTT_max(y1, y2));
4769 if (y > ay && y < by && x > ax) {
4770 float q0[2], q1[2], q2[2];
4778 if (equal(q0, q1) || equal(q1, q2)) {
4779 x0 = (
int)verts[i - 1].x;
4780 y0 = (
int)verts[i - 1].y;
4781 x1 = (
int)verts[i].x;
4782 y1 = (
int)verts[i].y;
4783 if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
4784 float x_inter = (
y - y0) / (y1 - y0) * (x1 - x0) + x0;
4786 winding += (y0 < y1) ? 1 : -1;
4790 int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
4793 winding += (hits[0][1] < 0 ? -1 : 1);
4796 winding += (hits[1][1] < 0 ? -1 : 1);
4804static float stbtt__cuberoot(
float x)
4807 return -(float)STBTT_pow(-x, 1.0f / 3.0f);
4809 return (
float)STBTT_pow(x, 1.0f / 3.0f);
4813static int stbtt__solve_cubic(
float a,
float b,
float c,
float *r)
4816 float p = b - a * a / 3;
4817 float q = a * (2 * a * a - 9 * b) / 27 + c;
4818 float p3 =
p *
p *
p;
4819 float d = q * q + 4 * p3 / 27;
4821 float z = (float)STBTT_sqrt(d);
4822 float u = (-q +
z) / 2;
4823 float v = (-q -
z) / 2;
4824 u = stbtt__cuberoot(u);
4825 v = stbtt__cuberoot(v);
4830 float u = (float)STBTT_sqrt(-p / 3);
4831 float v = (float)STBTT_acos(-STBTT_sqrt(-27 / p3) * q / 2) / 3;
4832 float m = (float)STBTT_cos(v);
4833 float n = (float)STBTT_cos(v - 3.141592 / 2) * 1.732050808f;
4834 r[0] =
s +
u * 2 * m;
4835 r[1] =
s -
u * (m + n);
4836 r[2] =
s -
u * (m - n);
4845STBTT_DEF
unsigned char *stbtt_GetGlyphSDF(
const stbtt_fontinfo *info,
float scale,
int glyph,
int padding,
4846 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
int *height,
4847 int *xoff,
int *yoff)
4849 float scale_x = scale, scale_y = scale;
4850 int ix0, iy0, ix1, iy1;
4852 unsigned char *
data;
4857 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f, 0.0f, &ix0, &iy0, &ix1, &iy1);
4860 if (ix0 == ix1 || iy0 == iy1)
4886 stbtt_vertex *verts;
4887 int num_verts = stbtt_GetGlyphShape(info, glyph, &verts);
4888 data = (
unsigned char *)STBTT_malloc(w * h, info->userdata);
4889 precompute = (
float *)STBTT_malloc(num_verts *
sizeof(
float), info->userdata);
4891 for (i = 0, j = num_verts - 1;
i < num_verts;
j =
i++) {
4892 if (verts[i].type == STBTT_vline) {
4893 float x0 = verts[
i].x * scale_x, y0 = verts[
i].y * scale_y;
4894 float x1 = verts[
j].x * scale_x, y1 = verts[
j].y * scale_y;
4895 float dist = (float)STBTT_sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
4896 precompute[
i] = (dist == 0) ? 0.0f : 1.0f / dist;
4898 else if (verts[i].type == STBTT_vcurve) {
4899 float x2 = verts[
j].x * scale_x, y2 = verts[
j].y * scale_y;
4900 float x1 = verts[
i].cx * scale_x, y1 = verts[
i].cy * scale_y;
4901 float x0 = verts[
i].x * scale_x, y0 = verts[
i].y * scale_y;
4902 float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
4903 float len2 = bx * bx + by * by;
4905 precompute[
i] = 1.0f / (bx * bx + by * by);
4907 precompute[
i] = 0.0f;
4910 precompute[
i] = 0.0f;
4913 for (y = iy0;
y < iy1; ++
y) {
4914 for (x = ix0;
x < ix1; ++
x) {
4916 float min_dist = 999999.0f;
4917 float sx = (float)x + 0.5f;
4918 float sy = (float)y + 0.5f;
4919 float x_gspace = (sx / scale_x);
4920 float y_gspace = (sy / scale_y);
4922 int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts,
4926 for (i = 0;
i < num_verts; ++
i) {
4927 float x0 = verts[
i].x * scale_x, y0 = verts[
i].y * scale_y;
4929 if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) {
4930 float x1 = verts[
i - 1].x * scale_x, y1 = verts[
i - 1].y * scale_y;
4932 float dist, dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
4933 if (dist2 < min_dist * min_dist)
4934 min_dist = (float)STBTT_sqrt(dist2);
4939 dist = (float)STBTT_fabs((x1 - x0) * (y0 - sy) - (y1 - y0) * (x0 - sx)) * precompute[
i];
4940 STBTT_assert(i != 0);
4941 if (dist < min_dist) {
4945 float dx = x1 - x0, dy = y1 - y0;
4946 float px = x0 - sx, py = y0 - sy;
4949 float t = -(px * dx + py * dy) / (dx * dx + dy * dy);
4950 if (t >= 0.0f && t <= 1.0f)
4954 else if (verts[i].type == STBTT_vcurve) {
4955 float x2 = verts[
i - 1].x * scale_x, y2 = verts[
i - 1].y * scale_y;
4956 float x1 = verts[
i].cx * scale_x, y1 = verts[
i].cy * scale_y;
4957 float box_x0 = STBTT_min(STBTT_min(x0, x1), x2);
4958 float box_y0 = STBTT_min(STBTT_min(y0, y1), y2);
4959 float box_x1 = STBTT_max(STBTT_max(x0, x1), x2);
4960 float box_y1 = STBTT_max(STBTT_max(y0, y1), y2);
4962 if (sx > box_x0 - min_dist && sx < box_x1 + min_dist && sy > box_y0 - min_dist && sy < box_y1 + min_dist) {
4964 float ax = x1 - x0, ay = y1 - y0;
4965 float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
4966 float mx = x0 - sx, my = y0 - sy;
4967 float res[3] = { 0.f, 0.f, 0.f };
4968 float px, py,
t, it, dist2;
4969 float a_inv = precompute[
i];
4971 float a = 3 * (ax * bx + ay * by);
4972 float b = 2 * (ax * ax + ay * ay) + (mx * bx + my * by);
4973 float c = mx * ax + my * ay;
4976 res[num++] = -c / b;
4980 float discriminant = b * b - 4 * a * c;
4981 if (discriminant < 0)
4984 float root = (float)STBTT_sqrt(discriminant);
4985 res[0] = (-b - root) / (2 * a);
4986 res[1] = (-b + root) / (2 * a);
4992 float b = 3 * (ax * bx + ay * by) * a_inv;
4993 float c = (2 * (ax * ax + ay * ay) + (mx * bx + my * by)) * a_inv;
4994 float d = (mx * ax + my * ay) * a_inv;
4995 num = stbtt__solve_cubic(b, c, d, res);
4997 dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
4998 if (dist2 < min_dist * min_dist)
4999 min_dist = (float)STBTT_sqrt(dist2);
5001 if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {
5002 t = res[0], it = 1.0f -
t;
5003 px = it * it * x0 + 2 *
t * it * x1 +
t *
t * x2;
5004 py = it * it * y0 + 2 *
t * it * y1 +
t *
t * y2;
5005 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
5006 if (dist2 < min_dist * min_dist)
5007 min_dist = (float)STBTT_sqrt(dist2);
5009 if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) {
5010 t = res[1], it = 1.0f -
t;
5011 px = it * it * x0 + 2 *
t * it * x1 +
t *
t * x2;
5012 py = it * it * y0 + 2 *
t * it * y1 +
t *
t * y2;
5013 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
5014 if (dist2 < min_dist * min_dist)
5015 min_dist = (float)STBTT_sqrt(dist2);
5017 if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) {
5018 t = res[2], it = 1.0f -
t;
5019 px = it * it * x0 + 2 *
t * it * x1 +
t *
t * x2;
5020 py = it * it * y0 + 2 *
t * it * y1 +
t *
t * y2;
5021 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
5022 if (dist2 < min_dist * min_dist)
5023 min_dist = (float)STBTT_sqrt(dist2);
5029 min_dist = -min_dist;
5030 val = onedge_value + pixel_dist_scale * min_dist;
5035 data[(
y - iy0) * w + (x - ix0)] = (
unsigned char)val;
5038 STBTT_free(precompute, info->userdata);
5039 STBTT_free(verts, info->userdata);
5044STBTT_DEF
unsigned char *stbtt_GetCodepointSDF(
const stbtt_fontinfo *info,
float scale,
int codepoint,
int padding,
5045 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
5046 int *height,
int *xoff,
int *yoff)
5048 return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale,
5049 width, height, xoff, yoff);
5052STBTT_DEF
void stbtt_FreeSDF(
unsigned char *bitmap,
void *userdata) { STBTT_free(bitmap, userdata); }
5060static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2,
5067 stbtt_uint16 ch = s2[0] * 256 + s2[1];
5074 else if (ch < 0x800) {
5077 if (s1[i++] != 0xc0 + (ch >> 6))
5079 if (s1[i++] != 0x80 + (ch & 0x3f))
5082 else if (ch >= 0xd800 && ch < 0xdc00) {
5084 stbtt_uint16 ch2 = s2[2] * 256 + s2[3];
5087 c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
5088 if (s1[i++] != 0xf0 + (c >> 18))
5090 if (s1[i++] != 0x80 + ((c >> 12) & 0x3f))
5092 if (s1[i++] != 0x80 + ((c >> 6) & 0x3f))
5094 if (s1[i++] != 0x80 + ((c)&0x3f))
5099 else if (ch >= 0xdc00 && ch < 0xe000) {
5105 if (s1[i++] != 0xe0 + (ch >> 12))
5107 if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f))
5109 if (s1[i++] != 0x80 + ((ch)&0x3f))
5118static int stbtt_CompareUTF8toUTF16_bigendian_internal(
char *s1,
int len1,
char *s2,
int len2)
5120 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8 *)s1, len1, (stbtt_uint8 *)s2, len2);
5125STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
5126 int languageID,
int nameID)
5128 stbtt_int32
i, count, stringOffset;
5129 stbtt_uint8 *fc = font->data;
5130 stbtt_uint32 offset = font->fontstart;
5131 stbtt_uint32 nm = stbtt__find_table(fc, offset,
"name");
5135 count = ttUSHORT(fc + nm + 2);
5136 stringOffset = nm + ttUSHORT(fc + nm + 4);
5137 for (i = 0;
i < count; ++
i) {
5138 stbtt_uint32 loc = nm + 6 + 12 *
i;
5139 if (platformID == ttUSHORT(fc + loc + 0) && encodingID == ttUSHORT(fc + loc + 2) &&
5140 languageID == ttUSHORT(fc + loc + 4) && nameID == ttUSHORT(fc + loc + 6)) {
5141 *length = ttUSHORT(fc + loc + 8);
5142 return (
const char *)(fc + stringOffset + ttUSHORT(fc + loc + 10));
5148static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen,
5149 stbtt_int32 target_id, stbtt_int32 next_id)
5152 stbtt_int32 count = ttUSHORT(fc + nm + 2);
5153 stbtt_int32 stringOffset = nm + ttUSHORT(fc + nm + 4);
5155 for (i = 0;
i < count; ++
i) {
5156 stbtt_uint32 loc = nm + 6 + 12 *
i;
5157 stbtt_int32
id = ttUSHORT(fc + loc + 6);
5158 if (
id == target_id) {
5160 stbtt_int32 platform = ttUSHORT(fc + loc + 0), encoding = ttUSHORT(fc + loc + 2),
5161 language = ttUSHORT(fc + loc + 4);
5164 if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
5165 stbtt_int32 slen = ttUSHORT(fc + loc + 8);
5166 stbtt_int32 off = ttUSHORT(fc + loc + 10);
5169 stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc + stringOffset + off, slen);
5170 if (matchlen >= 0) {
5172 if (i + 1 < count && ttUSHORT(fc + loc + 12 + 6) == next_id && ttUSHORT(fc + loc + 12) == platform &&
5173 ttUSHORT(fc + loc + 12 + 2) == encoding && ttUSHORT(fc + loc + 12 + 4) == language) {
5174 slen = ttUSHORT(fc + loc + 12 + 8);
5175 off = ttUSHORT(fc + loc + 12 + 10);
5177 if (matchlen == nlen)
5180 else if (matchlen < nlen && name[matchlen] ==
' ') {
5182 if (stbtt_CompareUTF8toUTF16_bigendian_internal((
char *)(name + matchlen), nlen - matchlen,
5183 (
char *)(fc + stringOffset + off), slen))
5189 if (matchlen == nlen)
5201static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
5203 stbtt_int32 nlen = (stbtt_int32)STBTT_strlen((
char *)name);
5204 stbtt_uint32 nm, hd;
5205 if (!stbtt__isfont(fc + offset))
5210 hd = stbtt__find_table(fc, offset,
"head");
5211 if ((ttUSHORT(fc + hd + 44) & 7) != (flags & 7))
5215 nm = stbtt__find_table(fc, offset,
"name");
5221 if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))
5223 if (stbtt__matchpair(fc, nm, name, nlen, 1, -1))
5225 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
5229 if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))
5231 if (stbtt__matchpair(fc, nm, name, nlen, 1, 2))
5233 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
5240static int stbtt_FindMatchingFont_internal(
unsigned char *font_collection,
char *name_utf8, stbtt_int32 flags)
5244 stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
5247 if (stbtt__matches((stbtt_uint8 *)font_collection, off, (stbtt_uint8 *)name_utf8, flags))
5252#if defined(__GNUC__) || defined(__clang__)
5253#pragma GCC diagnostic push
5254#pragma GCC diagnostic ignored "-Wcast-qual"
5257STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
float pixel_height,
unsigned char *pixels,
5258 int pw,
int ph,
int first_char,
int num_chars, stbtt_bakedchar *chardata)
5260 return stbtt_BakeFontBitmap_internal((
unsigned char *)data, offset, pixel_height, pixels, pw, ph, first_char,
5261 num_chars, chardata);
5264STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index)
5266 return stbtt_GetFontOffsetForIndex_internal((
unsigned char *)data, index);
5269STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data)
5271 return stbtt_GetNumberOfFonts_internal((
unsigned char *)data);
5274STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset)
5276 return stbtt_InitFont_internal(info, (
unsigned char *)data, offset);
5279STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags)
5281 return stbtt_FindMatchingFont_internal((
unsigned char *)fontdata, (
char *)name, flags);
5284STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2)
5286 return stbtt_CompareUTF8toUTF16_bigendian_internal((
char *)s1, len1, (
char *)s2, len2);
5289#if defined(__GNUC__) || defined(__clang__)
5290#pragma GCC diagnostic pop