1/*
2 * Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "CanvasRenderingContext2DBase.h"
29#include "CanvasTextAlign.h"
30#include "CanvasTextBaseline.h"
31#include "FontCascade.h"
32#include "FontSelectorClient.h"
33#include "HTMLCanvasElement.h"
34#include <memory>
35
36namespace WebCore {
37
38class TextMetrics;
39
40class CanvasRenderingContext2D final : public CanvasRenderingContext2DBase {
41 WTF_MAKE_ISO_ALLOCATED(CanvasRenderingContext2D);
42public:
43 static std::unique_ptr<CanvasRenderingContext2D> create(CanvasBase&, bool usesCSSCompatibilityParseMode);
44
45 virtual ~CanvasRenderingContext2D();
46
47 HTMLCanvasElement& canvas() const { return downcast<HTMLCanvasElement>(canvasBase()); }
48
49 void drawFocusIfNeeded(Element&);
50 void drawFocusIfNeeded(Path2D&, Element&);
51
52 float webkitBackingStorePixelRatio() const { return 1; }
53
54 String font() const;
55 void setFont(const String&);
56
57 CanvasTextAlign textAlign() const;
58 void setTextAlign(CanvasTextAlign);
59
60 CanvasTextBaseline textBaseline() const;
61 void setTextBaseline(CanvasTextBaseline);
62
63 CanvasDirection direction() const;
64 void setDirection(CanvasDirection);
65
66 void fillText(const String& text, float x, float y, Optional<float> maxWidth = WTF::nullopt);
67 void strokeText(const String& text, float x, float y, Optional<float> maxWidth = WTF::nullopt);
68 Ref<TextMetrics> measureText(const String& text);
69
70 bool is2d() const override { return true; }
71
72private:
73 CanvasRenderingContext2D(CanvasBase&, bool usesCSSCompatibilityParseMode);
74
75 // The relationship between FontCascade and CanvasRenderingContext2D::FontProxy must hold certain invariants.
76 // Therefore, all font operations must pass through the State.
77 const FontProxy& fontProxy();
78
79 void drawTextInternal(const String& text, float x, float y, bool fill, Optional<float> maxWidth = WTF::nullopt);
80
81 void drawFocusIfNeededInternal(const Path&, Element&);
82
83 TextDirection toTextDirection(CanvasRenderingContext2DBase::Direction, const RenderStyle** computedStyle = nullptr) const;
84
85 FloatPoint textOffset(float width, TextDirection);
86};
87
88} // namespace WebCore
89
90SPECIALIZE_TYPE_TRAITS_CANVASRENDERINGCONTEXT(WebCore::CanvasRenderingContext2D, is2d())
91