From a853f7b133730faf2647e25790360d68e2bf1fae Mon Sep 17 00:00:00 2001 From: MacDue Date: Fri, 14 Jul 2023 21:34:17 +0100 Subject: [PATCH] LibGfx/TinyVG: Map gradients to equivalent SVG gradients --- .../LibGfx/ImageFormats/TinyVGLoader.cpp | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp index 2b0828f0fa..3f7aa97dd2 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp @@ -207,19 +207,30 @@ public: auto color_index = TRY(m_stream.read_value()); return m_color_table[color_index]; }; + auto read_gradient = [&]() -> ErrorOr> { + auto point_0 = TRY(read_point()); + auto point_1 = TRY(read_point()); + auto color_0 = TRY(read_color()); + auto color_1 = TRY(read_color()); + // Map TinyVG gradients to SVG gradients (since we already have those). + // This is not entirely consistent with the spec, which uses gamma sRGB for gradients + // (but this matches the TVG -> SVG renderings). + auto svg_gradient = TRY([&]() -> ErrorOr> { + if (type == StyleType::LinearGradient) + return TRY(SVGLinearGradientPaintStyle::create(point_0, point_1)); + auto radius = point_1.distance_from(point_0); + return TRY(SVGRadialGradientPaintStyle::create(point_0, 0, point_0, radius)); + }()); + TRY(svg_gradient->add_color_stop(0, color_0)); + TRY(svg_gradient->add_color_stop(1, color_1)); + return svg_gradient; + }; switch (type) { - case StyleType::FlatColored: { + case StyleType::FlatColored: return TRY(read_color()); - } case StyleType::LinearGradient: - case StyleType::RadialGradinet: { - // TODO: Make PaintStyle (for these ultra basic gradients) - [[maybe_unused]] auto point_0 = TRY(read_point()); - [[maybe_unused]] auto point_1 = TRY(read_point()); - [[maybe_unused]] auto color_0 = TRY(read_color()); - [[maybe_unused]] auto color_1 = TRY(read_color()); - return Color(Color::Black); - } + case StyleType::RadialGradinet: + return TRY(read_gradient()); } return Error::from_string_literal("Invalid TinyVG: Bad style data"); }