all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* The right way to use svg?
@ 2021-09-09 12:33 Zhiwei Chen
  2021-09-09 14:53 ` Lars Ingebrigtsen
  2021-09-09 22:31 ` Nick Dokos
  0 siblings, 2 replies; 4+ messages in thread
From: Zhiwei Chen @ 2021-09-09 12:33 UTC (permalink / raw)
  To: help-gnu-emacs

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


#+begin_src elisp
(require 'svg)

(let* ((text "Hello, world")
       (w (* (length text) (window-font-width)))
       (h (window-font-height))
       (svg (svg-create w h)))
  (svg-rectangle svg 0 0 w h :stroke "black" :fill "none")
  (svg-text svg text
            :x           0
            :y           (- h 4) ;; Why 4 here?
            :fill        "black")
  (insert-image (svg-image svg :scale 1 :ascent 'center)))
#+end_src


[-- Attachment #2: Screenshot --]
[-- Type: image/png, Size: 39779 bytes --]

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


My question is why it uses 4 rather than other number?

#+begin_src elisp
(list (window-font-width)
      (window-font-height))
;; => (10 21)
#+end_src

on my machine with 1920x1080 screen resolution.

-- 
Zhiwei Chen

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The right way to use svg?
  2021-09-09 12:33 The right way to use svg? Zhiwei Chen
@ 2021-09-09 14:53 ` Lars Ingebrigtsen
  2021-09-09 14:54   ` Lars Ingebrigtsen
  2021-09-09 22:31 ` Nick Dokos
  1 sibling, 1 reply; 4+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-09 14:53 UTC (permalink / raw)
  To: Zhiwei Chen; +Cc: help-gnu-emacs

Zhiwei Chen <condy0919@gmail.com> writes:

>   (svg-text svg text
>             :x           0
>             :y           (- h 4) ;; Why 4 here?
>             :fill        "black")

I think I vaguely remember texts using a default anchor of "middle"?  So
perhaps you want

  :text-anchor "top"

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The right way to use svg?
  2021-09-09 14:53 ` Lars Ingebrigtsen
@ 2021-09-09 14:54   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 4+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-09 14:54 UTC (permalink / raw)
  To: Zhiwei Chen; +Cc: help-gnu-emacs

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Zhiwei Chen <condy0919@gmail.com> writes:
>
>>   (svg-text svg text
>>             :x           0
>>             :y           (- h 4) ;; Why 4 here?
>>             :fill        "black")
>
> I think I vaguely remember texts using a default anchor of "middle"?  So
> perhaps you want
>
>   :text-anchor "top"

No, never mind -- that can't be correct.  Hm.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The right way to use svg?
  2021-09-09 12:33 The right way to use svg? Zhiwei Chen
  2021-09-09 14:53 ` Lars Ingebrigtsen
@ 2021-09-09 22:31 ` Nick Dokos
  1 sibling, 0 replies; 4+ messages in thread
From: Nick Dokos @ 2021-09-09 22:31 UTC (permalink / raw)
  To: help-gnu-emacs

Zhiwei Chen <condy0919@gmail.com> writes:

> #+begin_src elisp
> (require 'svg)
>
> (let* ((text "Hello, world")
>        (w (* (length text) (window-font-width)))
>        (h (window-font-height))
>        (svg (svg-create w h)))
>   (svg-rectangle svg 0 0 w h :stroke "black" :fill "none")
>   (svg-text svg text
>             :x           0
>             :y           (- h 4) ;; Why 4 here?
>             :fill        "black")
>   (insert-image (svg-image svg :scale 1 :ascent 'center)))
> #+end_src
>
>
>
>
> My question is why it uses 4 rather than other number?

Not quite sure what you are asking, but I don't think there is any
magic associated with 4: it was probably trial and error and was
chosen because it raises the text enough so that the comma doesn't
stick out below the bottom of the box and it doesn't get cropped.

5 or 6 should also work, but if you subtract too much, then the `H'
and the `l' will start sticking outside the box at the top and will
get cropped.

Basically, if you want to fit text inside a box, you need to know the
depth of the largest descender in your font (maybe the comma, maybe
the descender in `g' or `y' or 'j' or 'p' or 'q') and raise the text
in the box by that much (and of course make the box big enough so that
the text fits inside).

>
> #+begin_src elisp
> (list (window-font-width)
>       (window-font-height))
> ;; => (10 21)
> #+end_src
>
> on my machine with 1920x1080 screen resolution.

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-09-09 22:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-09 12:33 The right way to use svg? Zhiwei Chen
2021-09-09 14:53 ` Lars Ingebrigtsen
2021-09-09 14:54   ` Lars Ingebrigtsen
2021-09-09 22:31 ` Nick Dokos

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.