1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "GraphicsContext.h"
28
29 #include "BidiResolver.h"
30 #include "Generator.h"
31 #include "GraphicsContextPrivate.h"
32 #include "Font.h"
33
34 using namespace std;
35
36 namespace WebCore {
37
38 class TextRunIterator {
39 public:
TextRunIterator()40 TextRunIterator()
41 : m_textRun(0)
42 , m_offset(0)
43 {
44 }
45
TextRunIterator(const TextRun * textRun,unsigned offset)46 TextRunIterator(const TextRun* textRun, unsigned offset)
47 : m_textRun(textRun)
48 , m_offset(offset)
49 {
50 }
51
TextRunIterator(const TextRunIterator & other)52 TextRunIterator(const TextRunIterator& other)
53 : m_textRun(other.m_textRun)
54 , m_offset(other.m_offset)
55 {
56 }
57
offset() const58 unsigned offset() const { return m_offset; }
increment()59 void increment() { m_offset++; }
atEnd() const60 bool atEnd() const { return !m_textRun || m_offset >= m_textRun->length(); }
current() const61 UChar current() const { return (*m_textRun)[m_offset]; }
direction() const62 WTF::Unicode::Direction direction() const { return atEnd() ? WTF::Unicode::OtherNeutral : WTF::Unicode::direction(current()); }
63
operator ==(const TextRunIterator & other)64 bool operator==(const TextRunIterator& other)
65 {
66 return m_offset == other.m_offset && m_textRun == other.m_textRun;
67 }
68
operator !=(const TextRunIterator & other)69 bool operator!=(const TextRunIterator& other) { return !operator==(other); }
70
71 private:
72 const TextRun* m_textRun;
73 int m_offset;
74 };
75
createGraphicsContextPrivate()76 GraphicsContextPrivate* GraphicsContext::createGraphicsContextPrivate()
77 {
78 return new GraphicsContextPrivate;
79 }
80
destroyGraphicsContextPrivate(GraphicsContextPrivate * deleteMe)81 void GraphicsContext::destroyGraphicsContextPrivate(GraphicsContextPrivate* deleteMe)
82 {
83 delete deleteMe;
84 }
85
save()86 void GraphicsContext::save()
87 {
88 if (paintingDisabled())
89 return;
90
91 m_common->stack.append(m_common->state);
92
93 savePlatformState();
94 }
95
restore()96 void GraphicsContext::restore()
97 {
98 if (paintingDisabled())
99 return;
100
101 if (m_common->stack.isEmpty()) {
102 LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
103 return;
104 }
105 m_common->state = m_common->stack.last();
106 m_common->stack.removeLast();
107
108 restorePlatformState();
109 }
110
setStrokeThickness(float thickness)111 void GraphicsContext::setStrokeThickness(float thickness)
112 {
113 m_common->state.strokeThickness = thickness;
114 setPlatformStrokeThickness(thickness);
115 }
116
setStrokeStyle(const StrokeStyle & style)117 void GraphicsContext::setStrokeStyle(const StrokeStyle& style)
118 {
119 m_common->state.strokeStyle = style;
120 setPlatformStrokeStyle(style);
121 }
122
setStrokeColor(const Color & color)123 void GraphicsContext::setStrokeColor(const Color& color)
124 {
125 m_common->state.strokeColorSpace = SolidColorSpace;
126 m_common->state.strokeColor = color;
127 setPlatformStrokeColor(color);
128 }
129
strokeColorSpace() const130 ColorSpace GraphicsContext::strokeColorSpace() const
131 {
132 return m_common->state.strokeColorSpace;
133 }
134
setShadow(const IntSize & size,int blur,const Color & color)135 void GraphicsContext::setShadow(const IntSize& size, int blur, const Color& color)
136 {
137 m_common->state.shadowSize = size;
138 m_common->state.shadowBlur = blur;
139 m_common->state.shadowColor = color;
140 setPlatformShadow(size, blur, color);
141 }
142
clearShadow()143 void GraphicsContext::clearShadow()
144 {
145 m_common->state.shadowSize = IntSize();
146 m_common->state.shadowBlur = 0;
147 m_common->state.shadowColor = Color();
148 clearPlatformShadow();
149 }
150
getShadow(IntSize & size,int & blur,Color & color) const151 bool GraphicsContext::getShadow(IntSize& size, int& blur, Color& color) const
152 {
153 size = m_common->state.shadowSize;
154 blur = m_common->state.shadowBlur;
155 color = m_common->state.shadowColor;
156
157 return color.isValid() && color.alpha() && (blur || size.width() || size.height());
158 }
159
strokeThickness() const160 float GraphicsContext::strokeThickness() const
161 {
162 return m_common->state.strokeThickness;
163 }
164
strokeStyle() const165 StrokeStyle GraphicsContext::strokeStyle() const
166 {
167 return m_common->state.strokeStyle;
168 }
169
strokeColor() const170 Color GraphicsContext::strokeColor() const
171 {
172 return m_common->state.strokeColor;
173 }
174
fillRule() const175 WindRule GraphicsContext::fillRule() const
176 {
177 return m_common->state.fillRule;
178 }
179
setFillRule(WindRule fillRule)180 void GraphicsContext::setFillRule(WindRule fillRule)
181 {
182 m_common->state.fillRule = fillRule;
183 }
184
setFillColor(const Color & color)185 void GraphicsContext::setFillColor(const Color& color)
186 {
187 m_common->state.fillColorSpace = SolidColorSpace;
188 m_common->state.fillColor = color;
189 setPlatformFillColor(color);
190 }
191
fillColor() const192 Color GraphicsContext::fillColor() const
193 {
194 return m_common->state.fillColor;
195 }
196
setShouldAntialias(bool b)197 void GraphicsContext::setShouldAntialias(bool b)
198 {
199 m_common->state.shouldAntialias = b;
200 setPlatformShouldAntialias(b);
201 }
202
shouldAntialias() const203 bool GraphicsContext::shouldAntialias() const
204 {
205 return m_common->state.shouldAntialias;
206 }
207
setStrokePattern(PassRefPtr<Pattern> pattern)208 void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
209 {
210 ASSERT(pattern);
211 if (!pattern) {
212 setStrokeColor(Color::black);
213 return;
214 }
215 m_common->state.strokeColorSpace = PatternColorSpace;
216 m_common->state.strokePattern = pattern;
217 setPlatformStrokePattern(m_common->state.strokePattern.get());
218 }
219
setFillPattern(PassRefPtr<Pattern> pattern)220 void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
221 {
222 ASSERT(pattern);
223 if (!pattern) {
224 setFillColor(Color::black);
225 return;
226 }
227 m_common->state.fillColorSpace = PatternColorSpace;
228 m_common->state.fillPattern = pattern;
229 setPlatformFillPattern(m_common->state.fillPattern.get());
230 }
231
setStrokeGradient(PassRefPtr<Gradient> gradient)232 void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
233 {
234 ASSERT(gradient);
235 if (!gradient) {
236 setStrokeColor(Color::black);
237 return;
238 }
239 m_common->state.strokeColorSpace = GradientColorSpace;
240 m_common->state.strokeGradient = gradient;
241 setPlatformStrokeGradient(m_common->state.strokeGradient.get());
242 }
243
setFillGradient(PassRefPtr<Gradient> gradient)244 void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
245 {
246 ASSERT(gradient);
247 if (!gradient) {
248 setFillColor(Color::black);
249 return;
250 }
251 m_common->state.fillColorSpace = GradientColorSpace;
252 m_common->state.fillGradient = gradient;
253 setPlatformFillGradient(m_common->state.fillGradient.get());
254 }
255
fillGradient() const256 Gradient* GraphicsContext::fillGradient() const
257 {
258 return m_common->state.fillGradient.get();
259 }
260
fillColorSpace() const261 ColorSpace GraphicsContext::fillColorSpace() const
262 {
263 return m_common->state.fillColorSpace;
264 }
265
strokeGradient() const266 Gradient* GraphicsContext::strokeGradient() const
267 {
268 return m_common->state.strokeGradient.get();
269 }
270
fillPattern() const271 Pattern* GraphicsContext::fillPattern() const
272 {
273 return m_common->state.fillPattern.get();
274 }
275
strokePattern() const276 Pattern* GraphicsContext::strokePattern() const
277 {
278 return m_common->state.strokePattern.get();
279 }
280
setShadowsIgnoreTransforms(bool ignoreTransforms)281 void GraphicsContext::setShadowsIgnoreTransforms(bool ignoreTransforms)
282 {
283 m_common->state.shadowsIgnoreTransforms = ignoreTransforms;
284 }
285
updatingControlTints() const286 bool GraphicsContext::updatingControlTints() const
287 {
288 return m_common->m_updatingControlTints;
289 }
290
setUpdatingControlTints(bool b)291 void GraphicsContext::setUpdatingControlTints(bool b)
292 {
293 setPaintingDisabled(b);
294 m_common->m_updatingControlTints = b;
295 }
296
setPaintingDisabled(bool f)297 void GraphicsContext::setPaintingDisabled(bool f)
298 {
299 m_common->state.paintingDisabled = f;
300 }
301
paintingDisabled() const302 bool GraphicsContext::paintingDisabled() const
303 {
304 return m_common->state.paintingDisabled;
305 }
306
drawImage(Image * image,const IntPoint & p,CompositeOperator op)307 void GraphicsContext::drawImage(Image* image, const IntPoint& p, CompositeOperator op)
308 {
309 drawImage(image, p, IntRect(0, 0, -1, -1), op);
310 }
311
drawImage(Image * image,const IntRect & r,CompositeOperator op,bool useLowQualityScale)312 void GraphicsContext::drawImage(Image* image, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
313 {
314 drawImage(image, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
315 }
316
drawImage(Image * image,const IntPoint & dest,const IntRect & srcRect,CompositeOperator op)317 void GraphicsContext::drawImage(Image* image, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
318 {
319 drawImage(image, IntRect(dest, srcRect.size()), srcRect, op);
320 }
321
drawImage(Image * image,const IntRect & dest,const IntRect & srcRect,CompositeOperator op,bool useLowQualityScale)322 void GraphicsContext::drawImage(Image* image, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
323 {
324 drawImage(image, FloatRect(dest), srcRect, op, useLowQualityScale);
325 }
326
327 #if !PLATFORM(WINCE) || PLATFORM(QT)
drawText(const Font & font,const TextRun & run,const IntPoint & point,int from,int to)328 void GraphicsContext::drawText(const Font& font, const TextRun& run, const IntPoint& point, int from, int to)
329 {
330 if (paintingDisabled())
331 return;
332
333 font.drawText(this, run, point, from, to);
334 }
335 #endif
336
drawBidiText(const Font & font,const TextRun & run,const FloatPoint & point)337 void GraphicsContext::drawBidiText(const Font& font, const TextRun& run, const FloatPoint& point)
338 {
339 if (paintingDisabled())
340 return;
341
342 BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
343 WTF::Unicode::Direction paragraphDirection = run.ltr() ? WTF::Unicode::LeftToRight : WTF::Unicode::RightToLeft;
344
345 bidiResolver.setStatus(BidiStatus(paragraphDirection, paragraphDirection, paragraphDirection, BidiContext::create(run.ltr() ? 0 : 1, paragraphDirection, run.directionalOverride())));
346
347 bidiResolver.setPosition(TextRunIterator(&run, 0));
348 bidiResolver.createBidiRunsForLine(TextRunIterator(&run, run.length()));
349
350 if (!bidiResolver.runCount())
351 return;
352
353 FloatPoint currPoint = point;
354 BidiCharacterRun* bidiRun = bidiResolver.firstRun();
355 while (bidiRun) {
356
357 TextRun subrun = run;
358 subrun.setText(run.data(bidiRun->start()), bidiRun->stop() - bidiRun->start());
359 subrun.setRTL(bidiRun->level() % 2);
360 subrun.setDirectionalOverride(bidiRun->dirOverride(false));
361
362 font.drawText(this, subrun, currPoint);
363
364 bidiRun = bidiRun->next();
365 // FIXME: Have Font::drawText return the width of what it drew so that we don't have to re-measure here.
366 if (bidiRun)
367 currPoint.move(font.floatWidth(subrun), 0.f);
368 }
369
370 bidiResolver.deleteRuns();
371 }
372
drawHighlightForText(const Font & font,const TextRun & run,const IntPoint & point,int h,const Color & backgroundColor,int from,int to)373 void GraphicsContext::drawHighlightForText(const Font& font, const TextRun& run, const IntPoint& point, int h, const Color& backgroundColor, int from, int to)
374 {
375 if (paintingDisabled())
376 return;
377
378 fillRect(font.selectionRectForText(run, point, h, from, to), backgroundColor);
379 }
380
initFocusRing(int width,int offset)381 void GraphicsContext::initFocusRing(int width, int offset)
382 {
383 if (paintingDisabled())
384 return;
385 clearFocusRing();
386
387 m_common->m_focusRingWidth = width;
388 m_common->m_focusRingOffset = offset;
389 }
390
clearFocusRing()391 void GraphicsContext::clearFocusRing()
392 {
393 m_common->m_focusRingRects.clear();
394 }
395
focusRingBoundingRect()396 IntRect GraphicsContext::focusRingBoundingRect()
397 {
398 IntRect result = IntRect(0, 0, 0, 0);
399
400 const Vector<IntRect>& rects = focusRingRects();
401 unsigned rectCount = rects.size();
402 for (unsigned i = 0; i < rectCount; i++)
403 result.unite(rects[i]);
404
405 return result;
406 }
407
addFocusRingRect(const IntRect & rect)408 void GraphicsContext::addFocusRingRect(const IntRect& rect)
409 {
410 if (paintingDisabled() || rect.isEmpty())
411 return;
412 m_common->m_focusRingRects.append(rect);
413 }
414
focusRingWidth() const415 int GraphicsContext::focusRingWidth() const
416 {
417 return m_common->m_focusRingWidth;
418 }
419
focusRingOffset() const420 int GraphicsContext::focusRingOffset() const
421 {
422 return m_common->m_focusRingOffset;
423 }
424
focusRingRects() const425 const Vector<IntRect>& GraphicsContext::focusRingRects() const
426 {
427 return m_common->m_focusRingRects;
428 }
429
drawImage(Image * image,const FloatRect & dest,const FloatRect & src,CompositeOperator op,bool useLowQualityScale)430 void GraphicsContext::drawImage(Image* image, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
431 {
432 if (paintingDisabled() || !image)
433 return;
434
435 float tsw = src.width();
436 float tsh = src.height();
437 float tw = dest.width();
438 float th = dest.height();
439
440 if (tsw == -1)
441 tsw = image->width();
442 if (tsh == -1)
443 tsh = image->height();
444
445 if (tw == -1)
446 tw = image->width();
447 if (th == -1)
448 th = image->height();
449
450 if (useLowQualityScale) {
451 save();
452 setImageInterpolationQuality(InterpolationNone);
453 }
454 image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op);
455 if (useLowQualityScale)
456 restore();
457 }
458
drawTiledImage(Image * image,const IntRect & rect,const IntPoint & srcPoint,const IntSize & tileSize,CompositeOperator op)459 void GraphicsContext::drawTiledImage(Image* image, const IntRect& rect, const IntPoint& srcPoint, const IntSize& tileSize, CompositeOperator op)
460 {
461 if (paintingDisabled() || !image)
462 return;
463
464 image->drawTiled(this, rect, srcPoint, tileSize, op);
465 }
466
drawTiledImage(Image * image,const IntRect & dest,const IntRect & srcRect,Image::TileRule hRule,Image::TileRule vRule,CompositeOperator op)467 void GraphicsContext::drawTiledImage(Image* image, const IntRect& dest, const IntRect& srcRect, Image::TileRule hRule, Image::TileRule vRule, CompositeOperator op)
468 {
469 if (paintingDisabled() || !image)
470 return;
471
472 if (hRule == Image::StretchTile && vRule == Image::StretchTile)
473 // Just do a scale.
474 return drawImage(image, dest, srcRect, op);
475
476 image->drawTiled(this, dest, srcRect, hRule, vRule, op);
477 }
478
addRoundedRectClip(const IntRect & rect,const IntSize & topLeft,const IntSize & topRight,const IntSize & bottomLeft,const IntSize & bottomRight)479 void GraphicsContext::addRoundedRectClip(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight,
480 const IntSize& bottomLeft, const IntSize& bottomRight)
481 {
482 if (paintingDisabled())
483 return;
484
485 clip(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
486 }
487
clipOutRoundedRect(const IntRect & rect,const IntSize & topLeft,const IntSize & topRight,const IntSize & bottomLeft,const IntSize & bottomRight)488 void GraphicsContext::clipOutRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight,
489 const IntSize& bottomLeft, const IntSize& bottomRight)
490 {
491 if (paintingDisabled())
492 return;
493
494 clipOut(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
495 }
496
textDrawingMode()497 int GraphicsContext::textDrawingMode()
498 {
499 return m_common->state.textDrawingMode;
500 }
501
setTextDrawingMode(int mode)502 void GraphicsContext::setTextDrawingMode(int mode)
503 {
504 m_common->state.textDrawingMode = mode;
505 if (paintingDisabled())
506 return;
507 setPlatformTextDrawingMode(mode);
508 }
509
fillRect(const FloatRect & rect,Generator & generator)510 void GraphicsContext::fillRect(const FloatRect& rect, Generator& generator)
511 {
512 if (paintingDisabled())
513 return;
514 generator.fill(this, rect);
515 }
516
517 #if !PLATFORM(SKIA)
setPlatformFillGradient(Gradient *)518 void GraphicsContext::setPlatformFillGradient(Gradient*)
519 {
520 }
521
setPlatformFillPattern(Pattern *)522 void GraphicsContext::setPlatformFillPattern(Pattern*)
523 {
524 }
525
setPlatformStrokeGradient(Gradient *)526 void GraphicsContext::setPlatformStrokeGradient(Gradient*)
527 {
528 }
529
setPlatformStrokePattern(Pattern *)530 void GraphicsContext::setPlatformStrokePattern(Pattern*)
531 {
532 }
533 #endif
534
535 #if !PLATFORM(CG) && !PLATFORM(SKIA)
536 // Implement this if you want to go ahead and push the drawing mode into your native context
537 // immediately.
setPlatformTextDrawingMode(int mode)538 void GraphicsContext::setPlatformTextDrawingMode(int mode)
539 {
540 }
541 #endif
542
543 #if !PLATFORM(QT) && !PLATFORM(CAIRO) && !PLATFORM(SKIA)
setPlatformStrokeStyle(const StrokeStyle &)544 void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle&)
545 {
546 }
547 #endif
548
549 }
550