Browse Source

Some extermly basic line-wrapping

master
A.Olokhtonov 3 years ago
parent
commit
9474289498
  1. 3
      .gitignore
  2. 2
      Makefile
  3. 30
      main.c
  4. 38
      project.4coder
  5. 27
      ttf-rasterize.c

3
.gitignore vendored

@ -0,0 +1,3 @@
Inter-Regular.ttf
build
out

2
Makefile

@ -0,0 +1,2 @@
debug:
gcc main.c -g -O0 -o build/ttf -lX11 -lm -Wall -Wextra -Wno-unused-function -Wno-unused-variable -fsanitize=address

30
main.c

@ -159,31 +159,37 @@ main(int argc, char **argv)
//memset(pixels, 0xFF, width * height * 4); //memset(pixels, 0xFF, width * height * 4);
memset(pixels, 0x00, width * height * 4); memset(pixels, 0x00, width * height * 4);
int wrap = 100 + (t * 4 % width);
int at = 75; int at = 75;
struct v2 box;
//render_utf_string(font, 18, pixels, width, L"The quick brown fox jumps over the lazy dog", 100, 150); //render_utf_string(font, 18, pixels, width, L"The quick brown fox jumps over the lazy dog", 100, 150);
//render_utf_string(font, 12, pixels, width, L"This text is seriously small", 100, at); at += 12; //render_utf_string(font, 12, pixels, width, L"This text is seriously small", 100, at); at += 12;
//render_utf_string(font, 13, pixels, width, L"This text is seriously small", 100, at); at += 13; //render_utf_string(font, 13, pixels, width, L"This text is seriously small", 100, at); at += 13;
render_utf_string(font, 12, pixels, width, L"This text is seriously small", 100, at); at += 12; box = render_utf_string(font, 12, pixels, width, L"This text is seriously small", wrap, 100, at);
render_utf_string(font, 18, pixels, width, L"aolo2@avx:~/Documents/code/ttf-test (master) $", 100, at); at += 20; at += box.y;
render_utf_string(font, 26, pixels, width, L"render_utf_string(font, 32, pixels, width, string3, 10 + w1 + w2, at);", 10, at); at += 24; box = render_utf_string(font, 18, pixels, width, L"aolo2@avx:~/Documents/code/ttf-test (master) $", wrap, 100, at); at += 20;
render_utf_string(font, 24, pixels, width, L"The quick brown fox jumps over the lazy dog", 10, at); at += 24; at += box.y;
box = render_utf_string(font, 26, pixels, width, L"render_utf_string(font, 32, pixels, width, string3, 10 + w1 + w2, at);", wrap, 10, at); at += 24;
at += box.y;
box = render_utf_string(font, 24, pixels, width, L"The quick brown fox jumps over the lazy dog", wrap, 10, at); at += 24;
wchar_t *string1 = L"iiiiiiiiiiiiiiiiiii"; wchar_t *string1 = L"iiiiiiiiiiiiiiiiiii";
wchar_t *string2 = L"something-some"; wchar_t *string2 = L"something-some";
wchar_t *string3 = L"MORE MORE"; wchar_t *string3 = L"MORE MORE";
int w1 = get_string_width(&font, 32, string1, wcslen(string1)); //get_string_width(&font, 32, string1, wcslen(string1));
int w2 = get_string_width(&font, 32, string2, wcslen(string2)); //get_string_width(&font, 32, string2, wcslen(string2));
render_utf_string(font, 32, pixels, width, string1, 10, at); //render_utf_string(font, 32, pixels, width, string1, 200, 10, at);
render_utf_string(font, 32, pixels, width, string2, 10 + w1, at); //render_utf_string(font, 32, pixels, width, string2, 200, 10 + w1, at);
render_utf_string(font, 32, pixels, width, string3, 10 + w1 + w2, at); //render_utf_string(font, 32, pixels, width, string3, 200, 10 + w1 + w2, at);
at += 80; //at += 80;
render_utf_string(font, 80, pixels, width, L"~!@#$%^&*()_+;:,./\\||||", 10, at); //render_utf_string(font, 80, pixels, width, L"~!@#$%^&*()_+;:,./\\||||", 200, 10, at);
XPutImage(display, window, default_gc, xwindow_buffer, 0, 0, 0, 0, width, height); XPutImage(display, window, default_gc, xwindow_buffer, 0, 0, 0, 0, width, height);
sleep(1); usleep(16667);
++t; ++t;
} }

38
project.4coder

@ -0,0 +1,38 @@
version(1);
project_name = "ttf-test";
patterns = {
"*.c",
"*.h",
"*.4coder",
"Makefile",
};
blacklist_patterns = {
".*",
"res",
"lib",
"build/*"
};
load_paths_base = {
{ ".", .relative = true, .recursive = true, }
};
load_paths = {
{ load_paths_base, .os = "linux", }
};
command_list = {
{
.name = "build debug",
.out = "*compilation*",
.footer_panel = false,
.save_dirty_files = true,
.cursor_at_end = true,
.cmd = {{ "make debug", .os = "linux" }},
},
};
fkey_command[1] = "build debug";

27
ttf-rasterize.c

@ -241,8 +241,8 @@ outline_to_lines(struct glyph g, f32 scale, int max_descent, struct line_contour
} }
static void static struct v2
render_utf_string(struct ttf_font font, int px_size, u32 *pixels, u32 width, wchar_t *string, int at_x, int at_y) render_utf_string(struct ttf_font font, int px_size, u32 *pixels, u32 width, wchar_t *string, u32 fit_width, int at_x, int at_y)
{ {
u32 offset_x = at_x; u32 offset_x = at_x;
u32 offset_y = at_y; u32 offset_y = at_y;
@ -250,8 +250,13 @@ render_utf_string(struct ttf_font font, int px_size, u32 *pixels, u32 width, wch
u32 len = wcslen(string); u32 len = wcslen(string);
struct v2 box = { 0 };
box.y = px_size;
for (u32 i = 0; i < len; ++i) { for (u32 i = 0; i < len; ++i) {
u16 codepoint = string[i]; u16 codepoint = string[i];
int advance;
if (codepoint != ' ') { if (codepoint != ' ') {
struct glyph g = get_outline(&font, codepoint); struct glyph g = get_outline(&font, codepoint);
@ -265,15 +270,21 @@ render_utf_string(struct ttf_font font, int px_size, u32 *pixels, u32 width, wch
render_glyph(g, px_size, &lines, scale, pixels, width, offset_x, offset_y); render_glyph(g, px_size, &lines, scale, pixels, width, offset_x, offset_y);
offset_x += ceil_f32(scale * g.advance); advance = ceil_f32(scale * g.advance);
free(lines.data); free(lines.data);
} else { } else {
int space_advance = get_codepoint_width(&font, scale, codepoint); advance = get_codepoint_width(&font, scale, codepoint);
offset_x += space_advance;
} }
//XPutImage(display, window, default_gc, xwindow_buffer, 0, 0, 0, 0, width, height); offset_x += advance;
//sleep(1); box.x += advance;
if (offset_x - at_x >= fit_width) {
offset_x = at_x;
offset_y += px_size;
box.y += px_size;
}
} }
return(box);
} }

Loading…
Cancel
Save