zoliky · 2022-08-05 14:42
zoliky · 2022-08-05 14:47
The config I'm using
(use-package nov
:mode ("\\.epub\\'" . nov-mode)
:preface
(defun zoli/nov-font-setup ()
(face-remap-add-relative 'variable-pitch
:family "ChareInk"
:height 210)
(setq line-spacing 0.1))
:hook (nov-mode . zoli/nov-font-setup)
:custom
(nov-text-width t))
The [https://depp.brause.cc/nov.el](https://depp.brause.cc/nov.el) website mentions something about custom rendering but the code is too difficult for me to understand. The justification of the text is fine. The only thing I want is to center the images in the EPUBs.
Gh0st1y · 2022-08-05 15:36
Ha, no effing way, of course there's an ebook reader.
karthink · 2022-08-05 19:16
As of Emacs 28.1, there's no built-in way to center an image accurately in Emacs, so you'll have to settle for a rough hack.
(defun nov-center-images ()
"Center images in document."
(let* ((pixel-buffer-width (shr-pixel-buffer-width))
match)
(save-excursion
(goto-char (point-min))
(while (setq match (text-property-search-forward
'display nil
(lambda (_ p) (eq (car-safe p) 'image))))
(when-let ((size (car (image-size
(prop-match-value match) 'pixels)))
((> size 150))
(center-pixel (floor (- pixel-buffer-width size) 2))
(center-pos (floor center-pixel (frame-char-width))))
(beginning-of-line)
(indent-to center-pos)
(end-of-line))))))
(add-hook 'nov-post-html-render-hook 'nov-center-images)
This will center _all_ images (in nov) over 150px in width, even inline ones. To avoid this, you can add an additional condition to the `when-let` that checks if the image is the only DOM element in the line. (If you do, please reply here so I can use it too.)
While we're talking about nov/shr, does someone know if there's a way to detect and invert image colors natively in Emacs? That is, without using imagemagick. I'd like to do this when using a dark theme.