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