]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
fix math fonts with LyX/Mac
[lyx.git] / src / frontends / qt2 / QLImage.C
1 /**
2  * \file QLImage.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QLImage.h"
15 #include "qt_helpers.h"
16
17 #include "debug.h"
18 #include "format.h"
19
20 #include "graphics/GraphicsParams.h"
21
22 #include "support/lstrings.h"       // lowercase
23 #include "support/lyxfunctional.h"  // compare_memfun
24
25 #include <qimage.h>
26 #include <qpainter.h>
27
28 #include <boost/tuple/tuple.hpp>
29
30 using lyx::support::lowercase;
31
32 using std::endl;
33 using std::find_if;
34 using std::string;
35
36
37 namespace lyx {
38 namespace graphics {
39
40 /// Access to this class is through this static method.
41 Image::ImagePtr QLImage::newImage()
42 {
43         ImagePtr ptr;
44         ptr.reset(new QLImage);
45         return ptr;
46 }
47
48
49 /// Return the list of loadable formats.
50 Image::FormatList QLImage::loadableFormats()
51 {
52         static FormatList fmts;
53
54         if (!fmts.empty())
55                 return fmts;
56
57         // The formats recognised by LyX
58         Formats::const_iterator begin = formats.begin();
59         Formats::const_iterator end   = formats.end();
60
61         lyxerr[Debug::GRAPHICS]
62                 << "\nThe image loader can load the following directly:\n";
63
64         QStrList qt_formats = QImageIO::inputFormats();
65
66         QStrListIterator it(qt_formats);
67
68         for (; it.current(); ++it) {
69                 lyxerr[Debug::GRAPHICS] << it.current() << endl;
70
71                 string ext = lowercase(it.current());
72
73                 // special case
74                 if (ext == "jpeg")
75                         ext = "jpg";
76
77                 Formats::const_iterator fit =
78                         find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
79                 if (fit != end)
80                         fmts.push_back(fit->name());
81         }
82
83         if (lyxerr.debugging()) {
84                 lyxerr[Debug::GRAPHICS]
85                         << "\nOf these, LyX recognises the following formats:\n";
86
87                 FormatList::const_iterator fbegin = fmts.begin();
88                 FormatList::const_iterator fend   = fmts.end();
89                 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
90                         if (fit != fbegin)
91                                 lyxerr[Debug::GRAPHICS] << ", ";
92                         lyxerr[Debug::GRAPHICS] << *fit;
93                 }
94                 lyxerr[Debug::GRAPHICS] << '\n' << endl;
95         }
96
97         return fmts;
98 }
99
100
101 QLImage::QLImage()
102         : Image()
103 {
104 }
105
106
107 QLImage::QLImage(QLImage const & other)
108         : Image(other), original_(other.original_),
109           transformed_(other.original_)
110 {}
111
112
113 QLImage::~QLImage()
114 {
115 }
116
117
118 Image * QLImage::clone_impl() const
119 {
120         return new QLImage(*this);
121 }
122
123
124 unsigned int QLImage::getWidth_impl() const
125 {
126         return transformed_.width();
127 }
128
129
130 unsigned int QLImage::getHeight_impl() const
131 {
132         return transformed_.height();
133 }
134
135
136 void QLImage::load_impl(string const & filename)
137 {
138         if (!original_.isNull()) {
139                 lyxerr[Debug::GRAPHICS]
140                         << "Image is loaded already!" << endl;
141                 finishedLoading(false);
142                 return;
143         }
144
145         if (!original_.load(toqstr(filename))) {
146                 lyxerr[Debug::GRAPHICS]
147                         << "Unable to open image" << endl;
148                 finishedLoading(false);
149                 return;
150         }
151         transformed_ = original_;
152         finishedLoading(true);
153 }
154
155
156 namespace {
157
158 // This code is taken from KImageEffect::toGray
159 QImage & toGray(QImage & img)
160 {
161         if (img.width() == 0 || img.height() == 0)
162                 return img;
163
164         int const pixels = img.depth() > 8 ?
165                 img.width() * img.height() : img.numColors();
166
167         unsigned int * const data = img.depth() > 8 ?
168                 (unsigned int *)img.bits() :
169                 (unsigned int *)img.colorTable();
170
171         for(int i = 0; i < pixels; ++i){
172                 int const val = qGray(data[i]);
173                 data[i] = qRgba(val, val, val, qAlpha(data[i]));
174         }
175         return img;
176 }
177
178 } // namespace anon
179
180
181 bool QLImage::setPixmap_impl(Params const & params)
182 {
183         if (original_.isNull() || params.display == NoDisplay)
184                 return false;
185
186         switch (params.display) {
187         case GrayscaleDisplay: {
188                 toGray(transformed_);
189                 break;
190         }
191
192         case MonochromeDisplay: {
193                 transformed_.convertDepth(transformed_.depth(), Qt::MonoOnly);
194                 break;
195         }
196
197         default:
198                 break;
199         }
200
201         return true;
202 }
203
204
205 void QLImage::clip_impl(Params const & params)
206 {
207         if (transformed_.isNull())
208                 return;
209
210         if (params.bb.empty())
211                 // No clipping is necessary.
212                 return;
213
214         int const new_width  = params.bb.xr - params.bb.xl;
215         int const new_height = params.bb.yt - params.bb.yb;
216
217         // No need to check if the width, height are > 0 because the
218         // Bounding Box would be empty() in this case.
219         if (new_width > original_.width() || new_height > original_.height()) {
220                 // Bounds are invalid.
221                 return;
222         }
223
224         if (new_width == original_.width() && new_height == original_.height())
225                 return;
226
227         int const xoffset_l = params.bb.xl;
228         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
229                                original_.height() - params.bb.yt : 0);
230
231         transformed_ = original_.copy(xoffset_l, yoffset_t,
232                                       new_width, new_height);
233 }
234
235
236 void QLImage::rotate_impl(Params const & params)
237 {
238         if (transformed_.isNull())
239                 return;
240
241         if (!params.angle)
242                 return;
243
244         QWMatrix m;
245         m.rotate(-params.angle);
246
247         transformed_.setAlphaBuffer(true);
248         transformed_ = transformed_.xForm(m);
249 }
250
251
252 void QLImage::scale_impl(Params const & params)
253 {
254         if (transformed_.isNull())
255                 return;
256
257         unsigned int width;
258         unsigned int height;
259         boost::tie(width, height) = getScaledDimensions(params);
260
261         if (width == getWidth() && height == getHeight())
262                 return;
263
264         QWMatrix m;
265         m.scale(double(width) / getWidth(), double(height) / getHeight());
266         transformed_ = transformed_.xForm(m);
267 }
268
269 } // namespace graphics
270 } // lyx