| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#if 0 |
| 239 |
#define STB_TRUETYPE_IMPLEMENTATION |
| 240 |
#include "stb_truetype.h" |
| 241 |
|
| 242 |
unsigned char ttf_buffer[1<<20]; |
| 243 |
unsigned char temp_bitmap[512*512]; |
| 244 |
|
| 245 |
stbtt_bakedchar cdata[96]; |
| 246 |
GLuint ftex; |
| 247 |
|
| 248 |
void my_stbtt_initfont(void) |
| 249 |
{ |
| 250 |
fread(ttf_buffer, 1, 1<<20, fopen("c:/windows/fonts/times.ttf", "rb")); |
| 251 |
stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); |
| 252 |
|
| 253 |
glGenTextures(1, &ftex); |
| 254 |
glBindTexture(GL_TEXTURE_2D, ftex); |
| 255 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); |
| 256 |
|
| 257 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 258 |
} |
| 259 |
|
| 260 |
void my_stbtt_print(float x, float y, char *text) |
| 261 |
{ |
| 262 |
|
| 263 |
glEnable(GL_TEXTURE_2D); |
| 264 |
glBindTexture(GL_TEXTURE_2D, ftex); |
| 265 |
glBegin(GL_QUADS); |
| 266 |
while (*text) { |
| 267 |
if (*text >= 32 && *text < 128) { |
| 268 |
stbtt_aligned_quad q; |
| 269 |
stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1); |
| 270 |
glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0); |
| 271 |
glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0); |
| 272 |
glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1); |
| 273 |
glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1); |
| 274 |
} |
| 275 |
++text; |
| 276 |
} |
| 277 |
glEnd(); |
| 278 |
} |
| 279 |
#endif |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#if 0 |
| 287 |
#include <stdio.h> |
| 288 |
#define STB_TRUETYPE_IMPLEMENTATION |
| 289 |
#include "stb_truetype.h" |
| 290 |
|
| 291 |
char ttf_buffer[1<<25]; |
| 292 |
|
| 293 |
int main(int argc, char **argv) |
| 294 |
{ |
| 295 |
stbtt_fontinfo font; |
| 296 |
unsigned char *bitmap; |
| 297 |
int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20); |
| 298 |
|
| 299 |
fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb")); |
| 300 |
|
| 301 |
stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0)); |
| 302 |
bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0); |
| 303 |
|
| 304 |
for (j=0; j < h; ++j) { |
| 305 |
for (i=0; i < w; ++i) |
| 306 |
putchar(" .:ioVM@"[bitmap[j*w+i]>>5]); |
| 307 |
putchar('\n'); |
| 308 |
} |
| 309 |
return 0; |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
#if 0 |
| 331 |
char buffer[24<<20]; |
| 332 |
unsigned char screen[20][79]; |
| 333 |
|
| 334 |
int main(int arg, char **argv) |
| 335 |
{ |
| 336 |
stbtt_fontinfo font; |
| 337 |
int i,j,ascent,baseline,ch=0; |
| 338 |
float scale, xpos=2; |
| 339 |
char *text = "Heljo World!"; |
| 340 |
|
| 341 |
fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb")); |
| 342 |
stbtt_InitFont(&font, buffer, 0); |
| 343 |
|
| 344 |
scale = stbtt_ScaleForPixelHeight(&font, 15); |
| 345 |
stbtt_GetFontVMetrics(&font, &ascent,0,0); |
| 346 |
baseline = (int) (ascent*scale); |
| 347 |
|
| 348 |
while (text[ch]) { |
| 349 |
int advance,lsb,x0,y0,x1,y1; |
| 350 |
float x_shift = xpos - (float) floor(xpos); |
| 351 |
stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb); |
| 352 |
stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1); |
| 353 |
stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]); |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
xpos += (advance * scale); |
| 359 |
if (text[ch+1]) |
| 360 |
xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]); |
| 361 |
++ch; |
| 362 |
} |
| 363 |
|
| 364 |
for (j=0; j < 20; ++j) { |
| 365 |
for (i=0; i < 78; ++i) |
| 366 |
putchar(" .:ioVM@"[screen[j][i]>>5]); |
| 367 |
putchar('\n'); |
| 368 |
} |
| 369 |
|
| 370 |
return 0; |
| 371 |
} |
| 372 |
#endif |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
#ifdef STB_TRUETYPE_IMPLEMENTATION |
| 384 |
|
| 385 |
#ifndef stbtt_uint8 |
| 386 |
typedef unsigned char stbtt_uint8; |
| 387 |
typedef signed char stbtt_int8; |
| 388 |
typedef unsigned short stbtt_uint16; |
| 389 |
typedef signed short stbtt_int16; |
| 390 |
typedef unsigned int stbtt_uint32; |
| 391 |
typedef signed int stbtt_int32; |
| 392 |
#endif |
| 393 |
|
| 394 |
typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1]; |
| 395 |
typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1]; |
| 396 |
|
| 397 |
|
| 398 |
#ifndef STBTT_ifloor |
| 399 |
#include <math.h> |
| 400 |
#define STBTT_ifloor(x) ((int) floor(x)) |
| 401 |
#define STBTT_iceil(x) ((int) ceil(x)) |
| 402 |
#endif |
| 403 |
|
| 404 |
#ifndef STBTT_sqrt |
| 405 |
#include <math.h> |
| 406 |
#define STBTT_sqrt(x) sqrt(x) |
| 407 |
#endif |
| 408 |
|
| 409 |
|
| 410 |
#ifndef STBTT_malloc |
| 411 |
#include <stdlib.h> |
| 412 |
#define STBTT_malloc(x,u) ((void)(u),malloc(x)) |
| 413 |
#define STBTT_free(x,u) ((void)(u),free(x)) |
| 414 |
#endif |
| 415 |
|
| 416 |
#ifndef STBTT_assert |
| 417 |
#include <assert.h> |
| 418 |
#define STBTT_assert(x) assert(x) |
| 419 |
#endif |
| 420 |
|
| 421 |
#ifndef STBTT_strlen |
| 422 |
#include <string.h> |
| 423 |
#define STBTT_strlen(x) strlen(x) |
| 424 |
#endif |
| 425 |
|
| 426 |
#ifndef STBTT_memcpy |
| 427 |
#include <memory.h> |
| 428 |
#define STBTT_memcpy memcpy |
| 429 |
#define STBTT_memset memset |
| 430 |
#endif |
| 431 |
#endif |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
#ifndef __STB_INCLUDE_STB_TRUETYPE_H__ |
| 441 |
#define __STB_INCLUDE_STB_TRUETYPE_H__ |
| 442 |
|
| 443 |
#ifdef STBTT_STATIC |
| 444 |
#define STBTT_DEF static |
| 445 |
#else |
| 446 |
#define STBTT_DEF extern |
| 447 |
#endif |
| 448 |
|
| 449 |
#ifdef __cplusplus |
| 450 |
extern "C" { |
| 451 |
#endif |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
typedef struct |
| 461 |
{ |
| 462 |
unsigned short x0,y0,x1,y1; |
| 463 |
float xoff,yoff,xadvance; |
| 464 |
} stbtt_bakedchar; |
| 465 |
|
| 466 |
STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, |
| 467 |
float pixel_height, |
| 468 |
unsigned char *pixels, int pw, int ph, |
| 469 |
int first_char, int num_chars, |
| 470 |
stbtt_bakedchar *chardata); |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
typedef struct |
| 477 |
{ |
| 478 |
float x0,y0,s0,t0; |
| 479 |
float x1,y1,s1,t1; |
| 480 |
} stbtt_aligned_quad; |
| 481 |
|
| 482 |
STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, |
| 483 |
int char_index, |
| 484 |
float *xpos, float *ypos, |
| 485 |
stbtt_aligned_quad *q, |
| 486 |
int opengl_fillrule); |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
typedef struct |
| 507 |
{ |
| 508 |
unsigned short x0,y0,x1,y1; |
| 509 |
float xoff,yoff,xadvance; |
| 510 |
float xoff2,yoff2; |
| 511 |
} stbtt_packedchar; |
| 512 |
|
| 513 |
typedef struct stbtt_pack_context stbtt_pack_context; |
| 514 |
typedef struct stbtt_fontinfo stbtt_fontinfo; |
| 515 |
#ifndef STB_RECT_PACK_VERSION |
| 516 |
typedef struct stbrp_rect stbrp_rect; |
| 517 |
#endif |
| 518 |
|
| 519 |
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); |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc); |
| 531 |
|
| 532 |
|
| 533 |
#define STBTT_POINT_SIZE(x) (-(x)) |
| 534 |
|
| 535 |
STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size, |
| 536 |
int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range); |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
typedef struct |
| 551 |
{ |
| 552 |
float font_size; |
| 553 |
int first_unicode_codepoint_in_range; |
| 554 |
int *array_of_unicode_codepoints; |
| 555 |
int num_chars; |
| 556 |
stbtt_packedchar *chardata_for_range; |
| 557 |
unsigned char h_oversample, v_oversample; |
| 558 |
} stbtt_pack_range; |
| 559 |
|
| 560 |
STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges); |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample); |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, |
| 583 |
int char_index, |
| 584 |
float *xpos, float *ypos, |
| 585 |
stbtt_aligned_quad *q, |
| 586 |
int align_to_integer); |
| 587 |
|
| 588 |
STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); |
| 589 |
STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects); |
| 590 |
STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
struct stbtt_pack_context { |
| 604 |
void *user_allocator_context; |
| 605 |
void *pack_info; |
| 606 |
int width; |
| 607 |
int height; |
| 608 |
int stride_in_bytes; |
| 609 |
int padding; |
| 610 |
unsigned int h_oversample, v_oversample; |
| 611 |
unsigned char *pixels; |
| 612 |
void *nodes; |
| 613 |
}; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
typedef struct stbtt_fontinfo |
| 633 |
{ |
| 634 |
void * userdata; |
| 635 |
unsigned char * data; |
| 636 |
int fontstart; |
| 637 |
|
| 638 |
int numGlyphs; |
| 639 |
|
| 640 |
int loca,head,glyf,hhea,hmtx,kern; |
| 641 |
int index_map; |
| 642 |
int indexToLocFormat; |
| 643 |
} stbtt_fontinfo; |
| 644 |
|
| 645 |
STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset); |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels); |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels); |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap); |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1); |
| 691 |
|
| 692 |
|
| 693 |
STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing); |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2); |
| 699 |
|
| 700 |
|
| 701 |
STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1); |
| 702 |
|
| 703 |
|
| 704 |
STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing); |
| 705 |
STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2); |
| 706 |
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
#ifndef STBTT_vmove |
| 717 |
enum { |
| 718 |
STBTT_vmove=1, |
| 719 |
STBTT_vline, |
| 720 |
STBTT_vcurve |
| 721 |
}; |
| 722 |
#endif |
| 723 |
|
| 724 |
#ifndef stbtt_vertex |
| 725 |
|
| 726 |
#define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file |
| 727 |
typedef struct |
| 728 |
{ |
| 729 |
stbtt_vertex_type x,y,cx,cy; |
| 730 |
unsigned char type,padding; |
| 731 |
} stbtt_vertex; |
| 732 |
#endif |
| 733 |
|
| 734 |
STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index); |
| 735 |
|
| 736 |
|
| 737 |
STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices); |
| 738 |
STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices); |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices); |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata); |
| 758 |
|
| 759 |
|
| 760 |
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); |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
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); |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
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); |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
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); |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
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); |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
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); |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
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); |
| 797 |
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); |
| 798 |
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); |
| 799 |
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); |
| 800 |
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); |
| 801 |
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); |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
typedef struct |
| 806 |
{ |
| 807 |
int w,h,stride; |
| 808 |
unsigned char *pixels; |
| 809 |
} stbtt__bitmap; |
| 810 |
|
| 811 |
|
| 812 |
STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, |
| 813 |
float flatness_in_pixels, |
| 814 |
stbtt_vertex *vertices, |
| 815 |
int num_verts, |
| 816 |
float scale_x, float scale_y, |
| 817 |
float shift_x, float shift_y, |
| 818 |
int x_off, int y_off, |
| 819 |
int invert, |
| 820 |
void *userdata); |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags); |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
#define STBTT_MACSTYLE_DONTCARE 0 |
| 850 |
#define STBTT_MACSTYLE_BOLD 1 |
| 851 |
#define STBTT_MACSTYLE_ITALIC 2 |
| 852 |
#define STBTT_MACSTYLE_UNDERSCORE 4 |
| 853 |
#define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0 |
| 854 |
|
| 855 |
STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2); |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID); |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
enum { |
| 868 |
STBTT_PLATFORM_ID_UNICODE =0, |
| 869 |
STBTT_PLATFORM_ID_MAC =1, |
| 870 |
STBTT_PLATFORM_ID_ISO =2, |
| 871 |
STBTT_PLATFORM_ID_MICROSOFT =3 |
| 872 |
}; |
| 873 |
|
| 874 |
enum { |
| 875 |
STBTT_UNICODE_EID_UNICODE_1_0 =0, |
| 876 |
STBTT_UNICODE_EID_UNICODE_1_1 =1, |
| 877 |
STBTT_UNICODE_EID_ISO_10646 =2, |
| 878 |
STBTT_UNICODE_EID_UNICODE_2_0_BMP=3, |
| 879 |
STBTT_UNICODE_EID_UNICODE_2_0_FULL=4 |
| 880 |
}; |
| 881 |
|
| 882 |
enum { |
| 883 |
STBTT_MS_EID_SYMBOL =0, |
| 884 |
STBTT_MS_EID_UNICODE_BMP =1, |
| 885 |
STBTT_MS_EID_SHIFTJIS =2, |
| 886 |
STBTT_MS_EID_UNICODE_FULL =10 |
| 887 |
}; |
| 888 |
|
| 889 |
enum { |
| 890 |
STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4, |
| 891 |
STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5, |
| 892 |
STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6, |
| 893 |
STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7 |
| 894 |
}; |
| 895 |
|
| 896 |
enum { |
| 897 |
|
| 898 |
STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410, |
| 899 |
STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411, |
| 900 |
STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412, |
| 901 |
STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419, |
| 902 |
STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409, |
| 903 |
STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D |
| 904 |
}; |
| 905 |
|
| 906 |
enum { |
| 907 |
STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11, |
| 908 |
STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23, |
| 909 |
STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32, |
| 910 |
STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 , |
| 911 |
STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 , |
| 912 |
STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33, |
| 913 |
STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19 |
| 914 |
}; |
| 915 |
|
| 916 |
#ifdef __cplusplus |
| 917 |
} |
| 918 |
#endif |
| 919 |
|
| 920 |
#endif |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
#ifdef STB_TRUETYPE_IMPLEMENTATION |
| 930 |
|
| 931 |
#ifndef STBTT_MAX_OVERSAMPLE |
| 932 |
#define STBTT_MAX_OVERSAMPLE 8 |
| 933 |
#endif |
| 934 |
|
| 935 |
#if STBTT_MAX_OVERSAMPLE > 255 |
| 936 |
#error "STBTT_MAX_OVERSAMPLE cannot be > 255" |
| 937 |
#endif |
| 938 |
|
| 939 |
typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1]; |
| 940 |
|
| 941 |
#ifndef STBTT_RASTERIZER_VERSION |
| 942 |
#define STBTT_RASTERIZER_VERSION 2 |
| 943 |
#endif |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
#define ttBYTE(p) (* (stbtt_uint8 *) (p)) |
| 954 |
#define ttCHAR(p) (* (stbtt_int8 *) (p)) |
| 955 |
#define ttFixed(p) ttLONG(p) |
| 956 |
|
| 957 |
#if defined(STB_TRUETYPE_BIGENDIAN) && !defined(ALLOW_UNALIGNED_TRUETYPE) |
| 958 |
|
| 959 |
#define ttUSHORT(p) (* (stbtt_uint16 *) (p)) |
| 960 |
#define ttSHORT(p) (* (stbtt_int16 *) (p)) |
| 961 |
#define ttULONG(p) (* (stbtt_uint32 *) (p)) |
| 962 |
#define ttLONG(p) (* (stbtt_int32 *) (p)) |
| 963 |
|
| 964 |
#else |
| 965 |
|
| 966 |
static stbtt_uint16 ttUSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; } |
| 967 |
static stbtt_int16 ttSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; } |
| 968 |
static stbtt_uint32 ttULONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } |
| 969 |
static stbtt_int32 ttLONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } |
| 970 |
|
| 971 |
#endif |
| 972 |
|
| 973 |
#define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3)) |
| 974 |
#define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3]) |
| 975 |
|
| 976 |
static int stbtt__isfont(const stbtt_uint8 *font) |
| 977 |
{ |
| 978 |
|
| 979 |
if (stbtt_tag4(font, '1',0,0,0)) return 1; |
| 980 |
if (stbtt_tag(font, "typ1")) return 1; |
| 981 |
if (stbtt_tag(font, "OTTO")) return 1; |
| 982 |
if (stbtt_tag4(font, 0,1,0,0)) return 1; |
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag) |
| 988 |
{ |
| 989 |
stbtt_int32 num_tables = ttUSHORT(data+fontstart+4); |
| 990 |
stbtt_uint32 tabledir = fontstart + 12; |
| 991 |
stbtt_int32 i; |
| 992 |
for (i=0; i < num_tables; ++i) { |
| 993 |
stbtt_uint32 loc = tabledir + 16*i; |
| 994 |
if (stbtt_tag(data+loc+0, tag)) |
| 995 |
return ttULONG(data+loc+8); |
| 996 |
} |
| 997 |
return 0; |
| 998 |
} |
| 999 |
|
| 1000 |
STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *font_collection, int index) |
| 1001 |
{ |
| 1002 |
|
| 1003 |
if (stbtt__isfont(font_collection)) |
| 1004 |
return index == 0 ? 0 : -1; |
| 1005 |
|
| 1006 |
|
| 1007 |
if (stbtt_tag(font_collection, "ttcf")) { |
| 1008 |
|
| 1009 |
if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { |
| 1010 |
stbtt_int32 n = ttLONG(font_collection+8); |
| 1011 |
if (index >= n) |
| 1012 |
return -1; |
| 1013 |
return ttULONG(font_collection+12+index*4); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
return -1; |
| 1017 |
} |
| 1018 |
|
| 1019 |
STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart) |
| 1020 |
{ |
| 1021 |
stbtt_uint8 *data = (stbtt_uint8 *) data2; |
| 1022 |
stbtt_uint32 cmap, t; |
| 1023 |
stbtt_int32 i,numTables; |
| 1024 |
|
| 1025 |
info->data = data; |
| 1026 |
info->fontstart = fontstart; |
| 1027 |
|
| 1028 |
cmap = stbtt__find_table(data, fontstart, "cmap"); |
| 1029 |
info->loca = stbtt__find_table(data, fontstart, "loca"); |
| 1030 |
info->head = stbtt__find_table(data, fontstart, "head"); |
| 1031 |
info->glyf = stbtt__find_table(data, fontstart, "glyf"); |
| 1032 |
info->hhea = stbtt__find_table(data, fontstart, "hhea"); |
| 1033 |
info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); |
| 1034 |
info->kern = stbtt__find_table(data, fontstart, "kern"); |
| 1035 |
if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx) |
| 1036 |
return 0; |
| 1037 |
|
| 1038 |
t = stbtt__find_table(data, fontstart, "maxp"); |
| 1039 |
if (t) |
| 1040 |
info->numGlyphs = ttUSHORT(data+t+4); |
| 1041 |
else |
| 1042 |
info->numGlyphs = 0xffff; |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
numTables = ttUSHORT(data + cmap + 2); |
| 1048 |
info->index_map = 0; |
| 1049 |
for (i=0; i < numTables; ++i) { |
| 1050 |
stbtt_uint32 encoding_record = cmap + 4 + 8 * i; |
| 1051 |
|
| 1052 |
switch(ttUSHORT(data+encoding_record)) { |
| 1053 |
case STBTT_PLATFORM_ID_MICROSOFT: |
| 1054 |
switch (ttUSHORT(data+encoding_record+2)) { |
| 1055 |
case STBTT_MS_EID_UNICODE_BMP: |
| 1056 |
case STBTT_MS_EID_UNICODE_FULL: |
| 1057 |
|
| 1058 |
info->index_map = cmap + ttULONG(data+encoding_record+4); |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
break; |
| 1062 |
case STBTT_PLATFORM_ID_UNICODE: |
| 1063 |
|
| 1064 |
|
| 1065 |
info->index_map = cmap + ttULONG(data+encoding_record+4); |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
if (info->index_map == 0) |
| 1070 |
return 0; |
| 1071 |
|
| 1072 |
info->indexToLocFormat = ttUSHORT(data+info->head + 50); |
| 1073 |
return 1; |
| 1074 |
} |
| 1075 |
|
| 1076 |
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint) |
| 1077 |
{ |
| 1078 |
stbtt_uint8 *data = info->data; |
| 1079 |
stbtt_uint32 index_map = info->index_map; |
| 1080 |
|
| 1081 |
stbtt_uint16 format = ttUSHORT(data + index_map + 0); |
| 1082 |
if (format == 0) { |
| 1083 |
stbtt_int32 bytes = ttUSHORT(data + index_map + 2); |
| 1084 |
if (unicode_codepoint < bytes-6) |
| 1085 |
return ttBYTE(data + index_map + 6 + unicode_codepoint); |
| 1086 |
return 0; |
| 1087 |
} else if (format == 6) { |
| 1088 |
stbtt_uint32 first = ttUSHORT(data + index_map + 6); |
| 1089 |
stbtt_uint32 count = ttUSHORT(data + index_map + 8); |
| 1090 |
if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count) |
| 1091 |
return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2); |
| 1092 |
return 0; |
| 1093 |
} else if (format == 2) { |
| 1094 |
STBTT_assert(0); |
| 1095 |
return 0; |
| 1096 |
} else if (format == 4) { |
| 1097 |
stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1; |
| 1098 |
stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1; |
| 1099 |
stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10); |
| 1100 |
stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1; |
| 1101 |
|
| 1102 |
|
| 1103 |
stbtt_uint32 endCount = index_map + 14; |
| 1104 |
stbtt_uint32 search = endCount; |
| 1105 |
|
| 1106 |
if (unicode_codepoint > 0xffff) |
| 1107 |
return 0; |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2)) |
| 1112 |
search += rangeShift*2; |
| 1113 |
|
| 1114 |
|
| 1115 |
search -= 2; |
| 1116 |
while (entrySelector) { |
| 1117 |
stbtt_uint16 end; |
| 1118 |
searchRange >>= 1; |
| 1119 |
end = ttUSHORT(data + search + searchRange*2); |
| 1120 |
if (unicode_codepoint > end) |
| 1121 |
search += searchRange*2; |
| 1122 |
--entrySelector; |
| 1123 |
} |
| 1124 |
search += 2; |
| 1125 |
|
| 1126 |
{ |
| 1127 |
stbtt_uint16 offset, start; |
| 1128 |
stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1); |
| 1129 |
|
| 1130 |
STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item)); |
| 1131 |
start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item); |
| 1132 |
if (unicode_codepoint < start) |
| 1133 |
return 0; |
| 1134 |
|
| 1135 |
offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item); |
| 1136 |
if (offset == 0) |
| 1137 |
return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item)); |
| 1138 |
|
| 1139 |
return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item); |
| 1140 |
} |
| 1141 |
} else if (format == 12 || format == 13) { |
| 1142 |
stbtt_uint32 ngroups = ttULONG(data+index_map+12); |
| 1143 |
stbtt_int32 low,high; |
| 1144 |
low = 0; high = (stbtt_int32)ngroups; |
| 1145 |
|
| 1146 |
while (low < high) { |
| 1147 |
stbtt_int32 mid = low + ((high-low) >> 1); |
| 1148 |
stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12); |
| 1149 |
stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4); |
| 1150 |
if ((stbtt_uint32) unicode_codepoint < start_char) |
| 1151 |
high = mid; |
| 1152 |
else if ((stbtt_uint32) unicode_codepoint > end_char) |
| 1153 |
low = mid+1; |
| 1154 |
else { |
| 1155 |
stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8); |
| 1156 |
if (format == 12) |
| 1157 |
return start_glyph + unicode_codepoint-start_char; |
| 1158 |
else |
| 1159 |
return start_glyph; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
|
| 1165 |
STBTT_assert(0); |
| 1166 |
return 0; |
| 1167 |
} |
| 1168 |
|
| 1169 |
STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices) |
| 1170 |
{ |
| 1171 |
return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices); |
| 1172 |
} |
| 1173 |
|
| 1174 |
static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy) |
| 1175 |
{ |
| 1176 |
v->type = type; |
| 1177 |
v->x = (stbtt_int16) x; |
| 1178 |
v->y = (stbtt_int16) y; |
| 1179 |
v->cx = (stbtt_int16) cx; |
| 1180 |
v->cy = (stbtt_int16) cy; |
| 1181 |
} |
| 1182 |
|
| 1183 |
static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index) |
| 1184 |
{ |
| 1185 |
int g1,g2; |
| 1186 |
|
| 1187 |
if (glyph_index >= info->numGlyphs) return -1; |
| 1188 |
if (info->indexToLocFormat >= 2) return -1; |
| 1189 |
|
| 1190 |
if (info->indexToLocFormat == 0) { |
| 1191 |
g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2; |
| 1192 |
g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2; |
| 1193 |
} else { |
| 1194 |
g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4); |
| 1195 |
g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4); |
| 1196 |
} |
| 1197 |
|
| 1198 |
return g1==g2 ? -1 : g1; |
| 1199 |
} |
| 1200 |
|
| 1201 |
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) |
| 1202 |
{ |
| 1203 |
int g = stbtt__GetGlyfOffset(info, glyph_index); |
| 1204 |
if (g < 0) return 0; |
| 1205 |
|
| 1206 |
if (x0) *x0 = ttSHORT(info->data + g + 2); |
| 1207 |
if (y0) *y0 = ttSHORT(info->data + g + 4); |
| 1208 |
if (x1) *x1 = ttSHORT(info->data + g + 6); |
| 1209 |
if (y1) *y1 = ttSHORT(info->data + g + 8); |
| 1210 |
return 1; |
| 1211 |
} |
| 1212 |
|
| 1213 |
STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1) |
| 1214 |
{ |
| 1215 |
return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1); |
| 1216 |
} |
| 1217 |
|
| 1218 |
STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index) |
| 1219 |
{ |
| 1220 |
stbtt_int16 numberOfContours; |
| 1221 |
int g = stbtt__GetGlyfOffset(info, glyph_index); |
| 1222 |
if (g < 0) return 1; |
| 1223 |
numberOfContours = ttSHORT(info->data + g); |
| 1224 |
return numberOfContours == 0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off, |
| 1228 |
stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy) |
| 1229 |
{ |
| 1230 |
if (start_off) { |
| 1231 |
if (was_off) |
| 1232 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy); |
| 1233 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy); |
| 1234 |
} else { |
| 1235 |
if (was_off) |
| 1236 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy); |
| 1237 |
else |
| 1238 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0); |
| 1239 |
} |
| 1240 |
return num_vertices; |
| 1241 |
} |
| 1242 |
|
| 1243 |
STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) |
| 1244 |
{ |
| 1245 |
stbtt_int16 numberOfContours; |
| 1246 |
stbtt_uint8 *endPtsOfContours; |
| 1247 |
stbtt_uint8 *data = info->data; |
| 1248 |
stbtt_vertex *vertices=0; |
| 1249 |
int num_vertices=0; |
| 1250 |
int g = stbtt__GetGlyfOffset(info, glyph_index); |
| 1251 |
|
| 1252 |
*pvertices = NULL; |
| 1253 |
|
| 1254 |
if (g < 0) return 0; |
| 1255 |
|
| 1256 |
numberOfContours = ttSHORT(data + g); |
| 1257 |
|
| 1258 |
if (numberOfContours > 0) { |
| 1259 |
stbtt_uint8 flags=0,flagcount; |
| 1260 |
stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; |
| 1261 |
stbtt_int32 x,y,cx,cy,sx,sy, scx,scy; |
| 1262 |
stbtt_uint8 *points; |
| 1263 |
endPtsOfContours = (data + g + 10); |
| 1264 |
ins = ttUSHORT(data + g + 10 + numberOfContours * 2); |
| 1265 |
points = data + g + 10 + numberOfContours * 2 + 2 + ins; |
| 1266 |
|
| 1267 |
n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2); |
| 1268 |
|
| 1269 |
m = n + 2*numberOfContours; |
| 1270 |
vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata); |
| 1271 |
if (vertices == 0) |
| 1272 |
return 0; |
| 1273 |
|
| 1274 |
next_move = 0; |
| 1275 |
flagcount=0; |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
off = m - n; |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
for (i=0; i < n; ++i) { |
| 1286 |
if (flagcount == 0) { |
| 1287 |
flags = *points++; |
| 1288 |
if (flags & 8) |
| 1289 |
flagcount = *points++; |
| 1290 |
} else |
| 1291 |
--flagcount; |
| 1292 |
vertices[off+i].type = flags; |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
x=0; |
| 1297 |
for (i=0; i < n; ++i) { |
| 1298 |
flags = vertices[off+i].type; |
| 1299 |
if (flags & 2) { |
| 1300 |
stbtt_int16 dx = *points++; |
| 1301 |
x += (flags & 16) ? dx : -dx; |
| 1302 |
} else { |
| 1303 |
if (!(flags & 16)) { |
| 1304 |
x = x + (stbtt_int16) (points[0]*256 + points[1]); |
| 1305 |
points += 2; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
vertices[off+i].x = (stbtt_int16) x; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
y=0; |
| 1313 |
for (i=0; i < n; ++i) { |
| 1314 |
flags = vertices[off+i].type; |
| 1315 |
if (flags & 4) { |
| 1316 |
stbtt_int16 dy = *points++; |
| 1317 |
y += (flags & 32) ? dy : -dy; |
| 1318 |
} else { |
| 1319 |
if (!(flags & 32)) { |
| 1320 |
y = y + (stbtt_int16) (points[0]*256 + points[1]); |
| 1321 |
points += 2; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
vertices[off+i].y = (stbtt_int16) y; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
num_vertices=0; |
| 1329 |
sx = sy = cx = cy = scx = scy = 0; |
| 1330 |
for (i=0; i < n; ++i) { |
| 1331 |
flags = vertices[off+i].type; |
| 1332 |
x = (stbtt_int16) vertices[off+i].x; |
| 1333 |
y = (stbtt_int16) vertices[off+i].y; |
| 1334 |
|
| 1335 |
if (next_move == i) { |
| 1336 |
if (i != 0) |
| 1337 |
num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); |
| 1338 |
|
| 1339 |
|
| 1340 |
start_off = !(flags & 1); |
| 1341 |
if (start_off) { |
| 1342 |
|
| 1343 |
|
| 1344 |
scx = x; |
| 1345 |
scy = y; |
| 1346 |
if (!(vertices[off+i+1].type & 1)) { |
| 1347 |
|
| 1348 |
sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1; |
| 1349 |
sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1; |
| 1350 |
} else { |
| 1351 |
|
| 1352 |
sx = (stbtt_int32) vertices[off+i+1].x; |
| 1353 |
sy = (stbtt_int32) vertices[off+i+1].y; |
| 1354 |
++i; |
| 1355 |
} |
| 1356 |
} else { |
| 1357 |
sx = x; |
| 1358 |
sy = y; |
| 1359 |
} |
| 1360 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0); |
| 1361 |
was_off = 0; |
| 1362 |
next_move = 1 + ttUSHORT(endPtsOfContours+j*2); |
| 1363 |
++j; |
| 1364 |
} else { |
| 1365 |
if (!(flags & 1)) { |
| 1366 |
if (was_off) |
| 1367 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy); |
| 1368 |
cx = x; |
| 1369 |
cy = y; |
| 1370 |
was_off = 1; |
| 1371 |
} else { |
| 1372 |
if (was_off) |
| 1373 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy); |
| 1374 |
else |
| 1375 |
stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0); |
| 1376 |
was_off = 0; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); |
| 1381 |
} else if (numberOfContours == -1) { |
| 1382 |
|
| 1383 |
int more = 1; |
| 1384 |
stbtt_uint8 *comp = data + g + 10; |
| 1385 |
num_vertices = 0; |
| 1386 |
vertices = 0; |
| 1387 |
while (more) { |
| 1388 |
stbtt_uint16 flags, gidx; |
| 1389 |
int comp_num_verts = 0, i; |
| 1390 |
stbtt_vertex *comp_verts = 0, *tmp = 0; |
| 1391 |
float mtx[6] = {1,0,0,1,0,0}, m, n; |
| 1392 |
|
| 1393 |
flags = ttSHORT(comp); comp+=2; |
| 1394 |
gidx = ttSHORT(comp); comp+=2; |
| 1395 |
|
| 1396 |
if (flags & 2) { |
| 1397 |
if (flags & 1) { |
| 1398 |
mtx[4] = ttSHORT(comp); comp+=2; |
| 1399 |
mtx[5] = ttSHORT(comp); comp+=2; |
| 1400 |
} else { |
| 1401 |
mtx[4] = ttCHAR(comp); comp+=1; |
| 1402 |
mtx[5] = ttCHAR(comp); comp+=1; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
else { |
| 1406 |
|
| 1407 |
STBTT_assert(0); |
| 1408 |
} |
| 1409 |
if (flags & (1<<3)) { |
| 1410 |
mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1411 |
mtx[1] = mtx[2] = 0; |
| 1412 |
} else if (flags & (1<<6)) { |
| 1413 |
mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1414 |
mtx[1] = mtx[2] = 0; |
| 1415 |
mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1416 |
} else if (flags & (1<<7)) { |
| 1417 |
mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1418 |
mtx[1] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1419 |
mtx[2] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1420 |
mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]); |
| 1425 |
n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]); |
| 1426 |
|
| 1427 |
|
| 1428 |
comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts); |
| 1429 |
if (comp_num_verts > 0) { |
| 1430 |
|
| 1431 |
for (i = 0; i < comp_num_verts; ++i) { |
| 1432 |
stbtt_vertex* v = &comp_verts[i]; |
| 1433 |
stbtt_vertex_type x,y; |
| 1434 |
x=v->x; y=v->y; |
| 1435 |
v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); |
| 1436 |
v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); |
| 1437 |
x=v->cx; y=v->cy; |
| 1438 |
v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); |
| 1439 |
v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); |
| 1440 |
} |
| 1441 |
|
| 1442 |
tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata); |
| 1443 |
if (!tmp) { |
| 1444 |
if (vertices) STBTT_free(vertices, info->userdata); |
| 1445 |
if (comp_verts) STBTT_free(comp_verts, info->userdata); |
| 1446 |
return 0; |
| 1447 |
} |
| 1448 |
if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); |
| 1449 |
STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex)); |
| 1450 |
if (vertices) STBTT_free(vertices, info->userdata); |
| 1451 |
vertices = tmp; |
| 1452 |
STBTT_free(comp_verts, info->userdata); |
| 1453 |
num_vertices += comp_num_verts; |
| 1454 |
} |
| 1455 |
|
| 1456 |
more = flags & (1<<5); |
| 1457 |
} |
| 1458 |
} else if (numberOfContours < 0) { |
| 1459 |
|
| 1460 |
STBTT_assert(0); |
| 1461 |
} else { |
| 1462 |
|
| 1463 |
} |
| 1464 |
|
| 1465 |
*pvertices = vertices; |
| 1466 |
return num_vertices; |
| 1467 |
} |
| 1468 |
|
| 1469 |
STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing) |
| 1470 |
{ |
| 1471 |
stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34); |
| 1472 |
if (glyph_index < numOfLongHorMetrics) { |
| 1473 |
if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index); |
| 1474 |
if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2); |
| 1475 |
} else { |
| 1476 |
if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1)); |
| 1477 |
if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics)); |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) |
| 1482 |
{ |
| 1483 |
stbtt_uint8 *data = info->data + info->kern; |
| 1484 |
stbtt_uint32 needle, straw; |
| 1485 |
int l, r, m; |
| 1486 |
|
| 1487 |
|
| 1488 |
if (!info->kern) |
| 1489 |
return 0; |
| 1490 |
if (ttUSHORT(data+2) < 1) |
| 1491 |
return 0; |
| 1492 |
if (ttUSHORT(data+8) != 1) |
| 1493 |
return 0; |
| 1494 |
|
| 1495 |
l = 0; |
| 1496 |
r = ttUSHORT(data+10) - 1; |
| 1497 |
needle = glyph1 << 16 | glyph2; |
| 1498 |
while (l <= r) { |
| 1499 |
m = (l + r) >> 1; |
| 1500 |
straw = ttULONG(data+18+(m*6)); |
| 1501 |
if (needle < straw) |
| 1502 |
r = m - 1; |
| 1503 |
else if (needle > straw) |
| 1504 |
l = m + 1; |
| 1505 |
else |
| 1506 |
return ttSHORT(data+22+(m*6)); |
| 1507 |
} |
| 1508 |
return 0; |
| 1509 |
} |
| 1510 |
|
| 1511 |
STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2) |
| 1512 |
{ |
| 1513 |
if (!info->kern) |
| 1514 |
return 0; |
| 1515 |
return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2)); |
| 1516 |
} |
| 1517 |
|
| 1518 |
STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing) |
| 1519 |
{ |
| 1520 |
stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing); |
| 1521 |
} |
| 1522 |
|
| 1523 |
STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap) |
| 1524 |
{ |
| 1525 |
if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4); |
| 1526 |
if (descent) *descent = ttSHORT(info->data+info->hhea + 6); |
| 1527 |
if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8); |
| 1528 |
} |
| 1529 |
|
| 1530 |
STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1) |
| 1531 |
{ |
| 1532 |
*x0 = ttSHORT(info->data + info->head + 36); |
| 1533 |
*y0 = ttSHORT(info->data + info->head + 38); |
| 1534 |
*x1 = ttSHORT(info->data + info->head + 40); |
| 1535 |
*y1 = ttSHORT(info->data + info->head + 42); |
| 1536 |
} |
| 1537 |
|
| 1538 |
STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height) |
| 1539 |
{ |
| 1540 |
int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6); |
| 1541 |
return (float) height / fheight; |
| 1542 |
} |
| 1543 |
|
| 1544 |
STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels) |
| 1545 |
{ |
| 1546 |
int unitsPerEm = ttUSHORT(info->data + info->head + 18); |
| 1547 |
return pixels / unitsPerEm; |
| 1548 |
} |
| 1549 |
|
| 1550 |
STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v) |
| 1551 |
{ |
| 1552 |
STBTT_free(v, info->userdata); |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
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) |
| 1561 |
{ |
| 1562 |
int x0=0,y0=0,x1,y1; |
| 1563 |
if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) { |
| 1564 |
|
| 1565 |
if (ix0) *ix0 = 0; |
| 1566 |
if (iy0) *iy0 = 0; |
| 1567 |
if (ix1) *ix1 = 0; |
| 1568 |
if (iy1) *iy1 = 0; |
| 1569 |
} else { |
| 1570 |
|
| 1571 |
if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x); |
| 1572 |
if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y); |
| 1573 |
if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x); |
| 1574 |
if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y); |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
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) |
| 1579 |
{ |
| 1580 |
stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1); |
| 1581 |
} |
| 1582 |
|
| 1583 |
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) |
| 1584 |
{ |
| 1585 |
stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1); |
| 1586 |
} |
| 1587 |
|
| 1588 |
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) |
| 1589 |
{ |
| 1590 |
stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1); |
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
typedef struct stbtt__hheap_chunk |
| 1598 |
{ |
| 1599 |
struct stbtt__hheap_chunk *next; |
| 1600 |
} stbtt__hheap_chunk; |
| 1601 |
|
| 1602 |
typedef struct stbtt__hheap |
| 1603 |
{ |
| 1604 |
struct stbtt__hheap_chunk *head; |
| 1605 |
void *first_free; |
| 1606 |
int num_remaining_in_head_chunk; |
| 1607 |
} stbtt__hheap; |
| 1608 |
|
| 1609 |
static void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata) |
| 1610 |
{ |
| 1611 |
if (hh->first_free) { |
| 1612 |
void *p = hh->first_free; |
| 1613 |
hh->first_free = * (void **) p; |
| 1614 |
return p; |
| 1615 |
} else { |
| 1616 |
if (hh->num_remaining_in_head_chunk == 0) { |
| 1617 |
int count = (size < 32 ? 2000 : size < 128 ? 800 : 100); |
| 1618 |
stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata); |
| 1619 |
if (c == NULL) |
| 1620 |
return NULL; |
| 1621 |
c->next = hh->head; |
| 1622 |
hh->head = c; |
| 1623 |
hh->num_remaining_in_head_chunk = count; |
| 1624 |
} |
| 1625 |
--hh->num_remaining_in_head_chunk; |
| 1626 |
return (char *) (hh->head) + size * hh->num_remaining_in_head_chunk; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
static void stbtt__hheap_free(stbtt__hheap *hh, void *p) |
| 1631 |
{ |
| 1632 |
*(void **) p = hh->first_free; |
| 1633 |
hh->first_free = p; |
| 1634 |
} |
| 1635 |
|
| 1636 |
static void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata) |
| 1637 |
{ |
| 1638 |
stbtt__hheap_chunk *c = hh->head; |
| 1639 |
while (c) { |
| 1640 |
stbtt__hheap_chunk *n = c->next; |
| 1641 |
STBTT_free(c, userdata); |
| 1642 |
c = n; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
typedef struct stbtt__edge { |
| 1647 |
float x0,y0, x1,y1; |
| 1648 |
int invert; |
| 1649 |
} stbtt__edge; |
| 1650 |
|
| 1651 |
|
| 1652 |
typedef struct stbtt__active_edge |
| 1653 |
{ |
| 1654 |
struct stbtt__active_edge *next; |
| 1655 |
#if STBTT_RASTERIZER_VERSION==1 |
| 1656 |
int x,dx; |
| 1657 |
float ey; |
| 1658 |
int direction; |
| 1659 |
#elif STBTT_RASTERIZER_VERSION==2 |
| 1660 |
float fx,fdx,fdy; |
| 1661 |
float direction; |
| 1662 |
float sy; |
| 1663 |
float ey; |
| 1664 |
#else |
| 1665 |
#error "Unrecognized value of STBTT_RASTERIZER_VERSION" |
| 1666 |
#endif |
| 1667 |
} stbtt__active_edge; |
| 1668 |
|
| 1669 |
#if STBTT_RASTERIZER_VERSION == 1 |
| 1670 |
#define STBTT_FIXSHIFT 10 |
| 1671 |
#define STBTT_FIX (1 << STBTT_FIXSHIFT) |
| 1672 |
#define STBTT_FIXMASK (STBTT_FIX-1) |
| 1673 |
|
| 1674 |
static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) |
| 1675 |
{ |
| 1676 |
stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); |
| 1677 |
float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); |
| 1678 |
STBTT_assert(z != NULL); |
| 1679 |
if (!z) return z; |
| 1680 |
|
| 1681 |
|
| 1682 |
if (dxdy < 0) |
| 1683 |
z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy); |
| 1684 |
else |
| 1685 |
z->dx = STBTT_ifloor(STBTT_FIX * dxdy); |
| 1686 |
|
| 1687 |
z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0)); |
| 1688 |
z->x -= off_x * STBTT_FIX; |
| 1689 |
|
| 1690 |
z->ey = e->y1; |
| 1691 |
z->next = 0; |
| 1692 |
z->direction = e->invert ? 1 : -1; |
| 1693 |
return z; |
| 1694 |
} |
| 1695 |
#elif STBTT_RASTERIZER_VERSION == 2 |
| 1696 |
static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) |
| 1697 |
{ |
| 1698 |
stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); |
| 1699 |
float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); |
| 1700 |
STBTT_assert(z != NULL); |
| 1701 |
|
| 1702 |
if (!z) return z; |
| 1703 |
z->fdx = dxdy; |
| 1704 |
z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f; |
| 1705 |
z->fx = e->x0 + dxdy * (start_point - e->y0); |
| 1706 |
z->fx -= off_x; |
| 1707 |
z->direction = e->invert ? 1.0f : -1.0f; |
| 1708 |
z->sy = e->y0; |
| 1709 |
z->ey = e->y1; |
| 1710 |
z->next = 0; |
| 1711 |
return z; |
| 1712 |
} |
| 1713 |
#else |
| 1714 |
#error "Unrecognized value of STBTT_RASTERIZER_VERSION" |
| 1715 |
#endif |
| 1716 |
|
| 1717 |
#if STBTT_RASTERIZER_VERSION == 1 |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight) |
| 1722 |
{ |
| 1723 |
|
| 1724 |
int x0=0, w=0; |
| 1725 |
|
| 1726 |
while (e) { |
| 1727 |
if (w == 0) { |
| 1728 |
|
| 1729 |
x0 = e->x; w += e->direction; |
| 1730 |
} else { |
| 1731 |
int x1 = e->x; w += e->direction; |
| 1732 |
|
| 1733 |
if (w == 0) { |
| 1734 |
int i = x0 >> STBTT_FIXSHIFT; |
| 1735 |
int j = x1 >> STBTT_FIXSHIFT; |
| 1736 |
|
| 1737 |
if (i < len && j >= 0) { |
| 1738 |
if (i == j) { |
| 1739 |
|
| 1740 |
scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT); |
| 1741 |
} else { |
| 1742 |
if (i >= 0) |
| 1743 |
scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT); |
| 1744 |
else |
| 1745 |
i = -1; |
| 1746 |
|
| 1747 |
if (j < len) |
| 1748 |
scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT); |
| 1749 |
else |
| 1750 |
j = len; |
| 1751 |
|
| 1752 |
for (++i; i < j; ++i) |
| 1753 |
scanline[i] = scanline[i] + (stbtt_uint8) max_weight; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
} |
| 1757 |
} |
| 1758 |
|
| 1759 |
e = e->next; |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata) |
| 1764 |
{ |
| 1765 |
stbtt__hheap hh = { 0, 0, 0 }; |
| 1766 |
stbtt__active_edge *active = NULL; |
| 1767 |
int y,j=0; |
| 1768 |
int max_weight = (255 / vsubsample); |
| 1769 |
int s; |
| 1770 |
unsigned char scanline_data[512], *scanline; |
| 1771 |
|
| 1772 |
if (result->w > 512) |
| 1773 |
scanline = (unsigned char *) STBTT_malloc(result->w, userdata); |
| 1774 |
else |
| 1775 |
scanline = scanline_data; |
| 1776 |
|
| 1777 |
y = off_y * vsubsample; |
| 1778 |
e[n].y0 = (off_y + result->h) * (float) vsubsample + 1; |
| 1779 |
|
| 1780 |
while (j < result->h) { |
| 1781 |
STBTT_memset(scanline, 0, result->w); |
| 1782 |
for (s=0; s < vsubsample; ++s) { |
| 1783 |
|
| 1784 |
float scan_y = y + 0.5f; |
| 1785 |
stbtt__active_edge **step = &active; |
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
while (*step) { |
| 1790 |
stbtt__active_edge * z = *step; |
| 1791 |
if (z->ey <= scan_y) { |
| 1792 |
*step = z->next; |
| 1793 |
STBTT_assert(z->direction); |
| 1794 |
z->direction = 0; |
| 1795 |
stbtt__hheap_free(&hh, z); |
| 1796 |
} else { |
| 1797 |
z->x += z->dx; |
| 1798 |
step = &((*step)->next); |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
for(;;) { |
| 1804 |
int changed=0; |
| 1805 |
step = &active; |
| 1806 |
while (*step && (*step)->next) { |
| 1807 |
if ((*step)->x > (*step)->next->x) { |
| 1808 |
stbtt__active_edge *t = *step; |
| 1809 |
stbtt__active_edge *q = t->next; |
| 1810 |
|
| 1811 |
t->next = q->next; |
| 1812 |
q->next = t; |
| 1813 |
*step = q; |
| 1814 |
changed = 1; |
| 1815 |
} |
| 1816 |
step = &(*step)->next; |
| 1817 |
} |
| 1818 |
if (!changed) break; |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
while (e->y0 <= scan_y) { |
| 1823 |
if (e->y1 > scan_y) { |
| 1824 |
stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata); |
| 1825 |
if (z != NULL) { |
| 1826 |
|
| 1827 |
if (active == NULL) |
| 1828 |
active = z; |
| 1829 |
else if (z->x < active->x) { |
| 1830 |
|
| 1831 |
z->next = active; |
| 1832 |
active = z; |
| 1833 |
} else { |
| 1834 |
|
| 1835 |
stbtt__active_edge *p = active; |
| 1836 |
while (p->next && p->next->x < z->x) |
| 1837 |
p = p->next; |
| 1838 |
|
| 1839 |
z->next = p->next; |
| 1840 |
p->next = z; |
| 1841 |
} |
| 1842 |
} |
| 1843 |
} |
| 1844 |
++e; |
| 1845 |
} |
| 1846 |
|
| 1847 |
|
| 1848 |
if (active) |
| 1849 |
stbtt__fill_active_edges(scanline, result->w, active, max_weight); |
| 1850 |
|
| 1851 |
++y; |
| 1852 |
} |
| 1853 |
STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w); |
| 1854 |
++j; |
| 1855 |
} |
| 1856 |
|
| 1857 |
stbtt__hheap_cleanup(&hh, userdata); |
| 1858 |
|
| 1859 |
if (scanline != scanline_data) |
| 1860 |
STBTT_free(scanline, userdata); |
| 1861 |
} |
| 1862 |
|
| 1863 |
#elif STBTT_RASTERIZER_VERSION == 2 |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1) |
| 1868 |
{ |
| 1869 |
if (y0 == y1) return; |
| 1870 |
STBTT_assert(y0 < y1); |
| 1871 |
STBTT_assert(e->sy <= e->ey); |
| 1872 |
if (y0 > e->ey) return; |
| 1873 |
if (y1 < e->sy) return; |
| 1874 |
if (y0 < e->sy) { |
| 1875 |
x0 += (x1-x0) * (e->sy - y0) / (y1-y0); |
| 1876 |
y0 = e->sy; |
| 1877 |
} |
| 1878 |
if (y1 > e->ey) { |
| 1879 |
x1 += (x1-x0) * (e->ey - y1) / (y1-y0); |
| 1880 |
y1 = e->ey; |
| 1881 |
} |
| 1882 |
|
| 1883 |
if (x0 == x) |
| 1884 |
STBTT_assert(x1 <= x+1); |
| 1885 |
else if (x0 == x+1) |
| 1886 |
STBTT_assert(x1 >= x); |
| 1887 |
else if (x0 <= x) |
| 1888 |
STBTT_assert(x1 <= x); |
| 1889 |
else if (x0 >= x+1) |
| 1890 |
STBTT_assert(x1 >= x+1); |
| 1891 |
else |
| 1892 |
STBTT_assert(x1 >= x && x1 <= x+1); |
| 1893 |
|
| 1894 |
if (x0 <= x && x1 <= x) |
| 1895 |
scanline[x] += e->direction * (y1-y0); |
| 1896 |
else if (x0 >= x+1 && x1 >= x+1) |
| 1897 |
; |
| 1898 |
else { |
| 1899 |
STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1); |
| 1900 |
scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top) |
| 1905 |
{ |
| 1906 |
float y_bottom = y_top+1; |
| 1907 |
|
| 1908 |
while (e) { |
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
STBTT_assert(e->ey >= y_top); |
| 1913 |
|
| 1914 |
if (e->fdx == 0) { |
| 1915 |
float x0 = e->fx; |
| 1916 |
if (x0 < len) { |
| 1917 |
if (x0 >= 0) { |
| 1918 |
stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom); |
| 1919 |
stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom); |
| 1920 |
} else { |
| 1921 |
stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom); |
| 1922 |
} |
| 1923 |
} |
| 1924 |
} else { |
| 1925 |
float x0 = e->fx; |
| 1926 |
float dx = e->fdx; |
| 1927 |
float xb = x0 + dx; |
| 1928 |
float x_top, x_bottom; |
| 1929 |
float sy0,sy1; |
| 1930 |
float dy = e->fdy; |
| 1931 |
STBTT_assert(e->sy <= y_bottom && e->ey >= y_top); |
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
if (e->sy > y_top) { |
| 1937 |
x_top = x0 + dx * (e->sy - y_top); |
| 1938 |
sy0 = e->sy; |
| 1939 |
} else { |
| 1940 |
x_top = x0; |
| 1941 |
sy0 = y_top; |
| 1942 |
} |
| 1943 |
if (e->ey < y_bottom) { |
| 1944 |
x_bottom = x0 + dx * (e->ey - y_top); |
| 1945 |
sy1 = e->ey; |
| 1946 |
} else { |
| 1947 |
x_bottom = xb; |
| 1948 |
sy1 = y_bottom; |
| 1949 |
} |
| 1950 |
|
| 1951 |
if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) { |
| 1952 |
|
| 1953 |
|
| 1954 |
if ((int) x_top == (int) x_bottom) { |
| 1955 |
float height; |
| 1956 |
|
| 1957 |
int x = (int) x_top; |
| 1958 |
height = sy1 - sy0; |
| 1959 |
STBTT_assert(x >= 0 && x < len); |
| 1960 |
scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height; |
| 1961 |
scanline_fill[x] += e->direction * height; |
| 1962 |
} else { |
| 1963 |
int x,x1,x2; |
| 1964 |
float y_crossing, step, sign, area; |
| 1965 |
|
| 1966 |
if (x_top > x_bottom) { |
| 1967 |
|
| 1968 |
float t; |
| 1969 |
sy0 = y_bottom - (sy0 - y_top); |
| 1970 |
sy1 = y_bottom - (sy1 - y_top); |
| 1971 |
t = sy0, sy0 = sy1, sy1 = t; |
| 1972 |
t = x_bottom, x_bottom = x_top, x_top = t; |
| 1973 |
dx = -dx; |
| 1974 |
dy = -dy; |
| 1975 |
t = x0, x0 = xb, xb = t; |
| 1976 |
} |
| 1977 |
|
| 1978 |
x1 = (int) x_top; |
| 1979 |
x2 = (int) x_bottom; |
| 1980 |
|
| 1981 |
y_crossing = (x1+1 - x0) * dy + y_top; |
| 1982 |
|
| 1983 |
sign = e->direction; |
| 1984 |
|
| 1985 |
area = sign * (y_crossing-sy0); |
| 1986 |
|
| 1987 |
scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); |
| 1988 |
|
| 1989 |
step = sign * dy; |
| 1990 |
for (x = x1+1; x < x2; ++x) { |
| 1991 |
scanline[x] += area + step/2; |
| 1992 |
area += step; |
| 1993 |
} |
| 1994 |
y_crossing += dy * (x2 - (x1+1)); |
| 1995 |
|
| 1996 |
STBTT_assert(fabs(area) <= 1.01f); |
| 1997 |
|
| 1998 |
scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); |
| 1999 |
|
| 2000 |
scanline_fill[x2] += sign * (sy1-sy0); |
| 2001 |
} |
| 2002 |
} else { |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
int x; |
| 2008 |
for (x=0; x < len; ++x) { |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
float y0 = y_top; |
| 2024 |
float x1 = (float) (x); |
| 2025 |
float x2 = (float) (x+1); |
| 2026 |
float x3 = xb; |
| 2027 |
float y3 = y_bottom; |
| 2028 |
float y1,y2; |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
y1 = (x - x0) / dx + y_top; |
| 2034 |
y2 = (x+1 - x0) / dx + y_top; |
| 2035 |
|
| 2036 |
if (x0 < x1 && x3 > x2) { |
| 2037 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); |
| 2038 |
stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2); |
| 2039 |
stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); |
| 2040 |
} else if (x3 < x1 && x0 > x2) { |
| 2041 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); |
| 2042 |
stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1); |
| 2043 |
stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); |
| 2044 |
} else if (x0 < x1 && x3 > x1) { |
| 2045 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); |
| 2046 |
stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); |
| 2047 |
} else if (x3 < x1 && x0 > x1) { |
| 2048 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); |
| 2049 |
stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); |
| 2050 |
} else if (x0 < x2 && x3 > x2) { |
| 2051 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); |
| 2052 |
stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); |
| 2053 |
} else if (x3 < x2 && x0 > x2) { |
| 2054 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); |
| 2055 |
stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); |
| 2056 |
} else { |
| 2057 |
stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3); |
| 2058 |
} |
| 2059 |
} |
| 2060 |
} |
| 2061 |
} |
| 2062 |
e = e->next; |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int , int off_x, int off_y, void *userdata) |
| 2068 |
{ |
| 2069 |
stbtt__hheap hh = { 0, 0, 0 }; |
| 2070 |
stbtt__active_edge *active = NULL; |
| 2071 |
int y,j=0, i; |
| 2072 |
float scanline_data[129], *scanline, *scanline2; |
| 2073 |
|
| 2074 |
if (result->w > 64) |
| 2075 |
scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata); |
| 2076 |
else |
| 2077 |
scanline = scanline_data; |
| 2078 |
|
| 2079 |
scanline2 = scanline + result->w; |
| 2080 |
|
| 2081 |
y = off_y; |
| 2082 |
e[n].y0 = (float) (off_y + result->h) + 1; |
| 2083 |
|
| 2084 |
while (j < result->h) { |
| 2085 |
|
| 2086 |
float scan_y_top = y + 0.0f; |
| 2087 |
float scan_y_bottom = y + 1.0f; |
| 2088 |
stbtt__active_edge **step = &active; |
| 2089 |
|
| 2090 |
STBTT_memset(scanline , 0, result->w*sizeof(scanline[0])); |
| 2091 |
STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0])); |
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
while (*step) { |
| 2096 |
stbtt__active_edge * z = *step; |
| 2097 |
if (z->ey <= scan_y_top) { |
| 2098 |
*step = z->next; |
| 2099 |
STBTT_assert(z->direction); |
| 2100 |
z->direction = 0; |
| 2101 |
stbtt__hheap_free(&hh, z); |
| 2102 |
} else { |
| 2103 |
step = &((*step)->next); |
| 2104 |
} |
| 2105 |
} |
| 2106 |
|
| 2107 |
|
| 2108 |
while (e->y0 <= scan_y_bottom) { |
| 2109 |
if (e->y0 != e->y1) { |
| 2110 |
stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata); |
| 2111 |
if (z != NULL) { |
| 2112 |
STBTT_assert(z->ey >= scan_y_top); |
| 2113 |
|
| 2114 |
z->next = active; |
| 2115 |
active = z; |
| 2116 |
} |
| 2117 |
} |
| 2118 |
++e; |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
if (active) |
| 2123 |
stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top); |
| 2124 |
|
| 2125 |
{ |
| 2126 |
float sum = 0; |
| 2127 |
for (i=0; i < result->w; ++i) { |
| 2128 |
float k; |
| 2129 |
int m; |
| 2130 |
sum += scanline2[i]; |
| 2131 |
k = scanline[i] + sum; |
| 2132 |
k = (float) fabs(k)*255 + 0.5f; |
| 2133 |
m = (int) k; |
| 2134 |
if (m > 255) m = 255; |
| 2135 |
result->pixels[j*result->stride + i] = (unsigned char) m; |
| 2136 |
} |
| 2137 |
} |
| 2138 |
|
| 2139 |
step = &active; |
| 2140 |
while (*step) { |
| 2141 |
stbtt__active_edge *z = *step; |
| 2142 |
z->fx += z->fdx; |
| 2143 |
step = &((*step)->next); |
| 2144 |
} |
| 2145 |
|
| 2146 |
++y; |
| 2147 |
++j; |
| 2148 |
} |
| 2149 |
|
| 2150 |
stbtt__hheap_cleanup(&hh, userdata); |
| 2151 |
|
| 2152 |
if (scanline != scanline_data) |
| 2153 |
STBTT_free(scanline, userdata); |
| 2154 |
} |
| 2155 |
#else |
| 2156 |
#error "Unrecognized value of STBTT_RASTERIZER_VERSION" |
| 2157 |
#endif |
| 2158 |
|
| 2159 |
#define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0) |
| 2160 |
|
| 2161 |
static void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n) |
| 2162 |
{ |
| 2163 |
int i,j; |
| 2164 |
for (i=1; i < n; ++i) { |
| 2165 |
stbtt__edge t = p[i], *a = &t; |
| 2166 |
j = i; |
| 2167 |
while (j > 0) { |
| 2168 |
stbtt__edge *b = &p[j-1]; |
| 2169 |
int c = STBTT__COMPARE(a,b); |
| 2170 |
if (!c) break; |
| 2171 |
p[j] = p[j-1]; |
| 2172 |
--j; |
| 2173 |
} |
| 2174 |
if (i != j) |
| 2175 |
p[j] = t; |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
static void stbtt__sort_edges_quicksort(stbtt__edge *p, int n) |
| 2180 |
{ |
| 2181 |
|
| 2182 |
while (n > 12) { |
| 2183 |
stbtt__edge t; |
| 2184 |
int c01,c12,c,m,i,j; |
| 2185 |
|
| 2186 |
|
| 2187 |
m = n >> 1; |
| 2188 |
c01 = STBTT__COMPARE(&p[0],&p[m]); |
| 2189 |
c12 = STBTT__COMPARE(&p[m],&p[n-1]); |
| 2190 |
|
| 2191 |
if (c01 != c12) { |
| 2192 |
|
| 2193 |
int z; |
| 2194 |
c = STBTT__COMPARE(&p[0],&p[n-1]); |
| 2195 |
|
| 2196 |
|
| 2197 |
z = (c == c12) ? 0 : n-1; |
| 2198 |
t = p[z]; |
| 2199 |
p[z] = p[m]; |
| 2200 |
p[m] = t; |
| 2201 |
} |
| 2202 |
|
| 2203 |
|
| 2204 |
t = p[0]; |
| 2205 |
p[0] = p[m]; |
| 2206 |
p[m] = t; |
| 2207 |
|
| 2208 |
|
| 2209 |
i=1; |
| 2210 |
j=n-1; |
| 2211 |
for(;;) { |
| 2212 |
|
| 2213 |
|
| 2214 |
for (;;++i) { |
| 2215 |
if (!STBTT__COMPARE(&p[i], &p[0])) break; |
| 2216 |
} |
| 2217 |
for (;;--j) { |
| 2218 |
if (!STBTT__COMPARE(&p[0], &p[j])) break; |
| 2219 |
} |
| 2220 |
|
| 2221 |
if (i >= j) break; |
| 2222 |
t = p[i]; |
| 2223 |
p[i] = p[j]; |
| 2224 |
p[j] = t; |
| 2225 |
|
| 2226 |
++i; |
| 2227 |
--j; |
| 2228 |
} |
| 2229 |
|
| 2230 |
if (j < (n-i)) { |
| 2231 |
stbtt__sort_edges_quicksort(p,j); |
| 2232 |
p = p+i; |
| 2233 |
n = n-i; |
| 2234 |
} else { |
| 2235 |
stbtt__sort_edges_quicksort(p+i, n-i); |
| 2236 |
n = j; |
| 2237 |
} |
| 2238 |
} |
| 2239 |
} |
| 2240 |
|
| 2241 |
static void stbtt__sort_edges(stbtt__edge *p, int n) |
| 2242 |
{ |
| 2243 |
stbtt__sort_edges_quicksort(p, n); |
| 2244 |
stbtt__sort_edges_ins_sort(p, n); |
| 2245 |
} |
| 2246 |
|
| 2247 |
typedef struct |
| 2248 |
{ |
| 2249 |
float x,y; |
| 2250 |
} stbtt__point; |
| 2251 |
|
| 2252 |
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) |
| 2253 |
{ |
| 2254 |
float y_scale_inv = invert ? -scale_y : scale_y; |
| 2255 |
stbtt__edge *e; |
| 2256 |
int n,i,j,k,m; |
| 2257 |
#if STBTT_RASTERIZER_VERSION == 1 |
| 2258 |
int vsubsample = result->h < 8 ? 15 : 5; |
| 2259 |
#elif STBTT_RASTERIZER_VERSION == 2 |
| 2260 |
int vsubsample = 1; |
| 2261 |
#else |
| 2262 |
#error "Unrecognized value of STBTT_RASTERIZER_VERSION" |
| 2263 |
#endif |
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
n = 0; |
| 2268 |
for (i=0; i < windings; ++i) |
| 2269 |
n += wcount[i]; |
| 2270 |
|
| 2271 |
e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); |
| 2272 |
if (e == 0) return; |
| 2273 |
n = 0; |
| 2274 |
|
| 2275 |
m=0; |
| 2276 |
for (i=0; i < windings; ++i) { |
| 2277 |
stbtt__point *p = pts + m; |
| 2278 |
m += wcount[i]; |
| 2279 |
j = wcount[i]-1; |
| 2280 |
for (k=0; k < wcount[i]; j=k++) { |
| 2281 |
int a=k,b=j; |
| 2282 |
|
| 2283 |
if (p[j].y == p[k].y) |
| 2284 |
continue; |
| 2285 |
|
| 2286 |
e[n].invert = 0; |
| 2287 |
if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) { |
| 2288 |
e[n].invert = 1; |
| 2289 |
a=j,b=k; |
| 2290 |
} |
| 2291 |
e[n].x0 = p[a].x * scale_x + shift_x; |
| 2292 |
e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; |
| 2293 |
e[n].x1 = p[b].x * scale_x + shift_x; |
| 2294 |
e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; |
| 2295 |
++n; |
| 2296 |
} |
| 2297 |
} |
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
stbtt__sort_edges(e, n); |
| 2302 |
|
| 2303 |
|
| 2304 |
stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); |
| 2305 |
|
| 2306 |
STBTT_free(e, userdata); |
| 2307 |
} |
| 2308 |
|
| 2309 |
static void stbtt__add_point(stbtt__point *points, int n, float x, float y) |
| 2310 |
{ |
| 2311 |
if (!points) return; |
| 2312 |
points[n].x = x; |
| 2313 |
points[n].y = y; |
| 2314 |
} |
| 2315 |
|
| 2316 |
|
| 2317 |
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) |
| 2318 |
{ |
| 2319 |
|
| 2320 |
float mx = (x0 + 2*x1 + x2)/4; |
| 2321 |
float my = (y0 + 2*y1 + y2)/4; |
| 2322 |
|
| 2323 |
float dx = (x0+x2)/2 - mx; |
| 2324 |
float dy = (y0+y2)/2 - my; |
| 2325 |
if (n > 16) |
| 2326 |
return 1; |
| 2327 |
if (dx*dx+dy*dy > objspace_flatness_squared) { |
| 2328 |
stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1); |
| 2329 |
stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1); |
| 2330 |
} else { |
| 2331 |
stbtt__add_point(points, *num_points,x2,y2); |
| 2332 |
*num_points = *num_points+1; |
| 2333 |
} |
| 2334 |
return 1; |
| 2335 |
} |
| 2336 |
|
| 2337 |
|
| 2338 |
static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata) |
| 2339 |
{ |
| 2340 |
stbtt__point *points=0; |
| 2341 |
int num_points=0; |
| 2342 |
|
| 2343 |
float objspace_flatness_squared = objspace_flatness * objspace_flatness; |
| 2344 |
int i,n=0,start=0, pass; |
| 2345 |
|
| 2346 |
|
| 2347 |
for (i=0; i < num_verts; ++i) |
| 2348 |
if (vertices[i].type == STBTT_vmove) |
| 2349 |
++n; |
| 2350 |
|
| 2351 |
*num_contours = n; |
| 2352 |
if (n == 0) return 0; |
| 2353 |
|
| 2354 |
*contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata); |
| 2355 |
|
| 2356 |
if (*contour_lengths == 0) { |
| 2357 |
*num_contours = 0; |
| 2358 |
return 0; |
| 2359 |
} |
| 2360 |
|
| 2361 |
|
| 2362 |
for (pass=0; pass < 2; ++pass) { |
| 2363 |
float x=0,y=0; |
| 2364 |
if (pass == 1) { |
| 2365 |
points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata); |
| 2366 |
if (points == NULL) goto error; |
| 2367 |
} |
| 2368 |
num_points = 0; |
| 2369 |
n= -1; |
| 2370 |
for (i=0; i < num_verts; ++i) { |
| 2371 |
switch (vertices[i].type) { |
| 2372 |
case STBTT_vmove: |
| 2373 |
|
| 2374 |
if (n >= 0) |
| 2375 |
(*contour_lengths)[n] = num_points - start; |
| 2376 |
++n; |
| 2377 |
start = num_points; |
| 2378 |
|
| 2379 |
x = vertices[i].x, y = vertices[i].y; |
| 2380 |
stbtt__add_point(points, num_points++, x,y); |
| 2381 |
break; |
| 2382 |
case STBTT_vline: |
| 2383 |
x = vertices[i].x, y = vertices[i].y; |
| 2384 |
stbtt__add_point(points, num_points++, x, y); |
| 2385 |
break; |
| 2386 |
case STBTT_vcurve: |
| 2387 |
stbtt__tesselate_curve(points, &num_points, x,y, |
| 2388 |
vertices[i].cx, vertices[i].cy, |
| 2389 |
vertices[i].x, vertices[i].y, |
| 2390 |
objspace_flatness_squared, 0); |
| 2391 |
x = vertices[i].x, y = vertices[i].y; |
| 2392 |
break; |
| 2393 |
} |
| 2394 |
} |
| 2395 |
(*contour_lengths)[n] = num_points - start; |
| 2396 |
} |
| 2397 |
|
| 2398 |
return points; |
| 2399 |
error: |
| 2400 |
STBTT_free(points, userdata); |
| 2401 |
STBTT_free(*contour_lengths, userdata); |
| 2402 |
*contour_lengths = 0; |
| 2403 |
*num_contours = 0; |
| 2404 |
return NULL; |
| 2405 |
} |
| 2406 |
|
| 2407 |
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) |
| 2408 |
{ |
| 2409 |
float scale = scale_x > scale_y ? scale_y : scale_x; |
| 2410 |
int winding_count, *winding_lengths; |
| 2411 |
stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata); |
| 2412 |
if (windings) { |
| 2413 |
stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata); |
| 2414 |
STBTT_free(winding_lengths, userdata); |
| 2415 |
STBTT_free(windings, userdata); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2419 |
STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata) |
| 2420 |
{ |
| 2421 |
STBTT_free(bitmap, userdata); |
| 2422 |
} |
| 2423 |
|
| 2424 |
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) |
| 2425 |
{ |
| 2426 |
int ix0,iy0,ix1,iy1; |
| 2427 |
stbtt__bitmap gbm; |
| 2428 |
stbtt_vertex *vertices; |
| 2429 |
int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); |
| 2430 |
|
| 2431 |
if (scale_x == 0) scale_x = scale_y; |
| 2432 |
if (scale_y == 0) { |
| 2433 |
if (scale_x == 0) { |
| 2434 |
STBTT_free(vertices, info->userdata); |
| 2435 |
return NULL; |
| 2436 |
} |
| 2437 |
scale_y = scale_x; |
| 2438 |
} |
| 2439 |
|
| 2440 |
stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1); |
| 2441 |
|
| 2442 |
|
| 2443 |
gbm.w = (ix1 - ix0); |
| 2444 |
gbm.h = (iy1 - iy0); |
| 2445 |
gbm.pixels = NULL; |
| 2446 |
|
| 2447 |
if (width ) *width = gbm.w; |
| 2448 |
if (height) *height = gbm.h; |
| 2449 |
if (xoff ) *xoff = ix0; |
| 2450 |
if (yoff ) *yoff = iy0; |
| 2451 |
|
| 2452 |
if (gbm.w && gbm.h) { |
| 2453 |
gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata); |
| 2454 |
if (gbm.pixels) { |
| 2455 |
gbm.stride = gbm.w; |
| 2456 |
|
| 2457 |
stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); |
| 2458 |
} |
| 2459 |
} |
| 2460 |
STBTT_free(vertices, info->userdata); |
| 2461 |
return gbm.pixels; |
| 2462 |
} |
| 2463 |
|
| 2464 |
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) |
| 2465 |
{ |
| 2466 |
return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff); |
| 2467 |
} |
| 2468 |
|
| 2469 |
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) |
| 2470 |
{ |
| 2471 |
int ix0,iy0; |
| 2472 |
stbtt_vertex *vertices; |
| 2473 |
int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); |
| 2474 |
stbtt__bitmap gbm; |
| 2475 |
|
| 2476 |
stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); |
| 2477 |
gbm.pixels = output; |
| 2478 |
gbm.w = out_w; |
| 2479 |
gbm.h = out_h; |
| 2480 |
gbm.stride = out_stride; |
| 2481 |
|
| 2482 |
if (gbm.w && gbm.h) |
| 2483 |
stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata); |
| 2484 |
|
| 2485 |
STBTT_free(vertices, info->userdata); |
| 2486 |
} |
| 2487 |
|
| 2488 |
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) |
| 2489 |
{ |
| 2490 |
stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph); |
| 2491 |
} |
| 2492 |
|
| 2493 |
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) |
| 2494 |
{ |
| 2495 |
return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff); |
| 2496 |
} |
| 2497 |
|
| 2498 |
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) |
| 2499 |
{ |
| 2500 |
stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint)); |
| 2501 |
} |
| 2502 |
|
| 2503 |
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) |
| 2504 |
{ |
| 2505 |
return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff); |
| 2506 |
} |
| 2507 |
|
| 2508 |
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) |
| 2509 |
{ |
| 2510 |
stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint); |
| 2511 |
} |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, |
| 2520 |
float pixel_height, |
| 2521 |
unsigned char *pixels, int pw, int ph, |
| 2522 |
int first_char, int num_chars, |
| 2523 |
stbtt_bakedchar *chardata) |
| 2524 |
{ |
| 2525 |
float scale; |
| 2526 |
int x,y,bottom_y, i; |
| 2527 |
stbtt_fontinfo f; |
| 2528 |
f.userdata = NULL; |
| 2529 |
if (!stbtt_InitFont(&f, data, offset)) |
| 2530 |
return -1; |
| 2531 |
STBTT_memset(pixels, 0, pw*ph); |
| 2532 |
x=y=1; |
| 2533 |
bottom_y = 1; |
| 2534 |
|
| 2535 |
scale = stbtt_ScaleForPixelHeight(&f, pixel_height); |
| 2536 |
|
| 2537 |
for (i=0; i < num_chars; ++i) { |
| 2538 |
int advance, lsb, x0,y0,x1,y1,gw,gh; |
| 2539 |
int g = stbtt_FindGlyphIndex(&f, first_char + i); |
| 2540 |
stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); |
| 2541 |
stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1); |
| 2542 |
gw = x1-x0; |
| 2543 |
gh = y1-y0; |
| 2544 |
if (x + gw + 1 >= pw) |
| 2545 |
y = bottom_y, x = 1; |
| 2546 |
if (y + gh + 1 >= ph) |
| 2547 |
return -i; |
| 2548 |
STBTT_assert(x+gw < pw); |
| 2549 |
STBTT_assert(y+gh < ph); |
| 2550 |
stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g); |
| 2551 |
chardata[i].x0 = (stbtt_int16) x; |
| 2552 |
chardata[i].y0 = (stbtt_int16) y; |
| 2553 |
chardata[i].x1 = (stbtt_int16) (x + gw); |
| 2554 |
chardata[i].y1 = (stbtt_int16) (y + gh); |
| 2555 |
chardata[i].xadvance = scale * advance; |
| 2556 |
chardata[i].xoff = (float) x0; |
| 2557 |
chardata[i].yoff = (float) y0; |
| 2558 |
x = x + gw + 1; |
| 2559 |
if (y+gh+1 > bottom_y) |
| 2560 |
bottom_y = y+gh+1; |
| 2561 |
} |
| 2562 |
return bottom_y; |
| 2563 |
} |
| 2564 |
|
| 2565 |
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) |
| 2566 |
{ |
| 2567 |
float d3d_bias = opengl_fillrule ? 0 : -0.5f; |
| 2568 |
float ipw = 1.0f / pw, iph = 1.0f / ph; |
| 2569 |
stbtt_bakedchar *b = chardata + char_index; |
| 2570 |
int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f); |
| 2571 |
int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f); |
| 2572 |
|
| 2573 |
q->x0 = round_x + d3d_bias; |
| 2574 |
q->y0 = round_y + d3d_bias; |
| 2575 |
q->x1 = round_x + b->x1 - b->x0 + d3d_bias; |
| 2576 |
q->y1 = round_y + b->y1 - b->y0 + d3d_bias; |
| 2577 |
|
| 2578 |
q->s0 = b->x0 * ipw; |
| 2579 |
q->t0 = b->y0 * iph; |
| 2580 |
q->s1 = b->x1 * ipw; |
| 2581 |
q->t1 = b->y1 * iph; |
| 2582 |
|
| 2583 |
*xpos += b->xadvance; |
| 2584 |
} |
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
#ifndef STB_RECT_PACK_VERSION |
| 2592 |
#ifdef _MSC_VER |
| 2593 |
#define STBTT__NOTUSED(v) (void)(v) |
| 2594 |
#else |
| 2595 |
#define STBTT__NOTUSED(v) (void)sizeof(v) |
| 2596 |
#endif |
| 2597 |
|
| 2598 |
typedef int stbrp_coord; |
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
typedef struct |
| 2612 |
{ |
| 2613 |
int width,height; |
| 2614 |
int x,y,bottom_y; |
| 2615 |
} stbrp_context; |
| 2616 |
|
| 2617 |
typedef struct |
| 2618 |
{ |
| 2619 |
unsigned char x; |
| 2620 |
} stbrp_node; |
| 2621 |
|
| 2622 |
struct stbrp_rect |
| 2623 |
{ |
| 2624 |
stbrp_coord x,y; |
| 2625 |
int id,w,h,was_packed; |
| 2626 |
}; |
| 2627 |
|
| 2628 |
static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes) |
| 2629 |
{ |
| 2630 |
con->width = pw; |
| 2631 |
con->height = ph; |
| 2632 |
con->x = 0; |
| 2633 |
con->y = 0; |
| 2634 |
con->bottom_y = 0; |
| 2635 |
STBTT__NOTUSED(nodes); |
| 2636 |
STBTT__NOTUSED(num_nodes); |
| 2637 |
} |
| 2638 |
|
| 2639 |
static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects) |
| 2640 |
{ |
| 2641 |
int i; |
| 2642 |
for (i=0; i < num_rects; ++i) { |
| 2643 |
if (con->x + rects[i].w > con->width) { |
| 2644 |
con->x = 0; |
| 2645 |
con->y = con->bottom_y; |
| 2646 |
} |
| 2647 |
if (con->y + rects[i].h > con->height) |
| 2648 |
break; |
| 2649 |
rects[i].x = con->x; |
| 2650 |
rects[i].y = con->y; |
| 2651 |
rects[i].was_packed = 1; |
| 2652 |
con->x += rects[i].w; |
| 2653 |
if (con->y + rects[i].h > con->bottom_y) |
| 2654 |
con->bottom_y = con->y + rects[i].h; |
| 2655 |
} |
| 2656 |
for ( ; i < num_rects; ++i) |
| 2657 |
rects[i].was_packed = 0; |
| 2658 |
} |
| 2659 |
#endif |
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
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) |
| 2669 |
{ |
| 2670 |
stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context); |
| 2671 |
int num_nodes = pw - padding; |
| 2672 |
stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context); |
| 2673 |
|
| 2674 |
if (context == NULL || nodes == NULL) { |
| 2675 |
if (context != NULL) STBTT_free(context, alloc_context); |
| 2676 |
if (nodes != NULL) STBTT_free(nodes , alloc_context); |
| 2677 |
return 0; |
| 2678 |
} |
| 2679 |
|
| 2680 |
spc->user_allocator_context = alloc_context; |
| 2681 |
spc->width = pw; |
| 2682 |
spc->height = ph; |
| 2683 |
spc->pixels = pixels; |
| 2684 |
spc->pack_info = context; |
| 2685 |
spc->nodes = nodes; |
| 2686 |
spc->padding = padding; |
| 2687 |
spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw; |
| 2688 |
spc->h_oversample = 1; |
| 2689 |
spc->v_oversample = 1; |
| 2690 |
|
| 2691 |
stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes); |
| 2692 |
|
| 2693 |
if (pixels) |
| 2694 |
STBTT_memset(pixels, 0, pw*ph); |
| 2695 |
|
| 2696 |
return 1; |
| 2697 |
} |
| 2698 |
|
| 2699 |
STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc) |
| 2700 |
{ |
| 2701 |
STBTT_free(spc->nodes , spc->user_allocator_context); |
| 2702 |
STBTT_free(spc->pack_info, spc->user_allocator_context); |
| 2703 |
} |
| 2704 |
|
| 2705 |
STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample) |
| 2706 |
{ |
| 2707 |
STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE); |
| 2708 |
STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE); |
| 2709 |
if (h_oversample <= STBTT_MAX_OVERSAMPLE) |
| 2710 |
spc->h_oversample = h_oversample; |
| 2711 |
if (v_oversample <= STBTT_MAX_OVERSAMPLE) |
| 2712 |
spc->v_oversample = v_oversample; |
| 2713 |
} |
| 2714 |
|
| 2715 |
#define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1) |
| 2716 |
|
| 2717 |
static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) |
| 2718 |
{ |
| 2719 |
unsigned char buffer[STBTT_MAX_OVERSAMPLE]; |
| 2720 |
int safe_w = w - kernel_width; |
| 2721 |
int j; |
| 2722 |
STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); |
| 2723 |
for (j=0; j < h; ++j) { |
| 2724 |
int i; |
| 2725 |
unsigned int total; |
| 2726 |
STBTT_memset(buffer, 0, kernel_width); |
| 2727 |
|
| 2728 |
total = 0; |
| 2729 |
|
| 2730 |
|
| 2731 |
switch (kernel_width) { |
| 2732 |
case 2: |
| 2733 |
for (i=0; i <= safe_w; ++i) { |
| 2734 |
total += pixels[i] - buffer[i & STBTT__OVER_MASK]; |
| 2735 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; |
| 2736 |
pixels[i] = (unsigned char) (total / 2); |
| 2737 |
} |
| 2738 |
break; |
| 2739 |
case 3: |
| 2740 |
for (i=0; i <= safe_w; ++i) { |
| 2741 |
total += pixels[i] - buffer[i & STBTT__OVER_MASK]; |
| 2742 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; |
| 2743 |
pixels[i] = (unsigned char) (total / 3); |
| 2744 |
} |
| 2745 |
break; |
| 2746 |
case 4: |
| 2747 |
for (i=0; i <= safe_w; ++i) { |
| 2748 |
total += pixels[i] - buffer[i & STBTT__OVER_MASK]; |
| 2749 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; |
| 2750 |
pixels[i] = (unsigned char) (total / 4); |
| 2751 |
} |
| 2752 |
break; |
| 2753 |
case 5: |
| 2754 |
for (i=0; i <= safe_w; ++i) { |
| 2755 |
total += pixels[i] - buffer[i & STBTT__OVER_MASK]; |
| 2756 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; |
| 2757 |
pixels[i] = (unsigned char) (total / 5); |
| 2758 |
} |
| 2759 |
break; |
| 2760 |
default: |
| 2761 |
for (i=0; i <= safe_w; ++i) { |
| 2762 |
total += pixels[i] - buffer[i & STBTT__OVER_MASK]; |
| 2763 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; |
| 2764 |
pixels[i] = (unsigned char) (total / kernel_width); |
| 2765 |
} |
| 2766 |
break; |
| 2767 |
} |
| 2768 |
|
| 2769 |
for (; i < w; ++i) { |
| 2770 |
STBTT_assert(pixels[i] == 0); |
| 2771 |
total -= buffer[i & STBTT__OVER_MASK]; |
| 2772 |
pixels[i] = (unsigned char) (total / kernel_width); |
| 2773 |
} |
| 2774 |
|
| 2775 |
pixels += stride_in_bytes; |
| 2776 |
} |
| 2777 |
} |
| 2778 |
|
| 2779 |
static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) |
| 2780 |
{ |
| 2781 |
unsigned char buffer[STBTT_MAX_OVERSAMPLE]; |
| 2782 |
int safe_h = h - kernel_width; |
| 2783 |
int j; |
| 2784 |
STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); |
| 2785 |
for (j=0; j < w; ++j) { |
| 2786 |
int i; |
| 2787 |
unsigned int total; |
| 2788 |
STBTT_memset(buffer, 0, kernel_width); |
| 2789 |
|
| 2790 |
total = 0; |
| 2791 |
|
| 2792 |
|
| 2793 |
switch (kernel_width) { |
| 2794 |
case 2: |
| 2795 |
for (i=0; i <= safe_h; ++i) { |
| 2796 |
total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; |
| 2797 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; |
| 2798 |
pixels[i*stride_in_bytes] = (unsigned char) (total / 2); |
| 2799 |
} |
| 2800 |
break; |
| 2801 |
case 3: |
| 2802 |
for (i=0; i <= safe_h; ++i) { |
| 2803 |
total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; |
| 2804 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; |
| 2805 |
pixels[i*stride_in_bytes] = (unsigned char) (total / 3); |
| 2806 |
} |
| 2807 |
break; |
| 2808 |
case 4: |
| 2809 |
for (i=0; i <= safe_h; ++i) { |
| 2810 |
total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; |
| 2811 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; |
| 2812 |
pixels[i*stride_in_bytes] = (unsigned char) (total / 4); |
| 2813 |
} |
| 2814 |
break; |
| 2815 |
case 5: |
| 2816 |
for (i=0; i <= safe_h; ++i) { |
| 2817 |
total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; |
| 2818 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; |
| 2819 |
pixels[i*stride_in_bytes] = (unsigned char) (total / 5); |
| 2820 |
} |
| 2821 |
break; |
| 2822 |
default: |
| 2823 |
for (i=0; i <= safe_h; ++i) { |
| 2824 |
total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; |
| 2825 |
buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; |
| 2826 |
pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); |
| 2827 |
} |
| 2828 |
break; |
| 2829 |
} |
| 2830 |
|
| 2831 |
for (; i < h; ++i) { |
| 2832 |
STBTT_assert(pixels[i*stride_in_bytes] == 0); |
| 2833 |
total -= buffer[i & STBTT__OVER_MASK]; |
| 2834 |
pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); |
| 2835 |
} |
| 2836 |
|
| 2837 |
pixels += 1; |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
static float stbtt__oversample_shift(int oversample) |
| 2842 |
{ |
| 2843 |
if (!oversample) |
| 2844 |
return 0.0f; |
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
return (float)-(oversample - 1) / (2.0f * (float)oversample); |
| 2851 |
} |
| 2852 |
|
| 2853 |
|
| 2854 |
STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) |
| 2855 |
{ |
| 2856 |
int i,j,k; |
| 2857 |
|
| 2858 |
k=0; |
| 2859 |
for (i=0; i < num_ranges; ++i) { |
| 2860 |
float fh = ranges[i].font_size; |
| 2861 |
float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); |
| 2862 |
ranges[i].h_oversample = (unsigned char) spc->h_oversample; |
| 2863 |
ranges[i].v_oversample = (unsigned char) spc->v_oversample; |
| 2864 |
for (j=0; j < ranges[i].num_chars; ++j) { |
| 2865 |
int x0,y0,x1,y1; |
| 2866 |
int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; |
| 2867 |
int glyph = stbtt_FindGlyphIndex(info, codepoint); |
| 2868 |
stbtt_GetGlyphBitmapBoxSubpixel(info,glyph, |
| 2869 |
scale * spc->h_oversample, |
| 2870 |
scale * spc->v_oversample, |
| 2871 |
0,0, |
| 2872 |
&x0,&y0,&x1,&y1); |
| 2873 |
rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1); |
| 2874 |
rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1); |
| 2875 |
++k; |
| 2876 |
} |
| 2877 |
} |
| 2878 |
|
| 2879 |
return k; |
| 2880 |
} |
| 2881 |
|
| 2882 |
|
| 2883 |
STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) |
| 2884 |
{ |
| 2885 |
int i,j,k, return_value = 1; |
| 2886 |
|
| 2887 |
|
| 2888 |
int old_h_over = spc->h_oversample; |
| 2889 |
int old_v_over = spc->v_oversample; |
| 2890 |
|
| 2891 |
k = 0; |
| 2892 |
for (i=0; i < num_ranges; ++i) { |
| 2893 |
float fh = ranges[i].font_size; |
| 2894 |
float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); |
| 2895 |
float recip_h,recip_v,sub_x,sub_y; |
| 2896 |
spc->h_oversample = ranges[i].h_oversample; |
| 2897 |
spc->v_oversample = ranges[i].v_oversample; |
| 2898 |
recip_h = 1.0f / spc->h_oversample; |
| 2899 |
recip_v = 1.0f / spc->v_oversample; |
| 2900 |
sub_x = stbtt__oversample_shift(spc->h_oversample); |
| 2901 |
sub_y = stbtt__oversample_shift(spc->v_oversample); |
| 2902 |
for (j=0; j < ranges[i].num_chars; ++j) { |
| 2903 |
stbrp_rect *r = &rects[k]; |
| 2904 |
if (r->was_packed) { |
| 2905 |
stbtt_packedchar *bc = &ranges[i].chardata_for_range[j]; |
| 2906 |
int advance, lsb, x0,y0,x1,y1; |
| 2907 |
int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; |
| 2908 |
int glyph = stbtt_FindGlyphIndex(info, codepoint); |
| 2909 |
stbrp_coord pad = (stbrp_coord) spc->padding; |
| 2910 |
|
| 2911 |
|
| 2912 |
r->x += pad; |
| 2913 |
r->y += pad; |
| 2914 |
r->w -= pad; |
| 2915 |
r->h -= pad; |
| 2916 |
stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb); |
| 2917 |
stbtt_GetGlyphBitmapBox(info, glyph, |
| 2918 |
scale * spc->h_oversample, |
| 2919 |
scale * spc->v_oversample, |
| 2920 |
&x0,&y0,&x1,&y1); |
| 2921 |
stbtt_MakeGlyphBitmapSubpixel(info, |
| 2922 |
spc->pixels + r->x + r->y*spc->stride_in_bytes, |
| 2923 |
r->w - spc->h_oversample+1, |
| 2924 |
r->h - spc->v_oversample+1, |
| 2925 |
spc->stride_in_bytes, |
| 2926 |
scale * spc->h_oversample, |
| 2927 |
scale * spc->v_oversample, |
| 2928 |
0,0, |
| 2929 |
glyph); |
| 2930 |
|
| 2931 |
if (spc->h_oversample > 1) |
| 2932 |
stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, |
| 2933 |
r->w, r->h, spc->stride_in_bytes, |
| 2934 |
spc->h_oversample); |
| 2935 |
|
| 2936 |
if (spc->v_oversample > 1) |
| 2937 |
stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, |
| 2938 |
r->w, r->h, spc->stride_in_bytes, |
| 2939 |
spc->v_oversample); |
| 2940 |
|
| 2941 |
bc->x0 = (stbtt_int16) r->x; |
| 2942 |
bc->y0 = (stbtt_int16) r->y; |
| 2943 |
bc->x1 = (stbtt_int16) (r->x + r->w); |
| 2944 |
bc->y1 = (stbtt_int16) (r->y + r->h); |
| 2945 |
bc->xadvance = scale * advance; |
| 2946 |
bc->xoff = (float) x0 * recip_h + sub_x; |
| 2947 |
bc->yoff = (float) y0 * recip_v + sub_y; |
| 2948 |
bc->xoff2 = (x0 + r->w) * recip_h + sub_x; |
| 2949 |
bc->yoff2 = (y0 + r->h) * recip_v + sub_y; |
| 2950 |
} else { |
| 2951 |
return_value = 0; |
| 2952 |
} |
| 2953 |
|
| 2954 |
++k; |
| 2955 |
} |
| 2956 |
} |
| 2957 |
|
| 2958 |
|
| 2959 |
spc->h_oversample = old_h_over; |
| 2960 |
spc->v_oversample = old_v_over; |
| 2961 |
|
| 2962 |
return return_value; |
| 2963 |
} |
| 2964 |
|
| 2965 |
STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects) |
| 2966 |
{ |
| 2967 |
stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects); |
| 2968 |
} |
| 2969 |
|
| 2970 |
STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges) |
| 2971 |
{ |
| 2972 |
stbtt_fontinfo info; |
| 2973 |
int i,j,n, return_value = 1; |
| 2974 |
|
| 2975 |
stbrp_rect *rects; |
| 2976 |
|
| 2977 |
|
| 2978 |
for (i=0; i < num_ranges; ++i) |
| 2979 |
for (j=0; j < ranges[i].num_chars; ++j) |
| 2980 |
ranges[i].chardata_for_range[j].x0 = |
| 2981 |
ranges[i].chardata_for_range[j].y0 = |
| 2982 |
ranges[i].chardata_for_range[j].x1 = |
| 2983 |
ranges[i].chardata_for_range[j].y1 = 0; |
| 2984 |
|
| 2985 |
n = 0; |
| 2986 |
for (i=0; i < num_ranges; ++i) |
| 2987 |
n += ranges[i].num_chars; |
| 2988 |
|
| 2989 |
rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context); |
| 2990 |
if (rects == NULL) |
| 2991 |
return 0; |
| 2992 |
|
| 2993 |
info.userdata = spc->user_allocator_context; |
| 2994 |
stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index)); |
| 2995 |
|
| 2996 |
n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects); |
| 2997 |
|
| 2998 |
stbtt_PackFontRangesPackRects(spc, rects, n); |
| 2999 |
|
| 3000 |
return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects); |
| 3001 |
|
| 3002 |
STBTT_free(rects, spc->user_allocator_context); |
| 3003 |
return return_value; |
| 3004 |
} |
| 3005 |
|
| 3006 |
STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size, |
| 3007 |
int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range) |
| 3008 |
{ |
| 3009 |
stbtt_pack_range range; |
| 3010 |
range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range; |
| 3011 |
range.array_of_unicode_codepoints = NULL; |
| 3012 |
range.num_chars = num_chars_in_range; |
| 3013 |
range.chardata_for_range = chardata_for_range; |
| 3014 |
range.font_size = font_size; |
| 3015 |
return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1); |
| 3016 |
} |
| 3017 |
|
| 3018 |
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) |
| 3019 |
{ |
| 3020 |
float ipw = 1.0f / pw, iph = 1.0f / ph; |
| 3021 |
stbtt_packedchar *b = chardata + char_index; |
| 3022 |
|
| 3023 |
if (align_to_integer) { |
| 3024 |
float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f); |
| 3025 |
float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f); |
| 3026 |
q->x0 = x; |
| 3027 |
q->y0 = y; |
| 3028 |
q->x1 = x + b->xoff2 - b->xoff; |
| 3029 |
q->y1 = y + b->yoff2 - b->yoff; |
| 3030 |
} else { |
| 3031 |
q->x0 = *xpos + b->xoff; |
| 3032 |
q->y0 = *ypos + b->yoff; |
| 3033 |
q->x1 = *xpos + b->xoff2; |
| 3034 |
q->y1 = *ypos + b->yoff2; |
| 3035 |
} |
| 3036 |
|
| 3037 |
q->s0 = b->x0 * ipw; |
| 3038 |
q->t0 = b->y0 * iph; |
| 3039 |
q->s1 = b->x1 * ipw; |
| 3040 |
q->t1 = b->y1 * iph; |
| 3041 |
|
| 3042 |
*xpos += b->xadvance; |
| 3043 |
} |
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(const stbtt_uint8 *s1, stbtt_int32 len1, const stbtt_uint8 *s2, stbtt_int32 len2) |
| 3053 |
{ |
| 3054 |
stbtt_int32 i=0; |
| 3055 |
|
| 3056 |
|
| 3057 |
while (len2) { |
| 3058 |
stbtt_uint16 ch = s2[0]*256 + s2[1]; |
| 3059 |
if (ch < 0x80) { |
| 3060 |
if (i >= len1) return -1; |
| 3061 |
if (s1[i++] != ch) return -1; |
| 3062 |
} else if (ch < 0x800) { |
| 3063 |
if (i+1 >= len1) return -1; |
| 3064 |
if (s1[i++] != 0xc0 + (ch >> 6)) return -1; |
| 3065 |
if (s1[i++] != 0x80 + (ch & 0x3f)) return -1; |
| 3066 |
} else if (ch >= 0xd800 && ch < 0xdc00) { |
| 3067 |
stbtt_uint32 c; |
| 3068 |
stbtt_uint16 ch2 = s2[2]*256 + s2[3]; |
| 3069 |
if (i+3 >= len1) return -1; |
| 3070 |
c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000; |
| 3071 |
if (s1[i++] != 0xf0 + (c >> 18)) return -1; |
| 3072 |
if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1; |
| 3073 |
if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1; |
| 3074 |
if (s1[i++] != 0x80 + ((c ) & 0x3f)) return -1; |
| 3075 |
s2 += 2; |
| 3076 |
len2 -= 2; |
| 3077 |
} else if (ch >= 0xdc00 && ch < 0xe000) { |
| 3078 |
return -1; |
| 3079 |
} else { |
| 3080 |
if (i+2 >= len1) return -1; |
| 3081 |
if (s1[i++] != 0xe0 + (ch >> 12)) return -1; |
| 3082 |
if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1; |
| 3083 |
if (s1[i++] != 0x80 + ((ch ) & 0x3f)) return -1; |
| 3084 |
} |
| 3085 |
s2 += 2; |
| 3086 |
len2 -= 2; |
| 3087 |
} |
| 3088 |
return i; |
| 3089 |
} |
| 3090 |
|
| 3091 |
STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2) |
| 3092 |
{ |
| 3093 |
return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((const stbtt_uint8*) s1, len1, (const stbtt_uint8*) s2, len2); |
| 3094 |
} |
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID) |
| 3099 |
{ |
| 3100 |
stbtt_int32 i,count,stringOffset; |
| 3101 |
stbtt_uint8 *fc = font->data; |
| 3102 |
stbtt_uint32 offset = font->fontstart; |
| 3103 |
stbtt_uint32 nm = stbtt__find_table(fc, offset, "name"); |
| 3104 |
if (!nm) return NULL; |
| 3105 |
|
| 3106 |
count = ttUSHORT(fc+nm+2); |
| 3107 |
stringOffset = nm + ttUSHORT(fc+nm+4); |
| 3108 |
for (i=0; i < count; ++i) { |
| 3109 |
stbtt_uint32 loc = nm + 6 + 12 * i; |
| 3110 |
if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2) |
| 3111 |
&& languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) { |
| 3112 |
*length = ttUSHORT(fc+loc+8); |
| 3113 |
return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10)); |
| 3114 |
} |
| 3115 |
} |
| 3116 |
return NULL; |
| 3117 |
} |
| 3118 |
|
| 3119 |
static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id) |
| 3120 |
{ |
| 3121 |
stbtt_int32 i; |
| 3122 |
stbtt_int32 count = ttUSHORT(fc+nm+2); |
| 3123 |
stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4); |
| 3124 |
|
| 3125 |
for (i=0; i < count; ++i) { |
| 3126 |
stbtt_uint32 loc = nm + 6 + 12 * i; |
| 3127 |
stbtt_int32 id = ttUSHORT(fc+loc+6); |
| 3128 |
if (id == target_id) { |
| 3129 |
|
| 3130 |
stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4); |
| 3131 |
|
| 3132 |
|
| 3133 |
if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) { |
| 3134 |
stbtt_int32 slen = ttUSHORT(fc+loc+8); |
| 3135 |
stbtt_int32 off = ttUSHORT(fc+loc+10); |
| 3136 |
|
| 3137 |
|
| 3138 |
stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen); |
| 3139 |
if (matchlen >= 0) { |
| 3140 |
|
| 3141 |
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) { |
| 3142 |
slen = ttUSHORT(fc+loc+12+8); |
| 3143 |
off = ttUSHORT(fc+loc+12+10); |
| 3144 |
if (slen == 0) { |
| 3145 |
if (matchlen == nlen) |
| 3146 |
return 1; |
| 3147 |
} else if (matchlen < nlen && name[matchlen] == ' ') { |
| 3148 |
++matchlen; |
| 3149 |
if (stbtt_CompareUTF8toUTF16_bigendian((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen)) |
| 3150 |
return 1; |
| 3151 |
} |
| 3152 |
} else { |
| 3153 |
|
| 3154 |
if (matchlen == nlen) |
| 3155 |
return 1; |
| 3156 |
} |
| 3157 |
} |
| 3158 |
} |
| 3159 |
|
| 3160 |
|
| 3161 |
} |
| 3162 |
} |
| 3163 |
return 0; |
| 3164 |
} |
| 3165 |
|
| 3166 |
static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags) |
| 3167 |
{ |
| 3168 |
stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name); |
| 3169 |
stbtt_uint32 nm,hd; |
| 3170 |
if (!stbtt__isfont(fc+offset)) return 0; |
| 3171 |
|
| 3172 |
|
| 3173 |
if (flags) { |
| 3174 |
hd = stbtt__find_table(fc, offset, "head"); |
| 3175 |
if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0; |
| 3176 |
} |
| 3177 |
|
| 3178 |
nm = stbtt__find_table(fc, offset, "name"); |
| 3179 |
if (!nm) return 0; |
| 3180 |
|
| 3181 |
if (flags) { |
| 3182 |
|
| 3183 |
if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1; |
| 3184 |
if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1; |
| 3185 |
if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; |
| 3186 |
} else { |
| 3187 |
if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1; |
| 3188 |
if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1; |
| 3189 |
if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; |
| 3190 |
} |
| 3191 |
|
| 3192 |
return 0; |
| 3193 |
} |
| 3194 |
|
| 3195 |
STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *font_collection, const char *name_utf8, stbtt_int32 flags) |
| 3196 |
{ |
| 3197 |
stbtt_int32 i; |
| 3198 |
for (i=0;;++i) { |
| 3199 |
stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i); |
| 3200 |
if (off < 0) return off; |
| 3201 |
if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags)) |
| 3202 |
return off; |
| 3203 |
} |
| 3204 |
} |
| 3205 |
|
| 3206 |
#endif |
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
|
| 3215 |
|
| 3216 |
|
| 3217 |
|
| 3218 |
|
| 3219 |
|
| 3220 |
|
| 3221 |
|
| 3222 |
|
| 3223 |
|
| 3224 |
|
| 3225 |
|
| 3226 |
|
| 3227 |
|
| 3228 |
|
| 3229 |
|
| 3230 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
|
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
|