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>
23 #include <comphelper/graphicmimetype.hxx>
25 using namespace ::com::sun::star
;
29 OUString
getMimeTypeForURL(const OUString
& rString
)
32 if (rString
.startsWith("vnd.sun.star.Package"))
34 OString aExtension
= OUStringToOString(rString
.copy(rString
.lastIndexOf(".") + 1), RTL_TEXTENCODING_ASCII_US
);
35 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension(aExtension
);
40 sal_uInt32
getQualityIndex(const OUString
& rMimeType
)
42 // pixel formats first
43 if (rMimeType
== "image/bmp")
47 if (rMimeType
== "image/gif")
51 if (rMimeType
== "image/jpeg")
55 if (rMimeType
== "image/png")
60 // vector formats, prefer always
61 if (rMimeType
== "image/x-vclgraphic") // MIMETYPE_VCLGRAPHIC
65 if (rMimeType
== "image/x-svm")
69 if (rMimeType
== "image/x-wmf")
73 if (rMimeType
== "image/x-emf")
77 if (rMimeType
== "image/x-eps")
81 if (rMimeType
== "application/pdf")
85 if (rMimeType
== "image/svg+xml")
94 MultiImageImportHelper::MultiImageImportHelper()
95 : maImplContextVector(),
96 mbSupportsMultipleContents(false)
100 MultiImageImportHelper::~MultiImageImportHelper()
104 SvXMLImportContextRef
MultiImageImportHelper::solveMultipleImages()
106 SvXMLImportContextRef pContext
;
107 if(maImplContextVector
.size() > 1)
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);
114 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
116 const SvXMLImportContext
& rContext
= *maImplContextVector
[a
];
121 const OUString
aStreamURL(getGraphicPackageURLFromImportContext(rContext
));
122 if (!aStreamURL
.isEmpty())
124 sMimeType
= getMimeTypeForURL(aStreamURL
);
128 uno::Reference
<graphic::XGraphic
> xGraphic(getGraphicFromImportContext(rContext
));
130 sMimeType
= comphelper::GraphicMimeTypeHelper::GetMimeTypeForXGraphic(xGraphic
);
133 sal_uInt32 nNewQuality
= getQualityIndex(sMimeType
);
134 if (nNewQuality
> nBestQuality
)
136 nBestQuality
= nNewQuality
;
137 nIndexOfPreferred
= a
;
141 // correct if needed, default is to use the last entry
142 if(nIndexOfPreferred
>= maImplContextVector
.size())
144 nIndexOfPreferred
= maImplContextVector
.size() - 1;
147 // Take out the most valuable one
148 const std::vector
< SvXMLImportContextRef
>::iterator
aRemove(maImplContextVector
.begin() + nIndexOfPreferred
);
150 maImplContextVector
.erase(aRemove
);
152 // remove the rest from parent
153 for(std::vector
<SvXMLImportContextRef
>::size_type a
= 0; a
< maImplContextVector
.size(); a
++)
155 SvXMLImportContext
& rCandidate
= *maImplContextVector
[a
];
157 removeGraphicFromImportContext(rCandidate
);
159 // re-insert it so that solveMultipleImages is idempotent
160 maImplContextVector
.clear();
161 maImplContextVector
.push_back(pContext
);
163 else if (maImplContextVector
.size() == 1)
165 pContext
= maImplContextVector
.front();
171 void MultiImageImportHelper::addContent(const SvXMLImportContext
& rSvXMLImportContext
)
173 maImplContextVector
.emplace_back(const_cast< SvXMLImportContext
* >(&rSvXMLImportContext
));
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */