LibreOffice Module xmloff (master)  1
xmlmultiimagehelper.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 <rtl/ustring.hxx>
21 #include <xmlmultiimagehelper.hxx>
22 
24 
25 using namespace ::com::sun::star;
26 
27 namespace
28 {
29  OUString getMimeTypeForURL(const OUString& rString)
30  {
31  OUString sMimeType;
32  if (rString.startsWith("vnd.sun.star.Package"))
33  {
34  OString aExtension = OUStringToOString(rString.copy(rString.lastIndexOf(".") + 1), RTL_TEXTENCODING_ASCII_US);
36  }
37  return sMimeType;
38  }
39 
40  sal_uInt32 getQualityIndex(std::u16string_view rMimeType)
41  {
42  // pixel formats first
43  if (rMimeType == u"image/bmp")
44  {
45  return 10;
46  }
47  if (rMimeType == u"image/gif")
48  {
49  return 20;
50  }
51  if (rMimeType == u"image/jpeg")
52  {
53  return 30;
54  }
55  if (rMimeType == u"image/png")
56  {
57  return 40;
58  }
59 
60  // vector formats, prefer always
61  if (rMimeType == u"image/x-vclgraphic") // MIMETYPE_VCLGRAPHIC
62  {
63  return 990;
64  }
65  if (rMimeType == u"image/x-svm")
66  {
67  return 1000;
68  }
69  if (rMimeType == u"image/x-wmf")
70  {
71  return 1010;
72  }
73  if (rMimeType == u"image/x-emf")
74  {
75  return 1020;
76  }
77  if (rMimeType == u"image/x-eps")
78  {
79  return 1025;
80  }
81  if (rMimeType == u"application/pdf")
82  {
83  return 1030;
84  }
85  if (rMimeType == u"image/svg+xml")
86  {
87  return 1040;
88  }
89 
90  return 0;
91  }
92 }
93 
95 : maImplContextVector(),
96  mbSupportsMultipleContents(false)
97 {
98 }
99 
101 {
102 }
103 
105 {
106  SvXMLImportContextRef pContext;
107  if(maImplContextVector.size() > 1)
108  {
109  // multiple child contexts were imported, decide which is the most valuable one
110  // and remove the rest
111  std::vector<SvXMLImportContextRef>::size_type nIndexOfPreferred(maImplContextVector.size());
112  sal_uInt32 nBestQuality(0);
113 
114  for(std::vector<SvXMLImportContextRef>::size_type a = 0; a < maImplContextVector.size(); a++)
115  {
116  const SvXMLImportContext& rContext = *maImplContextVector[a];
117 
118 
119  OUString sMimeType;
120 
121  const OUString aStreamURL(getGraphicPackageURLFromImportContext(rContext));
122  if (!aStreamURL.isEmpty())
123  {
124  sMimeType = getMimeTypeForURL(aStreamURL);
125  }
126  else
127  {
128  uno::Reference<graphic::XGraphic> xGraphic(getGraphicFromImportContext(rContext));
129  if (xGraphic.is())
131  }
132 
133  sal_uInt32 nNewQuality = getQualityIndex(sMimeType);
134  if (nNewQuality > nBestQuality)
135  {
136  nBestQuality = nNewQuality;
137  nIndexOfPreferred = a;
138  }
139  }
140 
141  // correct if needed, default is to use the last entry
142  if(nIndexOfPreferred >= maImplContextVector.size())
143  {
144  nIndexOfPreferred = maImplContextVector.size() - 1;
145  }
146 
147  // Take out the most valuable one
148  const std::vector< SvXMLImportContextRef >::iterator aRemove(maImplContextVector.begin() + nIndexOfPreferred);
149  pContext = *aRemove;
150  maImplContextVector.erase(aRemove);
151 
152  // remove the rest from parent
153  for(std::vector<SvXMLImportContextRef>::size_type a = 0; a < maImplContextVector.size(); a++)
154  {
155  SvXMLImportContext& rCandidate = *maImplContextVector[a];
156 
157  removeGraphicFromImportContext(rCandidate);
158  }
159  // re-insert it so that solveMultipleImages is idempotent
160  maImplContextVector.clear();
161  maImplContextVector.push_back(pContext);
162  }
163  else if (maImplContextVector.size() == 1)
164  {
165  pContext = maImplContextVector.front();
166  }
167 
168  return pContext;
169 }
170 
172 {
173  maImplContextVector.emplace_back(const_cast< SvXMLImportContext* >(&rSvXMLImportContext));
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GetMimeTypeForXGraphic(const css::uno::Reference< css::graphic::XGraphic > &xGraphic)
std::vector< SvXMLImportContextRef > maImplContextVector
virtual void removeGraphicFromImportContext(const SvXMLImportContext &rContext)=0
helper to get the created xShape instance, override this
virtual css::uno::Reference< css::graphic::XGraphic > getGraphicFromImportContext(const SvXMLImportContext &rContext) const =0
virtual OUString getGraphicPackageURLFromImportContext(const SvXMLImportContext &rContext) const =0
void addContent(const SvXMLImportContext &rSvXMLImportContext)
add a content to the remembered image import contexts
uno_Any a
static OUString GetMimeTypeForExtension(const OString &rExt)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
SvXMLImportContextRef solveMultipleImages()
solve multiple imported images.
Reference< XGraphic > xGraphic