OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
stb_truetype.h
Go to the documentation of this file.
1 // [ImGui] this is a slightly modified version of stb_truetype.h 1.02
2 // [ImGui] we added stbtt_PackFontRangesGatherRects() and stbtt_PackFontRangesRenderIntoRects() and modified stbtt_PackBegin()
3 
4 // stb_truetype.h - v1.02 - public domain
5 // authored from 2009-2014 by Sean Barrett / RAD Game Tools
6 //
7 // This library processes TrueType files:
8 // parse files
9 // extract glyph metrics
10 // extract glyph shapes
11 // render glyphs to one-channel bitmaps with antialiasing (box filter)
12 //
13 // Todo:
14 // non-MS cmaps
15 // crashproof on bad data
16 // hinting? (no longer patented)
17 // cleartype-style AA?
18 // optimize: use simple memory allocator for intermediates
19 // optimize: build edge-list directly from curves
20 // optimize: rasterize directly from curves?
21 //
22 // ADDITIONAL CONTRIBUTORS
23 //
24 // Mikko Mononen: compound shape support, more cmap formats
25 // Tor Andersson: kerning, subpixel rendering
26 //
27 // Bug/warning reports/fixes:
28 // "Zer" on mollyrocket (with fix)
29 // Cass Everitt
30 // stoiko (Haemimont Games)
31 // Brian Hook
32 // Walter van Niftrik
33 // David Gow
34 // David Given
35 // Ivan-Assen Ivanov
36 // Anthony Pesch
37 // Johan Duparc
38 // Hou Qiming
39 // Fabian "ryg" Giesen
40 //
41 // Misc other:
42 // Ryan Gordon
43 //
44 // VERSION HISTORY
45 //
46 // 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++
47 // 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match
48 // non-oversampled; STBTT_POINT_SIZE for packed case only
49 // 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling
50 // 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg)
51 // 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID
52 // 0.8b (2014-07-07) fix a warning
53 // 0.8 (2014-05-25) fix a few more warnings
54 // 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back
55 // 0.6c (2012-07-24) improve documentation
56 // 0.6b (2012-07-20) fix a few more warnings
57 // 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels,
58 // stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty
59 // 0.5 (2011-12-09) bugfixes:
60 // subpixel glyph renderer computed wrong bounding box
61 // first vertex of shape can be off-curve (FreeSans)
62 // 0.4b (2011-12-03) fixed an error in the font baking example
63 // 0.4 (2011-12-01) kerning, subpixel rendering (tor)
64 // bugfixes for:
65 // codepoint-to-glyph conversion using table fmt=12
66 // codepoint-to-glyph conversion using table fmt=4
67 // stbtt_GetBakedQuad with non-square texture (Zer)
68 // updated Hello World! sample to use kerning and subpixel
69 // fixed some warnings
70 // 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM)
71 // userdata, malloc-from-userdata, non-zero fill (stb)
72 // 0.2 (2009-03-11) Fix unsigned/signed char warnings
73 // 0.1 (2009-03-09) First public release
74 //
75 // LICENSE
76 //
77 // This software is in the public domain. Where that dedication is not
78 // recognized, you are granted a perpetual, irrevokable license to copy
79 // and modify this file as you see fit.
80 //
81 // USAGE
82 //
83 // Include this file in whatever places neeed to refer to it. In ONE C/C++
84 // file, write:
85 // #define STB_TRUETYPE_IMPLEMENTATION
86 // before the #include of this file. This expands out the actual
87 // implementation into that C/C++ file.
88 //
89 // Simple 3D API (don't ship this, but it's fine for tools and quick start)
90 // stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture
91 // stbtt_GetBakedQuad() -- compute quad to draw for a given char
92 //
93 // Improved 3D API (more shippable):
94 // #include "stb_rect_pack.h" -- optional, but you really want it
95 // stbtt_PackBegin()
96 // stbtt_PackSetOversample() -- for improved quality on small fonts
97 // stbtt_PackFontRanges() -- pack and renders
98 // stbtt_PackEnd()
99 // stbtt_GetPackedQuad()
100 //
101 // "Load" a font file from a memory buffer (you have to keep the buffer loaded)
102 // stbtt_InitFont()
103 // stbtt_GetFontOffsetForIndex() -- use for TTC font collections
104 //
105 // Render a unicode codepoint to a bitmap
106 // stbtt_GetCodepointBitmap() -- allocates and returns a bitmap
107 // stbtt_MakeCodepointBitmap() -- renders into bitmap you provide
108 // stbtt_GetCodepointBitmapBox() -- how big the bitmap must be
109 //
110 // Character advance/positioning
111 // stbtt_GetCodepointHMetrics()
112 // stbtt_GetFontVMetrics()
113 // stbtt_GetCodepointKernAdvance()
114 //
115 // ADDITIONAL DOCUMENTATION
116 //
117 // Immediately after this block comment are a series of sample programs.
118 //
119 // After the sample programs is the "header file" section. This section
120 // includes documentation for each API function.
121 //
122 // Some important concepts to understand to use this library:
123 //
124 // Codepoint
125 // Characters are defined by unicode codepoints, e.g. 65 is
126 // uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is
127 // the hiragana for "ma".
128 //
129 // Glyph
130 // A visual character shape (every codepoint is rendered as
131 // some glyph)
132 //
133 // Glyph index
134 // A font-specific integer ID representing a glyph
135 //
136 // Baseline
137 // Glyph shapes are defined relative to a baseline, which is the
138 // bottom of uppercase characters. Characters extend both above
139 // and below the baseline.
140 //
141 // Current Point
142 // As you draw text to the screen, you keep track of a "current point"
143 // which is the origin of each character. The current point's vertical
144 // position is the baseline. Even "baked fonts" use this model.
145 //
146 // Vertical Font Metrics
147 // The vertical qualities of the font, used to vertically position
148 // and space the characters. See docs for stbtt_GetFontVMetrics.
149 //
150 // Font Size in Pixels or Points
151 // The preferred interface for specifying font sizes in stb_truetype
152 // is to specify how tall the font's vertical extent should be in pixels.
153 // If that sounds good enough, skip the next paragraph.
154 //
155 // Most font APIs instead use "points", which are a common typographic
156 // measurement for describing font size, defined as 72 points per inch.
157 // stb_truetype provides a point API for compatibility. However, true
158 // "per inch" conventions don't make much sense on computer displays
159 // since they different monitors have different number of pixels per
160 // inch. For example, Windows traditionally uses a convention that
161 // there are 96 pixels per inch, thus making 'inch' measurements have
162 // nothing to do with inches, and thus effectively defining a point to
163 // be 1.333 pixels. Additionally, the TrueType font data provides
164 // an explicit scale factor to scale a given font's glyphs to points,
165 // but the author has observed that this scale factor is often wrong
166 // for non-commercial fonts, thus making fonts scaled in points
167 // according to the TrueType spec incoherently sized in practice.
168 //
169 // ADVANCED USAGE
170 //
171 // Quality:
172 //
173 // - Use the functions with Subpixel at the end to allow your characters
174 // to have subpixel positioning. Since the font is anti-aliased, not
175 // hinted, this is very import for quality. (This is not possible with
176 // baked fonts.)
177 //
178 // - Kerning is now supported, and if you're supporting subpixel rendering
179 // then kerning is worth using to give your text a polished look.
180 //
181 // Performance:
182 //
183 // - Convert Unicode codepoints to glyph indexes and operate on the glyphs;
184 // if you don't do this, stb_truetype is forced to do the conversion on
185 // every call.
186 //
187 // - There are a lot of memory allocations. We should modify it to take
188 // a temp buffer and allocate from the temp buffer (without freeing),
189 // should help performance a lot.
190 //
191 // NOTES
192 //
193 // The system uses the raw data found in the .ttf file without changing it
194 // and without building auxiliary data structures. This is a bit inefficient
195 // on little-endian systems (the data is big-endian), but assuming you're
196 // caching the bitmaps or glyph shapes this shouldn't be a big deal.
197 //
198 // It appears to be very hard to programmatically determine what font a
199 // given file is in a general way. I provide an API for this, but I don't
200 // recommend it.
201 //
202 //
203 // SOURCE STATISTICS (based on v0.6c, 2050 LOC)
204 //
205 // Documentation & header file 520 LOC \___ 660 LOC documentation
206 // Sample code 140 LOC /
207 // Truetype parsing 620 LOC ---- 620 LOC TrueType
208 // Software rasterization 240 LOC \ .
209 // Curve tesselation 120 LOC \__ 550 LOC Bitmap creation
210 // Bitmap management 100 LOC /
211 // Baked bitmap interface 70 LOC /
212 // Font name matching & access 150 LOC ---- 150
213 // C runtime library abstraction 60 LOC ---- 60
214 
215 
221 //
222 // Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless
223 //
224 #if 0
225 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
226 #include "stb_truetype.h"
227 
228 char ttf_buffer[1<<20];
229 unsigned char temp_bitmap[512*512];
230 
231 stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs
232 GLstbtt_uint ftex;
233 
234 void my_stbtt_initfont(void)
235 {
236  fread(ttf_buffer, 1, 1<<20, fopen("c:/windows/fonts/times.ttf", "rb"));
237  stbtt_BakeFontBitmap(data,0, 32.0, temp_bitmap,512,512, 32,96, cdata); // no guarantee this fits!
238  // can free ttf_buffer at this point
239  glGenTextures(1, &ftex);
240  glBindTexture(GL_TEXTURE_2D, ftex);
241  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
242  // can free temp_bitmap at this point
243  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
244 }
245 
246 void my_stbtt_print(float x, float y, char *text)
247 {
248  // assume orthographic projection with units = screen pixels, origin at top left
249  glBindTexture(GL_TEXTURE_2D, ftex);
250  glBegin(GL_QUADS);
251  while (*text) {
252  if (*text >= 32 && *text < 128) {
254  stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9
255  glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
256  glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
257  glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
258  glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);
259  }
260  ++text;
261  }
262  glEnd();
263 }
264 #endif
265 //
266 //
268 //
269 // Complete program (this compiles): get a single bitmap, print as ASCII art
270 //
271 #if 0
272 #include <stdio.h>
273 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
274 #include "stb_truetype.h"
275 
276 char ttf_buffer[1<<25];
277 
278 int main(int argc, char **argv)
279 {
280  stbtt_fontinfo font;
281  unsigned char *bitmap;
282  int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
283 
284  fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb"));
285 
286  stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
287  bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
288 
289  for (j=0; j < h; ++j) {
290  for (i=0; i < w; ++i)
291  putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);
292  putchar('\n');
293  }
294  return 0;
295 }
296 #endif
297 //
298 // Output:
299 //
300 // .ii.
301 // @@@@@@.
302 // V@Mio@@o
303 // :i. V@V
304 // :oM@@M
305 // :@@@MM@M
306 // @@o o@M
307 // :@@. M@M
308 // @@@o@@@@
309 // :M@@V:@@.
310 //
312 //
313 // Complete program: print "Hello World!" banner, with bugs
314 //
315 #if 0
316 char buffer[24<<20];
317 unsigned char screen[20][79];
318 
319 int main(int arg, char **argv)
320 {
321  stbtt_fontinfo font;
322  int i,j,ascent,baseline,ch=0;
323  float scale, xpos=2; // leave a little padding in case the character extends left
324  char *text = "Heljo World!";
325 
326  fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb"));
327  stbtt_InitFont(&font, buffer, 0);
328 
329  scale = stbtt_ScaleForPixelHeight(&font, 15);
330  stbtt_GetFontVMetrics(&font, &ascent,0,0);
331  baseline = (int) (ascent*scale);
332 
333  while (text[ch]) {
334  int advance,lsb,x0,y0,x1,y1;
335  float x_shift = xpos - (float) floor(xpos);
336  stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
337  stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);
338  stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);
339  // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong
340  // because this API is really for baking character bitmaps into textures. if you want to render
341  // a sequence of characters, you really need to render each bitmap to a temp buffer, then
342  // "alpha blend" that into the working buffer
343  xpos += (advance * scale);
344  if (text[ch+1])
345  xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);
346  ++ch;
347  }
348 
349  for (j=0; j < 20; ++j) {
350  for (i=0; i < 78; ++i)
351  putchar(" .:ioVM@"[screen[j][i]>>5]);
352  putchar('\n');
353  }
354 
355  return 0;
356 }
357 #endif
358 
359 
367 
368 #ifdef STB_TRUETYPE_IMPLEMENTATION
369  // #define your own (u)stbtt_int8/16/32 before including to override this
370  #ifndef stbtt_uint8
371  typedef unsigned char stbtt_uint8;
372  typedef signed char stbtt_int8;
373  typedef unsigned short stbtt_uint16;
374  typedef signed short stbtt_int16;
375  typedef unsigned int stbtt_uint32;
376  typedef signed int stbtt_int32;
377  #endif
378 
379  typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1];
380  typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1];
381 
382  #ifdef STBTT_STATIC
383  #define STBTT_DEF static
384  #else
385  #define STBTT_DEF extern
386  #endif
387 
388  // #define your own STBTT_sort() to override this to avoid qsort
389  #ifndef STBTT_sort
390  #include <stdlib.h>
391  #define STBTT_sort(data,num_items,item_size,compare_func) qsort(data,num_items,item_size,compare_func)
392  #endif
393 
394  // #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h
395  #ifndef STBTT_ifloor
396  #include <math.h>
397  #define STBTT_ifloor(x) ((int) floor(x))
398  #define STBTT_iceil(x) ((int) ceil(x))
399  #endif
400 
401  #ifndef STBTT_sqrt
402  #include <math.h>
403  #define STBTT_sqrt(x) sqrt(x)
404  #endif
405 
406  // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h
407  #ifndef STBTT_malloc
408  #include <stdlib.h>
409  #define STBTT_malloc(x,u) ((void)(u),malloc(x))
410  #define STBTT_free(x,u) ((void)(u),free(x))
411  #endif
412 
413  #ifndef STBTT_assert
414  #include <assert.h>
415  #define STBTT_assert(x) assert(x)
416  #endif
417 
418  #ifndef STBTT_strlen
419  #include <string.h>
420  #define STBTT_strlen(x) strlen(x)
421  #endif
422 
423  #ifndef STBTT_memcpy
424  #include <memory.h>
425  #define STBTT_memcpy memcpy
426  #define STBTT_memset memset
427  #endif
428 #endif
429 
436 
437 #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
438 #define __STB_INCLUDE_STB_TRUETYPE_H__
439 
440 #ifdef __cplusplus
441 extern "C" {
442 #endif
443 
445 //
446 // TEXTURE BAKING API
447 //
448 // If you use this API, you only have to call two functions ever.
449 //
450 
451 typedef struct
452 {
453  unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap
454  float xoff,yoff,xadvance;
456 
457 STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)
458  float pixel_height, // height of font in pixels
459  unsigned char *pixels, int pw, int ph, // bitmap to be filled in
460  int first_char, int num_chars, // characters to bake
461  stbtt_bakedchar *chardata); // you allocate this, it's num_chars long
462 // if return is positive, the first unused row of the bitmap
463 // if return is negative, returns the negative of the number of characters that fit
464 // if return is 0, no characters fit and no rows were used
465 // This uses a very crappy packing.
466 
467 typedef struct
468 {
469  float x0,y0,s0,t0; // top-left
470  float x1,y1,s1,t1; // bottom-right
472 
473 STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, // same data as above
474  int char_index, // character to display
475  float *xpos, float *ypos, // pointers to current position in screen pixel space
476  stbtt_aligned_quad *q, // output: quad to draw
477  int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier
478 // Call GetBakedQuad with char_index = 'character - first_char', and it
479 // creates the quad you need to draw and advances the current position.
480 //
481 // The coordinate system used assumes y increases downwards.
482 //
483 // Characters will extend both above and below the current position;
484 // see discussion of "BASELINE" above.
485 //
486 // It's inefficient; you might want to c&p it and optimize it.
487 
488 
489 
491 //
492 // NEW TEXTURE BAKING API
493 //
494 // This provides options for packing multiple fonts into one atlas, not
495 // perfectly but better than nothing.
496 
497 typedef struct
498 {
499  unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap
500  float xoff,yoff,xadvance;
501  float xoff2,yoff2;
503 
505 typedef struct stbtt_fontinfo stbtt_fontinfo;
506 #ifndef STB_RECT_PACK_VERSION
507 typedef struct stbrp_rect stbrp_rect;
508 #endif
509 
510 STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context);
511 // Initializes a packing context stored in the passed-in stbtt_pack_context.
512 // Future calls using this context will pack characters into the bitmap passed
513 // in here: a 1-channel bitmap that is weight x height. stride_in_bytes is
514 // the distance from one row to the next (or 0 to mean they are packed tightly
515 // together). "padding" is // the amount of padding to leave between each
516 // character (normally you want '1' for bitmaps you'll use as textures with
517 // bilinear filtering).
518 //
519 // Returns 0 on failure, 1 on success.
520 
521 STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc);
522 // Cleans up the packing context and frees all memory.
523 
524 #define STBTT_POINT_SIZE(x) (-(x))
525 
526 STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,
527  int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);
528 // Creates character bitmaps from the font_index'th font found in fontdata (use
529 // font_index=0 if you don't know what that is). It creates num_chars_in_range
530 // bitmaps for characters with unicode values starting at first_unicode_char_in_range
531 // and increasing. Data for how to render them is stored in chardata_for_range;
532 // pass these to stbtt_GetPackedQuad to get back renderable quads.
533 //
534 // font_size is the full height of the character from ascender to descender,
535 // as computed by stbtt_ScaleForPixelHeight. To use a point size as computed
536 // by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE()
537 // and pass that result as 'font_size':
538 // ..., 20 , ... // font max minus min y is 20 pixels tall
539 // ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall
540 
541 typedef struct
542 {
543  float font_size;
548 
549 STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);
550 // Creates character bitmaps from multiple ranges of characters stored in
551 // ranges. This will usually create a better-packed bitmap than multiple
552 // calls to stbtt_PackFontRange.
553 
554 STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
555 STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
556 // Those functions are called by stbtt_PackFontRanges(). If you want to
557 // pack multiple fonts or custom data into a same texture, you may copy
558 // the contents of stbtt_PackFontRanges() and create a custom version
559 // using those functions.
560 
561 STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);
562 // Oversampling a font increases the quality by allowing higher-quality subpixel
563 // positioning, and is especially valuable at smaller text sizes.
564 //
565 // This function sets the amount of oversampling for all following calls to
566 // stbtt_PackFontRange(s). The default (no oversampling) is achieved by
567 // h_oversample=1, v_oversample=1. The total number of pixels required is
568 // h_oversample*v_oversample larger than the default; for example, 2x2
569 // oversampling requires 4x the storage of 1x1. For best results, render
570 // oversampled textures with bilinear filtering. Look at the readme in
571 // stb/tests/oversample for information about oversampled fonts
572 
573 STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, // same data as above
574  int char_index, // character to display
575  float *xpos, float *ypos, // pointers to current position in screen pixel space
576  stbtt_aligned_quad *q, // output: quad to draw
577  int align_to_integer);
578 
579 // this is an opaque structure that you shouldn't mess with which holds
580 // all the context needed from PackBegin to PackEnd.
583  void *pack_info;
584  int width;
585  int height;
587  int padding;
588  unsigned int h_oversample, v_oversample;
589  unsigned char *pixels;
590  void *nodes;
591 };
592 
594 //
595 // FONT LOADING
596 //
597 //
598 
599 STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);
600 // Each .ttf/.ttc file may have more than one font. Each font has a sequential
601 // index number starting from 0. Call this function to get the font offset for
602 // a given index; it returns -1 if the index is out of range. A regular .ttf
603 // file will only define one font and it always be at offset 0, so it will
604 // return '0' for index 0, and -1 for all other indices. You can just skip
605 // this step if you know it's that kind of font.
606 
607 
608 // The following structure is defined publically so you can declare one on
609 // the stack or as a global or etc, but you should treat it as opaque.
610 typedef struct stbtt_fontinfo
611 {
612  void * userdata;
613  unsigned char * data; // pointer to .ttf file
614  int fontstart; // offset of start of font
615 
616  int numGlyphs; // number of glyphs, needed for range checking
617 
618  int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
619  int index_map; // a cmap mapping for our chosen character encoding
620  int indexToLocFormat; // format needed to map from glyph index to glyph
622 
623 STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);
624 // Given an offset into the file that defines a font, this function builds
625 // the necessary cached info for the rest of the system. You must allocate
626 // the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't
627 // need to do anything special to free it, because the contents are pure
628 // value data with no additional data structures. Returns 0 on failure.
629 
630 
632 //
633 // CHARACTER TO GLYPH-INDEX CONVERSIOn
634 
635 STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);
636 // If you're going to perform multiple operations on the same character
637 // and you want a speed-up, call this function with the character you're
638 // going to process, then use glyph-based functions instead of the
639 // codepoint-based functions.
640 
641 
643 //
644 // CHARACTER PROPERTIES
645 //
646 
647 STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);
648 // computes a scale factor to produce a font whose "height" is 'pixels' tall.
649 // Height is measured as the distance from the highest ascender to the lowest
650 // descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics
651 // and computing:
652 // scale = pixels / (ascent - descent)
653 // so if you prefer to measure height by the ascent only, use a similar calculation.
654 
655 STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);
656 // computes a scale factor to produce a font whose EM size is mapped to
657 // 'pixels' tall. This is probably what traditional APIs compute, but
658 // I'm not positive.
659 
660 STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);
661 // ascent is the coordinate above the baseline the font extends; descent
662 // is the coordinate below the baseline the font extends (i.e. it is typically negative)
663 // lineGap is the spacing between one row's descent and the next row's ascent...
664 // so you should advance the vertical position by "*ascent - *descent + *lineGap"
665 // these are expressed in unscaled coordinates, so you must multiply by
666 // the scale factor for a given size
667 
668 STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);
669 // the bounding box around all possible characters
670 
671 STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);
672 // leftSideBearing is the offset from the current horizontal position to the left edge of the character
673 // advanceWidth is the offset from the current horizontal position to the next horizontal position
674 // these are expressed in unscaled coordinates
675 
676 STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);
677 // an additional amount to add to the 'advance' value between ch1 and ch2
678 
679 STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);
680 // Gets the bounding box of the visible part of the glyph, in unscaled coordinates
681 
682 STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);
683 STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);
684 STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
685 // as above, but takes one or more glyph indices for greater efficiency
686 
687 
689 //
690 // GLYPH SHAPES (you probably don't need these, but they have to go before
691 // the bitmaps for C declaration-order reasons)
692 //
693 
694 #ifndef STBTT_vmove // you can predefine these to use different values (but why?)
695  enum {
699  };
700 #endif
701 
702 #ifndef stbtt_vertex // you can predefine this to use different values
703  // (we share this with other code at RAD)
704  #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file
705  typedef struct
706  {
708  unsigned char type,padding;
709  } stbtt_vertex;
710 #endif
711 
712 STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);
713 // returns non-zero if nothing is drawn for this glyph
714 
715 STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);
716 STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);
717 // returns # of vertices and fills *vertices with the pointer to them
718 // these are expressed in "unscaled" coordinates
719 //
720 // The shape is a series of countours. Each one starts with
721 // a STBTT_moveto, then consists of a series of mixed
722 // STBTT_lineto and STBTT_curveto segments. A lineto
723 // draws a line from previous endpoint to its x,y; a curveto
724 // draws a quadratic bezier from previous endpoint to
725 // its x,y, using cx,cy as the bezier control point.
726 
727 STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);
728 // frees the data allocated above
729 
731 //
732 // BITMAP RENDERING
733 //
734 
735 STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);
736 // frees the bitmap allocated below
737 
738 STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff);
739 // allocates a large-enough single-channel 8bpp bitmap and renders the
740 // specified character/glyph at the specified scale into it, with
741 // antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque).
742 // *width & *height are filled out with the width & height of the bitmap,
743 // which is stored left-to-right, top-to-bottom.
744 //
745 // xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap
746 
747 STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff);
748 // the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel
749 // shift for the character
750 
751 STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint);
752 // the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap
753 // in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap
754 // is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the
755 // width and height and positioning info for it first.
756 
757 STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint);
758 // same as stbtt_MakeCodepointBitmap, but you can specify a subpixel
759 // shift for the character
760 
761 STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);
762 // get the bbox of the bitmap centered around the glyph origin; so the
763 // bitmap width is ix1-ix0, height is iy1-iy0, and location to place
764 // the bitmap top left is (leftSideBearing*scale,iy0).
765 // (Note that the bitmap uses y-increases-down, but the shape uses
766 // y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.)
767 
768 STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);
769 // same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel
770 // shift for the character
771 
772 // the following functions are equivalent to the above functions, but operate
773 // on glyph indices instead of Unicode codepoints (for efficiency)
774 STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff);
775 STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff);
776 STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph);
777 STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph);
778 STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);
779 STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);
780 
781 
782 // @TODO: don't expose this structure
783 typedef struct
784 {
785  int w,h,stride;
786  unsigned char *pixels;
787 } stbtt__bitmap;
788 
789 STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata);
790 
792 //
793 // Finding the right font...
794 //
795 // You should really just solve this offline, keep your own tables
796 // of what font is what, and don't try to get it out of the .ttf file.
797 // That's because getting it out of the .ttf file is really hard, because
798 // the names in the file can appear in many possible encodings, in many
799 // possible languages, and e.g. if you need a case-insensitive comparison,
800 // the details of that depend on the encoding & language in a complex way
801 // (actually underspecified in truetype, but also gigantic).
802 //
803 // But you can use the provided functions in two possible ways:
804 // stbtt_FindMatchingFont() will use *case-sensitive* comparisons on
805 // unicode-encoded names to try to find the font you want;
806 // you can run this before calling stbtt_InitFont()
807 //
808 // stbtt_GetFontNameString() lets you get any of the various strings
809 // from the file yourself and do your own comparisons on them.
810 // You have to have called stbtt_InitFont() first.
811 
812 
813 STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);
814 // returns the offset (not index) of the font that matches, or -1 if none
815 // if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold".
816 // if you use any other flag, use a font name like "Arial"; this checks
817 // the 'macStyle' header field; i don't know if fonts set this consistently
818 #define STBTT_MACSTYLE_DONTCARE 0
819 #define STBTT_MACSTYLE_BOLD 1
820 #define STBTT_MACSTYLE_ITALIC 2
821 #define STBTT_MACSTYLE_UNDERSCORE 4
822 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0
823 
824 STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);
825 // returns 1/0 whether the first string interpreted as utf8 is identical to
826 // the second string interpreted as big-endian utf16... useful for strings from next func
827 
828 STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);
829 // returns the string (which may be big-endian double byte, e.g. for unicode)
830 // and puts the length in bytes in *length.
831 //
832 // some of the values for the IDs are below; for more see the truetype spec:
833 // http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html
834 // http://www.microsoft.com/typography/otspec/name.htm
835 
836 enum { // platformID
841 };
842 
843 enum { // encodingID for STBTT_PLATFORM_ID_UNICODE
849 };
850 
851 enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT
856 };
857 
858 enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes
863 };
864 
865 enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID...
866  // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs
873 };
874 
875 enum { // languageID for STBTT_PLATFORM_ID_MAC
883 };
884 
885 #ifdef __cplusplus
886 }
887 #endif
888 
889 #endif // __STB_INCLUDE_STB_TRUETYPE_H__
890 
897 
898 #ifdef STB_TRUETYPE_IMPLEMENTATION
899 
900 #ifndef STBTT_MAX_OVERSAMPLE
901 #define STBTT_MAX_OVERSAMPLE 8
902 #endif
903 
904 typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
905 
907 //
908 // accessors to parse data from file
909 //
910 
911 // on platforms that don't allow misaligned reads, if we want to allow
912 // truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE
913 
914 #define ttBYTE(p) (* (stbtt_uint8 *) (p))
915 #define ttCHAR(p) (* (stbtt_int8 *) (p))
916 #define ttFixed(p) ttLONG(p)
917 
918 #if defined(STB_TRUETYPE_BIGENDIAN) && !defined(ALLOW_UNALIGNED_TRUETYPE)
919 
920  #define ttUSHORT(p) (* (stbtt_uint16 *) (p))
921  #define ttSHORT(p) (* (stbtt_int16 *) (p))
922  #define ttULONG(p) (* (stbtt_uint32 *) (p))
923  #define ttLONG(p) (* (stbtt_int32 *) (p))
924 
925 #else
926 
927  static stbtt_uint16 ttUSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }
928  static stbtt_int16 ttSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }
929  static stbtt_uint32 ttULONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
930  static stbtt_int32 ttLONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
931 
932 #endif
933 
934 #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
935 #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
936 
937 static int stbtt__isfont(const stbtt_uint8 *font)
938 {
939  // check the version number
940  if (stbtt_tag4(font, '1',0,0,0)) return 1; // TrueType 1
941  if (stbtt_tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this!
942  if (stbtt_tag(font, "OTTO")) return 1; // OpenType with CFF
943  if (stbtt_tag4(font, 0,1,0,0)) return 1; // OpenType 1.0
944  return 0;
945 }
946 
947 // @OPTIMIZE: binary search
948 static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag)
949 {
950  stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
951  stbtt_uint32 tabledir = fontstart + 12;
952  stbtt_int32 i;
953  for (i=0; i < num_tables; ++i) {
954  stbtt_uint32 loc = tabledir + 16*i;
955  if (stbtt_tag(data+loc+0, tag))
956  return ttULONG(data+loc+8);
957  }
958  return 0;
959 }
960 
961 STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *font_collection, int index)
962 {
963  // if it's just a font, there's only one valid index
964  if (stbtt__isfont(font_collection))
965  return index == 0 ? 0 : -1;
966 
967  // check if it's a TTC
968  if (stbtt_tag(font_collection, "ttcf")) {
969  // version 1?
970  if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
971  stbtt_int32 n = ttLONG(font_collection+8);
972  if (index >= n)
973  return -1;
974  return ttULONG(font_collection+12+index*14);
975  }
976  }
977  return -1;
978 }
979 
980 STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart)
981 {
982  stbtt_uint8 *data = (stbtt_uint8 *) data2;
983  stbtt_uint32 cmap, t;
984  stbtt_int32 i,numTables;
985 
986  info->data = data;
987  info->fontstart = fontstart;
988 
989  cmap = stbtt__find_table(data, fontstart, "cmap"); // required
990  info->loca = stbtt__find_table(data, fontstart, "loca"); // required
991  info->head = stbtt__find_table(data, fontstart, "head"); // required
992  info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required
993  info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required
994  info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required
995  info->kern = stbtt__find_table(data, fontstart, "kern"); // not required
996  if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
997  return 0;
998 
999  t = stbtt__find_table(data, fontstart, "maxp");
1000  if (t)
1001  info->numGlyphs = ttUSHORT(data+t+4);
1002  else
1003  info->numGlyphs = 0xffff;
1004 
1005  // find a cmap encoding table we understand *now* to avoid searching
1006  // later. (todo: could make this installable)
1007  // the same regardless of glyph.
1008  numTables = ttUSHORT(data + cmap + 2);
1009  info->index_map = 0;
1010  for (i=0; i < numTables; ++i) {
1011  stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
1012  // find an encoding we understand:
1013  switch(ttUSHORT(data+encoding_record)) {
1015  switch (ttUSHORT(data+encoding_record+2)) {
1018  // MS/Unicode
1019  info->index_map = cmap + ttULONG(data+encoding_record+4);
1020  break;
1021  }
1022  break;
1024  // Mac/iOS has these
1025  // all the encodingIDs are unicode, so we don't bother to check it
1026  info->index_map = cmap + ttULONG(data+encoding_record+4);
1027  break;
1028  }
1029  }
1030  if (info->index_map == 0)
1031  return 0;
1032 
1033  info->indexToLocFormat = ttUSHORT(data+info->head + 50);
1034  return 1;
1035 }
1036 
1037 STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
1038 {
1039  stbtt_uint8 *data = info->data;
1040  stbtt_uint32 index_map = info->index_map;
1041 
1042  stbtt_uint16 format = ttUSHORT(data + index_map + 0);
1043  if (format == 0) { // apple byte encoding
1044  stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1045  if (unicode_codepoint < bytes-6)
1046  return ttBYTE(data + index_map + 6 + unicode_codepoint);
1047  return 0;
1048  } else if (format == 6) {
1049  stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1050  stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1051  if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
1052  return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
1053  return 0;
1054  } else if (format == 2) {
1055  STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean
1056  return 0;
1057  } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges
1058  stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
1059  stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
1060  stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
1061  stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
1062  stbtt_uint16 item, offset, start, end;
1063 
1064  // do a binary search of the segments
1065  stbtt_uint32 endCount = index_map + 14;
1066  stbtt_uint32 search = endCount;
1067 
1068  if (unicode_codepoint > 0xffff)
1069  return 0;
1070 
1071  // they lie from endCount .. endCount + segCount
1072  // but searchRange is the nearest power of two, so...
1073  if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
1074  search += rangeShift*2;
1075 
1076  // now decrement to bias correctly to find smallest
1077  search -= 2;
1078  while (entrySelector) {
1079  searchRange >>= 1;
1080  start = ttUSHORT(data + search + searchRange*2 + segcount*2 + 2);
1081  end = ttUSHORT(data + search + searchRange*2);
1082  if (unicode_codepoint > end)
1083  search += searchRange*2;
1084  --entrySelector;
1085  }
1086  search += 2;
1087 
1088  item = (stbtt_uint16) ((search - endCount) >> 1);
1089 
1090  STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
1091  start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
1092  end = ttUSHORT(data + index_map + 14 + 2 + 2*item);
1093  if (unicode_codepoint < start)
1094  return 0;
1095 
1096  offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
1097  if (offset == 0)
1098  return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
1099 
1100  return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
1101  } else if (format == 12 || format == 13) {
1102  stbtt_uint32 ngroups = ttULONG(data+index_map+12);
1103  stbtt_int32 low,high;
1104  low = 0; high = (stbtt_int32)ngroups;
1105  // Binary search the right group.
1106  while (low < high) {
1107  stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high
1108  stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
1109  stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
1110  if ((stbtt_uint32) unicode_codepoint < start_char)
1111  high = mid;
1112  else if ((stbtt_uint32) unicode_codepoint > end_char)
1113  low = mid+1;
1114  else {
1115  stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
1116  if (format == 12)
1117  return start_glyph + unicode_codepoint-start_char;
1118  else // format == 13
1119  return start_glyph;
1120  }
1121  }
1122  return 0; // not found
1123  }
1124  // @TODO
1125  STBTT_assert(0);
1126  return 0;
1127 }
1128 
1129 STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)
1130 {
1131  return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
1132 }
1133 
1134 static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
1135 {
1136  v->type = type;
1137  v->x = (stbtt_int16) x;
1138  v->y = (stbtt_int16) y;
1139  v->cx = (stbtt_int16) cx;
1140  v->cy = (stbtt_int16) cy;
1141 }
1142 
1143 static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)
1144 {
1145  int g1,g2;
1146 
1147  if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range
1148  if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format
1149 
1150  if (info->indexToLocFormat == 0) {
1151  g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
1152  g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
1153  } else {
1154  g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
1155  g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
1156  }
1157 
1158  return g1==g2 ? -1 : g1; // if length is 0, return -1
1159 }
1160 
1161 STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
1162 {
1163  int g = stbtt__GetGlyfOffset(info, glyph_index);
1164  if (g < 0) return 0;
1165 
1166  if (x0) *x0 = ttSHORT(info->data + g + 2);
1167  if (y0) *y0 = ttSHORT(info->data + g + 4);
1168  if (x1) *x1 = ttSHORT(info->data + g + 6);
1169  if (y1) *y1 = ttSHORT(info->data + g + 8);
1170  return 1;
1171 }
1172 
1173 STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)
1174 {
1175  return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);
1176 }
1177 
1178 STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)
1179 {
1180  stbtt_int16 numberOfContours;
1181  int g = stbtt__GetGlyfOffset(info, glyph_index);
1182  if (g < 0) return 1;
1183  numberOfContours = ttSHORT(info->data + g);
1184  return numberOfContours == 0;
1185 }
1186 
1187 static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off,
1188  stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1189 {
1190  if (start_off) {
1191  if (was_off)
1192  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
1193  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);
1194  } else {
1195  if (was_off)
1196  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);
1197  else
1198  stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);
1199  }
1200  return num_vertices;
1201 }
1202 
1203 STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)
1204 {
1205  stbtt_int16 numberOfContours;
1206  stbtt_uint8 *endPtsOfContours;
1207  stbtt_uint8 *data = info->data;
1208  stbtt_vertex *vertices=0;
1209  int num_vertices=0;
1210  int g = stbtt__GetGlyfOffset(info, glyph_index);
1211 
1212  *pvertices = NULL;
1213 
1214  if (g < 0) return 0;
1215 
1216  numberOfContours = ttSHORT(data + g);
1217 
1218  if (numberOfContours > 0) {
1219  stbtt_uint8 flags=0,flagcount;
1220  stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
1221  stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
1222  stbtt_uint8 *points;
1223  endPtsOfContours = (data + g + 10);
1224  ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1225  points = data + g + 10 + numberOfContours * 2 + 2 + ins;
1226 
1227  n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
1228 
1229  m = n + 2*numberOfContours; // a loose bound on how many vertices we might need
1230  vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata);
1231  if (vertices == 0)
1232  return 0;
1233 
1234  next_move = 0;
1235  flagcount=0;
1236 
1237  // in first pass, we load uninterpreted data into the allocated array
1238  // above, shifted to the end of the array so we won't overwrite it when
1239  // we create our final data starting from the front
1240 
1241  off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated
1242 
1243  // first load flags
1244 
1245  for (i=0; i < n; ++i) {
1246  if (flagcount == 0) {
1247  flags = *points++;
1248  if (flags & 8)
1249  flagcount = *points++;
1250  } else
1251  --flagcount;
1252  vertices[off+i].type = flags;
1253  }
1254 
1255  // now load x coordinates
1256  x=0;
1257  for (i=0; i < n; ++i) {
1258  flags = vertices[off+i].type;
1259  if (flags & 2) {
1260  stbtt_int16 dx = *points++;
1261  x += (flags & 16) ? dx : -dx; // ???
1262  } else {
1263  if (!(flags & 16)) {
1264  x = x + (stbtt_int16) (points[0]*256 + points[1]);
1265  points += 2;
1266  }
1267  }
1268  vertices[off+i].x = (stbtt_int16) x;
1269  }
1270 
1271  // now load y coordinates
1272  y=0;
1273  for (i=0; i < n; ++i) {
1274  flags = vertices[off+i].type;
1275  if (flags & 4) {
1276  stbtt_int16 dy = *points++;
1277  y += (flags & 32) ? dy : -dy; // ???
1278  } else {
1279  if (!(flags & 32)) {
1280  y = y + (stbtt_int16) (points[0]*256 + points[1]);
1281  points += 2;
1282  }
1283  }
1284  vertices[off+i].y = (stbtt_int16) y;
1285  }
1286 
1287  // now convert them to our format
1288  num_vertices=0;
1289  sx = sy = cx = cy = scx = scy = 0;
1290  for (i=0; i < n; ++i) {
1291  flags = vertices[off+i].type;
1292  x = (stbtt_int16) vertices[off+i].x;
1293  y = (stbtt_int16) vertices[off+i].y;
1294 
1295  if (next_move == i) {
1296  if (i != 0)
1297  num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1298 
1299  // now start the new one
1300  start_off = !(flags & 1);
1301  if (start_off) {
1302  // if we start off with an off-curve point, then when we need to find a point on the curve
1303  // where we can start, and we need to save some state for when we wraparound.
1304  scx = x;
1305  scy = y;
1306  if (!(vertices[off+i+1].type & 1)) {
1307  // next point is also a curve point, so interpolate an on-point curve
1308  sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
1309  sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
1310  } else {
1311  // otherwise just use the next point as our start point
1312  sx = (stbtt_int32) vertices[off+i+1].x;
1313  sy = (stbtt_int32) vertices[off+i+1].y;
1314  ++i; // we're using point i+1 as the starting point, so skip it
1315  }
1316  } else {
1317  sx = x;
1318  sy = y;
1319  }
1320  stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);
1321  was_off = 0;
1322  next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
1323  ++j;
1324  } else {
1325  if (!(flags & 1)) { // if it's a curve
1326  if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint
1327  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
1328  cx = x;
1329  cy = y;
1330  was_off = 1;
1331  } else {
1332  if (was_off)
1333  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);
1334  else
1335  stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);
1336  was_off = 0;
1337  }
1338  }
1339  }
1340  num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1341  } else if (numberOfContours == -1) {
1342  // Compound shapes.
1343  int more = 1;
1344  stbtt_uint8 *comp = data + g + 10;
1345  num_vertices = 0;
1346  vertices = 0;
1347  while (more) {
1348  stbtt_uint16 flags, gidx;
1349  int comp_num_verts = 0, i;
1350  stbtt_vertex *comp_verts = 0, *tmp = 0;
1351  float mtx[6] = {1,0,0,1,0,0}, m, n;
1352 
1353  flags = ttSHORT(comp); comp+=2;
1354  gidx = ttSHORT(comp); comp+=2;
1355 
1356  if (flags & 2) { // XY values
1357  if (flags & 1) { // shorts
1358  mtx[4] = ttSHORT(comp); comp+=2;
1359  mtx[5] = ttSHORT(comp); comp+=2;
1360  } else {
1361  mtx[4] = ttCHAR(comp); comp+=1;
1362  mtx[5] = ttCHAR(comp); comp+=1;
1363  }
1364  }
1365  else {
1366  // @TODO handle matching point
1367  STBTT_assert(0);
1368  }
1369  if (flags & (1<<3)) { // WE_HAVE_A_SCALE
1370  mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1371  mtx[1] = mtx[2] = 0;
1372  } else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE
1373  mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1374  mtx[1] = mtx[2] = 0;
1375  mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1376  } else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO
1377  mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1378  mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
1379  mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
1380  mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1381  }
1382 
1383  // Find transformation scales.
1384  m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
1385  n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
1386 
1387  // Get indexed glyph.
1388  comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
1389  if (comp_num_verts > 0) {
1390  // Transform vertices.
1391  for (i = 0; i < comp_num_verts; ++i) {
1392  stbtt_vertex* v = &comp_verts[i];
1393  stbtt_vertex_type x,y;
1394  x=v->x; y=v->y;
1395  v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1396  v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1397  x=v->cx; y=v->cy;
1398  v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1399  v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1400  }
1401  // Append vertices.
1402  tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata);
1403  if (!tmp) {
1404  if (vertices) STBTT_free(vertices, info->userdata);
1405  if (comp_verts) STBTT_free(comp_verts, info->userdata);
1406  return 0;
1407  }
1408  if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex));
1409  STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));
1410  if (vertices) STBTT_free(vertices, info->userdata);
1411  vertices = tmp;
1412  STBTT_free(comp_verts, info->userdata);
1413  num_vertices += comp_num_verts;
1414  }
1415  // More components ?
1416  more = flags & (1<<5);
1417  }
1418  } else if (numberOfContours < 0) {
1419  // @TODO other compound variations?
1420  STBTT_assert(0);
1421  } else {
1422  // numberOfCounters == 0, do nothing
1423  }
1424 
1425  *pvertices = vertices;
1426  return num_vertices;
1427 }
1428 
1429 STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)
1430 {
1431  stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
1432  if (glyph_index < numOfLongHorMetrics) {
1433  if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
1434  if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
1435  } else {
1436  if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
1437  if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
1438  }
1439 }
1440 
1441 STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)
1442 {
1443  stbtt_uint8 *data = info->data + info->kern;
1444  stbtt_uint32 needle, straw;
1445  int l, r, m;
1446 
1447  // we only look at the first table. it must be 'horizontal' and format 0.
1448  if (!info->kern)
1449  return 0;
1450  if (ttUSHORT(data+2) < 1) // number of tables, need at least 1
1451  return 0;
1452  if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format
1453  return 0;
1454 
1455  l = 0;
1456  r = ttUSHORT(data+10) - 1;
1457  needle = glyph1 << 16 | glyph2;
1458  while (l <= r) {
1459  m = (l + r) >> 1;
1460  straw = ttULONG(data+18+(m*6)); // note: unaligned read
1461  if (needle < straw)
1462  r = m - 1;
1463  else if (needle > straw)
1464  l = m + 1;
1465  else
1466  return ttSHORT(data+22+(m*6));
1467  }
1468  return 0;
1469 }
1470 
1471 STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)
1472 {
1473  if (!info->kern) // if no kerning table, don't waste time looking up both codepoint->glyphs
1474  return 0;
1475  return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));
1476 }
1477 
1478 STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)
1479 {
1480  stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
1481 }
1482 
1483 STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
1484 {
1485  if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
1486  if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
1487  if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
1488 }
1489 
1490 STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)
1491 {
1492  *x0 = ttSHORT(info->data + info->head + 36);
1493  *y0 = ttSHORT(info->data + info->head + 38);
1494  *x1 = ttSHORT(info->data + info->head + 40);
1495  *y1 = ttSHORT(info->data + info->head + 42);
1496 }
1497 
1498 STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)
1499 {
1500  int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
1501  return (float) height / fheight;
1502 }
1503 
1504 STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)
1505 {
1506  int unitsPerEm = ttUSHORT(info->data + info->head + 18);
1507  return pixels / unitsPerEm;
1508 }
1509 
1510 STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)
1511 {
1512  STBTT_free(v, info->userdata);
1513 }
1514 
1516 //
1517 // antialiasing software rasterizer
1518 //
1519 
1520 STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
1521 {
1522  int x0,y0,x1,y1;
1523  if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
1524  // e.g. space character
1525  if (ix0) *ix0 = 0;
1526  if (iy0) *iy0 = 0;
1527  if (ix1) *ix1 = 0;
1528  if (iy1) *iy1 = 0;
1529  } else {
1530  // move to integral bboxes (treating pixels as little squares, what pixels get touched)?
1531  if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
1532  if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
1533  if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
1534  if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
1535  }
1536 }
1537 
1538 STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
1539 {
1540  stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
1541 }
1542 
1543 STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
1544 {
1545  stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
1546 }
1547 
1548 STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
1549 {
1550  stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);
1551 }
1552 
1553 typedef struct stbtt__edge {
1554  float x0,y0, x1,y1;
1555  int invert;
1556 } stbtt__edge;
1557 
1558 typedef struct stbtt__active_edge
1559 {
1560  int x,dx;
1561  float ey;
1562  struct stbtt__active_edge *next;
1563  int valid;
1564 } stbtt__active_edge;
1565 
1566 #define FIXSHIFT 10
1567 #define FIX (1 << FIXSHIFT)
1568 #define FIXMASK (FIX-1)
1569 
1570 static stbtt__active_edge *new_active(stbtt__edge *e, int off_x, float start_point, void *userdata)
1571 {
1572  stbtt__active_edge *z = (stbtt__active_edge *) STBTT_malloc(sizeof(*z), userdata); // @TODO: make a pool of these!!!
1573  float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
1574  STBTT_assert(e->y0 <= start_point);
1575  if (!z) return z;
1576  // round dx down to avoid going too far
1577  if (dxdy < 0)
1578  z->dx = -STBTT_ifloor(FIX * -dxdy);
1579  else
1580  z->dx = STBTT_ifloor(FIX * dxdy);
1581  z->x = STBTT_ifloor(FIX * (e->x0 + dxdy * (start_point - e->y0)));
1582  z->x -= off_x * FIX;
1583  z->ey = e->y1;
1584  z->next = 0;
1585  z->valid = e->invert ? 1 : -1;
1586  return z;
1587 }
1588 
1589 // note: this routine clips fills that extend off the edges... ideally this
1590 // wouldn't happen, but it could happen if the truetype glyph bounding boxes
1591 // are wrong, or if the user supplies a too-small bitmap
1592 static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight)
1593 {
1594  // non-zero winding fill
1595  int x0=0, w=0;
1596 
1597  while (e) {
1598  if (w == 0) {
1599  // if we're currently at zero, we need to record the edge start point
1600  x0 = e->x; w += e->valid;
1601  } else {
1602  int x1 = e->x; w += e->valid;
1603  // if we went to zero, we need to draw
1604  if (w == 0) {
1605  int i = x0 >> FIXSHIFT;
1606  int j = x1 >> FIXSHIFT;
1607 
1608  if (i < len && j >= 0) {
1609  if (i == j) {
1610  // x0,x1 are the same pixel, so compute combined coverage
1611  scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> FIXSHIFT);
1612  } else {
1613  if (i >= 0) // add antialiasing for x0
1614  scanline[i] = scanline[i] + (stbtt_uint8) (((FIX - (x0 & FIXMASK)) * max_weight) >> FIXSHIFT);
1615  else
1616  i = -1; // clip
1617 
1618  if (j < len) // add antialiasing for x1
1619  scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & FIXMASK) * max_weight) >> FIXSHIFT);
1620  else
1621  j = len; // clip
1622 
1623  for (++i; i < j; ++i) // fill pixels between x0 and x1
1624  scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
1625  }
1626  }
1627  }
1628  }
1629 
1630  e = e->next;
1631  }
1632 }
1633 
1634 static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
1635 {
1636  stbtt__active_edge *active = NULL;
1637  int y,j=0;
1638  int max_weight = (255 / vsubsample); // weight per vertical scanline
1639  int s; // vertical subsample index
1640  unsigned char scanline_data[512], *scanline;
1641 
1642  if (result->w > 512)
1643  scanline = (unsigned char *) STBTT_malloc(result->w, userdata);
1644  else
1645  scanline = scanline_data;
1646 
1647  y = off_y * vsubsample;
1648  e[n].y0 = (off_y + result->h) * (float) vsubsample + 1;
1649 
1650  while (j < result->h) {
1651  STBTT_memset(scanline, 0, result->w);
1652  for (s=0; s < vsubsample; ++s) {
1653  // find center of pixel for this scanline
1654  float scan_y = y + 0.5f;
1655  stbtt__active_edge **step = &active;
1656 
1657  // update all active edges;
1658  // remove all active edges that terminate before the center of this scanline
1659  while (*step) {
1660  stbtt__active_edge * z = *step;
1661  if (z->ey <= scan_y) {
1662  *step = z->next; // delete from list
1663  STBTT_assert(z->valid);
1664  z->valid = 0;
1665  STBTT_free(z, userdata);
1666  } else {
1667  z->x += z->dx; // advance to position for current scanline
1668  step = &((*step)->next); // advance through list
1669  }
1670  }
1671 
1672  // resort the list if needed
1673  for(;;) {
1674  int changed=0;
1675  step = &active;
1676  while (*step && (*step)->next) {
1677  if ((*step)->x > (*step)->next->x) {
1678  stbtt__active_edge *t = *step;
1679  stbtt__active_edge *q = t->next;
1680 
1681  t->next = q->next;
1682  q->next = t;
1683  *step = q;
1684  changed = 1;
1685  }
1686  step = &(*step)->next;
1687  }
1688  if (!changed) break;
1689  }
1690 
1691  // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
1692  while (e->y0 <= scan_y) {
1693  if (e->y1 > scan_y) {
1694  stbtt__active_edge *z = new_active(e, off_x, scan_y, userdata);
1695  // find insertion point
1696  if (active == NULL)
1697  active = z;
1698  else if (z->x < active->x) {
1699  // insert at front
1700  z->next = active;
1701  active = z;
1702  } else {
1703  // find thing to insert AFTER
1704  stbtt__active_edge *p = active;
1705  while (p->next && p->next->x < z->x)
1706  p = p->next;
1707  // at this point, p->next->x is NOT < z->x
1708  z->next = p->next;
1709  p->next = z;
1710  }
1711  }
1712  ++e;
1713  }
1714 
1715  // now process all active edges in XOR fashion
1716  if (active)
1717  stbtt__fill_active_edges(scanline, result->w, active, max_weight);
1718 
1719  ++y;
1720  }
1721  STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
1722  ++j;
1723  }
1724 
1725  while (active) {
1726  stbtt__active_edge *z = active;
1727  active = active->next;
1728  STBTT_free(z, userdata);
1729  }
1730 
1731  if (scanline != scanline_data)
1732  STBTT_free(scanline, userdata);
1733 }
1734 
1735 static int stbtt__edge_compare(const void *p, const void *q)
1736 {
1737  stbtt__edge *a = (stbtt__edge *) p;
1738  stbtt__edge *b = (stbtt__edge *) q;
1739 
1740  if (a->y0 < b->y0) return -1;
1741  if (a->y0 > b->y0) return 1;
1742  return 0;
1743 }
1744 
1745 typedef struct
1746 {
1747  float x,y;
1748 } stbtt__point;
1749 
1750 static void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts, int *wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void *userdata)
1751 {
1752  float y_scale_inv = invert ? -scale_y : scale_y;
1753  stbtt__edge *e;
1754  int n,i,j,k,m;
1755  int vsubsample = result->h < 8 ? 15 : 5;
1756  // vsubsample should divide 255 evenly; otherwise we won't reach full opacity
1757 
1758  // now we have to blow out the windings into explicit edge lists
1759  n = 0;
1760  for (i=0; i < windings; ++i)
1761  n += wcount[i];
1762 
1763  e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); // add an extra one as a sentinel
1764  if (e == 0) return;
1765  n = 0;
1766 
1767  m=0;
1768  for (i=0; i < windings; ++i) {
1769  stbtt__point *p = pts + m;
1770  m += wcount[i];
1771  j = wcount[i]-1;
1772  for (k=0; k < wcount[i]; j=k++) {
1773  int a=k,b=j;
1774  // skip the edge if horizontal
1775  if (p[j].y == p[k].y)
1776  continue;
1777  // add edge from j to k to the list
1778  e[n].invert = 0;
1779  if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
1780  e[n].invert = 1;
1781  a=j,b=k;
1782  }
1783  e[n].x0 = p[a].x * scale_x + shift_x;
1784  e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
1785  e[n].x1 = p[b].x * scale_x + shift_x;
1786  e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
1787  ++n;
1788  }
1789  }
1790 
1791  // now sort the edges by their highest point (should snap to integer, and then by x)
1792  STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare);
1793 
1794  // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule
1795  stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
1796 
1797  STBTT_free(e, userdata);
1798 }
1799 
1800 static void stbtt__add_point(stbtt__point *points, int n, float x, float y)
1801 {
1802  if (!points) return; // during first pass, it's unallocated
1803  points[n].x = x;
1804  points[n].y = y;
1805 }
1806 
1807 // tesselate until threshhold p is happy... @TODO warped to compensate for non-linear stretching
1808 static int stbtt__tesselate_curve(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n)
1809 {
1810  // midpoint
1811  float mx = (x0 + 2*x1 + x2)/4;
1812  float my = (y0 + 2*y1 + y2)/4;
1813  // versus directly drawn line
1814  float dx = (x0+x2)/2 - mx;
1815  float dy = (y0+y2)/2 - my;
1816  if (n > 16) // 65536 segments on one curve better be enough!
1817  return 1;
1818  if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA
1819  stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
1820  stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
1821  } else {
1822  stbtt__add_point(points, *num_points,x2,y2);
1823  *num_points = *num_points+1;
1824  }
1825  return 1;
1826 }
1827 
1828 // returns number of contours
1829 static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)
1830 {
1831  stbtt__point *points=0;
1832  int num_points=0;
1833 
1834  float objspace_flatness_squared = objspace_flatness * objspace_flatness;
1835  int i,n=0,start=0, pass;
1836 
1837  // count how many "moves" there are to get the contour count
1838  for (i=0; i < num_verts; ++i)
1839  if (vertices[i].type == STBTT_vmove)
1840  ++n;
1841 
1842  *num_contours = n;
1843  if (n == 0) return 0;
1844 
1845  *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata);
1846 
1847  if (*contour_lengths == 0) {
1848  *num_contours = 0;
1849  return 0;
1850  }
1851 
1852  // make two passes through the points so we don't need to realloc
1853  for (pass=0; pass < 2; ++pass) {
1854  float x=0,y=0;
1855  if (pass == 1) {
1856  points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata);
1857  if (points == NULL) goto error;
1858  }
1859  num_points = 0;
1860  n= -1;
1861  for (i=0; i < num_verts; ++i) {
1862  switch (vertices[i].type) {
1863  case STBTT_vmove:
1864  // start the next contour
1865  if (n >= 0)
1866  (*contour_lengths)[n] = num_points - start;
1867  ++n;
1868  start = num_points;
1869 
1870  x = vertices[i].x, y = vertices[i].y;
1871  stbtt__add_point(points, num_points++, x,y);
1872  break;
1873  case STBTT_vline:
1874  x = vertices[i].x, y = vertices[i].y;
1875  stbtt__add_point(points, num_points++, x, y);
1876  break;
1877  case STBTT_vcurve:
1878  stbtt__tesselate_curve(points, &num_points, x,y,
1879  vertices[i].cx, vertices[i].cy,
1880  vertices[i].x, vertices[i].y,
1881  objspace_flatness_squared, 0);
1882  x = vertices[i].x, y = vertices[i].y;
1883  break;
1884  }
1885  }
1886  (*contour_lengths)[n] = num_points - start;
1887  }
1888 
1889  return points;
1890 error:
1891  STBTT_free(points, userdata);
1892  STBTT_free(*contour_lengths, userdata);
1893  *contour_lengths = 0;
1894  *num_contours = 0;
1895  return NULL;
1896 }
1897 
1898 STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)
1899 {
1900  float scale = scale_x > scale_y ? scale_y : scale_x;
1901  int winding_count, *winding_lengths;
1902  stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
1903  if (windings) {
1904  stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
1905  STBTT_free(winding_lengths, userdata);
1906  STBTT_free(windings, userdata);
1907  }
1908 }
1909 
1910 STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)
1911 {
1912  STBTT_free(bitmap, userdata);
1913 }
1914 
1915 STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff)
1916 {
1917  int ix0,iy0,ix1,iy1;
1918  stbtt__bitmap gbm;
1919  stbtt_vertex *vertices;
1920  int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
1921 
1922  if (scale_x == 0) scale_x = scale_y;
1923  if (scale_y == 0) {
1924  if (scale_x == 0) return NULL;
1925  scale_y = scale_x;
1926  }
1927 
1928  stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
1929 
1930  // now we get the size
1931  gbm.w = (ix1 - ix0);
1932  gbm.h = (iy1 - iy0);
1933  gbm.pixels = NULL; // in case we error
1934 
1935  if (width ) *width = gbm.w;
1936  if (height) *height = gbm.h;
1937  if (xoff ) *xoff = ix0;
1938  if (yoff ) *yoff = iy0;
1939 
1940  if (gbm.w && gbm.h) {
1941  gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);
1942  if (gbm.pixels) {
1943  gbm.stride = gbm.w;
1944 
1945  stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
1946  }
1947  }
1948  STBTT_free(vertices, info->userdata);
1949  return gbm.pixels;
1950 }
1951 
1952 STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff)
1953 {
1954  return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
1955 }
1956 
1957 STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph)
1958 {
1959  int ix0,iy0;
1960  stbtt_vertex *vertices;
1961  int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
1962  stbtt__bitmap gbm;
1963 
1964  stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);
1965  gbm.pixels = output;
1966  gbm.w = out_w;
1967  gbm.h = out_h;
1968  gbm.stride = out_stride;
1969 
1970  if (gbm.w && gbm.h)
1971  stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);
1972 
1973  STBTT_free(vertices, info->userdata);
1974 }
1975 
1976 STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph)
1977 {
1978  stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);
1979 }
1980 
1981 STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff)
1982 {
1983  return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
1984 }
1985 
1986 STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint)
1987 {
1988  stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));
1989 }
1990 
1991 STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff)
1992 {
1993  return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);
1994 }
1995 
1996 STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint)
1997 {
1998  stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);
1999 }
2000 
2002 //
2003 // bitmap baking
2004 //
2005 // This is SUPER-CRAPPY packing to keep source code small
2006 
2007 STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)
2008  float pixel_height, // height of font in pixels
2009  unsigned char *pixels, int pw, int ph, // bitmap to be filled in
2010  int first_char, int num_chars, // characters to bake
2011  stbtt_bakedchar *chardata)
2012 {
2013  float scale;
2014  int x,y,bottom_y, i;
2015  stbtt_fontinfo f;
2016  if (!stbtt_InitFont(&f, data, offset))
2017  return -1;
2018  STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels
2019  x=y=1;
2020  bottom_y = 1;
2021 
2022  scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
2023 
2024  for (i=0; i < num_chars; ++i) {
2025  int advance, lsb, x0,y0,x1,y1,gw,gh;
2026  int g = stbtt_FindGlyphIndex(&f, first_char + i);
2027  stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
2028  stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
2029  gw = x1-x0;
2030  gh = y1-y0;
2031  if (x + gw + 1 >= pw)
2032  y = bottom_y, x = 1; // advance to next row
2033  if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row
2034  return -i;
2035  STBTT_assert(x+gw < pw);
2036  STBTT_assert(y+gh < ph);
2037  stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
2038  chardata[i].x0 = (stbtt_int16) x;
2039  chardata[i].y0 = (stbtt_int16) y;
2040  chardata[i].x1 = (stbtt_int16) (x + gw);
2041  chardata[i].y1 = (stbtt_int16) (y + gh);
2042  chardata[i].xadvance = scale * advance;
2043  chardata[i].xoff = (float) x0;
2044  chardata[i].yoff = (float) y0;
2045  x = x + gw + 1;
2046  if (y+gh+1 > bottom_y)
2047  bottom_y = y+gh+1;
2048  }
2049  return bottom_y;
2050 }
2051 
2052 STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)
2053 {
2054  float d3d_bias = opengl_fillrule ? 0 : -0.5f;
2055  float ipw = 1.0f / pw, iph = 1.0f / ph;
2056  stbtt_bakedchar *b = chardata + char_index;
2057  int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5);
2058  int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5);
2059 
2060  q->x0 = round_x + d3d_bias;
2061  q->y0 = round_y + d3d_bias;
2062  q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
2063  q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
2064 
2065  q->s0 = b->x0 * ipw;
2066  q->t0 = b->y0 * iph;
2067  q->s1 = b->x1 * ipw;
2068  q->t1 = b->y1 * iph;
2069 
2070  *xpos += b->xadvance;
2071 }
2072 
2074 //
2075 // rectangle packing replacement routines if you don't have stb_rect_pack.h
2076 //
2077 
2078 #ifndef STB_RECT_PACK_VERSION
2079 #ifdef _MSC_VER
2080 #define STBTT__NOTUSED(v) (void)(v)
2081 #else
2082 #define STBTT__NOTUSED(v) (void)sizeof(v)
2083 #endif
2084 
2085 typedef int stbrp_coord;
2086 
2088 // //
2089 // //
2090 // COMPILER WARNING ?!?!? //
2091 // //
2092 // //
2093 // if you get a compile warning due to these symbols being defined more than //
2094 // once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" //
2095 // //
2097 
2098 typedef struct
2099 {
2100  int width,height;
2101  int x,y,bottom_y;
2102 } stbrp_context;
2103 
2104 typedef struct
2105 {
2106  unsigned char x;
2107 } stbrp_node;
2108 
2109 struct stbrp_rect
2110 {
2111  stbrp_coord x,y;
2112  int id,w,h,was_packed;
2113 };
2114 
2115 static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes)
2116 {
2117  con->width = pw;
2118  con->height = ph;
2119  con->x = 0;
2120  con->y = 0;
2121  con->bottom_y = 0;
2122  STBTT__NOTUSED(nodes);
2123  STBTT__NOTUSED(num_nodes);
2124 }
2125 
2126 static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects)
2127 {
2128  int i;
2129  for (i=0; i < num_rects; ++i) {
2130  if (con->x + rects[i].w > con->width) {
2131  con->x = 0;
2132  con->y = con->bottom_y;
2133  }
2134  if (con->y + rects[i].h > con->height)
2135  break;
2136  rects[i].x = con->x;
2137  rects[i].y = con->y;
2138  rects[i].was_packed = 1;
2139  con->x += rects[i].w;
2140  if (con->y + rects[i].h > con->bottom_y)
2141  con->bottom_y = con->y + rects[i].h;
2142  }
2143  for ( ; i < num_rects; ++i)
2144  rects[i].was_packed = 0;
2145 }
2146 #endif
2147 
2149 //
2150 // bitmap baking
2151 //
2152 // This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If
2153 // stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.
2154 
2155 STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)
2156 {
2157  stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context);
2158  int num_nodes = pw - padding;
2159  stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context);
2160 
2161  if (context == NULL || nodes == NULL) {
2162  if (context != NULL) STBTT_free(context, alloc_context);
2163  if (nodes != NULL) STBTT_free(nodes , alloc_context);
2164  return 0;
2165  }
2166 
2167  spc->user_allocator_context = alloc_context;
2168  spc->width = pw;
2169  spc->height = ph;
2170  spc->pixels = pixels;
2171  spc->pack_info = context;
2172  spc->nodes = nodes;
2173  spc->padding = padding;
2174  spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
2175  spc->h_oversample = 1;
2176  spc->v_oversample = 1;
2177 
2178  stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
2179 
2180  if (pixels)
2181  STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels
2182 
2183  return 1;
2184 }
2185 
2186 STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc)
2187 {
2190 }
2191 
2192 STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)
2193 {
2194  STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
2195  STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
2196  if (h_oversample <= STBTT_MAX_OVERSAMPLE)
2197  spc->h_oversample = h_oversample;
2198  if (v_oversample <= STBTT_MAX_OVERSAMPLE)
2199  spc->v_oversample = v_oversample;
2200 }
2201 
2202 #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
2203 
2204 static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
2205 {
2206  unsigned char buffer[STBTT_MAX_OVERSAMPLE];
2207  int safe_w = w - kernel_width;
2208  int j;
2209  for (j=0; j < h; ++j) {
2210  int i;
2211  unsigned int total;
2212  memset(buffer, 0, kernel_width);
2213 
2214  total = 0;
2215 
2216  // make kernel_width a constant in common cases so compiler can optimize out the divide
2217  switch (kernel_width) {
2218  case 2:
2219  for (i=0; i <= safe_w; ++i) {
2220  total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2221  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2222  pixels[i] = (unsigned char) (total / 2);
2223  }
2224  break;
2225  case 3:
2226  for (i=0; i <= safe_w; ++i) {
2227  total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2228  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2229  pixels[i] = (unsigned char) (total / 3);
2230  }
2231  break;
2232  case 4:
2233  for (i=0; i <= safe_w; ++i) {
2234  total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2235  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2236  pixels[i] = (unsigned char) (total / 4);
2237  }
2238  break;
2239  default:
2240  for (i=0; i <= safe_w; ++i) {
2241  total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2242  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2243  pixels[i] = (unsigned char) (total / kernel_width);
2244  }
2245  break;
2246  }
2247 
2248  for (; i < w; ++i) {
2249  STBTT_assert(pixels[i] == 0);
2250  total -= buffer[i & STBTT__OVER_MASK];
2251  pixels[i] = (unsigned char) (total / kernel_width);
2252  }
2253 
2254  pixels += stride_in_bytes;
2255  }
2256 }
2257 
2258 static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
2259 {
2260  unsigned char buffer[STBTT_MAX_OVERSAMPLE];
2261  int safe_h = h - kernel_width;
2262  int j;
2263  for (j=0; j < w; ++j) {
2264  int i;
2265  unsigned int total;
2266  memset(buffer, 0, kernel_width);
2267 
2268  total = 0;
2269 
2270  // make kernel_width a constant in common cases so compiler can optimize out the divide
2271  switch (kernel_width) {
2272  case 2:
2273  for (i=0; i <= safe_h; ++i) {
2274  total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2275  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2276  pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
2277  }
2278  break;
2279  case 3:
2280  for (i=0; i <= safe_h; ++i) {
2281  total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2282  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2283  pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
2284  }
2285  break;
2286  case 4:
2287  for (i=0; i <= safe_h; ++i) {
2288  total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2289  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2290  pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
2291  }
2292  break;
2293  default:
2294  for (i=0; i <= safe_h; ++i) {
2295  total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2296  buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2297  pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
2298  }
2299  break;
2300  }
2301 
2302  for (; i < h; ++i) {
2303  STBTT_assert(pixels[i*stride_in_bytes] == 0);
2304  total -= buffer[i & STBTT__OVER_MASK];
2305  pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
2306  }
2307 
2308  pixels += 1;
2309  }
2310 }
2311 
2312 static float stbtt__oversample_shift(int oversample)
2313 {
2314  if (!oversample)
2315  return 0.0f;
2316 
2317  // The prefilter is a box filter of width "oversample",
2318  // which shifts phase by (oversample - 1)/2 pixels in
2319  // oversampled space. We want to shift in the opposite
2320  // direction to counter this.
2321  return (float)-(oversample - 1) / (2.0f * (float)oversample);
2322 }
2323 
2324 // rects array must be big enough to accommodate all characters in the given ranges
2325 STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
2326 {
2327  int i,j,k;
2328 
2329  k=0;
2330  for (i=0; i < num_ranges; ++i) {
2331  float fh = ranges[i].font_size;
2332  float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
2333  for (j=0; j < ranges[i].num_chars_in_range; ++j) {
2334  int x0,y0,x1,y1;
2335  int glyph = stbtt_FindGlyphIndex(info,ranges[i].first_unicode_char_in_range + j);
2336  if (glyph) {
2338  scale * spc->h_oversample,
2339  scale * spc->v_oversample,
2340  0,0,
2341  &x0,&y0,&x1,&y1);
2342  rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);
2343  rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);
2344  } else {
2345  rects[k].w = rects[k].h = 1;
2346  }
2347  ++k;
2348  }
2349  }
2350 
2351  return k;
2352 }
2353 
2354 // rects array must be big enough to accommodate all characters in the given ranges
2355 STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
2356 {
2357  float recip_h = 1.0f / spc->h_oversample;
2358  float recip_v = 1.0f / spc->v_oversample;
2359  float sub_x = stbtt__oversample_shift(spc->h_oversample);
2360  float sub_y = stbtt__oversample_shift(spc->v_oversample);
2361  int i,j,k, return_value = 1;
2362 
2363  k = 0;
2364  for (i=0; i < num_ranges; ++i) {
2365  float fh = ranges[i].font_size;
2366  float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
2367  for (j=0; j < ranges[i].num_chars_in_range; ++j) {
2368  stbrp_rect *r = &rects[k];
2369  if (r->was_packed) {
2370  stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];
2371  int advance, lsb, x0,y0,x1,y1;
2372  int glyph = stbtt_FindGlyphIndex(info, ranges[i].first_unicode_char_in_range + j);
2373  stbrp_coord pad = (stbrp_coord) spc->padding;
2374 
2375  // pad on left and top
2376  r->x += pad;
2377  r->y += pad;
2378  r->w -= pad;
2379  r->h -= pad;
2380  stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
2381  stbtt_GetGlyphBitmapBox(info, glyph,
2382  scale * spc->h_oversample,
2383  scale * spc->v_oversample,
2384  &x0,&y0,&x1,&y1);
2386  spc->pixels + r->x + r->y*spc->stride_in_bytes,
2387  r->w - spc->h_oversample+1,
2388  r->h - spc->v_oversample+1,
2389  spc->stride_in_bytes,
2390  scale * spc->h_oversample,
2391  scale * spc->v_oversample,
2392  0,0,
2393  glyph);
2394 
2395  if (spc->h_oversample > 1)
2396  stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
2397  r->w, r->h, spc->stride_in_bytes,
2398  spc->h_oversample);
2399 
2400  if (spc->v_oversample > 1)
2401  stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
2402  r->w, r->h, spc->stride_in_bytes,
2403  spc->v_oversample);
2404 
2405  bc->x0 = (stbtt_int16) r->x;
2406  bc->y0 = (stbtt_int16) r->y;
2407  bc->x1 = (stbtt_int16) (r->x + r->w);
2408  bc->y1 = (stbtt_int16) (r->y + r->h);
2409  bc->xadvance = scale * advance;
2410  bc->xoff = (float) x0 * recip_h + sub_x;
2411  bc->yoff = (float) y0 * recip_v + sub_y;
2412  bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
2413  bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
2414  } else {
2415  return_value = 0; // if any fail, report failure
2416  }
2417 
2418  ++k;
2419  }
2420  }
2421 
2422  return return_value;
2423 }
2424 
2425 STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
2426 {
2427  stbtt_fontinfo info;
2428  int i,j,n, return_value = 1;
2429  stbrp_context *context = (stbrp_context *) spc->pack_info;
2430  stbrp_rect *rects;
2431 
2432  // flag all characters as NOT packed
2433  for (i=0; i < num_ranges; ++i)
2434  for (j=0; j < ranges[i].num_chars_in_range; ++j)
2435  ranges[i].chardata_for_range[j].x0 =
2436  ranges[i].chardata_for_range[j].y0 =
2437  ranges[i].chardata_for_range[j].x1 =
2438  ranges[i].chardata_for_range[j].y1 = 0;
2439 
2440  n = 0;
2441  for (i=0; i < num_ranges; ++i)
2442  n += ranges[i].num_chars_in_range;
2443 
2444  rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);
2445  if (rects == NULL)
2446  return 0;
2447 
2448  stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));
2449 
2450  n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
2451 
2452  stbrp_pack_rects(context, rects, n);
2453 
2454  return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
2455 
2456  return return_value;
2457 }
2458 
2459 STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,
2460  int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)
2461 {
2462  stbtt_pack_range range;
2463  range.first_unicode_char_in_range = first_unicode_char_in_range;
2464  range.num_chars_in_range = num_chars_in_range;
2465  range.chardata_for_range = chardata_for_range;
2466  range.font_size = font_size;
2467  return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
2468 }
2469 
2470 STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)
2471 {
2472  float ipw = 1.0f / pw, iph = 1.0f / ph;
2473  stbtt_packedchar *b = chardata + char_index;
2474 
2475  if (align_to_integer) {
2476  float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5);
2477  float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5);
2478  q->x0 = x;
2479  q->y0 = y;
2480  q->x1 = x + b->xoff2 - b->xoff;
2481  q->y1 = y + b->yoff2 - b->yoff;
2482  } else {
2483  q->x0 = *xpos + b->xoff;
2484  q->y0 = *ypos + b->yoff;
2485  q->x1 = *xpos + b->xoff2;
2486  q->y1 = *ypos + b->yoff2;
2487  }
2488 
2489  q->s0 = b->x0 * ipw;
2490  q->t0 = b->y0 * iph;
2491  q->s1 = b->x1 * ipw;
2492  q->t1 = b->y1 * iph;
2493 
2494  *xpos += b->xadvance;
2495 }
2496 
2497 
2499 //
2500 // font name matching -- recommended not to use this
2501 //
2502 
2503 // check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string
2504 static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(const stbtt_uint8 *s1, stbtt_int32 len1, const stbtt_uint8 *s2, stbtt_int32 len2)
2505 {
2506  stbtt_int32 i=0;
2507 
2508  // convert utf16 to utf8 and compare the results while converting
2509  while (len2) {
2510  stbtt_uint16 ch = s2[0]*256 + s2[1];
2511  if (ch < 0x80) {
2512  if (i >= len1) return -1;
2513  if (s1[i++] != ch) return -1;
2514  } else if (ch < 0x800) {
2515  if (i+1 >= len1) return -1;
2516  if (s1[i++] != 0xc0 + (ch >> 6)) return -1;
2517  if (s1[i++] != 0x80 + (ch & 0x3f)) return -1;
2518  } else if (ch >= 0xd800 && ch < 0xdc00) {
2519  stbtt_uint32 c;
2520  stbtt_uint16 ch2 = s2[2]*256 + s2[3];
2521  if (i+3 >= len1) return -1;
2522  c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
2523  if (s1[i++] != 0xf0 + (c >> 18)) return -1;
2524  if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1;
2525  if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1;
2526  if (s1[i++] != 0x80 + ((c ) & 0x3f)) return -1;
2527  s2 += 2; // plus another 2 below
2528  len2 -= 2;
2529  } else if (ch >= 0xdc00 && ch < 0xe000) {
2530  return -1;
2531  } else {
2532  if (i+2 >= len1) return -1;
2533  if (s1[i++] != 0xe0 + (ch >> 12)) return -1;
2534  if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1;
2535  if (s1[i++] != 0x80 + ((ch ) & 0x3f)) return -1;
2536  }
2537  s2 += 2;
2538  len2 -= 2;
2539  }
2540  return i;
2541 }
2542 
2543 STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2)
2544 {
2545  return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((const stbtt_uint8*) s1, len1, (const stbtt_uint8*) s2, len2);
2546 }
2547 
2548 // returns results in whatever encoding you request... but note that 2-byte encodings
2549 // will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare
2550 STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)
2551 {
2552  stbtt_int32 i,count,stringOffset;
2553  stbtt_uint8 *fc = font->data;
2554  stbtt_uint32 offset = font->fontstart;
2555  stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
2556  if (!nm) return NULL;
2557 
2558  count = ttUSHORT(fc+nm+2);
2559  stringOffset = nm + ttUSHORT(fc+nm+4);
2560  for (i=0; i < count; ++i) {
2561  stbtt_uint32 loc = nm + 6 + 12 * i;
2562  if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
2563  && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
2564  *length = ttUSHORT(fc+loc+8);
2565  return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
2566  }
2567  }
2568  return NULL;
2569 }
2570 
2571 static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
2572 {
2573  stbtt_int32 i;
2574  stbtt_int32 count = ttUSHORT(fc+nm+2);
2575  stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
2576 
2577  for (i=0; i < count; ++i) {
2578  stbtt_uint32 loc = nm + 6 + 12 * i;
2579  stbtt_int32 id = ttUSHORT(fc+loc+6);
2580  if (id == target_id) {
2581  // find the encoding
2582  stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
2583 
2584  // is this a Unicode encoding?
2585  if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
2586  stbtt_int32 slen = ttUSHORT(fc+loc+8);
2587  stbtt_int32 off = ttUSHORT(fc+loc+10);
2588 
2589  // check if there's a prefix match
2590  stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
2591  if (matchlen >= 0) {
2592  // check for target_id+1 immediately following, with same encoding & language
2593  if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) {
2594  slen = ttUSHORT(fc+loc+12+8);
2595  off = ttUSHORT(fc+loc+12+10);
2596  if (slen == 0) {
2597  if (matchlen == nlen)
2598  return 1;
2599  } else if (matchlen < nlen && name[matchlen] == ' ') {
2600  ++matchlen;
2601  if (stbtt_CompareUTF8toUTF16_bigendian((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen))
2602  return 1;
2603  }
2604  } else {
2605  // if nothing immediately following
2606  if (matchlen == nlen)
2607  return 1;
2608  }
2609  }
2610  }
2611 
2612  // @TODO handle other encodings
2613  }
2614  }
2615  return 0;
2616 }
2617 
2618 static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
2619 {
2620  stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name);
2621  stbtt_uint32 nm,hd;
2622  if (!stbtt__isfont(fc+offset)) return 0;
2623 
2624  // check italics/bold/underline flags in macStyle...
2625  if (flags) {
2626  hd = stbtt__find_table(fc, offset, "head");
2627  if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0;
2628  }
2629 
2630  nm = stbtt__find_table(fc, offset, "name");
2631  if (!nm) return 0;
2632 
2633  if (flags) {
2634  // if we checked the macStyle flags, then just check the family and ignore the subfamily
2635  if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1;
2636  if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1;
2637  if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
2638  } else {
2639  if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1;
2640  if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1;
2641  if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
2642  }
2643 
2644  return 0;
2645 }
2646 
2647 STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *font_collection, const char *name_utf8, stbtt_int32 flags)
2648 {
2649  stbtt_int32 i;
2650  for (i=0;;++i) {
2651  stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
2652  if (off < 0) return off;
2653  if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
2654  return off;
2655  }
2656 }
2657 
2658 #endif // STB_TRUETYPE_IMPLEMENTATION
STBTT_DEF unsigned char * stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff)
unsigned int h_oversample
Definition: stb_truetype.h:588
STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
STBTT_DEF unsigned char * stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff)
STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
unsigned short y0
Definition: stb_truetype.h:499
STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)
STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)
unsigned char type
Definition: stb_truetype.h:708
STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size, int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)
STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)
STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph)
STBRP_DEF void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)
unsigned short y0
Definition: stb_truetype.h:453
unsigned short x0
Definition: stb_truetype.h:453
unsigned short x0
Definition: stb_truetype.h:499
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
#define STBTT_malloc(x, u)
Definition: imgui.cpp:386
STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)
STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)
STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2)
stbtt_vertex_type cy
Definition: stb_truetype.h:707
stbtt_packedchar * chardata_for_range
Definition: stb_truetype.h:546
STBTT_DEF const char * stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)
STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint)
STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices)
STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context)
STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, float pixel_height, unsigned char *pixels, int pw, int ph, int first_char, int num_chars, stbtt_bakedchar *chardata)
#define stbtt_vertex_type
Definition: stb_truetype.h:704
STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels)
unsigned char * pixels
Definition: stb_truetype.h:589
stbtt_vertex_type x
Definition: stb_truetype.h:707
static void error(char *msg)
Definition: commander.c:19
stbtt_vertex_type y
Definition: stb_truetype.h:707
STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)
STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)
#define STBTT_free(x, u)
Definition: imgui.cpp:387
STBTT_DEF unsigned char * stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff)
unsigned short x1
Definition: stb_truetype.h:453
#define STBTT_assert(x)
Definition: imgui.cpp:388
unsigned char * data
Definition: stb_truetype.h:613
STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph)
STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset)
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
stbrp_coord h
Definition: stb_rect_pack.h:87
STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)
STBTT_DEF void stbtt_PackEnd(stbtt_pack_context *spc)
stbrp_coord y
Definition: stb_rect_pack.h:90
STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
stbrp_coord x
Definition: stb_rect_pack.h:90
STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)
STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices)
STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index)
STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)
STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
void * user_allocator_context
Definition: stb_truetype.h:582
static const float scale
Definition: Sprite.cpp:15
unsigned char * pixels
Definition: stb_truetype.h:786
unsigned short y1
Definition: stb_truetype.h:453
STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)
unsigned short y1
Definition: stb_truetype.h:499
int first_unicode_char_in_range
Definition: stb_truetype.h:544
stbrp_coord w
Definition: stb_rect_pack.h:87
unsigned short stbrp_coord
Definition: stb_rect_pack.h:56
int main(int argc, char *argv[])
Definition: main.cpp:36
stbtt_vertex_type cx
Definition: stb_truetype.h:707
unsigned int v_oversample
Definition: stb_truetype.h:588
unsigned short x1
Definition: stb_truetype.h:499
STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)
STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags)
STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint)
STBTT_DEF unsigned char * stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff)