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

CTU-FEE-B0B35APO-Semestral-project/lib-gui/src/gui_component_text.c -rw-r--r-- 1.8 KiB
7aa0ada9 — Rutherther chore: fix image in readme 1 year, 10 months ago
                                                                                
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
#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) {
  component_t comp =
      gui_component_create(x, y, w, h, gui_text_render, gui_text_update);
  comp.state = text;

  if (w == 0 || h == 0) {
    size2d_t measured = font_measure_text(text->font, text->line);

    comp.width = comp.width != 0 ? comp.width : measured.x;
    comp.height = comp.height != 0 ? comp.height : measured.y;
  }

  return comp;
}

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

      for (int i = 0; i < fit_chars; i++) {
        if (line[i] == '\n' && lines_fit > 1) {
          fit_chars = i + 1;
          break;
        }
      }

      renderer_write_string(gui->renderer, component->x, y, fit_chars,
                            state->font, line, state->color);

      y += line_height;
      remaining -= fit_chars;
      line += fit_chars;
    }

  }
}

void gui_text_update(container_t *container, component_t *component,
                     gui_t *gui) {
  // do nothing -- maybe horizontal scroll?
}
Do not follow this link