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