CanvasRenderingContext2D.cpp [plain text]
#include "config.h"
#include "CanvasRenderingContext2D.h"
#include "CSSFontSelector.h"
#include "CSSParser.h"
#include "CSSPropertyNames.h"
#include "CSSPropertyParserHelpers.h"
#include "Gradient.h"
#include "ImageBuffer.h"
#include "ImageData.h"
#include "InspectorInstrumentation.h"
#include "NodeRenderStyle.h"
#include "Path2D.h"
#include "RenderTheme.h"
#include "ResourceLoadObserver.h"
#include "RuntimeEnabledFeatures.h"
#include "Settings.h"
#include "StyleBuilder.h"
#include "StyleFontSizeFunctions.h"
#include "StyleProperties.h"
#include "StyleResolveForFontRaw.h"
#include "TextMetrics.h"
#include "TextRun.h"
#include <wtf/CheckedArithmetic.h>
#include <wtf/IsoMallocInlines.h>
#include <wtf/MathExtras.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace HTMLNames;
WTF_MAKE_ISO_ALLOCATED_IMPL(CanvasRenderingContext2D);
std::unique_ptr<CanvasRenderingContext2D> CanvasRenderingContext2D::create(CanvasBase& canvas, bool usesCSSCompatibilityParseMode)
{
auto renderingContext = std::unique_ptr<CanvasRenderingContext2D>(new CanvasRenderingContext2D(canvas, usesCSSCompatibilityParseMode));
InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
return renderingContext;
}
CanvasRenderingContext2D::CanvasRenderingContext2D(CanvasBase& canvas, bool usesCSSCompatibilityParseMode)
: CanvasRenderingContext2DBase(canvas, usesCSSCompatibilityParseMode)
{
}
CanvasRenderingContext2D::~CanvasRenderingContext2D() = default;
void CanvasRenderingContext2D::drawFocusIfNeeded(Element& element)
{
drawFocusIfNeededInternal(m_path, element);
}
void CanvasRenderingContext2D::drawFocusIfNeeded(Path2D& path, Element& element)
{
drawFocusIfNeededInternal(path.path(), element);
}
void CanvasRenderingContext2D::drawFocusIfNeededInternal(const Path& path, Element& element)
{
auto* context = drawingContext();
if (!element.focused() || !state().hasInvertibleTransform || path.isEmpty() || !element.isDescendantOf(canvas()) || !context)
return;
context->drawFocusRing(path, 1, 1, RenderTheme::singleton().focusRingColor(element.document().styleColorOptions(canvas().computedStyle())));
}
void CanvasRenderingContext2D::setFont(const String& newFont)
{
if (newFont.isEmpty())
return;
if (newFont == state().unparsedFont && state().font.realized())
return;
auto fontRaw = CSSParser::parseFontWorkerSafe(newFont, strictToCSSParserMode(!m_usesCSSCompatibilityParseMode));
if (!fontRaw)
return;
Document& document = canvas().document();
document.updateStyleIfNeeded();
FontCascadeDescription fontDescription;
if (auto* computedStyle = canvas().computedStyle())
fontDescription = FontCascadeDescription { computedStyle->fontDescription() };
else {
fontDescription.setOneFamily(DefaultFontFamily);
fontDescription.setSpecifiedSize(DefaultFontSize);
fontDescription.setComputedSize(DefaultFontSize);
}
auto fontStyle = Style::resolveForFontRaw(*fontRaw, WTFMove(fontDescription), document);
if (!fontStyle)
return;
String newFontSafeCopy(newFont); realizeSaves();
modifiableState().unparsedFont = newFontSafeCopy;
modifiableState().font.initialize(document.fontSelector(), *fontStyle);
ASSERT(state().font.realized());
ASSERT(state().font.isPopulated());
}
inline TextDirection CanvasRenderingContext2D::toTextDirection(Direction direction, const RenderStyle** computedStyle) const
{
auto* style = (computedStyle || direction == Direction::Inherit) ? canvas().computedStyle() : nullptr;
if (computedStyle)
*computedStyle = style;
switch (direction) {
case Direction::Inherit:
return style ? style->direction() : TextDirection::LTR;
case Direction::Rtl:
return TextDirection::RTL;
case Direction::Ltr:
return TextDirection::LTR;
}
ASSERT_NOT_REACHED();
return TextDirection::LTR;
}
CanvasDirection CanvasRenderingContext2D::direction() const
{
if (state().direction == Direction::Inherit)
canvas().document().updateStyleIfNeeded();
return toTextDirection(state().direction) == TextDirection::RTL ? CanvasDirection::Rtl : CanvasDirection::Ltr;
}
void CanvasRenderingContext2D::fillText(const String& text, float x, float y, Optional<float> maxWidth)
{
drawTextInternal(text, x, y, true, maxWidth);
}
void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, Optional<float> maxWidth)
{
drawTextInternal(text, x, y, false, maxWidth);
}
Ref<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
{
if (RuntimeEnabledFeatures::sharedFeatures().webAPIStatisticsEnabled()) {
auto& canvas = this->canvas();
ResourceLoadObserver::shared().logCanvasWriteOrMeasure(canvas.document(), text);
ResourceLoadObserver::shared().logCanvasRead(canvas.document());
}
Ref<TextMetrics> metrics = TextMetrics::create();
String normalizedText = text;
normalizeSpaces(normalizedText);
const RenderStyle* computedStyle;
auto direction = toTextDirection(state().direction, &computedStyle);
bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : false;
TextRun textRun(normalizedText, 0, 0, AllowRightExpansion, direction, override, true);
return measureTextInternal(textRun);
}
auto CanvasRenderingContext2D::fontProxy() -> const FontProxy* {
auto& canvas = downcast<HTMLCanvasElement>(canvasBase());
canvas.document().updateStyleIfNeeded();
if (!state().font.realized())
setFont(state().unparsedFont);
return &state().font;
}
void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, float y, bool fill, Optional<float> maxWidth)
{
if (RuntimeEnabledFeatures::sharedFeatures().webAPIStatisticsEnabled())
ResourceLoadObserver::shared().logCanvasWriteOrMeasure(this->canvas().document(), text);
if (!canDrawTextWithParams(x, y, fill, maxWidth))
return;
String normalizedText = text;
normalizeSpaces(normalizedText);
const RenderStyle* computedStyle;
auto direction = toTextDirection(state().direction, &computedStyle);
bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : false;
TextRun textRun(normalizedText, 0, 0, AllowRightExpansion, direction, override, true);
drawTextUnchecked(textRun, x, y, fill, maxWidth);
}
}