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
mod context;
mod itemize;
mod model_clip_iterator;

pub use self::context::CellMetrics;
pub use self::context::{Context, FontFeatures};
use self::model_clip_iterator::{ModelClipIteratorFactory, RowView};

use cairo;
use crate::color;
use pango;
use pangocairo;
use crate::sys::pangocairo::*;

use crate::cursor::{cursor_rect, Cursor};
use crate::highlight::HighlightMap;
use crate::ui_model;

trait ContextAlpha {
    fn set_source_rgbo(&self, _: &color::Color, _: Option<f64>);
}

impl ContextAlpha for cairo::Context {
    fn set_source_rgbo(&self, color: &color::Color, alpha: Option<f64>) {
        if let Some(alpha) = alpha {
            self.set_source_rgba(color.0, color.1, color.2, alpha);
        } else {
            self.set_source_rgb(color.0, color.1, color.2);
        }
    }
}

pub fn fill_background(ctx: &cairo::Context, hl: &HighlightMap, alpha: Option<f64>) {
    ctx.set_source_rgbo(hl.bg(), alpha);
    ctx.paint();
}

pub fn render<C: Cursor>(
    ctx: &cairo::Context,
    cursor: &C,
    font_ctx: &context::Context,
    ui_model: &ui_model::UiModel,
    hl: &HighlightMap,
    bg_alpha: Option<f64>,
) {
    let cell_metrics = font_ctx.cell_metrics();
    let &CellMetrics { char_width, .. } = cell_metrics;

    // draw background
    for row_view in ui_model.get_clip_iterator(ctx, cell_metrics) {
        let mut line_x = 0.0;

        for (col, cell) in row_view.line.line.iter().enumerate() {
            draw_cell_bg(&row_view, hl, cell, col, line_x, bg_alpha);
            line_x += char_width;
        }
    }

    // draw text
    for row_view in ui_model.get_clip_iterator(ctx, cell_metrics) {
        let mut line_x = 0.0;

        for (col, cell) in row_view.line.line.iter().enumerate() {
            draw_cell(&row_view, hl, cell, col, line_x, 0.0);
            draw_underline(&row_view, hl, cell, line_x, 0.0);

            line_x += char_width;
        }
    }

    draw_cursor(ctx, cursor, font_ctx, ui_model, hl, bg_alpha);
}

fn draw_cursor<C: Cursor>(
    ctx: &cairo::Context,
    cursor: &C,
    font_ctx: &context::Context,
    ui_model: &ui_model::UiModel,
    hl: &HighlightMap,
    bg_alpha: Option<f64>,
) {
    let cell_metrics = font_ctx.cell_metrics();
    let (cursor_row, cursor_col) = ui_model.get_cursor();

    let (x1, y1, x2, y2) = ctx.clip_extents();
    let line_x = cursor_col as f64 * cell_metrics.char_width;
    let line_y = cursor_row as f64 * cell_metrics.line_height;

    if line_x < x1 || line_y < y1 || line_x > x2 || line_y > y2 || !cursor.is_visible() {
        return;
    }

    let cell_metrics = font_ctx.cell_metrics();
    let row_view = ui_model.get_row_view(ctx, cell_metrics, cursor_row);
    let cell_start_col = row_view.line.cell_to_item(cursor_col);

    if let Some(cursor_line) = ui_model.model().get(cursor_row) {
        let double_width = cursor_line
            .line
            .get(cursor_col + 1)
            .map_or(false, |c| c.double_width);

        if cell_start_col >= 0 {
            let cell = &cursor_line[cursor_col];

            // clip cursor position
            let (clip_y, clip_width, clip_height) =
                cursor_rect(cursor.mode_info(), cell_metrics, line_y, double_width);
            ctx.rectangle(line_x, clip_y, clip_width, clip_height);
            ctx.clip();

            // repaint cell backgound
            ctx.set_operator(cairo::Operator::Source);
            fill_background(ctx, hl, bg_alpha);
            draw_cell_bg(&row_view, hl, cell, cursor_col, line_x, bg_alpha);

            // reapint cursor and text
            ctx.set_operator(cairo::Operator::Over);
            ctx.move_to(line_x, line_y);
            let cursor_alpha = cursor.draw(ctx, font_ctx, line_y, double_width, &hl);

            let cell_start_line_x =
                line_x - (cursor_col as i32 - cell_start_col) as f64 * cell_metrics.char_width;

            debug_assert!(cell_start_line_x >= 0.0);

            draw_cell(
                &row_view,
                hl,
                cell,
                cell_start_col as usize,
                cell_start_line_x,
                cursor_alpha,
            );
            draw_underline(&row_view, hl, cell, line_x, cursor_alpha);
        } else {
            ctx.move_to(line_x, line_y);
            cursor.draw(ctx, font_ctx, line_y, double_width, &hl);
        }
    }
}

fn draw_underline(
    cell_view: &RowView,
    hl: &HighlightMap,
    cell: &ui_model::Cell,
    line_x: f64,
    inverse_level: f64,
) {
    if cell.hl.underline || cell.hl.undercurl {
        let &RowView {
            ctx,
            line_y,
            cell_metrics:
                &CellMetrics {
                    line_height,
                    char_width,
                    underline_position,
                    underline_thickness,
                    ..
                },
            ..
        } = cell_view;

        if cell.hl.undercurl {
            let sp = hl.actual_cell_sp(cell).inverse(inverse_level);
            ctx.set_source_rgba(sp.0, sp.1, sp.2, 0.7);

            let max_undercurl_height = (line_height - underline_position) * 2.0;
            let undercurl_height = (underline_thickness * 4.0).min(max_undercurl_height);
            let undercurl_y = line_y + underline_position - undercurl_height / 2.0;

            pangocairo::functions::show_error_underline(
                ctx,
                line_x,
                undercurl_y,
                char_width,
                undercurl_height,
            );
        } else if cell.hl.underline {
            let fg = hl.actual_cell_fg(cell).inverse(inverse_level);
            ctx.set_source_rgb(fg.0, fg.1, fg.2);
            ctx.set_line_width(underline_thickness);
            ctx.move_to(line_x, line_y + underline_position);
            ctx.line_to(line_x + char_width, line_y + underline_position);
            ctx.stroke();
        }
    }
}

fn draw_cell_bg(
    cell_view: &RowView,
    hl: &HighlightMap,
    cell: &ui_model::Cell,
    col: usize,
    line_x: f64,
    bg_alpha: Option<f64>,
) {
    let &RowView {
        ctx,
        line,
        line_y,
        cell_metrics:
            &CellMetrics {
                char_width,
                line_height,
                ..
            },
        ..
    } = cell_view;

    let bg = hl.cell_bg(cell);

    if let Some(bg) = bg {
        if !line.is_binded_to_item(col) {
            if bg != hl.bg() {
                ctx.set_source_rgbo(bg, bg_alpha);
                ctx.rectangle(line_x, line_y, char_width, line_height);
                ctx.fill();
            }
        } else {
            ctx.set_source_rgbo(bg, bg_alpha);
            ctx.rectangle(
                line_x,
                line_y,
                char_width * line.item_len_from_idx(col) as f64,
                line_height,
            );
            ctx.fill();
        }
    }
}

fn draw_cell(
    row_view: &RowView,
    hl: &HighlightMap,
    cell: &ui_model::Cell,
    col: usize,
    line_x: f64,
    inverse_level: f64,
) {
    let &RowView {
        ctx,
        line,
        line_y,
        cell_metrics: &CellMetrics { ascent, .. },
        ..
    } = row_view;

    if let Some(item) = line.item_line[col].as_ref() {
        if let Some(ref glyphs) = item.glyphs {
            let fg = hl.actual_cell_fg(cell).inverse(inverse_level);

            ctx.move_to(line_x, line_y + ascent);
            ctx.set_source_rgb(fg.0, fg.1, fg.2);

            show_glyph_string(ctx, item.font(), glyphs);
        }
    }
}

pub fn shape_dirty(ctx: &context::Context, ui_model: &mut ui_model::UiModel, hl: &HighlightMap) {
    for line in ui_model.model_mut() {
        if !line.dirty_line {
            continue;
        }

        let styled_line = ui_model::StyledLine::from(line, hl, ctx.font_features());
        let items = ctx.itemize(&styled_line);
        line.merge(&styled_line, &items);

        for (col, cell) in line.line.iter_mut().enumerate() {
            if cell.dirty {
                if let Some(item) = line.item_line[col].as_mut() {
                    let mut glyphs = pango::GlyphString::new();
                    {
                        let analysis = item.analysis();
                        let offset = item.item.offset() as usize;
                        let length = item.item.length() as usize;
                        if let Some(line_str) = styled_line.line_str.get(offset..offset + length) {
                            pango::shape(&line_str, analysis, &mut glyphs);
                        } else {
                            warn!("Wrong itemize split");
                        }
                    }

                    item.set_glyphs(ctx, glyphs);
                }
            }

            cell.dirty = false;
        }

        line.dirty_line = false;
    }
}