~ruther/CTU-FEE-B0B35APO-Semestral-project

58be27abc63c8043cb3e2ccfc48b6c1a9dfda047 — František Boháček 3 years ago 8e2364f
feat: split text into multiple lines so it fits
2 files changed, 32 insertions(+), 3 deletions(-)

M lib-gui/src/gui_component_text.c
M text-viewer/src/text_viewer.c
M lib-gui/src/gui_component_text.c => lib-gui/src/gui_component_text.c +31 -2
@@ 1,6 1,7 @@
#include "gui_component_text.h"
#include "font.h"
#include "renderer.h"
#include <string.h>

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;
    }

  }
}


M text-viewer/src/text_viewer.c => text-viewer/src/text_viewer.c +1 -1
@@ 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);


Do not follow this link