From 58be27abc63c8043cb3e2ccfc48b6c1a9dfda047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 28 Jun 2021 22:17:57 +0200 Subject: [PATCH] feat: split text into multiple lines so it fits --- lib-gui/src/gui_component_text.c | 33 ++++++++++++++++++++++++++++++-- text-viewer/src/text_viewer.c | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib-gui/src/gui_component_text.c b/lib-gui/src/gui_component_text.c index 3530f0d..8a77091 100644 --- a/lib-gui/src/gui_component_text.c +++ b/lib-gui/src/gui_component_text.c @@ -1,6 +1,7 @@ #include "gui_component_text.h" #include "font.h" #include "renderer.h" +#include component_t gui_text_create(text_t *text, int16_t x, int16_t y, int16_t w, int16_t h) { @@ -21,8 +22,36 @@ component_t gui_text_create(text_t *text, int16_t x, int16_t y, int16_t w, void gui_text_render(container_t *container, component_t *component, gui_t *gui) { if (gui_is_component_visible(gui, container, component)) { - text_t *state = (text_t*)component->state; - renderer_write_string(gui->renderer, component->x, component->y, 0, state->font, state->line, state->color); + text_t *state = (text_t *)component->state; + uint16_t line_height = state->font->size + state->font->line_spacing; + uint16_t lines_fit = component->height / line_height; + + if (lines_fit == 0) { + lines_fit = 1; + } + + int32_t remaining = strlen(state->line); + char *line = state->line; + + int16_t y = component->y; + + while (lines_fit > 0 && remaining > 0) { + lines_fit--; + + size2d_t size = {.x = component->width, .y = line_height}; + uint16_t fit_chars = font_fit_cut(state->font, size, line); + if (fit_chars == 0) { + fit_chars = remaining; + } + + renderer_write_string(gui->renderer, component->x, y, fit_chars, + state->font, line, state->color); + + y += line_height; + remaining -= fit_chars; + line += fit_chars; + } + } } diff --git a/text-viewer/src/text_viewer.c b/text-viewer/src/text_viewer.c index 4662fda..ea26d50 100644 --- a/text-viewer/src/text_viewer.c +++ b/text-viewer/src/text_viewer.c @@ -136,7 +136,7 @@ text_viewer_gui_add_name_and_line(text_viewer_t *text_viewer, window_t *window, container_t name_and_line = gui_group_container_create(0, 0, name_and_line_components, 2); - component_t name = gui_text_create(name_text, 5, 3, 0, 0); + component_t name = gui_text_create(name_text, 5, 3, text_viewer->gui.size.x, 0); component_t line = gui_line_create(&WHITE_PIXEL, 0, name.height + name.y, text_viewer->gui.size.x, 1); -- 2.48.1