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: Wed, 23 Oct 2024 11:01:20 +0300 Message-ID: <86sesndz8v.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28956"; 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 Wed Oct 23 10:02:38 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 1t3WKE-0007FE-0Z for ged-emacs-devel@m.gmane-mx.org; Wed, 23 Oct 2024 10:02:38 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3WJ8-0005vA-Fs; Wed, 23 Oct 2024 04:01:30 -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 1t3WJ2-0005uz-Tg for emacs-devel@gnu.org; Wed, 23 Oct 2024 04:01:25 -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 1t3WJ2-0002K1-Kp; Wed, 23 Oct 2024 04:01:24 -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=zy+DssMb8XW1orUJIcNDggLtCMe7+QJt7zGPdRjroEg=; b=ko0R1tXHwDwl gabIuyCiyWPxi12ONcc914zFZyrfAHGzGKzyGOmhasPsRXMarNhf5QEZt0vZklXCg1DJoLRrCDY8w KNzg3zLo+ac31G/ob5qH/sFIJxs9tuBzc2wPoou4eFnC+95yD2JFDwd2YTC4HMuIOgQimymtu664t XrZCz3Zxukcfv0kdD/060eRJbb5EuhOrRotu3ru+p3pAQLPqrpziOCU91QE9DQY4g9q24IjnYfgFk eKRafp2QdNm6Dntktgw9pGlVjoE2uQa6FxJFlUbtokniFQqRkpO+luZ2eAAGKQBIQyzheQB3kCz1l 0kFSfRTonaN90ZZigEZvPA==; In-Reply-To: (message from Jimmy Yuen Ho Wong on Wed, 23 Oct 2024 06:01:18 +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:324773 Archived-At: > From: Jimmy Yuen Ho Wong > Date: Wed, 23 Oct 2024 06:01:18 +0100 > > Recently I've been trying to mold Corfu to render every string in every face in pixel precision but I've run into > some slight performance issues when truncating strings. The motivation for this is `truncate-string-to-width` > not only does not speak pixels, but also doesn't understand composed characters such as emojis. It just > chops off the emoji byte sequence in the middle as described in the PR, so I had to write a custom > `truncate-string-to-pixel-width` function using `string-glyph-split` and `string-pixel-width` to measure character > widths, which seems... silly. > > I've looked into how to get the width of a glyph from a font object by employing some dark magic of > undocumented internal functions and wrongly documented functions such as `char-displayable-p`, `font-info`, > `lglyth-width`, `lgstring-glyph`, `open-font` etc in combination of `composition-get-gstring`, which is correct and > fast enough.... for glyph strings with no faces. Then I looked into reverse engineering the face font selection > process, which quickly pushed the run time up 2 orders of magnitude due to the anonymous face + face > attribute inheritance + face lists madness. Then when I realized I still didn't deal with display text properties, I > threw my hands up. > > It'd be really nice if we could have a really performant C function that can truncate strings correctly and > preferably in pixel precision. Alternatively, a `glyph-pixel-width` that implements a fast path in C that's > somewhat akin to the process I described above, but also handles display text properties correctly would be > nice. > > I don't really know enough about Emacs C internals WRT to font rendering to implement this so I'm just > throwing this idea out there. I know next to nothing about Corfu, so I have hard time understanding what you are trying to do and why. Basically, the Emacs display engine already truncates stuff it displays, so it is completely unclear to me why you'd need to do that in Lisp. In particular, "truncate strings in pixel precision" makes no sense to me, since strings on display are made of characters (more accurately, "grapheme clusters"), so any reasonable truncation will always work at the grapheme-cluster granularity, not at pixel granularity. So please describe your problem in much more detail, assuming that I know nothing about Corfu and its display-related tricks. Thanks.