From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Third Newsgroups: gmane.emacs.devel Subject: Re: "Fix" sag scaling for hidpi Date: Fri, 12 Feb 2021 22:47:39 +0000 Message-ID: References: <87pn1erewq.fsf@gmail.com> <87wnvlecrw.fsf@gnus.org> <83sg69o3av.fsf@gnu.org> <87mtwhctte.fsf@gnus.org> <9C04F72C-1BB6-4308-AD8E-4A2B471CAC4E@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="5r/HHDo1qNp2cTt8" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9138"; mail-complaints-to="usenet@ciao.gmane.io" To: Yuan Fu , Lars Ingebrigtsen , Eli Zaretskii , Robert Pluim , emacs-devel , monnier@iro.umontreal.ca Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 12 23:50:31 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lAhGh-0002I0-DL for ged-emacs-devel@m.gmane-mx.org; Fri, 12 Feb 2021 23:50:31 +0100 Original-Received: from localhost ([::1]:47066 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAhGg-0000r6-Fl for ged-emacs-devel@m.gmane-mx.org; Fri, 12 Feb 2021 17:50:30 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59460) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAhEM-0008Qg-G4 for emacs-devel@gnu.org; Fri, 12 Feb 2021 17:48:06 -0500 Original-Received: from outbound.soverin.net ([116.202.65.218]:56461) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAhEC-0008TA-S5; Fri, 12 Feb 2021 17:48:06 -0500 Original-Received: from smtp.soverin.net (unknown [10.10.3.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by outbound.soverin.net (Postfix) with ESMTPS id E49F66008F; Fri, 12 Feb 2021 22:47:45 +0000 (UTC) Original-Received: from smtp.soverin.net (smtp.soverin.net [159.69.232.142]) by soverin.net DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=idiocy.org; s=soverin; t=1613170064; bh=ZCyfTnStU7kZ2jHeUtuay5CK+1cTK+Z7tQLH5gRNCjw=; h=Date:From:To:Subject:References:In-Reply-To:From; b=SYvhNsfDOW8NQCr/nAtSiXM9Xr8Q33FW0YgFfl1EzrPpi3S3rplosLWQAapYaJggd 2zHHgKySxGtKswW+gvaCQ0It9zilT3fnaKMNncZocVmsAfqdT5fE8b0lMva+gFQZBA KNiupuDR/bidwgHGrJC8HSWMigxk9eGcIHwHNyVDxZXM35Ajj0BJUkN7xmk657eBdi tGNoCWhWc2FQYtdPMS4oWh8XRkSq9Cw81BjdLDlT7GQkMHqEm55iQfFFndIwWq1NJ6 ESC/miFMLShgCMgQn0LvcAhCP56ENa998vqkFKYSwzdtxo2X2Bgl/olc+Ybcyi5qn1 C8JXv2PaPweaQ== Original-Received: by breton.holly.idiocy.org (Postfix, from userid 501) id D72C1202A64866; Fri, 12 Feb 2021 22:47:39 +0000 (GMT) Mail-Followup-To: Alan Third , Yuan Fu , Lars Ingebrigtsen , Eli Zaretskii , Robert Pluim , emacs-devel , monnier@iro.umontreal.ca Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=116.202.65.218; envelope-from=alan@idiocy.org; helo=outbound.soverin.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:264564 Archived-At: --5r/HHDo1qNp2cTt8 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 --5r/HHDo1qNp2cTt8 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-Scale-SVGs-to-match-frame-s-scale-factor.patch" >From 62cbe4ed2376deeb3a6fc5315b938018cb71ef78 Mon Sep 17 00:00:00 2001 From: Alan Third 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 --5r/HHDo1qNp2cTt8--