From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#64420: string-width of =?UTF-8?Q?=E2=80=A6?= is 2 in CJK environments Date: Fri, 07 Jul 2023 09:29:18 +0300 Message-ID: <838rbsgrpd.fsf@gnu.org> References: <961e5083-ccf3-9d39-175d-5c5957130d50@gutov.dev> <83cz1ao3x0.fsf@gnu.org> <83a5weo2dz.fsf@gnu.org> <39c8c0b0-070d-88c0-f074-a878a74ef780@gutov.dev> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15945"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 64420@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 07 08:30:22 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1qHeyz-0003uz-5I for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 07 Jul 2023 08:30:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qHeyi-000375-FW; Fri, 07 Jul 2023 02:30:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qHeyg-00036k-FM for bug-gnu-emacs@gnu.org; Fri, 07 Jul 2023 02:30:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qHeyg-0005ly-5c for bug-gnu-emacs@gnu.org; Fri, 07 Jul 2023 02:30:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qHeyf-0007kg-SR for bug-gnu-emacs@gnu.org; Fri, 07 Jul 2023 02:30:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 07 Jul 2023 06:30:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64420 X-GNU-PR-Package: emacs Original-Received: via spool by 64420-submit@debbugs.gnu.org id=B64420.168871136829733 (code B ref 64420); Fri, 07 Jul 2023 06:30:01 +0000 Original-Received: (at 64420) by debbugs.gnu.org; 7 Jul 2023 06:29:28 +0000 Original-Received: from localhost ([127.0.0.1]:42437 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qHey7-0007jU-TN for submit@debbugs.gnu.org; Fri, 07 Jul 2023 02:29:28 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:48814) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qHey3-0007jF-Hi for 64420@debbugs.gnu.org; Fri, 07 Jul 2023 02:29:27 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qHexx-0004Ht-L7; Fri, 07 Jul 2023 02:29:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=qnDC1WZ7oz6JK1oMMN9yenaERGjMO48j6oi9LblMXUE=; b=jgMBXGkZeKEPt1M2PRcG g4WhIJouMvjS3yZT7x8641N+BNyseF+2XMIS3OYLY/CTAz0RqJcFi8j1g25S2ycSi/5MkvP4sKiLQ xAz1DDCwRI0oyGUwUG6fzq2R+DA4ETuIh6FYRuEv+84ctxqdP1Zz6mENP8UMUq83Lf+fYR7JFWcMy 96uyGDkYGz2nsaf7/DGVYMVcXteakp6+NVqr86t6z0aAWI0ZD0y1N5Ll9fa2oDu5woMvqYzGUswgz 3Bv8G147xPneNzXW/jSMt42+7uDw76/GI5acTB/8AE9I4GwVTPacxoYQxdbqfbyc7ycPuNLvmYw4n 8irjGK83kQKbXQ==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qHexw-0005oH-BP; Fri, 07 Jul 2023 02:29:17 -0400 In-Reply-To: <39c8c0b0-070d-88c0-f074-a878a74ef780@gutov.dev> (message from Dmitry Gutov on Fri, 7 Jul 2023 05:13:50 +0300) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:264705 Archived-At: > Date: Fri, 7 Jul 2023 05:13:50 +0300 > Cc: 64420@debbugs.gnu.org > From: Dmitry Gutov > > On 02/07/2023 16:43, Eli Zaretskii wrote: > >> Is there some inherent reason why string-width differs from the result > >> of the above expression > > Because string-width doesn't consult the actual metrics of the font. > > It uses a char-table that we set "by hand". > > Would it be appropriate to fix the entry for … in that table either way? "Fix" in what way? In most language-environments we get (char-width ?…) => 1 What's wrong with that? > Or does that not match the principle with which those entries are done? Sorry, I don't understand the question: what principle are you talking about? > >> and especially only does that on CJK? > > In CJK locales, most characters are double-width because those locales > > use fonts where the glyphs are wider. Or at least this is the theory. > > string-pixel-width is free from these assumptions because it actually > > measures the font glyphs. > > I'm guessing it's somewhat slower because of that too It isn't. The entries in char-width-table are set up when you switch to the language-environment which requires that; see, for example, lisp/language/chinese.el where we call set-language-info-alist for any Chinese-* language-environment. > >> (defun company--string-width (str) > >> (if (display-graphic-p) > >> (ceiling (/ (string-pixel-width str) > >> (float (default-font-width)))) > >> (string-width str))) > > Yes, definitely. (Actually, display-multi-font-p is better than > > display-graphic-p, but in practice they will return the same value.) > > Could you suggest a similar alternative to move-to-column? Try this: (vertical-motion (cons (/ (float PIXELS) (default-font-width)) 0)) where PIXELS is the X coordinate in pixel units. That is, make the LINES argument of vertical-motion be a cons cell with its cdr zero and its car the required horizontal position, a float, in units of the frame's canonical character width. vertical-motion works internally in pixels when considering horizontal coordinates. Caveat: vertical-motion uses _visual_ columns, relative to the displayed portion of the line, so it differs from move-to-column when the line is a continuation line, or is truncated on display, or the window is hscrolled.