#pragma once #include "lumacs/editor_core.hpp" #include "lumacs/window.hpp" #include "lumacs/theme.hpp" #include #include #include #include namespace lumacs { // Forward declarations class EditorCore; class Window; // Constants for padding static constexpr double PADDING_LEFT = 8.0; static constexpr double PADDING_TOP = 8.0; static constexpr double PADDING_RIGHT = 8.0; static constexpr double PADDING_BOTTOM = 8.0; class GtkRenderer { public: GtkRenderer(EditorCore& core, Gtk::Widget& context_widget); void initialize_font_metrics(); void apply_face_attributes(Pango::AttrList& attr_list, const FaceAttributes& face, int start_index, int end_index); std::optional resolve_screen_pos(std::shared_ptr window, double x, double y); void on_draw(const Cairo::RefPtr& cr, int width, int height, std::shared_ptr active_window_cache, bool cursor_visible_state); void draw_window(const Cairo::RefPtr& cr, int width, int height, std::shared_ptr window, Gtk::Widget* widget, std::shared_ptr active_window_cache, bool cursor_visible_state); void render_modeline_for_window(const Cairo::RefPtr& cr, int width, int height, std::shared_ptr window, std::shared_ptr active_window_cache); void render_minibuffer(const Cairo::RefPtr& cr, int width, int height); /// @brief Gets the screen coordinates and dimensions of the minibuffer area. /// @param x Output parameter for the X coordinate. /// @param y Output parameter for the Y coordinate. /// @param width Output parameter for the width. /// @param height Output parameter for the height. /// @return True if the coordinates could be determined, false otherwise. bool get_minibuffer_coords(int& x, int& y, int& width, int& height) const; /// @brief Clears the internal render cache, forcing a full redraw. /// Useful when the theme changes. void invalidate_cache(); /// @brief Set the font size in points. void set_font_size(int size); /// @brief Get the current font size in points. [[nodiscard]] int font_size() const { return font_size_; } /// @brief Get the current line height in pixels. [[nodiscard]] double line_height() const { return line_height_; } /// @brief Get the current scale factor (for HiDPI). [[nodiscard]] int scale_factor() const { return scale_factor_; } private: /// @brief Convert character position to byte index for UTF-8 string. static size_t char_to_byte_index(const std::string& str, size_t char_pos); struct LineCacheEntry { std::string text_content; std::vector styles; // Check if styles changed Cairo::RefPtr surface; int width = 0; int height = 0; int scale = 1; // Scale factor when surface was created }; // Map Window ID (or ptr) -> (Line Index -> Cache Entry) std::unordered_map> render_cache_; EditorCore& core_; Gtk::Widget& context_widget_; Pango::FontDescription font_desc_; bool font_initialized_ = false; int font_size_ = 12; int scale_factor_ = 1; // HiDPI scale factor double char_width_ = 0; double line_height_ = 0; double ascent_ = 0; double descent_ = 0; }; } // namespace lumacs