all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Pixel padding with variable pitch font
@ 2021-06-07 13:19 Anand Tamariya
  2021-06-07 13:35 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Anand Tamariya @ 2021-06-07 13:19 UTC (permalink / raw)
  To: emacs-devel

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

If you need to pad space with variable pitch font, and you are unable to
use ':align-to display' option for any reason, you can use the following
code. This uses unicode spacing characters
<https://jkorpela.fi/chars/spaces.html> to insert required amount of space.

*Screenshot:* https://lifeofpenguin.blogspot.com/2021/06/gnu-emacs.html

(defun company--insert-space (width w)
  "Insert unicode space characters to fit the WIDTH."
  (let ((num 0))
    (dolist (spc '((#x2001 .  1) (#x2000 . 0.5) (#x2004 . 0.33)
(#x2005 . 0.25) (#x2006 . 0.16) (#x200A . 0.125)))
      (setq num (floor (/ width w (cdr spc))))
      (if (memq (car spc) '(#x2001 #x2000 #x2004 #x2005))
          ;; Allow finer spaces towards the end for better accuracy
          (setq num (1- num)))
      (when (> num 0)
        (dotimes (i num)
          (insert (car spc)))
        (setq width (- width (* num w (cdr spc))))
        ))))

[-- Attachment #2: Type: text/html, Size: 1650 bytes --]

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

* Re: Pixel padding with variable pitch font
  2021-06-07 13:19 Pixel padding with variable pitch font Anand Tamariya
@ 2021-06-07 13:35 ` Eli Zaretskii
  2021-06-08  2:59   ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2021-06-07 13:35 UTC (permalink / raw)
  To: Anand Tamariya; +Cc: emacs-devel

> From: Anand Tamariya <atamariya@gmail.com>
> Date: Mon, 7 Jun 2021 18:49:01 +0530
> 
> If you need to pad space with variable pitch font, and you are unable to use ':align-to display' option for any
> reason, you can use the following code. This uses unicode spacing characters to insert required amount of
> space.
> 
> Screenshot: https://lifeofpenguin.blogspot.com/2021/06/gnu-emacs.html
> 
> (defun company--insert-space (width w)
>   "Insert unicode space characters to fit the WIDTH."
>   (let ((num 0))
>     (dolist (spc '((#x2001 .  1) (#x2000 . 0.5) (#x2004 . 0.33) (#x2005 . 0.25) (#x2006 . 0.16) (#x200A . 0.125)))
>       (setq num (floor (/ width w (cdr spc))))
>       (if (memq (car spc) '(#x2001 #x2000 #x2004 #x2005))
>           ;; Allow finer spaces towards the end for better accuracy
>           (setq num (1- num)))
>       (when (> num 0)
>         (dotimes (i num)
>           (insert (car spc)))
>         (setq width (- width (* num w (cdr spc))))
>         ))))

This assumes that, e.g., #x2001 takes the same horizontal space as the
SPC character (or the same as a column)?  That's only so in
fixed-pitch fonts; in variable-pitch fonts the SPC could be much more
narrow, and columns are all but meaningless.  Caveat emptor!



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

* Re: Pixel padding with variable pitch font
  2021-06-07 13:35 ` Eli Zaretskii
@ 2021-06-08  2:59   ` Ihor Radchenko
  2021-06-08  5:04     ` Anand Tamariya
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2021-06-08  2:59 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel, Anand Tamariya

Eli Zaretskii <eliz@gnu.org> writes:

> This assumes that, e.g., #x2001 takes the same horizontal space as the
> SPC character (or the same as a column)?  That's only so in
> fixed-pitch fonts; in variable-pitch fonts the SPC could be much more
> narrow, and columns are all but meaningless.  Caveat emptor!

One can still measure the actual character width using
window-text-pixel-size and take it into account.



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

* Re: Pixel padding with variable pitch font
  2021-06-08  2:59   ` Ihor Radchenko
@ 2021-06-08  5:04     ` Anand Tamariya
  0 siblings, 0 replies; 4+ messages in thread
From: Anand Tamariya @ 2021-06-08  5:04 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Eli Zaretskii, emacs-devel

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

One can still measure the actual character width using
> window-text-pixel-size and take it into account.

 That's correct. width is pixel width returned by (posn-at-point) and
w is (font-get
(font-at (point)) :size).

This assumes that, e.g., #x2001 takes the same horizontal space as the
> SPC character (or the same as a column)?

The code is only assuming what the specs prescribe. It makes no guarantee
for non-standard fonts.

The complete code in action, if anybody is interested, is here
https://gitlab.com/atamariya/emacs/-/blob/dev/lisp/cedet/company.el#L2562 .

[-- Attachment #2: Type: text/html, Size: 1480 bytes --]

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

end of thread, other threads:[~2021-06-08  5:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-07 13:19 Pixel padding with variable pitch font Anand Tamariya
2021-06-07 13:35 ` Eli Zaretskii
2021-06-08  2:59   ` Ihor Radchenko
2021-06-08  5:04     ` Anand Tamariya

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.