test_truetype.c
This file
Path
tests/test_truetype.c
154 lines · 4.3 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1#ifndef _CRT_SECURE_NO_WARNINGS2// Fixes Compile Errors for Visual Studio 2005 or newer3 #define _CRT_SECURE_NO_WARNINGS4#endif56#include <stdlib.h>78// this isn't meant to compile standalone; link with test_c_compilation.c as well9#include "stb_rect_pack.h"10#define STB_TRUETYPE_IMPLEMENTATION11#include "stb_truetype.h"12#include "stb_image_write.h"1314#ifdef TT_TEST1516#include <stdio.h>1718unsigned char ttf_buffer[1 << 25];19unsigned char output[512*100];2021void debug(void)22{23 stbtt_fontinfo font;24 fread(ttf_buffer, 1, 1<<25, fopen("c:/x/lm/LiberationMono-Regular.ttf", "rb"));25 stbtt_InitFont(&font, ttf_buffer, 0);2627 stbtt_MakeGlyphBitmap(&font, output, 6, 9, 512, 5.172414E-03f, 5.172414E-03f, 54);28}2930#define BITMAP_W 25631#define BITMAP_H 51232unsigned char temp_bitmap[BITMAP_H][BITMAP_W];33stbtt_bakedchar cdata[256*2]; // ASCII 32..126 is 95 glyphs34stbtt_packedchar pdata[256*2];3536int main(int argc, char **argv)37{38 stbtt_fontinfo font;39 unsigned char *bitmap;40 int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : '@'), s = (argc > 2 ? atoi(argv[2]) : 32);4142 //debug();4344 // @TODO: why is minglui.ttc failing? 45 //fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/mingliu.ttc", "rb"));4647 fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/DejaVuSans.ttf", "rb"));4849 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));5051#if 052 {53 stbtt__bitmap b;54 stbtt__point p[2];55 int wcount[2] = { 2,0 };56 p[0].x = 0.2f;57 p[0].y = 0.3f;58 p[1].x = 3.8f;59 p[1].y = 0.8f;60 b.w = 16;61 b.h = 2;62 b.stride = 16;63 b.pixels = malloc(b.w*b.h);64 stbtt__rasterize(&b, p, wcount, 1, 1, 1, 0, 0, 0, 0, 0, NULL);65 for (i=0; i < 8; ++i)66 printf("%f\n", b.pixels[i]/255.0);67 }68#endif6970#if 171 {72 static stbtt_pack_context pc;73 static stbtt_packedchar cd[256];74 static unsigned char atlas[1024*1024];7576 stbtt_PackBegin(&pc, atlas, 1024,1024,1024,1,NULL);77 stbtt_PackFontRange(&pc, ttf_buffer, 0, 32.0, 0, 256, cd);78 stbtt_PackEnd(&pc);79 }80#endif818283#if 184 {85 static stbtt_pack_context pc;86 static stbtt_packedchar cd[256];87 static unsigned char atlas[1024*1024];88 unsigned char *data;8990 data = stbtt_GetCodepointSDF(&font, stbtt_ScaleForPixelHeight(&font,32.0), 'u', 4, 128, 128/4, &w,&h,&i,&j);91 for (j=0; j < h; ++j) {92 for (i=0; i < w; ++i) {93 putchar(" .:ioVM@"[data[j*w+i]>>5]);94 }95 putchar('\n');96 }97 }98#endif99100#if 0101 stbtt_BakeFontBitmap(ttf_buffer,stbtt_GetFontOffsetForIndex(ttf_buffer,0), 40.0, temp_bitmap[0],BITMAP_W,BITMAP_H, 32,96, cdata); // no guarantee this fits!102 stbi_write_png("fonttest1.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);103104 {105 stbtt_pack_context pc;106 stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, 1, NULL);107 stbtt_PackFontRange(&pc, ttf_buffer, 0, 20.0, 32, 95, pdata);108 stbtt_PackFontRange(&pc, ttf_buffer, 0, 20.0, 0xa0, 0x100-0xa0, pdata);109 stbtt_PackEnd(&pc);110 stbi_write_png("fonttest2.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);111 }112113 {114 stbtt_pack_context pc;115 stbtt_pack_range pr[2];116 stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, 1, NULL);117118 pr[0].chardata_for_range = pdata;119 pr[0].array_of_unicode_codepoints = NULL;120 pr[0].first_unicode_codepoint_in_range = 32;121 pr[0].num_chars = 95;122 pr[0].font_size = 20.0f;123 pr[1].chardata_for_range = pdata+256;124 pr[1].array_of_unicode_codepoints = NULL;125 pr[1].first_unicode_codepoint_in_range = 0xa0;126 pr[1].num_chars = 0x100 - 0xa0;127 pr[1].font_size = 20.0f;128129 stbtt_PackSetOversampling(&pc, 2, 2);130 stbtt_PackFontRanges(&pc, ttf_buffer, 0, pr, 2);131 stbtt_PackEnd(&pc);132 stbi_write_png("fonttest3.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);133 }134 return 0;135#endif136137 (void)stbtt_GetCodepointBitmapSubpixel(&font,138 0.4972374737262726f,139 0.4986416995525360f,140 0.2391788959503174f,141 0.1752119064331055f,142 'd',143 &w, &h,144 0,0);145146 bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, (float)s), c, &w, &h, 0,0);147 for (j=0; j < h; ++j) {148 for (i=0; i < w; ++i)149 putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);150 putchar('\n');151 }152 return 0;153}154#endif