source: trunk/src/gui/painting/qpaintengine_x11.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 86.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43
44#include "private/qpixmap_x11_p.h"
45
46#include "qapplication.h"
47#include "qdebug.h"
48#include "qfont.h"
49#include "qwidget.h"
50#include "qbitmap.h"
51#include "qpixmapcache.h"
52#include "qtextcodec.h"
53#include "qcoreevent.h"
54#include "qiodevice.h"
55#include <qmath.h>
56
57#include "qpainter_p.h"
58#include <qtextlayout.h>
59#include <qvarlengtharray.h>
60#include <private/qfont_p.h>
61#include <private/qtextengine_p.h>
62#include <private/qpaintengine_x11_p.h>
63#include <private/qfontengine_x11_p.h>
64#include <private/qwidget_p.h>
65#include <private/qpainterpath_p.h>
66
67#include "qpen.h"
68#include "qcolor.h"
69#include "qcolormap.h"
70
71#include <private/qpaintengine_p.h>
72#include "qpaintengine_x11_p.h"
73
74#include <private/qt_x11_p.h>
75#include <private/qnumeric_p.h>
76#include <limits.h>
77
78#ifndef QT_NO_XRENDER
79#include <private/qtessellator_p.h>
80#endif
81
82#include <private/qstylehelper_p.h>
83
84QT_BEGIN_NAMESPACE
85
86extern Drawable qt_x11Handle(const QPaintDevice *pd);
87extern const QX11Info *qt_x11Info(const QPaintDevice *pd);
88extern QPixmap qt_pixmapForBrush(int brushStyle, bool invert); //in qbrush.cpp
89extern QPixmap qt_toX11Pixmap(const QPixmap &pixmap);
90
91// use the same rounding as in qrasterizer.cpp (6 bit fixed point)
92static const qreal aliasedCoordinateDelta = 0.5 - 0.015625;
93
94#undef X11 // defined in qt_x11_p.h
95/*!
96 Returns the X11 specific pen GC for the painter \a p. Note that
97 QPainter::begin() must be called before this function returns a
98 valid GC.
99*/
100Q_GUI_EXPORT GC qt_x11_get_pen_gc(QPainter *p)
101{
102 if (p && p->paintEngine()
103 && p->paintEngine()->isActive()
104 && p->paintEngine()->type() == QPaintEngine::X11) {
105 return static_cast<QX11PaintEngine *>(p->paintEngine())->d_func()->gc;
106 }
107 return 0;
108}
109
110/*!
111 Returns the X11 specific brush GC for the painter \a p. Note that
112 QPainter::begin() must be called before this function returns a
113 valid GC.
114*/
115Q_GUI_EXPORT GC qt_x11_get_brush_gc(QPainter *p)
116{
117 if (p && p->paintEngine()
118 && p->paintEngine()->isActive()
119 && p->paintEngine()->type() == QPaintEngine::X11) {
120 return static_cast<QX11PaintEngine *>(p->paintEngine())->d_func()->gc_brush;
121 }
122 return 0;
123}
124#define X11 qt_x11Data
125
126#ifndef QT_NO_XRENDER
127static const int compositionModeToRenderOp[QPainter::CompositionMode_Xor + 1] = {
128 PictOpOver, //CompositionMode_SourceOver,
129 PictOpOverReverse, //CompositionMode_DestinationOver,
130 PictOpClear, //CompositionMode_Clear,
131 PictOpSrc, //CompositionMode_Source,
132 PictOpDst, //CompositionMode_Destination,
133 PictOpIn, //CompositionMode_SourceIn,
134 PictOpInReverse, //CompositionMode_DestinationIn,
135 PictOpOut, //CompositionMode_SourceOut,
136 PictOpOutReverse, //CompositionMode_DestinationOut,
137 PictOpAtop, //CompositionMode_SourceAtop,
138 PictOpAtopReverse, //CompositionMode_DestinationAtop,
139 PictOpXor //CompositionMode_Xor
140};
141
142static inline int qpainterOpToXrender(QPainter::CompositionMode mode)
143{
144 Q_ASSERT(mode <= QPainter::CompositionMode_Xor);
145 return compositionModeToRenderOp[mode];
146}
147#endif
148
149// hack, so we don't have to make QRegion::clipRectangles() public or include
150// X11 headers in qregion.h
151Q_GUI_EXPORT void *qt_getClipRects(const QRegion &r, int &num)
152{
153 return r.clipRectangles(num);
154}
155
156static inline void x11SetClipRegion(Display *dpy, GC gc, GC gc2,
157#ifndef QT_NO_XRENDER
158 Picture picture,
159#else
160 Qt::HANDLE picture,
161#endif
162 const QRegion &r)
163{
164 int num;
165 XRectangle *rects = (XRectangle *)qt_getClipRects(r, num);
166
167 if (gc)
168 XSetClipRectangles( dpy, gc, 0, 0, rects, num, YXBanded );
169 if (gc2)
170 XSetClipRectangles( dpy, gc2, 0, 0, rects, num, YXBanded );
171
172#ifndef QT_NO_XRENDER
173 if (picture)
174 XRenderSetPictureClipRectangles(dpy, picture, 0, 0, rects, num);
175#else
176 Q_UNUSED(picture);
177#endif // QT_NO_XRENDER
178}
179
180
181static inline void x11ClearClipRegion(Display *dpy, GC gc, GC gc2,
182#ifndef QT_NO_XRENDER
183 Picture picture
184#else
185 Qt::HANDLE picture
186#endif
187 )
188{
189 if (gc)
190 XSetClipMask(dpy, gc, XNone);
191 if (gc2)
192 XSetClipMask(dpy, gc2, XNone);
193
194#ifndef QT_NO_XRENDER
195 if (picture) {
196 XRenderPictureAttributes attrs;
197 attrs.clip_mask = XNone;
198 XRenderChangePicture (dpy, picture, CPClipMask, &attrs);
199 }
200#else
201 Q_UNUSED(picture);
202#endif // QT_NO_XRENDER
203}
204
205
206#define DITHER_SIZE 16
207static const uchar base_dither_matrix[DITHER_SIZE][DITHER_SIZE] = {
208 { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
209 { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
210 { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
211 { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
212 { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
213 { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
214 { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
215 { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
216 { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
217 { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
218 { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
219 { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
220 { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
221 { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
222 { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
223 { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
224};
225
226static QPixmap qt_patternForAlpha(uchar alpha, int screen)
227{
228 QPixmap pm;
229 QString key = QLatin1Literal("$qt-alpha-brush$")
230 % HexString<uchar>(alpha)
231 % HexString<int>(screen);
232
233 if (!QPixmapCache::find(key, pm)) {
234 // #### why not use a mono image here????
235 QImage pattern(DITHER_SIZE, DITHER_SIZE, QImage::Format_ARGB32);
236 pattern.fill(0xffffffff);
237 for (int y = 0; y < DITHER_SIZE; ++y) {
238 for (int x = 0; x < DITHER_SIZE; ++x) {
239 if (base_dither_matrix[x][y] <= alpha)
240 pattern.setPixel(x, y, 0x00000000);
241 }
242 }
243 pm = QBitmap::fromImage(pattern);
244 pm.x11SetScreen(screen);
245 QPixmapCache::insert(key, pm);
246 }
247 return pm;
248}
249
250#if !defined(QT_NO_XRENDER)
251
252class QXRenderTessellator : public QTessellator
253{
254public:
255 QXRenderTessellator() : traps(0), allocated(0), size(0) {}
256 ~QXRenderTessellator() { free(traps); }
257 XTrapezoid *traps;
258 int allocated;
259 int size;
260 void addTrap(const Trapezoid &trap);
261 QRect tessellate(const QPointF *points, int nPoints, bool winding) {
262 size = 0;
263 setWinding(winding);
264 return QTessellator::tessellate(points, nPoints).toRect();
265 }
266 void done() {
267 if (allocated > 64) {
268 free(traps);
269 traps = 0;
270 allocated = 0;
271 }
272 }
273};
274
275void QXRenderTessellator::addTrap(const Trapezoid &trap)
276{
277 if (size == allocated) {
278 allocated = qMax(2*allocated, 64);
279 traps = q_check_ptr((XTrapezoid *)realloc(traps, allocated * sizeof(XTrapezoid)));
280 }
281 traps[size].top = Q27Dot5ToXFixed(trap.top);
282 traps[size].bottom = Q27Dot5ToXFixed(trap.bottom);
283 traps[size].left.p1.x = Q27Dot5ToXFixed(trap.topLeft->x);
284 traps[size].left.p1.y = Q27Dot5ToXFixed(trap.topLeft->y);
285 traps[size].left.p2.x = Q27Dot5ToXFixed(trap.bottomLeft->x);
286 traps[size].left.p2.y = Q27Dot5ToXFixed(trap.bottomLeft->y);
287 traps[size].right.p1.x = Q27Dot5ToXFixed(trap.topRight->x);
288 traps[size].right.p1.y = Q27Dot5ToXFixed(trap.topRight->y);
289 traps[size].right.p2.x = Q27Dot5ToXFixed(trap.bottomRight->x);
290 traps[size].right.p2.y = Q27Dot5ToXFixed(trap.bottomRight->y);
291 ++size;
292}
293
294#endif // !defined(QT_NO_XRENDER)
295
296
297#ifndef QT_NO_XRENDER
298static Picture getPatternFill(int screen, const QBrush &b)
299{
300 if (!X11->use_xrender)
301 return XNone;
302
303 XRenderColor color = X11->preMultiply(b.color());
304 XRenderColor bg_color;
305
306 bg_color = X11->preMultiply(QColor(0, 0, 0, 0));
307
308 for (int i = 0; i < X11->pattern_fill_count; ++i) {
309 if (X11->pattern_fills[i].screen == screen
310 && X11->pattern_fills[i].opaque == false
311 && X11->pattern_fills[i].style == b.style()
312 && X11->pattern_fills[i].color.alpha == color.alpha
313 && X11->pattern_fills[i].color.red == color.red
314 && X11->pattern_fills[i].color.green == color.green
315 && X11->pattern_fills[i].color.blue == color.blue
316 && X11->pattern_fills[i].bg_color.alpha == bg_color.alpha
317 && X11->pattern_fills[i].bg_color.red == bg_color.red
318 && X11->pattern_fills[i].bg_color.green == bg_color.green
319 && X11->pattern_fills[i].bg_color.blue == bg_color.blue)
320 return X11->pattern_fills[i].picture;
321 }
322 // none found, replace one
323 int i = qrand() % 16;
324
325 if (X11->pattern_fills[i].screen != screen && X11->pattern_fills[i].picture) {
326 XRenderFreePicture (X11->display, X11->pattern_fills[i].picture);
327 X11->pattern_fills[i].picture = 0;
328 }
329
330 if (!X11->pattern_fills[i].picture) {
331 Pixmap pixmap = XCreatePixmap (X11->display, RootWindow (X11->display, screen), 8, 8, 32);
332 XRenderPictureAttributes attrs;
333 attrs.repeat = True;
334 X11->pattern_fills[i].picture = XRenderCreatePicture (X11->display, pixmap,
335 XRenderFindStandardFormat(X11->display, PictStandardARGB32),
336 CPRepeat, &attrs);
337 XFreePixmap (X11->display, pixmap);
338 }
339
340 X11->pattern_fills[i].screen = screen;
341 X11->pattern_fills[i].color = color;
342 X11->pattern_fills[i].bg_color = bg_color;
343 X11->pattern_fills[i].opaque = false;
344 X11->pattern_fills[i].style = b.style();
345
346 XRenderFillRectangle(X11->display, PictOpSrc, X11->pattern_fills[i].picture, &bg_color, 0, 0, 8, 8);
347
348 QPixmap pattern(qt_pixmapForBrush(b.style(), true));
349 XRenderPictureAttributes attrs;
350 attrs.repeat = true;
351 XRenderChangePicture(X11->display, pattern.x11PictureHandle(), CPRepeat, &attrs);
352
353 Picture fill_fg = X11->getSolidFill(screen, b.color());
354 XRenderComposite(X11->display, PictOpOver, fill_fg, pattern.x11PictureHandle(),
355 X11->pattern_fills[i].picture,
356 0, 0, 0, 0, 0, 0, 8, 8);
357
358 return X11->pattern_fills[i].picture;
359}
360
361static void qt_render_bitmap(Display *dpy, int scrn, Picture src, Picture dst,
362 int sx, int sy, int x, int y, int sw, int sh,
363 const QPen &pen)
364{
365 Picture fill_fg = X11->getSolidFill(scrn, pen.color());
366 XRenderComposite(dpy, PictOpOver,
367 fill_fg, src, dst, sx, sy, sx, sy, x, y, sw, sh);
368}
369#endif
370
371void QX11PaintEnginePrivate::init()
372{
373 dpy = 0;
374 scrn = 0;
375 hd = 0;
376 picture = 0;
377 xinfo = 0;
378#ifndef QT_NO_XRENDER
379 current_brush = 0;
380 composition_mode = PictOpOver;
381 tessellator = new QXRenderTessellator;
382#endif
383}
384
385void QX11PaintEnginePrivate::setupAdaptedOrigin(const QPoint &p)
386{
387 if (adapted_pen_origin)
388 XSetTSOrigin(dpy, gc, p.x(), p.y());
389 if (adapted_brush_origin)
390 XSetTSOrigin(dpy, gc_brush, p.x(), p.y());
391}
392
393void QX11PaintEnginePrivate::resetAdaptedOrigin()
394{
395 if (adapted_pen_origin)
396 XSetTSOrigin(dpy, gc, 0, 0);
397 if (adapted_brush_origin)
398 XSetTSOrigin(dpy, gc_brush, 0, 0);
399}
400
401void QX11PaintEnginePrivate::clipPolygon_dev(const QPolygonF &poly, QPolygonF *clipped_poly)
402{
403 int clipped_count = 0;
404 qt_float_point *clipped_points = 0;
405 polygonClipper.clipPolygon((qt_float_point *) poly.data(), poly.size(),
406 &clipped_points, &clipped_count);
407 clipped_poly->resize(clipped_count);
408 for (int i=0; i<clipped_count; ++i)
409 (*clipped_poly)[i] = *((QPointF *)(&clipped_points[i]));
410}
411
412void QX11PaintEnginePrivate::systemStateChanged()
413{
414 Q_Q(QX11PaintEngine);
415 QPainter *painter = q->state ? static_cast<QPainterState *>(q->state)->painter : 0;
416 if (painter && painter->hasClipping()) {
417 if (q->testDirty(QPaintEngine::DirtyTransform))
418 q->updateMatrix(q->state->transform());
419 QPolygonF clip_poly_dev(matrix.map(painter->clipPath().toFillPolygon()));
420 QPolygonF clipped_poly_dev;
421 clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
422 q->updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), Qt::ReplaceClip);
423 } else {
424 q->updateClipRegion_dev(QRegion(), Qt::NoClip);
425 }
426}
427
428static QPaintEngine::PaintEngineFeatures qt_decide_features()
429{
430 QPaintEngine::PaintEngineFeatures features =
431 QPaintEngine::PrimitiveTransform
432 | QPaintEngine::PatternBrush
433 | QPaintEngine::AlphaBlend
434 | QPaintEngine::PainterPaths
435 | QPaintEngine::RasterOpModes;
436
437 if (X11->use_xrender) {
438 features |= QPaintEngine::Antialiasing;
439 features |= QPaintEngine::PorterDuff;
440 features |= QPaintEngine::MaskedBrush;
441#if 0
442 if (X11->xrender_version > 10) {
443 features |= QPaintEngine::LinearGradientFill;
444 // ###
445 }
446#endif
447 }
448
449 return features;
450}
451
452/*
453 * QX11PaintEngine members
454 */
455
456QX11PaintEngine::QX11PaintEngine()
457 : QPaintEngine(*(new QX11PaintEnginePrivate), qt_decide_features())
458{
459 d_func()->init();
460}
461
462QX11PaintEngine::QX11PaintEngine(QX11PaintEnginePrivate &dptr)
463 : QPaintEngine(dptr, qt_decide_features())
464{
465 d_func()->init();
466}
467
468QX11PaintEngine::~QX11PaintEngine()
469{
470#ifndef QT_NO_XRENDER
471 Q_D(QX11PaintEngine);
472 delete d->tessellator;
473#endif
474}
475
476bool QX11PaintEngine::begin(QPaintDevice *pdev)
477{
478 Q_D(QX11PaintEngine);
479 d->xinfo = qt_x11Info(pdev);
480 QWidget *w = d->pdev->devType() == QInternal::Widget ? static_cast<QWidget *>(d->pdev) : 0;
481 const bool isAlienWidget = w && !w->internalWinId() && w->testAttribute(Qt::WA_WState_Created);
482#ifndef QT_NO_XRENDER
483 if (w) {
484 if (isAlienWidget)
485 d->picture = (::Picture)w->nativeParentWidget()->x11PictureHandle();
486 else
487 d->picture = (::Picture)w->x11PictureHandle();
488 } else if (pdev->devType() == QInternal::Pixmap) {
489 const QPixmap *pm = static_cast<const QPixmap *>(pdev);
490 QX11PixmapData *data = static_cast<QX11PixmapData*>(pm->data.data());
491 if (X11->use_xrender && data->depth() != 32 && data->x11_mask)
492 data->convertToARGB32();
493 d->picture = (::Picture)static_cast<const QPixmap *>(pdev)->x11PictureHandle();
494 }
495#else
496 d->picture = 0;
497#endif
498 d->hd = !isAlienWidget ? qt_x11Handle(pdev) : qt_x11Handle(w->nativeParentWidget());
499
500 Q_ASSERT(d->xinfo != 0);
501 d->dpy = d->xinfo->display(); // get display variable
502 d->scrn = d->xinfo->screen(); // get screen variable
503
504 d->crgn = QRegion();
505 d->gc = XCreateGC(d->dpy, d->hd, 0, 0);
506 d->gc_brush = XCreateGC(d->dpy, d->hd, 0, 0);
507 d->has_alpha_brush = false;
508 d->has_alpha_pen = false;
509 d->has_clipping = false;
510 d->has_complex_xform = false;
511 d->has_scaling_xform = false;
512 d->has_non_scaling_xform = true;
513 d->xform_scale = 1;
514 d->has_custom_pen = false;
515 d->matrix = QTransform();
516 d->pdev_depth = d->pdev->depth();
517 d->render_hints = 0;
518 d->txop = QTransform::TxNone;
519 d->use_path_fallback = false;
520#if !defined(QT_NO_XRENDER)
521 d->composition_mode = PictOpOver;
522#endif
523 d->xlibMaxLinePoints = 32762; // a safe number used to avoid, call to XMaxRequestSize(d->dpy) - 3;
524 d->opacity = 1;
525
526 // Set up the polygon clipper. Note: This will only work in
527 // polyline mode as long as we have a buffer zone, since a
528 // polyline may be clipped into several non-connected polylines.
529 const int BUFFERZONE = 1000;
530 QRect devClipRect(-BUFFERZONE, -BUFFERZONE,
531 pdev->width() + 2*BUFFERZONE, pdev->height() + 2*BUFFERZONE);
532 d->polygonClipper.setBoundingRect(devClipRect);
533
534 if (isAlienWidget) {
535 // Set system clip for alien widgets painting outside the paint event.
536 // This is not a problem with native windows since the windowing system
537 // will handle the clip.
538 QWidgetPrivate *wd = w->d_func();
539 QRegion widgetClip(wd->clipRect());
540 wd->clipToEffectiveMask(widgetClip);
541 wd->subtractOpaqueSiblings(widgetClip);
542 widgetClip.translate(w->mapTo(w->nativeParentWidget(), QPoint()));
543 setSystemClip(widgetClip);
544 }
545
546 QPixmap::x11SetDefaultScreen(d->xinfo->screen());
547
548 if (w && w->testAttribute(Qt::WA_PaintUnclipped)) { // paint direct on device
549 updatePen(QPen(Qt::black));
550 updateBrush(QBrush(Qt::white), QPoint());
551 XSetSubwindowMode(d->dpy, d->gc, IncludeInferiors);
552 XSetSubwindowMode(d->dpy, d->gc_brush, IncludeInferiors);
553#ifndef QT_NO_XRENDER
554 XRenderPictureAttributes attrs;
555 attrs.subwindow_mode = IncludeInferiors;
556 XRenderChangePicture(d->dpy, d->picture, CPSubwindowMode, &attrs);
557#endif
558 }
559
560 setDirty(QPaintEngine::DirtyClipRegion);
561 setDirty(QPaintEngine::DirtyPen);
562 setDirty(QPaintEngine::DirtyBrush);
563 setDirty(QPaintEngine::DirtyBackground);
564
565 return true;
566}
567
568bool QX11PaintEngine::end()
569{
570 Q_D(QX11PaintEngine);
571
572#if !defined(QT_NO_XRENDER)
573 if (d->picture) {
574 // reset clipping/subwindow mode on our render picture
575 XRenderPictureAttributes attrs;
576 attrs.subwindow_mode = ClipByChildren;
577 attrs.clip_mask = XNone;
578 XRenderChangePicture(d->dpy, d->picture, CPClipMask|CPSubwindowMode, &attrs);
579 }
580#endif
581
582 if (d->gc_brush && d->pdev->painters < 2) {
583 XFreeGC(d->dpy, d->gc_brush);
584 d->gc_brush = 0;
585 }
586
587 if (d->gc && d->pdev->painters < 2) {
588 XFreeGC(d->dpy, d->gc);
589 d->gc = 0;
590 }
591
592 // Restore system clip for alien widgets painting outside the paint event.
593 if (d->pdev->devType() == QInternal::Widget && !static_cast<QWidget *>(d->pdev)->internalWinId())
594 setSystemClip(QRegion());
595
596 return true;
597}
598
599static bool clipLine(QLineF *line, const QRect &rect)
600{
601 qreal x1 = line->x1();
602 qreal x2 = line->x2();
603 qreal y1 = line->y1();
604 qreal y2 = line->y2();
605
606 qreal left = rect.x();
607 qreal right = rect.x() + rect.width() - 1;
608 qreal top = rect.y();
609 qreal bottom = rect.y() + rect.height() - 1;
610
611 enum { Left, Right, Top, Bottom };
612 // clip the lines, after cohen-sutherland, see e.g. http://www.nondot.org/~sabre/graphpro/line6.html
613 int p1 = ((x1 < left) << Left)
614 | ((x1 > right) << Right)
615 | ((y1 < top) << Top)
616 | ((y1 > bottom) << Bottom);
617 int p2 = ((x2 < left) << Left)
618 | ((x2 > right) << Right)
619 | ((y2 < top) << Top)
620 | ((y2 > bottom) << Bottom);
621
622 if (p1 & p2)
623 // completely outside
624 return false;
625
626 if (p1 | p2) {
627 qreal dx = x2 - x1;
628 qreal dy = y2 - y1;
629
630 // clip x coordinates
631 if (x1 < left) {
632 y1 += dy/dx * (left - x1);
633 x1 = left;
634 } else if (x1 > right) {
635 y1 -= dy/dx * (x1 - right);
636 x1 = right;
637 }
638 if (x2 < left) {
639 y2 += dy/dx * (left - x2);
640 x2 = left;
641 } else if (x2 > right) {
642 y2 -= dy/dx * (x2 - right);
643 x2 = right;
644 }
645 p1 = ((y1 < top) << Top)
646 | ((y1 > bottom) << Bottom);
647 p2 = ((y2 < top) << Top)
648 | ((y2 > bottom) << Bottom);
649 if (p1 & p2)
650 return false;
651 // clip y coordinates
652 if (y1 < top) {
653 x1 += dx/dy * (top - y1);
654 y1 = top;
655 } else if (y1 > bottom) {
656 x1 -= dx/dy * (y1 - bottom);
657 y1 = bottom;
658 }
659 if (y2 < top) {
660 x2 += dx/dy * (top - y2);
661 y2 = top;
662 } else if (y2 > bottom) {
663 x2 -= dx/dy * (y2 - bottom);
664 y2 = bottom;
665 }
666 *line = QLineF(QPointF(x1, y1), QPointF(x2, y2));
667 }
668 return true;
669}
670
671void QX11PaintEngine::drawLines(const QLine *lines, int lineCount)
672{
673 Q_ASSERT(lines);
674 Q_ASSERT(lineCount);
675 Q_D(QX11PaintEngine);
676 if (d->has_alpha_brush
677 || d->has_alpha_pen
678 || d->has_custom_pen
679 || (d->cpen.widthF() > 0 && d->has_complex_xform
680 && !d->has_non_scaling_xform)
681 || (d->render_hints & QPainter::Antialiasing)) {
682 for (int i = 0; i < lineCount; ++i) {
683 QPainterPath path(lines[i].p1());
684 path.lineTo(lines[i].p2());
685 drawPath(path);
686 }
687 return;
688 }
689
690 if (d->has_pen) {
691 for (int i = 0; i < lineCount; ++i) {
692 QLineF linef;
693 if (d->txop == QTransform::TxNone) {
694 linef = lines[i];
695 } else {
696 linef = d->matrix.map(QLineF(lines[i]));
697 }
698 if (clipLine(&linef, d->polygonClipper.boundingRect())) {
699 int x1 = qRound(linef.x1() + aliasedCoordinateDelta);
700 int y1 = qRound(linef.y1() + aliasedCoordinateDelta);
701 int x2 = qRound(linef.x2() + aliasedCoordinateDelta);
702 int y2 = qRound(linef.y2() + aliasedCoordinateDelta);
703
704 XDrawLine(d->dpy, d->hd, d->gc, x1, y1, x2, y2);
705 }
706 }
707 }
708}
709
710void QX11PaintEngine::drawLines(const QLineF *lines, int lineCount)
711{
712 Q_ASSERT(lines);
713 Q_ASSERT(lineCount);
714 Q_D(QX11PaintEngine);
715 if (d->has_alpha_brush
716 || d->has_alpha_pen
717 || d->has_custom_pen
718 || (d->cpen.widthF() > 0 && d->has_complex_xform
719 && !d->has_non_scaling_xform)
720 || (d->render_hints & QPainter::Antialiasing)) {
721 for (int i = 0; i < lineCount; ++i) {
722 QPainterPath path(lines[i].p1());
723 path.lineTo(lines[i].p2());
724 drawPath(path);
725 }
726 return;
727 }
728
729 if (d->has_pen) {
730 for (int i = 0; i < lineCount; ++i) {
731 QLineF linef = d->matrix.map(lines[i]);
732 if (clipLine(&linef, d->polygonClipper.boundingRect())) {
733 int x1 = qRound(linef.x1() + aliasedCoordinateDelta);
734 int y1 = qRound(linef.y1() + aliasedCoordinateDelta);
735 int x2 = qRound(linef.x2() + aliasedCoordinateDelta);
736 int y2 = qRound(linef.y2() + aliasedCoordinateDelta);
737
738 XDrawLine(d->dpy, d->hd, d->gc, x1, y1, x2, y2);
739 }
740 }
741 }
742}
743
744static inline QLine clipStraightLine(const QRect &clip, const QLine &l)
745{
746 if (l.p1().x() == l.p2().x()) {
747 int x = qBound(clip.left(), l.p1().x(), clip.right());
748 int y1 = qBound(clip.top(), l.p1().y(), clip.bottom());
749 int y2 = qBound(clip.top(), l.p2().y(), clip.bottom());
750
751 return QLine(x, y1, x, y2);
752 } else {
753 Q_ASSERT(l.p1().y() == l.p2().y());
754
755 int x1 = qBound(clip.left(), l.p1().x(), clip.right());
756 int x2 = qBound(clip.left(), l.p2().x(), clip.right());
757 int y = qBound(clip.top(), l.p1().y(), clip.bottom());
758
759 return QLine(x1, y, x2, y);
760 }
761}
762
763void QX11PaintEngine::drawRects(const QRectF *rects, int rectCount)
764{
765 Q_D(QX11PaintEngine);
766 Q_ASSERT(rects);
767 Q_ASSERT(rectCount);
768
769 if (rectCount != 1
770 || d->has_pen
771 || d->has_alpha_brush
772 || d->has_complex_xform
773 || d->has_custom_pen
774 || d->cbrush.style() != Qt::SolidPattern)
775 {
776 QPaintEngine::drawRects(rects, rectCount);
777 return;
778 }
779
780 QPoint alignedOffset;
781 if (d->txop == QTransform::TxTranslate) {
782 QPointF offset(d->matrix.dx(), d->matrix.dy());
783 alignedOffset = offset.toPoint();
784 if (offset != QPointF(alignedOffset)) {
785 QPaintEngine::drawRects(rects, rectCount);
786 return;
787 }
788 }
789
790 const QRectF& r = rects[0];
791 QRect alignedRect = r.toAlignedRect();
792 if (r != QRectF(alignedRect)) {
793 QPaintEngine::drawRects(rects, rectCount);
794 return;
795 }
796 alignedRect.translate(alignedOffset);
797
798 QRect clip(d->polygonClipper.boundingRect());
799 alignedRect = alignedRect.intersected(clip);
800 if (alignedRect.isEmpty())
801 return;
802
803 // simple-case:
804 // the rectangle is pixel-aligned
805 // the fill brush is just a solid non-alpha color
806 // the painter transform is only integer translation
807 // ignore: antialiasing and just XFillRectangles directly
808 XRectangle xrect;
809 xrect.x = short(alignedRect.x());
810 xrect.y = short(alignedRect.y());
811 xrect.width = ushort(alignedRect.width());
812 xrect.height = ushort(alignedRect.height());
813 XFillRectangles(d->dpy, d->hd, d->gc_brush, &xrect, 1);
814}
815
816void QX11PaintEngine::drawRects(const QRect *rects, int rectCount)
817{
818 Q_D(QX11PaintEngine);
819 Q_ASSERT(rects);
820 Q_ASSERT(rectCount);
821
822 if (d->has_alpha_pen
823 || d->has_complex_xform
824 || d->has_custom_pen
825 || (d->render_hints & QPainter::Antialiasing))
826 {
827 for (int i = 0; i < rectCount; ++i) {
828 QPainterPath path;
829 path.addRect(rects[i]);
830 drawPath(path);
831 }
832 return;
833 }
834
835 QRect clip(d->polygonClipper.boundingRect());
836 QPoint offset(qRound(d->matrix.dx()), qRound(d->matrix.dy()));
837#if !defined(QT_NO_XRENDER)
838 ::Picture pict = d->picture;
839
840 if (X11->use_xrender && pict && d->has_brush && d->pdev_depth != 1
841 && (d->has_texture || d->has_alpha_brush))
842 {
843 XRenderColor xc;
844 if (!d->has_texture && !d->has_pattern)
845 xc = X11->preMultiply(d->cbrush.color());
846
847 for (int i = 0; i < rectCount; ++i) {
848 QRect r(rects[i]);
849 if (d->txop == QTransform::TxTranslate)
850 r.translate(offset);
851
852 if (r.width() == 0 || r.height() == 0) {
853 if (d->has_pen) {
854 const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
855 XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
856 }
857 continue;
858 }
859
860 r = r.intersected(clip);
861 if (r.isEmpty())
862 continue;
863 if (d->has_texture || d->has_pattern) {
864 XRenderComposite(d->dpy, d->composition_mode, d->current_brush, 0, pict,
865 qRound(r.x() - d->bg_origin.x()), qRound(r.y() - d->bg_origin.y()),
866 0, 0, r.x(), r.y(), r.width(), r.height());
867 } else {
868 XRenderFillRectangle(d->dpy, d->composition_mode, pict, &xc, r.x(), r.y(), r.width(), r.height());
869 }
870 if (d->has_pen)
871 XDrawRectangle(d->dpy, d->hd, d->gc, r.x(), r.y(), r.width(), r.height());
872 }
873 } else
874#endif // !QT_NO_XRENDER
875 {
876 if (d->has_brush && d->has_pen) {
877 for (int i = 0; i < rectCount; ++i) {
878 QRect r(rects[i]);
879 if (d->txop == QTransform::TxTranslate)
880 r.translate(offset);
881
882 if (r.width() == 0 || r.height() == 0) {
883 const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
884 XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
885 continue;
886 }
887
888 r = r.intersected(clip);
889 if (r.isEmpty())
890 continue;
891 d->setupAdaptedOrigin(r.topLeft());
892 XFillRectangle(d->dpy, d->hd, d->gc_brush, r.x(), r.y(), r.width(), r.height());
893 XDrawRectangle(d->dpy, d->hd, d->gc, r.x(), r.y(), r.width(), r.height());
894 }
895 d->resetAdaptedOrigin();
896 } else {
897 QVarLengthArray<XRectangle> xrects(rectCount);
898 int numClipped = rectCount;
899 for (int i = 0; i < rectCount; ++i) {
900 QRect r(rects[i]);
901 if (d->txop == QTransform::TxTranslate)
902 r.translate(offset);
903
904 if (r.width() == 0 || r.height() == 0) {
905 --numClipped;
906 if (d->has_pen) {
907 const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
908 XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
909 }
910 continue;
911 }
912
913 r = r.intersected(clip);
914 if (r.isEmpty()) {
915 --numClipped;
916 continue;
917 }
918 xrects[i].x = short(r.x());
919 xrects[i].y = short(r.y());
920 xrects[i].width = ushort(r.width());
921 xrects[i].height = ushort(r.height());
922 }
923 if (numClipped) {
924 d->setupAdaptedOrigin(rects[0].topLeft());
925 if (d->has_brush)
926 XFillRectangles(d->dpy, d->hd, d->gc_brush, xrects.data(), numClipped);
927 else if (d->has_pen)
928 XDrawRectangles(d->dpy, d->hd, d->gc, xrects.data(), numClipped);
929 d->resetAdaptedOrigin();
930 }
931 }
932 }
933}
934
935static inline void setCapStyle(int cap_style, GC gc)
936{
937 ulong mask = GCCapStyle;
938 XGCValues vals;
939 vals.cap_style = cap_style;
940 XChangeGC(X11->display, gc, mask, &vals);
941}
942
943void QX11PaintEngine::drawPoints(const QPoint *points, int pointCount)
944{
945 Q_ASSERT(points);
946 Q_ASSERT(pointCount);
947 Q_D(QX11PaintEngine);
948
949 if (!d->has_pen)
950 return;
951
952 // use the same test here as in drawPath to ensure that we don't use the path fallback
953 // and end up in XDrawLines for pens with width <= 1
954 if (d->cpen.widthF() > 1.0f
955 || (X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
956 || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate))
957 {
958 Qt::PenCapStyle capStyle = d->cpen.capStyle();
959 if (capStyle == Qt::FlatCap) {
960 setCapStyle(CapProjecting, d->gc);
961 d->cpen.setCapStyle(Qt::SquareCap);
962 }
963 const QPoint *end = points + pointCount;
964 while (points < end) {
965 QPainterPath path;
966 path.moveTo(*points);
967 path.lineTo(points->x()+.005, points->y());
968 drawPath(path);
969 ++points;
970 }
971
972 if (capStyle == Qt::FlatCap) {
973 setCapStyle(CapButt, d->gc);
974 d->cpen.setCapStyle(capStyle);
975 }
976 return;
977 }
978
979 static const int BUF_SIZE = 1024;
980 XPoint xPoints[BUF_SIZE];
981 int i = 0, j = 0;
982 while (i < pointCount) {
983 while (i < pointCount && j < BUF_SIZE) {
984 const QPoint &xformed = d->matrix.map(points[i]);
985 int x = xformed.x();
986 int y = xformed.y();
987 if (x >= SHRT_MIN && y >= SHRT_MIN && x < SHRT_MAX && y < SHRT_MAX) {
988 xPoints[j].x = x;
989 xPoints[j].y = y;
990 ++j;
991 }
992 ++i;
993 }
994 if (j)
995 XDrawPoints(d->dpy, d->hd, d->gc, xPoints, j, CoordModeOrigin);
996
997 j = 0;
998 }
999}
1000
1001void QX11PaintEngine::drawPoints(const QPointF *points, int pointCount)
1002{
1003 Q_ASSERT(points);
1004 Q_ASSERT(pointCount);
1005 Q_D(QX11PaintEngine);
1006
1007 if (!d->has_pen)
1008 return;
1009
1010 // use the same test here as in drawPath to ensure that we don't use the path fallback
1011 // and end up in XDrawLines for pens with width <= 1
1012 if (d->cpen.widthF() > 1.0f
1013 || (X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
1014 || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate))
1015 {
1016 Qt::PenCapStyle capStyle = d->cpen.capStyle();
1017 if (capStyle == Qt::FlatCap) {
1018 setCapStyle(CapProjecting, d->gc);
1019 d->cpen.setCapStyle(Qt::SquareCap);
1020 }
1021
1022 const QPointF *end = points + pointCount;
1023 while (points < end) {
1024 QPainterPath path;
1025 path.moveTo(*points);
1026 path.lineTo(points->x() + 0.005, points->y());
1027 drawPath(path);
1028 ++points;
1029 }
1030 if (capStyle == Qt::FlatCap) {
1031 setCapStyle(CapButt, d->gc);
1032 d->cpen.setCapStyle(capStyle);
1033 }
1034 return;
1035 }
1036
1037 static const int BUF_SIZE = 1024;
1038 XPoint xPoints[BUF_SIZE];
1039 int i = 0, j = 0;
1040 while (i < pointCount) {
1041 while (i < pointCount && j < BUF_SIZE) {
1042 const QPointF &xformed = d->matrix.map(points[i]);
1043 int x = qFloor(xformed.x());
1044 int y = qFloor(xformed.y());
1045
1046 if (x >= SHRT_MIN && y >= SHRT_MIN && x < SHRT_MAX && y < SHRT_MAX) {
1047 xPoints[j].x = x;
1048 xPoints[j].y = y;
1049 ++j;
1050 }
1051 ++i;
1052 }
1053 if (j)
1054 XDrawPoints(d->dpy, d->hd, d->gc, xPoints, j, CoordModeOrigin);
1055
1056 j = 0;
1057 }
1058}
1059
1060QPainter::RenderHints QX11PaintEngine::supportedRenderHints() const
1061{
1062#if !defined(QT_NO_XRENDER)
1063 if (X11->use_xrender)
1064 return QPainter::Antialiasing;
1065#endif
1066 return QFlag(0);
1067}
1068
1069void QX11PaintEngine::updateState(const QPaintEngineState &state)
1070{
1071 Q_D(QX11PaintEngine);
1072 QPaintEngine::DirtyFlags flags = state.state();
1073
1074
1075 if (flags & DirtyOpacity) {
1076 d->opacity = state.opacity();
1077 // Force update pen/brush as to get proper alpha colors propagated
1078 flags |= DirtyPen;
1079 flags |= DirtyBrush;
1080 }
1081
1082 if (flags & DirtyTransform) updateMatrix(state.transform());
1083 if (flags & DirtyPen) updatePen(state.pen());
1084 if (flags & (DirtyBrush | DirtyBrushOrigin)) updateBrush(state.brush(), state.brushOrigin());
1085 if (flags & DirtyFont) updateFont(state.font());
1086
1087 if (state.state() & DirtyClipEnabled) {
1088 if (state.isClipEnabled()) {
1089 QPolygonF clip_poly_dev(d->matrix.map(painter()->clipPath().toFillPolygon()));
1090 QPolygonF clipped_poly_dev;
1091 d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
1092 updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), Qt::ReplaceClip);
1093 } else {
1094 updateClipRegion_dev(QRegion(), Qt::NoClip);
1095 }
1096 }
1097
1098 if (flags & DirtyClipPath) {
1099 QPolygonF clip_poly_dev(d->matrix.map(state.clipPath().toFillPolygon()));
1100 QPolygonF clipped_poly_dev;
1101 d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
1102 updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon(), state.clipPath().fillRule()),
1103 state.clipOperation());
1104 } else if (flags & DirtyClipRegion) {
1105 extern QPainterPath qt_regionToPath(const QRegion &region);
1106 QPainterPath clip_path = qt_regionToPath(state.clipRegion());
1107 QPolygonF clip_poly_dev(d->matrix.map(clip_path.toFillPolygon()));
1108 QPolygonF clipped_poly_dev;
1109 d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
1110 updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), state.clipOperation());
1111 }
1112 if (flags & DirtyHints) updateRenderHints(state.renderHints());
1113 if (flags & DirtyCompositionMode) {
1114 int function = GXcopy;
1115 if (state.compositionMode() >= QPainter::RasterOp_SourceOrDestination) {
1116 switch (state.compositionMode()) {
1117 case QPainter::RasterOp_SourceOrDestination:
1118 function = GXor;
1119 break;
1120 case QPainter::RasterOp_SourceAndDestination:
1121 function = GXand;
1122 break;
1123 case QPainter::RasterOp_SourceXorDestination:
1124 function = GXxor;
1125 break;
1126 case QPainter::RasterOp_NotSourceAndNotDestination:
1127 function = GXnor;
1128 break;
1129 case QPainter::RasterOp_NotSourceOrNotDestination:
1130 function = GXnand;
1131 break;
1132 case QPainter::RasterOp_NotSourceXorDestination:
1133 function = GXequiv;
1134 break;
1135 case QPainter::RasterOp_NotSource:
1136 function = GXcopyInverted;
1137 break;
1138 case QPainter::RasterOp_SourceAndNotDestination:
1139 function = GXandReverse;
1140 break;
1141 case QPainter::RasterOp_NotSourceAndDestination:
1142 function = GXandInverted;
1143 break;
1144 default:
1145 function = GXcopy;
1146 }
1147 }
1148#if !defined(QT_NO_XRENDER)
1149 else {
1150 d->composition_mode =
1151 qpainterOpToXrender(state.compositionMode());
1152 }
1153#endif
1154 XSetFunction(X11->display, d->gc, function);
1155 XSetFunction(X11->display, d->gc_brush, function);
1156 }
1157 d->decidePathFallback();
1158 d->decideCoordAdjust();
1159}
1160
1161void QX11PaintEngine::updateRenderHints(QPainter::RenderHints hints)
1162{
1163 Q_D(QX11PaintEngine);
1164 d->render_hints = hints;
1165
1166#if !defined(QT_NO_XRENDER)
1167 if (X11->use_xrender && d->picture) {
1168 XRenderPictureAttributes attrs;
1169 attrs.poly_edge = (hints & QPainter::Antialiasing) ? PolyEdgeSmooth : PolyEdgeSharp;
1170 XRenderChangePicture(d->dpy, d->picture, CPPolyEdge, &attrs);
1171 }
1172#endif
1173}
1174
1175void QX11PaintEngine::updatePen(const QPen &pen)
1176{
1177 Q_D(QX11PaintEngine);
1178 d->cpen = pen;
1179 int cp = CapButt;
1180 int jn = JoinMiter;
1181 int ps = pen.style();
1182
1183 if (d->opacity < 1.0) {
1184 QColor c = d->cpen.color();
1185 c.setAlpha(qRound(c.alpha()*d->opacity));
1186 d->cpen.setColor(c);
1187 }
1188
1189 d->has_pen = (ps != Qt::NoPen);
1190 d->has_alpha_pen = (pen.color().alpha() != 255);
1191
1192 switch (pen.capStyle()) {
1193 case Qt::SquareCap:
1194 cp = CapProjecting;
1195 break;
1196 case Qt::RoundCap:
1197 cp = CapRound;
1198 break;
1199 case Qt::FlatCap:
1200 default:
1201 cp = CapButt;
1202 break;
1203 }
1204 switch (pen.joinStyle()) {
1205 case Qt::BevelJoin:
1206 jn = JoinBevel;
1207 break;
1208 case Qt::RoundJoin:
1209 jn = JoinRound;
1210 break;
1211 case Qt::MiterJoin:
1212 default:
1213 jn = JoinMiter;
1214 break;
1215 }
1216
1217 d->adapted_pen_origin = false;
1218
1219 char dashes[10]; // custom pen dashes
1220 int dash_len = 0; // length of dash list
1221 int xStyle = LineSolid;
1222
1223 /*
1224 We are emulating Windows here. Windows treats cpen.width() == 1
1225 (or 0) as a very special case. The fudge variable unifies this
1226 case with the general case.
1227 */
1228 qreal pen_width = pen.widthF();
1229 int scale = qRound(pen_width < 1 ? 1 : pen_width);
1230 int space = (pen_width < 1 && pen_width > 0 ? 1 : (2 * scale));
1231 int dot = 1 * scale;
1232 int dash = 4 * scale;
1233
1234 d->has_custom_pen = false;
1235
1236 switch (ps) {
1237 case Qt::NoPen:
1238 case Qt::SolidLine:
1239 xStyle = LineSolid;
1240 break;
1241 case Qt::DashLine:
1242 dashes[0] = dash;
1243 dashes[1] = space;
1244 dash_len = 2;
1245 xStyle = LineOnOffDash;
1246 break;
1247 case Qt::DotLine:
1248 dashes[0] = dot;
1249 dashes[1] = space;
1250 dash_len = 2;
1251 xStyle = LineOnOffDash;
1252 break;
1253 case Qt::DashDotLine:
1254 dashes[0] = dash;
1255 dashes[1] = space;
1256 dashes[2] = dot;
1257 dashes[3] = space;
1258 dash_len = 4;
1259 xStyle = LineOnOffDash;
1260 break;
1261 case Qt::DashDotDotLine:
1262 dashes[0] = dash;
1263 dashes[1] = space;
1264 dashes[2] = dot;
1265 dashes[3] = space;
1266 dashes[4] = dot;
1267 dashes[5] = space;
1268 dash_len = 6;
1269 xStyle = LineOnOffDash;
1270 break;
1271 case Qt::CustomDashLine:
1272 d->has_custom_pen = true;
1273 break;
1274 }
1275
1276 ulong mask = GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth
1277 | GCCapStyle | GCJoinStyle | GCLineStyle;
1278 XGCValues vals;
1279 vals.graphics_exposures = false;
1280 if (d->pdev_depth == 1) {
1281 vals.foreground = qGray(pen.color().rgb()) > 127 ? 0 : 1;
1282 vals.background = qGray(QColor(Qt::transparent).rgb()) > 127 ? 0 : 1;
1283 } else if (d->pdev->devType() == QInternal::Pixmap && d->pdev_depth == 32
1284 && X11->use_xrender) {
1285 vals.foreground = pen.color().rgba();
1286 vals.background = QColor(Qt::transparent).rgba();
1287 } else {
1288 QColormap cmap = QColormap::instance(d->scrn);
1289 vals.foreground = cmap.pixel(pen.color());
1290 vals.background = cmap.pixel(QColor(Qt::transparent));
1291 }
1292
1293
1294 vals.line_width = qRound(pen.widthF());
1295 vals.cap_style = cp;
1296 vals.join_style = jn;
1297 vals.line_style = xStyle;
1298
1299 XChangeGC(d->dpy, d->gc, mask, &vals);
1300
1301 if (dash_len) { // make dash list
1302 XSetDashes(d->dpy, d->gc, 0, dashes, dash_len);
1303 }
1304
1305 if (!d->has_clipping) { // if clipping is set the paintevent clip region is merged with the clip region
1306 QRegion sysClip = systemClip();
1307 if (!sysClip.isEmpty())
1308 x11SetClipRegion(d->dpy, d->gc, 0, d->picture, sysClip);
1309 else
1310 x11ClearClipRegion(d->dpy, d->gc, 0, d->picture);
1311 }
1312}
1313
1314void QX11PaintEngine::updateBrush(const QBrush &brush, const QPointF &origin)
1315{
1316 Q_D(QX11PaintEngine);
1317 d->cbrush = brush;
1318 d->bg_origin = origin;
1319 d->adapted_brush_origin = false;
1320#if !defined(QT_NO_XRENDER)
1321 d->current_brush = 0;
1322#endif
1323 if (d->opacity < 1.0) {
1324 QColor c = d->cbrush.color();
1325 c.setAlpha(qRound(c.alpha()*d->opacity));
1326 d->cbrush.setColor(c);
1327 }
1328
1329 int s = FillSolid;
1330 int bs = d->cbrush.style();
1331 d->has_brush = (bs != Qt::NoBrush);
1332 d->has_pattern = bs >= Qt::Dense1Pattern && bs <= Qt::DiagCrossPattern;
1333 d->has_texture = bs == Qt::TexturePattern;
1334 d->has_alpha_brush = brush.color().alpha() != 255;
1335 d->has_alpha_texture = d->has_texture && d->cbrush.texture().hasAlphaChannel();
1336
1337 ulong mask = GCForeground | GCBackground | GCGraphicsExposures
1338 | GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle;
1339 XGCValues vals;
1340 vals.graphics_exposures = false;
1341 if (d->pdev_depth == 1) {
1342 vals.foreground = qGray(d->cbrush.color().rgb()) > 127 ? 0 : 1;
1343 vals.background = qGray(QColor(Qt::transparent).rgb()) > 127 ? 0 : 1;
1344 } else if (X11->use_xrender && d->pdev->devType() == QInternal::Pixmap
1345 && d->pdev_depth == 32) {
1346 vals.foreground = d->cbrush.color().rgba();
1347 vals.background = QColor(Qt::transparent).rgba();
1348 } else {
1349 QColormap cmap = QColormap::instance(d->scrn);
1350 vals.foreground = cmap.pixel(d->cbrush.color());
1351 vals.background = cmap.pixel(QColor(Qt::transparent));
1352
1353 if (!X11->use_xrender && d->has_brush && !d->has_pattern && !brush.isOpaque()) {
1354 QPixmap pattern = qt_patternForAlpha(brush.color().alpha(), d->scrn);
1355 mask |= GCStipple;
1356 vals.stipple = pattern.handle();
1357 s = FillStippled;
1358 d->adapted_brush_origin = true;
1359 }
1360 }
1361 vals.cap_style = CapButt;
1362 vals.join_style = JoinMiter;
1363 vals.line_style = LineSolid;
1364
1365 if (d->has_pattern || d->has_texture) {
1366 if (bs == Qt::TexturePattern) {
1367 d->brush_pm = qt_toX11Pixmap(d->cbrush.texture());
1368#if !defined(QT_NO_XRENDER)
1369 if (X11->use_xrender) {
1370 XRenderPictureAttributes attrs;
1371 attrs.repeat = true;
1372 XRenderChangePicture(d->dpy, d->brush_pm.x11PictureHandle(), CPRepeat, &attrs);
1373 QX11PixmapData *data = static_cast<QX11PixmapData*>(d->brush_pm.data.data());
1374 if (data->mask_picture)
1375 XRenderChangePicture(d->dpy, data->mask_picture, CPRepeat, &attrs);
1376 }
1377#endif
1378 } else {
1379 d->brush_pm = qt_toX11Pixmap(qt_pixmapForBrush(bs, true));
1380 }
1381 d->brush_pm.x11SetScreen(d->scrn);
1382 if (d->brush_pm.depth() == 1) {
1383 mask |= GCStipple;
1384 vals.stipple = d->brush_pm.handle();
1385 s = FillStippled;
1386#if !defined(QT_NO_XRENDER)
1387 if (X11->use_xrender) {
1388 d->bitmap_texture = QPixmap(d->brush_pm.size());
1389 d->bitmap_texture.fill(Qt::transparent);
1390 d->bitmap_texture = qt_toX11Pixmap(d->bitmap_texture);
1391 d->bitmap_texture.x11SetScreen(d->scrn);
1392
1393 ::Picture src = X11->getSolidFill(d->scrn, d->cbrush.color());
1394 XRenderComposite(d->dpy, PictOpSrc, src, d->brush_pm.x11PictureHandle(),
1395 d->bitmap_texture.x11PictureHandle(),
1396 0, 0, d->brush_pm.width(), d->brush_pm.height(),
1397 0, 0, d->brush_pm.width(), d->brush_pm.height());
1398
1399 XRenderPictureAttributes attrs;
1400 attrs.repeat = true;
1401 XRenderChangePicture(d->dpy, d->bitmap_texture.x11PictureHandle(), CPRepeat, &attrs);
1402
1403 d->current_brush = d->bitmap_texture.x11PictureHandle();
1404 }
1405#endif
1406 } else {
1407 mask |= GCTile;
1408#ifndef QT_NO_XRENDER
1409 if (d->pdev_depth == 32 && d->brush_pm.depth() != 32) {
1410 d->brush_pm.detach();
1411 QX11PixmapData *brushData = static_cast<QX11PixmapData*>(d->brush_pm.data.data());
1412 brushData->convertToARGB32();
1413 }
1414#endif
1415 vals.tile = (d->brush_pm.depth() == d->pdev_depth
1416 ? d->brush_pm.handle()
1417 : static_cast<QX11PixmapData*>(d->brush_pm.data.data())->x11ConvertToDefaultDepth());
1418 s = FillTiled;
1419#if !defined(QT_NO_XRENDER)
1420 d->current_brush = d->cbrush.texture().x11PictureHandle();
1421#endif
1422 }
1423
1424 mask |= GCTileStipXOrigin | GCTileStipYOrigin;
1425 vals.ts_x_origin = qRound(origin.x());
1426 vals.ts_y_origin = qRound(origin.y());
1427 }
1428#if !defined(QT_NO_XRENDER)
1429 else if (d->has_alpha_brush) {
1430 d->current_brush = X11->getSolidFill(d->scrn, d->cbrush.color());
1431 }
1432#endif
1433
1434 vals.fill_style = s;
1435 XChangeGC(d->dpy, d->gc_brush, mask, &vals);
1436 if (!d->has_clipping) {
1437 QRegion sysClip = systemClip();
1438 if (!sysClip.isEmpty())
1439 x11SetClipRegion(d->dpy, d->gc_brush, 0, d->picture, sysClip);
1440 else
1441 x11ClearClipRegion(d->dpy, d->gc_brush, 0, d->picture);
1442 }
1443}
1444
1445void QX11PaintEngine::drawEllipse(const QRectF &rect)
1446{
1447 QRect aligned = rect.toAlignedRect();
1448 if (aligned == rect)
1449 drawEllipse(aligned);
1450 else
1451 QPaintEngine::drawEllipse(rect);
1452}
1453
1454void QX11PaintEngine::drawEllipse(const QRect &rect)
1455{
1456 if (rect.isEmpty()) {
1457 drawRects(&rect, 1);
1458 return;
1459 }
1460
1461 Q_D(QX11PaintEngine);
1462 QRect devclip(SHRT_MIN, SHRT_MIN, SHRT_MAX*2 - 1, SHRT_MAX*2 - 1);
1463 QRect r(rect);
1464 if (d->txop < QTransform::TxRotate) {
1465 r = d->matrix.mapRect(rect);
1466 } else if (d->txop == QTransform::TxRotate && rect.width() == rect.height()) {
1467 QPainterPath path;
1468 path.addEllipse(rect);
1469 r = d->matrix.map(path).boundingRect().toRect();
1470 }
1471
1472 if (d->has_alpha_brush || d->has_alpha_pen || d->has_custom_pen || (d->render_hints & QPainter::Antialiasing)
1473 || d->has_alpha_texture || devclip.intersected(r) != r
1474 || (d->has_complex_xform
1475 && !(d->has_non_scaling_xform && rect.width() == rect.height())))
1476 {
1477 QPainterPath path;
1478 path.addEllipse(rect);
1479 drawPath(path);
1480 return;
1481 }
1482
1483 int x = r.x();
1484 int y = r.y();
1485 int w = r.width();
1486 int h = r.height();
1487 if (w < 1 || h < 1)
1488 return;
1489 if (w == 1 && h == 1) {
1490 XDrawPoint(d->dpy, d->hd, d->has_pen ? d->gc : d->gc_brush, x, y);
1491 return;
1492 }
1493 d->setupAdaptedOrigin(rect.topLeft());
1494 if (d->has_brush) { // draw filled ellipse
1495 XFillArc(d->dpy, d->hd, d->gc_brush, x, y, w, h, 0, 360*64);
1496 if (!d->has_pen) // make smoother outline
1497 XDrawArc(d->dpy, d->hd, d->gc_brush, x, y, w-1, h-1, 0, 360*64);
1498 }
1499 if (d->has_pen) // draw outline
1500 XDrawArc(d->dpy, d->hd, d->gc, x, y, w, h, 0, 360*64);
1501 d->resetAdaptedOrigin();
1502}
1503
1504
1505
1506void QX11PaintEnginePrivate::fillPolygon_translated(const QPointF *polygonPoints, int pointCount,
1507 QX11PaintEnginePrivate::GCMode gcMode,
1508 QPaintEngine::PolygonDrawMode mode)
1509{
1510
1511 QVarLengthArray<QPointF> translated_points(pointCount);
1512 QPointF offset(matrix.dx(), matrix.dy());
1513
1514 qreal offs = adjust_coords ? aliasedCoordinateDelta : 0.0;
1515 if (!X11->use_xrender || !(render_hints & QPainter::Antialiasing))
1516 offset += QPointF(aliasedCoordinateDelta, aliasedCoordinateDelta);
1517
1518 for (int i = 0; i < pointCount; ++i) {
1519 translated_points[i] = polygonPoints[i] + offset;
1520
1521 translated_points[i].rx() = qRound(translated_points[i].x()) + offs;
1522 translated_points[i].ry() = qRound(translated_points[i].y()) + offs;
1523 }
1524
1525 fillPolygon_dev(translated_points.data(), pointCount, gcMode, mode);
1526}
1527
1528#ifndef QT_NO_XRENDER
1529static void qt_XRenderCompositeTrapezoids(Display *dpy,
1530 int op,
1531 Picture src,
1532 Picture dst,
1533 _Xconst XRenderPictFormat *maskFormat,
1534 int xSrc,
1535 int ySrc,
1536 const XTrapezoid *traps, int size)
1537{
1538 const int MAX_TRAPS = 50000;
1539 while (size) {
1540 int to_draw = size;
1541 if (to_draw > MAX_TRAPS)
1542 to_draw = MAX_TRAPS;
1543 XRenderCompositeTrapezoids(dpy, op, src, dst,
1544 maskFormat,
1545 xSrc, ySrc,
1546 traps, to_draw);
1547 size -= to_draw;
1548 traps += to_draw;
1549 }
1550}
1551#endif
1552
1553void QX11PaintEnginePrivate::fillPolygon_dev(const QPointF *polygonPoints, int pointCount,
1554 QX11PaintEnginePrivate::GCMode gcMode,
1555 QPaintEngine::PolygonDrawMode mode)
1556{
1557 Q_Q(QX11PaintEngine);
1558
1559 int clippedCount = 0;
1560 qt_float_point *clippedPoints = 0;
1561
1562#ifndef QT_NO_XRENDER
1563 //can change if we switch to pen if gcMode != BrushGC
1564 bool has_fill_texture = has_texture;
1565 bool has_fill_pattern = has_pattern;
1566 ::Picture src;
1567#endif
1568 QBrush fill;
1569 GC fill_gc;
1570 if (gcMode == BrushGC) {
1571 fill = cbrush;
1572 fill_gc = gc_brush;
1573#ifndef QT_NO_XRENDER
1574 if (current_brush)
1575 src = current_brush;
1576 else
1577 src = X11->getSolidFill(scrn, fill.color());
1578#endif
1579 } else {
1580 fill = QBrush(cpen.brush());
1581 fill_gc = gc;
1582#ifndef QT_NO_XRENDER
1583 //we use the pens brush
1584 has_fill_texture = (fill.style() == Qt::TexturePattern);
1585 has_fill_pattern = (fill.style() >= Qt::Dense1Pattern && fill.style() <= Qt::DiagCrossPattern);
1586 if (has_fill_texture)
1587 src = fill.texture().x11PictureHandle();
1588 else if (has_fill_pattern)
1589 src = getPatternFill(scrn, fill);
1590 else
1591 src = X11->getSolidFill(scrn, fill.color());
1592#endif
1593 }
1594
1595 polygonClipper.clipPolygon((qt_float_point *) polygonPoints, pointCount,
1596 &clippedPoints, &clippedCount);
1597
1598#ifndef QT_NO_XRENDER
1599 bool solid_fill = fill.color().alpha() == 255;
1600 if (has_fill_texture && fill.texture().depth() == 1 && solid_fill) {
1601 has_fill_texture = false;
1602 has_fill_pattern = true;
1603 }
1604
1605 bool antialias = render_hints & QPainter::Antialiasing;
1606
1607 if (X11->use_xrender
1608 && picture
1609 && !has_fill_pattern
1610 && (clippedCount > 0)
1611 && (fill.style() != Qt::NoBrush)
1612 && ((has_fill_texture && fill.texture().hasAlpha()) || antialias || !solid_fill || has_alpha_pen != has_alpha_brush))
1613 {
1614 QRect br = tessellator->tessellate((QPointF *)clippedPoints, clippedCount,
1615 mode == QPaintEngine::WindingMode);
1616 if (tessellator->size > 0) {
1617 XRenderPictureAttributes attrs;
1618 attrs.poly_edge = antialias ? PolyEdgeSmooth : PolyEdgeSharp;
1619 XRenderChangePicture(dpy, picture, CPPolyEdge, &attrs);
1620 int x_offset = int(XFixedToDouble(tessellator->traps[0].left.p1.x) - bg_origin.x());
1621 int y_offset = int(XFixedToDouble(tessellator->traps[0].left.p1.y) - bg_origin.y());
1622 qt_XRenderCompositeTrapezoids(dpy, composition_mode, src, picture,
1623 antialias
1624 ? XRenderFindStandardFormat(dpy, PictStandardA8)
1625 : XRenderFindStandardFormat(dpy, PictStandardA1),
1626 x_offset, y_offset,
1627 tessellator->traps, tessellator->size);
1628 tessellator->done();
1629 }
1630 } else
1631#endif
1632 if (fill.style() != Qt::NoBrush) {
1633 if (clippedCount > 200000) {
1634 QPolygon poly;
1635 for (int i = 0; i < clippedCount; ++i)
1636 poly << QPoint(qFloor(clippedPoints[i].x), qFloor(clippedPoints[i].y));
1637
1638 const QRect bounds = poly.boundingRect();
1639 const QRect aligned = bounds
1640 & QRect(QPoint(), QSize(pdev->width(), pdev->height()));
1641
1642 QImage img(aligned.size(), QImage::Format_ARGB32_Premultiplied);
1643 img.fill(0);
1644
1645 QPainter painter(&img);
1646 painter.translate(-aligned.x(), -aligned.y());
1647 painter.setPen(Qt::NoPen);
1648 painter.setBrush(fill);
1649 if (gcMode == BrushGC)
1650 painter.setBrushOrigin(q->painter()->brushOrigin());
1651 painter.drawPolygon(poly);
1652 painter.end();
1653
1654 q->drawImage(aligned, img, img.rect(), Qt::AutoColor);
1655 } else if (clippedCount > 0) {
1656 QVarLengthArray<XPoint> xpoints(clippedCount);
1657 for (int i = 0; i < clippedCount; ++i) {
1658 xpoints[i].x = qFloor(clippedPoints[i].x);
1659 xpoints[i].y = qFloor(clippedPoints[i].y);
1660 }
1661 if (mode == QPaintEngine::WindingMode)
1662 XSetFillRule(dpy, fill_gc, WindingRule);
1663 setupAdaptedOrigin(QPoint(xpoints[0].x, xpoints[0].y));
1664 XFillPolygon(dpy, hd, fill_gc,
1665 xpoints.data(), clippedCount,
1666 mode == QPaintEngine::ConvexMode ? Convex : Complex, CoordModeOrigin);
1667 resetAdaptedOrigin();
1668 if (mode == QPaintEngine::WindingMode)
1669 XSetFillRule(dpy, fill_gc, EvenOddRule);
1670 }
1671 }
1672}
1673
1674void QX11PaintEnginePrivate::strokePolygon_translated(const QPointF *polygonPoints, int pointCount, bool close)
1675{
1676 QVarLengthArray<QPointF> translated_points(pointCount);
1677 QPointF offset(matrix.dx(), matrix.dy());
1678 for (int i = 0; i < pointCount; ++i)
1679 translated_points[i] = polygonPoints[i] + offset;
1680 strokePolygon_dev(translated_points.data(), pointCount, close);
1681}
1682
1683void QX11PaintEnginePrivate::strokePolygon_dev(const QPointF *polygonPoints, int pointCount, bool close)
1684{
1685 int clippedCount = 0;
1686 qt_float_point *clippedPoints = 0;
1687 polygonClipper.clipPolygon((qt_float_point *) polygonPoints, pointCount,
1688 &clippedPoints, &clippedCount, close);
1689
1690 if (clippedCount > 0) {
1691 QVarLengthArray<XPoint> xpoints(clippedCount);
1692 for (int i = 0; i < clippedCount; ++i) {
1693 xpoints[i].x = qRound(clippedPoints[i].x + aliasedCoordinateDelta);
1694 xpoints[i].y = qRound(clippedPoints[i].y + aliasedCoordinateDelta);
1695 }
1696 uint numberPoints = qMin(clippedCount, xlibMaxLinePoints);
1697 XPoint *pts = xpoints.data();
1698 XDrawLines(dpy, hd, gc, pts, numberPoints, CoordModeOrigin);
1699 pts += numberPoints;
1700 clippedCount -= numberPoints;
1701 numberPoints = qMin(clippedCount, xlibMaxLinePoints-1);
1702 while (clippedCount) {
1703 XDrawLines(dpy, hd, gc, pts-1, numberPoints+1, CoordModeOrigin);
1704 pts += numberPoints;
1705 clippedCount -= numberPoints;
1706 numberPoints = qMin(clippedCount, xlibMaxLinePoints-1);
1707 }
1708 }
1709}
1710
1711void QX11PaintEngine::drawPolygon(const QPointF *polygonPoints, int pointCount, PolygonDrawMode mode)
1712{
1713 Q_D(QX11PaintEngine);
1714 if (d->use_path_fallback) {
1715 QPainterPath path(polygonPoints[0]);
1716 for (int i = 1; i < pointCount; ++i)
1717 path.lineTo(polygonPoints[i]);
1718 if (mode == PolylineMode) {
1719 QBrush oldBrush = d->cbrush;
1720 d->cbrush = QBrush(Qt::NoBrush);
1721 path.setFillRule(Qt::WindingFill);
1722 drawPath(path);
1723 d->cbrush = oldBrush;
1724 } else {
1725 path.setFillRule(mode == OddEvenMode ? Qt::OddEvenFill : Qt::WindingFill);
1726 path.closeSubpath();
1727 drawPath(path);
1728 }
1729 return;
1730 }
1731 if (mode != PolylineMode && d->has_brush)
1732 d->fillPolygon_translated(polygonPoints, pointCount, QX11PaintEnginePrivate::BrushGC, mode);
1733
1734 if (d->has_pen)
1735 d->strokePolygon_translated(polygonPoints, pointCount, mode != PolylineMode);
1736}
1737
1738
1739void QX11PaintEnginePrivate::fillPath(const QPainterPath &path, QX11PaintEnginePrivate::GCMode gc_mode, bool transform)
1740{
1741 qreal offs = adjust_coords ? aliasedCoordinateDelta : 0.0;
1742
1743 QPainterPath clippedPath;
1744 QPainterPath clipPath;
1745 clipPath.addRect(polygonClipper.boundingRect());
1746
1747 if (transform)
1748 clippedPath = (path*matrix).intersected(clipPath);
1749 else
1750 clippedPath = path.intersected(clipPath);
1751
1752 QList<QPolygonF> polys = clippedPath.toFillPolygons();
1753 for (int i = 0; i < polys.size(); ++i) {
1754 QVarLengthArray<QPointF> translated_points(polys.at(i).size());
1755
1756 for (int j = 0; j < polys.at(i).size(); ++j) {
1757 translated_points[j] = polys.at(i).at(j);
1758 if (!X11->use_xrender || !(render_hints & QPainter::Antialiasing)) {
1759 translated_points[j].rx() = qRound(translated_points[j].rx() + aliasedCoordinateDelta) + offs;
1760 translated_points[j].ry() = qRound(translated_points[j].ry() + aliasedCoordinateDelta) + offs;
1761 }
1762 }
1763
1764 fillPolygon_dev(translated_points.data(), polys.at(i).size(), gc_mode,
1765 path.fillRule() == Qt::OddEvenFill ? QPaintEngine::OddEvenMode : QPaintEngine::WindingMode);
1766 }
1767}
1768
1769void QX11PaintEngine::drawPath(const QPainterPath &path)
1770{
1771 Q_D(QX11PaintEngine);
1772 if (path.isEmpty())
1773 return;
1774 QTransform old_matrix = d->matrix;
1775
1776 if (d->has_brush)
1777 d->fillPath(path, QX11PaintEnginePrivate::BrushGC, true);
1778 if (d->has_pen
1779 && ((X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
1780 || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate
1781 && !d->has_non_scaling_xform)
1782 || (d->cpen.style() == Qt::CustomDashLine))) {
1783 QPainterPathStroker stroker;
1784 if (d->cpen.style() == Qt::CustomDashLine) {
1785 stroker.setDashPattern(d->cpen.dashPattern());
1786 stroker.setDashOffset(d->cpen.dashOffset());
1787 } else {
1788 stroker.setDashPattern(d->cpen.style());
1789 }
1790 stroker.setCapStyle(d->cpen.capStyle());
1791 stroker.setJoinStyle(d->cpen.joinStyle());
1792 QPainterPath stroke;
1793 qreal width = d->cpen.widthF();
1794 QPolygonF poly;
1795 QRectF deviceRect(0, 0, d->pdev->width(), d->pdev->height());
1796 // necessary to get aliased alphablended primitives to be drawn correctly
1797 if (d->cpen.isCosmetic() || d->has_scaling_xform) {
1798 if (d->cpen.isCosmetic())
1799 stroker.setWidth(width == 0 ? 1 : width);
1800 else
1801 stroker.setWidth(width * d->xform_scale);
1802 stroker.d_ptr->stroker.setClipRect(deviceRect);
1803 stroke = stroker.createStroke(path * d->matrix);
1804 if (stroke.isEmpty())
1805 return;
1806 stroke.setFillRule(Qt::WindingFill);
1807 d->fillPath(stroke, QX11PaintEnginePrivate::PenGC, false);
1808 } else {
1809 stroker.setWidth(width);
1810 stroker.d_ptr->stroker.setClipRect(d->matrix.inverted().mapRect(deviceRect));
1811 stroke = stroker.createStroke(path);
1812 if (stroke.isEmpty())
1813 return;
1814 stroke.setFillRule(Qt::WindingFill);
1815 d->fillPath(stroke, QX11PaintEnginePrivate::PenGC, true);
1816 }
1817 } else if (d->has_pen) {
1818 // if we have a cosmetic pen - use XDrawLine() for speed
1819 QList<QPolygonF> polys = path.toSubpathPolygons(d->matrix);
1820 for (int i = 0; i < polys.size(); ++i)
1821 d->strokePolygon_dev(polys.at(i).data(), polys.at(i).size(), false);
1822 }
1823}
1824
1825Q_GUI_EXPORT void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage &image,
1826 Drawable hd, GC gc, Display *dpy, Visual *visual, int depth)
1827{
1828 Q_ASSERT(image.format() == QImage::Format_RGB32);
1829 Q_ASSERT(image.depth() == 32);
1830
1831 XImage *xi;
1832 // Note: this code assumes either RGB or BGR, 8 bpc server layouts
1833 const uint red_mask = (uint) visual->red_mask;
1834 bool bgr_layout = (red_mask == 0xff);
1835
1836 const int w = rect.width();
1837 const int h = rect.height();
1838
1839 QImage im;
1840 int image_byte_order = ImageByteOrder(X11->display);
1841 if ((QSysInfo::ByteOrder == QSysInfo::BigEndian && ((image_byte_order == LSBFirst) || bgr_layout))
1842 || (image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian)
1843 || (image_byte_order == LSBFirst && bgr_layout))
1844 {
1845 im = image.copy(rect);
1846 const int iw = im.bytesPerLine() / 4;
1847 uint *data = (uint *)im.bits();
1848 for (int i=0; i < h; i++) {
1849 uint *p = data;
1850 uint *end = p + w;
1851 if (bgr_layout && image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
1852 while (p < end) {
1853 *p = ((*p << 8) & 0xffffff00) | ((*p >> 24) & 0x000000ff);
1854 p++;
1855 }
1856 } else if ((image_byte_order == LSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian)
1857 || (image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian)) {
1858 while (p < end) {
1859 *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
1860 | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
1861 p++;
1862 }
1863 } else if ((image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian)
1864 || (image_byte_order == LSBFirst && bgr_layout))
1865 {
1866 while (p < end) {
1867 *p = ((*p << 16) & 0x00ff0000) | ((*p >> 16) & 0x000000ff)
1868 | ((*p ) & 0xff00ff00);
1869 p++;
1870 }
1871 }
1872 data += iw;
1873 }
1874 xi = XCreateImage(dpy, visual, depth, ZPixmap,
1875 0, (char *) im.bits(), w, h, 32, im.bytesPerLine());
1876 } else {
1877 xi = XCreateImage(dpy, visual, depth, ZPixmap,
1878 0, (char *) image.scanLine(rect.y())+rect.x()*sizeof(uint), w, h, 32, image.bytesPerLine());
1879 }
1880 XPutImage(dpy, hd, gc, xi, 0, 0, pos.x(), pos.y(), w, h);
1881 xi->data = 0; // QImage owns these bits
1882 XDestroyImage(xi);
1883}
1884
1885void QX11PaintEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags flags)
1886{
1887 Q_D(QX11PaintEngine);
1888
1889 if (image.format() == QImage::Format_RGB32
1890 && d->pdev_depth >= 24 && image.depth() == 32
1891 && r.size() == sr.size())
1892 {
1893 int sx = qRound(sr.x());
1894 int sy = qRound(sr.y());
1895 int x = qRound(r.x());
1896 int y = qRound(r.y());
1897 int w = qRound(r.width());
1898 int h = qRound(r.height());
1899
1900 qt_x11_drawImage(QRect(sx, sy, w, h), QPoint(x, y), image, d->hd, d->gc, d->dpy,
1901 (Visual *)d->xinfo->visual(), d->pdev_depth);
1902 } else {
1903 QPaintEngine::drawImage(r, image, sr, flags);
1904 }
1905}
1906
1907void QX11PaintEngine::drawPixmap(const QRectF &r, const QPixmap &px, const QRectF &_sr)
1908{
1909 Q_D(QX11PaintEngine);
1910 QRectF sr = _sr;
1911 int x = qRound(r.x());
1912 int y = qRound(r.y());
1913 int sx = qRound(sr.x());
1914 int sy = qRound(sr.y());
1915 int sw = qRound(sr.width());
1916 int sh = qRound(sr.height());
1917
1918 QPixmap pixmap = qt_toX11Pixmap(px);
1919 if(pixmap.isNull())
1920 return;
1921
1922 if ((d->xinfo && d->xinfo->screen() != pixmap.x11Info().screen())
1923 || (pixmap.x11Info().screen() != DefaultScreen(X11->display))) {
1924 QPixmap* p = const_cast<QPixmap *>(&pixmap);
1925 p->x11SetScreen(d->xinfo ? d->xinfo->screen() : DefaultScreen(X11->display));
1926 }
1927
1928 QPixmap::x11SetDefaultScreen(pixmap.x11Info().screen());
1929
1930#ifndef QT_NO_XRENDER
1931 ::Picture src_pict = static_cast<QX11PixmapData*>(pixmap.data.data())->picture;
1932 if (src_pict && d->picture) {
1933 const int pDepth = pixmap.depth();
1934 if (pDepth == 1 && (d->has_alpha_pen)) {
1935 qt_render_bitmap(d->dpy, d->scrn, src_pict, d->picture,
1936 sx, sy, x, y, sw, sh, d->cpen);
1937 return;
1938 } else if (pDepth != 1 && (pDepth == 32 || pDepth != d->pdev_depth)) {
1939 XRenderComposite(d->dpy, d->composition_mode,
1940 src_pict, 0, d->picture, sx, sy, 0, 0, x, y, sw, sh);
1941 return;
1942 }
1943 }
1944#endif
1945
1946 bool mono_src = pixmap.depth() == 1;
1947 bool mono_dst = d->pdev_depth == 1;
1948 bool restore_clip = false;
1949
1950 if (static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask) { // pixmap has a mask
1951 QBitmap comb(sw, sh);
1952 GC cgc = XCreateGC(d->dpy, comb.handle(), 0, 0);
1953 XSetForeground(d->dpy, cgc, 0);
1954 XFillRectangle(d->dpy, comb.handle(), cgc, 0, 0, sw, sh);
1955 XSetBackground(d->dpy, cgc, 0);
1956 XSetForeground(d->dpy, cgc, 1);
1957 if (!d->crgn.isEmpty()) {
1958 int num;
1959 XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
1960 XSetClipRectangles(d->dpy, cgc, -x, -y, rects, num, Unsorted);
1961 } else if (d->has_clipping) {
1962 XSetClipRectangles(d->dpy, cgc, 0, 0, 0, 0, Unsorted);
1963 }
1964 XSetFillStyle(d->dpy, cgc, FillOpaqueStippled);
1965 XSetTSOrigin(d->dpy, cgc, -sx, -sy);
1966 XSetStipple(d->dpy, cgc,
1967 static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask);
1968 XFillRectangle(d->dpy, comb.handle(), cgc, 0, 0, sw, sh);
1969 XFreeGC(d->dpy, cgc);
1970
1971 XSetClipOrigin(d->dpy, d->gc, x, y);
1972 XSetClipMask(d->dpy, d->gc, comb.handle());
1973 restore_clip = true;
1974 }
1975
1976 if (mono_src) {
1977 if (!d->crgn.isEmpty()) {
1978 Pixmap comb = XCreatePixmap(d->dpy, d->hd, sw, sh, 1);
1979 GC cgc = XCreateGC(d->dpy, comb, 0, 0);
1980 XSetForeground(d->dpy, cgc, 0);
1981 XFillRectangle(d->dpy, comb, cgc, 0, 0, sw, sh);
1982 int num;
1983 XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
1984 XSetClipRectangles(d->dpy, cgc, -x, -y, rects, num, Unsorted);
1985 XCopyArea(d->dpy, pixmap.handle(), comb, cgc, sx, sy, sw, sh, 0, 0);
1986 XFreeGC(d->dpy, cgc);
1987
1988 XSetClipMask(d->dpy, d->gc, comb);
1989 XSetClipOrigin(d->dpy, d->gc, x, y);
1990 XFreePixmap(d->dpy, comb);
1991 } else {
1992 XSetClipMask(d->dpy, d->gc, pixmap.handle());
1993 XSetClipOrigin(d->dpy, d->gc, x - sx, y - sy);
1994 }
1995
1996 if (mono_dst) {
1997 XSetForeground(d->dpy, d->gc, qGray(d->cpen.color().rgb()) > 127 ? 0 : 1);
1998 } else {
1999 QColormap cmap = QColormap::instance(d->scrn);
2000 XSetForeground(d->dpy, d->gc, cmap.pixel(d->cpen.color()));
2001 }
2002 XFillRectangle(d->dpy, d->hd, d->gc, x, y, sw, sh);
2003 restore_clip = true;
2004 } else if (mono_dst && !mono_src) {
2005 QBitmap bitmap(pixmap);
2006 XCopyArea(d->dpy, bitmap.handle(), d->hd, d->gc, sx, sy, sw, sh, x, y);
2007 } else {
2008 XCopyArea(d->dpy, pixmap.handle(), d->hd, d->gc, sx, sy, sw, sh, x, y);
2009 }
2010
2011 if (d->pdev->devType() == QInternal::Pixmap) {
2012 const QPixmap *px = static_cast<const QPixmap*>(d->pdev);
2013 Pixmap src_mask = static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask;
2014 Pixmap dst_mask = static_cast<QX11PixmapData*>(px->data.data())->x11_mask;
2015 if (dst_mask) {
2016 GC cgc = XCreateGC(d->dpy, dst_mask, 0, 0);
2017 if (src_mask) { // copy src mask into dst mask
2018 XCopyArea(d->dpy, src_mask, dst_mask, cgc, sx, sy, sw, sh, x, y);
2019 } else { // no src mask, but make sure the area copied is opaque in dest
2020 XSetBackground(d->dpy, cgc, 0);
2021 XSetForeground(d->dpy, cgc, 1);
2022 XFillRectangle(d->dpy, dst_mask, cgc, x, y, sw, sh);
2023 }
2024 XFreeGC(d->dpy, cgc);
2025 }
2026 }
2027
2028 if (restore_clip) {
2029 XSetClipOrigin(d->dpy, d->gc, 0, 0);
2030 int num;
2031 XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
2032 if (num == 0)
2033 XSetClipMask(d->dpy, d->gc, XNone);
2034 else
2035 XSetClipRectangles(d->dpy, d->gc, 0, 0, rects, num, Unsorted);
2036 }
2037}
2038
2039void QX11PaintEngine::updateMatrix(const QTransform &mtx)
2040{
2041 Q_D(QX11PaintEngine);
2042 d->txop = mtx.type();
2043 d->matrix = mtx;
2044
2045 d->has_complex_xform = (d->txop > QTransform::TxTranslate);
2046
2047 extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
2048 bool scaling = qt_scaleForTransform(d->matrix, &d->xform_scale);
2049 d->has_scaling_xform = scaling && d->xform_scale != 1.0;
2050 d->has_non_scaling_xform = scaling && d->xform_scale == 1.0;
2051}
2052
2053/*
2054 NB! the clip region is expected to be in dev coordinates
2055*/
2056void QX11PaintEngine::updateClipRegion_dev(const QRegion &clipRegion, Qt::ClipOperation op)
2057{
2058 Q_D(QX11PaintEngine);
2059 QRegion sysClip = systemClip();
2060 if (op == Qt::NoClip) {
2061 d->has_clipping = false;
2062 d->crgn = sysClip;
2063 if (!sysClip.isEmpty()) {
2064 x11SetClipRegion(d->dpy, d->gc, d->gc_brush, d->picture, sysClip);
2065 } else {
2066 x11ClearClipRegion(d->dpy, d->gc, d->gc_brush, d->picture);
2067 }
2068 return;
2069 }
2070
2071 switch (op) {
2072 case Qt::IntersectClip:
2073 if (d->has_clipping) {
2074 d->crgn &= clipRegion;
2075 break;
2076 }
2077 // fall through
2078 case Qt::ReplaceClip:
2079 if (!sysClip.isEmpty())
2080 d->crgn = clipRegion.intersected(sysClip);
2081 else
2082 d->crgn = clipRegion;
2083 break;
2084 case Qt::UniteClip:
2085 d->crgn |= clipRegion;
2086 if (!sysClip.isEmpty())
2087 d->crgn = d->crgn.intersected(sysClip);
2088 break;
2089 default:
2090 break;
2091 }
2092 d->has_clipping = true;
2093 x11SetClipRegion(d->dpy, d->gc, d->gc_brush, d->picture, d->crgn);
2094}
2095
2096void QX11PaintEngine::updateFont(const QFont &)
2097{
2098}
2099
2100Qt::HANDLE QX11PaintEngine::handle() const
2101{
2102 Q_D(const QX11PaintEngine);
2103 Q_ASSERT(isActive());
2104 Q_ASSERT(d->hd);
2105 return d->hd;
2106}
2107
2108extern void qt_draw_tile(QPaintEngine *, qreal, qreal, qreal, qreal, const QPixmap &,
2109 qreal, qreal);
2110
2111void QX11PaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &p)
2112{
2113 int x = qRound(r.x());
2114 int y = qRound(r.y());
2115 int w = qRound(r.width());
2116 int h = qRound(r.height());
2117 int sx = qRound(p.x());
2118 int sy = qRound(p.y());
2119
2120 bool mono_src = pixmap.depth() == 1;
2121 Q_D(QX11PaintEngine);
2122
2123 if ((d->xinfo && d->xinfo->screen() != pixmap.x11Info().screen())
2124 || (pixmap.x11Info().screen() != DefaultScreen(X11->display))) {
2125 QPixmap* p = const_cast<QPixmap *>(&pixmap);
2126 p->x11SetScreen(d->xinfo ? d->xinfo->screen() : DefaultScreen(X11->display));
2127 }
2128
2129 QPixmap::x11SetDefaultScreen(pixmap.x11Info().screen());
2130
2131#ifndef QT_NO_XRENDER
2132 if (X11->use_xrender && d->picture && pixmap.x11PictureHandle()) {
2133#if 0
2134 // ### Qt 5: enable this
2135 XRenderPictureAttributes attrs;
2136 attrs.repeat = true;
2137 XRenderChangePicture(d->dpy, pixmap.x11PictureHandle(), CPRepeat, &attrs);
2138
2139 if (mono_src) {
2140 qt_render_bitmap(d->dpy, d->scrn, pixmap.x11PictureHandle(), d->picture,
2141 sx, sy, x, y, w, h, d->cpen);
2142 } else {
2143 XRenderComposite(d->dpy, d->composition_mode,
2144 pixmap.x11PictureHandle(), XNone, d->picture,
2145 sx, sy, 0, 0, x, y, w, h);
2146 }
2147#else
2148 const int numTiles = (w / pixmap.width()) * (h / pixmap.height());
2149 if (numTiles < 100) {
2150 // this is essentially qt_draw_tile(), inlined for
2151 // the XRenderComposite call
2152 int yPos, xPos, drawH, drawW, yOff, xOff;
2153 yPos = y;
2154 yOff = sy;
2155 while(yPos < y + h) {
2156 drawH = pixmap.height() - yOff; // Cropping first row
2157 if (yPos + drawH > y + h) // Cropping last row
2158 drawH = y + h - yPos;
2159 xPos = x;
2160 xOff = sx;
2161 while(xPos < x + w) {
2162 drawW = pixmap.width() - xOff; // Cropping first column
2163 if (xPos + drawW > x + w) // Cropping last column
2164 drawW = x + w - xPos;
2165 if (mono_src) {
2166 qt_render_bitmap(d->dpy, d->scrn, pixmap.x11PictureHandle(), d->picture,
2167 xOff, yOff, xPos, yPos, drawW, drawH, d->cpen);
2168 } else {
2169 XRenderComposite(d->dpy, d->composition_mode,
2170 pixmap.x11PictureHandle(), XNone, d->picture,
2171 xOff, yOff, 0, 0, xPos, yPos, drawW, drawH);
2172 }
2173 xPos += drawW;
2174 xOff = 0;
2175 }
2176 yPos += drawH;
2177 yOff = 0;
2178 }
2179 } else {
2180 w = qMin(w, d->pdev->width() - x);
2181 h = qMin(h, d->pdev->height() - y);
2182 if (w <= 0 || h <= 0)
2183 return;
2184
2185 const int pw = w + sx;
2186 const int ph = h + sy;
2187 QPixmap pm(pw, ph);
2188 if (pixmap.hasAlpha() || mono_src)
2189 pm.fill(Qt::transparent);
2190
2191 const int mode = pixmap.hasAlpha() ? PictOpOver : PictOpSrc;
2192 const ::Picture pmPicture = pm.x11PictureHandle();
2193
2194 // first tile
2195 XRenderComposite(d->dpy, mode,
2196 pixmap.x11PictureHandle(), XNone, pmPicture,
2197 0, 0, 0, 0, 0, 0, qMin(pw, pixmap.width()), qMin(ph, pixmap.height()));
2198
2199 // first row of tiles
2200 int xPos = pixmap.width();
2201 const int sh = qMin(ph, pixmap.height());
2202 while (xPos < pw) {
2203 const int sw = qMin(xPos, pw - xPos);
2204 XRenderComposite(d->dpy, mode,
2205 pmPicture, XNone, pmPicture,
2206 0, 0, 0, 0, xPos, 0, sw, sh);
2207 xPos *= 2;
2208 }
2209
2210 // remaining rows
2211 int yPos = pixmap.height();
2212 const int sw = pw;
2213 while (yPos < ph) {
2214 const int sh = qMin(yPos, ph - yPos);
2215 XRenderComposite(d->dpy, mode,
2216 pmPicture, XNone, pmPicture,
2217 0, 0, 0, 0, 0, yPos, sw, sh);
2218 yPos *= 2;
2219 }
2220
2221 // composite
2222 if (mono_src)
2223 qt_render_bitmap(d->dpy, d->scrn, pmPicture, d->picture,
2224 sx, sy, x, y, w, h, d->cpen);
2225 else
2226 XRenderComposite(d->dpy, d->composition_mode,
2227 pmPicture, XNone, d->picture,
2228 sx, sy, 0, 0, x, y, w, h);
2229 }
2230#endif
2231 } else
2232#endif // !QT_NO_XRENDER
2233 if (pixmap.depth() > 1 && !static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask) {
2234 XSetTile(d->dpy, d->gc, pixmap.handle());
2235 XSetFillStyle(d->dpy, d->gc, FillTiled);
2236 XSetTSOrigin(d->dpy, d->gc, x-sx, y-sy);
2237 XFillRectangle(d->dpy, d->hd, d->gc, x, y, w, h);
2238 XSetTSOrigin(d->dpy, d->gc, 0, 0);
2239 XSetFillStyle(d->dpy, d->gc, FillSolid);
2240 } else {
2241 qt_draw_tile(this, x, y, w, h, pixmap, sx, sy);
2242 }
2243}
2244
2245void QX11PaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
2246{
2247 const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
2248
2249 switch(ti.fontEngine->type()) {
2250 case QFontEngine::TestFontEngine:
2251 case QFontEngine::Box:
2252 d_func()->drawBoxTextItem(p, ti);
2253 break;
2254 case QFontEngine::XLFD:
2255 drawXLFD(p, ti);
2256 break;
2257#ifndef QT_NO_FONTCONFIG
2258 case QFontEngine::Freetype:
2259 drawFreetype(p, ti);
2260 break;
2261#endif
2262 default:
2263 Q_ASSERT(false);
2264 }
2265}
2266
2267void QX11PaintEngine::drawXLFD(const QPointF &p, const QTextItemInt &ti)
2268{
2269 Q_D(QX11PaintEngine);
2270
2271 if (d->txop > QTransform::TxTranslate) {
2272 // XServer or font don't support server side transformations, need to do it by hand
2273 QPaintEngine::drawTextItem(p, ti);
2274 return;
2275 }
2276
2277 if (!ti.glyphs.numGlyphs)
2278 return;
2279
2280 QVarLengthArray<QFixedPoint> positions;
2281 QVarLengthArray<glyph_t> glyphs;
2282 QTransform matrix = d->matrix;
2283 matrix.translate(p.x(), p.y());
2284 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
2285 if (glyphs.size() == 0)
2286 return;
2287
2288 QFontEngineXLFD *xlfd = static_cast<QFontEngineXLFD *>(ti.fontEngine);
2289 Qt::HANDLE font_id = xlfd->fontStruct()->fid;
2290
2291 XSetFont(d->dpy, d->gc, font_id);
2292
2293 const QFixed offs = QFixed::fromReal(aliasedCoordinateDelta);
2294 for (int i = 0; i < glyphs.size(); i++) {
2295 int xp = qRound(positions[i].x + offs);
2296 int yp = qRound(positions[i].y + offs);
2297 if (xp < SHRT_MAX && xp > SHRT_MIN && yp > SHRT_MIN && yp < SHRT_MAX) {
2298 XChar2b ch;
2299 ch.byte1 = glyphs[i] >> 8;
2300 ch.byte2 = glyphs[i] & 0xff;
2301 XDrawString16(d->dpy, d->hd, d->gc, xp, yp, &ch, 1);
2302 }
2303 }
2304}
2305
2306#ifndef QT_NO_FONTCONFIG
2307static QPainterPath path_for_glyphs(const QVarLengthArray<glyph_t> &glyphs,
2308 const QVarLengthArray<QFixedPoint> &positions,
2309 const QFontEngineFT *ft)
2310{
2311 QPainterPath path;
2312 path.setFillRule(Qt::WindingFill);
2313 ft->lockFace();
2314 int i = 0;
2315 while (i < glyphs.size()) {
2316 QFontEngineFT::Glyph *glyph = ft->loadGlyph(glyphs[i], QFontEngineFT::Format_Mono);
2317 // #### fix case where we don't get a glyph
2318 if (!glyph)
2319 break;
2320
2321 Q_ASSERT(glyph->format == QFontEngineFT::Format_Mono);
2322 int n = 0;
2323 int h = glyph->height;
2324 int xp = qRound(positions[i].x);
2325 int yp = qRound(positions[i].y);
2326
2327 xp += glyph->x;
2328 yp += -glyph->y + glyph->height;
2329 int pitch = ((glyph->width + 31) & ~31) >> 3;
2330
2331 uchar *src = glyph->data;
2332 while (h--) {
2333 for (int x = 0; x < glyph->width; ++x) {
2334 bool set = src[x >> 3] & (0x80 >> (x & 7));
2335 if (set) {
2336 QRect r(xp + x, yp - h, 1, 1);
2337 while (x < glyph->width-1 && src[(x+1) >> 3] & (0x80 >> ((x+1) & 7))) {
2338 ++x;
2339 r.setRight(r.right()+1);
2340 }
2341
2342 path.addRect(r);
2343 ++n;
2344 }
2345 }
2346 src += pitch;
2347 }
2348 ++i;
2349 }
2350 ft->unlockFace();
2351 return path;
2352}
2353
2354void QX11PaintEngine::drawFreetype(const QPointF &p, const QTextItemInt &ti)
2355{
2356 Q_D(QX11PaintEngine);
2357 if (!ti.glyphs.numGlyphs)
2358 return;
2359
2360 QFontEngineX11FT *ft = static_cast<QFontEngineX11FT *>(ti.fontEngine);
2361
2362 if (!d->cpen.isSolid()) {
2363 QPaintEngine::drawTextItem(p, ti);
2364 return;
2365 }
2366
2367 const bool xrenderPath = (X11->use_xrender
2368 && !(d->pdev->devType() == QInternal::Pixmap
2369 && static_cast<const QPixmap *>(d->pdev)->data->pixelType() == QPixmapData::BitmapType));
2370
2371 QVarLengthArray<QFixedPoint> positions;
2372 QVarLengthArray<glyph_t> glyphs;
2373 QTransform matrix;
2374
2375 if (xrenderPath)
2376 matrix = d->matrix;
2377 matrix.translate(p.x(), p.y());
2378 ft->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
2379 if (glyphs.count() == 0)
2380 return;
2381
2382#ifndef QT_NO_XRENDER
2383 QFontEngineFT::QGlyphSet *set = ft->defaultGlyphs();
2384 if (d->txop >= QTransform::TxScale && xrenderPath)
2385 set = ft->loadTransformedGlyphSet(d->matrix);
2386
2387 if (!set || set->outline_drawing
2388 || !ft->loadGlyphs(set, glyphs.data(), glyphs.size(), QFontEngineFT::Format_Render))
2389 {
2390 QPaintEngine::drawTextItem(p, ti);
2391 return;
2392 }
2393
2394 if (xrenderPath) {
2395 GlyphSet glyphSet = set->id;
2396 const QColor &pen = d->cpen.color();
2397 ::Picture src = X11->getSolidFill(d->scrn, pen);
2398 XRenderPictFormat *maskFormat = 0;
2399 if (ft->xglyph_format != PictStandardA1)
2400 maskFormat = XRenderFindStandardFormat(X11->display, ft->xglyph_format);
2401
2402 enum { t_min = SHRT_MIN, t_max = SHRT_MAX };
2403
2404 int i = 0;
2405 for (; i < glyphs.size()
2406 && (positions[i].x < t_min || positions[i].x > t_max
2407 || positions[i].y < t_min || positions[i].y > t_max);
2408 ++i)
2409 ;
2410
2411 if (i >= glyphs.size())
2412 return;
2413 ++i;
2414
2415 QFixed xp = positions[i - 1].x;
2416 QFixed yp = positions[i - 1].y;
2417 QFixed offs = QFixed::fromReal(aliasedCoordinateDelta);
2418
2419 XGlyphElt32 elt;
2420 elt.glyphset = glyphSet;
2421 elt.chars = &glyphs[i - 1];
2422 elt.nchars = 1;
2423 elt.xOff = qRound(xp + offs);
2424 elt.yOff = qRound(yp + offs);
2425 for (; i < glyphs.size(); ++i) {
2426 if (positions[i].x < t_min || positions[i].x > t_max
2427 || positions[i].y < t_min || positions[i].y > t_max) {
2428 break;
2429 }
2430 QFontEngineFT::Glyph *g = ft->cachedGlyph(glyphs[i - 1]);
2431 if (g
2432 && positions[i].x == xp + g->advance
2433 && positions[i].y == yp
2434 && elt.nchars < 253 // don't draw more than 253 characters as some X servers
2435 // hang with it
2436 ) {
2437 elt.nchars++;
2438 xp += g->advance;
2439 } else {
2440 xp = positions[i].x;
2441 yp = positions[i].y;
2442
2443 XRenderCompositeText32(X11->display, PictOpOver, src, d->picture,
2444 maskFormat, 0, 0, 0, 0,
2445 &elt, 1);
2446 elt.chars = &glyphs[i];
2447 elt.nchars = 1;
2448 elt.xOff = qRound(xp + offs);
2449 elt.yOff = qRound(yp + offs);
2450 }
2451 }
2452 XRenderCompositeText32(X11->display, PictOpOver, src, d->picture,
2453 maskFormat, 0, 0, 0, 0,
2454 &elt, 1);
2455
2456 return;
2457
2458 }
2459#endif
2460
2461 QPainterPath path = path_for_glyphs(glyphs, positions, ft);
2462 if (path.elementCount() <= 1)
2463 return;
2464 Q_ASSERT((path.elementCount() % 5) == 0);
2465 if (d->txop >= QTransform::TxScale) {
2466 painter()->save();
2467 painter()->setBrush(d->cpen.brush());
2468 painter()->setPen(Qt::NoPen);
2469 painter()->drawPath(path);
2470 painter()->restore();
2471 return;
2472 }
2473
2474 const int rectcount = 256;
2475 XRectangle rects[rectcount];
2476 int num_rects = 0;
2477
2478 QPoint delta(qRound(d->matrix.dx()), qRound(d->matrix.dy()));
2479 QRect clip(d->polygonClipper.boundingRect());
2480 for (int i=0; i < path.elementCount(); i+=5) {
2481 int x = qRound(path.elementAt(i).x);
2482 int y = qRound(path.elementAt(i).y);
2483 int w = qRound(path.elementAt(i+1).x) - x;
2484 int h = qRound(path.elementAt(i+2).y) - y;
2485
2486 QRect rect = QRect(x + delta.x(), y + delta.y(), w, h);
2487 rect = rect.intersected(clip);
2488 if (rect.isEmpty())
2489 continue;
2490
2491 rects[num_rects].x = short(rect.x());
2492 rects[num_rects].y = short(rect.y());
2493 rects[num_rects].width = ushort(rect.width());
2494 rects[num_rects].height = ushort(rect.height());
2495 ++num_rects;
2496 if (num_rects == rectcount) {
2497 XFillRectangles(d->dpy, d->hd, d->gc, rects, num_rects);
2498 num_rects = 0;
2499 }
2500 }
2501 if (num_rects > 0)
2502 XFillRectangles(d->dpy, d->hd, d->gc, rects, num_rects);
2503
2504}
2505#endif // !QT_NO_XRENDER
2506
2507QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.