OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / platform / graphics / haiku / GraphicsContextHaiku.cpp
1 /*
2  * Copyright (C) 2007 Ryan Leavengood <leavengood@gmail.com>
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28 #include "config.h"
29 #include "GraphicsContext.h"
30
31 #include "AffineTransform.h"
32 #include "Color.h"
33 #include "Font.h"
34 #include "FontData.h"
35 #include "NotImplemented.h"
36 #include "Path.h"
37 #include "Pen.h"
38 #include <wtf/text/CString.h>
39 #include <GraphicsDefs.h>
40 #include <Region.h>
41 #include <View.h>
42 #include <Window.h>
43 #include <stdio.h>
44
45
46 namespace WebCore {
47
48 class GraphicsContextPlatformPrivate {
49 public:
50     GraphicsContextPlatformPrivate(BView* view);
51     ~GraphicsContextPlatformPrivate();
52
53     BView* m_view;
54 };
55
56 GraphicsContextPlatformPrivate::GraphicsContextPlatformPrivate(BView* view)
57     : m_view(view)
58 {
59 }
60
61 GraphicsContextPlatformPrivate::~GraphicsContextPlatformPrivate()
62 {
63 }
64
65 GraphicsContext::GraphicsContext(PlatformGraphicsContext* context)
66     : m_common(createGraphicsContextPrivate())
67     , m_data(new GraphicsContextPlatformPrivate(context))
68 {
69     setPaintingDisabled(!context);
70 }
71
72 GraphicsContext::~GraphicsContext()
73 {
74     destroyGraphicsContextPrivate(m_common);
75     delete m_data;
76 }
77
78 PlatformGraphicsContext* GraphicsContext::platformContext() const
79 {
80     return m_data->m_view;
81 }
82
83 void GraphicsContext::savePlatformState()
84 {
85     m_data->m_view->PushState();
86 }
87
88 void GraphicsContext::restorePlatformState()
89 {
90     m_data->m_view->PopState();
91 }
92
93 // Draws a filled rectangle with a stroked border.
94 void GraphicsContext::drawRect(const IntRect& rect)
95 {
96     if (paintingDisabled())
97         return;
98
99     m_data->m_view->FillRect(rect);
100     if (strokeStyle() != NoStroke)
101         m_data->m_view->StrokeRect(rect, getHaikuStrokeStyle());
102 }
103
104 // This is only used to draw borders.
105 void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
106 {
107     if (paintingDisabled())
108         return;
109
110     if (strokeStyle() == NoStroke)
111         return;
112
113     m_data->m_view->StrokeLine(point1, point2, getHaikuStrokeStyle());
114 }
115
116 // This method is only used to draw the little circles used in lists.
117 void GraphicsContext::drawEllipse(const IntRect& rect)
118 {
119     if (paintingDisabled())
120         return;
121
122     m_data->m_view->FillEllipse(rect);
123     if (strokeStyle() != NoStroke)
124         m_data->m_view->StrokeEllipse(rect, getHaikuStrokeStyle());
125 }
126
127 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
128 {
129     if (paintingDisabled())
130         return;
131
132     m_data->m_view->StrokeArc(rect, startAngle, angleSpan, getHaikuStrokeStyle());
133 }
134
135 void GraphicsContext::strokePath()
136 {
137     notImplemented();
138 }
139
140 void GraphicsContext::drawConvexPolygon(size_t pointsLength, const FloatPoint* points, bool shouldAntialias)
141 {
142     if (paintingDisabled())
143         return;
144
145     BPoint bPoints[pointsLength];
146     for (size_t i = 0; i < pointsLength; i++)
147         bPoints[i] = points[i];
148
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());
153 }
154
155 void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialiased)
156 {
157     if (paintingDisabled())
158         return;
159
160     if (numPoints <= 1)
161         return;
162     
163     // FIXME: IMPLEMENT!!
164 }
165
166 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
167 {
168     if (paintingDisabled())
169         return;
170
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);
175 }
176
177 void GraphicsContext::fillRect(const FloatRect& rect)
178 {
179     if (paintingDisabled())
180         return;
181 }
182
183 void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
184 {
185     if (paintingDisabled() || !color.alpha())
186         return;
187
188     notImplemented();
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.
192 }
193
194 void GraphicsContext::fillPath()
195 {
196     notImplemented();
197 }
198
199 void GraphicsContext::beginPath()
200 {
201     notImplemented();
202 }
203
204 void GraphicsContext::addPath(const Path& path)
205 {
206     notImplemented();
207 }
208
209 void GraphicsContext::clip(const FloatRect& rect)
210 {
211     if (paintingDisabled())
212         return;
213
214     BRegion region(rect);
215     m_data->m_view->ConstrainClippingRegion(&region);
216 }
217
218 void GraphicsContext::drawFocusRing(const Vector<Path>& paths, int width, int offset, const Color& color)
219 {
220     // FIXME: implement
221 }
222
223 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int /* width */, int /* offset */, const Color& color)
224 {
225     if (paintingDisabled())
226         return;
227
228     unsigned rectCount = rects.size();
229
230     // FIXME: maybe we should implement this with BShape?
231
232     if (rects.size() > 1) {
233         BRegion    region;
234         for (int i = 0; i < rectCount; ++i)
235             region.Include(BRect(rects[i]));
236
237         m_data->m_view->SetHighColor(color);
238         m_data->m_view->StrokeRect(region.Frame(), B_MIXED_COLORS);
239     }
240 }
241
242 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
243 {
244     if (paintingDisabled())
245         return;
246
247     IntPoint endPoint = origin + IntSize(width, 0);
248     drawLine(origin, endPoint);
249 }
250
251 void GraphicsContext::drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle)
252 {
253     if (paintingDisabled())
254         return;
255
256     notImplemented();
257 }
258
259 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
260 {
261     notImplemented();
262     return rect;
263 }
264
265 void GraphicsContext::beginTransparencyLayer(float opacity)
266 {
267     if (paintingDisabled())
268         return;
269
270     notImplemented();
271 }
272
273 void GraphicsContext::endTransparencyLayer()
274 {
275     if (paintingDisabled())
276         return;
277
278     notImplemented();
279 }
280
281 void GraphicsContext::clearRect(const FloatRect& rect)
282 {
283     if (paintingDisabled())
284         return;
285
286     notImplemented();
287 }
288
289 void GraphicsContext::strokeRect(const FloatRect& rect, float width)
290 {
291     if (paintingDisabled())
292         return;
293
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);
298 }
299
300 void GraphicsContext::setLineCap(LineCap lineCap)
301 {
302     if (paintingDisabled())
303         return;
304
305     cap_mode mode = B_BUTT_CAP;
306     switch (lineCap) {
307     case RoundCap:
308         mode = B_ROUND_CAP;
309         break;
310     case SquareCap:
311         mode = B_SQUARE_CAP;
312         break;
313     case ButtCap:
314     default:
315         break;
316     }
317
318     m_data->m_view->SetLineMode(mode, m_data->m_view->LineJoinMode(), m_data->m_view->LineMiterLimit());
319 }
320
321 void GraphicsContext::setLineJoin(LineJoin lineJoin)
322 {
323     if (paintingDisabled())
324         return;
325
326     join_mode mode = B_MITER_JOIN;
327     switch (lineJoin) {
328     case RoundJoin:
329         mode = B_ROUND_JOIN;
330         break;
331     case BevelJoin:
332         mode = B_BEVEL_JOIN;
333         break;
334     case MiterJoin:
335     default:
336         break;
337     }
338
339     m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), mode, m_data->m_view->LineMiterLimit());
340 }
341
342 void GraphicsContext::setMiterLimit(float limit)
343 {
344     if (paintingDisabled())
345         return;
346
347     m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), m_data->m_view->LineJoinMode(), limit);
348 }
349
350 void GraphicsContext::setAlpha(float opacity)
351 {
352     if (paintingDisabled())
353         return;
354
355     notImplemented();
356 }
357
358 void GraphicsContext::setCompositeOperation(CompositeOperator op)
359 {
360     if (paintingDisabled())
361         return;
362
363     drawing_mode mode = B_OP_COPY;
364     switch (op) {
365     case CompositeClear:
366     case CompositeCopy:
367         // Use the default above
368         break;
369     case CompositeSourceOver:
370         mode = B_OP_OVER;
371         break;
372     default:
373         printf("GraphicsContext::setCompositeOperation: Unsupported composite operation %s\n",
374                 compositeOperatorName(op).utf8().data());
375     }
376     m_data->m_view->SetDrawingMode(mode);
377 }
378
379 void GraphicsContext::clip(const Path& path)
380 {
381     if (paintingDisabled())
382         return;
383
384     m_data->m_view->ConstrainClippingRegion(path.platformPath());
385 }
386
387 void GraphicsContext::canvasClip(const Path& path)
388 {
389     clip(path);
390 }
391
392 void GraphicsContext::clipOut(const Path& path)
393 {
394     if (paintingDisabled())
395         return;
396
397     notImplemented();
398 }
399
400 void GraphicsContext::clipToImageBuffer(const FloatRect&, const ImageBuffer*)
401 {
402     notImplemented();
403 }
404
405 AffineTransform GraphicsContext::getCTM() const
406 {
407     notImplemented();
408     return AffineTransform();
409 }
410
411 void GraphicsContext::translate(float x, float y)
412 {
413     if (paintingDisabled())
414         return;
415
416     notImplemented();
417 }
418
419 void GraphicsContext::rotate(float radians)
420 {
421     if (paintingDisabled())
422         return;
423
424     notImplemented();
425 }
426
427 void GraphicsContext::scale(const FloatSize& size)
428 {
429     if (paintingDisabled())
430         return;
431
432     notImplemented();
433 }
434
435 void GraphicsContext::clipOut(const IntRect& rect)
436 {
437     if (paintingDisabled())
438         return;
439
440     notImplemented();
441 }
442
443 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
444 {
445     if (paintingDisabled())
446         return;
447
448     notImplemented();
449 }
450
451 void GraphicsContext::concatCTM(const AffineTransform& transform)
452 {
453     if (paintingDisabled())
454         return;
455
456     notImplemented();
457 }
458
459 void GraphicsContext::setPlatformShouldAntialias(bool enable)
460 {
461     if (paintingDisabled())
462         return;
463
464     notImplemented();
465 }
466
467 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
468 {
469 }
470
471 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
472 {
473     notImplemented();
474     return InterpolationDefault;
475 }
476
477 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
478 {
479     notImplemented();
480 }
481
482 void GraphicsContext::setPlatformFont(const Font& font)
483 {
484     m_data->m_view->SetFont(font.primaryFont()->platformData().font());
485 }
486
487 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
488 {
489     if (paintingDisabled())
490         return;
491
492     m_data->m_view->SetHighColor(color);
493 }
494
495 pattern GraphicsContext::getHaikuStrokeStyle()
496 {
497     switch (strokeStyle()) {
498     case SolidStroke:
499         return B_SOLID_HIGH;
500         break;
501     case DottedStroke:
502         return B_MIXED_COLORS;
503         break;
504     case DashedStroke:
505         // FIXME: use a better dashed stroke!
506         notImplemented();
507         return B_MIXED_COLORS;
508         break;
509     default:
510         return B_SOLID_LOW;
511         break;
512     }
513 }
514
515 void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
516 {
517     // FIXME: see getHaikuStrokeStyle.
518     notImplemented();
519 }
520
521 void GraphicsContext::setPlatformStrokeThickness(float thickness)
522 {
523     if (paintingDisabled())
524         return;
525
526     m_data->m_view->SetPenSize(thickness);
527 }
528
529 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
530 {
531     if (paintingDisabled())
532         return;
533
534     m_data->m_view->SetHighColor(color);
535 }
536
537 void GraphicsContext::clearPlatformShadow()
538 {
539     notImplemented();
540 }
541
542 void GraphicsContext::setPlatformShadow(FloatSize const&, float, Color const&, ColorSpace)
543 {
544     notImplemented();
545 }
546
547 } // namespace WebCore
548