Debian Codegraph @

thirdparty/thorvg/src/loaders/svg/tvgSvgCssStyle.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2022 - 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 #include "tvgSvgCssStyle.h"
24
25 #include <cstring>
26
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
30
31 static bool _isImportanceApplicable(SvgStyleFlags &toFlagsImportance, SvgStyleFlags fromFlagsImportance, SvgStyleFlags flag)
32 {
33 if (!(toFlagsImportance & flag) && (fromFlagsImportance & flag)) {
34 return true;
35 }
36 return false;
37 }
38
39 static void _copyStyle(SvgStyleProperty* to, const SvgStyleProperty* from)
40 {
41 if (from == nullptr) return;
42 //Copy the properties of 'from' only if they were explicitly set (not the default ones).
43 if ((from->curColorSet && !(to->flags & SvgStyleFlags::Color)) ||
44 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Color)) {
45 to->color = from->color;
46 to->curColorSet = true;
47 to->flags = (to->flags | SvgStyleFlags::Color);
48 if (from->flagsImportance & SvgStyleFlags::Color) {
49 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Color);
50 }
51 }
52 if (((from->flags & SvgStyleFlags::PaintOrder) && !(to->flags & SvgStyleFlags::PaintOrder)) ||
53 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::PaintOrder)) {
54 to->paintOrder = from->paintOrder;
55 to->flags = (to->flags | SvgStyleFlags::PaintOrder);
56 if (from->flagsImportance & SvgStyleFlags::PaintOrder) {
57 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::PaintOrder);
58 }
59 }
60 if (((from->flags & SvgStyleFlags::Display) && !(to->flags & SvgStyleFlags::Display)) ||
61 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Display)) {
62 to->display = from->display;
63 to->flags = (to->flags | SvgStyleFlags::Display);
64 if (from->flagsImportance & SvgStyleFlags::Display) {
65 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Display);
66 }
67 }
68 //Fill
69 if (((from->fill.flags & SvgFillFlags::Paint) && !(to->flags & SvgStyleFlags::Fill)) ||
70 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Fill)) {
71 to->fill.paint.color = from->fill.paint.color;
72 to->fill.paint.none = from->fill.paint.none;
73 to->fill.paint.curColor = from->fill.paint.curColor;
74 if (from->fill.paint.url) {
75 if (to->fill.paint.url) free(to->fill.paint.url);
76 to->fill.paint.url = strdup(from->fill.paint.url);
77 }
78 to->fill.flags = (to->fill.flags | SvgFillFlags::Paint);
79 to->flags = (to->flags | SvgStyleFlags::Fill);
80 if (from->flagsImportance & SvgStyleFlags::Fill) {
81 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Fill);
82 }
83 }
84 if (((from->fill.flags & SvgFillFlags::Opacity) && !(to->flags & SvgStyleFlags::FillOpacity)) ||
85 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::FillOpacity)) {
86 to->fill.opacity = from->fill.opacity;
87 to->fill.flags = (to->fill.flags | SvgFillFlags::Opacity);
88 to->flags = (to->flags | SvgStyleFlags::FillOpacity);
89 if (from->flagsImportance & SvgStyleFlags::FillOpacity) {
90 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::FillOpacity);
91 }
92 }
93 if (((from->fill.flags & SvgFillFlags::FillRule) && !(to->flags & SvgStyleFlags::FillRule)) ||
94 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::FillRule)) {
95 to->fill.fillRule = from->fill.fillRule;
96 to->fill.flags = (to->fill.flags | SvgFillFlags::FillRule);
97 to->flags = (to->flags | SvgStyleFlags::FillRule);
98 if (from->flagsImportance & SvgStyleFlags::FillRule) {
99 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::FillRule);
100 }
101 }
102 //Stroke
103 if (((from->stroke.flags & SvgStrokeFlags::Paint) && !(to->flags & SvgStyleFlags::Stroke)) ||
104 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Stroke)) {
105 to->stroke.paint.color = from->stroke.paint.color;
106 to->stroke.paint.none = from->stroke.paint.none;
107 to->stroke.paint.curColor = from->stroke.paint.curColor;
108 if (from->stroke.paint.url) {
109 if (to->stroke.paint.url) free(to->stroke.paint.url);
110 to->stroke.paint.url = strdup(from->stroke.paint.url);
111 }
112 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Paint);
113 to->flags = (to->flags | SvgStyleFlags::Stroke);
114 if (from->flagsImportance & SvgStyleFlags::Stroke) {
115 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Stroke);
116 }
117 }
118 if (((from->stroke.flags & SvgStrokeFlags::Opacity) && !(to->flags & SvgStyleFlags::StrokeOpacity)) ||
119 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeOpacity)) {
120 to->stroke.opacity = from->stroke.opacity;
121 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Opacity);
122 to->flags = (to->flags | SvgStyleFlags::StrokeOpacity);
123 if (from->flagsImportance & SvgStyleFlags::StrokeOpacity) {
124 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeOpacity);
125 }
126 }
127 if (((from->stroke.flags & SvgStrokeFlags::Width) && !(to->flags & SvgStyleFlags::StrokeWidth)) ||
128 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeWidth)) {
129 to->stroke.width = from->stroke.width;
130 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Width);
131 to->flags = (to->flags | SvgStyleFlags::StrokeWidth);
132 if (from->flagsImportance & SvgStyleFlags::StrokeWidth) {
133 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeWidth);
134 }
135 }
136 if (((from->stroke.flags & SvgStrokeFlags::Dash) && !(to->flags & SvgStyleFlags::StrokeDashArray)) ||
137 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeDashArray)) {
138 if (from->stroke.dash.array.count > 0) {
139 to->stroke.dash.array.clear();
140 to->stroke.dash.array.reserve(from->stroke.dash.array.count);
141 for (uint32_t i = 0; i < from->stroke.dash.array.count; ++i) {
142 to->stroke.dash.array.push(from->stroke.dash.array[i]);
143 }
144 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Dash);
145 to->flags = (to->flags | SvgStyleFlags::StrokeDashArray);
146 if (from->flagsImportance & SvgStyleFlags::StrokeDashArray) {
147 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeDashArray);
148 }
149 }
150 }
151 if (((from->stroke.flags & SvgStrokeFlags::Cap) && !(to->flags & SvgStyleFlags::StrokeLineCap)) ||
152 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeLineCap)) {
153 to->stroke.cap = from->stroke.cap;
154 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Cap);
155 to->flags = (to->flags | SvgStyleFlags::StrokeLineCap);
156 if (from->flagsImportance & SvgStyleFlags::StrokeLineCap) {
157 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeLineCap);
158 }
159 }
160 if (((from->stroke.flags & SvgStrokeFlags::Join) && !(to->flags & SvgStyleFlags::StrokeLineJoin)) ||
161 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::StrokeLineJoin)) {
162 to->stroke.join = from->stroke.join;
163 to->stroke.flags = (to->stroke.flags | SvgStrokeFlags::Join);
164 to->flags = (to->flags | SvgStyleFlags::StrokeLineJoin);
165 if (from->flagsImportance & SvgStyleFlags::StrokeLineJoin) {
166 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::StrokeLineJoin);
167 }
168 }
169 //Opacity
170 //TODO: it can be set to be 255 and shouldn't be changed by attribute 'opacity'
171 if ((from->opacity < 255 && !(to->flags & SvgStyleFlags::Opacity)) ||
172 _isImportanceApplicable(to->flagsImportance, from->flagsImportance, SvgStyleFlags::Opacity)) {
173 to->opacity = from->opacity;
174 to->flags = (to->flags | SvgStyleFlags::Opacity);
175 if (from->flagsImportance & SvgStyleFlags::Opacity) {
176 to->flagsImportance = (to->flagsImportance | SvgStyleFlags::Opacity);
177 }
178 }
179 }
180
181
182 /************************************************************************/
183 /* External Class Implementation */
184 /************************************************************************/
185
186 void cssCopyStyleAttr(SvgNode* to, const SvgNode* from)
187 {
188 //Copy matrix attribute
189 if (from->transform && !(to->style->flags & SvgStyleFlags::Transform)) {
190 to->transform = (Matrix*)malloc(sizeof(Matrix));
191 if (to->transform) {
192 *to->transform = *from->transform;
193 to->style->flags = (to->style->flags | SvgStyleFlags::Transform);
194 }
195 }
196 //Copy style attribute
197 _copyStyle(to->style, from->style);
198
199 if (from->style->clipPath.url) {
200 if (to->style->clipPath.url) free(to->style->clipPath.url);
201 to->style->clipPath.url = strdup(from->style->clipPath.url);
202 }
203 if (from->style->mask.url) {
204 if (to->style->mask.url) free(to->style->mask.url);
205 to->style->mask.url = strdup(from->style->mask.url);
206 }
207 }
208
209
210 SvgNode* cssFindStyleNode(const SvgNode* style, const char* title, SvgNodeType type)
211 {
212 if (!style) return nullptr;
213
214 auto child = style->child.data;
215 for (uint32_t i = 0; i < style->child.count; ++i, ++child) {
216 if ((*child)->type == type) {
217 if ((!title && !(*child)->id) || (title && (*child)->id && !strcmp((*child)->id, title))) return (*child);
218 }
219 }
220 return nullptr;
221 }
222
223
224 SvgNode* cssFindStyleNode(const SvgNode* style, const char* title)
225 {
226 if (!style || !title) return nullptr;
227
228 auto child = style->child.data;
229 for (uint32_t i = 0; i < style->child.count; ++i, ++child) {
230 if ((*child)->type == SvgNodeType::CssStyle) {
231 if ((*child)->id && !strcmp((*child)->id, title)) return (*child);
232 }
233 }
234 return nullptr;
235 }
236
237
238 void cssUpdateStyle(SvgNode* doc, SvgNode* style)
239 {
240 if (doc->child.count > 0) {
241 auto child = doc->child.data;
242 for (uint32_t i = 0; i < doc->child.count; ++i, ++child) {
243 if (auto cssNode = cssFindStyleNode(style, nullptr, (*child)->type)) {
244 cssCopyStyleAttr(*child, cssNode);
245 }
246 cssUpdateStyle(*child, style);
247 }
248 }
249 }
250
251
252 void cssApplyStyleToPostponeds(Array<SvgNodeIdPair>& postponeds, SvgNode* style)
253 {
254 for (uint32_t i = 0; i < postponeds.count; ++i) {
255 auto nodeIdPair = postponeds[i];
256
257 //css styling: tag.name has higher priority than .name
258 if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id, nodeIdPair.node->type)) {
259 cssCopyStyleAttr(nodeIdPair.node, cssNode);
260 }
261 if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id)) {
262 cssCopyStyleAttr(nodeIdPair.node, cssNode);
263 }
264 }
265 }