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.devel Subject: Re: Need `truncate-string-to-pixel-width` and `glyph-pixel-width` functions in C Date: Thu, 24 Oct 2024 12:55:03 +0300 Message-ID: <86seslddvs.fsf@gnu.org> References: <86sesndz8v.fsf@gnu.org> <86ed46en1q.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23639"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Jimmy Yuen Ho Wong Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Oct 24 11:56:00 2024 Return-path: Envelope-to: ged-emacs-devel@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 1t3uZU-0005vt-EB for ged-emacs-devel@m.gmane-mx.org; Thu, 24 Oct 2024 11:56:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3uYg-0000W2-PT; Thu, 24 Oct 2024 05:55:11 -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 1t3uYe-0000Vn-N2 for emacs-devel@gnu.org; Thu, 24 Oct 2024 05:55:08 -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 1t3uYe-0003hO-7e; Thu, 24 Oct 2024 05:55:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=lb16AavULQlo/hbryGwEo7DYvqlqt1gPTDt/ezb7Lc4=; b=qAH3XYI7awN6 QggbP+DJh+MgD40acsycXfuwQQe2wH1SvdhYCgNR35nmTcqcd5fnyQUN4gb0p6/oxCwfcaV9QLoKS 0cSDG5AYiElwWwrkV0hxPX6Hd/zVnfuMvdEOcCyt3CVDpjevSXN2WnhxUKnAuuHqU1dqOTwyqf3Xs boe5iJBZE4awzK9zFZ55nO8QusxnWLoW0wXhFPNozjWh1Q92+IfWYDbdAP7IEd7BvAj5tGmryg16p KqlZpUiYZ4UFeyMvIXPNlVnXgg4/F5N8o7donc8YkvsVcUHYvMQazYPjkSjKt8rp55HStDRZzo6CM JGo5vIyMGLoW/NEBZGYlYw==; In-Reply-To: (message from Jimmy Yuen Ho Wong on Wed, 23 Oct 2024 21:03:55 +0100) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:324817 Archived-At: > From: Jimmy Yuen Ho Wong > Date: Wed, 23 Oct 2024 21:03:55 +0100 > Cc: emacs-devel@gnu.org > > Each completion in a list of completion candidates is a triple of (candidate string, prefix, annotation), all in > varying widths. Aligning these 3 columns requires calculating paddings. Since there are paddings, the > padding between the candidate string and the annotation can also be arbitrarily large. Naive truncation that > truncates the entire concatenated line against the width of the pop up box, which is actually the window width > in a child frame, may leave some arbitrarily long empty space in the end. In addition, naive truncation does > not replace extra characters in the relevant columns with ellipses to indicate truncation happened. Please look > at the link in my first email for further details. > > So the issue is not "pixel-wise truncation", the issue is to take > variable-width display elements into account when truncating, is that > right? > > To be precise, by " truncate strings correctly and preferably in pixel precision", I mean truncation that respects > pixel width constraints (and grapheme clusters, as clarified by you). Truncating variable-width display > elements on GUIs necessitate the ability to calculate the widths of the individual elements, I call this the > hypothetical `glyph-pixel-width`. Whether it is exposed to Elisp is less important to a hypothetical > `truncate-string-to-pixel-width` for my use case. OK, thanks. So let me see if I understand the issue. You want to create in a child frame a display such as below: ... And you want to do it in a way that all the "Type"s are aligned to the right (i.e. end at the right edge of the frame), no matter what is shown in the s and in the s, which could be of different width and could include characters of different fonts and even images/emoji. Is that correct, or is there something else? If the above is what you want, and since the popup is actually a frame, my first suggestion would be to use :align-to display property on the whitespace between the s and the s. Did you try that? The advantage of :align-to is that you don't have to measure the pixel-width of the candidate names, only the pixel-width of each string (to correctly set up the offset from the right edge of the text area) -- the rest will be done by the display engine. And the display engine always does everything in pixel resolution.