From a464f1cfc46502722ac4bbdde19255a59ec62a6a Mon Sep 17 00:00:00 2001 From: Corwin Brust Date: Thu, 25 Jun 2026 07:10:50 -0500 Subject: [PATCH] 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. --- cg-rummy.el | 32 ++++++++++++++++++++++++++++++-- cg-svg.el | 19 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cg-rummy.el b/cg-rummy.el index bf9ba0e..2e68dfc 100644 --- a/cg-rummy.el +++ b/cg-rummy.el @@ -48,6 +48,7 @@ (require 'cl-lib) (require 'eieio) (require 'cg-core) +(require 'cg-svg) (defconst cg-rummy-ranks ["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)))) (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) "Return a propertized row of CARDS. 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 '())) (dolist (c cards) (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)) (push (propertize (format "%4s" cs) 'face (or faces 'default)) out)) (setq i (1+ i))) - (apply #'concat (nreverse out)))) + (apply #'concat (nreverse out))))) (defun cg-rummy--melds-string (melds) "Return a one-line depiction of MELDS (a list of card-lists)." diff --git a/cg-svg.el b/cg-svg.el index 3a74be5..dea4b85 100644 --- a/cg-svg.el +++ b/cg-svg.el @@ -384,5 +384,24 @@ targets. PAD is the margin around the grid." (svg-print svg) (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) ;;; cg-svg.el ends here