-
Notifications
You must be signed in to change notification settings - Fork 60
/
ex-sdl-cairo-freetype-harfbuzz.c
296 lines (246 loc) · 10 KB
/
ex-sdl-cairo-freetype-harfbuzz.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <SDL.h>
#include <SDL_image.h>
#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/ftadvanc.h>
#include <freetype/ftsnames.h>
#include <freetype/tttables.h>
#include <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>
#include <harfbuzz/hb-icu.h> /* Alternatively you can use hb-glib.h */
#include <cairo/cairo.h>
#include <cairo/cairo-ft.h>
#define NUM_EXAMPLES 3
#define FONT_SIZE 40
const char *texts[NUM_EXAMPLES] = {
"This is some english text",
"هذه بعض النصوص العربية",
"這是一些中文",
};
const int text_directions[NUM_EXAMPLES] = {
HB_DIRECTION_LTR,
HB_DIRECTION_RTL,
HB_DIRECTION_TTB,
};
/* XXX: I don't know if these are correct or not, it doesn't seem to break anything
* regardless of their value. */
const char *languages[NUM_EXAMPLES] = {
"en",
"ar",
"ch",
};
const hb_script_t scripts[NUM_EXAMPLES] = {
HB_SCRIPT_LATIN,
HB_SCRIPT_ARABIC,
HB_SCRIPT_HAN,
};
enum {
ENGLISH=0, ARABIC, CHINESE
};
int main () {
/* Init font config */
FcInit(); //initializes Fontconfig
FcConfig* config = FcInitLoadConfigAndFonts();
assert(config);
FcPattern* pat = FcNameParse((const FcChar8*)"DejaVu Serif");
/* Increase the possible matches */
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcResult result;
FcPattern* font = FcFontMatch(config, pat, &result);
assert(font);
/* Will be freed with font */
FcChar8* file = NULL;
assert(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch);
/* Init freetype */
FT_Library ft_library;
assert(!FT_Init_FreeType(&ft_library));
/* Load our fonts */
FT_Face ft_face[NUM_EXAMPLES];
assert(!FT_New_Face(ft_library, (char *)file, 0, &ft_face[ENGLISH]));
assert(!FT_Set_Char_Size(ft_face[ENGLISH],FONT_SIZE*64, FONT_SIZE*64,0,0));
assert(!FT_New_Face(ft_library, "fonts/amiri-0.104/amiri-regular.ttf", 0, &ft_face[ARABIC]));
assert(!FT_Set_Char_Size(ft_face[ARABIC],FONT_SIZE*64, FONT_SIZE*64,0,0 ));
assert(!FT_New_Face(ft_library, "fonts/fireflysung-1.3.0/fireflysung.ttf", 0, &ft_face[CHINESE]));
assert(!FT_Set_Char_Size(ft_face[CHINESE],FONT_SIZE*64, FONT_SIZE*64,0,0 ));
/* Cleanup font config */
FcPatternDestroy(font);
FcPatternDestroy(pat);
FcConfigDestroy(config);
FcFini();
/* Get our cairo font structs */
cairo_font_face_t *cairo_ft_face[NUM_EXAMPLES];
cairo_ft_face[ENGLISH] = cairo_ft_font_face_create_for_ft_face(ft_face[ENGLISH], 0);
cairo_ft_face[ARABIC] = cairo_ft_font_face_create_for_ft_face(ft_face[ARABIC], 0);
cairo_ft_face[CHINESE] = cairo_ft_font_face_create_for_ft_face(ft_face[CHINESE], 0);
/* Get our harfbuzz font/face structs */
hb_font_t *hb_ft_font[NUM_EXAMPLES];
hb_face_t *hb_ft_face[NUM_EXAMPLES];
hb_ft_font[ENGLISH] = hb_ft_font_create(ft_face[ENGLISH], NULL);
hb_ft_face[ENGLISH] = hb_ft_face_create(ft_face[ENGLISH], NULL);
hb_ft_font[ARABIC] = hb_ft_font_create(ft_face[ARABIC] , NULL);
hb_ft_face[ARABIC] = hb_ft_face_create(ft_face[ARABIC] , NULL);
hb_ft_font[CHINESE] = hb_ft_font_create(ft_face[CHINESE], NULL);
hb_ft_face[CHINESE] = hb_ft_face_create(ft_face[CHINESE], NULL);
/* Setup our SDL window */
int width = 800;
int height = 600;
int videoFlags = SDL_WINDOW_RESIZABLE ;
/* Initialize our SDL window */
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Failed to initialize SDL");
return -1;
}
SDL_Window *screen = SDL_CreateWindow(
"\"Simple\" SDL+Cairo+FreeType+HarfBuzz Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
videoFlags
);
SDL_Renderer *renderer = SDL_CreateRenderer(
screen,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
/* Create an SDL image surface we can hand to cairo to draw to */
SDL_Surface *sdl_surface = SDL_CreateRGBSurface (
videoFlags,
width,
height,
32,
0x00ff0000,
0x0000ff00,
0x000000ff,
0
);
/* Our main event/draw loop */
int done = 0;
while (!done) {
/* Clear our surface */
SDL_FillRect( sdl_surface, NULL, 0 );
/* Create a cairo surface which will write directly to the sdl surface */
cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data (
(unsigned char *)sdl_surface->pixels,
CAIRO_FORMAT_RGB24,
sdl_surface->w,
sdl_surface->h,
sdl_surface->pitch
);
cairo_t *cr = cairo_create(cairo_surface);
/*******************************************/
/*** Draw our text to the cairo context ***/
/*******************************************/
int x = 0;
int y = 50;
for (int i=0; i < NUM_EXAMPLES; ++i) {
/* Create a buffer for harfbuzz to use */
hb_buffer_t *buf = hb_buffer_create();
//alternatively you can use hb_buffer_set_unicode_funcs(buf, hb_glib_get_unicode_funcs());
hb_buffer_set_unicode_funcs(buf, hb_icu_get_unicode_funcs());
hb_buffer_set_direction(buf, text_directions[i]); /* or LTR */
hb_buffer_set_script(buf, scripts[i]); /* see hb-unicode.h */
hb_buffer_set_language(buf, hb_language_from_string(languages[i], -1));
/* Layout the text */
hb_buffer_add_utf8(buf, texts[i], strlen(texts[i]), 0, strlen(texts[i]));
hb_shape(hb_ft_font[i], buf, NULL, 0);
/* Hand the layout to cairo to render */
unsigned int glyph_count;
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);
cairo_glyph_t *cairo_glyphs = malloc(sizeof(cairo_glyph_t) * glyph_count);
unsigned int string_width_in_pixels = 0;
for (int i=0; i < glyph_count; ++i) {
string_width_in_pixels += glyph_pos[i].x_advance/64;
}
if (i == ENGLISH) { x = 20; } /* left justify */
if (i == ARABIC) { x = width - string_width_in_pixels -20; } /* right justify */
if (i == CHINESE) { x = width/2 - string_width_in_pixels/2; } /* center */
for (int i=0; i < glyph_count; ++i) {
cairo_glyphs[i].index = glyph_info[i].codepoint;
cairo_glyphs[i].x = x + (glyph_pos[i].x_offset/64.0);
cairo_glyphs[i].y = y - (glyph_pos[i].y_offset/64.0);
x += glyph_pos[i].x_advance/64.0;
y -= glyph_pos[i].y_advance/64.0;
}
cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
cairo_set_font_face(cr, cairo_ft_face[i]);
cairo_set_font_size(cr, FONT_SIZE);
cairo_show_glyphs(cr, cairo_glyphs, glyph_count);
free(cairo_glyphs);
hb_buffer_destroy(buf);
y += 75;
}
/******************************************************************************/
/*** Cleanup our cairo surface, copy it to the screen, deal with SDL events ***/
/******************************************************************************/
/* Blit our new image to our visible screen */
SDL_SetRenderDrawColor(renderer,235, 52, 146,0);
SDL_RenderClear(renderer);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,sdl_surface);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(texture);
/* We're now done with our cairo surface */
cairo_surface_destroy(cairo_surface);
cairo_destroy(cr);
/* Handle SDL events. Wait for the first event, process any others that
* may also be pending. If you don't want to pause your program waiting
* for events, use SDL_PollEvent instead. */
SDL_Event event;
if (SDL_WaitEvent(&event)) {
do {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
done = 1;
}
break;
case SDL_QUIT:
done = 1;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
width = event.window.data1;
height = event.window.data2;
break;
case SDL_WINDOWEVENT_CLOSE:
event.type = SDL_QUIT;
SDL_PushEvent(&event);
break;
}
/* Create an SDL image surface we can hand to cairo to draw to */
SDL_FreeSurface(sdl_surface);
sdl_surface = SDL_CreateRGBSurface (
videoFlags,
width,
height,
32,
0x00ff0000,
0x0000ff00,
0x000000ff,
0
);
break;
}
} while(SDL_PollEvent(&event));
}
}
/* Cleanup */
for (int i=0; i < NUM_EXAMPLES; ++i) {
cairo_font_face_destroy(cairo_ft_face[i]);
hb_font_destroy(hb_ft_font[i]);
hb_face_destroy(hb_ft_face[i]);
}
FT_Done_FreeType(ft_library);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
SDL_Quit();
return 0;
}