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
|
/* Standalone Linux font regression/specimen. See vendor/fonts/README.md. */
#include "../vendor/text.c"
#include <assert.h>
#include <stdio.h>
static unsigned glyphs(PangoLayout *layout) {
unsigned count = 0;
PangoLayoutIter *it = pango_layout_get_iter(layout);
do {
PangoLayoutRun *run = pango_layout_iter_get_run_readonly(it);
if (run) count += run->glyphs->num_glyphs;
} while (pango_layout_iter_next_run(it));
pango_layout_iter_free(it);
return count;
}
int main(int argc, char **argv) {
assert(SDL_Init(0));
SDL_Surface *surface = SDL_CreateSurface(1440, 820, SDL_PIXELFORMAT_ARGB8888);
SDL_Renderer *r = SDL_CreateSoftwareRenderer(surface);
assert(r);
SDL_SetRenderDrawColor(r, 17, 21, 27, 255); SDL_RenderClear(r);
sk_text_begin_frame(r);
const char *samples[] = {
"Go Mono / native shaped text",
"Pardes / 46.8M accesses / 3.97M calls",
"0123456789 0x7ffed849acf0 () [] {} <> -> != ==",
"Latin: cafe\xcc\x81 / café / naïve / fi fl ffi",
"Greek / Cyrillic: λ → Ω Привет мир",
"Japanese fallback: 日本語 ひらがな カタカナ",
"Arabic shaping: العربية لا السلام عليكم",
"Hebrew bidi: שלום עולם | English 123",
"Symbols / emoji: ✓ ⌘ ⌥ ⌫ ⌜ ⌟ 😀 ★ ♥",
"draw.render → body_layer.renderBody → TerminalImages.draw"
};
for (unsigned i=0; i<sizeof(samples)/sizeof(*samples); ++i) {
assert(sk_text_draw(r, 30, 25 + 53*i, i==0?2.2f:1.6f, samples[i], strlen(samples[i]),
i==0?228:165,i==0?234:178,i==0?242:195,255));
TextCache *c=cache_for(r);
assert(pango_layout_get_unknown_glyphs_count(c->layout)==0);
}
TextCache *c=cache_for(r);
assert(sk_text_draw(r,30,575,1.0f,"e\xcc\x81",3,228,234,242,255));
PangoRectangle combining, plain;
pango_layout_get_pixel_extents(c->layout,NULL,&combining);
assert(sk_text_draw(r,50,575,1.0f,"e",1,228,234,242,255));
pango_layout_get_pixel_extents(c->layout,NULL,&plain);
assert(combining.width==plain.width);
assert(sk_text_draw(r,70,575,1.0f,"لا",strlen("لا"),228,234,242,255));
assert(glyphs(c->layout)==1);
assert(sk_text_draw(r,120,575,1.0f,"0123456789",10,228,234,242,255));
PangoRectangle bounds; pango_layout_get_pixel_extents(c->layout,NULL,&bounds);
assert(bounds.width>=75 && bounds.width<=85 && bounds.height>=13);
printf("scale 1: 10-digit advance %dpx, line box %dpx\n",bounds.width,bounds.height);
SDL_Rect clip={30,620,400,30}; SDL_SetRenderClipRect(r,&clip);
assert(sk_text_draw(r,30,617,1.6f,"Clipping preserves the baseline and glyph edges outside this box",strlen("Clipping preserves the baseline and glyph edges outside this box"),112,183,255,255));
SDL_SetRenderClipRect(r,NULL);
assert(SDL_RenderPresent(r));
if(argc>1) assert(SDL_SavePNG(surface,argv[1]));
assert(SDL_SetRenderLogicalPresentation(r,720,410,SDL_LOGICAL_PRESENTATION_LETTERBOX));
assert(fabsf(density(r)-2)<0.01f);
assert(sk_text_draw(r,10,10,1.0f,"0123456789",10,228,234,242,255));
pango_layout_get_pixel_extents(c->layout,NULL,&bounds);
assert(bounds.width>=150 && bounds.width<=170);
/* Eviction across frames and exhaustion inside one frame both stay bounded. */
for(unsigned i=0;i<800;i++) {
if(i%100==0) sk_text_begin_frame(r);
char text[32]; int n=snprintf(text,sizeof(text),"cache %u",i);
assert(sk_text_draw(r,0,0,1,text,(size_t)n,255,255,255,255));
assert(c->bytes<=CACHE_BYTES);
}
for(unsigned i=0;i<600;i++) {
char text[32]; int n=snprintf(text,sizeof(text),"transient %u",i);
assert(sk_text_draw(r,0,0,1,text,(size_t)n,255,255,255,255));
assert(c->bytes<=CACHE_BYTES);
}
SDL_DestroyRenderer(r); SDL_DestroySurface(surface); SDL_Quit();
puts("Go Mono, fallback, combining marks, Arabic shaping, 2x DPI, clipping and bounded cache passed");
}
|