1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "GraphicsContext.h"
28 
29 #include "BidiResolver.h"
30 #include "Font.h"
31 #include "Generator.h"
32 #include "ImageBuffer.h"
33 #include "IntRect.h"
34 #include "RoundedIntRect.h"
35 #include "TextRun.h"
36 
37 using namespace std;
38 
39 namespace WebCore {
40 
41 class TextRunIterator {
42 public:
TextRunIterator()43     TextRunIterator()
44         : m_textRun(0)
45         , m_offset(0)
46     {
47     }
48 
TextRunIterator(const TextRun * textRun,unsigned offset)49     TextRunIterator(const TextRun* textRun, unsigned offset)
50         : m_textRun(textRun)
51         , m_offset(offset)
52     {
53     }
54 
TextRunIterator(const TextRunIterator & other)55     TextRunIterator(const TextRunIterator& other)
56         : m_textRun(other.m_textRun)
57         , m_offset(other.m_offset)
58     {
59     }
60 
offset() const61     unsigned offset() const { return m_offset; }
increment()62     void increment() { m_offset++; }
atEnd() const63     bool atEnd() const { return !m_textRun || m_offset >= m_textRun->length(); }
current() const64     UChar current() const { return (*m_textRun)[m_offset]; }
direction() const65     WTF::Unicode::Direction direction() const { return atEnd() ? WTF::Unicode::OtherNeutral : WTF::Unicode::direction(current()); }
66 
operator ==(const TextRunIterator & other)67     bool operator==(const TextRunIterator& other)
68     {
69         return m_offset == other.m_offset && m_textRun == other.m_textRun;
70     }
71 
operator !=(const TextRunIterator & other)72     bool operator!=(const TextRunIterator& other) { return !operator==(other); }
73 
74 private:
75     const TextRun* m_textRun;
76     int m_offset;
77 };
78 
GraphicsContext(PlatformGraphicsContext * platformGraphicsContext)79 GraphicsContext::GraphicsContext(PlatformGraphicsContext* platformGraphicsContext)
80     : m_updatingControlTints(false)
81 {
82     platformInit(platformGraphicsContext);
83 }
84 
~GraphicsContext()85 GraphicsContext::~GraphicsContext()
86 {
87     ASSERT(m_stack.isEmpty());
88     platformDestroy();
89 }
90 
save()91 void GraphicsContext::save()
92 {
93     if (paintingDisabled())
94         return;
95 
96     m_stack.append(m_state);
97 
98     savePlatformState();
99 }
100 
restore()101 void GraphicsContext::restore()
102 {
103     if (paintingDisabled())
104         return;
105 
106     if (m_stack.isEmpty()) {
107         LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
108         return;
109     }
110     m_state = m_stack.last();
111     m_stack.removeLast();
112 
113     restorePlatformState();
114 }
115 
setStrokeThickness(float thickness)116 void GraphicsContext::setStrokeThickness(float thickness)
117 {
118     m_state.strokeThickness = thickness;
119     setPlatformStrokeThickness(thickness);
120 }
121 
setStrokeStyle(StrokeStyle style)122 void GraphicsContext::setStrokeStyle(StrokeStyle style)
123 {
124     m_state.strokeStyle = style;
125     setPlatformStrokeStyle(style);
126 }
127 
setStrokeColor(const Color & color,ColorSpace colorSpace)128 void GraphicsContext::setStrokeColor(const Color& color, ColorSpace colorSpace)
129 {
130     m_state.strokeColor = color;
131     m_state.strokeColorSpace = colorSpace;
132     m_state.strokeGradient.clear();
133     m_state.strokePattern.clear();
134     setPlatformStrokeColor(color, colorSpace);
135 }
136 
setShadow(const FloatSize & offset,float blur,const Color & color,ColorSpace colorSpace)137 void GraphicsContext::setShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
138 {
139     m_state.shadowOffset = offset;
140     m_state.shadowBlur = blur;
141     m_state.shadowColor = color;
142     m_state.shadowColorSpace = colorSpace;
143     setPlatformShadow(offset, blur, color, colorSpace);
144 }
145 
setLegacyShadow(const FloatSize & offset,float blur,const Color & color,ColorSpace colorSpace)146 void GraphicsContext::setLegacyShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
147 {
148     m_state.shadowOffset = offset;
149     m_state.shadowBlur = blur;
150     m_state.shadowColor = color;
151     m_state.shadowColorSpace = colorSpace;
152 #if USE(CG)
153     m_state.shadowsUseLegacyRadius = true;
154 #endif
155     setPlatformShadow(offset, blur, color, colorSpace);
156 }
157 
clearShadow()158 void GraphicsContext::clearShadow()
159 {
160     m_state.shadowOffset = FloatSize();
161     m_state.shadowBlur = 0;
162     m_state.shadowColor = Color();
163     m_state.shadowColorSpace = ColorSpaceDeviceRGB;
164     clearPlatformShadow();
165 }
166 
hasShadow() const167 bool GraphicsContext::hasShadow() const
168 {
169     return m_state.shadowColor.isValid() && m_state.shadowColor.alpha()
170            && (m_state.shadowBlur || m_state.shadowOffset.width() || m_state.shadowOffset.height());
171 }
172 
getShadow(FloatSize & offset,float & blur,Color & color,ColorSpace & colorSpace) const173 bool GraphicsContext::getShadow(FloatSize& offset, float& blur, Color& color, ColorSpace& colorSpace) const
174 {
175     offset = m_state.shadowOffset;
176     blur = m_state.shadowBlur;
177     color = m_state.shadowColor;
178     colorSpace = m_state.shadowColorSpace;
179 
180     return hasShadow();
181 }
182 
strokeThickness() const183 float GraphicsContext::strokeThickness() const
184 {
185     return m_state.strokeThickness;
186 }
187 
strokeStyle() const188 StrokeStyle GraphicsContext::strokeStyle() const
189 {
190     return m_state.strokeStyle;
191 }
192 
strokeColor() const193 Color GraphicsContext::strokeColor() const
194 {
195     return m_state.strokeColor;
196 }
197 
strokeColorSpace() const198 ColorSpace GraphicsContext::strokeColorSpace() const
199 {
200     return m_state.strokeColorSpace;
201 }
202 
fillRule() const203 WindRule GraphicsContext::fillRule() const
204 {
205     return m_state.fillRule;
206 }
207 
setFillRule(WindRule fillRule)208 void GraphicsContext::setFillRule(WindRule fillRule)
209 {
210     m_state.fillRule = fillRule;
211 }
212 
setFillColor(const Color & color,ColorSpace colorSpace)213 void GraphicsContext::setFillColor(const Color& color, ColorSpace colorSpace)
214 {
215     m_state.fillColor = color;
216     m_state.fillColorSpace = colorSpace;
217     m_state.fillGradient.clear();
218     m_state.fillPattern.clear();
219     setPlatformFillColor(color, colorSpace);
220 }
221 
fillColor() const222 Color GraphicsContext::fillColor() const
223 {
224     return m_state.fillColor;
225 }
226 
fillColorSpace() const227 ColorSpace GraphicsContext::fillColorSpace() const
228 {
229     return m_state.fillColorSpace;
230 }
231 
setShouldAntialias(bool b)232 void GraphicsContext::setShouldAntialias(bool b)
233 {
234     m_state.shouldAntialias = b;
235     setPlatformShouldAntialias(b);
236 }
237 
shouldAntialias() const238 bool GraphicsContext::shouldAntialias() const
239 {
240     return m_state.shouldAntialias;
241 }
242 
setShouldSmoothFonts(bool b)243 void GraphicsContext::setShouldSmoothFonts(bool b)
244 {
245     m_state.shouldSmoothFonts = b;
246     setPlatformShouldSmoothFonts(b);
247 }
248 
shouldSmoothFonts() const249 bool GraphicsContext::shouldSmoothFonts() const
250 {
251     return m_state.shouldSmoothFonts;
252 }
253 
state() const254 const GraphicsContextState& GraphicsContext::state() const
255 {
256     return m_state;
257 }
258 
setStrokePattern(PassRefPtr<Pattern> pattern)259 void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
260 {
261     ASSERT(pattern);
262     if (!pattern) {
263         setStrokeColor(Color::black, ColorSpaceDeviceRGB);
264         return;
265     }
266     m_state.strokeGradient.clear();
267     m_state.strokePattern = pattern;
268     setPlatformStrokePattern(m_state.strokePattern.get());
269 }
270 
setFillPattern(PassRefPtr<Pattern> pattern)271 void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
272 {
273     ASSERT(pattern);
274     if (!pattern) {
275         setFillColor(Color::black, ColorSpaceDeviceRGB);
276         return;
277     }
278     m_state.fillGradient.clear();
279     m_state.fillPattern = pattern;
280     setPlatformFillPattern(m_state.fillPattern.get());
281 }
282 
setStrokeGradient(PassRefPtr<Gradient> gradient)283 void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
284 {
285     ASSERT(gradient);
286     if (!gradient) {
287         setStrokeColor(Color::black, ColorSpaceDeviceRGB);
288         return;
289     }
290     m_state.strokeGradient = gradient;
291     m_state.strokePattern.clear();
292     setPlatformStrokeGradient(m_state.strokeGradient.get());
293 }
294 
setFillGradient(PassRefPtr<Gradient> gradient)295 void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
296 {
297     ASSERT(gradient);
298     if (!gradient) {
299         setFillColor(Color::black, ColorSpaceDeviceRGB);
300         return;
301     }
302     m_state.fillGradient = gradient;
303     m_state.fillPattern.clear();
304     setPlatformFillGradient(m_state.fillGradient.get());
305 }
306 
fillGradient() const307 Gradient* GraphicsContext::fillGradient() const
308 {
309     return m_state.fillGradient.get();
310 }
311 
strokeGradient() const312 Gradient* GraphicsContext::strokeGradient() const
313 {
314     return m_state.strokeGradient.get();
315 }
316 
fillPattern() const317 Pattern* GraphicsContext::fillPattern() const
318 {
319     return m_state.fillPattern.get();
320 }
321 
strokePattern() const322 Pattern* GraphicsContext::strokePattern() const
323 {
324     return m_state.strokePattern.get();
325 }
326 
setShadowsIgnoreTransforms(bool ignoreTransforms)327 void GraphicsContext::setShadowsIgnoreTransforms(bool ignoreTransforms)
328 {
329     m_state.shadowsIgnoreTransforms = ignoreTransforms;
330 }
331 
shadowsIgnoreTransforms() const332 bool GraphicsContext::shadowsIgnoreTransforms() const
333 {
334     return m_state.shadowsIgnoreTransforms;
335 }
336 
updatingControlTints() const337 bool GraphicsContext::updatingControlTints() const
338 {
339     return m_updatingControlTints;
340 }
341 
setUpdatingControlTints(bool b)342 void GraphicsContext::setUpdatingControlTints(bool b)
343 {
344     setPaintingDisabled(b);
345     m_updatingControlTints = b;
346 }
347 
setPaintingDisabled(bool f)348 void GraphicsContext::setPaintingDisabled(bool f)
349 {
350     m_state.paintingDisabled = f;
351 }
352 
paintingDisabled() const353 bool GraphicsContext::paintingDisabled() const
354 {
355     return m_state.paintingDisabled;
356 }
357 
drawImage(Image * image,ColorSpace styleColorSpace,const IntPoint & p,CompositeOperator op)358 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& p, CompositeOperator op)
359 {
360     drawImage(image, styleColorSpace, p, IntRect(0, 0, -1, -1), op);
361 }
362 
drawImage(Image * image,ColorSpace styleColorSpace,const IntRect & r,CompositeOperator op,bool useLowQualityScale)363 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
364 {
365     drawImage(image, styleColorSpace, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
366 }
367 
drawImage(Image * image,ColorSpace styleColorSpace,const IntPoint & dest,const IntRect & srcRect,CompositeOperator op)368 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
369 {
370     drawImage(image, styleColorSpace, IntRect(dest, srcRect.size()), srcRect, op);
371 }
372 
drawImage(Image * image,ColorSpace styleColorSpace,const IntRect & dest,const IntRect & srcRect,CompositeOperator op,bool useLowQualityScale)373 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
374 {
375     drawImage(image, styleColorSpace, FloatRect(dest), srcRect, op, useLowQualityScale);
376 }
377 
378 #if !OS(WINCE) || (PLATFORM(QT) && !HAVE(QRAWFONT))
drawText(const Font & font,const TextRun & run,const FloatPoint & point,int from,int to)379 void GraphicsContext::drawText(const Font& font, const TextRun& run, const FloatPoint& point, int from, int to)
380 {
381     if (paintingDisabled())
382         return;
383 
384     font.drawText(this, run, point, from, to);
385 }
386 #endif
387 
drawEmphasisMarks(const Font & font,const TextRun & run,const AtomicString & mark,const FloatPoint & point,int from,int to)388 void GraphicsContext::drawEmphasisMarks(const Font& font, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to)
389 {
390     if (paintingDisabled())
391         return;
392 
393     font.drawEmphasisMarks(this, run, mark, point, from, to);
394 }
395 
drawBidiText(const Font & font,const TextRun & run,const FloatPoint & point)396 void GraphicsContext::drawBidiText(const Font& font, const TextRun& run, const FloatPoint& point)
397 {
398     if (paintingDisabled())
399         return;
400 
401     BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
402     bidiResolver.setStatus(BidiStatus(run.direction(), run.directionalOverride()));
403     bidiResolver.setPosition(TextRunIterator(&run, 0));
404 
405     // FIXME: This ownership should be reversed. We should pass BidiRunList
406     // to BidiResolver in createBidiRunsForLine.
407     BidiRunList<BidiCharacterRun>& bidiRuns = bidiResolver.runs();
408     bidiResolver.createBidiRunsForLine(TextRunIterator(&run, run.length()));
409     if (!bidiRuns.runCount())
410         return;
411 
412     FloatPoint currPoint = point;
413     BidiCharacterRun* bidiRun = bidiRuns.firstRun();
414     while (bidiRun) {
415         TextRun subrun = run;
416         subrun.setText(run.data(bidiRun->start()), bidiRun->stop() - bidiRun->start());
417         bool isRTL = bidiRun->level() % 2;
418         subrun.setDirection(isRTL ? RTL : LTR);
419         subrun.setDirectionalOverride(bidiRun->dirOverride(false));
420 
421         font.drawText(this, subrun, currPoint);
422 
423         bidiRun = bidiRun->next();
424         // FIXME: Have Font::drawText return the width of what it drew so that we don't have to re-measure here.
425         if (bidiRun)
426             currPoint.move(font.width(subrun), 0);
427     }
428 
429     bidiRuns.deleteRuns();
430 }
431 
drawHighlightForText(const Font & font,const TextRun & run,const FloatPoint & point,int h,const Color & backgroundColor,ColorSpace colorSpace,int from,int to)432 void GraphicsContext::drawHighlightForText(const Font& font, const TextRun& run, const FloatPoint& point, int h, const Color& backgroundColor, ColorSpace colorSpace, int from, int to)
433 {
434     if (paintingDisabled())
435         return;
436 
437     fillRect(font.selectionRectForText(run, point, h, from, to), backgroundColor, colorSpace);
438 }
439 
drawImage(Image * image,ColorSpace styleColorSpace,const FloatRect & dest,const FloatRect & src,CompositeOperator op,bool useLowQualityScale)440 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
441 {
442     if (paintingDisabled() || !image)
443         return;
444 
445     float tsw = src.width();
446     float tsh = src.height();
447     float tw = dest.width();
448     float th = dest.height();
449 
450     if (tsw == -1)
451         tsw = image->width();
452     if (tsh == -1)
453         tsh = image->height();
454 
455     if (tw == -1)
456         tw = image->width();
457     if (th == -1)
458         th = image->height();
459 
460     if (useLowQualityScale) {
461         InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
462         // FIXME: Should be InterpolationLow
463         setImageInterpolationQuality(InterpolationNone);
464         image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), styleColorSpace, op);
465         setImageInterpolationQuality(previousInterpolationQuality);
466     } else
467         image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), styleColorSpace, op);
468 }
469 
drawTiledImage(Image * image,ColorSpace styleColorSpace,const IntRect & rect,const IntPoint & srcPoint,const IntSize & tileSize,CompositeOperator op,bool useLowQualityScale)470 void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& rect, const IntPoint& srcPoint, const IntSize& tileSize, CompositeOperator op, bool useLowQualityScale)
471 {
472     if (paintingDisabled() || !image)
473         return;
474 
475     if (useLowQualityScale) {
476         InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
477         setImageInterpolationQuality(InterpolationLow);
478         image->drawTiled(this, rect, srcPoint, tileSize, styleColorSpace, op);
479         setImageInterpolationQuality(previousInterpolationQuality);
480     } else
481         image->drawTiled(this, rect, srcPoint, tileSize, styleColorSpace, op);
482 }
483 
drawTiledImage(Image * image,ColorSpace styleColorSpace,const IntRect & dest,const IntRect & srcRect,Image::TileRule hRule,Image::TileRule vRule,CompositeOperator op,bool useLowQualityScale)484 void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, Image::TileRule hRule, Image::TileRule vRule, CompositeOperator op, bool useLowQualityScale)
485 {
486     if (paintingDisabled() || !image)
487         return;
488 
489     if (hRule == Image::StretchTile && vRule == Image::StretchTile) {
490         // Just do a scale.
491         drawImage(image, styleColorSpace, dest, srcRect, op);
492         return;
493     }
494 
495     if (useLowQualityScale) {
496         InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
497         setImageInterpolationQuality(InterpolationLow);
498         image->drawTiled(this, dest, srcRect, hRule, vRule, styleColorSpace, op);
499         setImageInterpolationQuality(previousInterpolationQuality);
500     } else
501         image->drawTiled(this, dest, srcRect, hRule, vRule, styleColorSpace, op);
502 }
503 
drawImageBuffer(ImageBuffer * image,ColorSpace styleColorSpace,const IntPoint & p,CompositeOperator op)504 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntPoint& p, CompositeOperator op)
505 {
506     drawImageBuffer(image, styleColorSpace, p, IntRect(0, 0, -1, -1), op);
507 }
508 
drawImageBuffer(ImageBuffer * image,ColorSpace styleColorSpace,const IntRect & r,CompositeOperator op,bool useLowQualityScale)509 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
510 {
511     drawImageBuffer(image, styleColorSpace, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
512 }
513 
drawImageBuffer(ImageBuffer * image,ColorSpace styleColorSpace,const IntPoint & dest,const IntRect & srcRect,CompositeOperator op)514 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
515 {
516     drawImageBuffer(image, styleColorSpace, IntRect(dest, srcRect.size()), srcRect, op);
517 }
518 
drawImageBuffer(ImageBuffer * image,ColorSpace styleColorSpace,const IntRect & dest,const IntRect & srcRect,CompositeOperator op,bool useLowQualityScale)519 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
520 {
521     drawImageBuffer(image, styleColorSpace, FloatRect(dest), srcRect, op, useLowQualityScale);
522 }
523 
drawImageBuffer(ImageBuffer * image,ColorSpace styleColorSpace,const FloatRect & dest,const FloatRect & src,CompositeOperator op,bool useLowQualityScale)524 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
525 {
526     if (paintingDisabled() || !image)
527         return;
528 
529     float tsw = src.width();
530     float tsh = src.height();
531     float tw = dest.width();
532     float th = dest.height();
533 
534     if (tsw == -1)
535         tsw = image->width();
536     if (tsh == -1)
537         tsh = image->height();
538 
539     if (tw == -1)
540         tw = image->width();
541     if (th == -1)
542         th = image->height();
543 
544     if (useLowQualityScale) {
545         InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
546         // FIXME: Should be InterpolationLow
547         setImageInterpolationQuality(InterpolationNone);
548         image->draw(this, styleColorSpace, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op, useLowQualityScale);
549         setImageInterpolationQuality(previousInterpolationQuality);
550     } else
551         image->draw(this, styleColorSpace, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op, useLowQualityScale);
552 }
553 
554 #if !PLATFORM(QT)
clip(const IntRect & rect)555 void GraphicsContext::clip(const IntRect& rect)
556 {
557     clip(FloatRect(rect));
558 }
559 #endif
560 
addRoundedRectClip(const RoundedIntRect & rect)561 void GraphicsContext::addRoundedRectClip(const RoundedIntRect& rect)
562 {
563     if (paintingDisabled())
564         return;
565 
566     Path path;
567     path.addRoundedRect(rect);
568     clip(path);
569 }
570 
clipOutRoundedRect(const RoundedIntRect & rect)571 void GraphicsContext::clipOutRoundedRect(const RoundedIntRect& rect)
572 {
573     if (paintingDisabled())
574         return;
575 
576     Path path;
577     path.addRoundedRect(rect);
578     clipOut(path);
579 }
580 
clipToImageBuffer(ImageBuffer * buffer,const FloatRect & rect)581 void GraphicsContext::clipToImageBuffer(ImageBuffer* buffer, const FloatRect& rect)
582 {
583     if (paintingDisabled())
584         return;
585     buffer->clip(this, rect);
586 }
587 
588 #if !USE(CG)
clipBounds() const589 IntRect GraphicsContext::clipBounds() const
590 {
591     ASSERT_NOT_REACHED();
592     return IntRect();
593 }
594 #endif
595 
textDrawingMode() const596 TextDrawingModeFlags GraphicsContext::textDrawingMode() const
597 {
598     return m_state.textDrawingMode;
599 }
600 
setTextDrawingMode(TextDrawingModeFlags mode)601 void GraphicsContext::setTextDrawingMode(TextDrawingModeFlags mode)
602 {
603     m_state.textDrawingMode = mode;
604     if (paintingDisabled())
605         return;
606     setPlatformTextDrawingMode(mode);
607 }
608 
fillRect(const FloatRect & rect,Generator & generator)609 void GraphicsContext::fillRect(const FloatRect& rect, Generator& generator)
610 {
611     if (paintingDisabled())
612         return;
613     generator.fill(this, rect);
614 }
615 
fillRoundedRect(const RoundedIntRect & rect,const Color & color,ColorSpace colorSpace)616 void GraphicsContext::fillRoundedRect(const RoundedIntRect& rect, const Color& color, ColorSpace colorSpace)
617 {
618     fillRoundedRect(rect.rect(), rect.radii().topLeft(), rect.radii().topRight(), rect.radii().bottomLeft(), rect.radii().bottomRight(), color, colorSpace);
619 }
620 
621 #if !USE(CG)
fillRectWithRoundedHole(const IntRect & rect,const RoundedIntRect & roundedHoleRect,const Color & color,ColorSpace colorSpace)622 void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const RoundedIntRect& roundedHoleRect, const Color& color, ColorSpace colorSpace)
623 {
624     if (paintingDisabled())
625         return;
626 
627     Path path;
628     path.addRect(rect);
629 
630     if (!roundedHoleRect.radii().isZero())
631         path.addRoundedRect(roundedHoleRect);
632     else
633         path.addRect(roundedHoleRect.rect());
634 
635     WindRule oldFillRule = fillRule();
636     Color oldFillColor = fillColor();
637     ColorSpace oldFillColorSpace = fillColorSpace();
638 
639     setFillRule(RULE_EVENODD);
640     setFillColor(color, colorSpace);
641 
642     fillPath(path);
643 
644     setFillRule(oldFillRule);
645     setFillColor(oldFillColor, oldFillColorSpace);
646 }
647 #endif
648 
setCompositeOperation(CompositeOperator compositeOperation)649 void GraphicsContext::setCompositeOperation(CompositeOperator compositeOperation)
650 {
651     m_state.compositeOperator = compositeOperation;
652     setPlatformCompositeOperation(compositeOperation);
653 }
654 
compositeOperation() const655 CompositeOperator GraphicsContext::compositeOperation() const
656 {
657     return m_state.compositeOperator;
658 }
659 
660 #if !USE(SKIA)
setPlatformFillGradient(Gradient *)661 void GraphicsContext::setPlatformFillGradient(Gradient*)
662 {
663 }
664 
setPlatformFillPattern(Pattern *)665 void GraphicsContext::setPlatformFillPattern(Pattern*)
666 {
667 }
668 
setPlatformStrokeGradient(Gradient *)669 void GraphicsContext::setPlatformStrokeGradient(Gradient*)
670 {
671 }
672 
setPlatformStrokePattern(Pattern *)673 void GraphicsContext::setPlatformStrokePattern(Pattern*)
674 {
675 }
676 #endif
677 
678 #if !USE(CG) && !USE(SKIA)
679 // Implement this if you want to go ahead and push the drawing mode into your native context
680 // immediately.
setPlatformTextDrawingMode(TextDrawingModeFlags mode)681 void GraphicsContext::setPlatformTextDrawingMode(TextDrawingModeFlags mode)
682 {
683 }
684 #endif
685 
686 #if !PLATFORM(QT) && !USE(CAIRO) && !USE(SKIA) && !PLATFORM(HAIKU) && !PLATFORM(OPENVG)
setPlatformStrokeStyle(StrokeStyle)687 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle)
688 {
689 }
690 #endif
691 
692 #if !USE(CG)
setPlatformShouldSmoothFonts(bool)693 void GraphicsContext::setPlatformShouldSmoothFonts(bool)
694 {
695 }
696 #endif
697 
698 #if !USE(SKIA)
setSharedGraphicsContext3D(SharedGraphicsContext3D *,DrawingBuffer *,const IntSize &)699 void GraphicsContext::setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&)
700 {
701 }
702 
syncSoftwareCanvas()703 void GraphicsContext::syncSoftwareCanvas()
704 {
705 }
706 
markDirtyRect(const IntRect &)707 void GraphicsContext::markDirtyRect(const IntRect&)
708 {
709 }
710 #endif
711 
712 
adjustLineToPixelBoundaries(FloatPoint & p1,FloatPoint & p2,float strokeWidth,StrokeStyle penStyle)713 void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle penStyle)
714 {
715     // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
716     // works out.  For example, with a border width of 3, WebKit will pass us (y1+y2)/2, e.g.,
717     // (50+53)/2 = 103/2 = 51 when we want 51.5.  It is always true that an even width gave
718     // us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
719     if (penStyle == DottedStroke || penStyle == DashedStroke) {
720         if (p1.x() == p2.x()) {
721             p1.setY(p1.y() + strokeWidth);
722             p2.setY(p2.y() - strokeWidth);
723         } else {
724             p1.setX(p1.x() + strokeWidth);
725             p2.setX(p2.x() - strokeWidth);
726         }
727     }
728 
729     if (static_cast<int>(strokeWidth) % 2) { //odd
730         if (p1.x() == p2.x()) {
731             // We're a vertical line.  Adjust our x.
732             p1.setX(p1.x() + 0.5f);
733             p2.setX(p2.x() + 0.5f);
734         } else {
735             // We're a horizontal line. Adjust our y.
736             p1.setY(p1.y() + 0.5f);
737             p2.setY(p2.y() + 0.5f);
738         }
739     }
740 }
741 
742 }
743