diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fc0f73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Inter-Regular.ttf +build +out diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..03ded17 --- /dev/null +++ b/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 diff --git a/main.c b/main.c index 2dc2860..3079855 100644 --- a/main.c +++ b/main.c @@ -159,31 +159,37 @@ main(int argc, char **argv) //memset(pixels, 0xFF, width * height * 4); memset(pixels, 0x00, width * height * 4); + int wrap = 100 + (t * 4 % width); + 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, 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, 12, pixels, width, L"This text is seriously small", 100, at); at += 12; - render_utf_string(font, 18, pixels, width, L"aolo2@avx:~/Documents/code/ttf-test (master) $", 100, at); at += 20; - render_utf_string(font, 26, pixels, width, L"render_utf_string(font, 32, pixels, width, string3, 10 + w1 + w2, at);", 10, at); at += 24; - render_utf_string(font, 24, pixels, width, L"The quick brown fox jumps over the lazy dog", 10, at); at += 24; + box = render_utf_string(font, 12, pixels, width, L"This text is seriously small", wrap, 100, at); + at += box.y; + box = render_utf_string(font, 18, pixels, width, L"aolo2@avx:~/Documents/code/ttf-test (master) $", wrap, 100, at); at += 20; + 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 *string2 = L"something-some"; wchar_t *string3 = L"MORE MORE"; - int w1 = get_string_width(&font, 32, string1, wcslen(string1)); - int w2 = get_string_width(&font, 32, string2, wcslen(string2)); + //get_string_width(&font, 32, string1, wcslen(string1)); + //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, string2, 10 + w1, at); - render_utf_string(font, 32, pixels, width, string3, 10 + w1 + w2, at); - at += 80; - render_utf_string(font, 80, pixels, width, L"~!@#$%^&*()_+;:,./\\||||", 10, at); + //render_utf_string(font, 32, pixels, width, string1, 200, 10, at); + //render_utf_string(font, 32, pixels, width, string2, 200, 10 + w1, at); + //render_utf_string(font, 32, pixels, width, string3, 200, 10 + w1 + w2, at); + //at += 80; + //render_utf_string(font, 80, pixels, width, L"~!@#$%^&*()_+;:,./\\||||", 200, 10, at); XPutImage(display, window, default_gc, xwindow_buffer, 0, 0, 0, 0, width, height); - sleep(1); + usleep(16667); ++t; } diff --git a/project.4coder b/project.4coder new file mode 100644 index 0000000..80b1c98 --- /dev/null +++ b/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"; diff --git a/ttf-rasterize.c b/ttf-rasterize.c index c72b279..ccea2f2 100644 --- a/ttf-rasterize.c +++ b/ttf-rasterize.c @@ -241,8 +241,8 @@ outline_to_lines(struct glyph g, f32 scale, int max_descent, struct line_contour } -static void -render_utf_string(struct ttf_font font, int px_size, u32 *pixels, u32 width, wchar_t *string, int at_x, int at_y) +static struct v2 +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_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); + struct v2 box = { 0 }; + + box.y = px_size; + for (u32 i = 0; i < len; ++i) { u16 codepoint = string[i]; + int advance; if (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); - offset_x += ceil_f32(scale * g.advance); - + advance = ceil_f32(scale * g.advance); free(lines.data); } else { - int space_advance = get_codepoint_width(&font, scale, codepoint); - offset_x += space_advance; + advance = get_codepoint_width(&font, scale, codepoint); } - //XPutImage(display, window, default_gc, xwindow_buffer, 0, 0, 0, 0, width, height); - //sleep(1); + offset_x += advance; + box.x += advance; + + if (offset_x - at_x >= fit_width) { + offset_x = at_x; + offset_y += px_size; + box.y += px_size; + } } + + return(box); }