LibreOffice Module xmloff (master)  1
ImageStyle.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <xmloff/ImageStyle.hxx>
21 #include <com/sun/star/awt/XBitmap.hpp>
22 #include <com/sun/star/graphic/XGraphic.hpp>
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlnamespace.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <xmloff/xmlexp.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <rtl/ustring.hxx>
29 #include <sal/log.hxx>
30 #include <xmloff/xmltkmap.hxx>
31 
32 using namespace css;
33 using namespace xmloff::token;
34 
35 void XMLImageStyle::exportXML(OUString const & rStrName, uno::Any const & rValue, SvXMLExport& rExport)
36 {
37  if (rStrName.isEmpty())
38  return;
39 
40  if (!rValue.has<uno::Reference<awt::XBitmap>>())
41  return;
42 
43  // Name
44  bool bEncoded = false;
46  rExport.EncodeStyleName(rStrName, &bEncoded));
47  if (bEncoded)
48  {
50  }
51 
52  auto xBitmap = rValue.get<uno::Reference<awt::XBitmap>>();
53  uno::Reference<graphic::XGraphic> xGraphic(xBitmap, uno::UNO_QUERY);
54 
55  OUString aMimeType;
56  const OUString aStr = rExport.AddEmbeddedXGraphic(xGraphic, aMimeType);
57 
58  // uri
59  if (!aStr.isEmpty())
60  {
61  rExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, aStr );
65  }
66 
67  // Do Write
68  SvXMLElementExport aElem(rExport, XML_NAMESPACE_DRAW, XML_FILL_IMAGE, true, true);
69 
70  if (xBitmap.is() && xGraphic.is())
71  {
72  // optional office:binary-data
73  rExport.AddEmbeddedXGraphicAsBase64(xGraphic);
74  }
75 }
76 
77 bool XMLImageStyle::importXML(uno::Reference<xml::sax::XFastAttributeList> const & xAttrList,
78  uno::Any& rValue, OUString& rStrName, SvXMLImport& rImport)
79 {
80  bool bHasHRef = false;
81  bool bHasName = false;
82  OUString aDisplayName;
83  uno::Reference<graphic::XGraphic> xGraphic;
84 
85  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
86  {
87  const OUString aStrValue = aIter.toString();
88 
89  switch( aIter.getToken() )
90  {
91  case XML_ELEMENT(DRAW, XML_NAME):
92  {
93  rStrName = aStrValue;
94  bHasName = true;
95  }
96  break;
97  case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
98  {
99  aDisplayName = aStrValue;
100  }
101  break;
102  case XML_ELEMENT(XLINK, XML_HREF):
103  {
104  xGraphic = rImport.loadGraphicByURL(aStrValue);
105  bHasHRef = true;
106  }
107  break;
108  case XML_ELEMENT(XLINK, XML_TYPE):
109  // ignore
110  break;
111  case XML_ELEMENT(XLINK, XML_SHOW):
112  // ignore
113  break;
114  case XML_ELEMENT(XLINK, XML_ACTUATE):
115  // ignore
116  break;
117  default:
118  XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
119  }
120  }
121 
122  if (xGraphic.is())
123  rValue <<= xGraphic;
124 
125  if( !aDisplayName.isEmpty() )
126  {
128  rStrName, aDisplayName );
129  rStrName = aDisplayName;
130  }
131 
132  return bHasName && bHasHRef;
133 }
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_uInt16 XML_NAMESPACE_XLINK
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
css::uno::Reference< css::graphic::XGraphic > loadGraphicByURL(OUString const &rURL)
Definition: xmlimp.cxx:1328
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:131
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:909
constexpr sal_uInt16 XML_NAMESPACE_DRAW
OUString AddEmbeddedXGraphic(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic, OUString &rOutMimeType, OUString const &rRequestedName=OUString())
Definition: xmlexp.cxx:1858
const char *const aMimeType[]
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1954
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1432
bool AddEmbeddedXGraphicAsBase64(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic)
Definition: xmlexp.cxx:1895
css::uno::Any const SvXMLExport & rExport
Definition: ImageStyle.hxx:38
Reference< XGraphic > xGraphic
aStr