AtlatestRepositorysigil-ui
1
(import (sigil test)2
(sigil ui color)3
(sigil ui raster)4
(sigil ui font)5
(sigil ui mask)6
(sigil ui display))8
;; A two-glyph 8x2 font: "A" is a full top row, "B" the left pixel of each row.9
(define tiny-1x10
(make-bitmap-font "tiny-8x2" 8 2 (bytevector #xFF #x00 #x80 #x80) (list 65 66)))11
;; Its double: 16x4, two bytes per row.12
(define tiny-2x13
(make-bitmap-font "tiny-16x4" 16 414
(bytevector #xFF #xFF #xFF #xFF #x00 #x00 #x00 #x0015
#xC0 #x00 #xC0 #x00 #xC0 #x00 #xC0 #x00)16
(list 65 66)))17
(define tiny (make-font-set (list (cons 1 tiny-1x) (cons 2 tiny-2x))))19
(test-group "color"20
(test "hex and channels"21
(assert-equal #x0E0E0C (hex "#0E0E0C"))22
(assert-equal 237 (color-red (hex "#EDBB45")))23
(assert-equal "#EDBB45" (color->hex (rgb 237 187 69))))24
(test "refuses bad input"25
(assert-error (hex "0E0E0C"))26
(assert-error (rgb 256 0 0))))28
(test-group "canvas"29
(test "fill and read back"30
(let ((c (make-canvas 4 3 #x102030)))31
(assert-equal #x102030 (canvas-pixel c 3 2))32
(canvas-fill! c 1 1 2 1 #xFFFFFF)33
(assert-equal #xFFFFFF (canvas-pixel c 1 1))34
(assert-equal #xFFFFFF (canvas-pixel c 2 1))35
(assert-equal #x102030 (canvas-pixel c 3 1))36
(assert-equal 2 (canvas-count-other c 0 0 4 3 #x102030))))37
(test "fill is clipped to the canvas and the clip"38
(let ((c (make-canvas 4 4)))39
(canvas-fill! c -2 -2 10 10 #x0000FF (list 1 1 3 2))40
(assert-equal 2 (canvas-count-other c 0 0 4 4 0))41
(assert-equal #x0000FF (canvas-pixel c 2 1))))42
(test "blend uses (bg (255 - a) + fg a + 127) / 255"43
(let ((c (make-canvas 1 1 #x0E0E0C)))44
;; level 8 of 15: a = 136; white F2F0EA over 0E0E0C45
(canvas-blend-fill! c 0 0 1 1 #xF2F0EA 136)46
(assert-equal (rgb (quotient (+ (* 14 119) (* 242 136) 127) 255)47
(quotient (+ (* 14 119) (* 240 136) 127) 255)48
(quotient (+ (* 12 119) (* 234 136) 127) 255))49
(canvas-pixel c 0 0))))50
(test "mask level 255 writes the color exactly, level 0 leaves the pixel"51
(let ((c (make-canvas 2 1 #x111111)))52
(canvas-mask! c 0 0 (bytevector 255 0) 2 #xABCDEF)53
(assert-equal #xABCDEF (canvas-pixel c 0 0))54
(assert-equal #x111111 (canvas-pixel c 1 0))))55
(test "bits draw set bits only"56
(let ((c (make-canvas 8 2 #x000000)))57
(canvas-bits! c 0 0 (bitmap-font-glyphs tiny-1x) 2 8 2 #xFF0000)58
(assert-equal #xFF0000 (canvas-pixel c 0 0))59
(assert-equal #xFF0000 (canvas-pixel c 0 1))60
(assert-equal 0 (canvas-pixel c 1 0))))61
(test "rgb export is R G B"62
(let ((c (make-canvas 1 1 #x123456)))63
(assert-equal (bytevector #x12 #x34 #x56) (canvas->rgb c))))64
(test "refuses a bytevector that is not a canvas"65
(assert-error (canvas-fill! (make-canvas 2 2) 0 0 1 1 300000000))))67
(test-group "font"68
(test "offsets and coverage"69
(assert-equal 0 (font-glyph-offset tiny-1x 65))70
(assert-equal 2 (font-glyph-offset tiny-1x 66))71
(assert-false (font-has? tiny-1x 67)))72
(test "a font set advances the same logical width at every scale"73
(assert-equal 8 (font-set-advance tiny))74
(assert-equal 2 (font-set-height tiny))75
(assert-equal 16 (text-width tiny "AB")))76
(test "a set whose fonts disagree is refused"77
(assert-error (make-font-set (list (cons 1 tiny-1x) (cons 3 tiny-2x)))))78
(test "missing glyphs and em dashes are refused"79
(assert-equal 67 (missing-codepoint tiny "ABC"))80
(assert-error (text-item 0 0 "AC" 0 tiny))81
(assert-error (check-text tiny (string #\A (integer->char #x2014))))))83
(test-group "mask"84
(test "a4rle decodes runs across rows"85
;; 3x2: run of 4 at level 15, then 2 at level 086
(let ((m (decode-a4rle (bytevector #x3F #x10) 3 2)))87
(assert-equal (bytevector 255 255 255 255 0 0) (mask-alpha m))))88
(test "a4rle round trips"89
(let ((m (make-mask 5 1 (bytevector 0 17 17 255 255))))90
(assert-equal (mask-alpha m) (mask-alpha (decode-a4rle (encode-a4rle m) 5 1)))))91
(test "a4rle refuses the wrong length"92
(assert-error (decode-a4rle (bytevector #x3F) 3 2))93
(assert-error (decode-a4rle (bytevector #xFF #xFF) 3 2))))95
(test-group "display"96
(test "text at scale 2 lands at (2x, 2y) with the 2x font"97
(let ((c (make-canvas 40 8 0)))98
(render-items! c (list (text-item 2 1 "BA" #x00FF00 tiny)) 2)99
;; "B" at device (4, 2): left two columns set on all four rows100
(assert-equal #x00FF00 (canvas-pixel c 4 2))101
(assert-equal #x00FF00 (canvas-pixel c 5 5))102
(assert-equal 0 (canvas-pixel c 6 2))103
;; "A" advances 16 device px: full top two rows from x 20104
(assert-equal #x00FF00 (canvas-pixel c 35 3))105
(assert-equal 0 (canvas-pixel c 20 4))))106
(test "spaces draw nothing"107
(let ((c (make-canvas 16 2 0))108
(f (make-font-set (list (cons 1 (make-bitmap-font "sp" 8 2 (bytevector #xFF #xFF #xFF #xFF) (list 32 65)))))))109
(render-items! c (list (text-item 0 0 " A" #xFFFFFF f)) 1)110
(assert-equal 0 (canvas-pixel c 0 0))111
(assert-equal #xFFFFFF (canvas-pixel c 8 0))))112
(test "text off the canvas is refused"113
(assert-error (render-items! (make-canvas 10 4) (list (text-item 4 0 "AB" 1 tiny)) 1)))114
(test "repainting damaged rectangles equals a full render"115
(let* ((items (list (fill-item 0 0 20 10 #x0E0E0C)116
(fill-item 2 2 10 4 #xEDBB45)117
(text-item 1 6 "AB" #xE6E2D5 tiny)))118
(full (make-canvas 40 20 0))119
(part (make-canvas 40 20 0)))120
(render-items! full items 2)121
(render-items! part (list (fill-item 0 0 20 10 #x123456)) 2)122
;; scribble everywhere, then repaint the whole area in two halves123
(repaint-rects! part items 2 #x0E0E0C (list (list 0 0 20 5) (list 0 5 20 5)))124
(assert-equal (canvas->rgb full) (canvas->rgb part)))))126
(test-group "charset"127
(test "parse-charset reads code points in index order and skips blank lines"128
(assert-equal '(65 8230 32) (parse-charset "0\tU+0041\tA\n1\tU+2026\t…\n\n2\tU+0020\t \n")))129
(test "parse-charset refuses a line out of order or without U+"130
(assert-error (parse-charset "1\tU+0041\tA\n"))131
(assert-error (parse-charset "0\t0041\tA\n"))))