diff --git a/modules/text_server_adv/thorvg_svg_in_ot.cpp b/modules/text_server_adv/thorvg_svg_in_ot.cpp index cbccb1909509..f92b9e67cf5b 100644 --- a/modules/text_server_adv/thorvg_svg_in_ot.cpp +++ b/modules/text_server_adv/thorvg_svg_in_ot.cpp @@ -91,59 +91,175 @@ FT_Error tvg_svg_in_ot_preset_slot(FT_GlyphSlot p_slot, FT_Bool p_cache, FT_Poin parser->_open_buffer((const uint8_t *)document->svg_document, document->svg_document_length); String xml_body; + String xml_node; + double embox_x = document->units_per_EM; double embox_y = document->units_per_EM; - while (parser->read() == OK) { - if (parser->has_attribute("id")) { - const String &gl_name = parser->get_named_attribute_value("id"); - if (gl_name.begins_with("glyph")) { + + uint64_t offset = 0; + if (!state->document_map.has(document->svg_document)) { + state->document_map[document->svg_document] = TVG_DocumentCache(); + } + TVG_DocumentCache &cache = state->document_map[document->svg_document]; + + if (!cache.xml_body.is_empty()) { + // If we have a cached document, that means we have already parsed it. + // All node cache should be available. + + xml_body = cache.xml_body; + embox_x = cache.embox_x; + embox_y = cache.embox_y; + + DEV_ASSERT(cache.node_caches.has(p_slot->glyph_index)); + TVG_NodeCache &nc = cache.node_caches[p_slot->glyph_index]; + + // Seek will call read() internally. + if (parser->seek(nc.document_offset) == OK) { + int64_t tag_count = -1; + +#ifdef DEV_ENABLED + { + DEV_ASSERT(parser->has_attribute("id")); + const String &gl_name = parser->get_named_attribute_value("id"); + DEV_ASSERT(gl_name.begins_with("glyph")); + int dot_pos = gl_name.find_char('.'); int64_t gl_idx = gl_name.substr(5, (dot_pos > 0) ? dot_pos - 5 : -1).to_int(); - if (p_slot->glyph_index != gl_idx) { - parser->skip_section(); - continue; - } + DEV_ASSERT(p_slot->glyph_index == gl_idx); } +#endif + offset = nc.body_offset; + tag_count = 0; + + // We only parse the glyph node. + do { + if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "svg") { + if (parser->has_attribute("viewBox")) { + PackedStringArray vb = parser->get_named_attribute_value("viewBox").split(" "); + + if (vb.size() == 4) { + embox_x = vb[2].to_float(); + embox_y = vb[3].to_float(); + } + } + if (parser->has_attribute("width")) { + embox_x = parser->get_named_attribute_value("width").to_float(); + } + if (parser->has_attribute("height")) { + embox_y = parser->get_named_attribute_value("height").to_float(); + } + } + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + xml_body += vformat("<%s", parser->get_node_name()); + + bool is_svg_tag = parser->get_node_name() == "svg"; + for (int i = 0; i < parser->get_attribute_count(); i++) { + String aname = parser->get_attribute_name(i); + if (is_svg_tag && (aname == "viewBox" || aname == "width" || aname == "height")) { + continue; + } + xml_body += vformat(" %s=\"%s\"", aname, parser->get_attribute_value(i)); + } + + if (parser->is_empty()) { + xml_body += "/>"; + } else { + xml_body += ">"; + tag_count++; + } + } else if (parser->get_node_type() == XMLParser::NODE_TEXT) { + xml_body += parser->get_node_data(); + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) { + xml_body += vformat("", parser->get_node_name()); + tag_count--; + } + } while (tag_count != 0 && parser->read() == OK); } - if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "svg") { - if (parser->has_attribute("viewBox")) { - PackedStringArray vb = parser->get_named_attribute_value("viewBox").split(" "); - - if (vb.size() == 4) { - embox_x = vb[2].to_float(); - embox_y = vb[3].to_float(); + } else { + String *xml = &xml_body; + int64_t tag_count = -1; + + while (parser->read() == OK) { + if (parser->has_attribute("id")) { + const String &gl_name = parser->get_named_attribute_value("id"); + if (gl_name.begins_with("glyph")) { + int dot_pos = gl_name.find_char('.'); + int64_t gl_idx = gl_name.substr(5, (dot_pos > 0) ? dot_pos - 5 : -1).to_int(); + + TVG_NodeCache node_cache = TVG_NodeCache(); + node_cache.document_offset = parser->get_node_offset(), + node_cache.body_offset = (uint64_t)xml_body.length(); + cache.node_caches[gl_idx] = node_cache; + + if (p_slot->glyph_index != gl_idx) { + parser->skip_section(); + continue; + } + offset = xml_body.length(); + tag_count = 0; + xml = &xml_node; } } - if (parser->has_attribute("width")) { - embox_x = parser->get_named_attribute_value("width").to_float(); - } - if (parser->has_attribute("height")) { - embox_y = parser->get_named_attribute_value("height").to_float(); - } - } - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - xml_body += vformat("<%s", parser->get_node_name()); - bool is_svg_tag = parser->get_node_name() == "svg"; - for (int i = 0; i < parser->get_attribute_count(); i++) { - String aname = parser->get_attribute_name(i); - if (is_svg_tag && (aname == "viewBox" || aname == "width" || aname == "height")) { - continue; + if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "svg") { + if (parser->has_attribute("viewBox")) { + PackedStringArray vb = parser->get_named_attribute_value("viewBox").split(" "); + + if (vb.size() == 4) { + embox_x = vb[2].to_float(); + embox_y = vb[3].to_float(); + } + } + if (parser->has_attribute("width")) { + embox_x = parser->get_named_attribute_value("width").to_float(); + } + if (parser->has_attribute("height")) { + embox_y = parser->get_named_attribute_value("height").to_float(); } - xml_body += vformat(" %s=\"%s\"", aname, parser->get_attribute_value(i)); } + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + *xml += vformat("<%s", parser->get_node_name()); + + bool is_svg_tag = parser->get_node_name() == "svg"; + for (int i = 0; i < parser->get_attribute_count(); i++) { + String aname = parser->get_attribute_name(i); + if (is_svg_tag && (aname == "viewBox" || aname == "width" || aname == "height")) { + continue; + } + *xml += vformat(" %s=\"%s\"", aname, parser->get_attribute_value(i)); + } - if (parser->is_empty()) { - xml_body += "/>"; - } else { - xml_body += ">"; + if (parser->is_empty()) { + *xml += "/>"; + if (tag_count == 0) { + xml = &xml_body; + tag_count = -1; + } + } else { + *xml += ">"; + if (tag_count > 0) { + tag_count++; + } + } + } else if (parser->get_node_type() == XMLParser::NODE_TEXT) { + *xml += parser->get_node_data(); + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) { + *xml += vformat("", parser->get_node_name()); + if (tag_count > 0) { + tag_count--; + if (tag_count == 0) { + xml = &xml_body; + } + } } - } else if (parser->get_node_type() == XMLParser::NODE_TEXT) { - xml_body += parser->get_node_data(); - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) { - xml_body += vformat("", parser->get_node_name()); } + + cache.xml_body = xml_body; + cache.embox_x = embox_x; + cache.embox_y = embox_y; } + xml_body = xml_body.insert(offset, xml_node); + std::unique_ptr picture = tvg::Picture::gen(); gl_state.xml_code = xml_body.utf8(); diff --git a/modules/text_server_adv/thorvg_svg_in_ot.h b/modules/text_server_adv/thorvg_svg_in_ot.h index a0e7e3a1d2fe..e924f8782bf8 100644 --- a/modules/text_server_adv/thorvg_svg_in_ot.h +++ b/modules/text_server_adv/thorvg_svg_in_ot.h @@ -68,9 +68,22 @@ struct GL_State { tvg::Matrix m; }; +struct TVG_NodeCache { + uint64_t document_offset; + uint64_t body_offset; +}; + +struct TVG_DocumentCache { + String xml_body; + double embox_x; + double embox_y; + HashMap node_caches; +}; + struct TVG_State { Mutex mutex; HashMap glyph_map; + HashMap document_map; }; FT_Error tvg_svg_in_ot_init(FT_Pointer *p_state);