| 1 | // What the platform provides to the litehtml container: fonts with metrics, |
| 2 | // text measurement and image decoding. Everything else the container needs |
| 3 | // (painting) goes through the DisplayList. One implementation per platform: |
| 4 | // CoreText in src/apple/coregraphics.cpp, JNI callbacks into |
| 5 | // LitehtmlBackend.kt in src/android/jni.cpp. All calls on the main thread. |
| 6 | #pragma once |
| 7 | |
| 8 | #include "canvas_ops.h" |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | namespace twlite { |
| 15 | |
| 16 | struct FontMetrics { |
| 17 | float ascent = 0; // baseline → top of the line box |
| 18 | float descent = 0; // baseline → bottom |
| 19 | float height = 0; // line height the font asks for (ascent + descent + leading) |
| 20 | float x_height = 0; |
| 21 | float zero_width = 0; // advance of "0" (the `ch` unit) |
| 22 | }; |
| 23 | |
| 24 | class Backend { |
| 25 | public: |
| 26 | virtual ~Backend() = default; |
| 27 | /// `families`: the CSS `font-family` list as written (quotes kept, comma separated); the |
| 28 | /// backend resolves generics (`-apple-system`, `system-ui`, `sans-serif`, …) and falls back |
| 29 | /// to its default face. Returns a handle > 0, or 0 when no font at all could be made. |
| 30 | virtual int create_font(const std::string& families, float size_px, int weight, bool italic, FontMetrics& out) = 0; |
| 31 | virtual void delete_font(int font) = 0; |
| 32 | /// Advance width of `text` (UTF-8) in CSS px. |
| 33 | virtual float text_width(int font, const std::string& text) = 0; |
| 34 | /// Decodes a `data:` image (anything else is refused by the container before this) and keeps it |
| 35 | /// under `id` for the painter; `w`/`h` get the intrinsic size in CSS px. False when undecodable. |
| 36 | virtual bool load_image(int id, const std::string& url, float& w, float& h) = 0; |
| 37 | |
| 38 | // ── 2D canvas: optional. Without it a <canvas> stays transparent and reads back as zeros. ── |
| 39 | /// Runs `ops` on the w × h px bitmap of canvas `id`, created on the first call; a Reset op |
| 40 | /// reallocates it. The painter draws it wherever a display list Image names `canvas = id`. |
| 41 | virtual void canvas_run(int /*id*/, int /*w*/, int /*h*/, const std::vector<CanvasOp>& /*ops*/) {} |
| 42 | /// Straight-alpha RGBA of a region; pixels outside the bitmap are transparent. False when unsupported. |
| 43 | virtual bool canvas_read(int /*id*/, int /*x*/, int /*y*/, int /*w*/, int /*h*/, std::vector<uint8_t>& /*rgba*/) { return false; } |
| 44 | virtual void canvas_release(int /*id*/) {} |
| 45 | }; |
| 46 | |
| 47 | } // namespace twlite |
| 48 | |