📄 canvasrenderingcontext2d.cpp
字号:
void CanvasRenderingContext2D::setFillColor(const String& color){ setFillStyle(CanvasStyle::create(color));}void CanvasRenderingContext2D::setFillColor(float grayLevel){ setFillStyle(CanvasStyle::create(grayLevel, 1));}void CanvasRenderingContext2D::setFillColor(const String& color, float alpha){ setFillStyle(CanvasStyle::create(color, alpha));}void CanvasRenderingContext2D::setFillColor(float grayLevel, float alpha){ setFillStyle(CanvasStyle::create(grayLevel, alpha));}void CanvasRenderingContext2D::setFillColor(float r, float g, float b, float a){ setFillStyle(CanvasStyle::create(r, g, b, a));}void CanvasRenderingContext2D::setFillColor(float c, float m, float y, float k, float a){ setFillStyle(CanvasStyle::create(c, m, y, k, a));}void CanvasRenderingContext2D::beginPath(){ m_path.clear();}void CanvasRenderingContext2D::closePath(){ m_path.closeSubpath();}void CanvasRenderingContext2D::moveTo(float x, float y){ if (!isfinite(x) | !isfinite(y)) return; if (!state().m_invertibleCTM) return; m_path.moveTo(FloatPoint(x, y));}void CanvasRenderingContext2D::lineTo(float x, float y){ if (!isfinite(x) | !isfinite(y)) return; if (!state().m_invertibleCTM) return; m_path.addLineTo(FloatPoint(x, y));}void CanvasRenderingContext2D::quadraticCurveTo(float cpx, float cpy, float x, float y){ if (!isfinite(cpx) | !isfinite(cpy) | !isfinite(x) | !isfinite(y)) return; if (!state().m_invertibleCTM) return; m_path.addQuadCurveTo(FloatPoint(cpx, cpy), FloatPoint(x, y));}void CanvasRenderingContext2D::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y){ if (!isfinite(cp1x) | !isfinite(cp1y) | !isfinite(cp2x) | !isfinite(cp2y) | !isfinite(x) | !isfinite(y)) return; if (!state().m_invertibleCTM) return; m_path.addBezierCurveTo(FloatPoint(cp1x, cp1y), FloatPoint(cp2x, cp2y), FloatPoint(x, y));}void CanvasRenderingContext2D::arcTo(float x0, float y0, float x1, float y1, float r, ExceptionCode& ec){ ec = 0; if (!isfinite(x0) | !isfinite(y0) | !isfinite(x1) | !isfinite(y1) | !isfinite(r)) return; if (r < 0) { ec = INDEX_SIZE_ERR; return; } if (!state().m_invertibleCTM) return; m_path.addArcTo(FloatPoint(x0, y0), FloatPoint(x1, y1), r);}void CanvasRenderingContext2D::arc(float x, float y, float r, float sa, float ea, bool anticlockwise, ExceptionCode& ec){ ec = 0; if (!isfinite(x) | !isfinite(y) | !isfinite(r) | !isfinite(sa) | !isfinite(ea)) return; if (r < 0) { ec = INDEX_SIZE_ERR; return; } if (!state().m_invertibleCTM) return; m_path.addArc(FloatPoint(x, y), r, sa, ea, anticlockwise);} static bool validateRectForCanvas(float& x, float& y, float& width, float& height){ if (!isfinite(x) | !isfinite(y) | !isfinite(width) | !isfinite(height)) return false; if (width < 0) { width = -width; x -= width; } if (height < 0) { height = -height; y -= height; } return true;}void CanvasRenderingContext2D::rect(float x, float y, float width, float height){ if (!validateRectForCanvas(x, y, width, height)) return; if (!state().m_invertibleCTM) return; m_path.addRect(FloatRect(x, y, width, height));}#if ENABLE(DASHBOARD_SUPPORT)void CanvasRenderingContext2D::clearPathForDashboardBackwardCompatibilityMode(){ if (Settings* settings = m_canvas->document()->settings()) if (settings->usesDashboardBackwardCompatibilityMode()) m_path.clear();}#endifvoid CanvasRenderingContext2D::fill(){ GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; if (!m_path.isEmpty()) { c->beginPath(); c->addPath(m_path); willDraw(m_path.boundingRect()); c->fillPath(); }#if ENABLE(DASHBOARD_SUPPORT) clearPathForDashboardBackwardCompatibilityMode();#endif}void CanvasRenderingContext2D::stroke(){ GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; if (!m_path.isEmpty()) { c->beginPath(); c->addPath(m_path); CanvasStrokeStyleApplier strokeApplier(this); FloatRect boundingRect = m_path.strokeBoundingRect(&strokeApplier); willDraw(boundingRect); c->strokePath(); }#if ENABLE(DASHBOARD_SUPPORT) clearPathForDashboardBackwardCompatibilityMode();#endif}void CanvasRenderingContext2D::clip(){ GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; c->clip(m_path);#if ENABLE(DASHBOARD_SUPPORT) clearPathForDashboardBackwardCompatibilityMode();#endif}bool CanvasRenderingContext2D::isPointInPath(const float x, const float y){ GraphicsContext* c = drawingContext(); if (!c) return false; if (!state().m_invertibleCTM) return false; FloatPoint point(x, y); TransformationMatrix ctm = state().m_transform; FloatPoint transformedPoint = ctm.inverse().mapPoint(point); return m_path.contains(transformedPoint);}void CanvasRenderingContext2D::clearRect(float x, float y, float width, float height){ if (!validateRectForCanvas(x, y, width, height)) return; GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; FloatRect rect(x, y, width, height); willDraw(rect); c->clearRect(rect);}void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height){ if (!validateRectForCanvas(x, y, width, height)) return; GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; FloatRect rect(x, y, width, height); willDraw(rect); c->save(); c->fillRect(rect); c->restore();}void CanvasRenderingContext2D::strokeRect(float x, float y, float width, float height){ if (!validateRectForCanvas(x, y, width, height)) return; strokeRect(x, y, width, height, state().m_lineWidth);}void CanvasRenderingContext2D::strokeRect(float x, float y, float width, float height, float lineWidth){ if (!validateRectForCanvas(x, y, width, height)) return; if (!(lineWidth >= 0)) return; GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; FloatRect rect(x, y, width, height); FloatRect boundingRect = rect; boundingRect.inflate(lineWidth / 2); willDraw(boundingRect); c->strokeRect(rect, lineWidth);}#if PLATFORM(CG)static inline CGSize adjustedShadowSize(CGFloat width, CGFloat height){ // Work around <rdar://problem/5539388> by ensuring that shadow offsets will get truncated // to the desired integer. static const CGFloat extraShadowOffset = narrowPrecisionToCGFloat(1.0 / 128); if (width > 0) width += extraShadowOffset; else if (width < 0) width -= extraShadowOffset; if (height > 0) height += extraShadowOffset; else if (height < 0) height -= extraShadowOffset; return CGSizeMake(width, height);}#endifvoid CanvasRenderingContext2D::setShadow(float width, float height, float blur){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; applyShadow();}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = color; applyShadow();}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float grayLevel){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; GraphicsContext* c = drawingContext(); if (!c) return; RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f); c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba));}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color, float alpha){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = color; GraphicsContext* c = drawingContext(); if (!c) return; RGBA32 rgba = 0; // default is transparent black if (!state().m_shadowColor.isEmpty()) CSSParser::parseColor(rgba, state().m_shadowColor); c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(colorWithOverrideAlpha(rgba, alpha)));}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float grayLevel, float alpha){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; GraphicsContext* c = drawingContext(); if (!c) return; RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha); c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba));}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float r, float g, float b, float a){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; GraphicsContext* c = drawingContext(); if (!c) return; RGBA32 rgba = makeRGBA32FromFloats(r, g, b, a); // default is transparent black if (!state().m_shadowColor.isEmpty()) CSSParser::parseColor(rgba, state().m_shadowColor); c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba));}void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float c, float m, float y, float k, float a){ state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; state().m_shadowColor = ""; GraphicsContext* dc = drawingContext(); if (!dc) return;#if PLATFORM(CG) const CGFloat components[5] = { c, m, y, k, a }; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceCMYK(); CGColorRef shadowColor = CGColorCreate(colorSpace, components); CGColorSpaceRelease(colorSpace); CGContextSetShadowWithColor(dc->platformContext(), adjustedShadowSize(width, -height), blur, shadowColor); CGColorRelease(shadowColor);#else dc->setShadow(IntSize(width, -height), blur, Color(c, m, y, k, a));#endif}void CanvasRenderingContext2D::clearShadow(){ state().m_shadowOffset = FloatSize(); state().m_shadowBlur = 0; state().m_shadowColor = ""; applyShadow();}void CanvasRenderingContext2D::applyShadow(){ GraphicsContext* c = drawingContext(); if (!c) return; RGBA32 rgba = 0; // default is transparent black if (!state().m_shadowColor.isEmpty()) CSSParser::parseColor(rgba, state().m_shadowColor); float width = state().m_shadowOffset.width(); float height = state().m_shadowOffset.height(); c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba));}static IntSize size(HTMLImageElement* image){ if (CachedImage* cachedImage = image->cachedImage()) return cachedImage->imageSize(1.0f); // FIXME: Not sure about this. return IntSize();}static inline FloatRect normalizeRect(const FloatRect& rect){ return FloatRect(min(rect.x(), rect.right()), min(rect.y(), rect.bottom()), max(rect.width(), -rect.width()), max(rect.height(), -rect.height()));}void CanvasRenderingContext2D::drawImage(HTMLImageElement* image, float x, float y){ ASSERT(image); IntSize s = size(image); ExceptionCode ec; drawImage(image, x, y, s.width(), s.height(), ec);}void CanvasRenderingContext2D::drawImage(HTMLImageElement* image, float x, float y, float width, float height, ExceptionCode& ec){ ASSERT(image); IntSize s = size(image); drawImage(image, FloatRect(0, 0, s.width(), s.height()), FloatRect(x, y, width, height), ec);}void CanvasRenderingContext2D::checkOrigin(const KURL& url){ RefPtr<SecurityOrigin> origin = SecurityOrigin::create(url); if (!m_canvas->document()->securityOrigin()->canAccess(origin.get())) m_canvas->setOriginTainted();}void CanvasRenderingContext2D::drawImage(HTMLImageElement* image, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode& ec){ ASSERT(image); ec = 0; FloatRect imageRect = FloatRect(FloatPoint(), size(image)); if (!imageRect.contains(normalizeRect(srcRect)) || srcRect.width() == 0 || srcRect.height() == 0) { ec = INDEX_SIZE_ERR; return; } if (!dstRect.width() || !dstRect.height()) return; GraphicsContext* c = drawingContext(); if (!c) return; if (!state().m_invertibleCTM) return; CachedImage* cachedImage = image->cachedImage(); if (!cachedImage) return; if (m_canvas->originClean()) checkOrigin(cachedImage->response().url()); if (m_canvas->originClean() && !cachedImage->image()->hasSingleSecurityOrigin()) m_canvas->setOriginTainted(); FloatRect sourceRect = c->roundToDevicePixels(srcRect); FloatRect destRect = c->roundToDevicePixels(dstRect); willDraw(destRect);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -