CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
text.cppm
Go to the documentation of this file.
1// Copyright (c) 2024, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4module;
5#include <cairomm-1.16/cairomm/cairomm.h>
6#include <tracy/Tracy.hpp>
7
9
10import std;
11
12export import cydui.dimensions;
14
15export namespace vg {
16 using font_extents_t = Cairo::FontExtents;
17 using text_extents_t = Cairo::TextExtents;
18
19 struct text: element_t,
20 attrs_core<text>,
21 attrs_fill<text>,
22 // attrs_stroke<text>,
23 attrs_font<text>,
24 attr_x<text>,
25 attr_y<text>,
26 attr_rotate<text>,
27 attr_pivot_x<text>,
28 attr_pivot_y<text>,
29 attr_scale_x<text>,
30 attr_scale_y<text>,
31 attr_w<text>,
32 attr_h<text> {
33 std::string _text;
34 explicit text(std::string _text)
35 : _text(std::move(_text)) {
36 // TracyAllocN(this, sizeof(decltype(*this)), "fragment_elements");
37 }
38 ~text() override {
39 // TracyFreeN(this, "fragment_elements");
40 }
41 void apply_to(pixelmap_editor_t& editor) const override {
42 apply_font(editor);
43 // apply_stroke(editor);
44 apply_fill(editor);
45
46 Cairo::FontExtents fextents;
47 editor->get_font_extents(fextents);
48
49 Cairo::TextExtents extents;
50 editor->get_text_extents(_text, extents);
51
52 editor->save();
53 editor->translate(origin_x + _x + _pivot_x, origin_y + _y - _pivot_y);
54 editor->rotate(_rotate * std::numbers::pi / 180.0);
55 editor->scale(_scale_x, _scale_y);
56 editor->move_to(-_pivot_x, _pivot_y);
57 set_source_to_fill(editor);
58 editor->show_text(_text);
59 editor->restore();
60
61 // editor->move_to(origin_x + _x, origin_y + _y + fextents.ascent);
62 // editor->show_text(_text);
63 // editor->move_to(origin_x + _x + 5, origin_y + _y + fextents.height);
64 // editor->show_text(_text);
65 // editor->move_to(origin_x + _x + 10, origin_y + _y + extents.height);
66 // editor->show_text(_text);
67 }
68
69 footprint get_footprint() const override {
70 pixelmap_t pm{0, 0};
71 pixelmap_editor_t pe{pm};
72
73 apply_font(pe);
74 apply_fill(pe);
75
76 Cairo::FontExtents fextents;
77 pe->get_font_extents(fextents);
78
79 Cairo::TextExtents extents;
80 pe->get_text_extents(_text, extents);
81
82 return {
83 .x = static_cast<int>(_x),
84 .y = static_cast<int>(_y - fextents.ascent),
85 .w = static_cast<int>(extents.x_advance),
86 .h = static_cast<int>(fextents.height),
87 };
88 }
89
91 pixelmap_t pm{0, 0};
92 pixelmap_editor_t pe{pm};
93
94 apply_font(pe);
95
96 Cairo::FontExtents fextents;
97 pe->get_font_extents(fextents);
98
99 return fextents;
100 }
101
103 pixelmap_t pm{0, 0};
104 pixelmap_editor_t pe{pm};
105
106 apply_font(pe);
107
108 Cairo::TextExtents extents;
109 pe->get_text_extents(_text, extents);
110
111 return extents;
112 }
113 };
114} // namespace vg
Cairo::TextExtents text_extents_t
Definition text.cppm:17
Cairo::FontExtents font_extents_t
Definition text.cppm:16
text & h(const quantify::quantity_t< UNIT, T > &_h_)
pivot_x - rotate pivot x-axis coordinate
pivot_y - rotate pivot y-axis coordinate
rotate - angle of rotation in degrees
rotate - angle of rotation in degrees
rotate - angle of rotation in degrees
x - x-axis coordinate
y - y-axis coordinate
text & y(const quantify::quantity_t< UNIT, T > &_y_)
void apply_fill(pixelmap_editor_t &editor) const
void set_source_to_fill(pixelmap_editor_t &editor) const
void apply_font(pixelmap_editor_t &editor) const
void apply_to(pixelmap_editor_t &editor) const override
Definition text.cppm:41
text(std::string _text)
Definition text.cppm:34
font_extents_t get_font_extents() const
Definition text.cppm:90
std::string _text
Definition text.cppm:33
~text() override
Definition text.cppm:38
footprint get_footprint() const override
Definition text.cppm:69
text_extents_t get_text_extents() const
Definition text.cppm:102