GNU bug report logs - #77042
31.0.50; `string-pixel-width' could return wrong results with alternative properties

Previous Next

Package: emacs;

Reported by: David Ponce <da_vid <at> orange.fr>

Date: Sat, 15 Mar 2025 22:45:02 UTC

Severity: normal

Found in version 31.0.50

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: David Ponce <da_vid <at> orange.fr>
To: 77042 <at> debbugs.gnu.org
Subject: bug#77042: 31.0.50; `string-pixel-width' could return wrong results with alternative properties
Date: Sat, 15 Mar 2025 23:43:33 +0100
[Message part 1 (text/plain, inline)]
Hello,

During some experiments to use `char-property-alias-alist', I
discovered that `string-pixel-width' could return wrong results with
alternative properties that affect how the string is displayed.

Here is a simple recipe to eval in the *scratch* buffer, that
illustrates the issue:

(let ((text0 (propertize "This text" 'face 'variable-pitch))
      (text1 (propertize "This text" 'my-face 'variable-pitch)))
  (with-temp-buffer
    (setq-local char-property-alias-alist '((face my-face)))
    (list
     (string-pixel-width text0 (current-buffer))
     (string-pixel-width text1 (current-buffer)))))

Normally the result should be a list of two identical numbers.  But,
on my config, the result is (89 117) instead of expected (89 89).

This problem is similar to the already solved case with
`face-remapping-alist', as is the solution.
                        
I propose the attached patch to fix this and slightly simplify the
code.  On my configuration, the new code has no significant impact on
performance.

The patch also includes several tests of `string-pixel-width' that
could be added to test/lisp/misc-test.el.  As expected with the
current implementation, all tests pass but the test
`misc-test-string-pixel-width-char-property-alias-alist'.  With the
patch applied, all tests pass:

-----------------------
emacs -batch -l ./test/lisp/misc-tests.el -f ert-run-tests-batch-and-exit

   [...]
   passed   6/11  misc-test-string-pixel-width-char-property-alias-alist (0.001030 sec)
   passed   7/11  misc-test-string-pixel-width-display-line-numbers (0.000057 sec)
   passed   8/11  misc-test-string-pixel-width-face-remapping-alist (0.000065 sec)
   passed   9/11  misc-test-string-pixel-width-line-and-wrap-prefix (0.000240 sec)
   [...]
-----------------------

Here is a possible Change log:

2025-03-15  David Ponce  <da_vid <at> orange.fr>

	Fix possible wrong result of `string-pixel-width' with alternate
	properties.  Create regression tests.

	* lisp/emacs-lisp/subr-x.el (string-pixel-width): Like for
	`face-remapping-alist', use in work buffer the value of
	`char-property-alias-alist' local to the passed buffer, to
	correctly compute pixel width.

	* test/lisp/misc-tests.el: Add tests for `string-pixel-width'.

Thanks
[string-pixel-width-fix-alt-props-plus-tests-V0.patch (text/x-patch, attachment)]

This bug report was last modified 141 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.