1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.hxx>
21 #include <xmlmultiimagehelper.hxx>
22 #include <o3tl/string_view.hxx>
23 #include <comphelper/graphicmimetype.hxx>
25 using namespace ::com::sun::star
;
29 OUString
getMimeTypeForURL(std::u16string_view rString
)
32 if (o3tl::starts_with(rString
, u
"vnd.sun.star.Package"))
34 OString aExtension
= OUStringToOString(rString
.substr(rString
.rfind('.') + 1), RTL_TEXTENCODING_ASCII_US
);
35 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension(aExtension
);
40 sal_uInt32
getQualityIndex(std::u16string_view rMimeType
)
42 // pixel formats first
43 if (rMimeType
== u
"image/bmp")
47 if (rMimeType
== u
"image/gif")
51 if (rMimeType
== u
"image/jpeg")
55 if (rMimeType
== u
"image/png")
60 // vector formats, prefer always
61 if (rMimeType
== u
"image/x-vclgraphic") // MIMETYPE_VCLGRAPHIC
65 if (rMimeType
== u
"image/x-svm")
69 if (rMimeType
== u
"image/x-wmf")
73 if (rMimeType
== u
"image/x-emf")
77 if (rMimeType
== u
"image/x-eps")
81 if (rMimeType
== u
"application/pdf")
85 if (rMimeType
== u
"image/svg+xml")
94 MultiImageImportHelper::MultiImageImportHelper()
95 : mbSupportsMultipleContents(false)
99 MultiImageImportHelper::~MultiImageImportHelper()
103 SvXMLImportContextRef
MultiImageImportHelper::solveMultipleImages()
105 SvXMLImportContextRef pContext
;
106 if(maImplContextVector
.size() > 1)
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);
113 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
115 const SvXMLImportContext
& rContext
= *maImplContextVector
[a
];
117 // First try the mime type provided by the document
118 OUString sMimeType
= getMimeTypeFromImportContext(rContext
);
119 if (sMimeType
.isEmpty())
121 // Try to determine the mime type from the extension
122 const OUString
aStreamURL(getGraphicPackageURLFromImportContext(rContext
));
123 if (!aStreamURL
.isEmpty())
125 sMimeType
= getMimeTypeForURL(aStreamURL
);
129 // Try to determine the mime type from the graphic itself
130 uno::Reference
<graphic::XGraphic
> xGraphic(getGraphicFromImportContext(rContext
));
132 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForXGraphic(xGraphic
);
136 sal_uInt32 nNewQuality
= getQualityIndex(sMimeType
);
137 if (nNewQuality
> nBestQuality
)
139 nBestQuality
= nNewQuality
;
140 nIndexOfPreferred
= a
;
144 // correct if needed, default is to use the last entry
145 if(nIndexOfPreferred
>= maImplContextVector
.size())
147 nIndexOfPreferred
= maImplContextVector
.size() - 1;
150 // Take out the most valuable one
151 const std::vector
< SvXMLImportContextRef
>::iterator
aRemove(maImplContextVector
.begin() + nIndexOfPreferred
);
153 maImplContextVector
.erase(aRemove
);
155 // remove the rest from parent
156 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
158 SvXMLImportContext
& rCandidate
= *maImplContextVector
[a
];
160 removeGraphicFromImportContext(rCandidate
);
162 // re-insert it so that solveMultipleImages is idempotent
163 maImplContextVector
.clear();
164 maImplContextVector
.push_back(pContext
);
166 else if (maImplContextVector
.size() == 1)
168 pContext
= maImplContextVector
.front();
174 void MultiImageImportHelper::addContent(const SvXMLImportContext
& rSvXMLImportContext
)
176 maImplContextVector
.emplace_back(const_cast< SvXMLImportContext
* >(&rSvXMLImportContext
));
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */