2 * Copyright (C) 2007 Ryan Leavengood <leavengood@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "GraphicsContext.h"
31 #include "AffineTransform.h"
35 #include "NotImplemented.h"
38 #include <wtf/text/CString.h>
39 #include <GraphicsDefs.h>
48 class GraphicsContextPlatformPrivate {
50 GraphicsContextPlatformPrivate(BView* view);
51 ~GraphicsContextPlatformPrivate();
56 GraphicsContextPlatformPrivate::GraphicsContextPlatformPrivate(BView* view)
61 GraphicsContextPlatformPrivate::~GraphicsContextPlatformPrivate()
65 GraphicsContext::GraphicsContext(PlatformGraphicsContext* context)
66 : m_common(createGraphicsContextPrivate())
67 , m_data(new GraphicsContextPlatformPrivate(context))
69 setPaintingDisabled(!context);
72 GraphicsContext::~GraphicsContext()
74 destroyGraphicsContextPrivate(m_common);
78 PlatformGraphicsContext* GraphicsContext::platformContext() const
80 return m_data->m_view;
83 void GraphicsContext::savePlatformState()
85 m_data->m_view->PushState();
88 void GraphicsContext::restorePlatformState()
90 m_data->m_view->PopState();
93 // Draws a filled rectangle with a stroked border.
94 void GraphicsContext::drawRect(const IntRect& rect)
96 if (paintingDisabled())
99 m_data->m_view->FillRect(rect);
100 if (strokeStyle() != NoStroke)
101 m_data->m_view->StrokeRect(rect, getHaikuStrokeStyle());
104 // This is only used to draw borders.
105 void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
107 if (paintingDisabled())
110 if (strokeStyle() == NoStroke)
113 m_data->m_view->StrokeLine(point1, point2, getHaikuStrokeStyle());
116 // This method is only used to draw the little circles used in lists.
117 void GraphicsContext::drawEllipse(const IntRect& rect)
119 if (paintingDisabled())
122 m_data->m_view->FillEllipse(rect);
123 if (strokeStyle() != NoStroke)
124 m_data->m_view->StrokeEllipse(rect, getHaikuStrokeStyle());
127 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
129 if (paintingDisabled())
132 m_data->m_view->StrokeArc(rect, startAngle, angleSpan, getHaikuStrokeStyle());
135 void GraphicsContext::strokePath()
140 void GraphicsContext::drawConvexPolygon(size_t pointsLength, const FloatPoint* points, bool shouldAntialias)
142 if (paintingDisabled())
145 BPoint bPoints[pointsLength];
146 for (size_t i = 0; i < pointsLength; i++)
147 bPoints[i] = points[i];
149 m_data->m_view->FillPolygon(bPoints, pointsLength);
150 if (strokeStyle() != NoStroke)
151 // Stroke with low color
152 m_data->m_view->StrokePolygon(bPoints, pointsLength, true, getHaikuStrokeStyle());
155 void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialiased)
157 if (paintingDisabled())
163 // FIXME: IMPLEMENT!!
166 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
168 if (paintingDisabled())
171 rgb_color oldColor = m_data->m_view->HighColor();
172 m_data->m_view->SetHighColor(color);
173 m_data->m_view->FillRect(rect);
174 m_data->m_view->SetHighColor(oldColor);
177 void GraphicsContext::fillRect(const FloatRect& rect)
179 if (paintingDisabled())
183 void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
185 if (paintingDisabled() || !color.alpha())
189 // FIXME: A simple implementation could just use FillRoundRect if all
190 // the sizes are the same, or even if they are not. Otherwise several
191 // FillRect and FillArc calls are needed.
194 void GraphicsContext::fillPath()
199 void GraphicsContext::beginPath()
204 void GraphicsContext::addPath(const Path& path)
209 void GraphicsContext::clip(const FloatRect& rect)
211 if (paintingDisabled())
214 BRegion region(rect);
215 m_data->m_view->ConstrainClippingRegion(®ion);
218 void GraphicsContext::drawFocusRing(const Vector<Path>& paths, int width, int offset, const Color& color)
223 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int /* width */, int /* offset */, const Color& color)
225 if (paintingDisabled())
228 unsigned rectCount = rects.size();
230 // FIXME: maybe we should implement this with BShape?
232 if (rects.size() > 1) {
234 for (int i = 0; i < rectCount; ++i)
235 region.Include(BRect(rects[i]));
237 m_data->m_view->SetHighColor(color);
238 m_data->m_view->StrokeRect(region.Frame(), B_MIXED_COLORS);
242 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
244 if (paintingDisabled())
247 IntPoint endPoint = origin + IntSize(width, 0);
248 drawLine(origin, endPoint);
251 void GraphicsContext::drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle)
253 if (paintingDisabled())
259 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
265 void GraphicsContext::beginTransparencyLayer(float opacity)
267 if (paintingDisabled())
273 void GraphicsContext::endTransparencyLayer()
275 if (paintingDisabled())
281 void GraphicsContext::clearRect(const FloatRect& rect)
283 if (paintingDisabled())
289 void GraphicsContext::strokeRect(const FloatRect& rect, float width)
291 if (paintingDisabled())
294 float oldSize = m_data->m_view->PenSize();
295 m_data->m_view->SetPenSize(width);
296 m_data->m_view->StrokeRect(rect, getHaikuStrokeStyle());
297 m_data->m_view->SetPenSize(oldSize);
300 void GraphicsContext::setLineCap(LineCap lineCap)
302 if (paintingDisabled())
305 cap_mode mode = B_BUTT_CAP;
318 m_data->m_view->SetLineMode(mode, m_data->m_view->LineJoinMode(), m_data->m_view->LineMiterLimit());
321 void GraphicsContext::setLineJoin(LineJoin lineJoin)
323 if (paintingDisabled())
326 join_mode mode = B_MITER_JOIN;
339 m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), mode, m_data->m_view->LineMiterLimit());
342 void GraphicsContext::setMiterLimit(float limit)
344 if (paintingDisabled())
347 m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), m_data->m_view->LineJoinMode(), limit);
350 void GraphicsContext::setAlpha(float opacity)
352 if (paintingDisabled())
358 void GraphicsContext::setCompositeOperation(CompositeOperator op)
360 if (paintingDisabled())
363 drawing_mode mode = B_OP_COPY;
367 // Use the default above
369 case CompositeSourceOver:
373 printf("GraphicsContext::setCompositeOperation: Unsupported composite operation %s\n",
374 compositeOperatorName(op).utf8().data());
376 m_data->m_view->SetDrawingMode(mode);
379 void GraphicsContext::clip(const Path& path)
381 if (paintingDisabled())
384 m_data->m_view->ConstrainClippingRegion(path.platformPath());
387 void GraphicsContext::canvasClip(const Path& path)
392 void GraphicsContext::clipOut(const Path& path)
394 if (paintingDisabled())
400 void GraphicsContext::clipToImageBuffer(const FloatRect&, const ImageBuffer*)
405 AffineTransform GraphicsContext::getCTM() const
408 return AffineTransform();
411 void GraphicsContext::translate(float x, float y)
413 if (paintingDisabled())
419 void GraphicsContext::rotate(float radians)
421 if (paintingDisabled())
427 void GraphicsContext::scale(const FloatSize& size)
429 if (paintingDisabled())
435 void GraphicsContext::clipOut(const IntRect& rect)
437 if (paintingDisabled())
443 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
445 if (paintingDisabled())
451 void GraphicsContext::concatCTM(const AffineTransform& transform)
453 if (paintingDisabled())
459 void GraphicsContext::setPlatformShouldAntialias(bool enable)
461 if (paintingDisabled())
467 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
471 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
474 return InterpolationDefault;
477 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
482 void GraphicsContext::setPlatformFont(const Font& font)
484 m_data->m_view->SetFont(font.primaryFont()->platformData().font());
487 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
489 if (paintingDisabled())
492 m_data->m_view->SetHighColor(color);
495 pattern GraphicsContext::getHaikuStrokeStyle()
497 switch (strokeStyle()) {
502 return B_MIXED_COLORS;
505 // FIXME: use a better dashed stroke!
507 return B_MIXED_COLORS;
515 void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
517 // FIXME: see getHaikuStrokeStyle.
521 void GraphicsContext::setPlatformStrokeThickness(float thickness)
523 if (paintingDisabled())
526 m_data->m_view->SetPenSize(thickness);
529 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
531 if (paintingDisabled())
534 m_data->m_view->SetHighColor(color);
537 void GraphicsContext::clearPlatformShadow()
542 void GraphicsContext::setPlatformShadow(FloatSize const&, float, Color const&, ColorSpace)
547 } // namespace WebCore