OffscreenCanvasRenderingContext2D.cpp [plain text]
#include "config.h"
#include "OffscreenCanvasRenderingContext2D.h"
#if ENABLE(OFFSCREEN_CANVAS)
#include "CSSFontSelector.h"
#include "CSSParser.h"
#include "CSSPropertyParserHelpers.h"
#include "Document.h"
#include "RenderStyle.h"
#include "StyleResolveForFontRaw.h"
#include "TextMetrics.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(OffscreenCanvasRenderingContext2D);
OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D(CanvasBase& canvas)
: CanvasRenderingContext2DBase(canvas, false)
{
}
OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() = default;
void OffscreenCanvasRenderingContext2D::commit()
{
downcast<OffscreenCanvas>(canvasBase()).commitToPlaceholderCanvas();
}
void OffscreenCanvasRenderingContext2D::setFont(const String& newFont)
{
auto& context = *canvasBase().scriptExecutionContext();
if (!is<Document>(context))
return;
if (newFont.isEmpty())
return;
if (newFont == state().unparsedFont && state().font.realized())
return;
auto fontRaw = CSSParser::parseFontWorkerSafe(newFont, strictToCSSParserMode(!m_usesCSSCompatibilityParseMode));
if (!fontRaw)
return;
String newFontSafeCopy(newFont); realizeSaves();
modifiableState().unparsedFont = newFontSafeCopy;
FontCascadeDescription fontDescription;
fontDescription.setOneFamily(DefaultFontFamily);
fontDescription.setSpecifiedSize(DefaultFontSize);
fontDescription.setComputedSize(DefaultFontSize);
auto& document = downcast<Document>(context);
auto fontStyle = Style::resolveForFontRaw(*fontRaw, WTFMove(fontDescription), document);
if (fontStyle)
modifiableState().font.initialize(document.fontSelector(), *fontStyle);
}
CanvasDirection OffscreenCanvasRenderingContext2D::direction() const
{
switch (state().direction) {
case Direction::Inherit:
case Direction::Ltr:
return Direction::Ltr;
case Direction::Rtl:
return Direction::Rtl;
}
ASSERT_NOT_REACHED();
return Direction::Ltr;
}
auto OffscreenCanvasRenderingContext2D::fontProxy() -> const FontProxy* {
if (!state().font.realized())
setFont(state().unparsedFont);
return &state().font;
}
void OffscreenCanvasRenderingContext2D::fillText(const String& text, float x, float y, Optional<float> maxWidth)
{
drawText(text, x, y, true, maxWidth);
}
void OffscreenCanvasRenderingContext2D::strokeText(const String& text, float x, float y, Optional<float> maxWidth)
{
drawText(text, x, y, false, maxWidth);
}
Ref<TextMetrics> OffscreenCanvasRenderingContext2D::measureText(const String& text)
{
return measureTextInternal(text);
}
}
#endif