// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Nostro.Ui:Draw_impl; import std; import :Draw; import :Geometry; import :Text; using namespace Nostro::Ui; DrawList::DrawList(const TextMeasure& measure, Box bounds) : measure(&measure), bounds(bounds) { } void DrawList::SetLayer(Layer to) noexcept { layer = to; } Layer DrawList::CurrentLayer() const noexcept { return layer; } void DrawList::PushClip(Box box) { clips.push_back(clips.empty() ? box.Intersect(bounds) : box.Intersect(clips.back())); } void DrawList::PopClip() { clips.pop_back(); } bool DrawList::Visible(Box box) const noexcept { return box.Overlaps(clips.empty() ? bounds : clips.back()); } Batch& DrawList::BatchFor(std::optional shape) { std::vector& batches = layers[static_cast(layer)]; const std::optional clip = clips.empty() ? std::nullopt : std::optional(clips.back()); bool fits = !batches.empty() && batches.back().clip == clip; if(fits && shape) { // In one pass the batch's text is drawn over all its shapes: a shape that comes after a text it touches needs the // next pass to cover it. fits = std::ranges::none_of(batches.back().texts, [&shape](const TextItem& text) { return text.bounds.Overlaps(*shape); }); } if(!fits) { batches.emplace_back().clip = clip; } return batches.back(); } void DrawList::Rect(const RectItem& item) { if(Visible(item.box)) { BatchFor(item.box).rects.push_back(item); } } void DrawList::Rect(Box box, Color color, float radius) { Rect(RectItem{.box = box, .color = color, .corners = Corners::All(radius)}); } void DrawList::Circle(const CircleItem& item) { const Box box{item.center.x - item.radius, item.center.y - item.radius, item.radius * 2, item.radius * 2}; if(Visible(box)) { BatchFor(box).circles.push_back(item); } } void DrawList::Circle(Point center, float radius, Color color) { Circle(CircleItem{.center = center, .radius = radius, .color = color}); } float DrawList::Text(std::string_view text, float x, float baseline, FontFace face, float px, Color color, TextAlign align) { const float width = TextWidth(*measure, text, face, px); const float left = align == TextAlign::Left ? x : (align == TextAlign::Center ? x - width / 2 : x - width); const Box box{left, baseline - measure->ascent(face, px), width, measure->lineHeight(face, px)}; if(!text.empty() && Visible(box)) { BatchFor(std::nullopt).texts.push_back(TextItem{.text = std::string(text), .x = x, .baseline = baseline, .face = face, .px = px, .color = color, .align = align, .bounds = box}); } return width; } std::span DrawList::Batches(Layer of) const noexcept { return layers[static_cast(of)]; } std::size_t DrawList::BatchCount() const noexcept { std::size_t count = 0; for(const std::vector& batches : layers) { count += batches.size(); } return count; } std::vector DrawList::Texts() const { std::vector texts; for(const std::vector& batches : layers) { for(const Batch& batch : batches) { for(const TextItem& text : batch.texts) { texts.push_back(text.text); } } } return texts; }