| 1 | // The 2D canvas interpreter: the `canvas` op's command list (docs/protocol.md, "Canvas commands") |
| 2 | // becomes CanvasOps a Backend draws. All state lives here (the transform, the state stack, |
| 3 | // styles, paths, Path2D and gradient objects, clips), so a backend only executes resolved |
| 4 | // geometry. Ported from twidget's WidgetCanvas.swift, whose CoreGraphics calls became this |
| 5 | // interpreter plus the backend interface. |
| 6 | #pragma once |
| 7 | |
| 8 | #include "backend.h" |
| 9 | #include "canvas_ops.h" |
| 10 | #include "twidget/json.h" |
| 11 | |
| 12 | #include <cstdint> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <unordered_map> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace twlite { |
| 19 | |
| 20 | /// What the interpreter asks of the renderer. |
| 21 | class CanvasHost { |
| 22 | public: |
| 23 | virtual ~CanvasHost() = default; |
| 24 | /// A backend font for a CSS `font` shorthand; kept by the host for the canvas's lifetime. |
| 25 | virtual int canvas_font(const std::string& css, FontMetrics& metrics) = 0; |
| 26 | virtual float canvas_text_width(int font, const std::string& text) = 0; |
| 27 | /// An image source `{n: id}`: a decoded <img> (`image`) or another canvas (`canvas`), with its size in px. |
| 28 | virtual bool canvas_source(int64_t node, int& image, int& canvas, float& w, float& h) = 0; |
| 29 | /// Pixels the canvas may still allocate, given every other canvas of the widget. |
| 30 | virtual int64_t canvas_allowance(int id) = 0; |
| 31 | }; |
| 32 | |
| 33 | class Canvas { |
| 34 | public: |
| 35 | static constexpr int MAX_SIDE = 8192; |
| 36 | static constexpr int64_t MAX_PIXELS = 4096 * 4096; |
| 37 | |
| 38 | Canvas(int id, int width, int height, CanvasHost& host); |
| 39 | |
| 40 | int id() const { return id_; } |
| 41 | int width() const { return width_; } |
| 42 | int height() const { return height_; } |
| 43 | int64_t pixels() const { return int64_t(width_) * height_; } |
| 44 | |
| 45 | /// One `canvas` op. `out` receives what the backend must run, in order. |
| 46 | void run(const twidget::Json& cmds, std::vector<CanvasOp>& out); |
| 47 | /// The bitmap size changed (`r`, or the element's width/height): cleared, state reset. |
| 48 | void resize(int width, int height, std::vector<CanvasOp>& out); |
| 49 | /// `canvasCall` queries that the state answers: isPointInPath, isPointInStroke, getTransform, getLineDash. |
| 50 | twidget::Json call(const std::string& method, const twidget::Json& args) const; |
| 51 | |
| 52 | private: |
| 53 | struct Matrix { |
| 54 | double a = 1, b = 0, c = 0, d = 1, e = 0, f = 0; |
| 55 | void apply(double x, double y, float& ox, float& oy) const { |
| 56 | ox = float(a * x + c * y + e); |
| 57 | oy = float(b * x + d * y + f); |
| 58 | } |
| 59 | Matrix then(const Matrix& m) const; // this, then m |
| 60 | bool invert(Matrix& out) const; |
| 61 | }; |
| 62 | struct Paint { |
| 63 | bool gradient = false; |
| 64 | Color color{0, 0, 0, 255}; |
| 65 | int id = 0; |
| 66 | }; |
| 67 | struct State { |
| 68 | Matrix matrix; |
| 69 | Paint fill, stroke; |
| 70 | std::string font = "10px sans-serif"; |
| 71 | std::string align = "start", baseline = "alphabetic"; |
| 72 | std::vector<float> dash; |
| 73 | float dash_offset = 0; |
| 74 | float line_width = 1; |
| 75 | int line_cap = 0, line_join = 0; |
| 76 | float miter_limit = 10; |
| 77 | float alpha = 1; |
| 78 | Composite composite = Composite::SourceOver; |
| 79 | Color shadow_color{0, 0, 0, 0}; |
| 80 | float shadow_blur = 0, shadow_x = 0, shadow_y = 0; |
| 81 | bool smoothing = true; |
| 82 | std::shared_ptr<const std::vector<CanvasClip>> clips; |
| 83 | }; |
| 84 | struct Gradient { |
| 85 | std::string kind; |
| 86 | std::vector<float> args; |
| 87 | std::vector<Stop> stops; |
| 88 | }; |
| 89 | /// A path being built: points already in canvas px (the current path) or in its own space (Path2D). |
| 90 | struct Builder { |
| 91 | Geometry geometry; |
| 92 | bool open = false; // a figure is open: segments append to it |
| 93 | bool has_point = false; // there is a current point; after closePath it is the closed figure's start |
| 94 | float sx = 0, sy = 0, cx = 0, cy = 0; // subpath start and current point, as stored |
| 95 | }; |
| 96 | struct Value; |
| 97 | |
| 98 | void call_method(const std::string& method, const std::vector<Value>& args, std::vector<CanvasOp>& out); |
| 99 | void path_method(Builder& path, const Matrix& m, const std::string& method, const std::vector<Value>& args) const; |
| 100 | void set(const std::string& property, const Value& value); |
| 101 | CanvasOp op(CanvasOp::Kind kind) const; |
| 102 | bool paint_of(const Paint& paint, CanvasPaint& out) const; |
| 103 | void fill(const Geometry& path, bool evenodd, std::vector<CanvasOp>& out); |
| 104 | void stroke(const Geometry& path, std::vector<CanvasOp>& out); |
| 105 | void text(const std::string& s, double x, double y, const Value* max_width, bool stroke, std::vector<CanvasOp>& out); |
| 106 | void image(const std::vector<Value>& args, std::vector<CanvasOp>& out); |
| 107 | Geometry transformed(const Geometry& path, const Matrix& m) const; |
| 108 | Geometry rect_geometry(double x, double y, double w, double h) const; |
| 109 | |
| 110 | int id_; |
| 111 | int width_, height_; |
| 112 | CanvasHost& host_; |
| 113 | State state_; |
| 114 | std::vector<State> stack_; |
| 115 | Builder path_; |
| 116 | std::unordered_map<int, Gradient> gradients_; |
| 117 | std::unordered_map<int, Builder> paths_; |
| 118 | }; |
| 119 | |
| 120 | /// Appends an SVG path (`d`) to a geometry, arcs as cubic béziers. Shared with Path2D. |
| 121 | void append_svg_path(const std::string& d, Geometry& out); |
| 122 | |
| 123 | } // namespace twlite |
| 124 | |