Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgText.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef _TVG_TEXT_H
24 #define _TVG_TEXT_H
25
26 #include <cstring>
27 #include "tvgMath.h"
28 #include "tvgShape.h"
29 #include "tvgFill.h"
30 #include "tvgLoader.h"
31
32 struct Text::Impl
33 {
34 FontLoader* loader = nullptr;
35 Text* paint;
36 Shape* shape;
37 FontMetrics metrics;
38 char* utf8 = nullptr;
39 float fontSize;
40 bool italic = false;
41 bool changed = false;
42
43 Impl(Text* p) : paint(p), shape(Shape::gen().release()) {}
44
45 ~Impl()
46 {
47 free(utf8);
48 LoaderMgr::retrieve(loader);
49 delete(shape);
50 }
51
52 Result text(const char* utf8)
53 {
54 free(this->utf8);
55 if (utf8) this->utf8 = strdup(utf8);
56 else this->utf8 = nullptr;
57 changed = true;
58
59 return Result::Success;
60 }
61
62 Result font(const char* name, float size, const char* style)
63 {
64 auto loader = LoaderMgr::loader(name);
65 if (!loader) return Result::InsufficientCondition;
66
67 if (style && strstr(style, "italic")) italic = true;
68 else italic = false;
69
70 fontSize = size;
71
72 //Same resource has been loaded.
73 if (this->loader == loader) {
74 this->loader->sharing--; //make it sure the reference counting.
75 return Result::Success;
76 } else if (this->loader) {
77 LoaderMgr::retrieve(this->loader);
78 }
79 this->loader = static_cast<FontLoader*>(loader);
80
81 changed = true;
82 return Result::Success;
83 }
84
85 RenderRegion bounds(RenderMethod* renderer)
86 {
87 return P(shape)->bounds(renderer);
88 }
89
90 bool render(RenderMethod* renderer)
91 {
92 if (!loader) return true;
93 renderer->blend(PP(paint)->blendMethod);
94 return PP(shape)->render(renderer);
95 }
96
97 float load()
98 {
99 if (!loader) return 0.0f;
100
101 //reload
102 if (changed) {
103 loader->read(shape, utf8, metrics);
104 changed = false;
105 }
106 return loader->transform(shape, metrics, fontSize, italic);
107 }
108
109 RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper)
110 {
111 auto scale = 1.0f / load();
112 if (tvg::zero(scale)) return nullptr;
113
114 //transform the gradient coordinates based on the final scaled font.
115 auto fill = P(shape)->rs.fill;
116 if (fill && P(shape)->rFlag & RenderUpdateFlag::Gradient) {
117 if (fill->type() == Type::LinearGradient) {
118 P(static_cast<LinearGradient*>(fill))->x1 *= scale;
119 P(static_cast<LinearGradient*>(fill))->y1 *= scale;
120 P(static_cast<LinearGradient*>(fill))->x2 *= scale;
121 P(static_cast<LinearGradient*>(fill))->y2 *= scale;
122 } else {
123 P(static_cast<RadialGradient*>(fill))->cx *= scale;
124 P(static_cast<RadialGradient*>(fill))->cy *= scale;
125 P(static_cast<RadialGradient*>(fill))->r *= scale;
126 P(static_cast<RadialGradient*>(fill))->fx *= scale;
127 P(static_cast<RadialGradient*>(fill))->fy *= scale;
128 P(static_cast<RadialGradient*>(fill))->fr *= scale;
129 }
130 }
131 return PP(shape)->update(renderer, transform, clips, opacity, pFlag, false);
132 }
133
134 bool bounds(float* x, float* y, float* w, float* h, TVG_UNUSED bool stroking)
135 {
136 if (load() == 0.0f) return false;
137 PP(shape)->bounds(x, y, w, h, true, true, false);
138 return true;
139 }
140
141 Paint* duplicate(Paint* ret)
142 {
143 if (ret) TVGERR("RENDERER", "TODO: duplicate()");
144
145 load();
146
147 auto text = Text::gen().release();
148 auto dup = text->pImpl;
149 P(shape)->duplicate(dup->shape);
150
151 if (loader) {
152 dup->loader = loader;
153 ++dup->loader->sharing;
154 }
155
156 dup->utf8 = strdup(utf8);
157 dup->italic = italic;
158 dup->fontSize = fontSize;
159
160 return text;
161 }
162
163 Iterator* iterator()
164 {
165 return nullptr;
166 }
167 };
168
169
170
171 #endif //_TVG_TEXT_H