/*
 * svg_doc.hpp
 * Lazily-rasterized SVG document for the Image Viewer.
 * Keeps the source bytes around so the rasterizer can be re-run whenever the
 * user changes zoom, giving true vector quality at every scale.
 * Copyright (c) 2026 Daniel Hammer
 */

#pragma once
#include <cstdint>

namespace imageviewer {

struct SvgDoc {
    uint8_t* source;       // SVG bytes (owned, freed in svgdoc_free)
    int      source_len;
    int      natural_w;    // intrinsic SVG dimensions
    int      natural_h;
    uint32_t* pixels;      // current rasterization (owned ARGB)
    int      render_w;     // pixel dimensions of `pixels`
    int      render_h;
    float    render_scale; // pixels = natural * render_scale (may differ from
                           // requested when capped by max_edge)
};

// Initialize from owned SVG source bytes. The doc takes ownership of `source`
// and will free it via montauk::mfree on svgdoc_free. Returns false on
// failure; on failure the caller still owns `source`.
bool svgdoc_init(SvgDoc* doc, uint8_t* source, int source_len);

// Ensure the rasterization matches `target_scale`. The longest rendered edge
// is capped at `max_edge` pixels; if `target_scale` would exceed that, the
// scale is clamped (render_scale will reflect the actual scale used).
// Returns false if rasterization fails; the existing cache is left intact.
bool svgdoc_render(SvgDoc* doc, float target_scale, int max_edge);

// Free all owned memory (source bytes + raster cache).
void svgdoc_free(SvgDoc* doc);

} // namespace imageviewer
