2 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "GraphicsContext.h"
29 #include "BidiResolver.h"
31 #include "Generator.h"
32 #include "ImageBuffer.h"
34 #include "RoundedIntRect.h"
41 class TextRunIterator {
49 TextRunIterator(const TextRun* textRun, unsigned offset)
55 TextRunIterator(const TextRunIterator& other)
56 : m_textRun(other.m_textRun)
57 , m_offset(other.m_offset)
61 unsigned offset() const { return m_offset; }
62 void increment() { m_offset++; }
63 bool atEnd() const { return !m_textRun || m_offset >= m_textRun->length(); }
64 UChar current() const { return (*m_textRun)[m_offset]; }
65 WTF::Unicode::Direction direction() const { return atEnd() ? WTF::Unicode::OtherNeutral : WTF::Unicode::direction(current()); }
67 bool operator==(const TextRunIterator& other)
69 return m_offset == other.m_offset && m_textRun == other.m_textRun;
72 bool operator!=(const TextRunIterator& other) { return !operator==(other); }
75 const TextRun* m_textRun;
79 GraphicsContext::GraphicsContext(PlatformGraphicsContext* platformGraphicsContext)
80 : m_updatingControlTints(false)
82 platformInit(platformGraphicsContext);
85 GraphicsContext::~GraphicsContext()
90 void GraphicsContext::save()
92 if (paintingDisabled())
95 m_stack.append(m_state);
100 void GraphicsContext::restore()
102 if (paintingDisabled())
105 if (m_stack.isEmpty()) {
106 LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
109 m_state = m_stack.last();
110 m_stack.removeLast();
112 restorePlatformState();
115 void GraphicsContext::setStrokeThickness(float thickness)
117 m_state.strokeThickness = thickness;
118 setPlatformStrokeThickness(thickness);
121 void GraphicsContext::setStrokeStyle(StrokeStyle style)
123 m_state.strokeStyle = style;
124 setPlatformStrokeStyle(style);
127 void GraphicsContext::setStrokeColor(const Color& color, ColorSpace colorSpace)
129 m_state.strokeColor = color;
130 m_state.strokeColorSpace = colorSpace;
131 m_state.strokeGradient.clear();
132 m_state.strokePattern.clear();
133 setPlatformStrokeColor(color, colorSpace);
136 void GraphicsContext::setShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
138 m_state.shadowOffset = offset;
139 m_state.shadowBlur = blur;
140 m_state.shadowColor = color;
141 m_state.shadowColorSpace = colorSpace;
142 setPlatformShadow(offset, blur, color, colorSpace);
145 void GraphicsContext::setLegacyShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
147 m_state.shadowOffset = offset;
148 m_state.shadowBlur = blur;
149 m_state.shadowColor = color;
150 m_state.shadowColorSpace = colorSpace;
152 m_state.shadowsUseLegacyRadius = true;
154 setPlatformShadow(offset, blur, color, colorSpace);
157 void GraphicsContext::clearShadow()
159 m_state.shadowOffset = FloatSize();
160 m_state.shadowBlur = 0;
161 m_state.shadowColor = Color();
162 m_state.shadowColorSpace = ColorSpaceDeviceRGB;
163 clearPlatformShadow();
166 bool GraphicsContext::hasShadow() const
168 return m_state.shadowColor.isValid() && m_state.shadowColor.alpha()
169 && (m_state.shadowBlur || m_state.shadowOffset.width() || m_state.shadowOffset.height());
172 bool GraphicsContext::getShadow(FloatSize& offset, float& blur, Color& color, ColorSpace& colorSpace) const
174 offset = m_state.shadowOffset;
175 blur = m_state.shadowBlur;
176 color = m_state.shadowColor;
177 colorSpace = m_state.shadowColorSpace;
182 float GraphicsContext::strokeThickness() const
184 return m_state.strokeThickness;
187 StrokeStyle GraphicsContext::strokeStyle() const
189 return m_state.strokeStyle;
192 Color GraphicsContext::strokeColor() const
194 return m_state.strokeColor;
197 ColorSpace GraphicsContext::strokeColorSpace() const
199 return m_state.strokeColorSpace;
202 WindRule GraphicsContext::fillRule() const
204 return m_state.fillRule;
207 void GraphicsContext::setFillRule(WindRule fillRule)
209 m_state.fillRule = fillRule;
212 void GraphicsContext::setFillColor(const Color& color, ColorSpace colorSpace)
214 m_state.fillColor = color;
215 m_state.fillColorSpace = colorSpace;
216 m_state.fillGradient.clear();
217 m_state.fillPattern.clear();
218 setPlatformFillColor(color, colorSpace);
221 Color GraphicsContext::fillColor() const
223 return m_state.fillColor;
226 ColorSpace GraphicsContext::fillColorSpace() const
228 return m_state.fillColorSpace;
231 void GraphicsContext::setShouldAntialias(bool b)
233 m_state.shouldAntialias = b;
234 setPlatformShouldAntialias(b);
237 bool GraphicsContext::shouldAntialias() const
239 return m_state.shouldAntialias;
242 void GraphicsContext::setShouldSmoothFonts(bool b)
244 m_state.shouldSmoothFonts = b;
245 setPlatformShouldSmoothFonts(b);
248 bool GraphicsContext::shouldSmoothFonts() const
250 return m_state.shouldSmoothFonts;
253 const GraphicsContextState& GraphicsContext::state() const
258 void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
262 setStrokeColor(Color::black, ColorSpaceDeviceRGB);
265 m_state.strokeGradient.clear();
266 m_state.strokePattern = pattern;
267 setPlatformStrokePattern(m_state.strokePattern.get());
270 void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
274 setFillColor(Color::black, ColorSpaceDeviceRGB);
277 m_state.fillGradient.clear();
278 m_state.fillPattern = pattern;
279 setPlatformFillPattern(m_state.fillPattern.get());
282 void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
286 setStrokeColor(Color::black, ColorSpaceDeviceRGB);
289 m_state.strokeGradient = gradient;
290 m_state.strokePattern.clear();
291 setPlatformStrokeGradient(m_state.strokeGradient.get());
294 void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
298 setFillColor(Color::black, ColorSpaceDeviceRGB);
301 m_state.fillGradient = gradient;
302 m_state.fillPattern.clear();
303 setPlatformFillGradient(m_state.fillGradient.get());
306 Gradient* GraphicsContext::fillGradient() const
308 return m_state.fillGradient.get();
311 Gradient* GraphicsContext::strokeGradient() const
313 return m_state.strokeGradient.get();
316 Pattern* GraphicsContext::fillPattern() const
318 return m_state.fillPattern.get();
321 Pattern* GraphicsContext::strokePattern() const
323 return m_state.strokePattern.get();
326 void GraphicsContext::setShadowsIgnoreTransforms(bool ignoreTransforms)
328 m_state.shadowsIgnoreTransforms = ignoreTransforms;
331 bool GraphicsContext::shadowsIgnoreTransforms() const
333 return m_state.shadowsIgnoreTransforms;
336 bool GraphicsContext::updatingControlTints() const
338 return m_updatingControlTints;
341 void GraphicsContext::setUpdatingControlTints(bool b)
343 setPaintingDisabled(b);
344 m_updatingControlTints = b;
347 void GraphicsContext::setPaintingDisabled(bool f)
349 m_state.paintingDisabled = f;
352 bool GraphicsContext::paintingDisabled() const
354 return m_state.paintingDisabled;
357 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& p, CompositeOperator op)
359 drawImage(image, styleColorSpace, p, IntRect(0, 0, -1, -1), op);
362 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
364 drawImage(image, styleColorSpace, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
367 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
369 drawImage(image, styleColorSpace, IntRect(dest, srcRect.size()), srcRect, op);
372 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
374 drawImage(image, styleColorSpace, FloatRect(dest), srcRect, op, useLowQualityScale);
377 #if !OS(WINCE) || PLATFORM(QT)
378 void GraphicsContext::drawText(const Font& font, const TextRun& run, const FloatPoint& point, int from, int to)
380 if (paintingDisabled())
383 font.drawText(this, run, point, from, to);
387 void GraphicsContext::drawEmphasisMarks(const Font& font, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to)
389 if (paintingDisabled())
392 font.drawEmphasisMarks(this, run, mark, point, from, to);
395 void GraphicsContext::drawBidiText(const Font& font, const TextRun& run, const FloatPoint& point)
397 if (paintingDisabled())
400 // FIXME: This ownership should be reversed. We should pass BidiRunList
401 // to BidiResolver in createBidiRunsForLine.
402 BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
403 BidiRunList<BidiCharacterRun>& bidiRuns = bidiResolver.runs();
405 WTF::Unicode::Direction paragraphDirection = run.ltr() ? WTF::Unicode::LeftToRight : WTF::Unicode::RightToLeft;
407 bidiResolver.setStatus(BidiStatus(paragraphDirection, paragraphDirection, paragraphDirection, BidiContext::create(run.ltr() ? 0 : 1, paragraphDirection, run.directionalOverride())));
409 bidiResolver.setPosition(TextRunIterator(&run, 0));
410 bidiResolver.createBidiRunsForLine(TextRunIterator(&run, run.length()));
412 if (!bidiRuns.runCount())
415 FloatPoint currPoint = point;
416 BidiCharacterRun* bidiRun = bidiRuns.firstRun();
419 TextRun subrun = run;
420 subrun.setText(run.data(bidiRun->start()), bidiRun->stop() - bidiRun->start());
421 subrun.setRTL(bidiRun->level() % 2);
422 subrun.setDirectionalOverride(bidiRun->dirOverride(false));
424 font.drawText(this, subrun, currPoint);
426 bidiRun = bidiRun->next();
427 // FIXME: Have Font::drawText return the width of what it drew so that we don't have to re-measure here.
429 currPoint.move(font.width(subrun), 0);
432 bidiRuns.deleteRuns();
435 void GraphicsContext::drawHighlightForText(const Font& font, const TextRun& run, const FloatPoint& point, int h, const Color& backgroundColor, ColorSpace colorSpace, int from, int to)
437 if (paintingDisabled())
440 fillRect(font.selectionRectForText(run, point, h, from, to), backgroundColor, colorSpace);
443 void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
445 if (paintingDisabled() || !image)
448 float tsw = src.width();
449 float tsh = src.height();
450 float tw = dest.width();
451 float th = dest.height();
454 tsw = image->width();
456 tsh = image->height();
461 th = image->height();
463 if (useLowQualityScale) {
464 InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
465 // FIXME: Should be InterpolationLow
466 setImageInterpolationQuality(InterpolationNone);
467 image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), styleColorSpace, op);
468 setImageInterpolationQuality(previousInterpolationQuality);
470 image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), styleColorSpace, op);
473 void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& rect, const IntPoint& srcPoint, const IntSize& tileSize, CompositeOperator op, bool useLowQualityScale)
475 if (paintingDisabled() || !image)
478 if (useLowQualityScale) {
479 InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
480 setImageInterpolationQuality(InterpolationLow);
481 image->drawTiled(this, rect, srcPoint, tileSize, styleColorSpace, op);
482 setImageInterpolationQuality(previousInterpolationQuality);
484 image->drawTiled(this, rect, srcPoint, tileSize, styleColorSpace, op);
487 void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, Image::TileRule hRule, Image::TileRule vRule, CompositeOperator op, bool useLowQualityScale)
489 if (paintingDisabled() || !image)
492 if (hRule == Image::StretchTile && vRule == Image::StretchTile) {
494 drawImage(image, styleColorSpace, dest, srcRect, op);
498 if (useLowQualityScale) {
499 InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
500 setImageInterpolationQuality(InterpolationLow);
501 image->drawTiled(this, dest, srcRect, hRule, vRule, styleColorSpace, op);
502 setImageInterpolationQuality(previousInterpolationQuality);
504 image->drawTiled(this, dest, srcRect, hRule, vRule, styleColorSpace, op);
507 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntPoint& p, CompositeOperator op)
509 drawImageBuffer(image, styleColorSpace, p, IntRect(0, 0, -1, -1), op);
512 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
514 drawImageBuffer(image, styleColorSpace, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
517 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
519 drawImageBuffer(image, styleColorSpace, IntRect(dest, srcRect.size()), srcRect, op);
522 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
524 drawImageBuffer(image, styleColorSpace, FloatRect(dest), srcRect, op, useLowQualityScale);
527 void GraphicsContext::drawImageBuffer(ImageBuffer* image, ColorSpace styleColorSpace, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
529 if (paintingDisabled() || !image)
532 float tsw = src.width();
533 float tsh = src.height();
534 float tw = dest.width();
535 float th = dest.height();
538 tsw = image->width();
540 tsh = image->height();
545 th = image->height();
547 if (useLowQualityScale) {
548 InterpolationQuality previousInterpolationQuality = imageInterpolationQuality();
549 // FIXME: Should be InterpolationLow
550 setImageInterpolationQuality(InterpolationNone);
551 image->draw(this, styleColorSpace, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op, useLowQualityScale);
552 setImageInterpolationQuality(previousInterpolationQuality);
554 image->draw(this, styleColorSpace, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op, useLowQualityScale);
558 void GraphicsContext::clip(const IntRect& rect)
560 clip(FloatRect(rect));
564 void GraphicsContext::addRoundedRectClip(const RoundedIntRect& rect)
566 if (paintingDisabled())
570 path.addRoundedRect(rect);
574 void GraphicsContext::clipOutRoundedRect(const RoundedIntRect& rect)
576 if (paintingDisabled())
580 path.addRoundedRect(rect);
584 void GraphicsContext::clipToImageBuffer(ImageBuffer* buffer, const FloatRect& rect)
586 if (paintingDisabled())
588 buffer->clip(this, rect);
592 IntRect GraphicsContext::clipBounds() const
594 ASSERT_NOT_REACHED();
599 TextDrawingModeFlags GraphicsContext::textDrawingMode() const
601 return m_state.textDrawingMode;
604 void GraphicsContext::setTextDrawingMode(TextDrawingModeFlags mode)
606 m_state.textDrawingMode = mode;
607 if (paintingDisabled())
609 setPlatformTextDrawingMode(mode);
612 void GraphicsContext::fillRect(const FloatRect& rect, Generator& generator)
614 if (paintingDisabled())
616 generator.fill(this, rect);
619 void GraphicsContext::fillRoundedRect(const RoundedIntRect& rect, const Color& color, ColorSpace colorSpace)
621 fillRoundedRect(rect.rect(), rect.radii().topLeft(), rect.radii().topRight(), rect.radii().bottomLeft(), rect.radii().bottomRight(), color, colorSpace);
625 void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const RoundedIntRect& roundedHoleRect, const Color& color, ColorSpace colorSpace)
627 if (paintingDisabled())
633 if (!roundedHoleRect.radii().isZero())
634 path.addRoundedRect(roundedHoleRect);
636 path.addRect(roundedHoleRect.rect());
638 WindRule oldFillRule = fillRule();
639 Color oldFillColor = fillColor();
640 ColorSpace oldFillColorSpace = fillColorSpace();
642 setFillRule(RULE_EVENODD);
643 setFillColor(color, colorSpace);
647 setFillRule(oldFillRule);
648 setFillColor(oldFillColor, oldFillColorSpace);
652 void GraphicsContext::setCompositeOperation(CompositeOperator compositeOperation)
654 m_state.compositeOperator = compositeOperation;
655 setPlatformCompositeOperation(compositeOperation);
658 CompositeOperator GraphicsContext::compositeOperation() const
660 return m_state.compositeOperator;
663 #if !(USE(SKIA) && !PLATFORM(ANDROID))
664 void GraphicsContext::setPlatformFillGradient(Gradient*)
668 void GraphicsContext::setPlatformFillPattern(Pattern*)
672 void GraphicsContext::setPlatformStrokeGradient(Gradient*)
676 void GraphicsContext::setPlatformStrokePattern(Pattern*)
681 #if !USE(CG) && !(USE(SKIA) && !PLATFORM(ANDROID))
682 // Implement this if you want to go ahead and push the drawing mode into your native context
684 void GraphicsContext::setPlatformTextDrawingMode(TextDrawingModeFlags mode)
689 #if !PLATFORM(QT) && !USE(CAIRO) && !(USE(SKIA) && !PLATFORM(ANDROID)) && !PLATFORM(HAIKU) && !PLATFORM(OPENVG)
690 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle)
696 void GraphicsContext::setPlatformShouldSmoothFonts(bool)
702 void GraphicsContext::setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&)
706 void GraphicsContext::syncSoftwareCanvas()
710 void GraphicsContext::markDirtyRect(const IntRect&)
716 void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle penStyle)
718 // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
719 // works out. For example, with a border width of 3, WebKit will pass us (y1+y2)/2, e.g.,
720 // (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave
721 // us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
722 if (penStyle == DottedStroke || penStyle == DashedStroke) {
723 if (p1.x() == p2.x()) {
724 p1.setY(p1.y() + strokeWidth);
725 p2.setY(p2.y() - strokeWidth);
727 p1.setX(p1.x() + strokeWidth);
728 p2.setX(p2.x() - strokeWidth);
732 if (static_cast<int>(strokeWidth) % 2) { //odd
733 if (p1.x() == p2.x()) {
734 // We're a vertical line. Adjust our x.
735 p1.setX(p1.x() + 0.5f);
736 p2.setX(p2.x() + 0.5f);
738 // We're a horizontal line. Adjust our y.
739 p1.setY(p1.y() + 0.5f);
740 p2.setY(p2.y() + 0.5f);