pspp 0.6.1

Statistical analysis software
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// PSPP - a program for statistical analysis.
// Copyright (C) 2025 Free Software Foundation, Inc.
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program.  If not, see <http://www.gnu.org/licenses/>.

use std::{cmp::min, f64::consts::PI, fmt::Write, ops::DerefMut, sync::Arc};

use cairo::Context;
use enum_map::{EnumMap, enum_map};
use itertools::Itertools;
use pango::{
    AttrFloat, AttrFontDesc, AttrInt, AttrList, Attribute, FontDescription, FontMask, Layout,
    SCALE, SCALE_SMALL, Underline, Weight,
};
use pangocairo::functions::show_layout;
use smallvec::{SmallVec, smallvec};

use crate::{
    output::{
        Details, Item,
        drivers::cairo::{px_to_xr, xr_to_pt},
        pivot::{
            Axis2, Coord2, PivotTable, Rect2,
            look::{BorderStyle, Color, FontStyle, HorzAlign, Stroke},
        },
        render::{Device, Extreme, Pager, Params},
        table::{Content, DrawCell},
    },
    spv::html::Markup,
};

/// Width of an ordinary line.
const LINE_WIDTH: isize = LINE_SPACE / 2;

/// Space between double lines.
const LINE_SPACE: isize = SCALE as isize;

/// Conversion from 1/96" units ("pixels") to Cairo/Pango units.
fn pxf_to_xr(x: f64) -> isize {
    (x * (SCALE as f64 * 72.0 / 96.0)).round() as isize
}

#[derive(Clone, Debug)]
pub struct CairoFsmStyle {
    /// Page size.
    pub size: Coord2,

    /// Minimum cell size to allow breaking.
    pub min_break: EnumMap<Axis2, isize>,

    /// The basic font.
    pub font: FontDescription,

    /// Foreground color.
    pub fg: Color,

    /// Use system colors?
    pub use_system_colors: bool,

    /// Vertical space between different output items.
    pub object_spacing: isize,

    /// Resolution, in units per inch, used for measuring font "points":
    ///
    /// - 72.0 if 1 pt is one device unit, e.g. for rendering to a surface
    ///   created by [cairo::PsSurface::new] with its default transformation
    ///   matrix of 72 units/inch.
    ///
    /// - 96.0 is traditional for a screen-based surface.
    pub font_resolution: f64,
}

impl CairoFsmStyle {
    fn new_layout(&self, context: &Context) -> Layout {
        let pangocairo_context = pangocairo::functions::create_context(context);
        pangocairo::functions::context_set_resolution(&pangocairo_context, self.font_resolution);
        Layout::new(&pangocairo_context)
    }
}

pub struct CairoFsm {
    style: Arc<CairoFsmStyle>,
    params: Params,
    item: Arc<Item>,
    layer_iterator: Option<Box<dyn Iterator<Item = SmallVec<[usize; 4]>>>>,
    pager: Option<Pager>,
}

impl CairoFsm {
    pub fn new(
        style: Arc<CairoFsmStyle>,
        printing: bool,
        context: &Context,
        item: Arc<Item>,
    ) -> Self {
        let params = Params {
            size: style.size,
            font_size: {
                let layout = style.new_layout(context);
                layout.set_font_description(Some(&style.font));
                layout.set_text("0");
                let char_size = layout.size();
                enum_map! {
                    Axis2::X => char_size.0 as isize,
                    Axis2::Y => char_size.1 as isize
                }
            },
            line_widths: enum_map! {
                Stroke::None => 0,
                Stroke::Solid | Stroke::Dashed => LINE_WIDTH,
                Stroke::Thick => LINE_WIDTH * 2,
                Stroke::Thin => LINE_WIDTH / 2,
                Stroke::Double => LINE_WIDTH * 2 + LINE_SPACE,
            },
            px_size: Some(px_to_xr(1)),
            min_break: style.min_break,
            supports_margins: true,
            rtl: false,
            printing,
            can_adjust_break: false, // XXX
            can_scale: true,
        };
        let device = CairoDevice {
            style: &style,
            params: &params,
            context,
        };
        let item = if let Some(text) = item.details.as_text() {
            Arc::new(Item::new(PivotTable::from(text.clone())))
        } else {
            item
        };
        let (layer_iterator, pager) = match &item.details {
            Details::Table(pivot_table) => {
                let mut layer_iterator = pivot_table.layers(printing);
                let layer_indexes = layer_iterator.next();
                (
                    Some(layer_iterator),
                    Some(Pager::new(
                        &device,
                        pivot_table,
                        layer_indexes.as_ref().map(|indexes| indexes.as_slice()),
                    )),
                )
            }
            _ => (None, None),
        };
        Self {
            style,
            params,
            item,
            layer_iterator,
            pager,
        }
    }

    pub fn draw_slice(&mut self, context: &Context, space: isize) -> isize {
        debug_assert!(self.params.printing);

        context.save().unwrap();
        let used = match &self.item.details {
            Details::Table(_) => self.draw_table(context, space),
            _ => 0,
        };
        context.restore().unwrap();

        used
    }

    fn draw_table(&mut self, context: &Context, space: isize) -> isize {
        let pivot_table = self.item.details.as_table().unwrap();
        let Some(pager) = &mut self.pager else {
            return 0;
        };
        let mut device = CairoDevice {
            style: &self.style,
            params: &self.params,
            context,
        };
        let mut used = pager.draw_next(&mut device, space);
        if pager.has_next(&device).is_none() {
            if let Some(layer_indexes) = self.layer_iterator.as_mut().unwrap().next() {
                self.pager = Some(Pager::new(
                    &device,
                    pivot_table,
                    Some(layer_indexes.as_slice()),
                ));
                if pivot_table.style.look.paginate_layers {
                    used = space;
                } else {
                    used += self.style.object_spacing;
                }
            } else {
                self.pager = None;
            }
        }
        used.min(space)
    }

    pub fn is_done(&self) -> bool {
        match &self.item.details {
            Details::Table(_) => self.pager.is_none(),
            _ => true,
        }
    }
}

fn xr_clip(context: &Context, clip: &Rect2) {
    if clip[Axis2::X].end != isize::MAX || clip[Axis2::Y].end != isize::MAX {
        let x0 = xr_to_pt(clip[Axis2::X].start);
        let y0 = xr_to_pt(clip[Axis2::Y].start);
        let x1 = xr_to_pt(clip[Axis2::X].end);
        let y1 = xr_to_pt(clip[Axis2::Y].end);
        context.rectangle(x0, y0, x1 - x0, y1 - y0);
        context.clip();
    }
}

fn xr_set_color(context: &Context, color: Color) {
    fn as_frac(x: u8) -> f64 {
        x as f64 / 255.0
    }

    context.set_source_rgba(
        as_frac(color.r),
        as_frac(color.g),
        as_frac(color.b),
        as_frac(color.alpha),
    );
}

fn xr_fill_rectangle(context: &Context, rectangle: Rect2) {
    context.new_path();
    context.set_line_width(xr_to_pt(LINE_WIDTH));

    let x0 = xr_to_pt(rectangle[Axis2::X].start);
    let y0 = xr_to_pt(rectangle[Axis2::Y].start);
    let width = xr_to_pt(rectangle[Axis2::X].len() as isize);
    let height = xr_to_pt(rectangle[Axis2::Y].len() as isize);
    context.rectangle(x0, y0, width, height);
    context.fill().unwrap();
}

fn margin(cell: &DrawCell, axis: Axis2) -> isize {
    px_to_xr(cell.cell_style.margins[axis].iter().sum::<i32>() as isize)
}

pub fn parse_font_style(font_style: &FontStyle) -> FontDescription {
    let font = font_style.font.as_str();
    let font = if font.eq_ignore_ascii_case("Monospaced") {
        "Monospace"
    } else {
        font
    };
    let mut font_desc = FontDescription::from_string(font);
    if !font_desc.set_fields().contains(FontMask::SIZE) {
        let default_size = if font_style.size != 0 {
            font_style.size * 1000
        } else {
            10_000
        };
        font_desc.set_size(((default_size as f64 / 1000.0) * (SCALE as f64)) as i32);
    }
    font_desc.set_weight(if font_style.bold {
        Weight::Bold
    } else {
        Weight::Normal
    });
    font_desc.set_style(if font_style.italic {
        pango::Style::Italic
    } else {
        pango::Style::Normal
    });
    font_desc
}

/// Deal with an oddity of the Unicode line-breaking algorithm (or perhaps in
/// Pango's implementation of it): it will break after a period or a comma that
/// precedes a digit, e.g. in `.000` it will break after the period.  This code
/// looks for such a situation and inserts a U+2060 WORD JOINER to prevent the
/// break.
///
/// This isn't necessary when the decimal point is between two digits
/// (e.g. `0.000` won't be broken) or when the display width is not limited so
/// that word wrapping won't happen.
///
/// It isn't necessary to look for more than one period or comma, as would
/// happen with grouping like `1,234,567.89` or `1.234.567,89` because if groups
/// are present then there will always be a digit on both sides of every period
/// and comma.
fn avoid_decimal_split(mut s: String) -> String {
    if let Some(position) = s.find(['.', ',']) {
        let followed_by_digit = s[position + 1..]
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_digit());
        let not_preceded_by_digit = s[..position]
            .chars()
            .next_back()
            .is_none_or(|c| !c.is_ascii_digit());
        if followed_by_digit && not_preceded_by_digit {
            s.insert(position + 1, '\u{2060}');
        }
    }
    s
}

impl<'a, 'b> DrawCell<'a, 'b> {
    pub(crate) fn layout(&self, bb: &Rect2, layout: &mut Layout, default_font: &FontDescription) {
        // XXX rotation

        let mut bb = bb.clone();
        layout.set_attributes(None);

        let parsed_font;
        let font = if !self.font_style.font.is_empty() {
            parsed_font = parse_font_style(&self.font_style);
            &parsed_font
        } else {
            default_font
        };
        layout.set_font_description(Some(font));

        let (body_display, suffixes) = self.display().split();
        let horz_align = self.horz_align(&body_display);

        let (mut body, mut attrs) = if let Some(markup) = body_display.markup() {
            let (body, attrs) = Markup::to_pango(markup, self.substitutions);
            (body, Some(attrs))
        } else {
            (avoid_decimal_split(body_display.to_string()), None)
        };

        if let Some(decimal_offset) = horz_align.decimal_offset()
            && !self.rotate
            && let Some(decimal) = body_display.decimal()
            && let Some(index) = body.rfind(char::from(decimal))
        {
            layout.set_text(&body[index..]);
            layout.set_width(-1);
            bb[Axis2::X].end -= pxf_to_xr(decimal_offset).saturating_sub(layout.size().0 as isize);
        }

        if self.font_style.underline {
            attrs
                .get_or_insert_default()
                .insert(AttrInt::new_underline(Underline::Single));
        }

        if !suffixes.is_empty() {
            let subscript_ofs = body.len();
            #[allow(unstable_name_collisions)]
            body.extend(suffixes.subscripts().intersperse(","));
            let has_subscripts = subscript_ofs != body.len();

            let footnote_ofs = body.len();
            for (index, footnote) in suffixes.footnotes().enumerate() {
                if index > 0 {
                    body.push(',');
                }
                write!(&mut body, "{footnote}").unwrap();
            }
            let has_footnotes = footnote_ofs != body.len();

            // Allow footnote markers to occupy the right margin.  That way,
            // numbers in the column are still aligned.
            if has_footnotes && horz_align == HorzAlign::Right {
                // Measure the width of the footnote marker, so we know how much we
                // need to make room for.
                layout.set_text(&body[footnote_ofs..]);

                let footnote_attrs = AttrList::new();
                footnote_attrs.insert(AttrFloat::new_scale(SCALE_SMALL));
                footnote_attrs.insert(AttrInt::new_rise(3000));
                layout.set_attributes(Some(&footnote_attrs));
                let footnote_width = layout.size().0 as isize;

                // Bound the adjustment by the width of the right margin.
                let right_margin = px_to_xr(self.cell_style.margins[Axis2::X][1] as isize);
                let footnote_adjustment = min(footnote_width, right_margin);

                // Adjust the bounding box.
                if self.rotate {
                    bb[Axis2::X].end = bb[Axis2::X].end.saturating_sub(footnote_adjustment);
                } else {
                    bb[Axis2::X].end = bb[Axis2::X].end.saturating_add(footnote_adjustment);
                }

                // Clean up.
                layout.set_attributes(None);
            }

            fn with_start<T: DerefMut<Target = Attribute>>(index: usize, mut attr: T) -> T {
                attr.deref_mut().set_start_index(index.try_into().unwrap());
                attr
            }
            fn with_end<T: DerefMut<Target = Attribute>>(index: usize, mut attr: T) -> T {
                attr.deref_mut().set_end_index(index.try_into().unwrap());
                attr
            }

            // Set attributes.
            let attrs = attrs.get_or_insert_default();
            attrs.insert(with_start(subscript_ofs, AttrFontDesc::new(font)));
            attrs.insert(with_start(subscript_ofs, AttrFloat::new_scale(SCALE_SMALL)));
            if has_subscripts {
                attrs.insert(with_start(
                    subscript_ofs,
                    with_end(footnote_ofs, AttrInt::new_rise(-3000)),
                ));
            }
            if has_footnotes {
                let rise = 3000; // XXX check look for superscript vs subscript
                attrs.insert(with_start(footnote_ofs, AttrInt::new_rise(rise)));
            }
        }

        layout.set_attributes(attrs.as_ref());
        layout.set_text(&body);
        layout.set_alignment(horz_align.into());
        if bb[Axis2::X].end == isize::MAX {
            layout.set_width(-1);
        } else {
            layout.set_width(bb[Axis2::X].len() as i32);
        }
    }

    pub(crate) fn draw(
        &self,
        bb: &Rect2,
        layout: &Layout,
        clip: Option<&Rect2>,
        context: &Context,
    ) {
        context.save().unwrap();
        if !self.rotate
            && let Some(clip) = clip
        {
            xr_clip(context, clip);
        }
        if self.rotate {
            let extra = (bb[Axis2::X].len() as isize - layout.size().1 as isize).max(0);
            let halign_offset = extra / 2;
            context.translate(
                xr_to_pt(bb[Axis2::X].start + halign_offset),
                xr_to_pt(bb[Axis2::Y].end),
            );
            context.rotate(-PI / 2.0);
        } else {
            context.translate(xr_to_pt(bb[Axis2::X].start), xr_to_pt(bb[Axis2::Y].start));
        }
        show_layout(context, &layout);
        context.restore().unwrap();
    }
}

struct CairoDevice<'a> {
    style: &'a CairoFsmStyle,
    params: &'a Params,
    context: &'a Context,
}

impl CairoDevice<'_> {
    fn measure_cell(&self, cell: &DrawCell, bb: Rect2) -> Coord2 {
        let mut layout = self.style.new_layout(self.context);
        cell.layout(&bb, &mut layout, &self.style.font);
        let (width, height) = layout.size();
        Coord2::new(width as isize, height as isize)
    }

    fn cell_draw(&self, cell: &DrawCell, bb: Rect2, clip: &Rect2) {
        let mut layout = self.style.new_layout(self.context);
        cell.layout(&bb, &mut layout, &self.style.font);
        cell.draw(&bb, &layout, Some(clip), &self.context);
    }

    fn do_draw_line(
        &self,
        x0: isize,
        y0: isize,
        x1: isize,
        y1: isize,
        stroke: Stroke,
        color: Color,
    ) {
        self.context.new_path();
        self.context.set_line_width(xr_to_pt(match stroke {
            Stroke::Thick => LINE_WIDTH * 2,
            Stroke::Thin => LINE_WIDTH / 2,
            _ => LINE_WIDTH,
        }));
        self.context.move_to(xr_to_pt(x0), xr_to_pt(y0));
        self.context.line_to(xr_to_pt(x1), xr_to_pt(y1));
        if !self.style.use_system_colors {
            xr_set_color(self.context, color);
        }
        if stroke == Stroke::Dashed {
            self.context.set_dash(&[2.0], 0.0);
            let _ = self.context.stroke();
            self.context.set_dash(&[], 0.0);
        } else {
            let _ = self.context.stroke();
        }
    }
}

impl Device for CairoDevice<'_> {
    fn params(&self) -> &Params {
        self.params
    }

    fn measure_cell_width(&self, cell: &DrawCell) -> EnumMap<Extreme, isize> {
        fn add_margins(cell: &DrawCell, width: isize) -> isize {
            if width > 0 {
                width + margin(cell, Axis2::X)
            } else {
                0
            }
        }

        enum_map![
            Extreme::Min => {
                let bb = Rect2::new(0..1, 0..isize::MAX);
                add_margins(cell, self.measure_cell(cell, bb).x())
            }
            Extreme::Max => {
                let bb = Rect2::new(0..isize::MAX, 0..isize::MAX);
                add_margins(cell, self.measure_cell(cell, bb).x())
            },
        ]
    }

    fn measure_cell_height(&self, cell: &DrawCell, width: isize) -> isize {
        let bb = Rect2::new(
            0..width.saturating_sub(margin(cell, Axis2::X)),
            0..isize::MAX,
        );
        self.measure_cell(cell, bb).y() + margin(cell, Axis2::Y)
    }

    fn adjust_break(&self, _cell: &Content, _size: Coord2) -> isize {
        todo!()
    }

    fn draw_line(&mut self, bb: Rect2, styles: EnumMap<Axis2, [BorderStyle; 2]>, _bg: Color) {
        let x0 = bb[Axis2::X].start;
        let y0 = bb[Axis2::Y].start;
        let x3 = bb[Axis2::X].end;
        let y3 = bb[Axis2::Y].end;

        let top = styles[Axis2::X][0].stroke;
        let bottom = styles[Axis2::X][1].stroke;
        let left = styles[Axis2::Y][0].stroke;
        let right = styles[Axis2::Y][1].stroke;

        let top_color = styles[Axis2::X][0].color;
        let bottom_color = styles[Axis2::X][1].color;
        let left_color = styles[Axis2::Y][0].color;
        let right_color = styles[Axis2::Y][1].color;

        // The algorithm here is somewhat subtle, to allow it to handle
        // all the kinds of intersections that we need.
        //
        // Three additional ordinates are assigned along the X axis.  The first
        // is `xc`, midway between `x0` and `x3`.  The others are `x1` and `x2`;
        // for a single vertical line these are equal to `xc`, and for a double
        // vertical line they are the ordinates of the left and right half of
        // the double line.
        //
        // `yc`, `y1`, and `y2` are assigned similarly along the Y axis.
        //
        // The following diagram shows the coordinate system and output for
        // double top and bottom lines, single left line, and no right line:
        //
        // ```
        //             x0       x1 xc  x2      x3
        //           y0 ________________________
        //              |        #     #       |
        //              |        #     #       |
        //              |        #     #       |
        //              |        #     #       |
        //              |        #     #       |
        // y1 = y2 = yc |#########     #       |
        //              |        #     #       |
        //              |        #     #       |
        //              |        #     #       |
        //              |        #     #       |
        //           y3 |________#_____#_______|
        // ```

        // Offset from center of each line in a pair of double lines.
        let double_line_ofs = (LINE_SPACE + LINE_WIDTH) / 2;

        // Are the lines along each axis single or double?  (It doesn't make
        // sense to have different kinds of line on the same axis, so we don't
        // try to gracefully handle that case.)
        let double_vert = top == Stroke::Double || bottom == Stroke::Double;
        let double_horz = left == Stroke::Double || right == Stroke::Double;

        // When horizontal lines are doubled, the left-side line along `y1`
        // normally runs from `x0` to `x2`, and the right-side line along `y1`
        // from `x3` to `x1`.  If the top-side line is also doubled, we shorten
        // the `y1` lines, so that the left-side line runs only to `x1`, and the
        // right-side line only to `x2`.  Otherwise, the horizontal line at `y =
        // y1` below would cut off the intersection, which looks ugly:
        //
        // ```
        //           x0       x1     x2      x3
        //         y0 ________________________
        //            |        #     #       |
        //            |        #     #       |
        //            |        #     #       |
        //            |        #     #       |
        //         y1 |#########     ########|
        //            |                      |
        //            |                      |
        //         y2 |######################|
        //            |                      |
        //            |                      |
        //         y3 |______________________|
        // ```
        //
        // It is more of a judgment call when the horizontal line is single.  We
        // choose to cut off the line anyhow, as shown in the first diagram
        // above.
        let shorten_y1_lines = top == Stroke::Double;
        let shorten_y2_lines = bottom == Stroke::Double;
        let shorten_yc_line = shorten_y1_lines && shorten_y2_lines;
        let horz_line_ofs = if double_vert { double_line_ofs } else { 0 };
        let xc = (x0 + x3) / 2;
        let x1 = xc - horz_line_ofs;
        let x2 = xc + horz_line_ofs;

        let shorten_x1_lines = left == Stroke::Double;
        let shorten_x2_lines = right == Stroke::Double;
        let shorten_xc_line = shorten_x1_lines && shorten_x2_lines;
        let vert_line_ofs = if double_horz { double_line_ofs } else { 0 };
        let yc = (y0 + y3) / 2;
        let y1 = yc - vert_line_ofs;
        let y2 = yc + vert_line_ofs;

        let horz_lines: SmallVec<[_; 2]> = if double_horz {
            smallvec![(y1, shorten_y1_lines), (y2, shorten_y2_lines)]
        } else {
            smallvec![(yc, shorten_yc_line)]
        };
        for (y, shorten) in horz_lines {
            if left != Stroke::None
                && right != Stroke::None
                && !shorten
                && left_color == right_color
            {
                self.do_draw_line(x0, y, x3, y, left, left_color);
            } else {
                if left != Stroke::None {
                    self.do_draw_line(x0, y, if shorten { x1 } else { x2 }, y, left, left_color);
                }
                if right != Stroke::None {
                    self.do_draw_line(if shorten { x2 } else { x1 }, y, x3, y, right, right_color);
                }
            }
        }

        let vert_lines: SmallVec<[_; 2]> = if double_vert {
            smallvec![(x1, shorten_x1_lines), (x2, shorten_x2_lines)]
        } else {
            smallvec![(xc, shorten_xc_line)]
        };
        for (x, shorten) in vert_lines {
            if top != Stroke::None
                && bottom != Stroke::None
                && !shorten
                && top_color == bottom_color
            {
                self.do_draw_line(x, y0, x, y3, top, top_color);
            } else {
                if top != Stroke::None {
                    self.do_draw_line(x, y0, x, if shorten { y1 } else { y2 }, top, top_color);
                }
                if bottom != Stroke::None {
                    self.do_draw_line(
                        x,
                        if shorten { y2 } else { y1 },
                        x,
                        y3,
                        bottom,
                        bottom_color,
                    );
                }
            }
        }
    }

    fn draw_cell(
        &mut self,
        draw_cell: &DrawCell,
        mut bb: Rect2,
        valign_offset: isize,
        spill: EnumMap<Axis2, [isize; 2]>,
        clip: &Rect2,
    ) {
        let bg = draw_cell.font_style.bg;
        if (bg.r != 255 || bg.g != 255 || bg.b != 255) && bg.alpha != 0 {
            self.context.save().unwrap();
            let bg_clip = Rect2::from_fn(|axis| {
                let start = if bb[axis].start == clip[axis].start {
                    clip[axis].start.saturating_sub(spill[axis][0])
                } else {
                    clip[axis].start
                };
                let end = if bb[axis].end == clip[axis].end {
                    clip[axis].end + spill[axis][1]
                } else {
                    clip[axis].end
                };
                start..end
            });
            xr_clip(self.context, &bg_clip);
            xr_set_color(self.context, bg);
            let x0 = bb[Axis2::X].start.saturating_sub(spill[Axis2::X][0]);
            let y0 = bb[Axis2::Y].start.saturating_sub(spill[Axis2::X][1]);
            let x1 = bb[Axis2::X].end + spill[Axis2::X][1];
            let y1 = bb[Axis2::Y].end + spill[Axis2::Y][1];
            xr_fill_rectangle(self.context, Rect2::new(x0..x1, y0..y1));
            self.context.restore().unwrap();
        }

        if !self.style.use_system_colors {
            xr_set_color(self.context, draw_cell.font_style.fg);
        }

        self.context.save().unwrap();
        bb[Axis2::Y].start += valign_offset;
        for axis in [Axis2::X, Axis2::Y] {
            bb[axis].start += px_to_xr(draw_cell.cell_style.margins[axis][0] as isize);
            bb[axis].end -= px_to_xr(draw_cell.cell_style.margins[axis][1] as isize);
        }
        if bb[Axis2::X].start < bb[Axis2::X].end && bb[Axis2::Y].start < bb[Axis2::Y].end {
            self.cell_draw(draw_cell, bb, clip);
        }
        self.context.restore().unwrap();
    }

    fn scale(&mut self, factor: f64) {
        self.context.scale(factor, factor);
    }
}