Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | | - |
41 | #include "qtextimagehandler_p.h" | - |
42 | | - |
43 | #include <qguiapplication.h> | - |
44 | #include <qtextformat.h> | - |
45 | #include <qpainter.h> | - |
46 | #include <qdebug.h> | - |
47 | #include <qfile.h> | - |
48 | #include <private/qtextengine_p.h> | - |
49 | #include <qpalette.h> | - |
50 | #include <qthread.h> | - |
51 | | - |
52 | QT_BEGIN_NAMESPACE | - |
53 | | - |
54 | extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, | - |
55 | qreal *sourceDevicePixelRatio); | - |
56 | static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio, | - |
57 | qreal *sourceDevicePixelRatio) | - |
58 | { | - |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | if (url->isValid()) { | - |
64 | if (url->scheme() == QLatin1Literal("qrc")) { | - |
65 | fileName = fileName.right(fileName.length() - 3); | - |
66 | } | - |
67 | else if (url->scheme() == QLatin1Literal("file")) { | - |
68 | fileName = url->toLocalFile(); | - |
69 | } | - |
70 | } | - |
71 | | - |
72 | if (targetDevicePixelRatio <= 1.0) | - |
73 | return fileName; | - |
74 | | - |
75 | | - |
76 | return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio); | - |
77 | } | - |
78 | | - |
79 | | - |
80 | static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0) | - |
81 | { | - |
82 | QPixmap pm; | - |
83 | | - |
84 | QString name = format.name(); | - |
85 | if (name.startsWith(QLatin1String(":/"))) TRUE | never evaluated | FALSE | never evaluated |
| 0 |
86 | name.prepend(QLatin1String("qrc")); never executed: name.prepend(QLatin1String("qrc")); | 0 |
87 | QUrl url = QUrl(name); | - |
88 | qreal sourcePixelRatio = 1.0; | - |
89 | name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio); | - |
90 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
91 | if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
92 | pm = qvariant_cast<QPixmap>(data); | - |
93 | } else if (data.type() == QVariant::ByteArray) { never executed: end of block TRUE | never evaluated | FALSE | never evaluated |
| 0 |
94 | pm.loadFromData(data.toByteArray()); | - |
95 | } never executed: end of block | 0 |
96 | | - |
97 | if (pm.isNull()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
98 | #if 0 | - |
99 | QString context; | - |
100 | | - |
101 | QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); | - |
102 | if (browser) | - |
103 | context = browser->source().toString(); | - |
104 | #endif | - |
105 | | - |
106 | QImage img; | - |
107 | if (name.isEmpty() || !img.load(name))TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
108 | return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); never executed: return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); | 0 |
109 | | - |
110 | pm = QPixmap::fromImage(img); | - |
111 | doc->addResource(QTextDocument::ImageResource, url, pm); | - |
112 | } never executed: end of block | 0 |
113 | | - |
114 | if (name.contains(QStringLiteralQLatin1String("@2x")))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
115 | pm.setDevicePixelRatio(sourcePixelRatio); never executed: pm.setDevicePixelRatio(sourcePixelRatio); | 0 |
116 | | - |
117 | return pm; never executed: return pm; | 0 |
118 | } | - |
119 | | - |
120 | static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format) | - |
121 | { | - |
122 | QPixmap pm; | - |
123 | | - |
124 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
125 | const int width = qRound(format.width()); | - |
126 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
127 | const int height = qRound(format.height()); | - |
128 | | - |
129 | QSize size(width, height); | - |
130 | if (!hasWidth || !hasHeight) { | - |
131 | pm = getPixmap(doc, format); | - |
132 | const int pmWidth = pm.width() / pm.devicePixelRatio(); | - |
133 | const int pmHeight = pm.height() / pm.devicePixelRatio(); | - |
134 | | - |
135 | if (!hasWidth) { | - |
136 | if (!hasHeight) | - |
137 | size.setWidth(pmWidth); | - |
138 | else | - |
139 | size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight))); | - |
140 | } | - |
141 | if (!hasHeight) { | - |
142 | if (!hasWidth) | - |
143 | size.setHeight(pmHeight); | - |
144 | else | - |
145 | size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth))); | - |
146 | } | - |
147 | } | - |
148 | | - |
149 | qreal scale = 1.0; | - |
150 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
151 | if (pdev) { | - |
152 | if (pm.isNull()) | - |
153 | pm = getPixmap(doc, format); | - |
154 | if (!pm.isNull()) | - |
155 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | - |
156 | } | - |
157 | size *= scale; | - |
158 | | - |
159 | return size; | - |
160 | } | - |
161 | | - |
162 | static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0) | - |
163 | { | - |
164 | QImage image; | - |
165 | | - |
166 | QString name = format.name(); | - |
167 | if (name.startsWith(QLatin1String(":/"))) | - |
168 | name.prepend(QLatin1String("qrc")); | - |
169 | QUrl url = QUrl(name); | - |
170 | qreal sourcePixelRatio = 1.0; | - |
171 | name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio); | - |
172 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
173 | if (data.type() == QVariant::Image) { | - |
174 | image = qvariant_cast<QImage>(data); | - |
175 | } else if (data.type() == QVariant::ByteArray) { | - |
176 | image.loadFromData(data.toByteArray()); | - |
177 | } | - |
178 | | - |
179 | if (image.isNull()) { | - |
180 | #if 0 | - |
181 | QString context; | - |
182 | | - |
183 | QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); | - |
184 | if (browser) | - |
185 | context = browser->source().toString(); | - |
186 | #endif | - |
187 | | - |
188 | | - |
189 | if (name.isEmpty() || !image.load(name)) | - |
190 | return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); | - |
191 | | - |
192 | doc->addResource(QTextDocument::ImageResource, url, image); | - |
193 | } | - |
194 | | - |
195 | if (sourcePixelRatio != 1.0) | - |
196 | image.setDevicePixelRatio(sourcePixelRatio); | - |
197 | | - |
198 | return image; | - |
199 | } | - |
200 | | - |
201 | static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format) | - |
202 | { | - |
203 | QImage image; | - |
204 | | - |
205 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
206 | const int width = qRound(format.width()); | - |
207 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
208 | const int height = qRound(format.height()); | - |
209 | | - |
210 | QSize size(width, height); | - |
211 | if (!hasWidth || !hasHeight) { | - |
212 | image = getImage(doc, format); | - |
213 | if (!hasWidth) | - |
214 | size.setWidth(image.width() / image.devicePixelRatio()); | - |
215 | if (!hasHeight) | - |
216 | size.setHeight(image.height() / image.devicePixelRatio()); | - |
217 | } | - |
218 | | - |
219 | qreal scale = 1.0; | - |
220 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
221 | if (pdev) { | - |
222 | if (image.isNull()) | - |
223 | image = getImage(doc, format); | - |
224 | if (!image.isNull()) | - |
225 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | - |
226 | } | - |
227 | size *= scale; | - |
228 | | - |
229 | return size; | - |
230 | } | - |
231 | | - |
232 | QTextImageHandler::QTextImageHandler(QObject *parent) | - |
233 | : QObject(parent) | - |
234 | { | - |
235 | } | - |
236 | | - |
237 | QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
238 | { | - |
239 | Q_UNUSED(posInDocument) | - |
240 | const QTextImageFormat imageFormat = format.toImageFormat(); | - |
241 | | - |
242 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) | - |
243 | return getImageSize(doc, imageFormat); | - |
244 | return getPixmapSize(doc, imageFormat); | - |
245 | } | - |
246 | | - |
247 | QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat) | - |
248 | { | - |
249 | Q_ASSERT(doc != 0); | - |
250 | | - |
251 | return getImage(doc, imageFormat); | - |
252 | } | - |
253 | | - |
254 | void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
255 | { | - |
256 | Q_UNUSED(posInDocument) | - |
257 | const QTextImageFormat imageFormat = format.toImageFormat(); | - |
258 | | - |
259 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) { | - |
260 | const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatioF()); | - |
261 | p->drawImage(rect, image, image.rect()); | - |
262 | } else { | - |
263 | const QPixmap pixmap = getPixmap(doc, imageFormat, p->device()->devicePixelRatioF()); | - |
264 | p->drawPixmap(rect, pixmap, pixmap.rect()); | - |
265 | } | - |
266 | } | - |
267 | | - |
268 | QT_END_NAMESPACE | - |
| | |