help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The right way to use svg?


From: Nick Dokos
Subject: Re: The right way to use svg?
Date: Thu, 09 Sep 2021 18:31:45 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]