GraphicsContext.cpp [plain text]
#include "config.h"
#include "GraphicsContext.h"
#include "BidiResolver.h"
#include "Font.h"
#include "Generator.h"
#include "GraphicsContextPrivate.h"
using namespace std;
namespace WebCore {
class TextRunIterator {
public:
TextRunIterator()
: m_textRun(0)
, m_offset(0)
{
}
TextRunIterator(const TextRun* textRun, unsigned offset)
: m_textRun(textRun)
, m_offset(offset)
{
}
TextRunIterator(const TextRunIterator& other)
: m_textRun(other.m_textRun)
, m_offset(other.m_offset)
{
}
unsigned offset() const { return m_offset; }
void increment() { m_offset++; }
bool atEnd() const { return !m_textRun || m_offset >= m_textRun->length(); }
UChar current() const { return (*m_textRun)[m_offset]; }
WTF::Unicode::Direction direction() const { return atEnd() ? WTF::Unicode::OtherNeutral : WTF::Unicode::direction(current()); }
bool operator==(const TextRunIterator& other)
{
return m_offset == other.m_offset && m_textRun == other.m_textRun;
}
bool operator!=(const TextRunIterator& other) { return !operator==(other); }
private:
const TextRun* m_textRun;
int m_offset;
};
GraphicsContextPrivate* GraphicsContext::createGraphicsContextPrivate()
{
return new GraphicsContextPrivate;
}
void GraphicsContext::destroyGraphicsContextPrivate(GraphicsContextPrivate* deleteMe)
{
delete deleteMe;
}
void GraphicsContext::save()
{
if (paintingDisabled())
return;
m_common->stack.append(m_common->state);
savePlatformState();
}
void GraphicsContext::restore()
{
if (paintingDisabled())
return;
if (m_common->stack.isEmpty()) {
LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
return;
}
m_common->state = m_common->stack.last();
m_common->stack.removeLast();
restorePlatformState();
}
void GraphicsContext::drawRaisedEllipse(const FloatRect& rect, const Color& ellipseColor, ColorSpace ellipseColorSpace, const Color& shadowColor, ColorSpace shadowColorSpace)
{
if (paintingDisabled())
return;
save();
setStrokeColor(shadowColor, shadowColorSpace);
setFillColor(shadowColor, shadowColorSpace);
drawEllipse(FloatRect(rect.x(), rect.y() + 1.0f, rect.width(), rect.height()));
setStrokeColor(ellipseColor, ellipseColorSpace);
setFillColor(ellipseColor, ellipseColorSpace);
drawEllipse(rect);
restore();
}
void GraphicsContext::setStrokeThickness(float thickness)
{
m_common->state.strokeThickness = thickness;
setPlatformStrokeThickness(thickness);
}
void GraphicsContext::setStrokeStyle(const StrokeStyle& style)
{
m_common->state.strokeStyle = style;
setPlatformStrokeStyle(style);
}
void GraphicsContext::setStrokeColor(const Color& color, ColorSpace colorSpace)
{
m_common->state.strokeColor = color;
m_common->state.strokeColorSpace = colorSpace;
m_common->state.strokeGradient.clear();
m_common->state.strokePattern.clear();
setPlatformStrokeColor(color, colorSpace);
}
void GraphicsContext::setShadow(const IntSize& size, int blur, const Color& color, ColorSpace colorSpace)
{
m_common->state.shadowSize = size;
m_common->state.shadowBlur = blur;
m_common->state.shadowColor = color;
setPlatformShadow(size, blur, color, colorSpace);
}
void GraphicsContext::clearShadow()
{
m_common->state.shadowSize = IntSize();
m_common->state.shadowBlur = 0;
m_common->state.shadowColor = Color();
clearPlatformShadow();
}
bool GraphicsContext::getShadow(IntSize& size, int& blur, Color& color) const
{
size = m_common->state.shadowSize;
blur = m_common->state.shadowBlur;
color = m_common->state.shadowColor;
return color.isValid() && color.alpha() && (blur || size.width() || size.height());
}
float GraphicsContext::strokeThickness() const
{
return m_common->state.strokeThickness;
}
StrokeStyle GraphicsContext::strokeStyle() const
{
return m_common->state.strokeStyle;
}
Color GraphicsContext::strokeColor() const
{
return m_common->state.strokeColor;
}
ColorSpace GraphicsContext::strokeColorSpace() const
{
return m_common->state.strokeColorSpace;
}
WindRule GraphicsContext::fillRule() const
{
return m_common->state.fillRule;
}
void GraphicsContext::setFillRule(WindRule fillRule)
{
m_common->state.fillRule = fillRule;
}
void GraphicsContext::setFillColor(const Color& color, ColorSpace colorSpace)
{
m_common->state.fillColor = color;
m_common->state.fillColorSpace = colorSpace;
m_common->state.fillGradient.clear();
m_common->state.fillPattern.clear();
setPlatformFillColor(color, colorSpace);
}
Color GraphicsContext::fillColor() const
{
return m_common->state.fillColor;
}
ColorSpace GraphicsContext::fillColorSpace() const
{
return m_common->state.fillColorSpace;
}
void GraphicsContext::setShouldAntialias(bool b)
{
m_common->state.shouldAntialias = b;
setPlatformShouldAntialias(b);
}
bool GraphicsContext::shouldAntialias() const
{
return m_common->state.shouldAntialias;
}
void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
{
ASSERT(pattern);
if (!pattern) {
setStrokeColor(Color::black, DeviceColorSpace);
return;
}
m_common->state.strokeGradient.clear();
m_common->state.strokePattern = pattern;
setPlatformStrokePattern(m_common->state.strokePattern.get());
}
void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
{
ASSERT(pattern);
if (!pattern) {
setFillColor(Color::black, DeviceColorSpace);
return;
}
m_common->state.fillGradient.clear();
m_common->state.fillPattern = pattern;
setPlatformFillPattern(m_common->state.fillPattern.get());
}
void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
{
ASSERT(gradient);
if (!gradient) {
setStrokeColor(Color::black, DeviceColorSpace);
return;
}
m_common->state.strokeGradient = gradient;
m_common->state.strokePattern.clear();
setPlatformStrokeGradient(m_common->state.strokeGradient.get());
}
void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
{
ASSERT(gradient);
if (!gradient) {
setFillColor(Color::black, DeviceColorSpace);
return;
}
m_common->state.fillGradient = gradient;
m_common->state.fillPattern.clear();
setPlatformFillGradient(m_common->state.fillGradient.get());
}
Gradient* GraphicsContext::fillGradient() const
{
return m_common->state.fillGradient.get();
}
Gradient* GraphicsContext::strokeGradient() const
{
return m_common->state.strokeGradient.get();
}
Pattern* GraphicsContext::fillPattern() const
{
return m_common->state.fillPattern.get();
}
Pattern* GraphicsContext::strokePattern() const
{
return m_common->state.strokePattern.get();
}
void GraphicsContext::setShadowsIgnoreTransforms(bool ignoreTransforms)
{
m_common->state.shadowsIgnoreTransforms = ignoreTransforms;
}
bool GraphicsContext::updatingControlTints() const
{
return m_common->m_updatingControlTints;
}
void GraphicsContext::setUpdatingControlTints(bool b)
{
setPaintingDisabled(b);
m_common->m_updatingControlTints = b;
}
void GraphicsContext::setPaintingDisabled(bool f)
{
m_common->state.paintingDisabled = f;
}
bool GraphicsContext::paintingDisabled() const
{
return m_common->state.paintingDisabled;
}
void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& p, CompositeOperator op)
{
drawImage(image, styleColorSpace, p, IntRect(0, 0, -1, -1), op);
}
void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& r, CompositeOperator op, bool useLowQualityScale)
{
drawImage(image, styleColorSpace, r, IntRect(0, 0, -1, -1), op, useLowQualityScale);
}
void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntPoint& dest, const IntRect& srcRect, CompositeOperator op)
{
drawImage(image, styleColorSpace, IntRect(dest, srcRect.size()), srcRect, op);
}
void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, CompositeOperator op, bool useLowQualityScale)
{
drawImage(image, styleColorSpace, FloatRect(dest), srcRect, op, useLowQualityScale);
}
float GraphicsContext::drawText(const Font& font, const TextRun& run, const IntPoint& point, int from, int to)
{
if (paintingDisabled())
return 0.f;
return font.drawText(this, run, point, from, to);
}
float GraphicsContext::drawBidiText(const Font& font, const TextRun& run, const FloatPoint& point, BidiStatus* status, int length)
{
if (paintingDisabled())
return 0.f;
BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
WTF::Unicode::Direction paragraphDirection = run.ltr() ? WTF::Unicode::LeftToRight : WTF::Unicode::RightToLeft;
bidiResolver.setStatus(status ? *status : BidiStatus(paragraphDirection, paragraphDirection, paragraphDirection, BidiContext::create(run.ltr() ? 0 : 1, paragraphDirection, run.directionalOverride())));
bidiResolver.setPosition(TextRunIterator(&run, 0));
bidiResolver.createBidiRunsForLine(TextRunIterator(&run, length < 0 ? run.length() : length));
if (!bidiResolver.runCount())
return 0.f;
FloatPoint currPoint = point;
BidiCharacterRun* bidiRun = bidiResolver.firstRun();
while (bidiRun) {
TextRun subrun = run;
subrun.setText(run.data(bidiRun->start()), bidiRun->stop() - bidiRun->start());
subrun.setRTL(bidiRun->level() % 2);
subrun.setDirectionalOverride(bidiRun->dirOverride(false));
float width = font.drawText(this, subrun, currPoint);
currPoint.move(width, 0.f);
bidiRun = bidiRun->next();
}
if (status)
*status = bidiResolver.status();
bidiResolver.deleteRuns();
return currPoint.x() - static_cast<float>(point.x());
}
void GraphicsContext::drawHighlightForText(const Font& font, const TextRun& run, const IntPoint& point, int h, const Color& backgroundColor, ColorSpace colorSpace, int from, int to)
{
if (paintingDisabled())
return;
fillRect(font.selectionRectForText(run, point, h, from, to), backgroundColor, colorSpace);
}
void GraphicsContext::drawImage(Image* image, ColorSpace styleColorSpace, const FloatRect& dest, const FloatRect& src, CompositeOperator op, bool useLowQualityScale)
{
if (paintingDisabled() || !image)
return;
float tsw = src.width();
float tsh = src.height();
float tw = dest.width();
float th = dest.height();
if (tsw == -1)
tsw = image->width();
if (tsh == -1)
tsh = image->height();
if (tw == -1)
tw = image->width();
if (th == -1)
th = image->height();
if (useLowQualityScale) {
save();
setImageInterpolationQuality(InterpolationNone);
}
image->draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), styleColorSpace, op);
if (useLowQualityScale)
restore();
}
void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& rect, const IntPoint& srcPoint, const IntSize& tileSize, CompositeOperator op, bool useLowQualityScale)
{
if (paintingDisabled() || !image)
return;
if (useLowQualityScale) {
save();
setImageInterpolationQuality(InterpolationLow);
}
image->drawTiled(this, rect, srcPoint, tileSize, styleColorSpace, op);
if (useLowQualityScale)
restore();
}
void GraphicsContext::drawTiledImage(Image* image, ColorSpace styleColorSpace, const IntRect& dest, const IntRect& srcRect, Image::TileRule hRule, Image::TileRule vRule, CompositeOperator op, bool useLowQualityScale)
{
if (paintingDisabled() || !image)
return;
if (useLowQualityScale) {
save();
setImageInterpolationQuality(InterpolationLow);
}
if (hRule == Image::StretchTile && vRule == Image::StretchTile)
drawImage(image, styleColorSpace, dest, srcRect, op);
else
image->drawTiled(this, dest, srcRect, hRule, vRule, styleColorSpace, op);
if (useLowQualityScale)
restore();
}
void GraphicsContext::addRoundedRectClip(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight,
const IntSize& bottomLeft, const IntSize& bottomRight)
{
if (paintingDisabled())
return;
clip(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
}
void GraphicsContext::addRoundedRectClip(const FloatRect& rect, const FloatSize& topLeft, const FloatSize& topRight,
const FloatSize& bottomLeft, const FloatSize& bottomRight)
{
if (paintingDisabled())
return;
clip(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
}
void GraphicsContext::clipOutRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight,
const IntSize& bottomLeft, const IntSize& bottomRight)
{
if (paintingDisabled())
return;
clipOut(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
}
int GraphicsContext::textDrawingMode()
{
return m_common->state.textDrawingMode;
}
void GraphicsContext::setTextDrawingMode(int mode)
{
m_common->state.textDrawingMode = mode;
if (paintingDisabled())
return;
setPlatformTextDrawingMode(mode);
}
void GraphicsContext::fillRect(const FloatRect& rect, Generator& generator)
{
if (paintingDisabled())
return;
generator.fill(this, rect);
}
bool GraphicsContext::emojiDrawingEnabled()
{
return m_common->state.emojiDrawingEnabled;
}
void GraphicsContext::setEmojiDrawingEnabled(bool emojiDrawingEnabled)
{
m_common->state.emojiDrawingEnabled = emojiDrawingEnabled;
}
#if !PLATFORM(SKIA)
void GraphicsContext::setPlatformFillGradient(Gradient*)
{
}
void GraphicsContext::setPlatformFillPattern(Pattern*)
{
}
void GraphicsContext::setPlatformStrokeGradient(Gradient*)
{
}
void GraphicsContext::setPlatformStrokePattern(Pattern*)
{
}
#endif
#if !PLATFORM(CG) && !PLATFORM(SKIA)
void GraphicsContext::setPlatformTextDrawingMode(int mode)
{
}
#endif
#if !PLATFORM(QT) && !PLATFORM(CAIRO) && !PLATFORM(SKIA) && !PLATFORM(HAIKU) && !PLATFORM(OPENVG)
void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle&)
{
}
#endif
void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, const StrokeStyle& penStyle)
{
if (penStyle == DottedStroke || penStyle == DashedStroke) {
if (p1.x() == p2.x()) {
p1.setY(p1.y() + strokeWidth);
p2.setY(p2.y() - strokeWidth);
} else {
p1.setX(p1.x() + strokeWidth);
p2.setX(p2.x() - strokeWidth);
}
}
if (static_cast<int>(strokeWidth) % 2) { if (p1.x() == p2.x()) {
p1.setX(p1.x() + 0.5f);
p2.setX(p2.x() + 0.5f);
} else {
p1.setY(p1.y() + 0.5f);
p2.setY(p2.y() + 0.5f);
}
}
}
}