OSDN Git Service

7dda245cd62daa59580b9a2c5ab293b17ea8f13b
[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(const Path&)
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(const Path&)
195 {
196     notImplemented();
197 }
198
199 void GraphicsContext::clip(const FloatRect& rect)
200 {
201     if (paintingDisabled())
202         return;
203
204     BRegion region(rect);
205     m_data->m_view->ConstrainClippingRegion(&region);
206 }
207
208 void GraphicsContext::drawFocusRing(const Path& path, int width, int offset, const Color& color)
209 {
210     // FIXME: implement
211 }
212
213 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int /* width */, int /* offset */, const Color& color)
214 {
215     if (paintingDisabled())
216         return;
217
218     unsigned rectCount = rects.size();
219
220     // FIXME: maybe we should implement this with BShape?
221
222     if (rects.size() > 1) {
223         BRegion    region;
224         for (int i = 0; i < rectCount; ++i)
225             region.Include(BRect(rects[i]));
226
227         m_data->m_view->SetHighColor(color);
228         m_data->m_view->StrokeRect(region.Frame(), B_MIXED_COLORS);
229     }
230 }
231
232 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
233 {
234     if (paintingDisabled())
235         return;
236
237     IntPoint endPoint = origin + IntSize(width, 0);
238     drawLine(origin, endPoint);
239 }
240
241 void GraphicsContext::drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle)
242 {
243     if (paintingDisabled())
244         return;
245
246     notImplemented();
247 }
248
249 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
250 {
251     notImplemented();
252     return rect;
253 }
254
255 void GraphicsContext::beginTransparencyLayer(float opacity)
256 {
257     if (paintingDisabled())
258         return;
259
260     notImplemented();
261 }
262
263 void GraphicsContext::endTransparencyLayer()
264 {
265     if (paintingDisabled())
266         return;
267
268     notImplemented();
269 }
270
271 void GraphicsContext::clearRect(const FloatRect& rect)
272 {
273     if (paintingDisabled())
274         return;
275
276     notImplemented();
277 }
278
279 void GraphicsContext::strokeRect(const FloatRect& rect, float width)
280 {
281     if (paintingDisabled())
282         return;
283
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);
288 }
289
290 void GraphicsContext::setLineCap(LineCap lineCap)
291 {
292     if (paintingDisabled())
293         return;
294
295     cap_mode mode = B_BUTT_CAP;
296     switch (lineCap) {
297     case RoundCap:
298         mode = B_ROUND_CAP;
299         break;
300     case SquareCap:
301         mode = B_SQUARE_CAP;
302         break;
303     case ButtCap:
304     default:
305         break;
306     }
307
308     m_data->m_view->SetLineMode(mode, m_data->m_view->LineJoinMode(), m_data->m_view->LineMiterLimit());
309 }
310
311 void GraphicsContext::setLineJoin(LineJoin lineJoin)
312 {
313     if (paintingDisabled())
314         return;
315
316     join_mode mode = B_MITER_JOIN;
317     switch (lineJoin) {
318     case RoundJoin:
319         mode = B_ROUND_JOIN;
320         break;
321     case BevelJoin:
322         mode = B_BEVEL_JOIN;
323         break;
324     case MiterJoin:
325     default:
326         break;
327     }
328
329     m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), mode, m_data->m_view->LineMiterLimit());
330 }
331
332 void GraphicsContext::setMiterLimit(float limit)
333 {
334     if (paintingDisabled())
335         return;
336
337     m_data->m_view->SetLineMode(m_data->m_view->LineCapMode(), m_data->m_view->LineJoinMode(), limit);
338 }
339
340 void GraphicsContext::setAlpha(float opacity)
341 {
342     if (paintingDisabled())
343         return;
344
345     notImplemented();
346 }
347
348 void GraphicsContext::setCompositeOperation(CompositeOperator op)
349 {
350     if (paintingDisabled())
351         return;
352
353     drawing_mode mode = B_OP_COPY;
354     switch (op) {
355     case CompositeClear:
356     case CompositeCopy:
357         // Use the default above
358         break;
359     case CompositeSourceOver:
360         mode = B_OP_OVER;
361         break;
362     default:
363         printf("GraphicsContext::setCompositeOperation: Unsupported composite operation %s\n",
364                 compositeOperatorName(op).utf8().data());
365     }
366     m_data->m_view->SetDrawingMode(mode);
367 }
368
369 void GraphicsContext::clip(const Path& path)
370 {
371     if (paintingDisabled())
372         return;
373
374     m_data->m_view->ConstrainClippingRegion(path.platformPath());
375 }
376
377 void GraphicsContext::canvasClip(const Path& path)
378 {
379     clip(path);
380 }
381
382 void GraphicsContext::clipOut(const Path& path)
383 {
384     if (paintingDisabled())
385         return;
386
387     notImplemented();
388 }
389
390 void GraphicsContext::clipToImageBuffer(const FloatRect&, const ImageBuffer*)
391 {
392     notImplemented();
393 }
394
395 AffineTransform GraphicsContext::getCTM() const
396 {
397     notImplemented();
398     return AffineTransform();
399 }
400
401 void GraphicsContext::translate(float x, float y)
402 {
403     if (paintingDisabled())
404         return;
405
406     notImplemented();
407 }
408
409 void GraphicsContext::rotate(float radians)
410 {
411     if (paintingDisabled())
412         return;
413
414     notImplemented();
415 }
416
417 void GraphicsContext::scale(const FloatSize& size)
418 {
419     if (paintingDisabled())
420         return;
421
422     notImplemented();
423 }
424
425 void GraphicsContext::clipOut(const IntRect& rect)
426 {
427     if (paintingDisabled())
428         return;
429
430     notImplemented();
431 }
432
433 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
434 {
435     if (paintingDisabled())
436         return;
437
438     notImplemented();
439 }
440
441 void GraphicsContext::concatCTM(const AffineTransform& transform)
442 {
443     if (paintingDisabled())
444         return;
445
446     notImplemented();
447 }
448
449 void GraphicsContext::setPlatformShouldAntialias(bool enable)
450 {
451     if (paintingDisabled())
452         return;
453
454     notImplemented();
455 }
456
457 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
458 {
459 }
460
461 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
462 {
463     notImplemented();
464     return InterpolationDefault;
465 }
466
467 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
468 {
469     notImplemented();
470 }
471
472 void GraphicsContext::setPlatformFont(const Font& font)
473 {
474     m_data->m_view->SetFont(font.primaryFont()->platformData().font());
475 }
476
477 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
478 {
479     if (paintingDisabled())
480         return;
481
482     m_data->m_view->SetHighColor(color);
483 }
484
485 pattern GraphicsContext::getHaikuStrokeStyle()
486 {
487     switch (strokeStyle()) {
488     case SolidStroke:
489         return B_SOLID_HIGH;
490         break;
491     case DottedStroke:
492         return B_MIXED_COLORS;
493         break;
494     case DashedStroke:
495         // FIXME: use a better dashed stroke!
496         notImplemented();
497         return B_MIXED_COLORS;
498         break;
499     default:
500         return B_SOLID_LOW;
501         break;
502     }
503 }
504
505 void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
506 {
507     // FIXME: see getHaikuStrokeStyle.
508     notImplemented();
509 }
510
511 void GraphicsContext::setPlatformStrokeThickness(float thickness)
512 {
513     if (paintingDisabled())
514         return;
515
516     m_data->m_view->SetPenSize(thickness);
517 }
518
519 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
520 {
521     if (paintingDisabled())
522         return;
523
524     m_data->m_view->SetHighColor(color);
525 }
526
527 void GraphicsContext::clearPlatformShadow()
528 {
529     notImplemented();
530 }
531
532 void GraphicsContext::setPlatformShadow(FloatSize const&, float, Color const&, ColorSpace)
533 {
534     notImplemented();
535 }
536
537 } // namespace WebCore
538