| 1 | // twidget experiment: render a static HTML file through litehtml with the |
| 2 | // repository's cairo+pango container (no GTK), at a given CSS width, and write |
| 3 | // a PNG. Prints stage timings as JSON. Usage: twidget_litehtml <in.html> <width> <out.png> |
| 4 | // Build (litehtml checkout at $LH, built with cmake; pango/cairo from Homebrew): |
| 5 | // c++ -std=c++20 -O2 -o twidget_litehtml twidget_litehtml.cpp $LH/containers/cairo/{container_cairo,container_cairo_pango,cairo_borders,conic_gradient}.cpp \ |
| 6 | // -I$LH/include -I$LH/include/litehtml -I$LH/containers/cairo $(pkg-config --cflags --libs pangocairo cairo cairo-png fontconfig) \ |
| 7 | // $LH/build/liblitehtml.a $LH/build/src/gumbo/libgumbo.a |
| 8 | // Run with FONTCONFIG_FILE=scripts/blitz-experiment/fonts.conf so -apple-system resolves to SF Pro like in Chromium. |
| 9 | #include <litehtml.h> |
| 10 | #include "container_cairo_pango.h" |
| 11 | #include "cairo_images_cache.h" |
| 12 | #include <cairo.h> |
| 13 | #include <chrono> |
| 14 | #include <cmath> |
| 15 | #include <cstdio> |
| 16 | #include <fstream> |
| 17 | #include <sstream> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | static std::vector<unsigned char> b64decode(const std::string& in) { |
| 22 | static int T[256]; static bool init = false; |
| 23 | if (!init) { for (int i = 0; i < 256; i++) T[i] = -1; const char* a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 24 | std::vector<unsigned char> out; int val = 0, bits = -8; |
| 25 | for (unsigned char c : in) { if (T[c] == -1) continue; val = (val << 6) + T[c]; bits += 6; if (bits >= |
| 26 | return out; |
| 27 | } |
| 28 | struct mem_reader { const std::vector<unsigned char>* data; size_t pos; }; |
| 29 | static cairo_status_t read_mem(void* closure, unsigned char* out, unsigned int len) { |
| 30 | auto* r = (mem_reader*)closure; if (r->pos + len > r->data->size()) return CAIRO_STATUS_READ_ERROR; |
| 31 | memcpy(out, r->data->data() + r->pos, len); r->pos += len; return CAIRO_STATUS_SUCCESS; |
| 32 | } |
| 33 | |
| 34 | class container : public container_cairo_pango { |
| 35 | int m_w, m_h; cairo_images_cache m_images; |
| 36 | public: |
| 37 | container(int w, int h) : m_w(w), m_h(h) {} |
| 38 | void load_image(const char*, const char*, bool) override {} |
| 39 | void set_caption(const char*) override {} |
| 40 | void set_base_url(const char*) override {} |
| 41 | void on_anchor_click(const char*, const litehtml::element::ptr&) override {} |
| 42 | void on_mouse_event(const litehtml::element::ptr&, litehtml::mouse_event) override {} |
| 43 | void set_cursor(const char*) override {} |
| 44 | void import_css(std::string& text, const std::string&, std::string&) override { text.clear(); } |
| 45 | double get_screen_dpi() const override { return 96; } |
| 46 | int get_screen_width() const override { return m_w; } |
| 47 | int get_screen_height() const override { return m_h; } |
| 48 | void get_viewport(litehtml::position& v) const override { v.x = 0; v.y = 0; v.width = m_w; v.height = m_h; } |
| 49 | const char* get_default_font_name() const override { return "-apple-system"; } |
| 50 | void make_url(const char* url, const char* basepath, std::string& out) override { out = url; (void)basepath; } |
| 51 | cairo_surface_t* get_image(const std::string& url) override { |
| 52 | if (url.rfind("data:image/png;base64,", 0) != 0) return nullptr; |
| 53 | cairo_surface_t* s = m_images.get_image(url); |
| 54 | if (!s) { |
| 55 | auto bytes = b64decode(url.substr(22)); |
| 56 | mem_reader r{ &bytes, 0 }; |
| 57 | s = cairo_image_surface_create_from_png_stream(read_mem, &r); |
| 58 | if (cairo_surface_status(s) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy(s); return nullptr; } |
| 59 | m_images.add_image(url, s); |
| 60 | s = cairo_surface_reference(s); |
| 61 | } |
| 62 | return s; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | int main(int argc, char** argv) { |
| 67 | if (argc < 4) { fprintf(stderr, "usage: %s in.html width out.png\n", argv[0]); return 2; } |
| 68 | int width = atoi(argv[2]); |
| 69 | std::stringstream ss; std::ifstream(argv[1], std::ios::binary) >> ss.rdbuf(); |
| 70 | std::string html = ss.str(); |
| 71 | using clk = std::chrono::steady_clock; |
| 72 | auto ms = [](clk::time_point a, clk::time_point b) { return std::chrono::duration<double, std::milli>(b |
| 73 | |
| 74 | // Pass 1 with a 24 px viewport (so 100vh does not inflate the page), pass 2 |
| 75 | // with the viewport at the resulting height, like the dev UI's iframe. |
| 76 | int vp_h = 24; litehtml::document::ptr doc; container* cont = nullptr; |
| 77 | double t_parse = 0, t_layout = 0; int height = 0; int passes = 0; |
| 78 | for (;;) { |
| 79 | doc = nullptr; delete cont; cont = new container(width, vp_h); |
| 80 | auto t0 = clk::now(); |
| 81 | doc = litehtml::document::createFromString(html, cont); |
| 82 | auto t1 = clk::now(); |
| 83 | doc->render(width); |
| 84 | auto t2 = clk::now(); |
| 85 | t_parse = ms(t0, t1); t_layout = ms(t1, t2); |
| 86 | height = (int)std::ceil((double)doc->height()); |
| 87 | height = std::max(24, std::min(900, height)); |
| 88 | passes++; |
| 89 | if (height == vp_h || passes >= 6) break; |
| 90 | vp_h = height; |
| 91 | } |
| 92 | auto t3 = clk::now(); |
| 93 | cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); |
| 94 | cairo_t* cr = cairo_create(surface); |
| 95 | cairo_set_source_rgb(cr, 1, 1, 1); cairo_paint(cr); |
| 96 | litehtml::position clip(0, 0, width, height); |
| 97 | doc->draw((litehtml::uint_ptr)cr, 0, 0, &clip); |
| 98 | cairo_surface_flush(surface); |
| 99 | auto t4 = clk::now(); |
| 100 | cairo_surface_write_to_png(surface, argv[3]); |
| 101 | printf("{\"parse_ms\":%.1f,\"layout_ms\":%.1f,\"paint_ms\":%.1f,\"passes\":%d,\"width\" |
| 102 | t_parse, t_layout, ms(t3, t4), passes, width, height, (double)doc->height()); |
| 103 | cairo_destroy(cr); cairo_surface_destroy(surface); |
| 104 | doc = nullptr; delete cont; |
| 105 | return 0; |
| 106 | } |
| 107 | |