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>
22#include <o3tl/string_view.hxx>
24
25using namespace ::com::sun::star;
26
27namespace
28{
29 OUString getMimeTypeForURL(std::u16string_view rString)
30 {
31 OUString sMimeType;
32 if (o3tl::starts_with(rString, u"vnd.sun.star.Package"))
33 {
34 OString aExtension = OUStringToOString(rString.substr(rString.rfind('.') + 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: mbSupportsMultipleContents(false)
96{
97}
98
100{
101}
102
104{
105 SvXMLImportContextRef pContext;
106 if(maImplContextVector.size() > 1)
107 {
108 // multiple child contexts were imported, decide which is the most valuable one
109 // and remove the rest
110 std::vector<SvXMLImportContextRef>::size_type nIndexOfPreferred(maImplContextVector.size());
111 sal_uInt32 nBestQuality(0);
112
113 for(std::vector<SvXMLImportContextRef>::size_type a = 0; a < maImplContextVector.size(); a++)
114 {
115 const SvXMLImportContext& rContext = *maImplContextVector[a];
116
117
118 OUString sMimeType;
119
120 const OUString aStreamURL(getGraphicPackageURLFromImportContext(rContext));
121 if (!aStreamURL.isEmpty())
122 {
123 sMimeType = getMimeTypeForURL(aStreamURL);
124 }
125 else
126 {
127 uno::Reference<graphic::XGraphic> xGraphic(getGraphicFromImportContext(rContext));
128 if (xGraphic.is())
130 }
131
132 sal_uInt32 nNewQuality = getQualityIndex(sMimeType);
133 if (nNewQuality > nBestQuality)
134 {
135 nBestQuality = nNewQuality;
136 nIndexOfPreferred = a;
137 }
138 }
139
140 // correct if needed, default is to use the last entry
141 if(nIndexOfPreferred >= maImplContextVector.size())
142 {
143 nIndexOfPreferred = maImplContextVector.size() - 1;
144 }
145
146 // Take out the most valuable one
147 const std::vector< SvXMLImportContextRef >::iterator aRemove(maImplContextVector.begin() + nIndexOfPreferred);
148 pContext = *aRemove;
149 maImplContextVector.erase(aRemove);
150
151 // remove the rest from parent
152 for(std::vector<SvXMLImportContextRef>::size_type a = 0; a < maImplContextVector.size(); a++)
153 {
155
157 }
158 // re-insert it so that solveMultipleImages is idempotent
159 maImplContextVector.clear();
160 maImplContextVector.push_back(pContext);
161 }
162 else if (maImplContextVector.size() == 1)
163 {
164 pContext = maImplContextVector.front();
165 }
166
167 return pContext;
168}
169
171{
172 maImplContextVector.emplace_back(const_cast< SvXMLImportContext* >(&rSvXMLImportContext));
173}
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void removeGraphicFromImportContext(const SvXMLImportContext &rContext)=0
helper to get the created xShape instance, override this
std::vector< SvXMLImportContextRef > maImplContextVector
virtual OUString getGraphicPackageURLFromImportContext(const SvXMLImportContext &rContext) const =0
virtual css::uno::Reference< css::graphic::XGraphic > getGraphicFromImportContext(const SvXMLImportContext &rContext) const =0
SvXMLImportContextRef solveMultipleImages()
solve multiple imported images.
void addContent(const SvXMLImportContext &rSvXMLImportContext)
add a content to the remembered image import contexts
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
static OUString GetMimeTypeForXGraphic(const css::uno::Reference< css::graphic::XGraphic > &xGraphic)
static OUString GetMimeTypeForExtension(std::string_view rExt)
uno_Any a
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)