GraphicsContextCGWin.cpp [plain text]
#include "config.h"
#include "GraphicsContextCG.h"
#include "AffineTransform.h"
#include "GraphicsContextPlatformPrivateCG.h"
#include "Path.h"
#include <CoreGraphics/CGBitmapContext.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include <wtf/win/GDIObject.h>
using namespace std;
namespace WebCore {
static CGContextRef CGContextWithHDC(HDC hdc, bool hasAlpha)
{
HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
DIBPixelData pixelData(bitmap);
if (pixelData.bitsPerPixel() != 32)
return 0;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | (hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst);
CGContextRef context = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), bitmapInfo);
CGContextTranslateCTM(context, 0, pixelData.size().height());
CGContextScaleCTM(context, 1, -1);
SetGraphicsMode(hdc, GM_ADVANCED);
return context;
}
GraphicsContext::GraphicsContext(HDC hdc, bool hasAlpha)
{
platformInit(hdc, hasAlpha);
}
void GraphicsContext::platformInit(HDC hdc, bool hasAlpha)
{
if (!hdc)
return;
m_data = new GraphicsContextPlatformPrivate(CGContextWithHDC(hdc, hasAlpha));
CGContextRelease(m_data->m_cgContext.get());
m_data->m_hdc = hdc;
if (m_data->m_cgContext) {
setPlatformFillColor(fillColor());
setPlatformStrokeColor(strokeColor());
}
}
void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
{
bool createdBitmap = mayCreateBitmap && (!m_data->m_hdc || isInTransparencyLayer());
if (!createdBitmap) {
m_data->restore();
return;
}
if (dstRect.isEmpty())
return;
auto bitmap = adoptGDIObject(static_cast<HBITMAP>(::GetCurrentObject(hdc, OBJ_BITMAP)));
DIBPixelData pixelData(bitmap.get());
ASSERT(pixelData.bitsPerPixel() == 32);
CGContextRef bitmapContext = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little |
(supportAlphaBlend ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst));
CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
CGContextDrawImage(m_data->m_cgContext.get(), dstRect, image);
CGImageRelease(image);
CGContextRelease(bitmapContext);
::DeleteDC(hdc);
}
void GraphicsContext::drawWindowsBitmap(WindowsBitmap* image, const IntPoint& point)
{
RetainPtr<CFDataRef> imageData = adoptCF(CFDataCreate(kCFAllocatorDefault, image->buffer(), image->bufferLength()));
RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateWithCFData(imageData.get()));
RetainPtr<CGImageRef> cgImage = adoptCF(CGImageCreate(image->size().width(), image->size().height(), 8, 32, image->bytesPerRow(), deviceRGBColorSpaceRef(),
kCGBitmapByteOrder32Little | kCGImageAlphaFirst, dataProvider.get(), 0, true, kCGRenderingIntentDefault));
CGContextDrawImage(m_data->m_cgContext.get(), CGRectMake(point.x(), point.y(), image->size().width(), image->size().height()), cgImage.get());
}
void GraphicsContext::drawFocusRing(const Path& path, float width, float offset, const Color& color)
{
}
void GraphicsContext::drawFocusRing(const Vector<FloatRect>& rects, float width, float offset, const Color& color)
{
if (paintingDisabled())
return;
float radius = (width - 1) / 2.0f;
offset += radius;
CGColorRef colorRef = color.isValid() ? cachedCGColor(color) : nullptr;
CGMutablePathRef focusRingPath = CGPathCreateMutable();
unsigned rectCount = rects.size();
for (unsigned i = 0; i < rectCount; i++)
CGPathAddRect(focusRingPath, 0, CGRectInset(rects[i], -offset, -offset));
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextAddPath(context, focusRingPath);
wkDrawFocusRing(context, colorRef, radius);
CGPathRelease(focusRingPath);
CGContextRestoreGState(context);
}
static void setCGStrokeColor(CGContextRef context, const Color& color)
{
CGFloat red, green, blue, alpha;
color.getRGBA(red, green, blue, alpha);
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
}
static const Color& spellingPatternColor() {
static const Color spellingColor(255, 0, 0);
return spellingColor;
}
static const Color& grammarPatternColor() {
static const Color grammarColor(0, 128, 0);
return grammarColor;
}
void GraphicsContext::updateDocumentMarkerResources()
{
}
void GraphicsContext::drawLineForDocumentMarker(const FloatPoint& point, float width, DocumentMarkerLineStyle style)
{
if (paintingDisabled())
return;
if (style != DocumentMarkerSpellingLineStyle && style != DocumentMarkerGrammarLineStyle)
return;
const int patternHeight = 3; ASSERT(cMisspellingLineThickness == patternHeight);
const int patternWidth = 4; ASSERT(patternWidth == cMisspellingLinePatternWidth);
float widthMod = fmodf(width, patternWidth);
if (patternWidth - widthMod > cMisspellingLinePatternGapWidth)
width -= widthMod;
CGContextRef context = platformContext();
CGContextSaveGState(context);
const Color& patternColor = style == DocumentMarkerGrammarLineStyle ? grammarPatternColor() : spellingPatternColor();
setCGStrokeColor(context, patternColor);
wkSetPatternPhaseInUserSpace(context, point);
CGContextSetBlendMode(context, kCGBlendModeNormal);
const CGPoint upperPoints [] = {{point.x(), point.y() + patternHeight - 2.5 }, {point.x() + width, point.y() + patternHeight - 2.5}};
const CGPoint middlePoints [] = {{point.x(), point.y() + patternHeight - 1.5 }, {point.x() + width, point.y() + patternHeight - 1.5}};
const CGPoint lowerPoints [] = {{point.x(), point.y() + patternHeight - 0.5 }, {point.x() + width, point.y() + patternHeight - 0.5 }};
static const CGFloat edge_dash_lengths[] = {2.0f, 2.0f};
static const CGFloat middle_dash_lengths[] = { 2.76f, 1.24f };
static const CGFloat edge_offset = -(edge_dash_lengths[1] - 1.0f) / 2.0f;
static const CGFloat middle_offset = -(middle_dash_lengths[1] - 1.0f) / 2.0f;
const float upperOpacity = 0.33f;
const float middleOpacity = 0.75f;
const float lowerOpacity = 0.88f;
CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
CGContextSetAlpha(context, upperOpacity);
CGContextStrokeLineSegments(context, upperPoints, 2);
CGContextSetLineDash(context, middle_offset, middle_dash_lengths, WTF_ARRAY_LENGTH(middle_dash_lengths));
CGContextSetAlpha(context, middleOpacity);
CGContextStrokeLineSegments(context, middlePoints, 2);
CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
CGContextSetAlpha(context, lowerOpacity);
CGContextStrokeLineSegments(context, lowerPoints, 2);
CGContextRestoreGState(context);
}
void GraphicsContextPlatformPrivate::flush()
{
CGContextFlush(m_cgContext.get());
}
}