From 9487ffef982ad0f9fb9e0e1930b3c5d82a50085f Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 13 May 2026 06:27:38 -0500 Subject: refactor(calibredb-epub): extract the nov image-padding helper I pulled the image-centering math out of `cj/nov-center-images` into `cj/--nov-image-padding-cols`. The wrapper still loops over the image text properties and writes the line/wrap-prefix. The helper takes col-width, img-px, and font-width-px and returns the left padding. With the math in a pure helper I can unit-test the centering rule directly instead of building a buffer with image-display properties. --- modules/calibredb-epub-config.el | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/calibredb-epub-config.el b/modules/calibredb-epub-config.el index b8c2a9c0..c1dba8b5 100644 --- a/modules/calibredb-epub-config.el +++ b/modules/calibredb-epub-config.el @@ -261,6 +261,16 @@ A positive DELTA narrows the text column; a negative DELTA widens it." ("t" . nov-goto-toc) ("C-c C-b" . cj/nov-jump-to-calibredb))) +(defun cj/--nov-image-padding-cols (col-width img-px font-width-px) + "Return left-padding columns to center an IMG-PX-wide image in COL-WIDTH cols. +FONT-WIDTH-PX is the column width in pixels; clamped up to 1 so a zero or +negative value can't divide. When the image is at least as wide as COL-WIDTH +the result is 0 -- no centering is possible." + (let* ((fw (max 1 font-width-px)) + (img-cols (max 1 (ceiling (/ (float img-px) fw)))) + (pad (/ (- col-width img-cols) 2))) + (max 0 pad))) + (defun cj/nov-center-images () "Center images in the current Nov buffer without modifying text. @@ -283,10 +293,8 @@ computed column based on the window text area width." (when m (let* ((img (prop-match-value m)) (img-px (car (image-size img t))) ;; pixel width - ;; Convert pixel image width to columns for alignment. - (img-cols (max 1 (ceiling (/ (float img-px) - (max 1 (window-font-width win)))))) - (pad-cols (max 0 (/ (- col-width img-cols) 2))) + (pad-cols (cj/--nov-image-padding-cols + col-width img-px (window-font-width win))) (prefix (propertize " " 'display `(space :align-to ,pad-cols)))) (save-excursion (goto-char (prop-match-beginning m)) -- cgit v1.2.3