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 const std::string_view::size_type nLastDot
= rString
.rfind('.');
35 const auto nAfterDot
= (nLastDot
!= std::string_view::npos
) ? (nLastDot
+ 1) : 0;
36 OString aExtension
= OUStringToOString(rString
.substr(nAfterDot
), RTL_TEXTENCODING_ASCII_US
);
37 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension(aExtension
);
42 sal_uInt32
getQualityIndex(std::u16string_view rMimeType
)
44 // pixel formats first
45 if (rMimeType
== u
"image/bmp")
49 if (rMimeType
== u
"image/gif")
53 if (rMimeType
== u
"image/jpeg")
57 if (rMimeType
== u
"image/png")
62 // vector formats, prefer always
63 if (rMimeType
== u
"image/x-vclgraphic") // MIMETYPE_VCLGRAPHIC
67 if (rMimeType
== u
"image/x-svm")
71 if (rMimeType
== u
"image/x-wmf")
75 if (rMimeType
== u
"image/x-emf")
79 if (rMimeType
== u
"image/x-eps")
83 if (rMimeType
== u
"application/pdf")
87 if (rMimeType
== u
"image/svg+xml")
96 MultiImageImportHelper::MultiImageImportHelper()
97 : mbSupportsMultipleContents(false)
101 MultiImageImportHelper::~MultiImageImportHelper()
105 SvXMLImportContextRef
MultiImageImportHelper::solveMultipleImages()
107 SvXMLImportContextRef pContext
;
108 if(maImplContextVector
.size() > 1)
110 // multiple child contexts were imported, decide which is the most valuable one
111 // and remove the rest
112 std::vector
<SvXMLImportContextRef
>::size_type
nIndexOfPreferred(maImplContextVector
.size());
113 sal_uInt32
nBestQuality(0);
115 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
117 const SvXMLImportContext
& rContext
= *maImplContextVector
[a
];
119 // First try the mime type provided by the document
120 OUString sMimeType
= getMimeTypeFromImportContext(rContext
);
121 if (sMimeType
.isEmpty())
123 // Try to determine the mime type from the extension
124 const OUString
aStreamURL(getGraphicPackageURLFromImportContext(rContext
));
125 if (!aStreamURL
.isEmpty())
127 sMimeType
= getMimeTypeForURL(aStreamURL
);
131 // Try to determine the mime type from the graphic itself
132 uno::Reference
<graphic::XGraphic
> xGraphic(getGraphicFromImportContext(rContext
));
134 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForXGraphic(xGraphic
);
138 sal_uInt32 nNewQuality
= getQualityIndex(sMimeType
);
139 if (nNewQuality
> nBestQuality
)
141 nBestQuality
= nNewQuality
;
142 nIndexOfPreferred
= a
;
146 // correct if needed, default is to use the last entry
147 if(nIndexOfPreferred
>= maImplContextVector
.size())
149 nIndexOfPreferred
= maImplContextVector
.size() - 1;
152 // Take out the most valuable one
153 const std::vector
< SvXMLImportContextRef
>::iterator
aRemove(maImplContextVector
.begin() + nIndexOfPreferred
);
155 maImplContextVector
.erase(aRemove
);
157 // remove the rest from parent
158 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
160 SvXMLImportContext
& rCandidate
= *maImplContextVector
[a
];
162 removeGraphicFromImportContext(rCandidate
);
164 // re-insert it so that solveMultipleImages is idempotent
165 maImplContextVector
.clear();
166 maImplContextVector
.push_back(pContext
);
168 else if (maImplContextVector
.size() == 1)
170 pContext
= maImplContextVector
.front();
176 void MultiImageImportHelper::addContent(const SvXMLImportContext
& rSvXMLImportContext
)
178 maImplContextVector
.emplace_back(const_cast< SvXMLImportContext
* >(&rSvXMLImportContext
));
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */