From acbf5b0c400cdaad69be722671663dd47309f616 Mon Sep 17 00:00:00 2001 From: Kamil Kerimov Date: Wed, 29 May 2024 17:14:10 +0500 Subject: [PATCH] Fix bug #68170 Add image alternative text and description --- OdfFile/Common/odf_elements_type.h | 1 + OdfFile/Reader/Converter/docx_drawing.cpp | 14 ++++++++ OdfFile/Reader/Format/draw_frame.cpp | 8 +++++ OdfFile/Reader/Format/draw_frame.h | 3 ++ OdfFile/Reader/Format/draw_frame_docx.cpp | 5 +++ OdfFile/Reader/Format/font_face.cpp | 44 +++++++++++++++++++++++ OdfFile/Reader/Format/font_face.h | 27 ++++++++++++++ 7 files changed, 102 insertions(+) diff --git a/OdfFile/Common/odf_elements_type.h b/OdfFile/Common/odf_elements_type.h index f103201224..cc0aa85cd9 100644 --- a/OdfFile/Common/odf_elements_type.h +++ b/OdfFile/Common/odf_elements_type.h @@ -307,6 +307,7 @@ enum ElementType typeStyleFontFace, + typeSvgTitle, typeSvgDesc, typeSvgFontFaceUri, typeSvgFontFaceFormat, diff --git a/OdfFile/Reader/Converter/docx_drawing.cpp b/OdfFile/Reader/Converter/docx_drawing.cpp index f4bdd4e3e2..4499362aed 100644 --- a/OdfFile/Reader/Converter/docx_drawing.cpp +++ b/OdfFile/Reader/Converter/docx_drawing.cpp @@ -288,9 +288,16 @@ void docx_serialize_image_child(std::wostream & strm, _docx_drawing & val) { CP_XML_NODE(L"pic:cNvPr") { + _CP_OPT(std::wstring) title, descr; + GetProperty(val.additional, L"svg:title", title); + GetProperty(val.additional, L"svg:desc", descr); + //CP_XML_ATTR(L"desc text",L""); CP_XML_ATTR(L"id", val.id + 1); CP_XML_ATTR(L"name", val.name); + + CP_XML_ATTR_OPT(L"title", title); + CP_XML_ATTR_OPT(L"descr", descr); //oox_serialize_action(CP_XML_STREAM(), val.action); } @@ -399,8 +406,15 @@ void docx_serialize_common(std::wostream & strm, _docx_drawing & val) { CP_XML_NODE(L"wp:docPr") { + _CP_OPT(std::wstring) title, descr; + GetProperty(val.additional, L"svg:title", title); + GetProperty(val.additional, L"svg:desc", descr); + CP_XML_ATTR(L"name", val.name); CP_XML_ATTR(L"id", 0xf000 + val.id + 1); + + CP_XML_ATTR_OPT(L"title", title); + CP_XML_ATTR_OPT(L"descr", descr); oox_serialize_action(CP_XML_STREAM(), val.action); } diff --git a/OdfFile/Reader/Format/draw_frame.cpp b/OdfFile/Reader/Format/draw_frame.cpp index 89eed08178..53a0079c75 100644 --- a/OdfFile/Reader/Format/draw_frame.cpp +++ b/OdfFile/Reader/Format/draw_frame.cpp @@ -299,6 +299,14 @@ void draw_frame::add_child_element( xml::sax * Reader, const std::wstring & Ns, { CP_CREATE_ELEMENT(draw_contour_); } + else if (CP_CHECK_NAME(L"svg", L"title")) + { + CP_CREATE_ELEMENT(svg_title_); + } + else if (CP_CHECK_NAME(L"svg", L"desc")) + { + CP_CREATE_ELEMENT(svg_desc_); + } else { CP_NOT_APPLICABLE_ELM(); diff --git a/OdfFile/Reader/Format/draw_frame.h b/OdfFile/Reader/Format/draw_frame.h index 0a8dbe524e..6325d12a70 100644 --- a/OdfFile/Reader/Format/draw_frame.h +++ b/OdfFile/Reader/Format/draw_frame.h @@ -177,6 +177,9 @@ public: office_element_ptr draw_contour_; // draw-contour-polygon or draw-contour-path + office_element_ptr svg_title_; + office_element_ptr svg_desc_; + friend class odf_document; friend class draw_image; friend class draw_chart; diff --git a/OdfFile/Reader/Format/draw_frame_docx.cpp b/OdfFile/Reader/Format/draw_frame_docx.cpp index d66a4a97da..010bc35fe7 100644 --- a/OdfFile/Reader/Format/draw_frame_docx.cpp +++ b/OdfFile/Reader/Format/draw_frame_docx.cpp @@ -1578,6 +1578,11 @@ void draw_frame::docx_convert(oox::docx_conversion_context & Context) drawing->id = Context.get_drawing_context().get_current_frame_id(); drawing->name = Context.get_drawing_context().get_current_object_name(); drawing->inGroup = Context.get_drawing_context().in_group(); + + if (svg_title_) + svg_title_->docx_convert(Context); + if(svg_desc_) + svg_desc_->docx_convert(Context); common_draw_docx_convert(Context, common_draw_attlists_, drawing); //----------------------------------------------------------------------------------------------------- diff --git a/OdfFile/Reader/Format/font_face.cpp b/OdfFile/Reader/Format/font_face.cpp index cc8982460a..f2bbd64a75 100644 --- a/OdfFile/Reader/Format/font_face.cpp +++ b/OdfFile/Reader/Format/font_face.cpp @@ -32,15 +32,59 @@ #include "font_face.h" +#include "draw_frame.h" + #include #include "serialize_elements.h" namespace cpdoccore { namespace odf_reader { +// svg:title +//--------------------------------------------------------------------------------------- +const wchar_t* svg_title::ns = L"svg"; +const wchar_t* svg_title::name = L"title"; + +void svg_title::docx_convert(oox::docx_conversion_context& Context) +{ + odf_reader::draw_frame* current_frame = Context.get_drawing_context().get_current_frame(); + + if (current_frame && current_frame->oox_drawing_) + { + current_frame->oox_drawing_->additional.push_back(odf_reader::_property(L"svg:title", text_)); + } +} + +std::wostream& svg_title::text_to_stream(std::wostream& _Wostream, bool bXmlEncode) const +{ + _Wostream << text_; + return _Wostream; +} + +void svg_title::add_text(const std::wstring& Text) +{ + text_ += Text; +} +void svg_title::add_space(const std::wstring& Text) +{ + text_ += Text; +} +// svg:desc +//--------------------------------------------------------------------------------------- + const wchar_t * svg_desc::ns = L"svg"; const wchar_t * svg_desc::name = L"desc"; +void svg_desc::docx_convert(oox::docx_conversion_context& Context) +{ + odf_reader::draw_frame* current_frame = Context.get_drawing_context().get_current_frame(); + + if (current_frame && current_frame->oox_drawing_) + { + current_frame->oox_drawing_->additional.push_back(odf_reader::_property(L"svg:desc", text_)); + } +} + std::wostream & svg_desc::text_to_stream(std::wostream & _Wostream, bool bXmlEncode) const { _Wostream << text_ ; diff --git a/OdfFile/Reader/Format/font_face.h b/OdfFile/Reader/Format/font_face.h index 5c78d56b4b..7ff84f9b13 100644 --- a/OdfFile/Reader/Format/font_face.h +++ b/OdfFile/Reader/Format/font_face.h @@ -78,6 +78,31 @@ private: }; CP_REGISTER_OFFICE_ELEMENT2(svg_font_face_uri); +// svg:title +class svg_title : public office_element_impl +{ +public: + static const wchar_t* ns; + static const wchar_t* name; + static const xml::NodeType xml_type = xml::typeElement; + static const ElementType type = typeSvgTitle; + + CPDOCCORE_DEFINE_VISITABLE(); + + void docx_convert(oox::docx_conversion_context& Context); + + virtual std::wostream& text_to_stream(std::wostream& _Wostream, bool bXmlEncode = true) const; + + std::wstring text_; + +private: + virtual void add_attributes(const xml::attributes_wc_ptr& Attributes) {} + virtual void add_child_element(xml::sax* Reader, const std::wstring& Ns, const std::wstring& Name) {} + virtual void add_text(const std::wstring& Text); + virtual void add_space(const std::wstring& Text); +}; +CP_REGISTER_OFFICE_ELEMENT2(svg_title); + // svg:desc class svg_desc : public office_element_impl { @@ -89,6 +114,8 @@ public: CPDOCCORE_DEFINE_VISITABLE(); + void docx_convert(oox::docx_conversion_context& Context); + virtual std::wostream & text_to_stream(std::wostream & _Wostream, bool bXmlEncode = true) const; std::wstring text_;