summaryrefslogtreecommitdiffstats
path: root/src/ui/grid/render.rs
blob: 441db167393500f35d38bd2ea865addaeb061575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use gtk::prelude::*;
use gtk::DrawingArea;
use pango::Attribute;

use crate::nvim_bridge::GridLineSegment;
use crate::ui::color::Highlight;
use crate::ui::color::HlDefs;
use crate::ui::grid::context::{CellMetrics, Context};
use crate::ui::grid::row::{Cell, Segment};

/// Renders text to `cr`.
///
/// * `cr` - The cairo context to render to.
/// * `pango_context` - The pango context to use for text rendering.
/// * `cm` - Cell metrics to use for text placement.
/// * `hl` - The highlighting to use.
/// * `hl_defs` - Global hl defs. Used to get default values.
/// * `text` - The text to render.
/// * `x` - Target x coordinate for `cr`.
/// * `y` - Target y coordinate for `cr`.
/// * `w` - Target width for `cr`.
/// * `h` - Target height for `cr`.
fn render_text(
    cr: &cairo::Context,
    pango_context: &pango::Context,
    cm: &CellMetrics,
    hl: &Highlight,
    hl_defs: &HlDefs,
    text: &str,
    x: f64,
    y: f64,
    w: f64,
    h: f64,
) {
    let (fg, bg) = if hl.reverse {
        (
            hl.background.unwrap_or(hl_defs.default_bg),
            hl.foreground.unwrap_or(hl_defs.default_fg),
        )
    } else {
        (
            hl.foreground.unwrap_or(hl_defs.default_fg),
            hl.background.unwrap_or(hl_defs.default_bg),
        )
    };

    cr.save();
    cr.set_source_rgb(bg.r, bg.g, bg.b);
    cr.rectangle(x, y, w, h);
    cr.fill();
    cr.restore();

    let attrs = pango::AttrList::new();

    if hl.bold {
        let attr = Attribute::new_weight(pango::Weight::Bold).unwrap();
        attrs.insert(attr);
    }
    if hl.italic {
        let attr = Attribute::new_style(pango::Style::Italic).unwrap();
        attrs.insert(attr);
    }

    cr.save();
    cr.set_source_rgb(fg.r, fg.g, fg.b);

    let items =
        pango::itemize(pango_context, text, 0, text.len() as i32, &attrs, None);

    let mut x_offset = 0.0;
    for item in items {
        let a = item.analysis();
        let item_offset = item.offset() as usize;
        let mut glyphs = pango::GlyphString::new();

        pango::shape(
            &text[item_offset..item_offset + item.length() as usize],
            &a,
            &mut glyphs,
        );

        cr.move_to(x + x_offset, y + cm.ascent);
        pangocairo::functions::show_glyph_string(&cr, &a.font(), &mut glyphs);

        x_offset += f64::from(item.num_chars()) * cm.width;
        //x_offset += f64::from(glyphs.get_width());
    }

    // Since we can't (for some reason) use pango attributes to draw
    // underline and undercurl, we'll have to do that manually.
    let sp = hl.special.unwrap_or(hl_defs.default_sp);
    cr.set_source_rgb(sp.r, sp.g, sp.b);
    if hl.undercurl {
        pangocairo::functions::show_error_underline(
            cr,
            x,
            y + h + cm.underline_position - cm.underline_thickness,
            w,
            cm.underline_thickness * 2.0,
        );
    }
    if hl.underline {
        let y = y + h + cm.underline_position;
        cr.rectangle(x, y, w, cm.underline_thickness);
        cr.fill();
    }

    cr.restore();
}

/// Draws (inverted) cell to `cr`.
pub fn cursor_cell(
    cr: &cairo::Context,
    pango_context: &pango::Context,
    cell: &Cell,
    cm: &CellMetrics,
    hl_defs: &HlDefs,
) {
    let mut hl = *hl_defs.get(&cell.hl_id).unwrap();

    hl.reverse = !hl.reverse;

    let x = 0.0;
    let y = 0.0;
    let w = if cell.double_width {
        cm.width * 2.0
    } else {
        cm.width
    };
    let h = cm.height;

    render_text(cr, pango_context, cm, &hl, hl_defs, &cell.text, x, y, w, h);
}

/// Renders `segments` to `cr`.
fn put_segments(
    cr: &cairo::Context,
    pango_context: &pango::Context,
    queue_draw_area: &mut Vec<(f64, f64, f64, f64)>,
    cm: &CellMetrics,
    hl_defs: &HlDefs,
    segments: Vec<Segment>,
    row: usize,
) {
    let cw = cm.width;
    let ch = cm.height;

    for seg in segments {
        let hl = hl_defs.get(&seg.hl_id).unwrap();

        let x = (seg.start as f64 * cw).floor();
        let y = (row as f64 * ch).floor();
        let w = (seg.len as f64 * cw).ceil();
        let h = ch.ceil();

        let text = &seg.text;
        render_text(cr, pango_context, cm, &hl, hl_defs, &text, x, y, w, h);

        queue_draw_area.push((x, y, w, h));
    }
}

pub fn redraw(
    context: &mut Context,
    pango_context: &pango::Context,
    hl_defs: &HlDefs,
) {
    for (i, row) in context.rows.iter_mut().enumerate() {
        let segments = row.as_segments(0, row.len);

        put_segments(
            &context.cairo_context,
            pango_context,
            &mut context.queue_draw_area,
            &context.cell_metrics,
            hl_defs,
            segments,
            i,
        );
    }
}

/// Renders `line` to `context.cairo_context`.
pub fn put_line(
    context: &mut Context,
    pango_context: &pango::Context,
    line: GridLineSegment,
    hl_defs: &HlDefs,
) {
    let row = line.row as usize;
    let mut affected_segments = context
        .rows
        .get_mut(row)
        .unwrap_or_else(|| panic!("Failed to get row {}", line.row))
        .update(line);

    // NOTE(ville): I haven't noticed any cases where a character is overflowing
    //              to the left. Probably doesn't apply to languages that goes
    //              from right to left, instead of left to right.
    // Rendering the segments in reversed order fixes issues when some character
    // is overflowing to the right.
    affected_segments.reverse();
    put_segments(
        &context.cairo_context,
        pango_context,
        &mut context.queue_draw_area,
        &context.cell_metrics,
        hl_defs,
        affected_segments,
        row,
    );
}

/// Clears whole `da` with `hl_defs.default_bg`.
pub fn clear(da: &DrawingArea, ctx: &mut Context, hl_defs: &HlDefs) {
    let cr = &ctx.cairo_context;
    let w = da.get_allocated_width();
    let h = da.get_allocated_height();
    let bg = &hl_defs.default_bg;

    cr.save();
    cr.set_source_rgb(bg.r, bg.g, bg.b);
    cr.rectangle(0.0, 0.0, f64::from(w), f64::from(h));
    cr.fill();
    cr.restore();

    ctx.queue_draw_area
        .push((0.0, 0.0, f64::from(w), f64::from(h)));
}

/// Scrolls contents in `ctx.cairo_context` and `ctx.rows`, based on `reg`.
pub fn scroll(ctx: &mut Context, hl_defs: &HlDefs, reg: [u64; 4], count: i64) {
    let cr = &ctx.cairo_context;
    let cm = &ctx.cell_metrics;
    let bg = &hl_defs.default_bg;

    let s = cr.get_target();

    let top = reg[0];
    let bot = reg[1];
    let left = reg[2];
    let right = reg[3];

    let (src_top, src_bot, dst_top, dst_bot