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(const Path&)
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(const Path&)
199 void GraphicsContext::clip(const FloatRect& rect)
201 if (paintingDisabled())
204 BRegion region(rect);
205 m_data->m_view->ConstrainClippingRegion(®ion);
208 void GraphicsContext::drawFocusRing(const Path& path, int width, int offset, const Color& color)
213 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int /* width */, int /* offset */, const Color& color)
215 if (paintingDisabled())
218 unsigned rectCount = rects.size();
220 // FIXME: maybe we should implement this with BShape?
222 if (rects.size() > 1) {
224 for (int i = 0; i < rectCount; ++i)
225 region.Include(BRect(rects[i]));
227 m_data->m_view->SetHighColor(color);
228 m_data->m_view->StrokeRect(region.Frame(), B_MIXED_COLORS);
232 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
234 if (paintingDisabled())
237 IntPoint endPoint = origin + IntSize(width, 0);
238 drawLine(origin, endPoint);
241 void GraphicsContext::drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle)
243 if (paintingDisabled())
249 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
255 void GraphicsContext::beginTransparencyLayer(float opacity)
257 if (paintingDisabled())
263 void GraphicsContext::endTransparencyLayer()
265 if (paintingDisabled())
271 void GraphicsContext::clearRect(const FloatRect& rect)
273 if (paintingDisabled())
279 void GraphicsContext::strokeRect(const FloatRect& rect, float width)
281 if (paintingDisabled())
284 float oldSize = m_data->m_view->PenSize();
285 m_data->m_view->SetPenSize(width);
286 m_data->m_view->StrokeRect(rect, getHaikuStrokeStyle());
287 m_data->m_view->SetPenSize(oldSize);
290 void GraphicsContext::setLineCap(LineCap lineCap)
292 if (paintingDisabled())
295 cap_mode mode = B_BUTT_CAP;
308 m_data->m_view->SetLineMode(mode, m_data->m_view->LineJoinMode(), m_data->m_view->LineMiterLimit());
311 void GraphicsContext::setLineJoin(LineJoin lineJoin)
313 if (paintingDisabled())
316 join_mode mode = B_MITER_JOIN;
329 m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), mode, m_data->m_view->LineMiterLimit());
332 void GraphicsContext::setMiterLimit(float limit)
334 if (paintingDisabled())
337 m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), m_data->m_view->LineJoinMode(), limit);
340 void GraphicsContext::setAlpha(float opacity)
342 if (paintingDisabled())
348 void GraphicsContext::setCompositeOperation(CompositeOperator op)
350 if (paintingDisabled())
353 drawing_mode mode = B_OP_COPY;
357 // Use the default above
359 case CompositeSourceOver:
363 printf("GraphicsContext::setCompositeOperation: Unsupported composite operation %s\n",
364 compositeOperatorName(op).utf8().data());
366 m_data->m_view->SetDrawingMode(mode);
369 void GraphicsContext::clip(const Path& path)
371 if (paintingDisabled())
374 m_data->m_view->ConstrainClippingRegion(path.platformPath());
377 void GraphicsContext::canvasClip(const Path& path)
382 void GraphicsContext::clipOut(const Path& path)
384 if (paintingDisabled())
390 void GraphicsContext::clipToImageBuffer(const FloatRect&, const ImageBuffer*)
395 AffineTransform GraphicsContext::getCTM() const
398 return AffineTransform();
401 void GraphicsContext::translate(float x, float y)
403 if (paintingDisabled())
409 void GraphicsContext::rotate(float radians)
411 if (paintingDisabled())
417 void GraphicsContext::scale(const FloatSize& size)
419 if (paintingDisabled())
425 void GraphicsContext::clipOut(const IntRect& rect)
427 if (paintingDisabled())
433 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
435 if (paintingDisabled())
441 void GraphicsContext::concatCTM(const AffineTransform& transform)
443 if (paintingDisabled())
449 void GraphicsContext::setPlatformShouldAntialias(bool enable)
451 if (paintingDisabled())
457 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
461 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
464 return InterpolationDefault;
467 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
472 void GraphicsContext::setPlatformFont(const Font& font)
474 m_data->m_view->SetFont(font.primaryFont()->platformData().font());
477 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
479 if (paintingDisabled())
482 m_data->m_view->SetHighColor(color);
485 pattern GraphicsContext::getHaikuStrokeStyle()
487 switch (strokeStyle()) {
492 return B_MIXED_COLORS;
495 // FIXME: use a better dashed stroke!
497 return B_MIXED_COLORS;
505 void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
507 // FIXME: see getHaikuStrokeStyle.
511 void GraphicsContext::setPlatformStrokeThickness(float thickness)
513 if (paintingDisabled())
516 m_data->m_view->SetPenSize(thickness);
519 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
521 if (paintingDisabled())
524 m_data->m_view->SetHighColor(color);
527 void GraphicsContext::clearPlatformShadow()
532 void GraphicsContext::setPlatformShadow(FloatSize const&, float, Color const&, ColorSpace)
537 } // namespace WebCore