Line data Source code
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 <xmloff/xmlmultiimagehelper.hxx>
21 : #include <rtl/ustring.hxx>
22 :
23 : using namespace ::com::sun::star;
24 :
25 : namespace
26 : {
27 0 : sal_uInt32 getQualityIndex(const OUString& rString)
28 : {
29 0 : sal_uInt32 nRetval(0);
30 :
31 : // pixel formats first
32 0 : if(rString.endsWithAsciiL(".bmp", 4))
33 : {
34 0 : return 10;
35 : }
36 0 : if(rString.endsWithAsciiL(".gif", 4))
37 : {
38 0 : return 20;
39 : }
40 0 : if(rString.endsWithAsciiL(".jpg", 4))
41 : {
42 0 : return 30;
43 : }
44 0 : if(rString.endsWithAsciiL(".png", 4))
45 : {
46 0 : return 40;
47 : }
48 :
49 : // vector formats, prefer always
50 0 : if(rString.endsWithAsciiL(".svm", 4))
51 : {
52 0 : return 1000;
53 : }
54 0 : if(rString.endsWithAsciiL(".wmf", 4))
55 : {
56 0 : return 1010;
57 : }
58 0 : if(rString.endsWithAsciiL(".emf", 4))
59 : {
60 0 : return 1020;
61 : }
62 0 : else if(rString.endsWithAsciiL(".svg", 4))
63 : {
64 0 : return 1030;
65 : }
66 :
67 0 : return nRetval;
68 : }
69 : }
70 :
71 0 : MultiImageImportHelper::MultiImageImportHelper()
72 : : maImplContextVector(),
73 0 : mbSupportsMultipleContents(false)
74 : {
75 0 : }
76 :
77 0 : MultiImageImportHelper::~MultiImageImportHelper()
78 : {
79 0 : while(!maImplContextVector.empty())
80 : {
81 0 : delete *(maImplContextVector.end() - 1);
82 0 : maImplContextVector.pop_back();
83 : }
84 0 : }
85 :
86 0 : SvXMLImportContextRef MultiImageImportHelper::solveMultipleImages()
87 : {
88 0 : SvXMLImportContextRef pContext;
89 0 : if(maImplContextVector.size() > 1)
90 : {
91 : // multiple child contexts were imported, decide which is the most valuable one
92 : // and remove the rest
93 0 : sal_uInt32 nIndexOfPreferred(maImplContextVector.size());
94 0 : sal_uInt32 nBestQuality(0), a(0);
95 :
96 0 : for(a = 0; a < maImplContextVector.size(); a++)
97 : {
98 0 : const OUString aStreamURL(getGraphicURLFromImportContext(**maImplContextVector[a]));
99 0 : const sal_uInt32 nNewQuality(getQualityIndex(aStreamURL));
100 :
101 0 : if(nNewQuality > nBestQuality)
102 : {
103 0 : nBestQuality = nNewQuality;
104 0 : nIndexOfPreferred = a;
105 : }
106 0 : }
107 :
108 : // correct if needed, default is to use the last entry
109 0 : if(nIndexOfPreferred >= maImplContextVector.size())
110 : {
111 0 : nIndexOfPreferred = maImplContextVector.size() - 1;
112 : }
113 :
114 : // Take out the most valuable one
115 0 : const std::vector< SvXMLImportContextRef* >::iterator aRemove(maImplContextVector.begin() + nIndexOfPreferred);
116 0 : pContext = **aRemove;
117 0 : delete *aRemove;
118 0 : maImplContextVector.erase(aRemove);
119 :
120 : // remove the rest from parent
121 0 : for(a = 0; a < maImplContextVector.size(); a++)
122 : {
123 0 : SvXMLImportContext& rCandidate = **maImplContextVector[a];
124 :
125 0 : if(pContext)
126 : {
127 : // #i124143# evtl. copy imported GluePoints before deprecating
128 : // this graphic and context
129 0 : pContext->onDemandRescueUsefulDataFromTemporary(rCandidate);
130 : }
131 :
132 0 : removeGraphicFromImportContext(rCandidate);
133 : }
134 : }
135 0 : else if (maImplContextVector.size() == 1)
136 : {
137 0 : pContext = *maImplContextVector.front();
138 : }
139 :
140 0 : return pContext;
141 : }
142 :
143 0 : void MultiImageImportHelper::addContent(const SvXMLImportContext& rSvXMLImportContext)
144 : {
145 0 : if(dynamic_cast< const SvXMLImportContext* >(&rSvXMLImportContext))
146 : {
147 0 : maImplContextVector.push_back(new SvXMLImportContextRef(const_cast< SvXMLImportContext* >(&rSvXMLImportContext)));
148 : }
149 0 : }
150 :
151 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|