2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "GraphicsContext.h"
29 #include "DeprecatedString.h"
36 struct GraphicsContextState {
37 GraphicsContextState() : fillColor(Color::black), paintingDisabled(false) { }
41 bool paintingDisabled;
44 class GraphicsContextPrivate {
46 GraphicsContextPrivate();
48 GraphicsContextState state;
49 Vector<GraphicsContextState> stack;
50 Vector<IntRect> m_focusRingRects;
52 int m_focusRingOffset;
53 bool m_usesInactiveTextBackgroundColor;
54 bool m_updatingControlTints;
57 GraphicsContextPrivate::GraphicsContextPrivate()
59 , m_focusRingOffset(0)
60 , m_usesInactiveTextBackgroundColor(false)
61 , m_updatingControlTints(false)
65 GraphicsContextPrivate* GraphicsContext::createGraphicsContextPrivate()
67 return new GraphicsContextPrivate;
70 void GraphicsContext::destroyGraphicsContextPrivate(GraphicsContextPrivate* deleteMe)
75 void GraphicsContext::save()
77 if (paintingDisabled())
80 m_common->stack.append(m_common->state);
85 void GraphicsContext::restore()
87 if (paintingDisabled())
90 if (m_common->stack.isEmpty()) {
91 LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
94 m_common->state = m_common->stack.last();
95 m_common->stack.removeLast();
97 restorePlatformState();
100 const Font& GraphicsContext::font() const
102 return m_common->state.font;
105 void GraphicsContext::setFont(const Font& aFont)
107 m_common->state.font = aFont;
110 const Pen& GraphicsContext::pen() const
112 return m_common->state.pen;
115 void GraphicsContext::setPen(const Pen& pen)
117 m_common->state.pen = pen;
120 void GraphicsContext::setPen(Pen::PenStyle style)
122 m_common->state.pen.setStyle(style);
123 m_common->state.pen.setColor(Color::black);
124 m_common->state.pen.setWidth(0);
127 void GraphicsContext::setPen(RGBA32 rgb)
129 m_common->state.pen.setStyle(Pen::SolidLine);
130 m_common->state.pen.setColor(rgb);
131 m_common->state.pen.setWidth(0);
134 void GraphicsContext::setFillColor(const Color& color)
136 m_common->state.fillColor = color;
139 Color GraphicsContext::fillColor() const
141 return m_common->state.fillColor;
144 void GraphicsContext::setUsesInactiveTextBackgroundColor(bool u)
146 m_common->m_usesInactiveTextBackgroundColor = u;
149 bool GraphicsContext::usesInactiveTextBackgroundColor() const
151 return m_common->m_usesInactiveTextBackgroundColor;
154 bool GraphicsContext::updatingControlTints() const
156 return m_common->m_updatingControlTints;
159 void GraphicsContext::setUpdatingControlTints(bool b)
161 setPaintingDisabled(b);
162 m_common->m_updatingControlTints = b;
165 void GraphicsContext::setPaintingDisabled(bool f)
167 m_common->state.paintingDisabled = f;
170 bool GraphicsContext::paintingDisabled() const
172 return m_common->state.paintingDisabled;
175 void GraphicsContext::drawImage(Image* image, const IntPoint& p, CompositeOperator op)
177 drawImage(image, p, IntRect(0, 0, -1, -1), op);
180 void GraphicsContext::drawImage(Image* image, const IntRect& r, CompositeOperator op)
182 drawImage(image, r, IntRect(0, 0, -1, -1), op);
185 void GraphicsContext::drawImage(Image* image, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
187 drawImage(image, IntRect(dest, srcRect.size()), srcRect, op);
190 void GraphicsContext::drawImage(Image* image, const IntRect& dest, const IntRect& srcRect, CompositeOperator op)
192 drawImage(image, FloatRect(dest), srcRect, op);
195 void GraphicsContext::drawText(const TextRun& run, const IntPoint& point, int tabWidth, int xpos, int toAdd,
196 TextDirection d, bool visuallyOrdered)
198 if (paintingDisabled())
201 font().drawText(this, run, point, tabWidth, xpos, toAdd, d, visuallyOrdered);
204 void GraphicsContext::drawHighlightForText(const TextRun& run, const IntPoint& point, int h, int tabWidth, int xpos, int toAdd,
205 TextDirection d, bool visuallyOrdered, const Color& backgroundColor)
207 if (paintingDisabled())
210 return font().drawHighlightForText(this, run, point, h, tabWidth, xpos, toAdd, d, visuallyOrdered, backgroundColor);
213 void GraphicsContext::drawLineForText(const IntPoint& point, int yOffset, int width)
215 if (paintingDisabled())
218 return font().drawLineForText(this, point, yOffset, width);
222 void GraphicsContext::drawLineForMisspelling(const IntPoint& point, int width)
224 if (paintingDisabled())
227 return font().drawLineForMisspelling(this, point, width);
230 int GraphicsContext::misspellingLineThickness()
232 return font().misspellingLineThickness(this);
235 void GraphicsContext::initFocusRing(int width, int offset)
237 if (paintingDisabled())
241 m_common->m_focusRingWidth = width;
242 m_common->m_focusRingOffset = offset;
245 void GraphicsContext::clearFocusRing()
247 m_common->m_focusRingRects.clear();
250 void GraphicsContext::addFocusRingRect(const IntRect& rect)
252 if (paintingDisabled() || rect.isEmpty())
254 m_common->m_focusRingRects.append(rect);
257 int GraphicsContext::focusRingWidth() const
259 return m_common->m_focusRingWidth;
262 int GraphicsContext::focusRingOffset() const
264 return m_common->m_focusRingOffset;
267 const Vector<IntRect>& GraphicsContext::focusRingRects() const
269 return m_common->m_focusRingRects;
272 void GraphicsContext::drawImage(Image* image, const FloatRect& dest, const FloatRect& src, CompositeOperator op)
274 if (paintingDisabled())
277 float tsw = src.width();
278 float tsh = src.height();
279 float tw = dest.width();
280 float th = dest.height();
283 tsw = image->width();
285 tsh = image->height();
290 th = image->height();
292 image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op);
295 void GraphicsContext::drawTiledImage(Image* image, const IntRect& rect, const IntPoint& srcPoint, const IntSize& tileSize)
297 if (paintingDisabled())
300 image->drawTiled(this, rect, srcPoint, tileSize);
303 void GraphicsContext::drawTiledImage(Image* image, const IntRect& dest, const IntRect& srcRect, Image::TileRule hRule, Image::TileRule vRule)
305 if (paintingDisabled())
308 if (hRule == Image::StretchTile && vRule == Image::StretchTile)
310 return drawImage(image, dest, srcRect);
312 image->drawTiled(this, dest, srcRect, hRule, vRule);