Flutter Engine
The Flutter Engine
Classes | Functions | Variables
SkXmp.cpp File Reference
#include "include/private/SkXmp.h"
#include "include/core/SkColor.h"
#include "include/core/SkData.h"
#include "include/core/SkScalar.h"
#include "include/core/SkStream.h"
#include "include/private/SkGainmapInfo.h"
#include "include/utils/SkParse.h"
#include "src/codec/SkCodecPriv.h"
#include "src/xml/SkDOM.h"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include <utility>

Go to the source code of this file.

Classes

class  SkXmpImpl
 

Functions

static const char * get_namespace_prefix (const char *name)
 
static const char * get_unique_child_text (const SkDOM &dom, const SkDOM::Node *node, const std::string &childName)
 
static const SkDOM::Nodeget_typed_child (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &type)
 
static const char * get_attr (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key)
 
static bool get_attr_bool (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, bool *outValue)
 
static bool get_attr_int32 (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, int32_t *value)
 
static bool get_attr_float (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, float *outValue)
 
static bool get_attr_float3_as_list (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, SkColor4f *outValue)
 
static bool get_attr_float3 (const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, SkColor4f *outValue)
 
static void find_uri_namespaces (const SkDOM &dom, const SkDOM::Node *node, size_t count, const char *uris[], const char *outNamespaces[])
 
static const SkDOM::Nodefind_uri_namespaces (const SkDOM &dom, size_t count, const char *uris[], const char *outNamespaces[])
 

Variables

const char * kXmlnsPrefix = "xmlns:"
 
const size_t kXmlnsPrefixLength = 6
 

Function Documentation

◆ find_uri_namespaces() [1/2]

static void find_uri_namespaces ( const SkDOM dom,
const SkDOM::Node node,
size_t  count,
const char *  uris[],
const char *  outNamespaces[] 
)
static

Definition at line 271 of file SkXmp.cpp.

275 {
276 // Search all attributes for xmlns:NAMESPACEi="URIi".
277 for (const auto* attr = dom.getFirstAttr(node); attr; attr = dom.getNextAttr(node, attr)) {
278 const char* attrName = dom.getAttrName(node, attr);
279 const char* attrValue = dom.getAttrValue(node, attr);
280 if (!attrName || !attrValue) {
281 continue;
282 }
283 // Make sure the name starts with "xmlns:".
284 if (strlen(attrName) <= kXmlnsPrefixLength) {
285 continue;
286 }
287 if (memcmp(attrName, kXmlnsPrefix, kXmlnsPrefixLength) != 0) {
288 continue;
289 }
290 // Search for a requested URI that matches.
291 for (size_t i = 0; i < count; ++i) {
292 if (strcmp(attrValue, uris[i]) != 0) {
293 continue;
294 }
295 outNamespaces[i] = attrName;
296 }
297 }
298}
int count
Definition: FontMgrTest.cpp:50
const size_t kXmlnsPrefixLength
Definition: SkXmp.cpp:30
const char * kXmlnsPrefix
Definition: SkXmp.cpp:29
Definition: dom.py:1

◆ find_uri_namespaces() [2/2]

static const SkDOM::Node * find_uri_namespaces ( const SkDOM dom,
size_t  count,
const char *  uris[],
const char *  outNamespaces[] 
)
static

Definition at line 302 of file SkXmp.cpp.

305 {
306 const SkDOM::Node* root = dom.getRootNode();
307 if (!root) {
308 return nullptr;
309 }
310
311 // Ensure that the root node identifies itself as XMP metadata.
312 const char* rootName = dom.getName(root);
313 if (!rootName || strcmp(rootName, "x:xmpmeta") != 0) {
314 return nullptr;
315 }
316
317 // Iterate the children with name rdf:RDF.
318 const char* kRdf = "rdf:RDF";
319 for (const auto* rdf = dom.getFirstChild(root, kRdf); rdf;
320 rdf = dom.getNextSibling(rdf, kRdf)) {
321 std::vector<const char*> rdfNamespaces(count, nullptr);
322 find_uri_namespaces(dom, rdf, count, uris, rdfNamespaces.data());
323
324 // Iterate the children with name rdf::Description.
325 const char* kDesc = "rdf:Description";
326 for (const auto* desc = dom.getFirstChild(rdf, kDesc); desc;
327 desc = dom.getNextSibling(desc, kDesc)) {
328 std::vector<const char*> descNamespaces = rdfNamespaces;
329 find_uri_namespaces(dom, desc, count, uris, descNamespaces.data());
330
331 // If we have a match for all the requested URIs, return.
332 bool foundAllUris = true;
333 for (size_t i = 0; i < count; ++i) {
334 if (!descNamespaces[i]) {
335 foundAllUris = false;
336 break;
337 }
338 }
339 if (foundAllUris) {
340 for (size_t i = 0; i < count; ++i) {
341 outNamespaces[i] = descNamespaces[i];
342 }
343 return desc;
344 }
345 }
346 }
347 return nullptr;
348}
static void find_uri_namespaces(const SkDOM &dom, const SkDOM::Node *node, size_t count, const char *uris[], const char *outNamespaces[])
Definition: SkXmp.cpp:271
string root
Definition: scale_cpu.py:20

◆ get_attr()

static const char * get_attr ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key 
)
static

Definition at line 125 of file SkXmp.cpp.

128 {
129 const auto name = prefix + ":" + key;
130 const char* attr = dom->findAttr(node, name.c_str());
131 if (attr) {
132 return attr;
133 }
134 return get_unique_child_text(*dom, node, name);
135}
static const char * get_unique_child_text(const SkDOM &dom, const SkDOM::Node *node, const std::string &childName)
Definition: SkXmp.cpp:52
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ get_attr_bool()

static bool get_attr_bool ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key,
bool *  outValue 
)
static

Definition at line 138 of file SkXmp.cpp.

142 {
143 const char* attr = get_attr(dom, node, prefix, key);
144 if (!attr) {
145 return false;
146 }
147 switch (SkParse::FindList(attr, "False,True")) {
148 case 0:
149 *outValue = false;
150 return true;
151 case 1:
152 *outValue = true;
153 return true;
154 default:
155 break;
156 }
157 return false;
158}
static const char * get_attr(const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key)
Definition: SkXmp.cpp:125
static int FindList(const char str[], const char list[])
Definition: SkParse.cpp:278

◆ get_attr_float()

static bool get_attr_float ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key,
float *  outValue 
)
static

Definition at line 177 of file SkXmp.cpp.

181 {
182 const char* attr = get_attr(dom, node, prefix, key);
183 if (!attr) {
184 return false;
185 }
186 SkScalar value = 0.f;
187 if (SkParse::FindScalar(attr, &value)) {
188 *outValue = value;
189 return true;
190 }
191 return false;
192}
static const char * FindScalar(const char str[], SkScalar *value)
Definition: SkParse.cpp:216
float SkScalar
Definition: extension.cpp:12
uint8_t value

◆ get_attr_float3()

static bool get_attr_float3 ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key,
SkColor4f outValue 
)
static

Definition at line 255 of file SkXmp.cpp.

259 {
260 if (get_attr_float3_as_list(dom, node, prefix, key, outValue)) {
261 return true;
262 }
263 SkScalar value = -1.0;
264 if (get_attr_float(dom, node, prefix, key, &value)) {
265 *outValue = {value, value, value, 1.f};
266 return true;
267 }
268 return false;
269}
static bool get_attr_float3_as_list(const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, SkColor4f *outValue)
Definition: SkXmp.cpp:196
static bool get_attr_float(const SkDOM *dom, const SkDOM::Node *node, const std::string &prefix, const std::string &key, float *outValue)
Definition: SkXmp.cpp:177

◆ get_attr_float3_as_list()

static bool get_attr_float3_as_list ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key,
SkColor4f outValue 
)
static

Definition at line 196 of file SkXmp.cpp.

200 {
201 const auto name = prefix + ":" + key;
202
203 // Fail if there are multiple children with childName.
204 if (dom->countChildren(node, name.c_str()) != 1) {
205 return false;
206 }
207 // Find the child.
208 const auto* child = dom->getFirstChild(node, name.c_str());
209 if (!child) {
210 return false;
211 }
212
213 // Search for the rdf:Seq child.
214 const auto* seq = dom->getFirstChild(child, "rdf:Seq");
215 if (!seq) {
216 return false;
217 }
218
219 size_t count = 0;
220 SkScalar values[3] = {0.f, 0.f, 0.f};
221 for (const auto* liNode = dom->getFirstChild(seq, "rdf:li"); liNode;
222 liNode = dom->getNextSibling(liNode, "rdf:li")) {
223 if (count > 2) {
224 SkCodecPrintf("Too many items in list.\n");
225 return false;
226 }
227 if (dom->countChildren(liNode) != 1) {
228 SkCodecPrintf("Item can only have one child.\n");
229 return false;
230 }
231 const auto* liTextNode = dom->getFirstChild(liNode);
232 if (dom->getType(liTextNode) != SkDOM::kText_Type) {
233 SkCodecPrintf("Item's only child must be text.\n");
234 return false;
235 }
236 const char* liText = dom->getName(liTextNode);
237 if (!liText) {
238 SkCodecPrintf("Failed to get item's text.\n");
239 return false;
240 }
241 if (!SkParse::FindScalar(liText, values + count)) {
242 SkCodecPrintf("Failed to parse item's text to float.\n");
243 return false;
244 }
245 count += 1;
246 }
247 if (count < 3) {
248 SkCodecPrintf("List didn't have enough items.\n");
249 return false;
250 }
251 *outValue = {values[0], values[1], values[2], 1.f};
252 return true;
253}
#define SkCodecPrintf(...)
Definition: SkCodecPriv.h:23
@ kText_Type
Definition: SkDOM.h:44

◆ get_attr_int32()

static bool get_attr_int32 ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  key,
int32_t *  value 
)
static

Definition at line 161 of file SkXmp.cpp.

165 {
166 const char* attr = get_attr(dom, node, prefix, key);
167 if (!attr) {
168 return false;
169 }
170 if (!SkParse::FindS32(attr, value)) {
171 return false;
172 }
173 return true;
174}
static const char * FindS32(const char str[], int32_t *value)
Definition: SkParse.cpp:143

◆ get_namespace_prefix()

static const char * get_namespace_prefix ( const char *  name)
static

Definition at line 32 of file SkXmp.cpp.

32 {
33 if (strlen(name) <= kXmlnsPrefixLength) {
34 return nullptr;
35 }
36 return name + kXmlnsPrefixLength;
37}

◆ get_typed_child()

static const SkDOM::Node * get_typed_child ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  prefix,
const std::string &  type 
)
static

Definition at line 86 of file SkXmp.cpp.

89 {
90 const auto name = prefix + std::string(":") + type;
91 const SkDOM::Node* child = dom->getFirstChild(node, name.c_str());
92 if (child) {
93 return child;
94 }
95
96 const SkDOM::Node* typeChild = dom->getFirstChild(node, "rdf:type");
97 if (!typeChild) {
98 return nullptr;
99 }
100 const char* typeChildResource = dom->findAttr(typeChild, "rdf:resource");
101 if (!typeChildResource || typeChildResource != type) {
102 return nullptr;
103 }
104
105 const SkDOM::Node* valueChild = dom->getFirstChild(node, "rdf:value");
106 if (!valueChild) {
107 return nullptr;
108 }
109 const char* valueChildParseType = dom->findAttr(valueChild, "rdf:parseType");
110 if (!valueChildParseType || strcmp(valueChildParseType, "Resource") != 0) {
111 return nullptr;
112 }
113 return valueChild;
114}
GLenum type

◆ get_unique_child_text()

static const char * get_unique_child_text ( const SkDOM dom,
const SkDOM::Node node,
const std::string &  childName 
)
static

Definition at line 52 of file SkXmp.cpp.

54 {
55 // Fail if there are multiple children with childName.
56 if (dom.countChildren(node, childName.c_str()) != 1) {
57 return nullptr;
58 }
59 const auto* child = dom.getFirstChild(node, childName.c_str());
60 if (!child) {
61 return nullptr;
62 }
63 // Fail if the child has any children besides text.
64 if (dom.countChildren(child) != 1) {
65 return nullptr;
66 }
67 const auto* grandChild = dom.getFirstChild(child);
68 if (dom.getType(grandChild) != SkDOM::kText_Type) {
69 return nullptr;
70 }
71 // Return the text.
72 return dom.getName(grandChild);
73}

Variable Documentation

◆ kXmlnsPrefix

const char* kXmlnsPrefix = "xmlns:"

Definition at line 29 of file SkXmp.cpp.

◆ kXmlnsPrefixLength

const size_t kXmlnsPrefixLength = 6

Definition at line 30 of file SkXmp.cpp.