all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Third <alan@idiocy.org>
To: Yuan Fu <casouri@gmail.com>, Lars Ingebrigtsen <larsi@gnus.org>,
	Eli Zaretskii <eliz@gnu.org>, Robert Pluim <rpluim@gmail.com>,
	emacs-devel <emacs-devel@gnu.org>,
	monnier@iro.umontreal.ca
Subject: Re: "Fix" sag scaling for hidpi
Date: Fri, 12 Feb 2021 22:47:39 +0000	[thread overview]
Message-ID: <YCcFi11eSkuJ04qr@breton.holly.idiocy.org> (raw)
In-Reply-To: <YCWRIXdr+CRcU3XF@breton.holly.idiocy.org>

[-- Attachment #1: Type: text/plain, Size: 911 bytes --]

On Thu, Feb 11, 2021 at 08:18:41PM +0000, Alan Third wrote:
> On Thu, Feb 11, 2021 at 12:34:50PM -0500, Yuan Fu wrote:
> > Actually, :pixel-ratio probably isn’t a good idea. I guess we should
> > just let the user decide what they want when inserting an image, and
> > automatically add :scale in image-mode.
> 
> Yes, I agree. That's really what I was struggling with, getting
> image-mode to load the image at physical pixel size. I don't do much
> lisp coding, so I'm quite slow to get to understand it.
> 
> I'll keep looking, though.

OK, I've got something that works here. It seemed a bit too easy given
how much code there is in image-mode.el to do with scaling, but as I
said, it seems good to me.

(And I've just realised when attaching this that the commit name is
nonsense, but I imagine it will require a NEWS entry that I've not
written, so this won't be the final patch anyway.)
-- 
Alan Third

[-- Attachment #2: 0001-Scale-SVGs-to-match-frame-s-scale-factor.patch --]
[-- Type: text/plain, Size: 6368 bytes --]

From 62cbe4ed2376deeb3a6fc5315b938018cb71ef78 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Wed, 10 Feb 2021 22:12:16 +0000
Subject: [PATCH] Scale SVGs to match frame's scale factor

* lisp/image-mode.el (image-vector-type): New variable.
(image-default-scale): New function.
(image-toggle-display-image): Use image-default-scale.
* src/image.c (FRAME_SCALE_FACTOR): New #define for getting frame
scale factor.
(image_set_transform):
(svg_load_image): Use FRAME_SCALE_FACTOR.
(Fframe_scale_factor): Returns the frame's scale factor.
* src/nsterm.m (ns_frame_scale_factor): Get the scale factor for an NS
frame.
---
 lisp/image-mode.el | 19 +++++++++++++++++--
 src/image.c        | 35 +++++++++++++++++++++++++++++++----
 src/nsterm.h       |  1 +
 src/nsterm.m       | 11 +++++++++++
 4 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 9ed295e2aa..4b5e282328 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -100,6 +100,11 @@ image-transform-right-angle-fudge
 There's no deep theory behind the default value, it should just
 be somewhat larger than ImageMagick's MagickEpsilon.")
 
+(defvar image-vector-type '(svg)
+  "List of types that won't be rescaled by the frame's scale factor.
+Vector image types should not be scaled down to match the
+screen's physical pixel dimensions.")
+
 (defun image-mode-winprops (&optional window cleanup)
   "Return winprops of WINDOW.
 A winprops object has the shape (WINDOW . ALIST).
@@ -435,6 +440,14 @@ image-mode-fit-frame
 				     (cons width height)
 				     window-configuration)))))))
 
+(defun image-default-scale (type)
+  "Return the default scale amount for image type TYPE.
+Should be 1 / frame-scale-factor for all image types except
+vector image types listed in `image-vector-type' that don't
+require rescaling."
+    (if (member type image-vector-type) 1.0
+      (/ 1 (frame-scale-factor))))
+
 ;;; Image Mode setup
 
 (defvar-local image-type nil
@@ -868,9 +881,11 @@ image-toggle-display-image
     ;; :scale 1: If we do not set this, create-image will apply
     ;; default scaling based on font size.
     (setq image (if (not edges)
-		    (create-image file-or-data type data-p :scale 1
+		    (create-image file-or-data type data-p
+                                  :scale (image-default-scale type)
                                   :format (and filename data-p))
-		  (create-image file-or-data type data-p :scale 1
+		  (create-image file-or-data type data-p
+                                :scale (image-default-scale type)
 				:max-width max-width
 				:max-height max-height
                                 ;; Type hint.
diff --git a/src/image.c b/src/image.c
index a124cf91ba..3f06860794 100644
--- a/src/image.c
+++ b/src/image.c
@@ -135,6 +135,12 @@ #define PIX_MASK_DRAW	1
 # define COLOR_TABLE_SUPPORT 1
 #endif
 
+#ifdef HAVE_NS
+# define FRAME_SCALE_FACTOR(f) ns_frame_scale_factor (f)
+#else
+# define FRAME_SCALE_FACTOR(f) 1;
+#endif
+
 static void image_disable_image (struct frame *, struct image *);
 static void image_edge_detection (struct frame *, struct image *, Lisp_Object,
                                   Lisp_Object);
@@ -2207,8 +2213,8 @@ image_set_transform (struct frame *f, struct image *img)
   /* SVGs are pre-scaled to the correct size.  */
   if (EQ (image_spec_value (img->spec, QCtype, NULL), Qsvg))
     {
-      width = img->width;
-      height = img->height;
+      width = img->width / FRAME_SCALE_FACTOR (f);
+      height = img->height / FRAME_SCALE_FACTOR (f);
     }
   else
 #endif
@@ -10008,6 +10014,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents,
   compute_image_size (viewbox_width, viewbox_height, img->spec,
                       &width, &height);
 
+  width *= FRAME_SCALE_FACTOR (f);
+  height *= FRAME_SCALE_FACTOR (f);
+
   if (! check_image_size (f, width, height))
     {
       image_size_error ();
@@ -10499,9 +10508,8 @@ DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0,
 
 #endif /* GLYPH_DEBUG */
 
-
 /***********************************************************************
-			    Initialization
+			    Image Scaling
  ***********************************************************************/
 
 DEFUN ("image-transforms-p", Fimage_transforms_p, Simage_transforms_p, 0, 1, 0,
@@ -10538,6 +10546,24 @@ DEFUN ("image-transforms-p", Fimage_transforms_p, Simage_transforms_p, 0, 1, 0,
   return Qnil;
 }
 
+DEFUN ("frame-scale-factor", Fframe_scale_factor, Sframe_scale_factor, 0, 1, 0,
+       doc: /* Return the image scale factor for FRAME.  */)
+       (Lisp_Object frame)
+{
+  struct frame *f = decode_live_frame (frame);
+  if (FRAME_WINDOW_P (f))
+    {
+      return make_float (FRAME_SCALE_FACTOR (f));
+    }
+
+  return make_float (1);
+}
+
+
+/***********************************************************************
+			    Initialization
+ ***********************************************************************/
+
 DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 1, 1, 0,
        doc: /* Initialize image library implementing image type TYPE.
 Return t if TYPE is a supported image type.
@@ -10820,6 +10846,7 @@ syms_of_image (void)
 #endif
 
   defsubr (&Simage_transforms_p);
+  defsubr (&Sframe_scale_factor);
 
   DEFVAR_BOOL ("cross-disabled-images", cross_disabled_images,
     doc: /* Non-nil means always draw a cross over disabled images.
diff --git a/src/nsterm.h b/src/nsterm.h
index eae1d0725e..017c2394ef 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -1252,6 +1252,7 @@ #define NSAPP_DATA2_RUNFILEDIALOG 11
 extern void ns_init_events (struct input_event *);
 extern void ns_finish_events (void);
 
+extern double ns_frame_scale_factor (struct frame *);
 
 #ifdef NS_IMPL_GNUSTEP
 extern char gnustep_base_version[];  /* version tracking */
diff --git a/src/nsterm.m b/src/nsterm.m
index 3aba76ff5e..c095212d3a 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -857,6 +857,17 @@ Free a pool and temporary objects it refers to (callable from C)
 }
 
 
+double
+ns_frame_scale_factor (struct frame *f)
+{
+#ifdef NS_IMPL_COCOA
+  return [[FRAME_NS_VIEW (f) window] backingScaleFactor];
+#else
+  return 1;
+#endif
+}
+
+
 /* ==========================================================================
 
     Focus (clipping) and screen update
-- 
2.29.2


  reply	other threads:[~2021-02-12 22:47 UTC|newest]

Thread overview: 192+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-04 20:49 "Fix" sag scaling for hidpi Yuan Fu
2021-02-04 22:07 ` Stefan Monnier
2021-02-04 23:10   ` Yuan Fu
2021-02-05  9:02     ` Lars Ingebrigtsen
2021-02-05 10:24       ` Robert Pluim
2021-02-06 10:00         ` Lars Ingebrigtsen
2021-02-06 11:15           ` Eli Zaretskii
2021-02-06 11:35             ` Lars Ingebrigtsen
2021-02-06 12:20               ` Eli Zaretskii
2021-02-07 12:05                 ` Lars Ingebrigtsen
2021-02-07 13:14               ` Robert Pluim
2021-02-09 20:01               ` Alan Third
2021-02-10  8:29                 ` Lars Ingebrigtsen
2021-02-10 18:00                 ` Yuan Fu
2021-02-10 18:07                   ` Yuan Fu
2021-02-10 18:17                     ` Lars Ingebrigtsen
2021-02-10 18:24                       ` Robert Pluim
2021-02-10 18:41                         ` Lars Ingebrigtsen
2021-02-10 18:52                         ` Basil L. Contovounesios
2021-02-11 13:53                           ` Robert Pluim
2021-02-11 14:30                             ` Basil L. Contovounesios
2021-02-11 15:11                               ` Robert Pluim
2021-02-11 22:18                                 ` Cleaning out old X11 toolkits? chad
2021-02-12  7:09                                   ` Eli Zaretskii
2021-02-12  8:44                                     ` Colin Baxter
2021-02-12 11:22                                       ` Eli Zaretskii
2021-02-12  9:30                                     ` Robert Pluim
2021-02-12  9:35                                       ` Lars Ingebrigtsen
2021-02-12 10:00                                         ` Robert Pluim
2021-02-12 10:02                                           ` Lars Ingebrigtsen
2021-02-12 10:04                                             ` Lars Ingebrigtsen
2021-02-12 10:18                                               ` Robert Pluim
2021-02-12 10:30                                                 ` Lars Ingebrigtsen
2021-02-12 21:49                                         ` Jean Louis
2021-02-12 22:03                                           ` Jean Louis
2021-02-12 22:45                                             ` Stefan Kangas
2021-02-13  7:51                                           ` Eli Zaretskii
2021-02-13 12:05                                             ` Jean Louis
2021-02-12 11:28                                       ` Eli Zaretskii
2021-02-12 11:38                                         ` tomas
2021-02-12 13:26                                         ` Robert Pluim
2021-02-12 13:57                                           ` Basil L. Contovounesios
2021-02-15 16:49                                         ` Sean Whitton
2021-02-12 11:47                                     ` Ulrich Mueller
2021-02-12 10:00                                   ` martin rudalics
2021-02-12 10:14                                     ` Robert Pluim
2021-02-12 17:56                                       ` martin rudalics
2021-02-12 18:14                                         ` Merging native-comp and pgtk Stefan Monnier
2021-02-12 18:36                                           ` Eli Zaretskii
2021-02-12 22:29                                             ` Andy Moreton
2021-02-12 23:06                                               ` Stefan Monnier
2021-02-12 23:28                                                 ` Andy Moreton
2021-02-13  9:39                                                 ` Eli Zaretskii
2021-02-13 13:27                                                   ` Stefan Monnier
2021-02-13 14:10                                                     ` Andy Moreton
2021-02-13 14:14                                                       ` Eli Zaretskii
2021-02-13 14:17                                                     ` Eli Zaretskii
2021-02-13 15:48                                                     ` martin rudalics
2021-02-13 15:58                                                       ` Eli Zaretskii
2021-02-14  8:34                                                         ` martin rudalics
2021-02-13  9:20                                               ` Eli Zaretskii
2021-02-13 13:07                                                 ` Andy Moreton
2021-02-13 14:16                                                   ` Eli Zaretskii
2021-02-13 18:01                                                     ` Andy Moreton
2021-02-13 18:12                                                       ` Eli Zaretskii
2021-02-14  7:33                                                 ` Andrea Corallo via Emacs development discussions.
2021-02-14 15:25                                                   ` Eli Zaretskii
2021-02-13  9:26                                               ` Andreas Schwab
2021-02-13 12:45                                                 ` Andy Moreton
2021-02-16 23:22                                               ` Phillip Lord
2021-02-17 15:32                                                 ` Eli Zaretskii
2021-02-17 17:32                                                   ` Andy Moreton
2021-02-17 18:09                                                     ` Óscar Fuentes
2021-02-17 22:20                                                       ` Andy Moreton
2021-02-17 22:52                                                         ` Óscar Fuentes
2021-02-17 19:20                                                     ` Eli Zaretskii
2021-02-12 23:04                                             ` Stefan Monnier
2021-02-13  9:30                                               ` Eli Zaretskii
2021-02-13 13:24                                                 ` Stefan Monnier
2021-02-13 14:15                                                   ` Eli Zaretskii
2021-02-12 21:47                                           ` Andrea Corallo via Emacs development discussions.
2021-02-13  7:39                                             ` Eli Zaretskii
2021-02-13 11:16                                             ` Lars Ingebrigtsen
2021-02-13 11:28                                               ` Tassilo Horn
2021-02-13 11:57                                                 ` Lars Ingebrigtsen
2021-02-13 12:17                                                   ` Dmitry Gutov
2021-02-13 14:53                                                     ` Lars Ingebrigtsen
2021-02-13 20:30                                                       ` Dmitry Gutov
2021-02-13 21:03                                                         ` Lars Ingebrigtsen
2021-02-13 21:30                                                           ` Dmitry Gutov
2021-02-13 21:42                                                             ` Lars Ingebrigtsen
2021-02-13 22:48                                                               ` Lars Ingebrigtsen
2021-02-13 23:44                                                                 ` Tassilo Horn
2021-02-13 17:38                                                   ` Stefan Kangas
2021-02-14 18:36                                                     ` Andrea Corallo via Emacs development discussions.
2021-02-14 19:23                                                       ` Stefan Kangas
2021-02-14 19:32                                                         ` Andrea Corallo via Emacs development discussions.
2021-02-14 23:18                                                           ` Óscar Fuentes
2021-02-15 14:15                                                             ` Andrea Corallo via Emacs development discussions.
2021-02-13 13:41                                               ` Stefan Monnier
2021-02-13 14:18                                                 ` Eli Zaretskii
2021-02-13 20:32                                                   ` Dmitry Gutov
2021-02-14  3:34                                                     ` Eli Zaretskii
2021-02-14  4:32                                                   ` Stefan Monnier
2021-02-13 14:58                                                 ` Lars Ingebrigtsen
2021-02-14 18:34                                                   ` Andrea Corallo via Emacs development discussions.
2021-02-14  7:42                                               ` Andrea Corallo via Emacs development discussions.
2021-02-12 18:32                                         ` Cleaning out old X11 toolkits? Eli Zaretskii
2021-02-13  9:01                                           ` martin rudalics
2021-02-13  9:44                                             ` Eli Zaretskii
2021-02-13 10:29                                               ` martin rudalics
2021-02-14 12:50                                           ` Robert Pluim
2021-02-14 15:53                                             ` Eli Zaretskii
2021-02-15  9:48                                               ` Robert Pluim
2021-02-15 15:23                                                 ` Eli Zaretskii
2021-02-15 16:02                                                   ` Robert Pluim
2021-02-15 16:13                                                     ` tomas
2021-02-15 17:03                                                     ` Eli Zaretskii
2021-02-15 17:20                                                       ` Robert Pluim
2021-02-12 13:04                                     ` Dmitry Gutov
2021-02-12 17:56                                       ` martin rudalics
2021-02-12 18:33                                         ` Eli Zaretskii
2021-02-12 20:09                                           ` martin rudalics
2021-02-12 20:13                                             ` Eli Zaretskii
2021-02-13  9:02                                               ` martin rudalics
2021-02-13  9:47                                                 ` Eli Zaretskii
2021-02-13 10:29                                                 ` Colin Baxter
2021-02-13 13:24                                                   ` martin rudalics
2021-02-13 13:53                                                     ` Ulrich Mueller
2021-02-13 15:49                                                       ` martin rudalics
2021-02-13 19:23                                                         ` Jean Louis
2021-02-14  8:35                                                           ` martin rudalics
2021-02-14  9:33                                                             ` Ulrich Mueller
2021-02-14 17:16                                                               ` martin rudalics
2021-02-14 17:48                                                               ` Eli Zaretskii
2021-02-14 18:18                                                                 ` Andy Moreton
2021-02-14 19:15                                                                   ` Eli Zaretskii
2021-02-14 22:33                                                                     ` Óscar Fuentes
2021-02-15  8:17                                                                     ` martin rudalics
2021-02-15  9:19                                                                     ` Andy Moreton
2021-02-13 14:39                                                     ` Colin Baxter
2021-02-13 14:43                                                       ` Jean Louis
2021-02-13 15:50                                                       ` martin rudalics
2021-02-13 16:04                                                         ` Colin Baxter
2021-02-13 21:42                                                 ` Stefan Monnier
2021-02-14 12:41                                                   ` Robert Pluim
2021-02-14 15:51                                                     ` Eli Zaretskii
2021-02-14 18:18                                                       ` Stefan Monnier
2021-02-14 19:10                                                         ` Eli Zaretskii
2021-02-12 11:20                                   ` Ulrich Mueller
2021-02-12 13:33                                   ` James Cloos
2021-02-12 13:49                                     ` Robert Pluim
2021-02-10 19:19                         ` "Fix" sag scaling for hidpi Stefan Monnier
2021-02-11 14:01                           ` Robert Pluim
2021-02-11 14:59                             ` Stefan Monnier
2021-02-11 15:16                               ` Robert Pluim
2021-02-10 23:55                   ` Alan Third
2021-02-11  2:01                     ` Yuan Fu
2021-02-11 17:34                       ` Yuan Fu
2021-02-11 20:18                         ` Alan Third
2021-02-12 22:47                           ` Alan Third [this message]
2021-02-13 11:13                             ` Lars Ingebrigtsen
2021-02-13 11:17                               ` Alan Third
2021-02-13 11:52                                 ` Lars Ingebrigtsen
2021-02-13 12:54                                   ` Alan Third
2021-02-13 13:09                                     ` Alan Third
2021-02-13 15:09                                       ` Lars Ingebrigtsen
2021-02-13 16:00                                         ` Alan Third
2021-02-13 23:30                                           ` Alan Third
2021-02-13 20:09                                         ` Yuan Fu
2021-02-13 21:14                                           ` Lars Ingebrigtsen
2021-02-13 22:50                                       ` Dmitry Gutov
2021-02-13 23:14                                         ` Alan Third
2021-02-14  0:02                                           ` Dmitry Gutov
2021-02-14  2:06                                             ` Yuan Fu
2021-02-14  2:09                                               ` Yuan Fu
2021-02-14 12:19                                               ` Dmitry Gutov
2021-02-14  9:58                                             ` Alan Third
2021-02-13 15:08                                     ` Lars Ingebrigtsen
2021-02-13 15:59                                       ` Alan Third
2021-02-13 21:08                                         ` Lars Ingebrigtsen
2021-02-13 21:53                                           ` Alan Third
2021-02-13 21:57                                             ` Lars Ingebrigtsen
2021-02-13 22:13                                               ` Alan Third
2021-02-13 18:29                                       ` chad
2021-02-13 19:52                                         ` chad
2021-02-13 20:06                                       ` Yuan Fu
2021-02-13 21:11                                         ` Lars Ingebrigtsen
2021-02-13 23:20                                           ` Alan Third
2021-02-09 17:26         ` Grzegorz Kowzan
2021-02-05 20:29       ` Alan Third
2021-02-07 21:58     ` Alan Third

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YCcFi11eSkuJ04qr@breton.holly.idiocy.org \
    --to=alan@idiocy.org \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=rpluim@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.