nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / core / xmlmultiimagehelper.cxx
blobaf03e7bb136192278e088649de2af80908567834
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
27 namespace
29 OUString getMimeTypeForURL(const OUString& rString)
31 OUString sMimeType;
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);
37 return sMimeType;
40 sal_uInt32 getQualityIndex(const OUString& rMimeType)
42 // pixel formats first
43 if (rMimeType == "image/bmp")
45 return 10;
47 if (rMimeType == "image/gif")
49 return 20;
51 if (rMimeType == "image/jpeg")
53 return 30;
55 if (rMimeType == "image/png")
57 return 40;
60 // vector formats, prefer always
61 if (rMimeType == "image/x-vclgraphic") // MIMETYPE_VCLGRAPHIC
63 return 990;
65 if (rMimeType == "image/x-svm")
67 return 1000;
69 if (rMimeType == "image/x-wmf")
71 return 1010;
73 if (rMimeType == "image/x-emf")
75 return 1020;
77 if (rMimeType == "image/x-eps")
79 return 1025;
81 if (rMimeType == "application/pdf")
83 return 1030;
85 if (rMimeType == "image/svg+xml")
87 return 1040;
90 return 0;
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];
119 OUString sMimeType;
121 const OUString aStreamURL(getGraphicPackageURLFromImportContext(rContext));
122 if (!aStreamURL.isEmpty())
124 sMimeType = getMimeTypeForURL(aStreamURL);
126 else
128 uno::Reference<graphic::XGraphic> xGraphic(getGraphicFromImportContext(rContext));
129 if (xGraphic.is())
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);
149 pContext = *aRemove;
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();
168 return pContext;
171 void MultiImageImportHelper::addContent(const SvXMLImportContext& rSvXMLImportContext)
173 maImplContextVector.emplace_back(const_cast< SvXMLImportContext* >(&rSvXMLImportContext));
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */