diff --git a/core/indigo-core/molecule/src/molecule_cdxml_loader.cpp b/core/indigo-core/molecule/src/molecule_cdxml_loader.cpp index c9e5e6cee6..3edfd6b0c3 100644 --- a/core/indigo-core/molecule/src/molecule_cdxml_loader.cpp +++ b/core/indigo-core/molecule/src/molecule_cdxml_loader.cpp @@ -1706,93 +1706,80 @@ void MoleculeCdxmlLoader::_parseTextToKetObject(BaseCDXElement& elem, std::vecto // add paragraph if (kto.block().empty()) kto.block().push_back(SimpleTextObject::KETTextParagraph()); + auto& paragraph = kto.block().back(); - // break parts into separate paragraphs if CR presents - auto lines = split_with_empty(style_text, '\n'); - for (size_t i = 0; i < lines.size(); ++i) + if (style_text.size()) { - if (i) - kto.block().push_back(SimpleTextObject::KETTextParagraph()); - - const auto& part = lines[i]; - if (part.size()) + FONT_STYLE_SET fss; + font_face = 0; + font_size = 0.0; + font_id = 0; + font_color_index.reset(); + auto style = text_style->firstProperty(); + applyDispatcher(*style, style_dispatcher); + + // fill fss + + CDXMLFontStyle fs(font_face); + if (font_face == KCDXMLChemicalFontStyle) { - // parts should be added to the last paragraph - auto& paragraph = kto.block().back(); - if (line_starts.size()) - { - paragraph.line_starts = line_starts; - line_starts.clear(); - } - - FONT_STYLE_SET fss; - font_face = 0; - font_size = 0.0; - font_id = 0; - font_color_index.reset(); - auto style = text_style->firstProperty(); - applyDispatcher(*style, style_dispatcher); - - // fill fss - - CDXMLFontStyle fs(font_face); - if (font_face == KCDXMLChemicalFontStyle) - { - // special case - } - else - { - if (fs.is_bold) - fss.emplace(KETFontStyle::FontStyle::EBold, true); - if (fs.is_italic) - fss.emplace(KETFontStyle::FontStyle::EBold, true); - if (fs.is_superscript) - fss.emplace(KETFontStyle::FontStyle::ESuperScript, true); - if (fs.is_superscript) - fss.emplace(KETFontStyle::FontStyle::ESubScript, true); - } + // special case + } + else + { + if (fs.is_bold) + fss.emplace(KETFontStyle::FontStyle::EBold, true); + if (fs.is_italic) + fss.emplace(KETFontStyle::FontStyle::EBold, true); + if (fs.is_superscript) + fss.emplace(KETFontStyle::FontStyle::ESuperScript, true); + if (fs.is_superscript) + fss.emplace(KETFontStyle::FontStyle::ESubScript, true); + } - // set fss font size - if (font_size > 0 && (int)font_size != KDefaultFontSize) - fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::ESize, static_cast(font_size)), - std::forward_as_tuple(true)); + // set fss font size + if (font_size > 0 && (int)font_size != KDefaultFontSize) + fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::ESize, static_cast(font_size)), + std::forward_as_tuple(true)); - // set fss font color - if (font_color_index.has_value()) - { - auto fidx = font_color_index.value(); - auto font_color = fidx >= 0 && font_color_index.value() < static_cast(color_table.size()) ? color_table[font_color_index.value()] - : (fidx == -1 ? 0xFFFFFF : 0); - fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::EColor, font_color), std::forward_as_tuple(true)); - } + // set fss font color + if (font_color_index.has_value()) + { + auto fidx = font_color_index.value(); + auto font_color = fidx >= 0 && font_color_index.value() < static_cast(color_table.size()) ? color_table[font_color_index.value()] + : (fidx == -1 ? 0xFFFFFF : 0); + fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::EColor, font_color), std::forward_as_tuple(true)); + } - // set fss font family - auto font_it = font_table.find(font_id); - if (font_it != font_table.end()) - fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::EFamily, font_it->second), - std::forward_as_tuple(true)); + // set fss font family + auto font_it = font_table.find(font_id); + if (font_it != font_table.end()) + fss.emplace(std::piecewise_construct, std::forward_as_tuple(KETFontStyle::FontStyle::EFamily, font_it->second), + std::forward_as_tuple(true)); - auto prev_it = paragraph.font_styles.find(paragraph.text.size()); - if (prev_it != paragraph.font_styles.end()) - prev_it->second += fss; - else - paragraph.font_styles.emplace(paragraph.text.size(), fss); + auto prev_it = paragraph.font_styles.find(paragraph.text.size()); + if (prev_it != paragraph.font_styles.end()) + prev_it->second += fss; + else + paragraph.font_styles.emplace(paragraph.text.size(), fss); - paragraph.text += part; - // turn off the styles - FONT_STYLE_SET fss_off; - for (auto& fs_off : fss) - { - fss.emplace(fs_off.first, false); - } - if (fss.size()) - paragraph.font_styles.emplace(paragraph.text.size(), fss); + paragraph.text += style_text; + // turn off the styles + FONT_STYLE_SET fss_off; + for (auto& fs_off : fss) + { + fss.emplace(fs_off.first, false); } + if (fss.size()) + paragraph.font_styles.emplace(paragraph.text.size(), fss); } } } if (kto.block().size()) + { + kto.block().back().line_starts = line_starts; text_objects.push_back(kto); + } } void MoleculeCdxmlLoader::_parseText(BaseCDXElement& elem, std::vector& text_parsed) diff --git a/core/render2d/src/render_item_aux.cpp b/core/render2d/src/render_item_aux.cpp index 0d371bd130..4679006629 100644 --- a/core/render2d/src/render_item_aux.cpp +++ b/core/render2d/src/render_item_aux.cpp @@ -368,6 +368,7 @@ void RenderItemAuxiliary::_drawMeta(bool idle) for (auto& text_item : ko.block()) { float text_max_height = _getMaxHeight(text_item); + auto line_starts = text_item.line_starts; int first_index = -1; int second_index = -1; double text_offset_x = 0; @@ -390,34 +391,35 @@ void RenderItemAuxiliary::_drawMeta(bool idle) std::wstring_convert> utf82w; std::wstring_convert> w2utf8; - auto sub_text = w2utf8.to_bytes(utf82w.from_bytes(text_item.text).substr(first_index, second_index - first_index)); - fillKETStyle(ti, current_styles); - ti.text.readString(sub_text.c_str(), true); - _rc.setTextItemSize(ti); - ti.bbp.x = static_cast(text_origin.x - ti.relpos.x + text_offset_x); - ti.bbp.y = static_cast(text_origin.y - ti.relpos.y + text_max_height / 2 + text_offset_y); + std::vector styled_lines; + if (line_starts.has_value() && line_starts.value().size() && line_starts.value().front() <= second_index && + line_starts.value().front() > first_index) + { + auto ls_index = line_starts.value().front(); + line_starts.value().erase(line_starts.value().begin()); + styled_lines.push_back(text_item.text.substr(first_index, ls_index - first_index)); + styled_lines.push_back(text_item.text.substr(ls_index, second_index - ls_index)); + } + else + styled_lines.push_back(text_item.text.substr(first_index, second_index - first_index)); + fillKETStyle(ti, current_styles); float red = (float)((ti.color >> 16) & 0xFF) / 255.0f; float green = (float)((ti.color >> 8) & 0xFF) / 255.0f; float blue = (float)(ti.color & 0xFF) / 255.0f; - if (text_item.line_starts.has_value()) - { - int line_start_offset = 0; - for (auto& line_start : text_item.line_starts.value()) - { - ti.text.readString(sub_text.substr(line_start_offset, line_start - line_start_offset).c_str(), true); - line_start_offset = line_start; - _rc.drawTextItemText(ti, Vec3f(red, green, blue), idle); - ti.bbp.y += text_max_height + _settings.boundExtent; - } - text_offset_y += text_max_height * (text_item.line_starts.value().size() - 1); - } - else + // check for multiple lines + for (auto& styled_text : styled_lines) { + auto sub_text = w2utf8.to_bytes(utf82w.from_bytes(styled_text)); + ti.text.readString(sub_text.c_str(), true); + _rc.setTextItemSize(ti); + ti.bbp.x = static_cast(text_origin.x - ti.relpos.x + text_offset_x); + ti.bbp.y = static_cast(text_origin.y - ti.relpos.y + text_max_height / 2 + text_offset_y); _rc.drawTextItemText(ti, Vec3f(red, green, blue), idle); + if (styled_lines.size() > 1) + text_offset_y += text_max_height + _settings.boundExtent; } - text_offset_x += ti.bbsz.x; current_styles = kvp.second; first_index = second_index; @@ -595,8 +597,8 @@ float RenderItemAuxiliary::_getMaxHeight(const SimpleTextObject::KETTextParagrap std::wstring_convert> utf82w; std::wstring_convert> w2utf8; - auto utf32str = utf82w.from_bytes(tl.text); - auto sub_text = w2utf8.to_bytes(utf32str.substr(first_index, second_index - first_index)); + auto utf32str = utf82w.from_bytes(tl.text.substr(first_index, second_index - first_index)); + auto sub_text = w2utf8.to_bytes(utf32str); ti.text.readString(sub_text.c_str(), true); fillKETStyle(ti, current_styles);