xref: /openbmc/u-boot/drivers/video/stb_truetype.h (revision ebca902a)
18340ef62SSimon Glass // stb_truetype.h - v1.08 - public domain
28340ef62SSimon Glass // authored from 2009-2015 by Sean Barrett / RAD Game Tools
38340ef62SSimon Glass //
48340ef62SSimon Glass //   This library processes TrueType files:
58340ef62SSimon Glass //        parse files
68340ef62SSimon Glass //        extract glyph metrics
78340ef62SSimon Glass //        extract glyph shapes
88340ef62SSimon Glass //        render glyphs to one-channel bitmaps with antialiasing (box filter)
98340ef62SSimon Glass //
108340ef62SSimon Glass //   Todo:
118340ef62SSimon Glass //        non-MS cmaps
128340ef62SSimon Glass //        crashproof on bad data
138340ef62SSimon Glass //        hinting? (no longer patented)
148340ef62SSimon Glass //        cleartype-style AA?
158340ef62SSimon Glass //        optimize: use simple memory allocator for intermediates
168340ef62SSimon Glass //        optimize: build edge-list directly from curves
178340ef62SSimon Glass //        optimize: rasterize directly from curves?
188340ef62SSimon Glass //
198340ef62SSimon Glass // ADDITIONAL CONTRIBUTORS
208340ef62SSimon Glass //
218340ef62SSimon Glass //   Mikko Mononen: compound shape support, more cmap formats
228340ef62SSimon Glass //   Tor Andersson: kerning, subpixel rendering
238340ef62SSimon Glass //
248340ef62SSimon Glass //   Bug/warning reports/fixes:
258340ef62SSimon Glass //       "Zer" on mollyrocket (with fix)
268340ef62SSimon Glass //       Cass Everitt
278340ef62SSimon Glass //       stoiko (Haemimont Games)
288340ef62SSimon Glass //       Brian Hook
298340ef62SSimon Glass //       Walter van Niftrik
308340ef62SSimon Glass //       David Gow
318340ef62SSimon Glass //       David Given
328340ef62SSimon Glass //       Ivan-Assen Ivanov
338340ef62SSimon Glass //       Anthony Pesch
348340ef62SSimon Glass //       Johan Duparc
358340ef62SSimon Glass //       Hou Qiming
368340ef62SSimon Glass //       Fabian "ryg" Giesen
378340ef62SSimon Glass //       Martins Mozeiko
388340ef62SSimon Glass //       Cap Petschulat
398340ef62SSimon Glass //       Omar Cornut
408340ef62SSimon Glass //       github:aloucks
418340ef62SSimon Glass //       Peter LaValle
428340ef62SSimon Glass //       Sergey Popov
438340ef62SSimon Glass //       Giumo X. Clanjor
448340ef62SSimon Glass //       Higor Euripedes
458340ef62SSimon Glass //
468340ef62SSimon Glass //   Misc other:
478340ef62SSimon Glass //       Ryan Gordon
488340ef62SSimon Glass //
498340ef62SSimon Glass // VERSION HISTORY
508340ef62SSimon Glass //
518340ef62SSimon Glass //   1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges
528340ef62SSimon Glass //   1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints;
538340ef62SSimon Glass //                     variant PackFontRanges to pack and render in separate phases;
548340ef62SSimon Glass //                     fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?);
558340ef62SSimon Glass //                     fixed an assert() bug in the new rasterizer
568340ef62SSimon Glass //                     replace assert() with STBTT_assert() in new rasterizer
578340ef62SSimon Glass //   1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine)
588340ef62SSimon Glass //                     also more precise AA rasterizer, except if shapes overlap
598340ef62SSimon Glass //                     remove need for STBTT_sort
608340ef62SSimon Glass //   1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC
618340ef62SSimon Glass //   1.04 (2015-04-15) typo in example
628340ef62SSimon Glass //   1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes
638340ef62SSimon Glass //
648340ef62SSimon Glass //   Full history can be found at the end of this file.
658340ef62SSimon Glass //
668340ef62SSimon Glass // LICENSE
678340ef62SSimon Glass //
688340ef62SSimon Glass //   This software is in the public domain. Where that dedication is not
698340ef62SSimon Glass //   recognized, you are granted a perpetual, irrevocable license to copy,
708340ef62SSimon Glass //   distribute, and modify this file as you see fit.
718340ef62SSimon Glass //
728340ef62SSimon Glass // USAGE
738340ef62SSimon Glass //
748340ef62SSimon Glass //   Include this file in whatever places neeed to refer to it. In ONE C/C++
758340ef62SSimon Glass //   file, write:
768340ef62SSimon Glass //      #define STB_TRUETYPE_IMPLEMENTATION
778340ef62SSimon Glass //   before the #include of this file. This expands out the actual
788340ef62SSimon Glass //   implementation into that C/C++ file.
798340ef62SSimon Glass //
808340ef62SSimon Glass //   To make the implementation private to the file that generates the implementation,
818340ef62SSimon Glass //      #define STBTT_STATIC
828340ef62SSimon Glass //
838340ef62SSimon Glass //   Simple 3D API (don't ship this, but it's fine for tools and quick start)
848340ef62SSimon Glass //           stbtt_BakeFontBitmap()               -- bake a font to a bitmap for use as texture
858340ef62SSimon Glass //           stbtt_GetBakedQuad()                 -- compute quad to draw for a given char
868340ef62SSimon Glass //
878340ef62SSimon Glass //   Improved 3D API (more shippable):
888340ef62SSimon Glass //           #include "stb_rect_pack.h"           -- optional, but you really want it
898340ef62SSimon Glass //           stbtt_PackBegin()
908340ef62SSimon Glass //           stbtt_PackSetOversample()            -- for improved quality on small fonts
918340ef62SSimon Glass //           stbtt_PackFontRanges()               -- pack and renders
928340ef62SSimon Glass //           stbtt_PackEnd()
938340ef62SSimon Glass //           stbtt_GetPackedQuad()
948340ef62SSimon Glass //
958340ef62SSimon Glass //   "Load" a font file from a memory buffer (you have to keep the buffer loaded)
968340ef62SSimon Glass //           stbtt_InitFont()
978340ef62SSimon Glass //           stbtt_GetFontOffsetForIndex()        -- use for TTC font collections
988340ef62SSimon Glass //
998340ef62SSimon Glass //   Render a unicode codepoint to a bitmap
1008340ef62SSimon Glass //           stbtt_GetCodepointBitmap()           -- allocates and returns a bitmap
1018340ef62SSimon Glass //           stbtt_MakeCodepointBitmap()          -- renders into bitmap you provide
1028340ef62SSimon Glass //           stbtt_GetCodepointBitmapBox()        -- how big the bitmap must be
1038340ef62SSimon Glass //
1048340ef62SSimon Glass //   Character advance/positioning
1058340ef62SSimon Glass //           stbtt_GetCodepointHMetrics()
1068340ef62SSimon Glass //           stbtt_GetFontVMetrics()
1078340ef62SSimon Glass //           stbtt_GetCodepointKernAdvance()
1088340ef62SSimon Glass //
1098340ef62SSimon Glass //   Starting with version 1.06, the rasterizer was replaced with a new,
1108340ef62SSimon Glass //   faster and generally-more-precise rasterizer. The new rasterizer more
1118340ef62SSimon Glass //   accurately measures pixel coverage for anti-aliasing, except in the case
1128340ef62SSimon Glass //   where multiple shapes overlap, in which case it overestimates the AA pixel
1138340ef62SSimon Glass //   coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If
1148340ef62SSimon Glass //   this turns out to be a problem, you can re-enable the old rasterizer with
1158340ef62SSimon Glass //        #define STBTT_RASTERIZER_VERSION 1
1168340ef62SSimon Glass //   which will incur about a 15% speed hit.
1178340ef62SSimon Glass //
1188340ef62SSimon Glass // ADDITIONAL DOCUMENTATION
1198340ef62SSimon Glass //
1208340ef62SSimon Glass //   Immediately after this block comment are a series of sample programs.
1218340ef62SSimon Glass //
1228340ef62SSimon Glass //   After the sample programs is the "header file" section. This section
1238340ef62SSimon Glass //   includes documentation for each API function.
1248340ef62SSimon Glass //
1258340ef62SSimon Glass //   Some important concepts to understand to use this library:
1268340ef62SSimon Glass //
1278340ef62SSimon Glass //      Codepoint
1288340ef62SSimon Glass //         Characters are defined by unicode codepoints, e.g. 65 is
1298340ef62SSimon Glass //         uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is
1308340ef62SSimon Glass //         the hiragana for "ma".
1318340ef62SSimon Glass //
1328340ef62SSimon Glass //      Glyph
1338340ef62SSimon Glass //         A visual character shape (every codepoint is rendered as
1348340ef62SSimon Glass //         some glyph)
1358340ef62SSimon Glass //
1368340ef62SSimon Glass //      Glyph index
1378340ef62SSimon Glass //         A font-specific integer ID representing a glyph
1388340ef62SSimon Glass //
1398340ef62SSimon Glass //      Baseline
1408340ef62SSimon Glass //         Glyph shapes are defined relative to a baseline, which is the
1418340ef62SSimon Glass //         bottom of uppercase characters. Characters extend both above
1428340ef62SSimon Glass //         and below the baseline.
1438340ef62SSimon Glass //
1448340ef62SSimon Glass //      Current Point
1458340ef62SSimon Glass //         As you draw text to the screen, you keep track of a "current point"
1468340ef62SSimon Glass //         which is the origin of each character. The current point's vertical
1478340ef62SSimon Glass //         position is the baseline. Even "baked fonts" use this model.
1488340ef62SSimon Glass //
1498340ef62SSimon Glass //      Vertical Font Metrics
1508340ef62SSimon Glass //         The vertical qualities of the font, used to vertically position
1518340ef62SSimon Glass //         and space the characters. See docs for stbtt_GetFontVMetrics.
1528340ef62SSimon Glass //
1538340ef62SSimon Glass //      Font Size in Pixels or Points
1548340ef62SSimon Glass //         The preferred interface for specifying font sizes in stb_truetype
1558340ef62SSimon Glass //         is to specify how tall the font's vertical extent should be in pixels.
1568340ef62SSimon Glass //         If that sounds good enough, skip the next paragraph.
1578340ef62SSimon Glass //
1588340ef62SSimon Glass //         Most font APIs instead use "points", which are a common typographic
1598340ef62SSimon Glass //         measurement for describing font size, defined as 72 points per inch.
1608340ef62SSimon Glass //         stb_truetype provides a point API for compatibility. However, true
1618340ef62SSimon Glass //         "per inch" conventions don't make much sense on computer displays
1628340ef62SSimon Glass //         since they different monitors have different number of pixels per
1638340ef62SSimon Glass //         inch. For example, Windows traditionally uses a convention that
1648340ef62SSimon Glass //         there are 96 pixels per inch, thus making 'inch' measurements have
1658340ef62SSimon Glass //         nothing to do with inches, and thus effectively defining a point to
1668340ef62SSimon Glass //         be 1.333 pixels. Additionally, the TrueType font data provides
1678340ef62SSimon Glass //         an explicit scale factor to scale a given font's glyphs to points,
1688340ef62SSimon Glass //         but the author has observed that this scale factor is often wrong
1698340ef62SSimon Glass //         for non-commercial fonts, thus making fonts scaled in points
1708340ef62SSimon Glass //         according to the TrueType spec incoherently sized in practice.
1718340ef62SSimon Glass //
1728340ef62SSimon Glass // ADVANCED USAGE
1738340ef62SSimon Glass //
1748340ef62SSimon Glass //   Quality:
1758340ef62SSimon Glass //
1768340ef62SSimon Glass //    - Use the functions with Subpixel at the end to allow your characters
1778340ef62SSimon Glass //      to have subpixel positioning. Since the font is anti-aliased, not
1788340ef62SSimon Glass //      hinted, this is very import for quality. (This is not possible with
1798340ef62SSimon Glass //      baked fonts.)
1808340ef62SSimon Glass //
1818340ef62SSimon Glass //    - Kerning is now supported, and if you're supporting subpixel rendering
1828340ef62SSimon Glass //      then kerning is worth using to give your text a polished look.
1838340ef62SSimon Glass //
1848340ef62SSimon Glass //   Performance:
1858340ef62SSimon Glass //
1868340ef62SSimon Glass //    - Convert Unicode codepoints to glyph indexes and operate on the glyphs;
1878340ef62SSimon Glass //      if you don't do this, stb_truetype is forced to do the conversion on
1888340ef62SSimon Glass //      every call.
1898340ef62SSimon Glass //
1908340ef62SSimon Glass //    - There are a lot of memory allocations. We should modify it to take
1918340ef62SSimon Glass //      a temp buffer and allocate from the temp buffer (without freeing),
1928340ef62SSimon Glass //      should help performance a lot.
1938340ef62SSimon Glass //
1948340ef62SSimon Glass // NOTES
1958340ef62SSimon Glass //
1968340ef62SSimon Glass //   The system uses the raw data found in the .ttf file without changing it
1978340ef62SSimon Glass //   and without building auxiliary data structures. This is a bit inefficient
1988340ef62SSimon Glass //   on little-endian systems (the data is big-endian), but assuming you're
1998340ef62SSimon Glass //   caching the bitmaps or glyph shapes this shouldn't be a big deal.
2008340ef62SSimon Glass //
2018340ef62SSimon Glass //   It appears to be very hard to programmatically determine what font a
2028340ef62SSimon Glass //   given file is in a general way. I provide an API for this, but I don't
2038340ef62SSimon Glass //   recommend it.
2048340ef62SSimon Glass //
2058340ef62SSimon Glass //
2068340ef62SSimon Glass // SOURCE STATISTICS (based on v0.6c, 2050 LOC)
2078340ef62SSimon Glass //
2088340ef62SSimon Glass //   Documentation & header file        520 LOC  \___ 660 LOC documentation
2098340ef62SSimon Glass //   Sample code                        140 LOC  /
2108340ef62SSimon Glass //   Truetype parsing                   620 LOC  ---- 620 LOC TrueType
2118340ef62SSimon Glass //   Software rasterization             240 LOC  \                           .
2128340ef62SSimon Glass //   Curve tesselation                  120 LOC   \__ 550 LOC Bitmap creation
2138340ef62SSimon Glass //   Bitmap management                  100 LOC   /
2148340ef62SSimon Glass //   Baked bitmap interface              70 LOC  /
2158340ef62SSimon Glass //   Font name matching & access        150 LOC  ---- 150
2168340ef62SSimon Glass //   C runtime library abstraction       60 LOC  ----  60
2178340ef62SSimon Glass //
2188340ef62SSimon Glass //
2198340ef62SSimon Glass // PERFORMANCE MEASUREMENTS FOR 1.06:
2208340ef62SSimon Glass //
2218340ef62SSimon Glass //                      32-bit     64-bit
2228340ef62SSimon Glass //   Previous release:  8.83 s     7.68 s
2238340ef62SSimon Glass //   Pool allocations:  7.72 s     6.34 s
2248340ef62SSimon Glass //   Inline sort     :  6.54 s     5.65 s
2258340ef62SSimon Glass //   New rasterizer  :  5.63 s     5.00 s
2268340ef62SSimon Glass 
2278340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
2288340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
2298340ef62SSimon Glass ////
2308340ef62SSimon Glass ////  SAMPLE PROGRAMS
2318340ef62SSimon Glass ////
2328340ef62SSimon Glass //
2338340ef62SSimon Glass //  Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless
2348340ef62SSimon Glass //
2358340ef62SSimon Glass #if 0
2368340ef62SSimon Glass #define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation
2378340ef62SSimon Glass #include "stb_truetype.h"
2388340ef62SSimon Glass 
2398340ef62SSimon Glass unsigned char ttf_buffer[1<<20];
2408340ef62SSimon Glass unsigned char temp_bitmap[512*512];
2418340ef62SSimon Glass 
2428340ef62SSimon Glass stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs
2438340ef62SSimon Glass GLuint ftex;
2448340ef62SSimon Glass 
2458340ef62SSimon Glass void my_stbtt_initfont(void)
2468340ef62SSimon Glass {
2478340ef62SSimon Glass    fread(ttf_buffer, 1, 1<<20, fopen("c:/windows/fonts/times.ttf", "rb"));
2488340ef62SSimon Glass    stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); // no guarantee this fits!
2498340ef62SSimon Glass    // can free ttf_buffer at this point
2508340ef62SSimon Glass    glGenTextures(1, &ftex);
2518340ef62SSimon Glass    glBindTexture(GL_TEXTURE_2D, ftex);
2528340ef62SSimon Glass    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
2538340ef62SSimon Glass    // can free temp_bitmap at this point
2548340ef62SSimon Glass    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2558340ef62SSimon Glass }
2568340ef62SSimon Glass 
2578340ef62SSimon Glass void my_stbtt_print(float x, float y, char *text)
2588340ef62SSimon Glass {
2598340ef62SSimon Glass    // assume orthographic projection with units = screen pixels, origin at top left
2608340ef62SSimon Glass    glEnable(GL_TEXTURE_2D);
2618340ef62SSimon Glass    glBindTexture(GL_TEXTURE_2D, ftex);
2628340ef62SSimon Glass    glBegin(GL_QUADS);
2638340ef62SSimon Glass    while (*text) {
2648340ef62SSimon Glass       if (*text >= 32 && *text < 128) {
2658340ef62SSimon Glass          stbtt_aligned_quad q;
2668340ef62SSimon Glass          stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9
2678340ef62SSimon Glass          glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
2688340ef62SSimon Glass          glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
2698340ef62SSimon Glass          glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
2708340ef62SSimon Glass          glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);
2718340ef62SSimon Glass       }
2728340ef62SSimon Glass       ++text;
2738340ef62SSimon Glass    }
2748340ef62SSimon Glass    glEnd();
2758340ef62SSimon Glass }
2768340ef62SSimon Glass #endif
2778340ef62SSimon Glass //
2788340ef62SSimon Glass //
2798340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
2808340ef62SSimon Glass //
2818340ef62SSimon Glass // Complete program (this compiles): get a single bitmap, print as ASCII art
2828340ef62SSimon Glass //
2838340ef62SSimon Glass #if 0
2848340ef62SSimon Glass #include <stdio.h>
2858340ef62SSimon Glass #define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation
2868340ef62SSimon Glass #include "stb_truetype.h"
2878340ef62SSimon Glass 
2888340ef62SSimon Glass char ttf_buffer[1<<25];
2898340ef62SSimon Glass 
2908340ef62SSimon Glass int main(int argc, char **argv)
2918340ef62SSimon Glass {
2928340ef62SSimon Glass    stbtt_fontinfo font;
2938340ef62SSimon Glass    unsigned char *bitmap;
2948340ef62SSimon Glass    int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
2958340ef62SSimon Glass 
2968340ef62SSimon Glass    fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb"));
2978340ef62SSimon Glass 
2988340ef62SSimon Glass    stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
2998340ef62SSimon Glass    bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
3008340ef62SSimon Glass 
3018340ef62SSimon Glass    for (j=0; j < h; ++j) {
3028340ef62SSimon Glass       for (i=0; i < w; ++i)
3038340ef62SSimon Glass          putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);
3048340ef62SSimon Glass       putchar('\n');
3058340ef62SSimon Glass    }
3068340ef62SSimon Glass    return 0;
3078340ef62SSimon Glass }
3088340ef62SSimon Glass #endif
3098340ef62SSimon Glass //
3108340ef62SSimon Glass // Output:
3118340ef62SSimon Glass //
3128340ef62SSimon Glass //     .ii.
3138340ef62SSimon Glass //    @@@@@@.
3148340ef62SSimon Glass //   V@Mio@@o
3158340ef62SSimon Glass //   :i.  V@V
3168340ef62SSimon Glass //     :oM@@M
3178340ef62SSimon Glass //   :@@@MM@M
3188340ef62SSimon Glass //   @@o  o@M
3198340ef62SSimon Glass //  :@@.  M@M
3208340ef62SSimon Glass //   @@@o@@@@
3218340ef62SSimon Glass //   :M@@V:@@.
3228340ef62SSimon Glass //
3238340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
3248340ef62SSimon Glass //
3258340ef62SSimon Glass // Complete program: print "Hello World!" banner, with bugs
3268340ef62SSimon Glass //
3278340ef62SSimon Glass #if 0
3288340ef62SSimon Glass char buffer[24<<20];
3298340ef62SSimon Glass unsigned char screen[20][79];
3308340ef62SSimon Glass 
3318340ef62SSimon Glass int main(int arg, char **argv)
3328340ef62SSimon Glass {
3338340ef62SSimon Glass    stbtt_fontinfo font;
3348340ef62SSimon Glass    int i,j,ascent,baseline,ch=0;
3358340ef62SSimon Glass    float scale, xpos=2; // leave a little padding in case the character extends left
3368340ef62SSimon Glass    char *text = "Heljo World!"; // intentionally misspelled to show 'lj' brokenness
3378340ef62SSimon Glass 
3388340ef62SSimon Glass    fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb"));
3398340ef62SSimon Glass    stbtt_InitFont(&font, buffer, 0);
3408340ef62SSimon Glass 
3418340ef62SSimon Glass    scale = stbtt_ScaleForPixelHeight(&font, 15);
3428340ef62SSimon Glass    stbtt_GetFontVMetrics(&font, &ascent,0,0);
3438340ef62SSimon Glass    baseline = (int) (ascent*scale);
3448340ef62SSimon Glass 
3458340ef62SSimon Glass    while (text[ch]) {
3468340ef62SSimon Glass       int advance,lsb,x0,y0,x1,y1;
3478340ef62SSimon Glass       float x_shift = xpos - (float) floor(xpos);
3488340ef62SSimon Glass       stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
3498340ef62SSimon Glass       stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);
3508340ef62SSimon Glass       stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);
3518340ef62SSimon Glass       // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong
3528340ef62SSimon Glass       // because this API is really for baking character bitmaps into textures. if you want to render
3538340ef62SSimon Glass       // a sequence of characters, you really need to render each bitmap to a temp buffer, then
3548340ef62SSimon Glass       // "alpha blend" that into the working buffer
3558340ef62SSimon Glass       xpos += (advance * scale);
3568340ef62SSimon Glass       if (text[ch+1])
3578340ef62SSimon Glass          xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);
3588340ef62SSimon Glass       ++ch;
3598340ef62SSimon Glass    }
3608340ef62SSimon Glass 
3618340ef62SSimon Glass    for (j=0; j < 20; ++j) {
3628340ef62SSimon Glass       for (i=0; i < 78; ++i)
3638340ef62SSimon Glass          putchar(" .:ioVM@"[screen[j][i]>>5]);
3648340ef62SSimon Glass       putchar('\n');
3658340ef62SSimon Glass    }
3668340ef62SSimon Glass 
3678340ef62SSimon Glass    return 0;
3688340ef62SSimon Glass }
3698340ef62SSimon Glass #endif
3708340ef62SSimon Glass 
3718340ef62SSimon Glass 
3728340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
3738340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
3748340ef62SSimon Glass ////
3758340ef62SSimon Glass ////   INTEGRATION WITH YOUR CODEBASE
3768340ef62SSimon Glass ////
3778340ef62SSimon Glass ////   The following sections allow you to supply alternate definitions
3788340ef62SSimon Glass ////   of C library functions used by stb_truetype.
3798340ef62SSimon Glass 
3808340ef62SSimon Glass #ifdef STB_TRUETYPE_IMPLEMENTATION
3818340ef62SSimon Glass    // #define your own (u)stbtt_int8/16/32 before including to override this
3828340ef62SSimon Glass    #ifndef stbtt_uint8
3838340ef62SSimon Glass    typedef unsigned char   stbtt_uint8;
3848340ef62SSimon Glass    typedef signed   char   stbtt_int8;
3858340ef62SSimon Glass    typedef unsigned short  stbtt_uint16;
3868340ef62SSimon Glass    typedef signed   short  stbtt_int16;
3878340ef62SSimon Glass    typedef unsigned int    stbtt_uint32;
3888340ef62SSimon Glass    typedef signed   int    stbtt_int32;
3898340ef62SSimon Glass    #endif
3908340ef62SSimon Glass 
3918340ef62SSimon Glass    typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1];
3928340ef62SSimon Glass    typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1];
3938340ef62SSimon Glass 
3948340ef62SSimon Glass    // #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h
3958340ef62SSimon Glass    #ifndef STBTT_ifloor
3968340ef62SSimon Glass    #include <math.h>
3978340ef62SSimon Glass    #define STBTT_ifloor(x)   ((int) floor(x))
3988340ef62SSimon Glass    #define STBTT_iceil(x)    ((int) ceil(x))
3998340ef62SSimon Glass    #endif
4008340ef62SSimon Glass 
4018340ef62SSimon Glass    #ifndef STBTT_sqrt
4028340ef62SSimon Glass    #include <math.h>
4038340ef62SSimon Glass    #define STBTT_sqrt(x)      sqrt(x)
4048340ef62SSimon Glass    #endif
4058340ef62SSimon Glass 
4068340ef62SSimon Glass    #ifndef STBTT_fabs
4078340ef62SSimon Glass    #include <math.h>
4088340ef62SSimon Glass    #define STBTT_fabs(x)      fabs(x)
4098340ef62SSimon Glass    #endif
4108340ef62SSimon Glass 
4118340ef62SSimon Glass    // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h
4128340ef62SSimon Glass    #ifndef STBTT_malloc
4138340ef62SSimon Glass    #include <stdlib.h>
4148340ef62SSimon Glass    #define STBTT_malloc(x,u)  ((void)(u),malloc(x))
4158340ef62SSimon Glass    #define STBTT_free(x,u)    ((void)(u),free(x))
4168340ef62SSimon Glass    #endif
4178340ef62SSimon Glass 
4188340ef62SSimon Glass    #ifndef STBTT_assert
4198340ef62SSimon Glass    #include <assert.h>
4208340ef62SSimon Glass    #define STBTT_assert(x)    assert(x)
4218340ef62SSimon Glass    #endif
4228340ef62SSimon Glass 
4238340ef62SSimon Glass    #ifndef STBTT_strlen
4248340ef62SSimon Glass    #include <string.h>
4258340ef62SSimon Glass    #define STBTT_strlen(x)    strlen(x)
4268340ef62SSimon Glass    #endif
4278340ef62SSimon Glass 
4288340ef62SSimon Glass    #ifndef STBTT_memcpy
4298340ef62SSimon Glass    #include <memory.h>
4308340ef62SSimon Glass    #define STBTT_memcpy       memcpy
4318340ef62SSimon Glass    #define STBTT_memset       memset
4328340ef62SSimon Glass    #endif
4338340ef62SSimon Glass #endif
4348340ef62SSimon Glass 
4358340ef62SSimon Glass ///////////////////////////////////////////////////////////////////////////////
4368340ef62SSimon Glass ///////////////////////////////////////////////////////////////////////////////
4378340ef62SSimon Glass ////
4388340ef62SSimon Glass ////   INTERFACE
4398340ef62SSimon Glass ////
4408340ef62SSimon Glass ////
4418340ef62SSimon Glass 
4428340ef62SSimon Glass #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
4438340ef62SSimon Glass #define __STB_INCLUDE_STB_TRUETYPE_H__
4448340ef62SSimon Glass 
4458340ef62SSimon Glass #ifdef STBTT_STATIC
4468340ef62SSimon Glass #define STBTT_DEF static
4478340ef62SSimon Glass #else
4488340ef62SSimon Glass #define STBTT_DEF extern
4498340ef62SSimon Glass #endif
4508340ef62SSimon Glass 
4518340ef62SSimon Glass #ifdef __cplusplus
4528340ef62SSimon Glass extern "C" {
4538340ef62SSimon Glass #endif
4548340ef62SSimon Glass 
4558340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
4568340ef62SSimon Glass //
4578340ef62SSimon Glass // TEXTURE BAKING API
4588340ef62SSimon Glass //
4598340ef62SSimon Glass // If you use this API, you only have to call two functions ever.
4608340ef62SSimon Glass //
4618340ef62SSimon Glass 
4628340ef62SSimon Glass typedef struct
4638340ef62SSimon Glass {
4648340ef62SSimon Glass    unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap
4658340ef62SSimon Glass    float xoff,yoff,xadvance;
4668340ef62SSimon Glass } stbtt_bakedchar;
4678340ef62SSimon Glass 
4688340ef62SSimon Glass STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset,  // font location (use offset=0 for plain .ttf)
4698340ef62SSimon Glass                                 float pixel_height,                     // height of font in pixels
4708340ef62SSimon Glass                                 unsigned char *pixels, int pw, int ph,  // bitmap to be filled in
4718340ef62SSimon Glass                                 int first_char, int num_chars,          // characters to bake
4728340ef62SSimon Glass                                 stbtt_bakedchar *chardata);             // you allocate this, it's num_chars long
4738340ef62SSimon Glass // if return is positive, the first unused row of the bitmap
4748340ef62SSimon Glass // if return is negative, returns the negative of the number of characters that fit
4758340ef62SSimon Glass // if return is 0, no characters fit and no rows were used
4768340ef62SSimon Glass // This uses a very crappy packing.
4778340ef62SSimon Glass 
4788340ef62SSimon Glass typedef struct
4798340ef62SSimon Glass {
4808340ef62SSimon Glass    float x0,y0,s0,t0; // top-left
4818340ef62SSimon Glass    float x1,y1,s1,t1; // bottom-right
4828340ef62SSimon Glass } stbtt_aligned_quad;
4838340ef62SSimon Glass 
4848340ef62SSimon Glass STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph,  // same data as above
4858340ef62SSimon Glass                                int char_index,             // character to display
4868340ef62SSimon Glass                                float *xpos, float *ypos,   // pointers to current position in screen pixel space
4878340ef62SSimon Glass                                stbtt_aligned_quad *q,      // output: quad to draw
4888340ef62SSimon Glass                                int opengl_fillrule);       // true if opengl fill rule; false if DX9 or earlier
4898340ef62SSimon Glass // Call GetBakedQuad with char_index = 'character - first_char', and it
4908340ef62SSimon Glass // creates the quad you need to draw and advances the current position.
4918340ef62SSimon Glass //
4928340ef62SSimon Glass // The coordinate system used assumes y increases downwards.
4938340ef62SSimon Glass //
4948340ef62SSimon Glass // Characters will extend both above and below the current position;
4958340ef62SSimon Glass // see discussion of "BASELINE" above.
4968340ef62SSimon Glass //
4978340ef62SSimon Glass // It's inefficient; you might want to c&p it and optimize it.
4988340ef62SSimon Glass 
4998340ef62SSimon Glass 
5008340ef62SSimon Glass 
5018340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
5028340ef62SSimon Glass //
5038340ef62SSimon Glass // NEW TEXTURE BAKING API
5048340ef62SSimon Glass //
5058340ef62SSimon Glass // This provides options for packing multiple fonts into one atlas, not
5068340ef62SSimon Glass // perfectly but better than nothing.
5078340ef62SSimon Glass 
5088340ef62SSimon Glass typedef struct
5098340ef62SSimon Glass {
5108340ef62SSimon Glass    unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap
5118340ef62SSimon Glass    float xoff,yoff,xadvance;
5128340ef62SSimon Glass    float xoff2,yoff2;
5138340ef62SSimon Glass } stbtt_packedchar;
5148340ef62SSimon Glass 
5158340ef62SSimon Glass typedef struct stbtt_pack_context stbtt_pack_context;
5168340ef62SSimon Glass typedef struct stbtt_fontinfo stbtt_fontinfo;
5178340ef62SSimon Glass #ifndef STB_RECT_PACK_VERSION
5188340ef62SSimon Glass typedef struct stbrp_rect stbrp_rect;
5198340ef62SSimon Glass #endif
5208340ef62SSimon Glass 
5218340ef62SSimon Glass STBTT_DEF int  stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context);
5228340ef62SSimon Glass // Initializes a packing context stored in the passed-in stbtt_pack_context.
5238340ef62SSimon Glass // Future calls using this context will pack characters into the bitmap passed
5248340ef62SSimon Glass // in here: a 1-channel bitmap that is weight x height. stride_in_bytes is
5258340ef62SSimon Glass // the distance from one row to the next (or 0 to mean they are packed tightly
5268340ef62SSimon Glass // together). "padding" is the amount of padding to leave between each
5278340ef62SSimon Glass // character (normally you want '1' for bitmaps you'll use as textures with
5288340ef62SSimon Glass // bilinear filtering).
5298340ef62SSimon Glass //
5308340ef62SSimon Glass // Returns 0 on failure, 1 on success.
5318340ef62SSimon Glass 
5328340ef62SSimon Glass STBTT_DEF void stbtt_PackEnd  (stbtt_pack_context *spc);
5338340ef62SSimon Glass // Cleans up the packing context and frees all memory.
5348340ef62SSimon Glass 
5358340ef62SSimon Glass #define STBTT_POINT_SIZE(x)   (-(x))
5368340ef62SSimon Glass 
5378340ef62SSimon Glass STBTT_DEF int  stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,
5388340ef62SSimon Glass                                 int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);
5398340ef62SSimon Glass // Creates character bitmaps from the font_index'th font found in fontdata (use
5408340ef62SSimon Glass // font_index=0 if you don't know what that is). It creates num_chars_in_range
5418340ef62SSimon Glass // bitmaps for characters with unicode values starting at first_unicode_char_in_range
5428340ef62SSimon Glass // and increasing. Data for how to render them is stored in chardata_for_range;
5438340ef62SSimon Glass // pass these to stbtt_GetPackedQuad to get back renderable quads.
5448340ef62SSimon Glass //
5458340ef62SSimon Glass // font_size is the full height of the character from ascender to descender,
5468340ef62SSimon Glass // as computed by stbtt_ScaleForPixelHeight. To use a point size as computed
5478340ef62SSimon Glass // by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE()
5488340ef62SSimon Glass // and pass that result as 'font_size':
5498340ef62SSimon Glass //       ...,                  20 , ... // font max minus min y is 20 pixels tall
5508340ef62SSimon Glass //       ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall
5518340ef62SSimon Glass 
5528340ef62SSimon Glass typedef struct
5538340ef62SSimon Glass {
5548340ef62SSimon Glass    float font_size;
5558340ef62SSimon Glass    int first_unicode_codepoint_in_range;  // if non-zero, then the chars are continuous, and this is the first codepoint
5568340ef62SSimon Glass    int *array_of_unicode_codepoints;       // if non-zero, then this is an array of unicode codepoints
5578340ef62SSimon Glass    int num_chars;
5588340ef62SSimon Glass    stbtt_packedchar *chardata_for_range; // output
5598340ef62SSimon Glass    unsigned char h_oversample, v_oversample; // don't set these, they're used internally
5608340ef62SSimon Glass } stbtt_pack_range;
5618340ef62SSimon Glass 
5628340ef62SSimon Glass STBTT_DEF int  stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);
5638340ef62SSimon Glass // Creates character bitmaps from multiple ranges of characters stored in
5648340ef62SSimon Glass // ranges. This will usually create a better-packed bitmap than multiple
5658340ef62SSimon Glass // calls to stbtt_PackFontRange. Note that you can call this multiple
5668340ef62SSimon Glass // times within a single PackBegin/PackEnd.
5678340ef62SSimon Glass 
5688340ef62SSimon Glass STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);
5698340ef62SSimon Glass // Oversampling a font increases the quality by allowing higher-quality subpixel
5708340ef62SSimon Glass // positioning, and is especially valuable at smaller text sizes.
5718340ef62SSimon Glass //
5728340ef62SSimon Glass // This function sets the amount of oversampling for all following calls to
5738340ef62SSimon Glass // stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given
5748340ef62SSimon Glass // pack context. The default (no oversampling) is achieved by h_oversample=1
5758340ef62SSimon Glass // and v_oversample=1. The total number of pixels required is
5768340ef62SSimon Glass // h_oversample*v_oversample larger than the default; for example, 2x2
5778340ef62SSimon Glass // oversampling requires 4x the storage of 1x1. For best results, render
5788340ef62SSimon Glass // oversampled textures with bilinear filtering. Look at the readme in
5798340ef62SSimon Glass // stb/tests/oversample for information about oversampled fonts
5808340ef62SSimon Glass //
5818340ef62SSimon Glass // To use with PackFontRangesGather etc., you must set it before calls
5828340ef62SSimon Glass // call to PackFontRangesGatherRects.
5838340ef62SSimon Glass 
5848340ef62SSimon Glass STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph,  // same data as above
5858340ef62SSimon Glass                                int char_index,             // character to display
5868340ef62SSimon Glass                                float *xpos, float *ypos,   // pointers to current position in screen pixel space
5878340ef62SSimon Glass                                stbtt_aligned_quad *q,      // output: quad to draw
5888340ef62SSimon Glass                                int align_to_integer);
5898340ef62SSimon Glass 
5908340ef62SSimon Glass STBTT_DEF int  stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
5918340ef62SSimon Glass STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects);
5928340ef62SSimon Glass STBTT_DEF int  stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
5938340ef62SSimon Glass // Calling these functions in sequence is roughly equivalent to calling
5948340ef62SSimon Glass // stbtt_PackFontRanges(). If you more control over the packing of multiple
5958340ef62SSimon Glass // fonts, or if you want to pack custom data into a font texture, take a look
5968340ef62SSimon Glass // at the source to of stbtt_PackFontRanges() and create a custom version
5978340ef62SSimon Glass // using these functions, e.g. call GatherRects multiple times,
5988340ef62SSimon Glass // building up a single array of rects, then call PackRects once,
5998340ef62SSimon Glass // then call RenderIntoRects repeatedly. This may result in a
6008340ef62SSimon Glass // better packing than calling PackFontRanges multiple times
6018340ef62SSimon Glass // (or it may not).
6028340ef62SSimon Glass 
6038340ef62SSimon Glass // this is an opaque structure that you shouldn't mess with which holds
6048340ef62SSimon Glass // all the context needed from PackBegin to PackEnd.
6058340ef62SSimon Glass struct stbtt_pack_context {
6068340ef62SSimon Glass    void *user_allocator_context;
6078340ef62SSimon Glass    void *pack_info;
6088340ef62SSimon Glass    int   width;
6098340ef62SSimon Glass    int   height;
6108340ef62SSimon Glass    int   stride_in_bytes;
6118340ef62SSimon Glass    int   padding;
6128340ef62SSimon Glass    unsigned int   h_oversample, v_oversample;
6138340ef62SSimon Glass    unsigned char *pixels;
6148340ef62SSimon Glass    void  *nodes;
6158340ef62SSimon Glass };
6168340ef62SSimon Glass 
6178340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
6188340ef62SSimon Glass //
6198340ef62SSimon Glass // FONT LOADING
6208340ef62SSimon Glass //
6218340ef62SSimon Glass //
6228340ef62SSimon Glass 
6238340ef62SSimon Glass STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);
6248340ef62SSimon Glass // Each .ttf/.ttc file may have more than one font. Each font has a sequential
6258340ef62SSimon Glass // index number starting from 0. Call this function to get the font offset for
6268340ef62SSimon Glass // a given index; it returns -1 if the index is out of range. A regular .ttf
6278340ef62SSimon Glass // file will only define one font and it always be at offset 0, so it will
6288340ef62SSimon Glass // return '0' for index 0, and -1 for all other indices. You can just skip
6298340ef62SSimon Glass // this step if you know it's that kind of font.
6308340ef62SSimon Glass 
6318340ef62SSimon Glass 
6328340ef62SSimon Glass // The following structure is defined publically so you can declare one on
6338340ef62SSimon Glass // the stack or as a global or etc, but you should treat it as opaque.
6348340ef62SSimon Glass typedef struct stbtt_fontinfo
6358340ef62SSimon Glass {
6368340ef62SSimon Glass    void           * userdata;
6378340ef62SSimon Glass    unsigned char  * data;              // pointer to .ttf file
6388340ef62SSimon Glass    int              fontstart;         // offset of start of font
6398340ef62SSimon Glass 
6408340ef62SSimon Glass    int numGlyphs;                     // number of glyphs, needed for range checking
6418340ef62SSimon Glass 
6428340ef62SSimon Glass    int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
6438340ef62SSimon Glass    int index_map;                     // a cmap mapping for our chosen character encoding
6448340ef62SSimon Glass    int indexToLocFormat;              // format needed to map from glyph index to glyph
6458340ef62SSimon Glass } stbtt_fontinfo;
6468340ef62SSimon Glass 
6478340ef62SSimon Glass STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);
6488340ef62SSimon Glass // Given an offset into the file that defines a font, this function builds
6498340ef62SSimon Glass // the necessary cached info for the rest of the system. You must allocate
6508340ef62SSimon Glass // the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't
6518340ef62SSimon Glass // need to do anything special to free it, because the contents are pure
6528340ef62SSimon Glass // value data with no additional data structures. Returns 0 on failure.
6538340ef62SSimon Glass 
6548340ef62SSimon Glass 
6558340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
6568340ef62SSimon Glass //
6578340ef62SSimon Glass // CHARACTER TO GLYPH-INDEX CONVERSIOn
6588340ef62SSimon Glass 
6598340ef62SSimon Glass STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);
6608340ef62SSimon Glass // If you're going to perform multiple operations on the same character
6618340ef62SSimon Glass // and you want a speed-up, call this function with the character you're
6628340ef62SSimon Glass // going to process, then use glyph-based functions instead of the
6638340ef62SSimon Glass // codepoint-based functions.
6648340ef62SSimon Glass 
6658340ef62SSimon Glass 
6668340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
6678340ef62SSimon Glass //
6688340ef62SSimon Glass // CHARACTER PROPERTIES
6698340ef62SSimon Glass //
6708340ef62SSimon Glass 
6718340ef62SSimon Glass STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);
6728340ef62SSimon Glass // computes a scale factor to produce a font whose "height" is 'pixels' tall.
6738340ef62SSimon Glass // Height is measured as the distance from the highest ascender to the lowest
6748340ef62SSimon Glass // descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics
6758340ef62SSimon Glass // and computing:
6768340ef62SSimon Glass //       scale = pixels / (ascent - descent)
6778340ef62SSimon Glass // so if you prefer to measure height by the ascent only, use a similar calculation.
6788340ef62SSimon Glass 
6798340ef62SSimon Glass STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);
6808340ef62SSimon Glass // computes a scale factor to produce a font whose EM size is mapped to
6818340ef62SSimon Glass // 'pixels' tall. This is probably what traditional APIs compute, but
6828340ef62SSimon Glass // I'm not positive.
6838340ef62SSimon Glass 
6848340ef62SSimon Glass STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);
6858340ef62SSimon Glass // ascent is the coordinate above the baseline the font extends; descent
6868340ef62SSimon Glass // is the coordinate below the baseline the font extends (i.e. it is typically negative)
6878340ef62SSimon Glass // lineGap is the spacing between one row's descent and the next row's ascent...
6888340ef62SSimon Glass // so you should advance the vertical position by "*ascent - *descent + *lineGap"
6898340ef62SSimon Glass //   these are expressed in unscaled coordinates, so you must multiply by
6908340ef62SSimon Glass //   the scale factor for a given size
6918340ef62SSimon Glass 
6928340ef62SSimon Glass STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);
6938340ef62SSimon Glass // the bounding box around all possible characters
6948340ef62SSimon Glass 
6958340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);
6968340ef62SSimon Glass // leftSideBearing is the offset from the current horizontal position to the left edge of the character
6978340ef62SSimon Glass // advanceWidth is the offset from the current horizontal position to the next horizontal position
6988340ef62SSimon Glass //   these are expressed in unscaled coordinates
6998340ef62SSimon Glass 
7008340ef62SSimon Glass STBTT_DEF int  stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);
7018340ef62SSimon Glass // an additional amount to add to the 'advance' value between ch1 and ch2
7028340ef62SSimon Glass 
7038340ef62SSimon Glass STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);
7048340ef62SSimon Glass // Gets the bounding box of the visible part of the glyph, in unscaled coordinates
7058340ef62SSimon Glass 
7068340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);
7078340ef62SSimon Glass STBTT_DEF int  stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);
7088340ef62SSimon Glass STBTT_DEF int  stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
7098340ef62SSimon Glass // as above, but takes one or more glyph indices for greater efficiency
7108340ef62SSimon Glass 
7118340ef62SSimon Glass 
7128340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
7138340ef62SSimon Glass //
7148340ef62SSimon Glass // GLYPH SHAPES (you probably don't need these, but they have to go before
7158340ef62SSimon Glass // the bitmaps for C declaration-order reasons)
7168340ef62SSimon Glass //
7178340ef62SSimon Glass 
7188340ef62SSimon Glass #ifndef STBTT_vmove // you can predefine these to use different values (but why?)
7198340ef62SSimon Glass    enum {
7208340ef62SSimon Glass       STBTT_vmove=1,
7218340ef62SSimon Glass       STBTT_vline,
7228340ef62SSimon Glass       STBTT_vcurve
7238340ef62SSimon Glass    };
7248340ef62SSimon Glass #endif
7258340ef62SSimon Glass 
7268340ef62SSimon Glass #ifndef stbtt_vertex // you can predefine this to use different values
7278340ef62SSimon Glass                    // (we share this with other code at RAD)
7288340ef62SSimon Glass    #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file
7298340ef62SSimon Glass    typedef struct
7308340ef62SSimon Glass    {
7318340ef62SSimon Glass       stbtt_vertex_type x,y,cx,cy;
7328340ef62SSimon Glass       unsigned char type,padding;
7338340ef62SSimon Glass    } stbtt_vertex;
7348340ef62SSimon Glass #endif
7358340ef62SSimon Glass 
7368340ef62SSimon Glass STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);
7378340ef62SSimon Glass // returns non-zero if nothing is drawn for this glyph
7388340ef62SSimon Glass 
7398340ef62SSimon Glass STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);
7408340ef62SSimon Glass STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);
7418340ef62SSimon Glass // returns # of vertices and fills *vertices with the pointer to them
7428340ef62SSimon Glass //   these are expressed in "unscaled" coordinates
7438340ef62SSimon Glass //
7448340ef62SSimon Glass // The shape is a series of countours. Each one starts with
7458340ef62SSimon Glass // a STBTT_moveto, then consists of a series of mixed
7468340ef62SSimon Glass // STBTT_lineto and STBTT_curveto segments. A lineto
7478340ef62SSimon Glass // draws a line from previous endpoint to its x,y; a curveto
7488340ef62SSimon Glass // draws a quadratic bezier from previous endpoint to
7498340ef62SSimon Glass // its x,y, using cx,cy as the bezier control point.
7508340ef62SSimon Glass 
7518340ef62SSimon Glass STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);
7528340ef62SSimon Glass // frees the data allocated above
7538340ef62SSimon Glass 
7548340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
7558340ef62SSimon Glass //
7568340ef62SSimon Glass // BITMAP RENDERING
7578340ef62SSimon Glass //
7588340ef62SSimon Glass 
7598340ef62SSimon Glass STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);
7608340ef62SSimon Glass // frees the bitmap allocated below
7618340ef62SSimon Glass 
7628340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff);
7638340ef62SSimon Glass // allocates a large-enough single-channel 8bpp bitmap and renders the
7648340ef62SSimon Glass // specified character/glyph at the specified scale into it, with
7658340ef62SSimon Glass // antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque).
7668340ef62SSimon Glass // *width & *height are filled out with the width & height of the bitmap,
7678340ef62SSimon Glass // which is stored left-to-right, top-to-bottom.
7688340ef62SSimon Glass //
7698340ef62SSimon Glass // xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap
7708340ef62SSimon Glass 
7718340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff);
7728340ef62SSimon Glass // the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel
7738340ef62SSimon Glass // shift for the character
7748340ef62SSimon Glass 
7758340ef62SSimon Glass STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint);
7768340ef62SSimon Glass // the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap
7778340ef62SSimon Glass // in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap
7788340ef62SSimon Glass // is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the
7798340ef62SSimon Glass // width and height and positioning info for it first.
7808340ef62SSimon Glass 
7818340ef62SSimon Glass STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint);
7828340ef62SSimon Glass // same as stbtt_MakeCodepointBitmap, but you can specify a subpixel
7838340ef62SSimon Glass // shift for the character
7848340ef62SSimon Glass 
7858340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);
7868340ef62SSimon Glass // get the bbox of the bitmap centered around the glyph origin; so the
7878340ef62SSimon Glass // bitmap width is ix1-ix0, height is iy1-iy0, and location to place
7888340ef62SSimon Glass // the bitmap top left is (leftSideBearing*scale,iy0).
7898340ef62SSimon Glass // (Note that the bitmap uses y-increases-down, but the shape uses
7908340ef62SSimon Glass // y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.)
7918340ef62SSimon Glass 
7928340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);
7938340ef62SSimon Glass // same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel
7948340ef62SSimon Glass // shift for the character
7958340ef62SSimon Glass 
7968340ef62SSimon Glass // the following functions are equivalent to the above functions, but operate
7978340ef62SSimon Glass // on glyph indices instead of Unicode codepoints (for efficiency)
7988340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff);
7998340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff);
8008340ef62SSimon Glass STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph);
8018340ef62SSimon Glass STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph);
8028340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);
8038340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);
8048340ef62SSimon Glass 
8058340ef62SSimon Glass 
8068340ef62SSimon Glass // @TODO: don't expose this structure
8078340ef62SSimon Glass typedef struct
8088340ef62SSimon Glass {
8098340ef62SSimon Glass    int w,h,stride;
8108340ef62SSimon Glass    unsigned char *pixels;
8118340ef62SSimon Glass } stbtt__bitmap;
8128340ef62SSimon Glass 
8138340ef62SSimon Glass // rasterize a shape with quadratic beziers into a bitmap
8148340ef62SSimon Glass STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result,        // 1-channel bitmap to draw into
8158340ef62SSimon Glass                                float flatness_in_pixels,     // allowable error of curve in pixels
8168340ef62SSimon Glass                                stbtt_vertex *vertices,       // array of vertices defining shape
8178340ef62SSimon Glass                                int num_verts,                // number of vertices in above array
8188340ef62SSimon Glass                                float scale_x, float scale_y, // scale applied to input vertices
8198340ef62SSimon Glass                                float shift_x, float shift_y, // translation applied to input vertices
8208340ef62SSimon Glass                                int x_off, int y_off,         // another translation applied to input
8218340ef62SSimon Glass                                int invert,                   // if non-zero, vertically flip shape
8228340ef62SSimon Glass                                void *userdata);              // context for to STBTT_MALLOC
8238340ef62SSimon Glass 
8248340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
8258340ef62SSimon Glass //
8268340ef62SSimon Glass // Finding the right font...
8278340ef62SSimon Glass //
8288340ef62SSimon Glass // You should really just solve this offline, keep your own tables
8298340ef62SSimon Glass // of what font is what, and don't try to get it out of the .ttf file.
8308340ef62SSimon Glass // That's because getting it out of the .ttf file is really hard, because
8318340ef62SSimon Glass // the names in the file can appear in many possible encodings, in many
8328340ef62SSimon Glass // possible languages, and e.g. if you need a case-insensitive comparison,
8338340ef62SSimon Glass // the details of that depend on the encoding & language in a complex way
8348340ef62SSimon Glass // (actually underspecified in truetype, but also gigantic).
8358340ef62SSimon Glass //
8368340ef62SSimon Glass // But you can use the provided functions in two possible ways:
8378340ef62SSimon Glass //     stbtt_FindMatchingFont() will use *case-sensitive* comparisons on
8388340ef62SSimon Glass //             unicode-encoded names to try to find the font you want;
8398340ef62SSimon Glass //             you can run this before calling stbtt_InitFont()
8408340ef62SSimon Glass //
8418340ef62SSimon Glass //     stbtt_GetFontNameString() lets you get any of the various strings
8428340ef62SSimon Glass //             from the file yourself and do your own comparisons on them.
8438340ef62SSimon Glass //             You have to have called stbtt_InitFont() first.
8448340ef62SSimon Glass 
8458340ef62SSimon Glass 
8468340ef62SSimon Glass STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);
8478340ef62SSimon Glass // returns the offset (not index) of the font that matches, or -1 if none
8488340ef62SSimon Glass //   if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold".
8498340ef62SSimon Glass //   if you use any other flag, use a font name like "Arial"; this checks
8508340ef62SSimon Glass //     the 'macStyle' header field; i don't know if fonts set this consistently
8518340ef62SSimon Glass #define STBTT_MACSTYLE_DONTCARE     0
8528340ef62SSimon Glass #define STBTT_MACSTYLE_BOLD         1
8538340ef62SSimon Glass #define STBTT_MACSTYLE_ITALIC       2
8548340ef62SSimon Glass #define STBTT_MACSTYLE_UNDERSCORE   4
8558340ef62SSimon Glass #define STBTT_MACSTYLE_NONE         8   // <= not same as 0, this makes us check the bitfield is 0
8568340ef62SSimon Glass 
8578340ef62SSimon Glass STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);
8588340ef62SSimon Glass // returns 1/0 whether the first string interpreted as utf8 is identical to
8598340ef62SSimon Glass // the second string interpreted as big-endian utf16... useful for strings from next func
8608340ef62SSimon Glass 
8618340ef62SSimon Glass STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);
8628340ef62SSimon Glass // returns the string (which may be big-endian double byte, e.g. for unicode)
8638340ef62SSimon Glass // and puts the length in bytes in *length.
8648340ef62SSimon Glass //
8658340ef62SSimon Glass // some of the values for the IDs are below; for more see the truetype spec:
8668340ef62SSimon Glass //     http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html
8678340ef62SSimon Glass //     http://www.microsoft.com/typography/otspec/name.htm
8688340ef62SSimon Glass 
8698340ef62SSimon Glass enum { // platformID
8708340ef62SSimon Glass    STBTT_PLATFORM_ID_UNICODE   =0,
8718340ef62SSimon Glass    STBTT_PLATFORM_ID_MAC       =1,
8728340ef62SSimon Glass    STBTT_PLATFORM_ID_ISO       =2,
8738340ef62SSimon Glass    STBTT_PLATFORM_ID_MICROSOFT =3
8748340ef62SSimon Glass };
8758340ef62SSimon Glass 
8768340ef62SSimon Glass enum { // encodingID for STBTT_PLATFORM_ID_UNICODE
8778340ef62SSimon Glass    STBTT_UNICODE_EID_UNICODE_1_0    =0,
8788340ef62SSimon Glass    STBTT_UNICODE_EID_UNICODE_1_1    =1,
8798340ef62SSimon Glass    STBTT_UNICODE_EID_ISO_10646      =2,
8808340ef62SSimon Glass    STBTT_UNICODE_EID_UNICODE_2_0_BMP=3,
8818340ef62SSimon Glass    STBTT_UNICODE_EID_UNICODE_2_0_FULL=4
8828340ef62SSimon Glass };
8838340ef62SSimon Glass 
8848340ef62SSimon Glass enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT
8858340ef62SSimon Glass    STBTT_MS_EID_SYMBOL        =0,
8868340ef62SSimon Glass    STBTT_MS_EID_UNICODE_BMP   =1,
8878340ef62SSimon Glass    STBTT_MS_EID_SHIFTJIS      =2,
8888340ef62SSimon Glass    STBTT_MS_EID_UNICODE_FULL  =10
8898340ef62SSimon Glass };
8908340ef62SSimon Glass 
8918340ef62SSimon Glass enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes
8928340ef62SSimon Glass    STBTT_MAC_EID_ROMAN        =0,   STBTT_MAC_EID_ARABIC       =4,
8938340ef62SSimon Glass    STBTT_MAC_EID_JAPANESE     =1,   STBTT_MAC_EID_HEBREW       =5,
8948340ef62SSimon Glass    STBTT_MAC_EID_CHINESE_TRAD =2,   STBTT_MAC_EID_GREEK        =6,
8958340ef62SSimon Glass    STBTT_MAC_EID_KOREAN       =3,   STBTT_MAC_EID_RUSSIAN      =7
8968340ef62SSimon Glass };
8978340ef62SSimon Glass 
8988340ef62SSimon Glass enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID...
8998340ef62SSimon Glass        // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs
9008340ef62SSimon Glass    STBTT_MS_LANG_ENGLISH     =0x0409,   STBTT_MS_LANG_ITALIAN     =0x0410,
9018340ef62SSimon Glass    STBTT_MS_LANG_CHINESE     =0x0804,   STBTT_MS_LANG_JAPANESE    =0x0411,
9028340ef62SSimon Glass    STBTT_MS_LANG_DUTCH       =0x0413,   STBTT_MS_LANG_KOREAN      =0x0412,
9038340ef62SSimon Glass    STBTT_MS_LANG_FRENCH      =0x040c,   STBTT_MS_LANG_RUSSIAN     =0x0419,
9048340ef62SSimon Glass    STBTT_MS_LANG_GERMAN      =0x0407,   STBTT_MS_LANG_SPANISH     =0x0409,
9058340ef62SSimon Glass    STBTT_MS_LANG_HEBREW      =0x040d,   STBTT_MS_LANG_SWEDISH     =0x041D
9068340ef62SSimon Glass };
9078340ef62SSimon Glass 
9088340ef62SSimon Glass enum { // languageID for STBTT_PLATFORM_ID_MAC
9098340ef62SSimon Glass    STBTT_MAC_LANG_ENGLISH      =0 ,   STBTT_MAC_LANG_JAPANESE     =11,
9108340ef62SSimon Glass    STBTT_MAC_LANG_ARABIC       =12,   STBTT_MAC_LANG_KOREAN       =23,
9118340ef62SSimon Glass    STBTT_MAC_LANG_DUTCH        =4 ,   STBTT_MAC_LANG_RUSSIAN      =32,
9128340ef62SSimon Glass    STBTT_MAC_LANG_FRENCH       =1 ,   STBTT_MAC_LANG_SPANISH      =6 ,
9138340ef62SSimon Glass    STBTT_MAC_LANG_GERMAN       =2 ,   STBTT_MAC_LANG_SWEDISH      =5 ,
9148340ef62SSimon Glass    STBTT_MAC_LANG_HEBREW       =10,   STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33,
9158340ef62SSimon Glass    STBTT_MAC_LANG_ITALIAN      =3 ,   STBTT_MAC_LANG_CHINESE_TRAD =19
9168340ef62SSimon Glass };
9178340ef62SSimon Glass 
9188340ef62SSimon Glass #ifdef __cplusplus
9198340ef62SSimon Glass }
9208340ef62SSimon Glass #endif
9218340ef62SSimon Glass 
9228340ef62SSimon Glass #endif // __STB_INCLUDE_STB_TRUETYPE_H__
9238340ef62SSimon Glass 
9248340ef62SSimon Glass ///////////////////////////////////////////////////////////////////////////////
9258340ef62SSimon Glass ///////////////////////////////////////////////////////////////////////////////
9268340ef62SSimon Glass ////
9278340ef62SSimon Glass ////   IMPLEMENTATION
9288340ef62SSimon Glass ////
9298340ef62SSimon Glass ////
9308340ef62SSimon Glass 
9318340ef62SSimon Glass #ifdef STB_TRUETYPE_IMPLEMENTATION
9328340ef62SSimon Glass 
9338340ef62SSimon Glass #ifndef STBTT_MAX_OVERSAMPLE
9348340ef62SSimon Glass #define STBTT_MAX_OVERSAMPLE   8
9358340ef62SSimon Glass #endif
9368340ef62SSimon Glass 
9378340ef62SSimon Glass #if STBTT_MAX_OVERSAMPLE > 255
9388340ef62SSimon Glass #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
9398340ef62SSimon Glass #endif
9408340ef62SSimon Glass 
9418340ef62SSimon Glass typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
9428340ef62SSimon Glass 
9438340ef62SSimon Glass #ifndef STBTT_RASTERIZER_VERSION
9448340ef62SSimon Glass #define STBTT_RASTERIZER_VERSION 2
9458340ef62SSimon Glass #endif
9468340ef62SSimon Glass 
9478340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////
9488340ef62SSimon Glass //
9498340ef62SSimon Glass // accessors to parse data from file
9508340ef62SSimon Glass //
9518340ef62SSimon Glass 
9528340ef62SSimon Glass // on platforms that don't allow misaligned reads, if we want to allow
9538340ef62SSimon Glass // truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE
9548340ef62SSimon Glass 
9558340ef62SSimon Glass #define ttBYTE(p)     (* (stbtt_uint8 *) (p))
9568340ef62SSimon Glass #define ttCHAR(p)     (* (stbtt_int8 *) (p))
9578340ef62SSimon Glass #define ttFixed(p)    ttLONG(p)
9588340ef62SSimon Glass 
9598340ef62SSimon Glass #if defined(STB_TRUETYPE_BIGENDIAN) && !defined(ALLOW_UNALIGNED_TRUETYPE)
9608340ef62SSimon Glass 
9618340ef62SSimon Glass    #define ttUSHORT(p)   (* (stbtt_uint16 *) (p))
9628340ef62SSimon Glass    #define ttSHORT(p)    (* (stbtt_int16 *) (p))
9638340ef62SSimon Glass    #define ttULONG(p)    (* (stbtt_uint32 *) (p))
9648340ef62SSimon Glass    #define ttLONG(p)     (* (stbtt_int32 *) (p))
9658340ef62SSimon Glass 
9668340ef62SSimon Glass #else
9678340ef62SSimon Glass 
ttUSHORT(const stbtt_uint8 * p)9688340ef62SSimon Glass    static stbtt_uint16 ttUSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }
ttSHORT(const stbtt_uint8 * p)9698340ef62SSimon Glass    static stbtt_int16 ttSHORT(const stbtt_uint8 *p)   { return p[0]*256 + p[1]; }
ttULONG(const stbtt_uint8 * p)9708340ef62SSimon Glass    static stbtt_uint32 ttULONG(const stbtt_uint8 *p)  { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
ttLONG(const stbtt_uint8 * p)9718340ef62SSimon Glass    static stbtt_int32 ttLONG(const stbtt_uint8 *p)    { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
9728340ef62SSimon Glass 
9738340ef62SSimon Glass #endif
9748340ef62SSimon Glass 
9758340ef62SSimon Glass #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
9768340ef62SSimon Glass #define stbtt_tag(p,str)           stbtt_tag4(p,str[0],str[1],str[2],str[3])
9778340ef62SSimon Glass 
stbtt__isfont(const stbtt_uint8 * font)9788340ef62SSimon Glass static int stbtt__isfont(const stbtt_uint8 *font)
9798340ef62SSimon Glass {
9808340ef62SSimon Glass    // check the version number
9818340ef62SSimon Glass    if (stbtt_tag4(font, '1',0,0,0))  return 1; // TrueType 1
9828340ef62SSimon Glass    if (stbtt_tag(font, "typ1"))   return 1; // TrueType with type 1 font -- we don't support this!
9838340ef62SSimon Glass    if (stbtt_tag(font, "OTTO"))   return 1; // OpenType with CFF
9848340ef62SSimon Glass    if (stbtt_tag4(font, 0,1,0,0)) return 1; // OpenType 1.0
9858340ef62SSimon Glass    return 0;
9868340ef62SSimon Glass }
9878340ef62SSimon Glass 
9888340ef62SSimon Glass // @OPTIMIZE: binary search
stbtt__find_table(stbtt_uint8 * data,stbtt_uint32 fontstart,const char * tag)9898340ef62SSimon Glass static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag)
9908340ef62SSimon Glass {
9918340ef62SSimon Glass    stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
9928340ef62SSimon Glass    stbtt_uint32 tabledir = fontstart + 12;
9938340ef62SSimon Glass    stbtt_int32 i;
9948340ef62SSimon Glass    for (i=0; i < num_tables; ++i) {
9958340ef62SSimon Glass       stbtt_uint32 loc = tabledir + 16*i;
9968340ef62SSimon Glass       if (stbtt_tag(data+loc+0, tag))
9978340ef62SSimon Glass          return ttULONG(data+loc+8);
9988340ef62SSimon Glass    }
9998340ef62SSimon Glass    return 0;
10008340ef62SSimon Glass }
10018340ef62SSimon Glass 
stbtt_GetFontOffsetForIndex(const unsigned char * font_collection,int index)10028340ef62SSimon Glass STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *font_collection, int index)
10038340ef62SSimon Glass {
10048340ef62SSimon Glass    // if it's just a font, there's only one valid index
10058340ef62SSimon Glass    if (stbtt__isfont(font_collection))
10068340ef62SSimon Glass       return index == 0 ? 0 : -1;
10078340ef62SSimon Glass 
10088340ef62SSimon Glass    // check if it's a TTC
10098340ef62SSimon Glass    if (stbtt_tag(font_collection, "ttcf")) {
10108340ef62SSimon Glass       // version 1?
10118340ef62SSimon Glass       if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
10128340ef62SSimon Glass          stbtt_int32 n = ttLONG(font_collection+8);
10138340ef62SSimon Glass          if (index >= n)
10148340ef62SSimon Glass             return -1;
10158340ef62SSimon Glass          return ttULONG(font_collection+12+index*4);
10168340ef62SSimon Glass       }
10178340ef62SSimon Glass    }
10188340ef62SSimon Glass    return -1;
10198340ef62SSimon Glass }
10208340ef62SSimon Glass 
stbtt_InitFont(stbtt_fontinfo * info,const unsigned char * data2,int fontstart)10218340ef62SSimon Glass STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart)
10228340ef62SSimon Glass {
10238340ef62SSimon Glass    stbtt_uint8 *data = (stbtt_uint8 *) data2;
10248340ef62SSimon Glass    stbtt_uint32 cmap, t;
10258340ef62SSimon Glass    stbtt_int32 i,numTables;
10268340ef62SSimon Glass 
10278340ef62SSimon Glass    info->data = data;
10288340ef62SSimon Glass    info->fontstart = fontstart;
10298340ef62SSimon Glass 
10308340ef62SSimon Glass    cmap = stbtt__find_table(data, fontstart, "cmap");       // required
10318340ef62SSimon Glass    info->loca = stbtt__find_table(data, fontstart, "loca"); // required
10328340ef62SSimon Glass    info->head = stbtt__find_table(data, fontstart, "head"); // required
10338340ef62SSimon Glass    info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required
10348340ef62SSimon Glass    info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required
10358340ef62SSimon Glass    info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required
10368340ef62SSimon Glass    info->kern = stbtt__find_table(data, fontstart, "kern"); // not required
10378340ef62SSimon Glass    if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
10388340ef62SSimon Glass       return 0;
10398340ef62SSimon Glass 
10408340ef62SSimon Glass    t = stbtt__find_table(data, fontstart, "maxp");
10418340ef62SSimon Glass    if (t)
10428340ef62SSimon Glass       info->numGlyphs = ttUSHORT(data+t+4);
10438340ef62SSimon Glass    else
10448340ef62SSimon Glass       info->numGlyphs = 0xffff;
10458340ef62SSimon Glass 
10468340ef62SSimon Glass    // find a cmap encoding table we understand *now* to avoid searching
10478340ef62SSimon Glass    // later. (todo: could make this installable)
10488340ef62SSimon Glass    // the same regardless of glyph.
10498340ef62SSimon Glass    numTables = ttUSHORT(data + cmap + 2);
10508340ef62SSimon Glass    info->index_map = 0;
10518340ef62SSimon Glass    for (i=0; i < numTables; ++i) {
10528340ef62SSimon Glass       stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
10538340ef62SSimon Glass       // find an encoding we understand:
10548340ef62SSimon Glass       switch(ttUSHORT(data+encoding_record)) {
10558340ef62SSimon Glass          case STBTT_PLATFORM_ID_MICROSOFT:
10568340ef62SSimon Glass             switch (ttUSHORT(data+encoding_record+2)) {
10578340ef62SSimon Glass                case STBTT_MS_EID_UNICODE_BMP:
10588340ef62SSimon Glass                case STBTT_MS_EID_UNICODE_FULL:
10598340ef62SSimon Glass                   // MS/Unicode
10608340ef62SSimon Glass                   info->index_map = cmap + ttULONG(data+encoding_record+4);
10618340ef62SSimon Glass                   break;
10628340ef62SSimon Glass             }
10638340ef62SSimon Glass             break;
10648340ef62SSimon Glass         case STBTT_PLATFORM_ID_UNICODE:
10658340ef62SSimon Glass             // Mac/iOS has these
10668340ef62SSimon Glass             // all the encodingIDs are unicode, so we don't bother to check it
10678340ef62SSimon Glass             info->index_map = cmap + ttULONG(data+encoding_record+4);
10688340ef62SSimon Glass             break;
10698340ef62SSimon Glass       }
10708340ef62SSimon Glass    }
10718340ef62SSimon Glass    if (info->index_map == 0)
10728340ef62SSimon Glass       return 0;
10738340ef62SSimon Glass 
10748340ef62SSimon Glass    info->indexToLocFormat = ttUSHORT(data+info->head + 50);
10758340ef62SSimon Glass    return 1;
10768340ef62SSimon Glass }
10778340ef62SSimon Glass 
stbtt_FindGlyphIndex(const stbtt_fontinfo * info,int unicode_codepoint)10788340ef62SSimon Glass STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
10798340ef62SSimon Glass {
10808340ef62SSimon Glass    stbtt_uint8 *data = info->data;
10818340ef62SSimon Glass    stbtt_uint32 index_map = info->index_map;
10828340ef62SSimon Glass 
10838340ef62SSimon Glass    stbtt_uint16 format = ttUSHORT(data + index_map + 0);
10848340ef62SSimon Glass    if (format == 0) { // apple byte encoding
10858340ef62SSimon Glass       stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
10868340ef62SSimon Glass       if (unicode_codepoint < bytes-6)
10878340ef62SSimon Glass          return ttBYTE(data + index_map + 6 + unicode_codepoint);
10888340ef62SSimon Glass       return 0;
10898340ef62SSimon Glass    } else if (format == 6) {
10908340ef62SSimon Glass       stbtt_uint32 first = ttUSHORT(data + index_map + 6);
10918340ef62SSimon Glass       stbtt_uint32 count = ttUSHORT(data + index_map + 8);
10928340ef62SSimon Glass       if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
10938340ef62SSimon Glass          return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
10948340ef62SSimon Glass       return 0;
10958340ef62SSimon Glass    } else if (format == 2) {
10968340ef62SSimon Glass       STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean
10978340ef62SSimon Glass       return 0;
10988340ef62SSimon Glass    } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges
10998340ef62SSimon Glass       stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
11008340ef62SSimon Glass       stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
11018340ef62SSimon Glass       stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
11028340ef62SSimon Glass       stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
11038340ef62SSimon Glass 
11048340ef62SSimon Glass       // do a binary search of the segments
11058340ef62SSimon Glass       stbtt_uint32 endCount = index_map + 14;
11068340ef62SSimon Glass       stbtt_uint32 search = endCount;
11078340ef62SSimon Glass 
11088340ef62SSimon Glass       if (unicode_codepoint > 0xffff)
11098340ef62SSimon Glass          return 0;
11108340ef62SSimon Glass 
11118340ef62SSimon Glass       // they lie from endCount .. endCount + segCount
11128340ef62SSimon Glass       // but searchRange is the nearest power of two, so...
11138340ef62SSimon Glass       if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
11148340ef62SSimon Glass          search += rangeShift*2;
11158340ef62SSimon Glass 
11168340ef62SSimon Glass       // now decrement to bias correctly to find smallest
11178340ef62SSimon Glass       search -= 2;
11188340ef62SSimon Glass       while (entrySelector) {
11198340ef62SSimon Glass          stbtt_uint16 end;
11208340ef62SSimon Glass          searchRange >>= 1;
11218340ef62SSimon Glass          end = ttUSHORT(data + search + searchRange*2);
11228340ef62SSimon Glass          if (unicode_codepoint > end)
11238340ef62SSimon Glass             search += searchRange*2;
11248340ef62SSimon Glass          --entrySelector;
11258340ef62SSimon Glass       }
11268340ef62SSimon Glass       search += 2;
11278340ef62SSimon Glass 
11288340ef62SSimon Glass       {
11298340ef62SSimon Glass          stbtt_uint16 offset, start;
11308340ef62SSimon Glass          stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
11318340ef62SSimon Glass 
11328340ef62SSimon Glass          STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
11338340ef62SSimon Glass          start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
11348340ef62SSimon Glass          if (unicode_codepoint < start)
11358340ef62SSimon Glass             return 0;
11368340ef62SSimon Glass 
11378340ef62SSimon Glass          offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
11388340ef62SSimon Glass          if (offset == 0)
11398340ef62SSimon Glass             return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
11408340ef62SSimon Glass 
11418340ef62SSimon Glass          return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
11428340ef62SSimon Glass       }
11438340ef62SSimon Glass    } else if (format == 12 || format == 13) {
11448340ef62SSimon Glass       stbtt_uint32 ngroups = ttULONG(data+index_map+12);
11458340ef62SSimon Glass       stbtt_int32 low,high;
11468340ef62SSimon Glass       low = 0; high = (stbtt_int32)ngroups;
11478340ef62SSimon Glass       // Binary search the right group.
11488340ef62SSimon Glass       while (low < high) {
11498340ef62SSimon Glass          stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high
11508340ef62SSimon Glass          stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
11518340ef62SSimon Glass          stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
11528340ef62SSimon Glass          if ((stbtt_uint32) unicode_codepoint < start_char)
11538340ef62SSimon Glass             high = mid;
11548340ef62SSimon Glass          else if ((stbtt_uint32) unicode_codepoint > end_char)
11558340ef62SSimon Glass             low = mid+1;
11568340ef62SSimon Glass          else {
11578340ef62SSimon Glass             stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
11588340ef62SSimon Glass             if (format == 12)
11598340ef62SSimon Glass                return start_glyph + unicode_codepoint-start_char;
11608340ef62SSimon Glass             else // format == 13
11618340ef62SSimon Glass                return start_glyph;
11628340ef62SSimon Glass          }
11638340ef62SSimon Glass       }
11648340ef62SSimon Glass       return 0; // not found
11658340ef62SSimon Glass    }
11668340ef62SSimon Glass    // @TODO
11678340ef62SSimon Glass    STBTT_assert(0);
11688340ef62SSimon Glass    return 0;
11698340ef62SSimon Glass }
11708340ef62SSimon Glass 
stbtt_GetCodepointShape(const stbtt_fontinfo * info,int unicode_codepoint,stbtt_vertex ** vertices)11718340ef62SSimon Glass STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)
11728340ef62SSimon Glass {
11738340ef62SSimon Glass    return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
11748340ef62SSimon Glass }
11758340ef62SSimon Glass 
stbtt_setvertex(stbtt_vertex * v,stbtt_uint8 type,stbtt_int32 x,stbtt_int32 y,stbtt_int32 cx,stbtt_int32 cy)11768340ef62SSimon Glass static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
11778340ef62SSimon Glass {
11788340ef62SSimon Glass    v->type = type;
11798340ef62SSimon Glass    v->x = (stbtt_int16) x;
11808340ef62SSimon Glass    v->y = (stbtt_int16) y;
11818340ef62SSimon Glass    v->cx = (stbtt_int16) cx;
11828340ef62SSimon Glass    v->cy = (stbtt_int16) cy;
11838340ef62SSimon Glass }
11848340ef62SSimon Glass 
stbtt__GetGlyfOffset(const stbtt_fontinfo * info,int glyph_index)11858340ef62SSimon Glass static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)
11868340ef62SSimon Glass {
11878340ef62SSimon Glass    int g1,g2;
11888340ef62SSimon Glass 
11898340ef62SSimon Glass    if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range
11908340ef62SSimon Glass    if (info->indexToLocFormat >= 2)    return -1; // unknown index->glyph map format
11918340ef62SSimon Glass 
11928340ef62SSimon Glass    if (info->indexToLocFormat == 0) {
11938340ef62SSimon Glass       g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
11948340ef62SSimon Glass       g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
11958340ef62SSimon Glass    } else {
11968340ef62SSimon Glass       g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
11978340ef62SSimon Glass       g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
11988340ef62SSimon Glass    }
11998340ef62SSimon Glass 
12008340ef62SSimon Glass    return g1==g2 ? -1 : g1; // if length is 0, return -1
12018340ef62SSimon Glass }
12028340ef62SSimon Glass 
stbtt_GetGlyphBox(const stbtt_fontinfo * info,int glyph_index,int * x0,int * y0,int * x1,int * y1)12038340ef62SSimon Glass STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
12048340ef62SSimon Glass {
12058340ef62SSimon Glass    int g = stbtt__GetGlyfOffset(info, glyph_index);
12068340ef62SSimon Glass    if (g < 0) return 0;
12078340ef62SSimon Glass 
12088340ef62SSimon Glass    if (x0) *x0 = ttSHORT(info->data + g + 2);
12098340ef62SSimon Glass    if (y0) *y0 = ttSHORT(info->data + g + 4);
12108340ef62SSimon Glass    if (x1) *x1 = ttSHORT(info->data + g + 6);
12118340ef62SSimon Glass    if (y1) *y1 = ttSHORT(info->data + g + 8);
12128340ef62SSimon Glass    return 1;
12138340ef62SSimon Glass }
12148340ef62SSimon Glass 
stbtt_GetCodepointBox(const stbtt_fontinfo * info,int codepoint,int * x0,int * y0,int * x1,int * y1)12158340ef62SSimon Glass STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)
12168340ef62SSimon Glass {
12178340ef62SSimon Glass    return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);
12188340ef62SSimon Glass }
12198340ef62SSimon Glass 
stbtt_IsGlyphEmpty(const stbtt_fontinfo * info,int glyph_index)12208340ef62SSimon Glass STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)
12218340ef62SSimon Glass {
12228340ef62SSimon Glass    stbtt_int16 numberOfContours;
12238340ef62SSimon Glass    int g = stbtt__GetGlyfOffset(info, glyph_index);
12248340ef62SSimon Glass    if (g < 0) return 1;
12258340ef62SSimon Glass    numberOfContours = ttSHORT(info->data + g);
12268340ef62SSimon Glass    return numberOfContours == 0;
12278340ef62SSimon Glass }
12288340ef62SSimon Glass 
stbtt__close_shape(stbtt_vertex * vertices,int num_vertices,int was_off,int start_off,stbtt_int32 sx,stbtt_int32 sy,stbtt_int32 scx,stbtt_int32 scy,stbtt_int32 cx,stbtt_int32 cy)12298340ef62SSimon Glass static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off,
12308340ef62SSimon Glass     stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
12318340ef62SSimon Glass {
12328340ef62SSimon Glass    if (start_off) {
12338340ef62SSimon Glass       if (was_off)
12348340ef62SSimon Glass          stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
12358340ef62SSimon Glass       stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);
12368340ef62SSimon Glass    } else {
12378340ef62SSimon Glass       if (was_off)
12388340ef62SSimon Glass          stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);
12398340ef62SSimon Glass       else
12408340ef62SSimon Glass          stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);
12418340ef62SSimon Glass    }
12428340ef62SSimon Glass    return num_vertices;
12438340ef62SSimon Glass }
12448340ef62SSimon Glass 
stbtt_GetGlyphShape(const stbtt_fontinfo * info,int glyph_index,stbtt_vertex ** pvertices)12458340ef62SSimon Glass STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)
12468340ef62SSimon Glass {
12478340ef62SSimon Glass    stbtt_int16 numberOfContours;
12488340ef62SSimon Glass    stbtt_uint8 *endPtsOfContours;
12498340ef62SSimon Glass    stbtt_uint8 *data = info->data;
12508340ef62SSimon Glass    stbtt_vertex *vertices=0;
12518340ef62SSimon Glass    int num_vertices=0;
12528340ef62SSimon Glass    int g = stbtt__GetGlyfOffset(info, glyph_index);
12538340ef62SSimon Glass 
12548340ef62SSimon Glass    *pvertices = NULL;
12558340ef62SSimon Glass 
12568340ef62SSimon Glass    if (g < 0) return 0;
12578340ef62SSimon Glass 
12588340ef62SSimon Glass    numberOfContours = ttSHORT(data + g);
12598340ef62SSimon Glass 
12608340ef62SSimon Glass    if (numberOfContours > 0) {
12618340ef62SSimon Glass       stbtt_uint8 flags=0,flagcount;
12628340ef62SSimon Glass       stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
12638340ef62SSimon Glass       stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
12648340ef62SSimon Glass       stbtt_uint8 *points;
12658340ef62SSimon Glass       endPtsOfContours = (data + g + 10);
12668340ef62SSimon Glass       ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
12678340ef62SSimon Glass       points = data + g + 10 + numberOfContours * 2 + 2 + ins;
12688340ef62SSimon Glass 
12698340ef62SSimon Glass       n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
12708340ef62SSimon Glass 
12718340ef62SSimon Glass       m = n + 2*numberOfContours;  // a loose bound on how many vertices we might need
12728340ef62SSimon Glass       vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata);
12738340ef62SSimon Glass       if (vertices == 0)
12748340ef62SSimon Glass          return 0;
12758340ef62SSimon Glass 
12768340ef62SSimon Glass       next_move = 0;
12778340ef62SSimon Glass       flagcount=0;
12788340ef62SSimon Glass 
12798340ef62SSimon Glass       // in first pass, we load uninterpreted data into the allocated array
12808340ef62SSimon Glass       // above, shifted to the end of the array so we won't overwrite it when
12818340ef62SSimon Glass       // we create our final data starting from the front
12828340ef62SSimon Glass 
12838340ef62SSimon Glass       off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated
12848340ef62SSimon Glass 
12858340ef62SSimon Glass       // first load flags
12868340ef62SSimon Glass 
12878340ef62SSimon Glass       for (i=0; i < n; ++i) {
12888340ef62SSimon Glass          if (flagcount == 0) {
12898340ef62SSimon Glass             flags = *points++;
12908340ef62SSimon Glass             if (flags & 8)
12918340ef62SSimon Glass                flagcount = *points++;
12928340ef62SSimon Glass          } else
12938340ef62SSimon Glass             --flagcount;
12948340ef62SSimon Glass          vertices[off+i].type = flags;
12958340ef62SSimon Glass       }
12968340ef62SSimon Glass 
12978340ef62SSimon Glass       // now load x coordinates
12988340ef62SSimon Glass       x=0;
12998340ef62SSimon Glass       for (i=0; i < n; ++i) {
13008340ef62SSimon Glass          flags = vertices[off+i].type;
13018340ef62SSimon Glass          if (flags & 2) {
13028340ef62SSimon Glass             stbtt_int16 dx = *points++;
13038340ef62SSimon Glass             x += (flags & 16) ? dx : -dx; // ???
13048340ef62SSimon Glass          } else {
13058340ef62SSimon Glass             if (!(flags & 16)) {
13068340ef62SSimon Glass                x = x + (stbtt_int16) (points[0]*256 + points[1]);
13078340ef62SSimon Glass                points += 2;
13088340ef62SSimon Glass             }
13098340ef62SSimon Glass          }
13108340ef62SSimon Glass          vertices[off+i].x = (stbtt_int16) x;
13118340ef62SSimon Glass       }
13128340ef62SSimon Glass 
13138340ef62SSimon Glass       // now load y coordinates
13148340ef62SSimon Glass       y=0;
13158340ef62SSimon Glass       for (i=0; i < n; ++i) {
13168340ef62SSimon Glass          flags = vertices[off+i].type;
13178340ef62SSimon Glass          if (flags & 4) {
13188340ef62SSimon Glass             stbtt_int16 dy = *points++;
13198340ef62SSimon Glass             y += (flags & 32) ? dy : -dy; // ???
13208340ef62SSimon Glass          } else {
13218340ef62SSimon Glass             if (!(flags & 32)) {
13228340ef62SSimon Glass                y = y + (stbtt_int16) (points[0]*256 + points[1]);
13238340ef62SSimon Glass                points += 2;
13248340ef62SSimon Glass             }
13258340ef62SSimon Glass          }
13268340ef62SSimon Glass          vertices[off+i].y = (stbtt_int16) y;
13278340ef62SSimon Glass       }
13288340ef62SSimon Glass 
13298340ef62SSimon Glass       // now convert them to our format
13308340ef62SSimon Glass       num_vertices=0;
13318340ef62SSimon Glass       sx = sy = cx = cy = scx = scy = 0;
13328340ef62SSimon Glass       for (i=0; i < n; ++i) {
13338340ef62SSimon Glass          flags = vertices[off+i].type;
13348340ef62SSimon Glass          x     = (stbtt_int16) vertices[off+i].x;
13358340ef62SSimon Glass          y     = (stbtt_int16) vertices[off+i].y;
13368340ef62SSimon Glass 
13378340ef62SSimon Glass          if (next_move == i) {
13388340ef62SSimon Glass             if (i != 0)
13398340ef62SSimon Glass                num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
13408340ef62SSimon Glass 
13418340ef62SSimon Glass             // now start the new one
13428340ef62SSimon Glass             start_off = !(flags & 1);
13438340ef62SSimon Glass             if (start_off) {
13448340ef62SSimon Glass                // if we start off with an off-curve point, then when we need to find a point on the curve
13458340ef62SSimon Glass                // where we can start, and we need to save some state for when we wraparound.
13468340ef62SSimon Glass                scx = x;
13478340ef62SSimon Glass                scy = y;
13488340ef62SSimon Glass                if (!(vertices[off+i+1].type & 1)) {
13498340ef62SSimon Glass                   // next point is also a curve point, so interpolate an on-point curve
13508340ef62SSimon Glass                   sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
13518340ef62SSimon Glass                   sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
13528340ef62SSimon Glass                } else {
13538340ef62SSimon Glass                   // otherwise just use the next point as our start point
13548340ef62SSimon Glass                   sx = (stbtt_int32) vertices[off+i+1].x;
13558340ef62SSimon Glass                   sy = (stbtt_int32) vertices[off+i+1].y;
13568340ef62SSimon Glass                   ++i; // we're using point i+1 as the starting point, so skip it
13578340ef62SSimon Glass                }
13588340ef62SSimon Glass             } else {
13598340ef62SSimon Glass                sx = x;
13608340ef62SSimon Glass                sy = y;
13618340ef62SSimon Glass             }
13628340ef62SSimon Glass             stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);
13638340ef62SSimon Glass             was_off = 0;
13648340ef62SSimon Glass             next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
13658340ef62SSimon Glass             ++j;
13668340ef62SSimon Glass          } else {
13678340ef62SSimon Glass             if (!(flags & 1)) { // if it's a curve
13688340ef62SSimon Glass                if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint
13698340ef62SSimon Glass                   stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
13708340ef62SSimon Glass                cx = x;
13718340ef62SSimon Glass                cy = y;
13728340ef62SSimon Glass                was_off = 1;
13738340ef62SSimon Glass             } else {
13748340ef62SSimon Glass                if (was_off)
13758340ef62SSimon Glass                   stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);
13768340ef62SSimon Glass                else
13778340ef62SSimon Glass                   stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);
13788340ef62SSimon Glass                was_off = 0;
13798340ef62SSimon Glass             }
13808340ef62SSimon Glass          }
13818340ef62SSimon Glass       }
13828340ef62SSimon Glass       num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
13838340ef62SSimon Glass    } else if (numberOfContours == -1) {
13848340ef62SSimon Glass       // Compound shapes.
13858340ef62SSimon Glass       int more = 1;
13868340ef62SSimon Glass       stbtt_uint8 *comp = data + g + 10;
13878340ef62SSimon Glass       num_vertices = 0;
13888340ef62SSimon Glass       vertices = 0;
13898340ef62SSimon Glass       while (more) {
13908340ef62SSimon Glass          stbtt_uint16 flags, gidx;
13918340ef62SSimon Glass          int comp_num_verts = 0, i;
13928340ef62SSimon Glass          stbtt_vertex *comp_verts = 0, *tmp = 0;
13938340ef62SSimon Glass          float mtx[6] = {1,0,0,1,0,0}, m, n;
13948340ef62SSimon Glass 
13958340ef62SSimon Glass          flags = ttSHORT(comp); comp+=2;
13968340ef62SSimon Glass          gidx = ttSHORT(comp); comp+=2;
13978340ef62SSimon Glass 
13988340ef62SSimon Glass          if (flags & 2) { // XY values
13998340ef62SSimon Glass             if (flags & 1) { // shorts
14008340ef62SSimon Glass                mtx[4] = ttSHORT(comp); comp+=2;
14018340ef62SSimon Glass                mtx[5] = ttSHORT(comp); comp+=2;
14028340ef62SSimon Glass             } else {
14038340ef62SSimon Glass                mtx[4] = ttCHAR(comp); comp+=1;
14048340ef62SSimon Glass                mtx[5] = ttCHAR(comp); comp+=1;
14058340ef62SSimon Glass             }
14068340ef62SSimon Glass          }
14078340ef62SSimon Glass          else {
14088340ef62SSimon Glass             // @TODO handle matching point
14098340ef62SSimon Glass             STBTT_assert(0);
14108340ef62SSimon Glass          }
14118340ef62SSimon Glass          if (flags & (1<<3)) { // WE_HAVE_A_SCALE
14128340ef62SSimon Glass             mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
14138340ef62SSimon Glass             mtx[1] = mtx[2] = 0;
14148340ef62SSimon Glass          } else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE
14158340ef62SSimon Glass             mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
14168340ef62SSimon Glass             mtx[1] = mtx[2] = 0;
14178340ef62SSimon Glass             mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
14188340ef62SSimon Glass          } else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO
14198340ef62SSimon Glass             mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
14208340ef62SSimon Glass             mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
14218340ef62SSimon Glass             mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
14228340ef62SSimon Glass             mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
14238340ef62SSimon Glass          }
14248340ef62SSimon Glass 
14258340ef62SSimon Glass          // Find transformation scales.
14268340ef62SSimon Glass          m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
14278340ef62SSimon Glass          n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
14288340ef62SSimon Glass 
14298340ef62SSimon Glass          // Get indexed glyph.
14308340ef62SSimon Glass          comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
14318340ef62SSimon Glass          if (comp_num_verts > 0) {
14328340ef62SSimon Glass             // Transform vertices.
14338340ef62SSimon Glass             for (i = 0; i < comp_num_verts; ++i) {
14348340ef62SSimon Glass                stbtt_vertex* v = &comp_verts[i];
14358340ef62SSimon Glass                stbtt_vertex_type x,y;
14368340ef62SSimon Glass                x=v->x; y=v->y;
14378340ef62SSimon Glass                v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
14388340ef62SSimon Glass                v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
14398340ef62SSimon Glass                x=v->cx; y=v->cy;
14408340ef62SSimon Glass                v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
14418340ef62SSimon Glass                v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
14428340ef62SSimon Glass             }
14438340ef62SSimon Glass             // Append vertices.
14448340ef62SSimon Glass             tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata);
14458340ef62SSimon Glass             if (!tmp) {
14468340ef62SSimon Glass                if (vertices) STBTT_free(vertices, info->userdata);
14478340ef62SSimon Glass                if (comp_verts) STBTT_free(comp_verts, info->userdata);
14488340ef62SSimon Glass                return 0;
14498340ef62SSimon Glass             }
14508340ef62SSimon Glass             if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex));
14518340ef62SSimon Glass             STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));
14528340ef62SSimon Glass             if (vertices) STBTT_free(vertices, info->userdata);
14538340ef62SSimon Glass             vertices = tmp;
14548340ef62SSimon Glass             STBTT_free(comp_verts, info->userdata);
14558340ef62SSimon Glass             num_vertices += comp_num_verts;
14568340ef62SSimon Glass          }
14578340ef62SSimon Glass          // More components ?
14588340ef62SSimon Glass          more = flags & (1<<5);
14598340ef62SSimon Glass       }
14608340ef62SSimon Glass    } else if (numberOfContours < 0) {
14618340ef62SSimon Glass       // @TODO other compound variations?
14628340ef62SSimon Glass       STBTT_assert(0);
14638340ef62SSimon Glass    } else {
14648340ef62SSimon Glass       // numberOfCounters == 0, do nothing
14658340ef62SSimon Glass    }
14668340ef62SSimon Glass 
14678340ef62SSimon Glass    *pvertices = vertices;
14688340ef62SSimon Glass    return num_vertices;
14698340ef62SSimon Glass }
14708340ef62SSimon Glass 
stbtt_GetGlyphHMetrics(const stbtt_fontinfo * info,int glyph_index,int * advanceWidth,int * leftSideBearing)14718340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)
14728340ef62SSimon Glass {
14738340ef62SSimon Glass    stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
14748340ef62SSimon Glass    if (glyph_index < numOfLongHorMetrics) {
14758340ef62SSimon Glass       if (advanceWidth)     *advanceWidth    = ttSHORT(info->data + info->hmtx + 4*glyph_index);
14768340ef62SSimon Glass       if (leftSideBearing)  *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
14778340ef62SSimon Glass    } else {
14788340ef62SSimon Glass       if (advanceWidth)     *advanceWidth    = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
14798340ef62SSimon Glass       if (leftSideBearing)  *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
14808340ef62SSimon Glass    }
14818340ef62SSimon Glass }
14828340ef62SSimon Glass 
stbtt_GetGlyphKernAdvance(const stbtt_fontinfo * info,int glyph1,int glyph2)14838340ef62SSimon Glass STBTT_DEF int  stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)
14848340ef62SSimon Glass {
14858340ef62SSimon Glass    stbtt_uint8 *data = info->data + info->kern;
14868340ef62SSimon Glass    stbtt_uint32 needle, straw;
14878340ef62SSimon Glass    int l, r, m;
14888340ef62SSimon Glass 
14898340ef62SSimon Glass    // we only look at the first table. it must be 'horizontal' and format 0.
14908340ef62SSimon Glass    if (!info->kern)
14918340ef62SSimon Glass       return 0;
14928340ef62SSimon Glass    if (ttUSHORT(data+2) < 1) // number of tables, need at least 1
14938340ef62SSimon Glass       return 0;
14948340ef62SSimon Glass    if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format
14958340ef62SSimon Glass       return 0;
14968340ef62SSimon Glass 
14978340ef62SSimon Glass    l = 0;
14988340ef62SSimon Glass    r = ttUSHORT(data+10) - 1;
14998340ef62SSimon Glass    needle = glyph1 << 16 | glyph2;
15008340ef62SSimon Glass    while (l <= r) {
15018340ef62SSimon Glass       m = (l + r) >> 1;
15028340ef62SSimon Glass       straw = ttULONG(data+18+(m*6)); // note: unaligned read
15038340ef62SSimon Glass       if (needle < straw)
15048340ef62SSimon Glass          r = m - 1;
15058340ef62SSimon Glass       else if (needle > straw)
15068340ef62SSimon Glass          l = m + 1;
15078340ef62SSimon Glass       else
15088340ef62SSimon Glass          return ttSHORT(data+22+(m*6));
15098340ef62SSimon Glass    }
15108340ef62SSimon Glass    return 0;
15118340ef62SSimon Glass }
15128340ef62SSimon Glass 
stbtt_GetCodepointKernAdvance(const stbtt_fontinfo * info,int ch1,int ch2)15138340ef62SSimon Glass STBTT_DEF int  stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)
15148340ef62SSimon Glass {
15158340ef62SSimon Glass    if (!info->kern) // if no kerning table, don't waste time looking up both codepoint->glyphs
15168340ef62SSimon Glass       return 0;
15178340ef62SSimon Glass    return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));
15188340ef62SSimon Glass }
15198340ef62SSimon Glass 
stbtt_GetCodepointHMetrics(const stbtt_fontinfo * info,int codepoint,int * advanceWidth,int * leftSideBearing)15208340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)
15218340ef62SSimon Glass {
15228340ef62SSimon Glass    stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
15238340ef62SSimon Glass }
15248340ef62SSimon Glass 
stbtt_GetFontVMetrics(const stbtt_fontinfo * info,int * ascent,int * descent,int * lineGap)15258340ef62SSimon Glass STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
15268340ef62SSimon Glass {
15278340ef62SSimon Glass    if (ascent ) *ascent  = ttSHORT(info->data+info->hhea + 4);
15288340ef62SSimon Glass    if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
15298340ef62SSimon Glass    if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
15308340ef62SSimon Glass }
15318340ef62SSimon Glass 
stbtt_GetFontBoundingBox(const stbtt_fontinfo * info,int * x0,int * y0,int * x1,int * y1)15328340ef62SSimon Glass STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)
15338340ef62SSimon Glass {
15348340ef62SSimon Glass    *x0 = ttSHORT(info->data + info->head + 36);
15358340ef62SSimon Glass    *y0 = ttSHORT(info->data + info->head + 38);
15368340ef62SSimon Glass    *x1 = ttSHORT(info->data + info->head + 40);
15378340ef62SSimon Glass    *y1 = ttSHORT(info->data + info->head + 42);
15388340ef62SSimon Glass }
15398340ef62SSimon Glass 
stbtt_ScaleForPixelHeight(const stbtt_fontinfo * info,float height)15408340ef62SSimon Glass STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)
15418340ef62SSimon Glass {
15428340ef62SSimon Glass    int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
15438340ef62SSimon Glass    return (float) height / fheight;
15448340ef62SSimon Glass }
15458340ef62SSimon Glass 
stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo * info,float pixels)15468340ef62SSimon Glass STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)
15478340ef62SSimon Glass {
15488340ef62SSimon Glass    int unitsPerEm = ttUSHORT(info->data + info->head + 18);
15498340ef62SSimon Glass    return pixels / unitsPerEm;
15508340ef62SSimon Glass }
15518340ef62SSimon Glass 
stbtt_FreeShape(const stbtt_fontinfo * info,stbtt_vertex * v)15528340ef62SSimon Glass STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)
15538340ef62SSimon Glass {
15548340ef62SSimon Glass    STBTT_free(v, info->userdata);
15558340ef62SSimon Glass }
15568340ef62SSimon Glass 
15578340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
15588340ef62SSimon Glass //
15598340ef62SSimon Glass // antialiasing software rasterizer
15608340ef62SSimon Glass //
15618340ef62SSimon Glass 
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)15628340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
15638340ef62SSimon Glass {
15648340ef62SSimon Glass    int x0,y0,x1,y1;
15658340ef62SSimon Glass    if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
15668340ef62SSimon Glass       // e.g. space character
15678340ef62SSimon Glass       if (ix0) *ix0 = 0;
15688340ef62SSimon Glass       if (iy0) *iy0 = 0;
15698340ef62SSimon Glass       if (ix1) *ix1 = 0;
15708340ef62SSimon Glass       if (iy1) *iy1 = 0;
15718340ef62SSimon Glass    } else {
15728340ef62SSimon Glass       // move to integral bboxes (treating pixels as little squares, what pixels get touched)?
15738340ef62SSimon Glass       if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
15748340ef62SSimon Glass       if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
15758340ef62SSimon Glass       if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
15768340ef62SSimon Glass       if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
15778340ef62SSimon Glass    }
15788340ef62SSimon Glass }
15798340ef62SSimon Glass 
stbtt_GetGlyphBitmapBox(const stbtt_fontinfo * font,int glyph,float scale_x,float scale_y,int * ix0,int * iy0,int * ix1,int * iy1)15808340ef62SSimon Glass STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
15818340ef62SSimon Glass {
15828340ef62SSimon Glass    stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
15838340ef62SSimon Glass }
15848340ef62SSimon Glass 
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)15858340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)
15868340ef62SSimon Glass {
15878340ef62SSimon Glass    stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
15888340ef62SSimon Glass }
15898340ef62SSimon Glass 
stbtt_GetCodepointBitmapBox(const stbtt_fontinfo * font,int codepoint,float scale_x,float scale_y,int * ix0,int * iy0,int * ix1,int * iy1)15908340ef62SSimon Glass STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
15918340ef62SSimon Glass {
15928340ef62SSimon Glass    stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);
15938340ef62SSimon Glass }
15948340ef62SSimon Glass 
15958340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
15968340ef62SSimon Glass //
15978340ef62SSimon Glass //  Rasterizer
15988340ef62SSimon Glass 
15998340ef62SSimon Glass typedef struct stbtt__hheap_chunk
16008340ef62SSimon Glass {
16018340ef62SSimon Glass    struct stbtt__hheap_chunk *next;
16028340ef62SSimon Glass } stbtt__hheap_chunk;
16038340ef62SSimon Glass 
16048340ef62SSimon Glass typedef struct stbtt__hheap
16058340ef62SSimon Glass {
16068340ef62SSimon Glass    struct stbtt__hheap_chunk *head;
16078340ef62SSimon Glass    void   *first_free;
16088340ef62SSimon Glass    int    num_remaining_in_head_chunk;
16098340ef62SSimon Glass } stbtt__hheap;
16108340ef62SSimon Glass 
stbtt__hheap_alloc(stbtt__hheap * hh,size_t size,void * userdata)16118340ef62SSimon Glass static void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata)
16128340ef62SSimon Glass {
16138340ef62SSimon Glass    if (hh->first_free) {
16148340ef62SSimon Glass       void *p = hh->first_free;
16158340ef62SSimon Glass       hh->first_free = * (void **) p;
16168340ef62SSimon Glass       return p;
16178340ef62SSimon Glass    } else {
16188340ef62SSimon Glass       if (hh->num_remaining_in_head_chunk == 0) {
16198340ef62SSimon Glass          int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
16208340ef62SSimon Glass          stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata);
16218340ef62SSimon Glass          if (c == NULL)
16228340ef62SSimon Glass             return NULL;
16238340ef62SSimon Glass          c->next = hh->head;
16248340ef62SSimon Glass          hh->head = c;
16258340ef62SSimon Glass          hh->num_remaining_in_head_chunk = count;
16268340ef62SSimon Glass       }
16278340ef62SSimon Glass       --hh->num_remaining_in_head_chunk;
16288340ef62SSimon Glass       return (char *) (hh->head) + size * hh->num_remaining_in_head_chunk;
16298340ef62SSimon Glass    }
16308340ef62SSimon Glass }
16318340ef62SSimon Glass 
stbtt__hheap_free(stbtt__hheap * hh,void * p)16328340ef62SSimon Glass static void stbtt__hheap_free(stbtt__hheap *hh, void *p)
16338340ef62SSimon Glass {
16348340ef62SSimon Glass    *(void **) p = hh->first_free;
16358340ef62SSimon Glass    hh->first_free = p;
16368340ef62SSimon Glass }
16378340ef62SSimon Glass 
stbtt__hheap_cleanup(stbtt__hheap * hh,void * userdata)16388340ef62SSimon Glass static void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata)
16398340ef62SSimon Glass {
16408340ef62SSimon Glass    stbtt__hheap_chunk *c = hh->head;
16418340ef62SSimon Glass    while (c) {
16428340ef62SSimon Glass       stbtt__hheap_chunk *n = c->next;
16438340ef62SSimon Glass       STBTT_free(c, userdata);
16448340ef62SSimon Glass       c = n;
16458340ef62SSimon Glass    }
16468340ef62SSimon Glass }
16478340ef62SSimon Glass 
16488340ef62SSimon Glass typedef struct stbtt__edge {
16498340ef62SSimon Glass    float x0,y0, x1,y1;
16508340ef62SSimon Glass    int invert;
16518340ef62SSimon Glass } stbtt__edge;
16528340ef62SSimon Glass 
16538340ef62SSimon Glass 
16548340ef62SSimon Glass typedef struct stbtt__active_edge
16558340ef62SSimon Glass {
16568340ef62SSimon Glass    struct stbtt__active_edge *next;
16578340ef62SSimon Glass    #if STBTT_RASTERIZER_VERSION==1
16588340ef62SSimon Glass    int x,dx;
16598340ef62SSimon Glass    float ey;
16608340ef62SSimon Glass    int direction;
16618340ef62SSimon Glass    #elif STBTT_RASTERIZER_VERSION==2
16628340ef62SSimon Glass    float fx,fdx,fdy;
16638340ef62SSimon Glass    float direction;
16648340ef62SSimon Glass    float sy;
16658340ef62SSimon Glass    float ey;
16668340ef62SSimon Glass    #else
16678340ef62SSimon Glass    #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
16688340ef62SSimon Glass    #endif
16698340ef62SSimon Glass } stbtt__active_edge;
16708340ef62SSimon Glass 
16718340ef62SSimon Glass #if STBTT_RASTERIZER_VERSION == 1
16728340ef62SSimon Glass #define STBTT_FIXSHIFT   10
16738340ef62SSimon Glass #define STBTT_FIX        (1 << STBTT_FIXSHIFT)
16748340ef62SSimon Glass #define STBTT_FIXMASK    (STBTT_FIX-1)
16758340ef62SSimon Glass 
stbtt__new_active(stbtt__hheap * hh,stbtt__edge * e,int off_x,float start_point,void * userdata)16768340ef62SSimon Glass static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)
16778340ef62SSimon Glass {
16788340ef62SSimon Glass    stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);
16798340ef62SSimon Glass    float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
16808340ef62SSimon Glass    if (!z) return z;
16818340ef62SSimon Glass 
16828340ef62SSimon Glass    // round dx down to avoid overshooting
16838340ef62SSimon Glass    if (dxdy < 0)
16848340ef62SSimon Glass       z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
16858340ef62SSimon Glass    else
16868340ef62SSimon Glass       z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
16878340ef62SSimon Glass 
16888340ef62SSimon Glass    z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0)); // use z->dx so when we offset later it's by the same amount
16898340ef62SSimon Glass    z->x -= off_x * STBTT_FIX;
16908340ef62SSimon Glass 
16918340ef62SSimon Glass    z->ey = e->y1;
16928340ef62SSimon Glass    z->next = 0;
16938340ef62SSimon Glass    z->direction = e->invert ? 1 : -1;
16948340ef62SSimon Glass    return z;
16958340ef62SSimon Glass }
16968340ef62SSimon Glass #elif STBTT_RASTERIZER_VERSION == 2
stbtt__new_active(stbtt__hheap * hh,stbtt__edge * e,int off_x,float start_point,void * userdata)16978340ef62SSimon Glass static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)
16988340ef62SSimon Glass {
16998340ef62SSimon Glass    stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);
17008340ef62SSimon Glass    float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
17018340ef62SSimon Glass    //STBTT_assert(e->y0 <= start_point);
17028340ef62SSimon Glass    if (!z) return z;
17038340ef62SSimon Glass    z->fdx = dxdy;
17048340ef62SSimon Glass    z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;
17058340ef62SSimon Glass    z->fx = e->x0 + dxdy * (start_point - e->y0);
17068340ef62SSimon Glass    z->fx -= off_x;
17078340ef62SSimon Glass    z->direction = e->invert ? 1.0f : -1.0f;
17088340ef62SSimon Glass    z->sy = e->y0;
17098340ef62SSimon Glass    z->ey = e->y1;
17108340ef62SSimon Glass    z->next = 0;
17118340ef62SSimon Glass    return z;
17128340ef62SSimon Glass }
17138340ef62SSimon Glass #else
17148340ef62SSimon Glass #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
17158340ef62SSimon Glass #endif
17168340ef62SSimon Glass 
17178340ef62SSimon Glass #if STBTT_RASTERIZER_VERSION == 1
17188340ef62SSimon Glass // note: this routine clips fills that extend off the edges... ideally this
17198340ef62SSimon Glass // wouldn't happen, but it could happen if the truetype glyph bounding boxes
17208340ef62SSimon Glass // are wrong, or if the user supplies a too-small bitmap
stbtt__fill_active_edges(unsigned char * scanline,int len,stbtt__active_edge * e,int max_weight)17218340ef62SSimon Glass static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight)
17228340ef62SSimon Glass {
17238340ef62SSimon Glass    // non-zero winding fill
17248340ef62SSimon Glass    int x0=0, w=0;
17258340ef62SSimon Glass 
17268340ef62SSimon Glass    while (e) {
17278340ef62SSimon Glass       if (w == 0) {
17288340ef62SSimon Glass          // if we're currently at zero, we need to record the edge start point
17298340ef62SSimon Glass          x0 = e->x; w += e->direction;
17308340ef62SSimon Glass       } else {
17318340ef62SSimon Glass          int x1 = e->x; w += e->direction;
17328340ef62SSimon Glass          // if we went to zero, we need to draw
17338340ef62SSimon Glass          if (w == 0) {
17348340ef62SSimon Glass             int i = x0 >> STBTT_FIXSHIFT;
17358340ef62SSimon Glass             int j = x1 >> STBTT_FIXSHIFT;
17368340ef62SSimon Glass 
17378340ef62SSimon Glass             if (i < len && j >= 0) {
17388340ef62SSimon Glass                if (i == j) {
17398340ef62SSimon Glass                   // x0,x1 are the same pixel, so compute combined coverage
17408340ef62SSimon Glass                   scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
17418340ef62SSimon Glass                } else {
17428340ef62SSimon Glass                   if (i >= 0) // add antialiasing for x0
17438340ef62SSimon Glass                      scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
17448340ef62SSimon Glass                   else
17458340ef62SSimon Glass                      i = -1; // clip
17468340ef62SSimon Glass 
17478340ef62SSimon Glass                   if (j < len) // add antialiasing for x1
17488340ef62SSimon Glass                      scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
17498340ef62SSimon Glass                   else
17508340ef62SSimon Glass                      j = len; // clip
17518340ef62SSimon Glass 
17528340ef62SSimon Glass                   for (++i; i < j; ++i) // fill pixels between x0 and x1
17538340ef62SSimon Glass                      scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
17548340ef62SSimon Glass                }
17558340ef62SSimon Glass             }
17568340ef62SSimon Glass          }
17578340ef62SSimon Glass       }
17588340ef62SSimon Glass 
17598340ef62SSimon Glass       e = e->next;
17608340ef62SSimon Glass    }
17618340ef62SSimon Glass }
17628340ef62SSimon Glass 
stbtt__rasterize_sorted_edges(stbtt__bitmap * result,stbtt__edge * e,int n,int vsubsample,int off_x,int off_y,void * userdata)17638340ef62SSimon Glass static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
17648340ef62SSimon Glass {
17658340ef62SSimon Glass    stbtt__hheap hh = { 0, 0, 0 };
17668340ef62SSimon Glass    stbtt__active_edge *active = NULL;
17678340ef62SSimon Glass    int y,j=0;
17688340ef62SSimon Glass    int max_weight = (255 / vsubsample);  // weight per vertical scanline
17698340ef62SSimon Glass    int s; // vertical subsample index
17708340ef62SSimon Glass    unsigned char scanline_data[512], *scanline;
17718340ef62SSimon Glass 
17728340ef62SSimon Glass    if (result->w > 512)
17738340ef62SSimon Glass       scanline = (unsigned char *) STBTT_malloc(result->w, userdata);
17748340ef62SSimon Glass    else
17758340ef62SSimon Glass       scanline = scanline_data;
17768340ef62SSimon Glass 
17778340ef62SSimon Glass    y = off_y * vsubsample;
17788340ef62SSimon Glass    e[n].y0 = (off_y + result->h) * (float) vsubsample + 1;
17798340ef62SSimon Glass 
17808340ef62SSimon Glass    while (j < result->h) {
17818340ef62SSimon Glass       STBTT_memset(scanline, 0, result->w);
17828340ef62SSimon Glass       for (s=0; s < vsubsample; ++s) {
17838340ef62SSimon Glass          // find center of pixel for this scanline
17848340ef62SSimon Glass          float scan_y = y + 0.5f;
17858340ef62SSimon Glass          stbtt__active_edge **step = &active;
17868340ef62SSimon Glass 
17878340ef62SSimon Glass          // update all active edges;
17888340ef62SSimon Glass          // remove all active edges that terminate before the center of this scanline
17898340ef62SSimon Glass          while (*step) {
17908340ef62SSimon Glass             stbtt__active_edge * z = *step;
17918340ef62SSimon Glass             if (z->ey <= scan_y) {
17928340ef62SSimon Glass                *step = z->next; // delete from list
17938340ef62SSimon Glass                STBTT_assert(z->direction);
17948340ef62SSimon Glass                z->direction = 0;
17958340ef62SSimon Glass                stbtt__hheap_free(&hh, z);
17968340ef62SSimon Glass             } else {
17978340ef62SSimon Glass                z->x += z->dx; // advance to position for current scanline
17988340ef62SSimon Glass                step = &((*step)->next); // advance through list
17998340ef62SSimon Glass             }
18008340ef62SSimon Glass          }
18018340ef62SSimon Glass 
18028340ef62SSimon Glass          // resort the list if needed
18038340ef62SSimon Glass          for(;;) {
18048340ef62SSimon Glass             int changed=0;
18058340ef62SSimon Glass             step = &active;
18068340ef62SSimon Glass             while (*step && (*step)->next) {
18078340ef62SSimon Glass                if ((*step)->x > (*step)->next->x) {
18088340ef62SSimon Glass                   stbtt__active_edge *t = *step;
18098340ef62SSimon Glass                   stbtt__active_edge *q = t->next;
18108340ef62SSimon Glass 
18118340ef62SSimon Glass                   t->next = q->next;
18128340ef62SSimon Glass                   q->next = t;
18138340ef62SSimon Glass                   *step = q;
18148340ef62SSimon Glass                   changed = 1;
18158340ef62SSimon Glass                }
18168340ef62SSimon Glass                step = &(*step)->next;
18178340ef62SSimon Glass             }
18188340ef62SSimon Glass             if (!changed) break;
18198340ef62SSimon Glass          }
18208340ef62SSimon Glass 
18218340ef62SSimon Glass          // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
18228340ef62SSimon Glass          while (e->y0 <= scan_y) {
18238340ef62SSimon Glass             if (e->y1 > scan_y) {
18248340ef62SSimon Glass                stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
18258340ef62SSimon Glass                // find insertion point
18268340ef62SSimon Glass                if (active == NULL)
18278340ef62SSimon Glass                   active = z;
18288340ef62SSimon Glass                else if (z->x < active->x) {
18298340ef62SSimon Glass                   // insert at front
18308340ef62SSimon Glass                   z->next = active;
18318340ef62SSimon Glass                   active = z;
18328340ef62SSimon Glass                } else {
18338340ef62SSimon Glass                   // find thing to insert AFTER
18348340ef62SSimon Glass                   stbtt__active_edge *p = active;
18358340ef62SSimon Glass                   while (p->next && p->next->x < z->x)
18368340ef62SSimon Glass                      p = p->next;
18378340ef62SSimon Glass                   // at this point, p->next->x is NOT < z->x
18388340ef62SSimon Glass                   z->next = p->next;
18398340ef62SSimon Glass                   p->next = z;
18408340ef62SSimon Glass                }
18418340ef62SSimon Glass             }
18428340ef62SSimon Glass             ++e;
18438340ef62SSimon Glass          }
18448340ef62SSimon Glass 
18458340ef62SSimon Glass          // now process all active edges in XOR fashion
18468340ef62SSimon Glass          if (active)
18478340ef62SSimon Glass             stbtt__fill_active_edges(scanline, result->w, active, max_weight);
18488340ef62SSimon Glass 
18498340ef62SSimon Glass          ++y;
18508340ef62SSimon Glass       }
18518340ef62SSimon Glass       STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
18528340ef62SSimon Glass       ++j;
18538340ef62SSimon Glass    }
18548340ef62SSimon Glass 
18558340ef62SSimon Glass    stbtt__hheap_cleanup(&hh, userdata);
18568340ef62SSimon Glass 
18578340ef62SSimon Glass    if (scanline != scanline_data)
18588340ef62SSimon Glass       STBTT_free(scanline, userdata);
18598340ef62SSimon Glass }
18608340ef62SSimon Glass 
18618340ef62SSimon Glass #elif STBTT_RASTERIZER_VERSION == 2
18628340ef62SSimon Glass 
18638340ef62SSimon Glass // the edge passed in here does not cross the vertical line at x or the vertical line at x+1
18648340ef62SSimon Glass // (i.e. it has already been clipped to those)
stbtt__handle_clipped_edge(float * scanline,int x,stbtt__active_edge * e,float x0,float y0,float x1,float y1)18658340ef62SSimon Glass static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1)
18668340ef62SSimon Glass {
18678340ef62SSimon Glass    if (y0 == y1) return;
18688340ef62SSimon Glass    STBTT_assert(y0 < y1);
18698340ef62SSimon Glass    STBTT_assert(e->sy <= e->ey);
18708340ef62SSimon Glass    if (y0 > e->ey) return;
18718340ef62SSimon Glass    if (y1 < e->sy) return;
18728340ef62SSimon Glass    if (y0 < e->sy) {
18738340ef62SSimon Glass       x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
18748340ef62SSimon Glass       y0 = e->sy;
18758340ef62SSimon Glass    }
18768340ef62SSimon Glass    if (y1 > e->ey) {
18778340ef62SSimon Glass       x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
18788340ef62SSimon Glass       y1 = e->ey;
18798340ef62SSimon Glass    }
18808340ef62SSimon Glass 
18818340ef62SSimon Glass    if (x0 == x)
18828340ef62SSimon Glass       STBTT_assert(x1 <= x+1);
18838340ef62SSimon Glass    else if (x0 == x+1)
18848340ef62SSimon Glass       STBTT_assert(x1 >= x);
18858340ef62SSimon Glass    else if (x0 <= x)
18868340ef62SSimon Glass       STBTT_assert(x1 <= x);
18878340ef62SSimon Glass    else if (x0 >= x+1)
18888340ef62SSimon Glass       STBTT_assert(x1 >= x+1);
18898340ef62SSimon Glass    else
18908340ef62SSimon Glass       STBTT_assert(x1 >= x && x1 <= x+1);
18918340ef62SSimon Glass 
18928340ef62SSimon Glass    if (x0 <= x && x1 <= x)
18938340ef62SSimon Glass       scanline[x] += e->direction * (y1-y0);
18948340ef62SSimon Glass    else if (x0 >= x+1 && x1 >= x+1)
18958340ef62SSimon Glass       ;
18968340ef62SSimon Glass    else {
18978340ef62SSimon Glass       STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
18988340ef62SSimon Glass       scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); // coverage = 1 - average x position
18998340ef62SSimon Glass    }
19008340ef62SSimon Glass }
19018340ef62SSimon Glass 
stbtt__fill_active_edges_new(float * scanline,float * scanline_fill,int len,stbtt__active_edge * e,float y_top)19028340ef62SSimon Glass static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top)
19038340ef62SSimon Glass {
19048340ef62SSimon Glass    float y_bottom = y_top+1;
19058340ef62SSimon Glass 
19068340ef62SSimon Glass    while (e) {
19078340ef62SSimon Glass       // brute force every pixel
19088340ef62SSimon Glass 
19098340ef62SSimon Glass       // compute intersection points with top & bottom
19108340ef62SSimon Glass       STBTT_assert(e->ey >= y_top);
19118340ef62SSimon Glass 
19128340ef62SSimon Glass       if (e->fdx == 0) {
19138340ef62SSimon Glass          float x0 = e->fx;
19148340ef62SSimon Glass          if (x0 < len) {
19158340ef62SSimon Glass             if (x0 >= 0) {
19168340ef62SSimon Glass                stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
19178340ef62SSimon Glass                stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
19188340ef62SSimon Glass             } else {
19198340ef62SSimon Glass                stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
19208340ef62SSimon Glass             }
19218340ef62SSimon Glass          }
19228340ef62SSimon Glass       } else {
19238340ef62SSimon Glass          float x0 = e->fx;
19248340ef62SSimon Glass          float dx = e->fdx;
19258340ef62SSimon Glass          float xb = x0 + dx;
19268340ef62SSimon Glass          float x_top, x_bottom;
19278340ef62SSimon Glass          float sy0,sy1;
19288340ef62SSimon Glass          float dy = e->fdy;
19298340ef62SSimon Glass          STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
19308340ef62SSimon Glass 
19318340ef62SSimon Glass          // compute endpoints of line segment clipped to this scanline (if the
19328340ef62SSimon Glass          // line segment starts on this scanline. x0 is the intersection of the
19338340ef62SSimon Glass          // line with y_top, but that may be off the line segment.
19348340ef62SSimon Glass          if (e->sy > y_top) {
19358340ef62SSimon Glass             x_top = x0 + dx * (e->sy - y_top);
19368340ef62SSimon Glass             sy0 = e->sy;
19378340ef62SSimon Glass          } else {
19388340ef62SSimon Glass             x_top = x0;
19398340ef62SSimon Glass             sy0 = y_top;
19408340ef62SSimon Glass          }
19418340ef62SSimon Glass          if (e->ey < y_bottom) {
19428340ef62SSimon Glass             x_bottom = x0 + dx * (e->ey - y_top);
19438340ef62SSimon Glass             sy1 = e->ey;
19448340ef62SSimon Glass          } else {
19458340ef62SSimon Glass             x_bottom = xb;
19468340ef62SSimon Glass             sy1 = y_bottom;
19478340ef62SSimon Glass          }
19488340ef62SSimon Glass 
19498340ef62SSimon Glass          if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
19508340ef62SSimon Glass             // from here on, we don't have to range check x values
19518340ef62SSimon Glass 
19528340ef62SSimon Glass             if ((int) x_top == (int) x_bottom) {
19538340ef62SSimon Glass                float height;
19548340ef62SSimon Glass                // simple case, only spans one pixel
19558340ef62SSimon Glass                int x = (int) x_top;
19568340ef62SSimon Glass                height = sy1 - sy0;
19578340ef62SSimon Glass                STBTT_assert(x >= 0 && x < len);
19588340ef62SSimon Glass                scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2)  * height;
19598340ef62SSimon Glass                scanline_fill[x] += e->direction * height; // everything right of this pixel is filled
19608340ef62SSimon Glass             } else {
19618340ef62SSimon Glass                int x,x1,x2;
19628340ef62SSimon Glass                float y_crossing, step, sign, area;
19638340ef62SSimon Glass                // covers 2+ pixels
19648340ef62SSimon Glass                if (x_top > x_bottom) {
19658340ef62SSimon Glass                   // flip scanline vertically; signed area is the same
19668340ef62SSimon Glass                   float t;
19678340ef62SSimon Glass                   sy0 = y_bottom - (sy0 - y_top);
19688340ef62SSimon Glass                   sy1 = y_bottom - (sy1 - y_top);
19698340ef62SSimon Glass                   t = sy0, sy0 = sy1, sy1 = t;
19708340ef62SSimon Glass                   t = x_bottom, x_bottom = x_top, x_top = t;
19718340ef62SSimon Glass                   dx = -dx;
19728340ef62SSimon Glass                   dy = -dy;
19738340ef62SSimon Glass                   t = x0, x0 = xb, xb = t;
19748340ef62SSimon Glass                }
19758340ef62SSimon Glass 
19768340ef62SSimon Glass                x1 = (int) x_top;
19778340ef62SSimon Glass                x2 = (int) x_bottom;
19788340ef62SSimon Glass                // compute intersection with y axis at x1+1
19798340ef62SSimon Glass                y_crossing = (x1+1 - x0) * dy + y_top;
19808340ef62SSimon Glass 
19818340ef62SSimon Glass                sign = e->direction;
19828340ef62SSimon Glass                // area of the rectangle covered from y0..y_crossing
19838340ef62SSimon Glass                area = sign * (y_crossing-sy0);
19848340ef62SSimon Glass                // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing)
19858340ef62SSimon Glass                scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);
19868340ef62SSimon Glass 
19878340ef62SSimon Glass                step = sign * dy;
19888340ef62SSimon Glass                for (x = x1+1; x < x2; ++x) {
19898340ef62SSimon Glass                   scanline[x] += area + step/2;
19908340ef62SSimon Glass                   area += step;
19918340ef62SSimon Glass                }
19928340ef62SSimon Glass                y_crossing += dy * (x2 - (x1+1));
19938340ef62SSimon Glass 
19948340ef62SSimon Glass                STBTT_assert(fabs(area) <= 1.01f);
19958340ef62SSimon Glass 
1996*348f044fSHeinrich Schuchardt                scanline[x2] += area + sign * (1-(x_bottom-x2)/2) * (sy1-y_crossing);
19978340ef62SSimon Glass 
19988340ef62SSimon Glass                scanline_fill[x2] += sign * (sy1-sy0);
19998340ef62SSimon Glass             }
20008340ef62SSimon Glass          } else {
20018340ef62SSimon Glass             // if edge goes outside of box we're drawing, we require
20028340ef62SSimon Glass             // clipping logic. since this does not match the intended use
20038340ef62SSimon Glass             // of this library, we use a different, very slow brute
20048340ef62SSimon Glass             // force implementation
20058340ef62SSimon Glass             int x;
20068340ef62SSimon Glass             for (x=0; x < len; ++x) {
20078340ef62SSimon Glass                // cases:
20088340ef62SSimon Glass                //
20098340ef62SSimon Glass                // there can be up to two intersections with the pixel. any intersection
20108340ef62SSimon Glass                // with left or right edges can be handled by splitting into two (or three)
20118340ef62SSimon Glass                // regions. intersections with top & bottom do not necessitate case-wise logic.
20128340ef62SSimon Glass                //
20138340ef62SSimon Glass                // the old way of doing this found the intersections with the left & right edges,
20148340ef62SSimon Glass                // then used some simple logic to produce up to three segments in sorted order
20158340ef62SSimon Glass                // from top-to-bottom. however, this had a problem: if an x edge was epsilon
20168340ef62SSimon Glass                // across the x border, then the corresponding y position might not be distinct
20178340ef62SSimon Glass                // from the other y segment, and it might ignored as an empty segment. to avoid
20188340ef62SSimon Glass                // that, we need to explicitly produce segments based on x positions.
20198340ef62SSimon Glass 
20208340ef62SSimon Glass                // rename variables to clear pairs
20218340ef62SSimon Glass                float y0 = y_top;
20228340ef62SSimon Glass                float x1 = (float) (x);
20238340ef62SSimon Glass                float x2 = (float) (x+1);
20248340ef62SSimon Glass                float x3 = xb;
20258340ef62SSimon Glass                float y3 = y_bottom;
20268340ef62SSimon Glass                float y1,y2;
20278340ef62SSimon Glass 
20288340ef62SSimon Glass                // x = e->x + e->dx * (y-y_top)
20298340ef62SSimon Glass                // (y-y_top) = (x - e->x) / e->dx
20308340ef62SSimon Glass                // y = (x - e->x) / e->dx + y_top
20318340ef62SSimon Glass                y1 = (x - x0) / dx + y_top;
20328340ef62SSimon Glass                y2 = (x+1 - x0) / dx + y_top;
20338340ef62SSimon Glass 
20348340ef62SSimon Glass                if (x0 < x1 && x3 > x2) {         // three segments descending down-right
20358340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
20368340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);
20378340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
20388340ef62SSimon Glass                } else if (x3 < x1 && x0 > x2) {  // three segments descending down-left
20398340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
20408340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);
20418340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
20428340ef62SSimon Glass                } else if (x0 < x1 && x3 > x1) {  // two segments across x, down-right
20438340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
20448340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
20458340ef62SSimon Glass                } else if (x3 < x1 && x0 > x1) {  // two segments across x, down-left
20468340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
20478340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
20488340ef62SSimon Glass                } else if (x0 < x2 && x3 > x2) {  // two segments across x+1, down-right
20498340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
20508340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
20518340ef62SSimon Glass                } else if (x3 < x2 && x0 > x2) {  // two segments across x+1, down-left
20528340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
20538340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
20548340ef62SSimon Glass                } else {  // one segment
20558340ef62SSimon Glass                   stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);
20568340ef62SSimon Glass                }
20578340ef62SSimon Glass             }
20588340ef62SSimon Glass          }
20598340ef62SSimon Glass       }
20608340ef62SSimon Glass       e = e->next;
20618340ef62SSimon Glass    }
20628340ef62SSimon Glass }
20638340ef62SSimon Glass 
20648340ef62SSimon Glass // directly AA rasterize edges w/o supersampling
stbtt__rasterize_sorted_edges(stbtt__bitmap * result,stbtt__edge * e,int n,int vsubsample,int off_x,int off_y,void * userdata)20658340ef62SSimon Glass static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
20668340ef62SSimon Glass {
20678340ef62SSimon Glass    stbtt__hheap hh = { 0, 0, 0 };
20688340ef62SSimon Glass    stbtt__active_edge *active = NULL;
20698340ef62SSimon Glass    int y,j=0, i;
20708340ef62SSimon Glass    float scanline_data[129], *scanline, *scanline2;
20718340ef62SSimon Glass 
20728340ef62SSimon Glass    if (result->w > 64)
20738340ef62SSimon Glass       scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata);
20748340ef62SSimon Glass    else
20758340ef62SSimon Glass       scanline = scanline_data;
20768340ef62SSimon Glass 
20778340ef62SSimon Glass    scanline2 = scanline + result->w;
20788340ef62SSimon Glass 
20798340ef62SSimon Glass    y = off_y;
20808340ef62SSimon Glass    e[n].y0 = (float) (off_y + result->h) + 1;
20818340ef62SSimon Glass 
20828340ef62SSimon Glass    while (j < result->h) {
20838340ef62SSimon Glass       // find center of pixel for this scanline
20848340ef62SSimon Glass       float scan_y_top    = y + 0.0f;
20858340ef62SSimon Glass       float scan_y_bottom = y + 1.0f;
20868340ef62SSimon Glass       stbtt__active_edge **step = &active;
20878340ef62SSimon Glass 
20888340ef62SSimon Glass       STBTT_memset(scanline , 0, result->w*sizeof(scanline[0]));
20898340ef62SSimon Glass       STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0]));
20908340ef62SSimon Glass 
20918340ef62SSimon Glass       // update all active edges;
20928340ef62SSimon Glass       // remove all active edges that terminate before the top of this scanline
20938340ef62SSimon Glass       while (*step) {
20948340ef62SSimon Glass          stbtt__active_edge * z = *step;
20958340ef62SSimon Glass          if (z->ey <= scan_y_top) {
20968340ef62SSimon Glass             *step = z->next; // delete from list
20978340ef62SSimon Glass             STBTT_assert(z->direction);
20988340ef62SSimon Glass             z->direction = 0;
20998340ef62SSimon Glass             stbtt__hheap_free(&hh, z);
21008340ef62SSimon Glass          } else {
21018340ef62SSimon Glass             step = &((*step)->next); // advance through list
21028340ef62SSimon Glass          }
21038340ef62SSimon Glass       }
21048340ef62SSimon Glass 
21058340ef62SSimon Glass       // insert all edges that start before the bottom of this scanline
21068340ef62SSimon Glass       while (e->y0 <= scan_y_bottom) {
21078340ef62SSimon Glass          if (e->y0 != e->y1) {
21088340ef62SSimon Glass             stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
21098340ef62SSimon Glass             STBTT_assert(z->ey >= scan_y_top);
21108340ef62SSimon Glass             // insert at front
21118340ef62SSimon Glass             z->next = active;
21128340ef62SSimon Glass             active = z;
21138340ef62SSimon Glass          }
21148340ef62SSimon Glass          ++e;
21158340ef62SSimon Glass       }
21168340ef62SSimon Glass 
21178340ef62SSimon Glass       // now process all active edges
21188340ef62SSimon Glass       if (active)
21198340ef62SSimon Glass          stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
21208340ef62SSimon Glass 
21218340ef62SSimon Glass       {
21228340ef62SSimon Glass          float sum = 0;
21238340ef62SSimon Glass          for (i=0; i < result->w; ++i) {
21248340ef62SSimon Glass             float k;
21258340ef62SSimon Glass             int m;
21268340ef62SSimon Glass             sum += scanline2[i];
21278340ef62SSimon Glass             k = scanline[i] + sum;
21288340ef62SSimon Glass             k = (float) STBTT_fabs(k)*255 + 0.5f;
21298340ef62SSimon Glass             m = (int) k;
21308340ef62SSimon Glass             if (m > 255) m = 255;
21318340ef62SSimon Glass             result->pixels[j*result->stride + i] = (unsigned char) m;
21328340ef62SSimon Glass          }
21338340ef62SSimon Glass       }
21348340ef62SSimon Glass       // advance all the edges
21358340ef62SSimon Glass       step = &active;
21368340ef62SSimon Glass       while (*step) {
21378340ef62SSimon Glass          stbtt__active_edge *z = *step;
21388340ef62SSimon Glass          z->fx += z->fdx; // advance to position for current scanline
21398340ef62SSimon Glass          step = &((*step)->next); // advance through list
21408340ef62SSimon Glass       }
21418340ef62SSimon Glass 
21428340ef62SSimon Glass       ++y;
21438340ef62SSimon Glass       ++j;
21448340ef62SSimon Glass    }
21458340ef62SSimon Glass 
21468340ef62SSimon Glass    stbtt__hheap_cleanup(&hh, userdata);
21478340ef62SSimon Glass 
21488340ef62SSimon Glass    if (scanline != scanline_data)
21498340ef62SSimon Glass       STBTT_free(scanline, userdata);
21508340ef62SSimon Glass }
21518340ef62SSimon Glass #else
21528340ef62SSimon Glass #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
21538340ef62SSimon Glass #endif
21548340ef62SSimon Glass 
21558340ef62SSimon Glass #define STBTT__COMPARE(a,b)  ((a)->y0 < (b)->y0)
21568340ef62SSimon Glass 
stbtt__sort_edges_ins_sort(stbtt__edge * p,int n)21578340ef62SSimon Glass static void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n)
21588340ef62SSimon Glass {
21598340ef62SSimon Glass    int i,j;
21608340ef62SSimon Glass    for (i=1; i < n; ++i) {
21618340ef62SSimon Glass       stbtt__edge t = p[i], *a = &t;
21628340ef62SSimon Glass       j = i;
21638340ef62SSimon Glass       while (j > 0) {
21648340ef62SSimon Glass          stbtt__edge *b = &p[j-1];
21658340ef62SSimon Glass          int c = STBTT__COMPARE(a,b);
21668340ef62SSimon Glass          if (!c) break;
21678340ef62SSimon Glass          p[j] = p[j-1];
21688340ef62SSimon Glass          --j;
21698340ef62SSimon Glass       }
21708340ef62SSimon Glass       if (i != j)
21718340ef62SSimon Glass          p[j] = t;
21728340ef62SSimon Glass    }
21738340ef62SSimon Glass }
21748340ef62SSimon Glass 
stbtt__sort_edges_quicksort(stbtt__edge * p,int n)21758340ef62SSimon Glass static void stbtt__sort_edges_quicksort(stbtt__edge *p, int n)
21768340ef62SSimon Glass {
21778340ef62SSimon Glass    /* threshhold for transitioning to insertion sort */
21788340ef62SSimon Glass    while (n > 12) {
21798340ef62SSimon Glass       stbtt__edge t;
21808340ef62SSimon Glass       int c01,c12,c,m,i,j;
21818340ef62SSimon Glass 
21828340ef62SSimon Glass       /* compute median of three */
21838340ef62SSimon Glass       m = n >> 1;
21848340ef62SSimon Glass       c01 = STBTT__COMPARE(&p[0],&p[m]);
21858340ef62SSimon Glass       c12 = STBTT__COMPARE(&p[m],&p[n-1]);
21868340ef62SSimon Glass       /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
21878340ef62SSimon Glass       if (c01 != c12) {
21888340ef62SSimon Glass          /* otherwise, we'll need to swap something else to middle */
21898340ef62SSimon Glass          int z;
21908340ef62SSimon Glass          c = STBTT__COMPARE(&p[0],&p[n-1]);
21918340ef62SSimon Glass          /* 0>mid && mid<n:  0>n => n; 0<n => 0 */
21928340ef62SSimon Glass          /* 0<mid && mid>n:  0>n => 0; 0<n => n */
21938340ef62SSimon Glass          z = (c == c12) ? 0 : n-1;
21948340ef62SSimon Glass          t = p[z];
21958340ef62SSimon Glass          p[z] = p[m];
21968340ef62SSimon Glass          p[m] = t;
21978340ef62SSimon Glass       }
21988340ef62SSimon Glass       /* now p[m] is the median-of-three */
21998340ef62SSimon Glass       /* swap it to the beginning so it won't move around */
22008340ef62SSimon Glass       t = p[0];
22018340ef62SSimon Glass       p[0] = p[m];
22028340ef62SSimon Glass       p[m] = t;
22038340ef62SSimon Glass 
22048340ef62SSimon Glass       /* partition loop */
22058340ef62SSimon Glass       i=1;
22068340ef62SSimon Glass       j=n-1;
22078340ef62SSimon Glass       for(;;) {
22088340ef62SSimon Glass          /* handling of equality is crucial here */
22098340ef62SSimon Glass          /* for sentinels & efficiency with duplicates */
22108340ef62SSimon Glass          for (;;++i) {
22118340ef62SSimon Glass             if (!STBTT__COMPARE(&p[i], &p[0])) break;
22128340ef62SSimon Glass          }
22138340ef62SSimon Glass          for (;;--j) {
22148340ef62SSimon Glass             if (!STBTT__COMPARE(&p[0], &p[j])) break;
22158340ef62SSimon Glass          }
22168340ef62SSimon Glass          /* make sure we haven't crossed */
22178340ef62SSimon Glass          if (i >= j) break;
22188340ef62SSimon Glass          t = p[i];
22198340ef62SSimon Glass          p[i] = p[j];
22208340ef62SSimon Glass          p[j] = t;
22218340ef62SSimon Glass 
22228340ef62SSimon Glass          ++i;
22238340ef62SSimon Glass          --j;
22248340ef62SSimon Glass       }
22258340ef62SSimon Glass       /* recurse on smaller side, iterate on larger */
22268340ef62SSimon Glass       if (j < (n-i)) {
22278340ef62SSimon Glass          stbtt__sort_edges_quicksort(p,j);
22288340ef62SSimon Glass          p = p+i;
22298340ef62SSimon Glass          n = n-i;
22308340ef62SSimon Glass       } else {
22318340ef62SSimon Glass          stbtt__sort_edges_quicksort(p+i, n-i);
22328340ef62SSimon Glass          n = j;
22338340ef62SSimon Glass       }
22348340ef62SSimon Glass    }
22358340ef62SSimon Glass }
22368340ef62SSimon Glass 
stbtt__sort_edges(stbtt__edge * p,int n)22378340ef62SSimon Glass static void stbtt__sort_edges(stbtt__edge *p, int n)
22388340ef62SSimon Glass {
22398340ef62SSimon Glass    stbtt__sort_edges_quicksort(p, n);
22408340ef62SSimon Glass    stbtt__sort_edges_ins_sort(p, n);
22418340ef62SSimon Glass }
22428340ef62SSimon Glass 
22438340ef62SSimon Glass typedef struct
22448340ef62SSimon Glass {
22458340ef62SSimon Glass    float x,y;
22468340ef62SSimon Glass } stbtt__point;
22478340ef62SSimon Glass 
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)22488340ef62SSimon Glass 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)
22498340ef62SSimon Glass {
22508340ef62SSimon Glass    float y_scale_inv = invert ? -scale_y : scale_y;
22518340ef62SSimon Glass    stbtt__edge *e;
22528340ef62SSimon Glass    int n,i,j,k,m;
22538340ef62SSimon Glass #if STBTT_RASTERIZER_VERSION == 1
22548340ef62SSimon Glass    int vsubsample = result->h < 8 ? 15 : 5;
22558340ef62SSimon Glass #elif STBTT_RASTERIZER_VERSION == 2
22568340ef62SSimon Glass    int vsubsample = 1;
22578340ef62SSimon Glass #else
22588340ef62SSimon Glass    #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
22598340ef62SSimon Glass #endif
22608340ef62SSimon Glass    // vsubsample should divide 255 evenly; otherwise we won't reach full opacity
22618340ef62SSimon Glass 
22628340ef62SSimon Glass    // now we have to blow out the windings into explicit edge lists
22638340ef62SSimon Glass    n = 0;
22648340ef62SSimon Glass    for (i=0; i < windings; ++i)
22658340ef62SSimon Glass       n += wcount[i];
22668340ef62SSimon Glass 
22678340ef62SSimon Glass    e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); // add an extra one as a sentinel
22688340ef62SSimon Glass    if (e == 0) return;
22698340ef62SSimon Glass    n = 0;
22708340ef62SSimon Glass 
22718340ef62SSimon Glass    m=0;
22728340ef62SSimon Glass    for (i=0; i < windings; ++i) {
22738340ef62SSimon Glass       stbtt__point *p = pts + m;
22748340ef62SSimon Glass       m += wcount[i];
22758340ef62SSimon Glass       j = wcount[i]-1;
22768340ef62SSimon Glass       for (k=0; k < wcount[i]; j=k++) {
22778340ef62SSimon Glass          int a=k,b=j;
22788340ef62SSimon Glass          // skip the edge if horizontal
22798340ef62SSimon Glass          if (p[j].y == p[k].y)
22808340ef62SSimon Glass             continue;
22818340ef62SSimon Glass          // add edge from j to k to the list
22828340ef62SSimon Glass          e[n].invert = 0;
22838340ef62SSimon Glass          if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
22848340ef62SSimon Glass             e[n].invert = 1;
22858340ef62SSimon Glass             a=j,b=k;
22868340ef62SSimon Glass          }
22878340ef62SSimon Glass          e[n].x0 = p[a].x * scale_x + shift_x;
22888340ef62SSimon Glass          e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
22898340ef62SSimon Glass          e[n].x1 = p[b].x * scale_x + shift_x;
22908340ef62SSimon Glass          e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
22918340ef62SSimon Glass          ++n;
22928340ef62SSimon Glass       }
22938340ef62SSimon Glass    }
22948340ef62SSimon Glass 
22958340ef62SSimon Glass    // now sort the edges by their highest point (should snap to integer, and then by x)
22968340ef62SSimon Glass    //STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare);
22978340ef62SSimon Glass    stbtt__sort_edges(e, n);
22988340ef62SSimon Glass 
22998340ef62SSimon Glass    // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule
23008340ef62SSimon Glass    stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
23018340ef62SSimon Glass 
23028340ef62SSimon Glass    STBTT_free(e, userdata);
23038340ef62SSimon Glass }
23048340ef62SSimon Glass 
stbtt__add_point(stbtt__point * points,int n,float x,float y)23058340ef62SSimon Glass static void stbtt__add_point(stbtt__point *points, int n, float x, float y)
23068340ef62SSimon Glass {
23078340ef62SSimon Glass    if (!points) return; // during first pass, it's unallocated
23088340ef62SSimon Glass    points[n].x = x;
23098340ef62SSimon Glass    points[n].y = y;
23108340ef62SSimon Glass }
23118340ef62SSimon Glass 
23128340ef62SSimon Glass // tesselate until threshhold p is happy... @TODO warped to compensate for non-linear stretching
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)23138340ef62SSimon Glass 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)
23148340ef62SSimon Glass {
23158340ef62SSimon Glass    // midpoint
23168340ef62SSimon Glass    float mx = (x0 + 2*x1 + x2)/4;
23178340ef62SSimon Glass    float my = (y0 + 2*y1 + y2)/4;
23188340ef62SSimon Glass    // versus directly drawn line
23198340ef62SSimon Glass    float dx = (x0+x2)/2 - mx;
23208340ef62SSimon Glass    float dy = (y0+y2)/2 - my;
23218340ef62SSimon Glass    if (n > 16) // 65536 segments on one curve better be enough!
23228340ef62SSimon Glass       return 1;
23238340ef62SSimon Glass    if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA
23248340ef62SSimon Glass       stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
23258340ef62SSimon Glass       stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
23268340ef62SSimon Glass    } else {
23278340ef62SSimon Glass       stbtt__add_point(points, *num_points,x2,y2);
23288340ef62SSimon Glass       *num_points = *num_points+1;
23298340ef62SSimon Glass    }
23308340ef62SSimon Glass    return 1;
23318340ef62SSimon Glass }
23328340ef62SSimon Glass 
23338340ef62SSimon Glass // returns number of contours
stbtt_FlattenCurves(stbtt_vertex * vertices,int num_verts,float objspace_flatness,int ** contour_lengths,int * num_contours,void * userdata)23348340ef62SSimon Glass static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)
23358340ef62SSimon Glass {
23368340ef62SSimon Glass    stbtt__point *points=0;
23378340ef62SSimon Glass    int num_points=0;
23388340ef62SSimon Glass 
23398340ef62SSimon Glass    float objspace_flatness_squared = objspace_flatness * objspace_flatness;
23408340ef62SSimon Glass    int i,n=0,start=0, pass;
23418340ef62SSimon Glass 
23428340ef62SSimon Glass    // count how many "moves" there are to get the contour count
23438340ef62SSimon Glass    for (i=0; i < num_verts; ++i)
23448340ef62SSimon Glass       if (vertices[i].type == STBTT_vmove)
23458340ef62SSimon Glass          ++n;
23468340ef62SSimon Glass 
23478340ef62SSimon Glass    *num_contours = n;
23488340ef62SSimon Glass    if (n == 0) return 0;
23498340ef62SSimon Glass 
23508340ef62SSimon Glass    *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata);
23518340ef62SSimon Glass 
23528340ef62SSimon Glass    if (*contour_lengths == 0) {
23538340ef62SSimon Glass       *num_contours = 0;
23548340ef62SSimon Glass       return 0;
23558340ef62SSimon Glass    }
23568340ef62SSimon Glass 
23578340ef62SSimon Glass    // make two passes through the points so we don't need to realloc
23588340ef62SSimon Glass    for (pass=0; pass < 2; ++pass) {
23598340ef62SSimon Glass       float x=0,y=0;
23608340ef62SSimon Glass       if (pass == 1) {
23618340ef62SSimon Glass          points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata);
23628340ef62SSimon Glass          if (points == NULL) goto error;
23638340ef62SSimon Glass       }
23648340ef62SSimon Glass       num_points = 0;
23658340ef62SSimon Glass       n= -1;
23668340ef62SSimon Glass       for (i=0; i < num_verts; ++i) {
23678340ef62SSimon Glass          switch (vertices[i].type) {
23688340ef62SSimon Glass             case STBTT_vmove:
23698340ef62SSimon Glass                // start the next contour
23708340ef62SSimon Glass                if (n >= 0)
23718340ef62SSimon Glass                   (*contour_lengths)[n] = num_points - start;
23728340ef62SSimon Glass                ++n;
23738340ef62SSimon Glass                start = num_points;
23748340ef62SSimon Glass 
23758340ef62SSimon Glass                x = vertices[i].x, y = vertices[i].y;
23768340ef62SSimon Glass                stbtt__add_point(points, num_points++, x,y);
23778340ef62SSimon Glass                break;
23788340ef62SSimon Glass             case STBTT_vline:
23798340ef62SSimon Glass                x = vertices[i].x, y = vertices[i].y;
23808340ef62SSimon Glass                stbtt__add_point(points, num_points++, x, y);
23818340ef62SSimon Glass                break;
23828340ef62SSimon Glass             case STBTT_vcurve:
23838340ef62SSimon Glass                stbtt__tesselate_curve(points, &num_points, x,y,
23848340ef62SSimon Glass                                         vertices[i].cx, vertices[i].cy,
23858340ef62SSimon Glass                                         vertices[i].x,  vertices[i].y,
23868340ef62SSimon Glass                                         objspace_flatness_squared, 0);
23878340ef62SSimon Glass                x = vertices[i].x, y = vertices[i].y;
23888340ef62SSimon Glass                break;
23898340ef62SSimon Glass          }
23908340ef62SSimon Glass       }
23918340ef62SSimon Glass       (*contour_lengths)[n] = num_points - start;
23928340ef62SSimon Glass    }
23938340ef62SSimon Glass 
23948340ef62SSimon Glass    return points;
23958340ef62SSimon Glass error:
23968340ef62SSimon Glass    STBTT_free(points, userdata);
23978340ef62SSimon Glass    STBTT_free(*contour_lengths, userdata);
23988340ef62SSimon Glass    *contour_lengths = 0;
23998340ef62SSimon Glass    *num_contours = 0;
24008340ef62SSimon Glass    return NULL;
24018340ef62SSimon Glass }
24028340ef62SSimon Glass 
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)24038340ef62SSimon Glass STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)
24048340ef62SSimon Glass {
24058340ef62SSimon Glass    float scale = scale_x > scale_y ? scale_y : scale_x;
24068340ef62SSimon Glass    int winding_count, *winding_lengths;
24078340ef62SSimon Glass    stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
24088340ef62SSimon Glass    if (windings) {
24098340ef62SSimon Glass       stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
24108340ef62SSimon Glass       STBTT_free(winding_lengths, userdata);
24118340ef62SSimon Glass       STBTT_free(windings, userdata);
24128340ef62SSimon Glass    }
24138340ef62SSimon Glass }
24148340ef62SSimon Glass 
stbtt_FreeBitmap(unsigned char * bitmap,void * userdata)24158340ef62SSimon Glass STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)
24168340ef62SSimon Glass {
24178340ef62SSimon Glass    STBTT_free(bitmap, userdata);
24188340ef62SSimon Glass }
24198340ef62SSimon Glass 
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)24208340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff)
24218340ef62SSimon Glass {
24228340ef62SSimon Glass    int ix0,iy0,ix1,iy1;
24238340ef62SSimon Glass    stbtt__bitmap gbm;
24248340ef62SSimon Glass    stbtt_vertex *vertices;
24258340ef62SSimon Glass    int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
24268340ef62SSimon Glass 
24278340ef62SSimon Glass    if (scale_x == 0) scale_x = scale_y;
24288340ef62SSimon Glass    if (scale_y == 0) {
2429d5b9d11cSSimon Glass       if (scale_x == 0) {
2430d5b9d11cSSimon Glass          STBTT_free(vertices, info->userdata);
2431d5b9d11cSSimon Glass          return NULL;
2432d5b9d11cSSimon Glass       }
24338340ef62SSimon Glass       scale_y = scale_x;
24348340ef62SSimon Glass    }
24358340ef62SSimon Glass 
24368340ef62SSimon Glass    stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
24378340ef62SSimon Glass 
24388340ef62SSimon Glass    // now we get the size
24398340ef62SSimon Glass    gbm.w = (ix1 - ix0);
24408340ef62SSimon Glass    gbm.h = (iy1 - iy0);
24418340ef62SSimon Glass    gbm.pixels = NULL; // in case we error
24428340ef62SSimon Glass 
24438340ef62SSimon Glass    if (width ) *width  = gbm.w;
24448340ef62SSimon Glass    if (height) *height = gbm.h;
24458340ef62SSimon Glass    if (xoff  ) *xoff   = ix0;
24468340ef62SSimon Glass    if (yoff  ) *yoff   = iy0;
24478340ef62SSimon Glass 
24488340ef62SSimon Glass    if (gbm.w && gbm.h) {
24498340ef62SSimon Glass       gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);
24508340ef62SSimon Glass       if (gbm.pixels) {
24518340ef62SSimon Glass          gbm.stride = gbm.w;
24528340ef62SSimon Glass 
24538340ef62SSimon Glass          stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
24548340ef62SSimon Glass       }
24558340ef62SSimon Glass    }
24568340ef62SSimon Glass    STBTT_free(vertices, info->userdata);
24578340ef62SSimon Glass    return gbm.pixels;
24588340ef62SSimon Glass }
24598340ef62SSimon Glass 
stbtt_GetGlyphBitmap(const stbtt_fontinfo * info,float scale_x,float scale_y,int glyph,int * width,int * height,int * xoff,int * yoff)24608340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff)
24618340ef62SSimon Glass {
24628340ef62SSimon Glass    return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
24638340ef62SSimon Glass }
24648340ef62SSimon Glass 
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)24658340ef62SSimon Glass STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph)
24668340ef62SSimon Glass {
24678340ef62SSimon Glass    int ix0,iy0;
24688340ef62SSimon Glass    stbtt_vertex *vertices;
24698340ef62SSimon Glass    int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
24708340ef62SSimon Glass    stbtt__bitmap gbm;
24718340ef62SSimon Glass 
24728340ef62SSimon Glass    stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);
24738340ef62SSimon Glass    gbm.pixels = output;
24748340ef62SSimon Glass    gbm.w = out_w;
24758340ef62SSimon Glass    gbm.h = out_h;
24768340ef62SSimon Glass    gbm.stride = out_stride;
24778340ef62SSimon Glass 
24788340ef62SSimon Glass    if (gbm.w && gbm.h)
24798340ef62SSimon Glass       stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);
24808340ef62SSimon Glass 
24818340ef62SSimon Glass    STBTT_free(vertices, info->userdata);
24828340ef62SSimon Glass }
24838340ef62SSimon Glass 
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)24848340ef62SSimon Glass STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph)
24858340ef62SSimon Glass {
24868340ef62SSimon Glass    stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);
24878340ef62SSimon Glass }
24888340ef62SSimon Glass 
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)24898340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff)
24908340ef62SSimon Glass {
24918340ef62SSimon Glass    return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
24928340ef62SSimon Glass }
24938340ef62SSimon Glass 
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)24948340ef62SSimon Glass STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint)
24958340ef62SSimon Glass {
24968340ef62SSimon Glass    stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));
24978340ef62SSimon Glass }
24988340ef62SSimon Glass 
stbtt_GetCodepointBitmap(const stbtt_fontinfo * info,float scale_x,float scale_y,int codepoint,int * width,int * height,int * xoff,int * yoff)24998340ef62SSimon Glass STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff)
25008340ef62SSimon Glass {
25018340ef62SSimon Glass    return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);
25028340ef62SSimon Glass }
25038340ef62SSimon Glass 
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)25048340ef62SSimon Glass STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint)
25058340ef62SSimon Glass {
25068340ef62SSimon Glass    stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);
25078340ef62SSimon Glass }
25088340ef62SSimon Glass 
25098340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
25108340ef62SSimon Glass //
25118340ef62SSimon Glass // bitmap baking
25128340ef62SSimon Glass //
25138340ef62SSimon Glass // This is SUPER-CRAPPY packing to keep source code small
25148340ef62SSimon Glass 
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)25158340ef62SSimon Glass STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset,  // font location (use offset=0 for plain .ttf)
25168340ef62SSimon Glass                                 float pixel_height,                     // height of font in pixels
25178340ef62SSimon Glass                                 unsigned char *pixels, int pw, int ph,  // bitmap to be filled in
25188340ef62SSimon Glass                                 int first_char, int num_chars,          // characters to bake
25198340ef62SSimon Glass                                 stbtt_bakedchar *chardata)
25208340ef62SSimon Glass {
25218340ef62SSimon Glass    float scale;
25228340ef62SSimon Glass    int x,y,bottom_y, i;
25238340ef62SSimon Glass    stbtt_fontinfo f;
25248340ef62SSimon Glass    if (!stbtt_InitFont(&f, data, offset))
25258340ef62SSimon Glass       return -1;
25268340ef62SSimon Glass    STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels
25278340ef62SSimon Glass    x=y=1;
25288340ef62SSimon Glass    bottom_y = 1;
25298340ef62SSimon Glass 
25308340ef62SSimon Glass    scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
25318340ef62SSimon Glass 
25328340ef62SSimon Glass    for (i=0; i < num_chars; ++i) {
25338340ef62SSimon Glass       int advance, lsb, x0,y0,x1,y1,gw,gh;
25348340ef62SSimon Glass       int g = stbtt_FindGlyphIndex(&f, first_char + i);
25358340ef62SSimon Glass       stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
25368340ef62SSimon Glass       stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
25378340ef62SSimon Glass       gw = x1-x0;
25388340ef62SSimon Glass       gh = y1-y0;
25398340ef62SSimon Glass       if (x + gw + 1 >= pw)
25408340ef62SSimon Glass          y = bottom_y, x = 1; // advance to next row
25418340ef62SSimon Glass       if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row
25428340ef62SSimon Glass          return -i;
25438340ef62SSimon Glass       STBTT_assert(x+gw < pw);
25448340ef62SSimon Glass       STBTT_assert(y+gh < ph);
25458340ef62SSimon Glass       stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
25468340ef62SSimon Glass       chardata[i].x0 = (stbtt_int16) x;
25478340ef62SSimon Glass       chardata[i].y0 = (stbtt_int16) y;
25488340ef62SSimon Glass       chardata[i].x1 = (stbtt_int16) (x + gw);
25498340ef62SSimon Glass       chardata[i].y1 = (stbtt_int16) (y + gh);
25508340ef62SSimon Glass       chardata[i].xadvance = scale * advance;
25518340ef62SSimon Glass       chardata[i].xoff     = (float) x0;
25528340ef62SSimon Glass       chardata[i].yoff     = (float) y0;
25538340ef62SSimon Glass       x = x + gw + 1;
25548340ef62SSimon Glass       if (y+gh+1 > bottom_y)
25558340ef62SSimon Glass          bottom_y = y+gh+1;
25568340ef62SSimon Glass    }
25578340ef62SSimon Glass    return bottom_y;
25588340ef62SSimon Glass }
25598340ef62SSimon Glass 
stbtt_GetBakedQuad(stbtt_bakedchar * chardata,int pw,int ph,int char_index,float * xpos,float * ypos,stbtt_aligned_quad * q,int opengl_fillrule)25608340ef62SSimon Glass STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)
25618340ef62SSimon Glass {
25628340ef62SSimon Glass    float d3d_bias = opengl_fillrule ? 0 : -0.5f;
25638340ef62SSimon Glass    float ipw = 1.0f / pw, iph = 1.0f / ph;
25648340ef62SSimon Glass    stbtt_bakedchar *b = chardata + char_index;
25658340ef62SSimon Glass    int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
25668340ef62SSimon Glass    int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
25678340ef62SSimon Glass 
25688340ef62SSimon Glass    q->x0 = round_x + d3d_bias;
25698340ef62SSimon Glass    q->y0 = round_y + d3d_bias;
25708340ef62SSimon Glass    q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
25718340ef62SSimon Glass    q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
25728340ef62SSimon Glass 
25738340ef62SSimon Glass    q->s0 = b->x0 * ipw;
25748340ef62SSimon Glass    q->t0 = b->y0 * iph;
25758340ef62SSimon Glass    q->s1 = b->x1 * ipw;
25768340ef62SSimon Glass    q->t1 = b->y1 * iph;
25778340ef62SSimon Glass 
25788340ef62SSimon Glass    *xpos += b->xadvance;
25798340ef62SSimon Glass }
25808340ef62SSimon Glass 
25818340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
25828340ef62SSimon Glass //
25838340ef62SSimon Glass // rectangle packing replacement routines if you don't have stb_rect_pack.h
25848340ef62SSimon Glass //
25858340ef62SSimon Glass 
25868340ef62SSimon Glass #ifndef STB_RECT_PACK_VERSION
25878340ef62SSimon Glass #ifdef _MSC_VER
25888340ef62SSimon Glass #define STBTT__NOTUSED(v)  (void)(v)
25898340ef62SSimon Glass #else
25908340ef62SSimon Glass #define STBTT__NOTUSED(v)  (void)sizeof(v)
25918340ef62SSimon Glass #endif
25928340ef62SSimon Glass 
25938340ef62SSimon Glass typedef int stbrp_coord;
25948340ef62SSimon Glass 
25958340ef62SSimon Glass ////////////////////////////////////////////////////////////////////////////////////
25968340ef62SSimon Glass //                                                                                //
25978340ef62SSimon Glass //                                                                                //
25988340ef62SSimon Glass // COMPILER WARNING ?!?!?                                                         //
25998340ef62SSimon Glass //                                                                                //
26008340ef62SSimon Glass //                                                                                //
26018340ef62SSimon Glass // if you get a compile warning due to these symbols being defined more than      //
26028340ef62SSimon Glass // once, move #include "stb_rect_pack.h" before #include "stb_truetype.h"         //
26038340ef62SSimon Glass //                                                                                //
26048340ef62SSimon Glass ////////////////////////////////////////////////////////////////////////////////////
26058340ef62SSimon Glass 
26068340ef62SSimon Glass typedef struct
26078340ef62SSimon Glass {
26088340ef62SSimon Glass    int width,height;
26098340ef62SSimon Glass    int x,y,bottom_y;
26108340ef62SSimon Glass } stbrp_context;
26118340ef62SSimon Glass 
26128340ef62SSimon Glass typedef struct
26138340ef62SSimon Glass {
26148340ef62SSimon Glass    unsigned char x;
26158340ef62SSimon Glass } stbrp_node;
26168340ef62SSimon Glass 
26178340ef62SSimon Glass struct stbrp_rect
26188340ef62SSimon Glass {
26198340ef62SSimon Glass    stbrp_coord x,y;
26208340ef62SSimon Glass    int id,w,h,was_packed;
26218340ef62SSimon Glass };
26228340ef62SSimon Glass 
stbrp_init_target(stbrp_context * con,int pw,int ph,stbrp_node * nodes,int num_nodes)26238340ef62SSimon Glass static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes)
26248340ef62SSimon Glass {
26258340ef62SSimon Glass    con->width  = pw;
26268340ef62SSimon Glass    con->height = ph;
26278340ef62SSimon Glass    con->x = 0;
26288340ef62SSimon Glass    con->y = 0;
26298340ef62SSimon Glass    con->bottom_y = 0;
26308340ef62SSimon Glass    STBTT__NOTUSED(nodes);
26318340ef62SSimon Glass    STBTT__NOTUSED(num_nodes);
26328340ef62SSimon Glass }
26338340ef62SSimon Glass 
stbrp_pack_rects(stbrp_context * con,stbrp_rect * rects,int num_rects)26348340ef62SSimon Glass static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects)
26358340ef62SSimon Glass {
26368340ef62SSimon Glass    int i;
26378340ef62SSimon Glass    for (i=0; i < num_rects; ++i) {
26388340ef62SSimon Glass       if (con->x + rects[i].w > con->width) {
26398340ef62SSimon Glass          con->x = 0;
26408340ef62SSimon Glass          con->y = con->bottom_y;
26418340ef62SSimon Glass       }
26428340ef62SSimon Glass       if (con->y + rects[i].h > con->height)
26438340ef62SSimon Glass          break;
26448340ef62SSimon Glass       rects[i].x = con->x;
26458340ef62SSimon Glass       rects[i].y = con->y;
26468340ef62SSimon Glass       rects[i].was_packed = 1;
26478340ef62SSimon Glass       con->x += rects[i].w;
26488340ef62SSimon Glass       if (con->y + rects[i].h > con->bottom_y)
26498340ef62SSimon Glass          con->bottom_y = con->y + rects[i].h;
26508340ef62SSimon Glass    }
26518340ef62SSimon Glass    for (   ; i < num_rects; ++i)
26528340ef62SSimon Glass       rects[i].was_packed = 0;
26538340ef62SSimon Glass }
26548340ef62SSimon Glass #endif
26558340ef62SSimon Glass 
26568340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
26578340ef62SSimon Glass //
26588340ef62SSimon Glass // bitmap baking
26598340ef62SSimon Glass //
26608340ef62SSimon Glass // This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If
26618340ef62SSimon Glass // stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.
26628340ef62SSimon Glass 
stbtt_PackBegin(stbtt_pack_context * spc,unsigned char * pixels,int pw,int ph,int stride_in_bytes,int padding,void * alloc_context)26638340ef62SSimon Glass STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)
26648340ef62SSimon Glass {
26658340ef62SSimon Glass    stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context)            ,alloc_context);
26668340ef62SSimon Glass    int            num_nodes = pw - padding;
26678340ef62SSimon Glass    stbrp_node    *nodes   = (stbrp_node    *) STBTT_malloc(sizeof(*nodes  ) * num_nodes,alloc_context);
26688340ef62SSimon Glass 
26698340ef62SSimon Glass    if (context == NULL || nodes == NULL) {
26708340ef62SSimon Glass       if (context != NULL) STBTT_free(context, alloc_context);
26718340ef62SSimon Glass       if (nodes   != NULL) STBTT_free(nodes  , alloc_context);
26728340ef62SSimon Glass       return 0;
26738340ef62SSimon Glass    }
26748340ef62SSimon Glass 
26758340ef62SSimon Glass    spc->user_allocator_context = alloc_context;
26768340ef62SSimon Glass    spc->width = pw;
26778340ef62SSimon Glass    spc->height = ph;
26788340ef62SSimon Glass    spc->pixels = pixels;
26798340ef62SSimon Glass    spc->pack_info = context;
26808340ef62SSimon Glass    spc->nodes = nodes;
26818340ef62SSimon Glass    spc->padding = padding;
26828340ef62SSimon Glass    spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
26838340ef62SSimon Glass    spc->h_oversample = 1;
26848340ef62SSimon Glass    spc->v_oversample = 1;
26858340ef62SSimon Glass 
26868340ef62SSimon Glass    stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
26878340ef62SSimon Glass 
26888340ef62SSimon Glass    if (pixels)
26898340ef62SSimon Glass       STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels
26908340ef62SSimon Glass 
26918340ef62SSimon Glass    return 1;
26928340ef62SSimon Glass }
26938340ef62SSimon Glass 
stbtt_PackEnd(stbtt_pack_context * spc)26948340ef62SSimon Glass STBTT_DEF void stbtt_PackEnd  (stbtt_pack_context *spc)
26958340ef62SSimon Glass {
26968340ef62SSimon Glass    STBTT_free(spc->nodes    , spc->user_allocator_context);
26978340ef62SSimon Glass    STBTT_free(spc->pack_info, spc->user_allocator_context);
26988340ef62SSimon Glass }
26998340ef62SSimon Glass 
stbtt_PackSetOversampling(stbtt_pack_context * spc,unsigned int h_oversample,unsigned int v_oversample)27008340ef62SSimon Glass STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)
27018340ef62SSimon Glass {
27028340ef62SSimon Glass    STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
27038340ef62SSimon Glass    STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
27048340ef62SSimon Glass    if (h_oversample <= STBTT_MAX_OVERSAMPLE)
27058340ef62SSimon Glass       spc->h_oversample = h_oversample;
27068340ef62SSimon Glass    if (v_oversample <= STBTT_MAX_OVERSAMPLE)
27078340ef62SSimon Glass       spc->v_oversample = v_oversample;
27088340ef62SSimon Glass }
27098340ef62SSimon Glass 
27108340ef62SSimon Glass #define STBTT__OVER_MASK  (STBTT_MAX_OVERSAMPLE-1)
27118340ef62SSimon Glass 
stbtt__h_prefilter(unsigned char * pixels,int w,int h,int stride_in_bytes,unsigned int kernel_width)27128340ef62SSimon Glass static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
27138340ef62SSimon Glass {
27148340ef62SSimon Glass    unsigned char buffer[STBTT_MAX_OVERSAMPLE];
27158340ef62SSimon Glass    int safe_w = w - kernel_width;
27168340ef62SSimon Glass    int j;
27178340ef62SSimon Glass    for (j=0; j < h; ++j) {
27188340ef62SSimon Glass       int i;
27198340ef62SSimon Glass       unsigned int total;
27208340ef62SSimon Glass       STBTT_memset(buffer, 0, kernel_width);
27218340ef62SSimon Glass 
27228340ef62SSimon Glass       total = 0;
27238340ef62SSimon Glass 
27248340ef62SSimon Glass       // make kernel_width a constant in common cases so compiler can optimize out the divide
27258340ef62SSimon Glass       switch (kernel_width) {
27268340ef62SSimon Glass          case 2:
27278340ef62SSimon Glass             for (i=0; i <= safe_w; ++i) {
27288340ef62SSimon Glass                total += pixels[i] - buffer[i & STBTT__OVER_MASK];
27298340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
27308340ef62SSimon Glass                pixels[i] = (unsigned char) (total / 2);
27318340ef62SSimon Glass             }
27328340ef62SSimon Glass             break;
27338340ef62SSimon Glass          case 3:
27348340ef62SSimon Glass             for (i=0; i <= safe_w; ++i) {
27358340ef62SSimon Glass                total += pixels[i] - buffer[i & STBTT__OVER_MASK];
27368340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
27378340ef62SSimon Glass                pixels[i] = (unsigned char) (total / 3);
27388340ef62SSimon Glass             }
27398340ef62SSimon Glass             break;
27408340ef62SSimon Glass          case 4:
27418340ef62SSimon Glass             for (i=0; i <= safe_w; ++i) {
27428340ef62SSimon Glass                total += pixels[i] - buffer[i & STBTT__OVER_MASK];
27438340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
27448340ef62SSimon Glass                pixels[i] = (unsigned char) (total / 4);
27458340ef62SSimon Glass             }
27468340ef62SSimon Glass             break;
27478340ef62SSimon Glass          case 5:
27488340ef62SSimon Glass             for (i=0; i <= safe_w; ++i) {
27498340ef62SSimon Glass                total += pixels[i] - buffer[i & STBTT__OVER_MASK];
27508340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
27518340ef62SSimon Glass                pixels[i] = (unsigned char) (total / 5);
27528340ef62SSimon Glass             }
27538340ef62SSimon Glass             break;
27548340ef62SSimon Glass          default:
27558340ef62SSimon Glass             for (i=0; i <= safe_w; ++i) {
27568340ef62SSimon Glass                total += pixels[i] - buffer[i & STBTT__OVER_MASK];
27578340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
27588340ef62SSimon Glass                pixels[i] = (unsigned char) (total / kernel_width);
27598340ef62SSimon Glass             }
27608340ef62SSimon Glass             break;
27618340ef62SSimon Glass       }
27628340ef62SSimon Glass 
27638340ef62SSimon Glass       for (; i < w; ++i) {
27648340ef62SSimon Glass          STBTT_assert(pixels[i] == 0);
27658340ef62SSimon Glass          total -= buffer[i & STBTT__OVER_MASK];
27668340ef62SSimon Glass          pixels[i] = (unsigned char) (total / kernel_width);
27678340ef62SSimon Glass       }
27688340ef62SSimon Glass 
27698340ef62SSimon Glass       pixels += stride_in_bytes;
27708340ef62SSimon Glass    }
27718340ef62SSimon Glass }
27728340ef62SSimon Glass 
stbtt__v_prefilter(unsigned char * pixels,int w,int h,int stride_in_bytes,unsigned int kernel_width)27738340ef62SSimon Glass static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
27748340ef62SSimon Glass {
27758340ef62SSimon Glass    unsigned char buffer[STBTT_MAX_OVERSAMPLE];
27768340ef62SSimon Glass    int safe_h = h - kernel_width;
27778340ef62SSimon Glass    int j;
27788340ef62SSimon Glass    for (j=0; j < w; ++j) {
27798340ef62SSimon Glass       int i;
27808340ef62SSimon Glass       unsigned int total;
27818340ef62SSimon Glass       STBTT_memset(buffer, 0, kernel_width);
27828340ef62SSimon Glass 
27838340ef62SSimon Glass       total = 0;
27848340ef62SSimon Glass 
27858340ef62SSimon Glass       // make kernel_width a constant in common cases so compiler can optimize out the divide
27868340ef62SSimon Glass       switch (kernel_width) {
27878340ef62SSimon Glass          case 2:
27888340ef62SSimon Glass             for (i=0; i <= safe_h; ++i) {
27898340ef62SSimon Glass                total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
27908340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
27918340ef62SSimon Glass                pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
27928340ef62SSimon Glass             }
27938340ef62SSimon Glass             break;
27948340ef62SSimon Glass          case 3:
27958340ef62SSimon Glass             for (i=0; i <= safe_h; ++i) {
27968340ef62SSimon Glass                total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
27978340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
27988340ef62SSimon Glass                pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
27998340ef62SSimon Glass             }
28008340ef62SSimon Glass             break;
28018340ef62SSimon Glass          case 4:
28028340ef62SSimon Glass             for (i=0; i <= safe_h; ++i) {
28038340ef62SSimon Glass                total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
28048340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
28058340ef62SSimon Glass                pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
28068340ef62SSimon Glass             }
28078340ef62SSimon Glass             break;
28088340ef62SSimon Glass          case 5:
28098340ef62SSimon Glass             for (i=0; i <= safe_h; ++i) {
28108340ef62SSimon Glass                total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
28118340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
28128340ef62SSimon Glass                pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
28138340ef62SSimon Glass             }
28148340ef62SSimon Glass             break;
28158340ef62SSimon Glass          default:
28168340ef62SSimon Glass             for (i=0; i <= safe_h; ++i) {
28178340ef62SSimon Glass                total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
28188340ef62SSimon Glass                buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
28198340ef62SSimon Glass                pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
28208340ef62SSimon Glass             }
28218340ef62SSimon Glass             break;
28228340ef62SSimon Glass       }
28238340ef62SSimon Glass 
28248340ef62SSimon Glass       for (; i < h; ++i) {
28258340ef62SSimon Glass          STBTT_assert(pixels[i*stride_in_bytes] == 0);
28268340ef62SSimon Glass          total -= buffer[i & STBTT__OVER_MASK];
28278340ef62SSimon Glass          pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
28288340ef62SSimon Glass       }
28298340ef62SSimon Glass 
28308340ef62SSimon Glass       pixels += 1;
28318340ef62SSimon Glass    }
28328340ef62SSimon Glass }
28338340ef62SSimon Glass 
stbtt__oversample_shift(int oversample)28348340ef62SSimon Glass static float stbtt__oversample_shift(int oversample)
28358340ef62SSimon Glass {
28368340ef62SSimon Glass    if (!oversample)
28378340ef62SSimon Glass       return 0.0f;
28388340ef62SSimon Glass 
28398340ef62SSimon Glass    // The prefilter is a box filter of width "oversample",
28408340ef62SSimon Glass    // which shifts phase by (oversample - 1)/2 pixels in
28418340ef62SSimon Glass    // oversampled space. We want to shift in the opposite
28428340ef62SSimon Glass    // direction to counter this.
28438340ef62SSimon Glass    return (float)-(oversample - 1) / (2.0f * (float)oversample);
28448340ef62SSimon Glass }
28458340ef62SSimon Glass 
28468340ef62SSimon Glass // rects array must be big enough to accommodate all characters in the given ranges
stbtt_PackFontRangesGatherRects(stbtt_pack_context * spc,stbtt_fontinfo * info,stbtt_pack_range * ranges,int num_ranges,stbrp_rect * rects)28478340ef62SSimon Glass STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
28488340ef62SSimon Glass {
28498340ef62SSimon Glass    int i,j,k;
28508340ef62SSimon Glass 
28518340ef62SSimon Glass    k=0;
28528340ef62SSimon Glass    for (i=0; i < num_ranges; ++i) {
28538340ef62SSimon Glass       float fh = ranges[i].font_size;
28548340ef62SSimon Glass       float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
28558340ef62SSimon Glass       ranges[i].h_oversample = (unsigned char) spc->h_oversample;
28568340ef62SSimon Glass       ranges[i].v_oversample = (unsigned char) spc->v_oversample;
28578340ef62SSimon Glass       for (j=0; j < ranges[i].num_chars; ++j) {
28588340ef62SSimon Glass          int x0,y0,x1,y1;
28598340ef62SSimon Glass          int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
28608340ef62SSimon Glass          int glyph = stbtt_FindGlyphIndex(info, codepoint);
28618340ef62SSimon Glass          stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,
28628340ef62SSimon Glass                                          scale * spc->h_oversample,
28638340ef62SSimon Glass                                          scale * spc->v_oversample,
28648340ef62SSimon Glass                                          0,0,
28658340ef62SSimon Glass                                          &x0,&y0,&x1,&y1);
28668340ef62SSimon Glass          rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);
28678340ef62SSimon Glass          rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);
28688340ef62SSimon Glass          ++k;
28698340ef62SSimon Glass       }
28708340ef62SSimon Glass    }
28718340ef62SSimon Glass 
28728340ef62SSimon Glass    return k;
28738340ef62SSimon Glass }
28748340ef62SSimon Glass 
28758340ef62SSimon Glass // rects array must be big enough to accommodate all characters in the given ranges
stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context * spc,stbtt_fontinfo * info,stbtt_pack_range * ranges,int num_ranges,stbrp_rect * rects)28768340ef62SSimon Glass STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
28778340ef62SSimon Glass {
28788340ef62SSimon Glass    int i,j,k, return_value = 1;
28798340ef62SSimon Glass 
28808340ef62SSimon Glass    // save current values
28818340ef62SSimon Glass    int old_h_over = spc->h_oversample;
28828340ef62SSimon Glass    int old_v_over = spc->v_oversample;
28838340ef62SSimon Glass 
28848340ef62SSimon Glass    k = 0;
28858340ef62SSimon Glass    for (i=0; i < num_ranges; ++i) {
28868340ef62SSimon Glass       float fh = ranges[i].font_size;
28878340ef62SSimon Glass       float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
28888340ef62SSimon Glass       float recip_h,recip_v,sub_x,sub_y;
28898340ef62SSimon Glass       spc->h_oversample = ranges[i].h_oversample;
28908340ef62SSimon Glass       spc->v_oversample = ranges[i].v_oversample;
28918340ef62SSimon Glass       recip_h = 1.0f / spc->h_oversample;
28928340ef62SSimon Glass       recip_v = 1.0f / spc->v_oversample;
28938340ef62SSimon Glass       sub_x = stbtt__oversample_shift(spc->h_oversample);
28948340ef62SSimon Glass       sub_y = stbtt__oversample_shift(spc->v_oversample);
28958340ef62SSimon Glass       for (j=0; j < ranges[i].num_chars; ++j) {
28968340ef62SSimon Glass          stbrp_rect *r = &rects[k];
28978340ef62SSimon Glass          if (r->was_packed) {
28988340ef62SSimon Glass             stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];
28998340ef62SSimon Glass             int advance, lsb, x0,y0,x1,y1;
29008340ef62SSimon Glass             int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
29018340ef62SSimon Glass             int glyph = stbtt_FindGlyphIndex(info, codepoint);
29028340ef62SSimon Glass             stbrp_coord pad = (stbrp_coord) spc->padding;
29038340ef62SSimon Glass 
29048340ef62SSimon Glass             // pad on left and top
29058340ef62SSimon Glass             r->x += pad;
29068340ef62SSimon Glass             r->y += pad;
29078340ef62SSimon Glass             r->w -= pad;
29088340ef62SSimon Glass             r->h -= pad;
29098340ef62SSimon Glass             stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
29108340ef62SSimon Glass             stbtt_GetGlyphBitmapBox(info, glyph,
29118340ef62SSimon Glass                                     scale * spc->h_oversample,
29128340ef62SSimon Glass                                     scale * spc->v_oversample,
29138340ef62SSimon Glass                                     &x0,&y0,&x1,&y1);
29148340ef62SSimon Glass             stbtt_MakeGlyphBitmapSubpixel(info,
29158340ef62SSimon Glass                                           spc->pixels + r->x + r->y*spc->stride_in_bytes,
29168340ef62SSimon Glass                                           r->w - spc->h_oversample+1,
29178340ef62SSimon Glass                                           r->h - spc->v_oversample+1,
29188340ef62SSimon Glass                                           spc->stride_in_bytes,
29198340ef62SSimon Glass                                           scale * spc->h_oversample,
29208340ef62SSimon Glass                                           scale * spc->v_oversample,
29218340ef62SSimon Glass                                           0,0,
29228340ef62SSimon Glass                                           glyph);
29238340ef62SSimon Glass 
29248340ef62SSimon Glass             if (spc->h_oversample > 1)
29258340ef62SSimon Glass                stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
29268340ef62SSimon Glass                                   r->w, r->h, spc->stride_in_bytes,
29278340ef62SSimon Glass                                   spc->h_oversample);
29288340ef62SSimon Glass 
29298340ef62SSimon Glass             if (spc->v_oversample > 1)
29308340ef62SSimon Glass                stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
29318340ef62SSimon Glass                                   r->w, r->h, spc->stride_in_bytes,
29328340ef62SSimon Glass                                   spc->v_oversample);
29338340ef62SSimon Glass 
29348340ef62SSimon Glass             bc->x0       = (stbtt_int16)  r->x;
29358340ef62SSimon Glass             bc->y0       = (stbtt_int16)  r->y;
29368340ef62SSimon Glass             bc->x1       = (stbtt_int16) (r->x + r->w);
29378340ef62SSimon Glass             bc->y1       = (stbtt_int16) (r->y + r->h);
29388340ef62SSimon Glass             bc->xadvance =                scale * advance;
29398340ef62SSimon Glass             bc->xoff     =       (float)  x0 * recip_h + sub_x;
29408340ef62SSimon Glass             bc->yoff     =       (float)  y0 * recip_v + sub_y;
29418340ef62SSimon Glass             bc->xoff2    =                (x0 + r->w) * recip_h + sub_x;
29428340ef62SSimon Glass             bc->yoff2    =                (y0 + r->h) * recip_v + sub_y;
29438340ef62SSimon Glass          } else {
29448340ef62SSimon Glass             return_value = 0; // if any fail, report failure
29458340ef62SSimon Glass          }
29468340ef62SSimon Glass 
29478340ef62SSimon Glass          ++k;
29488340ef62SSimon Glass       }
29498340ef62SSimon Glass    }
29508340ef62SSimon Glass 
29518340ef62SSimon Glass    // restore original values
29528340ef62SSimon Glass    spc->h_oversample = old_h_over;
29538340ef62SSimon Glass    spc->v_oversample = old_v_over;
29548340ef62SSimon Glass 
29558340ef62SSimon Glass    return return_value;
29568340ef62SSimon Glass }
29578340ef62SSimon Glass 
stbtt_PackFontRangesPackRects(stbtt_pack_context * spc,stbrp_rect * rects,int num_rects)29588340ef62SSimon Glass STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects)
29598340ef62SSimon Glass {
29608340ef62SSimon Glass    stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects);
29618340ef62SSimon Glass }
29628340ef62SSimon Glass 
stbtt_PackFontRanges(stbtt_pack_context * spc,unsigned char * fontdata,int font_index,stbtt_pack_range * ranges,int num_ranges)29638340ef62SSimon Glass STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
29648340ef62SSimon Glass {
29658340ef62SSimon Glass    stbtt_fontinfo info;
29668340ef62SSimon Glass    int i,j,n, return_value = 1;
29678340ef62SSimon Glass    //stbrp_context *context = (stbrp_context *) spc->pack_info;
29688340ef62SSimon Glass    stbrp_rect    *rects;
29698340ef62SSimon Glass 
29708340ef62SSimon Glass    // flag all characters as NOT packed
29718340ef62SSimon Glass    for (i=0; i < num_ranges; ++i)
29728340ef62SSimon Glass       for (j=0; j < ranges[i].num_chars; ++j)
29738340ef62SSimon Glass          ranges[i].chardata_for_range[j].x0 =
29748340ef62SSimon Glass          ranges[i].chardata_for_range[j].y0 =
29758340ef62SSimon Glass          ranges[i].chardata_for_range[j].x1 =
29768340ef62SSimon Glass          ranges[i].chardata_for_range[j].y1 = 0;
29778340ef62SSimon Glass 
29788340ef62SSimon Glass    n = 0;
29798340ef62SSimon Glass    for (i=0; i < num_ranges; ++i)
29808340ef62SSimon Glass       n += ranges[i].num_chars;
29818340ef62SSimon Glass 
29828340ef62SSimon Glass    rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);
29838340ef62SSimon Glass    if (rects == NULL)
29848340ef62SSimon Glass       return 0;
29858340ef62SSimon Glass 
29868340ef62SSimon Glass    stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));
29878340ef62SSimon Glass 
29888340ef62SSimon Glass    n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
29898340ef62SSimon Glass 
29908340ef62SSimon Glass    stbtt_PackFontRangesPackRects(spc, rects, n);
29918340ef62SSimon Glass 
29928340ef62SSimon Glass    return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
29938340ef62SSimon Glass 
29948340ef62SSimon Glass    STBTT_free(rects, spc->user_allocator_context);
29958340ef62SSimon Glass    return return_value;
29968340ef62SSimon Glass }
29978340ef62SSimon Glass 
stbtt_PackFontRange(stbtt_pack_context * spc,unsigned char * fontdata,int font_index,float font_size,int first_unicode_codepoint_in_range,int num_chars_in_range,stbtt_packedchar * chardata_for_range)29988340ef62SSimon Glass STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,
29998340ef62SSimon Glass             int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)
30008340ef62SSimon Glass {
30018340ef62SSimon Glass    stbtt_pack_range range;
30028340ef62SSimon Glass    range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
30038340ef62SSimon Glass    range.array_of_unicode_codepoints = NULL;
30048340ef62SSimon Glass    range.num_chars                   = num_chars_in_range;
30058340ef62SSimon Glass    range.chardata_for_range          = chardata_for_range;
30068340ef62SSimon Glass    range.font_size                   = font_size;
30078340ef62SSimon Glass    return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
30088340ef62SSimon Glass }
30098340ef62SSimon Glass 
stbtt_GetPackedQuad(stbtt_packedchar * chardata,int pw,int ph,int char_index,float * xpos,float * ypos,stbtt_aligned_quad * q,int align_to_integer)30108340ef62SSimon Glass STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)
30118340ef62SSimon Glass {
30128340ef62SSimon Glass    float ipw = 1.0f / pw, iph = 1.0f / ph;
30138340ef62SSimon Glass    stbtt_packedchar *b = chardata + char_index;
30148340ef62SSimon Glass 
30158340ef62SSimon Glass    if (align_to_integer) {
30168340ef62SSimon Glass       float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f);
30178340ef62SSimon Glass       float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f);
30188340ef62SSimon Glass       q->x0 = x;
30198340ef62SSimon Glass       q->y0 = y;
30208340ef62SSimon Glass       q->x1 = x + b->xoff2 - b->xoff;
30218340ef62SSimon Glass       q->y1 = y + b->yoff2 - b->yoff;
30228340ef62SSimon Glass    } else {
30238340ef62SSimon Glass       q->x0 = *xpos + b->xoff;
30248340ef62SSimon Glass       q->y0 = *ypos + b->yoff;
30258340ef62SSimon Glass       q->x1 = *xpos + b->xoff2;
30268340ef62SSimon Glass       q->y1 = *ypos + b->yoff2;
30278340ef62SSimon Glass    }
30288340ef62SSimon Glass 
30298340ef62SSimon Glass    q->s0 = b->x0 * ipw;
30308340ef62SSimon Glass    q->t0 = b->y0 * iph;
30318340ef62SSimon Glass    q->s1 = b->x1 * ipw;
30328340ef62SSimon Glass    q->t1 = b->y1 * iph;
30338340ef62SSimon Glass 
30348340ef62SSimon Glass    *xpos += b->xadvance;
30358340ef62SSimon Glass }
30368340ef62SSimon Glass 
30378340ef62SSimon Glass 
30388340ef62SSimon Glass //////////////////////////////////////////////////////////////////////////////
30398340ef62SSimon Glass //
30408340ef62SSimon Glass // font name matching -- recommended not to use this
30418340ef62SSimon Glass //
30428340ef62SSimon Glass 
30438340ef62SSimon Glass // check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string
stbtt__CompareUTF8toUTF16_bigendian_prefix(const stbtt_uint8 * s1,stbtt_int32 len1,const stbtt_uint8 * s2,stbtt_int32 len2)30448340ef62SSimon Glass static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(const stbtt_uint8 *s1, stbtt_int32 len1, const stbtt_uint8 *s2, stbtt_int32 len2)
30458340ef62SSimon Glass {
30468340ef62SSimon Glass    stbtt_int32 i=0;
30478340ef62SSimon Glass 
30488340ef62SSimon Glass    // convert utf16 to utf8 and compare the results while converting
30498340ef62SSimon Glass    while (len2) {
30508340ef62SSimon Glass       stbtt_uint16 ch = s2[0]*256 + s2[1];
30518340ef62SSimon Glass       if (ch < 0x80) {
30528340ef62SSimon Glass          if (i >= len1) return -1;
30538340ef62SSimon Glass          if (s1[i++] != ch) return -1;
30548340ef62SSimon Glass       } else if (ch < 0x800) {
30558340ef62SSimon Glass          if (i+1 >= len1) return -1;
30568340ef62SSimon Glass          if (s1[i++] != 0xc0 + (ch >> 6)) return -1;
30578340ef62SSimon Glass          if (s1[i++] != 0x80 + (ch & 0x3f)) return -1;
30588340ef62SSimon Glass       } else if (ch >= 0xd800 && ch < 0xdc00) {
30598340ef62SSimon Glass          stbtt_uint32 c;
30608340ef62SSimon Glass          stbtt_uint16 ch2 = s2[2]*256 + s2[3];
30618340ef62SSimon Glass          if (i+3 >= len1) return -1;
30628340ef62SSimon Glass          c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
30638340ef62SSimon Glass          if (s1[i++] != 0xf0 + (c >> 18)) return -1;
30648340ef62SSimon Glass          if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1;
30658340ef62SSimon Glass          if (s1[i++] != 0x80 + ((c >>  6) & 0x3f)) return -1;
30668340ef62SSimon Glass          if (s1[i++] != 0x80 + ((c      ) & 0x3f)) return -1;
30678340ef62SSimon Glass          s2 += 2; // plus another 2 below
30688340ef62SSimon Glass          len2 -= 2;
30698340ef62SSimon Glass       } else if (ch >= 0xdc00 && ch < 0xe000) {
30708340ef62SSimon Glass          return -1;
30718340ef62SSimon Glass       } else {
30728340ef62SSimon Glass          if (i+2 >= len1) return -1;
30738340ef62SSimon Glass          if (s1[i++] != 0xe0 + (ch >> 12)) return -1;
30748340ef62SSimon Glass          if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1;
30758340ef62SSimon Glass          if (s1[i++] != 0x80 + ((ch     ) & 0x3f)) return -1;
30768340ef62SSimon Glass       }
30778340ef62SSimon Glass       s2 += 2;
30788340ef62SSimon Glass       len2 -= 2;
30798340ef62SSimon Glass    }
30808340ef62SSimon Glass    return i;
30818340ef62SSimon Glass }
30828340ef62SSimon Glass 
stbtt_CompareUTF8toUTF16_bigendian(const char * s1,int len1,const char * s2,int len2)30838340ef62SSimon Glass STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2)
30848340ef62SSimon Glass {
30858340ef62SSimon Glass    return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((const stbtt_uint8*) s1, len1, (const stbtt_uint8*) s2, len2);
30868340ef62SSimon Glass }
30878340ef62SSimon Glass 
30888340ef62SSimon Glass // returns results in whatever encoding you request... but note that 2-byte encodings
30898340ef62SSimon Glass // will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare
stbtt_GetFontNameString(const stbtt_fontinfo * font,int * length,int platformID,int encodingID,int languageID,int nameID)30908340ef62SSimon Glass STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)
30918340ef62SSimon Glass {
30928340ef62SSimon Glass    stbtt_int32 i,count,stringOffset;
30938340ef62SSimon Glass    stbtt_uint8 *fc = font->data;
30948340ef62SSimon Glass    stbtt_uint32 offset = font->fontstart;
30958340ef62SSimon Glass    stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
30968340ef62SSimon Glass    if (!nm) return NULL;
30978340ef62SSimon Glass 
30988340ef62SSimon Glass    count = ttUSHORT(fc+nm+2);
30998340ef62SSimon Glass    stringOffset = nm + ttUSHORT(fc+nm+4);
31008340ef62SSimon Glass    for (i=0; i < count; ++i) {
31018340ef62SSimon Glass       stbtt_uint32 loc = nm + 6 + 12 * i;
31028340ef62SSimon Glass       if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
31038340ef62SSimon Glass           && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
31048340ef62SSimon Glass          *length = ttUSHORT(fc+loc+8);
31058340ef62SSimon Glass          return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
31068340ef62SSimon Glass       }
31078340ef62SSimon Glass    }
31088340ef62SSimon Glass    return NULL;
31098340ef62SSimon Glass }
31108340ef62SSimon Glass 
stbtt__matchpair(stbtt_uint8 * fc,stbtt_uint32 nm,stbtt_uint8 * name,stbtt_int32 nlen,stbtt_int32 target_id,stbtt_int32 next_id)31118340ef62SSimon Glass static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
31128340ef62SSimon Glass {
31138340ef62SSimon Glass    stbtt_int32 i;
31148340ef62SSimon Glass    stbtt_int32 count = ttUSHORT(fc+nm+2);
31158340ef62SSimon Glass    stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
31168340ef62SSimon Glass 
31178340ef62SSimon Glass    for (i=0; i < count; ++i) {
31188340ef62SSimon Glass       stbtt_uint32 loc = nm + 6 + 12 * i;
31198340ef62SSimon Glass       stbtt_int32 id = ttUSHORT(fc+loc+6);
31208340ef62SSimon Glass       if (id == target_id) {
31218340ef62SSimon Glass          // find the encoding
31228340ef62SSimon Glass          stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
31238340ef62SSimon Glass 
31248340ef62SSimon Glass          // is this a Unicode encoding?
31258340ef62SSimon Glass          if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
31268340ef62SSimon Glass             stbtt_int32 slen = ttUSHORT(fc+loc+8);
31278340ef62SSimon Glass             stbtt_int32 off = ttUSHORT(fc+loc+10);
31288340ef62SSimon Glass 
31298340ef62SSimon Glass             // check if there's a prefix match
31308340ef62SSimon Glass             stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
31318340ef62SSimon Glass             if (matchlen >= 0) {
31328340ef62SSimon Glass                // check for target_id+1 immediately following, with same encoding & language
31338340ef62SSimon Glass                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) {
31348340ef62SSimon Glass                   slen = ttUSHORT(fc+loc+12+8);
31358340ef62SSimon Glass                   off = ttUSHORT(fc+loc+12+10);
31368340ef62SSimon Glass                   if (slen == 0) {
31378340ef62SSimon Glass                      if (matchlen == nlen)
31388340ef62SSimon Glass                         return 1;
31398340ef62SSimon Glass                   } else if (matchlen < nlen && name[matchlen] == ' ') {
31408340ef62SSimon Glass                      ++matchlen;
31418340ef62SSimon Glass                      if (stbtt_CompareUTF8toUTF16_bigendian((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen))
31428340ef62SSimon Glass                         return 1;
31438340ef62SSimon Glass                   }
31448340ef62SSimon Glass                } else {
31458340ef62SSimon Glass                   // if nothing immediately following
31468340ef62SSimon Glass                   if (matchlen == nlen)
31478340ef62SSimon Glass                      return 1;
31488340ef62SSimon Glass                }
31498340ef62SSimon Glass             }
31508340ef62SSimon Glass          }
31518340ef62SSimon Glass 
31528340ef62SSimon Glass          // @TODO handle other encodings
31538340ef62SSimon Glass       }
31548340ef62SSimon Glass    }
31558340ef62SSimon Glass    return 0;
31568340ef62SSimon Glass }
31578340ef62SSimon Glass 
stbtt__matches(stbtt_uint8 * fc,stbtt_uint32 offset,stbtt_uint8 * name,stbtt_int32 flags)31588340ef62SSimon Glass static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
31598340ef62SSimon Glass {
31608340ef62SSimon Glass    stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name);
31618340ef62SSimon Glass    stbtt_uint32 nm,hd;
31628340ef62SSimon Glass    if (!stbtt__isfont(fc+offset)) return 0;
31638340ef62SSimon Glass 
31648340ef62SSimon Glass    // check italics/bold/underline flags in macStyle...
31658340ef62SSimon Glass    if (flags) {
31668340ef62SSimon Glass       hd = stbtt__find_table(fc, offset, "head");
31678340ef62SSimon Glass       if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0;
31688340ef62SSimon Glass    }
31698340ef62SSimon Glass 
31708340ef62SSimon Glass    nm = stbtt__find_table(fc, offset, "name");
31718340ef62SSimon Glass    if (!nm) return 0;
31728340ef62SSimon Glass 
31738340ef62SSimon Glass    if (flags) {
31748340ef62SSimon Glass       // if we checked the macStyle flags, then just check the family and ignore the subfamily
31758340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))  return 1;
31768340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen,  1, -1))  return 1;
31778340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen,  3, -1))  return 1;
31788340ef62SSimon Glass    } else {
31798340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))  return 1;
31808340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen,  1,  2))  return 1;
31818340ef62SSimon Glass       if (stbtt__matchpair(fc, nm, name, nlen,  3, -1))  return 1;
31828340ef62SSimon Glass    }
31838340ef62SSimon Glass 
31848340ef62SSimon Glass    return 0;
31858340ef62SSimon Glass }
31868340ef62SSimon Glass 
stbtt_FindMatchingFont(const unsigned char * font_collection,const char * name_utf8,stbtt_int32 flags)31878340ef62SSimon Glass STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *font_collection, const char *name_utf8, stbtt_int32 flags)
31888340ef62SSimon Glass {
31898340ef62SSimon Glass    stbtt_int32 i;
31908340ef62SSimon Glass    for (i=0;;++i) {
31918340ef62SSimon Glass       stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
31928340ef62SSimon Glass       if (off < 0) return off;
31938340ef62SSimon Glass       if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
31948340ef62SSimon Glass          return off;
31958340ef62SSimon Glass    }
31968340ef62SSimon Glass }
31978340ef62SSimon Glass 
31988340ef62SSimon Glass #endif // STB_TRUETYPE_IMPLEMENTATION
31998340ef62SSimon Glass 
32008340ef62SSimon Glass 
32018340ef62SSimon Glass // FULL VERSION HISTORY
32028340ef62SSimon Glass //
32038340ef62SSimon Glass //   1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges
32048340ef62SSimon Glass //   1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints;
32058340ef62SSimon Glass //                     allow PackFontRanges to pack and render in separate phases;
32068340ef62SSimon Glass //                     fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?);
32078340ef62SSimon Glass //                     fixed an assert() bug in the new rasterizer
32088340ef62SSimon Glass //                     replace assert() with STBTT_assert() in new rasterizer
32098340ef62SSimon Glass //   1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine)
32108340ef62SSimon Glass //                     also more precise AA rasterizer, except if shapes overlap
32118340ef62SSimon Glass //                     remove need for STBTT_sort
32128340ef62SSimon Glass //   1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC
32138340ef62SSimon Glass //   1.04 (2015-04-15) typo in example
32148340ef62SSimon Glass //   1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes
32158340ef62SSimon Glass //   1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++
32168340ef62SSimon Glass //   1.01 (2014-12-08) fix subpixel position when oversampling to exactly match
32178340ef62SSimon Glass //                        non-oversampled; STBTT_POINT_SIZE for packed case only
32188340ef62SSimon Glass //   1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling
32198340ef62SSimon Glass //   0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg)
32208340ef62SSimon Glass //   0.9  (2014-08-07) support certain mac/iOS fonts without an MS platformID
32218340ef62SSimon Glass //   0.8b (2014-07-07) fix a warning
32228340ef62SSimon Glass //   0.8  (2014-05-25) fix a few more warnings
32238340ef62SSimon Glass //   0.7  (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back
32248340ef62SSimon Glass //   0.6c (2012-07-24) improve documentation
32258340ef62SSimon Glass //   0.6b (2012-07-20) fix a few more warnings
32268340ef62SSimon Glass //   0.6  (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels,
32278340ef62SSimon Glass //                        stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty
32288340ef62SSimon Glass //   0.5  (2011-12-09) bugfixes:
32298340ef62SSimon Glass //                        subpixel glyph renderer computed wrong bounding box
32308340ef62SSimon Glass //                        first vertex of shape can be off-curve (FreeSans)
32318340ef62SSimon Glass //   0.4b (2011-12-03) fixed an error in the font baking example
32328340ef62SSimon Glass //   0.4  (2011-12-01) kerning, subpixel rendering (tor)
32338340ef62SSimon Glass //                    bugfixes for:
32348340ef62SSimon Glass //                        codepoint-to-glyph conversion using table fmt=12
32358340ef62SSimon Glass //                        codepoint-to-glyph conversion using table fmt=4
32368340ef62SSimon Glass //                        stbtt_GetBakedQuad with non-square texture (Zer)
32378340ef62SSimon Glass //                    updated Hello World! sample to use kerning and subpixel
32388340ef62SSimon Glass //                    fixed some warnings
32398340ef62SSimon Glass //   0.3  (2009-06-24) cmap fmt=12, compound shapes (MM)
32408340ef62SSimon Glass //                    userdata, malloc-from-userdata, non-zero fill (stb)
32418340ef62SSimon Glass //   0.2  (2009-03-11) Fix unsigned/signed char warnings
32428340ef62SSimon Glass //   0.1  (2009-03-09) First public release
32438340ef62SSimon Glass //
3244