@@ -0,0 +1,236 @@ |
| 1 | +# gartk-render |
| 2 | + |
| 3 | +Cairo/Pango rendering with surface management. |
| 4 | + |
| 5 | +## Surface |
| 6 | + |
| 7 | +Cairo ImageSurface wrapper. |
| 8 | + |
| 9 | +```rust |
| 10 | +use gartk_render::Surface; |
| 11 | +use gartk_core::Color; |
| 12 | + |
| 13 | +let mut surface = Surface::new(800, 600)?; |
| 14 | + |
| 15 | +// Clear |
| 16 | +surface.clear(Color::BLACK)?; |
| 17 | + |
| 18 | +// Access Cairo context |
| 19 | +let ctx = surface.context()?; |
| 20 | +// ... custom Cairo drawing ... |
| 21 | + |
| 22 | +// Resize |
| 23 | +surface.resize(1024, 768)?; |
| 24 | + |
| 25 | +// Get raw pixel data (ARGB32 format) |
| 26 | +let data: &[u8] = surface.data()?; |
| 27 | + |
| 28 | +// Dimensions |
| 29 | +surface.size(); // Size |
| 30 | +surface.width(); |
| 31 | +surface.height(); |
| 32 | + |
| 33 | +// Flush pending operations |
| 34 | +surface.flush(); |
| 35 | + |
| 36 | +// Access underlying Cairo surface |
| 37 | +surface.cairo_surface(); |
| 38 | +``` |
| 39 | + |
| 40 | +### DoubleBufferedSurface |
| 41 | + |
| 42 | +For flicker-free rendering (swap buffers pattern). |
| 43 | + |
| 44 | +```rust |
| 45 | +use gartk_render::DoubleBufferedSurface; |
| 46 | + |
| 47 | +let mut db = DoubleBufferedSurface::new(800, 600)?; |
| 48 | + |
| 49 | +// Draw to back buffer |
| 50 | +let ctx = db.back_context()?; |
| 51 | +// ... draw ... |
| 52 | + |
| 53 | +// Swap buffers |
| 54 | +db.swap(); |
| 55 | + |
| 56 | +// Access front buffer data |
| 57 | +let data = db.front_data()?; |
| 58 | +``` |
| 59 | + |
| 60 | +## Renderer |
| 61 | + |
| 62 | +High-level rendering API combining shapes and text. |
| 63 | + |
| 64 | +```rust |
| 65 | +use gartk_render::Renderer; |
| 66 | +use gartk_core::{Theme, Color, Rect, Point}; |
| 67 | + |
| 68 | +// Create with theme |
| 69 | +let theme = Theme::dark(); |
| 70 | +let renderer = Renderer::with_theme(800, 600, theme)?; |
| 71 | + |
| 72 | +// Or without theme |
| 73 | +let renderer = Renderer::new(800, 600)?; |
| 74 | + |
| 75 | +// Clear |
| 76 | +renderer.clear()?; // Uses theme background |
| 77 | +renderer.clear_color(Color::RED)?; |
| 78 | + |
| 79 | +// Resize |
| 80 | +renderer.resize(1024, 768)?; |
| 81 | + |
| 82 | +// After drawing, flush |
| 83 | +renderer.flush(); |
| 84 | + |
| 85 | +// Access internals |
| 86 | +renderer.surface(); |
| 87 | +renderer.theme(); |
| 88 | +renderer.size(); |
| 89 | +renderer.context()?; // Cairo context for custom drawing |
| 90 | +``` |
| 91 | + |
| 92 | +### Shapes |
| 93 | + |
| 94 | +```rust |
| 95 | +use gartk_core::{Rect, Color}; |
| 96 | + |
| 97 | +let rect = Rect::new(10, 10, 100, 50); |
| 98 | + |
| 99 | +// Rectangles |
| 100 | +renderer.fill_rect(rect, Color::BLUE)?; |
| 101 | +renderer.stroke_rect(rect, Color::WHITE, 2.0)?; |
| 102 | + |
| 103 | +// Rounded rectangles |
| 104 | +renderer.fill_rounded_rect(rect, 8.0, Color::BLUE)?; |
| 105 | +renderer.stroke_rounded_rect(rect, 8.0, Color::WHITE, 2.0)?; |
| 106 | + |
| 107 | +// Circles |
| 108 | +renderer.fill_circle(50.0, 50.0, 20.0, Color::RED)?; |
| 109 | + |
| 110 | +// Lines |
| 111 | +renderer.line(0.0, 0.0, 100.0, 100.0, Color::WHITE, 1.0)?; |
| 112 | +``` |
| 113 | + |
| 114 | +### Text |
| 115 | + |
| 116 | +```rust |
| 117 | +use gartk_render::TextStyle; |
| 118 | +use gartk_core::Color; |
| 119 | + |
| 120 | +let style = TextStyle::new() |
| 121 | + .font_family("sans-serif") |
| 122 | + .font_size(14.0) |
| 123 | + .color(Color::WHITE); |
| 124 | + |
| 125 | +// Draw text at position |
| 126 | +renderer.text("Hello", 10.0, 20.0, &style)?; |
| 127 | + |
| 128 | +// Draw with theme defaults |
| 129 | +renderer.text_default("Hello", 10.0, 20.0, Color::WHITE)?; |
| 130 | + |
| 131 | +// Draw in rectangle (auto-centers vertically) |
| 132 | +renderer.text_in_rect("Hello", rect, &style)?; |
| 133 | + |
| 134 | +// Draw centered at point |
| 135 | +renderer.text_centered("Hello", Point::new(100, 100), &style)?; |
| 136 | + |
| 137 | +// Measure text |
| 138 | +let size = renderer.measure_text("Hello", &style)?; |
| 139 | +``` |
| 140 | + |
| 141 | +## TextStyle |
| 142 | + |
| 143 | +Text rendering configuration. |
| 144 | + |
| 145 | +```rust |
| 146 | +use gartk_render::{TextStyle, TextAlign}; |
| 147 | +use gartk_core::Color; |
| 148 | + |
| 149 | +let style = TextStyle::new() |
| 150 | + .font_family("JetBrains Mono") |
| 151 | + .font_size(12.0) |
| 152 | + .color(Color::WHITE) |
| 153 | + .align(TextAlign::Left) // Left, Center, Right |
| 154 | + .ellipsize(true) // Add "..." when truncated |
| 155 | + .wrap(true) // Word wrap |
| 156 | + .max_width(200); // Width constraint (required for ellipsize/wrap) |
| 157 | +``` |
| 158 | + |
| 159 | +## TextRenderer |
| 160 | + |
| 161 | +Lower-level Pango text rendering (used internally by Renderer). |
| 162 | + |
| 163 | +```rust |
| 164 | +use gartk_render::TextRenderer; |
| 165 | + |
| 166 | +let text_renderer = TextRenderer::new(); |
| 167 | + |
| 168 | +// Or with default style |
| 169 | +let text_renderer = TextRenderer::with_style(style); |
| 170 | + |
| 171 | +// Measure |
| 172 | +let size = text_renderer.measure(&ctx, "Hello", &style); |
| 173 | + |
| 174 | +// Draw |
| 175 | +text_renderer.draw(&ctx, "Hello", 10.0, 20.0, &style); |
| 176 | +text_renderer.draw_in_rect(&ctx, "Hello", rect, &style); |
| 177 | +text_renderer.draw_centered(&ctx, "Hello", center_point, &style); |
| 178 | +``` |
| 179 | + |
| 180 | +## Low-Level Shape Functions |
| 181 | + |
| 182 | +Direct Cairo drawing (requires Cairo context). |
| 183 | + |
| 184 | +```rust |
| 185 | +use gartk_render::{fill_rect, stroke_rect, fill_rounded_rect, rounded_rect_path}; |
| 186 | +use gartk_render::{fill_circle, stroke_circle, circle_path}; |
| 187 | +use gartk_render::{line, hline, vline, set_color}; |
| 188 | + |
| 189 | +let ctx = surface.context()?; |
| 190 | + |
| 191 | +// Color |
| 192 | +set_color(&ctx, Color::RED); |
| 193 | + |
| 194 | +// Paths (for custom operations) |
| 195 | +rect_path(&ctx, rect); |
| 196 | +rounded_rect_path(&ctx, rect, radius); |
| 197 | +circle_path(&ctx, cx, cy, radius); |
| 198 | + |
| 199 | +// Fill/stroke |
| 200 | +fill_rect(&ctx, rect, color); |
| 201 | +stroke_rect(&ctx, rect, color, line_width); |
| 202 | +fill_rounded_rect(&ctx, rect, radius, color); |
| 203 | +stroke_rounded_rect(&ctx, rect, radius, color, line_width); |
| 204 | +fill_circle(&ctx, cx, cy, radius, color); |
| 205 | +stroke_circle(&ctx, cx, cy, radius, color, line_width); |
| 206 | + |
| 207 | +// Lines |
| 208 | +line(&ctx, x1, y1, x2, y2, color, line_width); |
| 209 | +hline(&ctx, x1, x2, y, color, line_width); |
| 210 | +vline(&ctx, x, y1, y2, color, line_width); |
| 211 | +``` |
| 212 | + |
| 213 | +## Font Parsing |
| 214 | + |
| 215 | +Parse font specification strings. |
| 216 | + |
| 217 | +```rust |
| 218 | +use gartk_render::parse_font_spec; |
| 219 | + |
| 220 | +// Pango-style |
| 221 | +let (family, size) = parse_font_spec("Sans 12")?; |
| 222 | +let (family, size) = parse_font_spec("JetBrains Mono 11")?; |
| 223 | + |
| 224 | +// Polybar-style |
| 225 | +let (family, size) = parse_font_spec("Monospace:size=14")?; |
| 226 | +``` |
| 227 | + |
| 228 | +## Re-exports |
| 229 | + |
| 230 | +For direct access to underlying libraries: |
| 231 | + |
| 232 | +```rust |
| 233 | +use gartk_render::cairo; |
| 234 | +use gartk_render::pango; |
| 235 | +use gartk_render::pangocairo; |
| 236 | +``` |