Hello,

TL;DR: `org--get-display-dpi` calculated the dpi with data from different
monitors, i fixed it and now it returns the highest correct dpi.

I noticed that when I connected my laptop to a new monitor the latex fragment scaling changed. So I looked under `org-create-formula-image` (org.el) how the scaling was computed, and if I'm not mistaken it scales based on the dpi. The dpi is computed with `org--get-display-dpi`, I fiddled with the function and I
discovered that it computed the dpi using `display-pixel-height` and
`display-mm-height`.

The problem is that one returns the pixel-height of one screen and the other
returns the mm-height of the other one (the plugged one). Seemingly these
function returned the highest value for each field.

My original goal was to have the scaling always refer to one monitor, or in any
case be consistent in such a way that simply the latex fragment previews
wouldn't just change from one moment to the other. In fact whilst the `:scale` factor (of `org-format-latex-options`) is fixed, the dpi calculating function retrieves the monitor parameters dynamically. But I just started learning lisp yesterday and currently my patch works for my use-case, but most importantly it
properly calculates the dpi.

The patch retrieves its data from `display-monitor-attributes-list` which seems more consistent than the previously used functions, albeit having more overhead (parsing is to be done). It also accounts for the scaling field provided by the
aforementioned function.

TODO Let users have a default monitor of reference for the `:scale` attribute.
This patch is temporary as it does not fix the problem of varying latex
fragments scaling if a new monitor with higher dpi is connected.

This is the first time I submit code to a project so any critique is welcomed!

William
From 1cb6e40b9af61b63bd14ec15f04996eb743f21bb Mon Sep 17 00:00:00 2001
From: William <william.br...@studio.unibo.it>
Date: Sat, 3 May 2025 00:28:47 +0200
Subject: [PATCH] TMP BUGFIX: Correct dpi scaling

---
 lisp/org.el | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 9e8bf8c75..da134d8c0 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16494,10 +16494,20 @@ This uses  `org-latex-to-html-convert-command', which see."
   "Get the DPI of the display.
 The function assumes that the display has the same pixel width in
 the horizontal and vertical directions."
-  (if (display-graphic-p)
-      (round (/ (display-pixel-height)
-		(/ (display-mm-height) 25.4)))
-    (error "Attempt to calculate the dpi of a non-graphic display")))
+  (cl-labels
+      ((compute-dpi (list-of-attr)
+         "Compute the DPI for a given LIST-OF-ATTR."
+         (let* ((geometry (seq-find (lambda (prop) (eq (car prop) 'geometry)) list-of-attr))
+                (mm-size (seq-find (lambda (prop) (eq (car prop) 'mm-size)) list-of-attr))
+                (scale-factor (seq-find (lambda (prop) (eq (car prop) 'scale-factor)) list-of-attr))
+                (height-px (and geometry (nth 4 geometry)))
+                (height-mm (and mm-size (nth 2 mm-size)))
+                (scale (and scale-factor (cdr scale-factor))))
+           (when (and height-px height-mm scale)
+             (round (/ (/ height-px scale) (/ height-mm 25.4)))))))
+    (if (display-graphic-p)
+        (seq-max (mapcar #'compute-dpi (display-monitor-attributes-list)))
+      (error "Attempt to calculate the dpi of a non-graphic display"))))
 
 (defun org-create-formula-image
     (string tofile options buffer &optional processing-type)
-- 
2.49.0

Reply via email to