| 1 | // Host harness (macOS): feeds a JSON array of host → renderer messages (as |
| 2 | // `pnpm tsx scripts/sandbox-node.ts <widget.html> --ops-file msgs.json` |
| 3 | // writes it) to the litehtml renderer, prints what it answers, optionally taps |
| 4 | // it, and writes the paint as PNG. Usage: |
| 5 | // twidget-litehtml-run msgs.json out.png [--width 360] [--dpr 2] [--dark] [--tap x,y]... [--html out.html] [--quiet] |
| 6 | #include "../include/twidget_litehtml.h" |
| 7 |
| 8 | #include <CoreGraphics/CoreGraphics.h> |
| 9 | #include <ImageIO/ImageIO.h> |
| 10 |
| 11 | #include <chrono> |
| 12 | #include <cstdio> |
| 13 | #include <cstdlib> |
| 14 | #include <cstring> |
| 15 | #include <fstream> |
| 16 | #include <sstream> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 |
| 20 | static bool write_png(const char* path, const std::vector<uint8_t>& rgba, uint32_t w, uint32_t h) { |
| 21 | CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); |
| 22 | CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, rgba.data(), rgba.size(), nullptr); |
| 23 | CGImageRef image = CGImageCreate(w, h, 8, 32, size_t(w) * 4, rgb, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, provider, nullptr, false, kCGRenderingIntentDefault); |
| 24 | CFStringRef cfpath = CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingUTF8); |
| 25 | CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfpath, kCFURLPOSIXPathStyle, false); |
| 26 | CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, CFSTR("public.png"), 1, nullptr); |
| 27 | bool ok = false; |
| 28 | if (dest && image) { |
| 29 | CGImageDestinationAddImage(dest, image, nullptr); |
| 30 | ok = CGImageDestinationFinalize(dest); |
| 31 | } |
| 32 | if (dest) CFRelease(dest); |
| 33 | CFRelease(url); |
| 34 | CFRelease(cfpath); |
| 35 | if (image) CGImageRelease(image); |
| 36 | CGDataProviderRelease(provider); |
| 37 | CGColorSpaceRelease(rgb); |
| 38 | return ok; |
| 39 | } |
| 40 |
| 41 | /// Splits a top-level JSON array of objects into its elements (the messages file is trusted, produced by our own tooling). |
| 42 | static std::vector<std::string> split_messages(const std::string& text) { |
| 43 | std::vector<std::string> out; |
| 44 | int depth = 0; |
| 45 | bool in_string = false, escape = false; |
| 46 | size_t start = 0; |
| 47 | for (size_t i = 0; i < text.size(); i++) { |
| 48 | char c = text[i]; |
| 49 | if (in_string) { |
| 50 | if (escape) escape = false; |
| 51 | else if (c == '\\') escape = true; |
| 52 | else if (c == '"') in_string = false; |
| 53 | continue; |
| 54 | } |
| 55 | if (c == '"') in_string = true; |
| 56 | else if (c == '{' || c == '[') { |
| 57 | if (depth == 1 && c == '{') start = i; |
| 58 | depth++; |
| 59 | } else if (c == '}' || c == ']') { |
| 60 | depth--; |
| 61 | if (depth == 1 && c == '}') out.push_back(text.substr(start, i - start + 1)); |
| 62 | } |
| 63 | } |
| 64 | return out; |
| 65 | } |
| 66 |
| 67 | int main(int argc, char** argv) { |
| 68 | if (argc < 3) { |
| 69 | std::fprintf(stderr, "usage: %s msgs.json out.png [--width 360] [--dpr 1] [--dark] [--tap x,y]... [--html out.html] [--quiet]\n", argv[0]); |
| 70 | return 2; |
| 71 | } |
| 72 | double width = 360, dpr = 1; |
| 73 | bool dark = false, quiet = false; |
| 74 | const char* html_out = nullptr; |
| 75 | std::vector<std::pair<double, double>> taps; |
| 76 | for (int i = 3; i < argc; i++) { |
| 77 | std::string a = argv[i]; |
| 78 | if (a == "--width" && i + 1 < argc) width = std::atof(argv[++i]); |
| 79 | else if (a == "--dpr" && i + 1 < argc) dpr = std::atof(argv[++i]); |
| 80 | else if (a == "--dark") dark = true; |
| 81 | else if (a == "--quiet") quiet = true; |
| 82 | else if (a == "--html" && i + 1 < argc) html_out = argv[++i]; |
| 83 | else if (a == "--tap" && i + 1 < argc) { |
| 84 | double x = 0, y = 0; |
| 85 | if (std::sscanf(argv[++i], "%lf,%lf", &x, &y) == 2) taps.emplace_back(x, y); |
| 86 | } |
| 87 | } |
| 88 | std::stringstream ss; |
| 89 | std::ifstream in(argv[1], std::ios::binary); |
| 90 | if (!in) { std::fprintf(stderr, "cannot read %s\n", argv[1]); return 1; } |
| 91 | ss << in.rdbuf(); |
| 92 | std::vector<std::string> msgs = split_messages(ss.str()); |
| 93 |
| 94 | using clk = std::chrono::steady_clock; |
| 95 | auto ms = [](clk::time_point a, clk::time_point b) { return std::chrono::duration<double, std::milli>(b - a).count(); }; |
| 96 | auto t0 = clk::now(); |
| 97 | twl_renderer* r = twl_create(width, dpr, dark ? "dark" : "light"); |
| 98 | if (!r) { std::fprintf(stderr, "twl_create failed\n"); return 1; } |
| 99 | auto t1 = clk::now(); |
| 100 | auto say = [&](const char* label, char* json) { |
| 101 | if (json && !quiet && std::strcmp(json, "[]")) std::printf("%s → %s\n", label, json); |
| 102 | twl_string_free(json); |
| 103 | }; |
| 104 | for (const std::string& m : msgs) { |
| 105 | auto a = clk::now(); |
| 106 | char* out = twl_receive(r, m.c_str()); |
| 107 | auto b = clk::now(); |
| 108 | if (!quiet) std::printf("message %.60s… in %.1f ms\n", m.c_str(), ms(a, b)); |
| 109 | say(" answer", out); |
| 110 | } |
| 111 | for (auto [x, y] : taps) { |
| 112 | say("tap down", twl_touch(r, 0, x, y, 0, true)); |
| 113 | say("tap up", twl_touch(r, 2, x, y, 0, true)); |
| 114 | } |
| 115 | uint32_t w = 0, h = 0; |
| 116 | twl_size(r, &w, &h); |
| 117 | std::vector<uint8_t> pixels(size_t(w) * h * 4); |
| 118 | double paint_ms = twl_render(r, pixels.data(), pixels.size()); |
| 119 | char* stats = twl_stats(r); |
| 120 | std::printf("{\"create_ms\":%.2f,\"paint_ms\":%.2f,\"width_px\":%u,\"height_px\":%u,\"messages\":%zu,\"stats\":%s}\n", ms(t0, t1), paint_ms, w, h, msgs.size(), stats ? stats : "null"); |
| 121 | twl_string_free(stats); |
| 122 | if (html_out) { |
| 123 | char* html = twl_html(r); |
| 124 | std::ofstream(html_out, std::ios::binary) << (html ? html : ""); |
| 125 | twl_string_free(html); |
| 126 | } |
| 127 | bool ok = paint_ms >= 0 && write_png(argv[2], pixels, w, h); |
| 128 | twl_destroy(r); |
| 129 | if (!ok) { std::fprintf(stderr, "paint or PNG failed\n"); return 1; } |
| 130 | return 0; |
| 131 | } |
| 132 |