Render SVG card faces in the hand-based games

Add cg-svg-hand-svg, a hand-row primitive that fans a list of card
specs with cursor, hint, and mark rings, reusing the existing card art.
Route cg-rummy--render-cards through it on a graphical display, with the
plain-text row kept as the terminal/batch fallback (cg-rummy-svg-cards).

This gives SVG faces to every game that shares that helper: Gin, Rummy,
Rummy 500, Hand & Foot, Go Fish, Old Maid, Cribbage, Scopa, Casino, and
Spite & Malice. Suite still 109/109; batch rendering uses the text path.
This commit is contained in:
Corwin Brust 2026-06-25 07:10:50 -05:00
parent 09adcaa3ea
commit a464f1cfc4
2 changed files with 49 additions and 2 deletions

View file

@ -48,6 +48,7 @@
(require 'cl-lib) (require 'cl-lib)
(require 'eieio) (require 'eieio)
(require 'cg-core) (require 'cg-core)
(require 'cg-svg)
(defconst cg-rummy-ranks (defconst cg-rummy-ranks
["A" "2" "3" "4" "5" "6" "7" "8" "9" "10" "J" "Q" "K"] ["A" "2" "3" "4" "5" "6" "7" "8" "9" "10" "J" "Q" "K"]
@ -278,10 +279,37 @@ NDECKS defaults to 1 and JOKERS to 0."
(sb (if (cg-rummy-joker-p b) 99 (car b)))) (sb (if (cg-rummy-joker-p b) 99 (car b))))
(if (= sa sb) (< (cdr a) (cdr b)) (< sa sb)))))) (if (= sa sb) (< (cdr a) (cdr b)) (< sa sb))))))
(defcustom cg-rummy-svg-cards t
"When non-nil, draw cards as SVG images on a graphical display.
Set to nil to force the plain-text card row everywhere."
:type 'boolean :group 'card-games)
(defun cg-rummy--card-spec (card)
"Return the cg-svg display spec (RANK-STRING . SUIT) for CARD."
(if (cg-rummy-joker-p card) (cons "" 'joker)
(cons (aref cg-rummy-ranks (cdr card)) (car card))))
(defun cg-rummy--svg-row (cards cursor marks hint-fn)
"Return a one-image SVG row for CARDS with CURSOR, MARKS and HINT-FN."
(let* ((specs (mapcar #'cg-rummy--card-spec cards))
(hints (when hint-fn
(let ((hs '()) (i 0))
(dolist (c cards) (when (funcall hint-fn c) (push i hs))
(setq i (1+ i)))
hs)))
(cur (and (integerp cursor) (>= cursor 0) cursor))
(overlap (if (> (length cards) 11) (max 0 (- cg-svg-card-width 24)) 0))
(svg (cg-svg-hand-svg specs :cursor cur :hints hints
:marks marks :overlap overlap)))
(propertize "*" 'display (cg-svg-image svg (cg-scale)))))
(defun cg-rummy--render-cards (cards cursor marks &optional hint-fn) (defun cg-rummy--render-cards (cards cursor marks &optional hint-fn)
"Return a propertized row of CARDS. "Return a propertized row of CARDS.
CURSOR is the highlighted index, MARKS a list of marked indices, and CURSOR is the highlighted index, MARKS a list of marked indices, and
HINT-FN an optional predicate marking playable cards." HINT-FN an optional predicate marking playable cards. Draws SVG cards on
a graphical display (see `cg-rummy-svg-cards'), else a plain-text row."
(if (and cg-rummy-svg-cards (display-graphic-p))
(cg-rummy--svg-row cards cursor marks hint-fn)
(let ((i 0) (out '())) (let ((i 0) (out '()))
(dolist (c cards) (dolist (c cards)
(let ((cs (cg-rummy-card-string c)) (faces nil)) (let ((cs (cg-rummy-card-string c)) (faces nil))
@ -292,7 +320,7 @@ HINT-FN an optional predicate marking playable cards."
(when (eql i cursor) (push 'cg-cursor faces)) (when (eql i cursor) (push 'cg-cursor faces))
(push (propertize (format "%4s" cs) 'face (or faces 'default)) out)) (push (propertize (format "%4s" cs) 'face (or faces 'default)) out))
(setq i (1+ i))) (setq i (1+ i)))
(apply #'concat (nreverse out)))) (apply #'concat (nreverse out)))))
(defun cg-rummy--melds-string (melds) (defun cg-rummy--melds-string (melds)
"Return a one-line depiction of MELDS (a list of card-lists)." "Return a one-line depiction of MELDS (a list of card-lists)."

View file

@ -384,5 +384,24 @@ targets. PAD is the margin around the grid."
(svg-print svg) (svg-print svg)
(buffer-string))) (buffer-string)))
(cl-defun cg-svg-hand-svg (specs &key cursor hints marks (overlap 0) (pad 8))
"Return an svg drawing SPECS as a left-to-right hand.
CURSOR is the index to ring as the cursor; HINTS and MARKS are lists of
indices to ring as playable and as marked; OVERLAP fans the cards."
(let* ((w cg-svg-card-width) (h cg-svg-card-height)
(step (max 1 (- (+ w cg-svg-card-gap) overlap)))
(n (length specs))
(width (+ (* 2 pad) (if (> n 0) (+ (* (1- n) step) w) w)))
(height (+ (* 2 pad) h))
(svg (svg-create width height))
(x pad) (i 0))
(dolist (spec specs)
(cg-svg--draw-spec svg x pad spec (eql i cursor) (and (memq i hints) t))
(when (memq i marks)
(svg-rectangle svg (- x 3) (- pad 3) (+ w 6) (+ h 6)
:rx 7 :fill "none" :stroke "#4a90d9" :stroke-width 3))
(setq x (+ x step) i (1+ i)))
svg))
(provide 'cg-svg) (provide 'cg-svg)
;;; cg-svg.el ends here ;;; cg-svg.el ends here