#include "layout/layout_engine.hpp" #include "gfx/font_engine.hpp" #include "ipc/ipc_channel.hpp" #include #include #include #include namespace astra { namespace { float line_height_for_size(float font_size) { return FontEngine::instance().line_height(font_size); } uint32_t apply_opacity(uint32_t argb, float opacity) { if (opacity >= 0.999f) return argb; uint32_t a = (argb >> 24) & 0xFFu; uint32_t na = static_cast(std::round(static_cast(a) * std::clamp(opacity, 0.0f, 1.0f))); return (na << 24) | (argb & 0x00FFFFFFu); } std::vector wrap_text(const std::string& raw, float max_width, float font_size, bool bold, bool mono) { std::vector lines; auto& fe = FontEngine::instance(); float usable_w = std::max(40.0f, max_width); std::string cur; size_t i = 0; while (i < raw.size()) { if (raw[i] == '\n') { if (!cur.empty()) { lines.push_back(cur); cur.clear(); } ++i; continue; } while (i < raw.size() && (raw[i] == ' ' || raw[i] == '\t' || raw[i] == '\r')) ++i; size_t wstart = i; while (i < raw.size() && !std::isspace(static_cast(raw[i]))) ++i; if (i == wstart) break; std::string word = raw.substr(wstart, i - wstart); if (cur.empty()) { cur = std::move(word); } else { std::string cand = cur + " " + word; if (fe.measure_text(cand, font_size, bold, mono) <= usable_w) { cur = std::move(cand); } else { lines.push_back(std::move(cur)); cur = std::move(word); } } } if (!cur.empty()) lines.push_back(std::move(cur)); return lines; } void offset_subtree(LayoutFrame* frame, float dx, float dy) { if (!frame || (dx == 0.0f && dy == 0.0f)) return; frame->border_box.x += dx; frame->border_box.y += dy; frame->content_box.x += dx; frame->content_box.y += dy; for (LayoutFrame* ch : frame->children) { offset_subtree(ch, dx, dy); } } Rect intersect_rect(const Rect& a, const Rect& b) { float x1 = std::max(a.x, b.x); float y1 = std::max(a.y, b.y); float x2 = std::min(a.x + a.w, b.x + b.w); float y2 = std::min(a.y + a.h, b.y + b.h); return Rect{x1, y1, std::max(0.0f, x2 - x1), std::max(0.0f, y2 - y1)}; } LayoutFrame* build_frame_tree(SlabArena& arena, const DomNode* node, const CssStylesheet& sheet, const ComputedStyle* parent_style, float viewport_w) { if (!node) return nullptr; if (node->type == NodeType::Text) { LayoutFrame* f = arena.create(); f->node = node; if (parent_style) f->style = *parent_style; f->style.display = DisplayType::Inline; f->style.position = PositionType::Static; f->style.overflow = OverflowMode::Visible; f->style.background = Color{0, 0, 0, 0}; f->style.background_gradient.enabled = false; f->style.border_width = 0; f->style.border_left_width = 0; f->style.padding = {}; f->style.margin = {}; f->style.width = 0; f->style.height = 0; f->style.translate_x = 0; f->style.translate_y = 0; f->style.box_shadow.enabled = false; f->style.opacity = 1.0f; f->style.float_mode = FloatMode::None; f->style.clear_mode = ClearMode::None; f->style.width_pct = -1.0f; f->style.width_calc.clear(); f->style.has_content = false; f->style.content_text.clear(); return f; } ComputedStyle st = StyleEngine::compute_style(node, sheet, parent_style, viewport_w); if (st.display == DisplayType::None) return nullptr; // Default styling for interactive