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: How to use "smart kerning" font features in Emacs using composition-function-table Date: Sun, 26 Nov 2023 19:52:47 +0200 Message-ID: <83ttp89zbk.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5692"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Steve Molitor Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Nov 26 18:54:07 2023 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 1r7JKZ-0001Gw-KX for ged-emacs-devel@m.gmane-mx.org; Sun, 26 Nov 2023 18:54:07 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r7JJb-00023G-Fs; Sun, 26 Nov 2023 12:53:07 -0500 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 1r7JJZ-000232-OG for emacs-devel@gnu.org; Sun, 26 Nov 2023 12:53:05 -0500 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 1r7JJZ-000557-Fs; Sun, 26 Nov 2023 12:53:05 -0500 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=prNTuUjoDLyXiAfAQSwEhQul77UmHaRVtnZMyg7jyq4=; b=INyrRxbF2X7h QV/G9y8ASNmBW5LSK/teKaOI1ipkHKr23v6NXKMC+UCaxytjF6c2nLkcsrB+tJWGjqml3AwxjWMat N6RtClGhuDQnQB5RxE3sn6vY0pXqvHp6LaJdUjnWs580aVUqjLbGYi7w3PX9ASy/a5qdITmZy8OTz h4HvXnYsT4rx+Y83Q3eXAIjGbYg2SvjPLBq9Ru4GH8CwwUJTY2X8pYjoLM316IcTBBKlKr2bZknOT 9gq7yfvDgQHdfmX8DW65n7gcOHXcKHLrlDc5zL4LMYaa2yGTnrOGSN1ia+htlW72vCYkUPF8nfmNv AGgqhcAwzTB76DcozsytlA==; In-Reply-To: (message from Steve Molitor on Sun, 26 Nov 2023 09:48:34 -0600) 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:313243 Archived-At: > From: Steve Molitor > Date: Sun, 26 Nov 2023 09:48:34 -0600 > > Using composition-function-table, is it possible to have the font > render a glyph for each character, based on the previous and following > character, in a moving window? For example, the rendering of character > 1 will depend on what characters 0 and 2 are, character 2 depends on 1 > and 3, etc. Not really, see below. > I decided to see if I could get it to work just for the "Commit" > string by adding the following elisp: > > (set-char-table-range composition-function-table ?o > '(["Com" 1 font-shape-gstring])) > (set-char-table-range composition-function-table ?m > '(["mmi" 1 font-shape-gstring])) > > My thinking was if the current character is "o", preceded by a "C" and > followed by an "m", let the font replace the "o". Next, if the current > character is an "m" preceded by an "m" and followed by an "i", let the > font render the middle "m". If that worked I was going to do something > more generic. > > It half worked. If I type "Com" it works as expected - the "o" shifts > slightly to the left when I type the "m": > > https://cdn.zappy.app/f1506de028c470750428ebf984dfbe41.gif > > If I type "mmi" on a separate line it also works - the second "m" > shifts to the left when I type the "i": > > https://cdn.zappy.app/6a1a90013d102bfb494fc7d1de54ca32.gif > > However, if I type "Commit", the "Com" works (the "o" shifts"), but > nothing happens when I type "mit". If I use three m's and type > "Commmit" I do see the effect on the final "mit": > > https://cdn.zappy.app/e98141872235a25bf52f6e332b610d7b.gif > > It seems I've created 3 character, non-overlapping gifs, which is not > the effect I want. > > For comparison, here it is in VSCode, typing "Commit". Various > shifting effects happen as expected: > > https://cdn.zappy.app/8f3bc49a174c4673747c402f7b39d54b.gif > > I think when Emacs matches the 3 character regexp in the composition > function table, it assumes that those 3 characters constitute a single > glyph or ligature. That makes sense for ligatures, but it's not what I > want here. I want a "moving window" where the glyph for each character > depends on the previous and following character. > > Is this possible to do in Emacs currently? No, it's not possible. When characters are composed successfully (i.e., they match some rule in composition-function-table, and the font support their composition), Emacs will not examine them again for other rules. It will restart examining from the first character _after_ those which matched. So to use kerning like the one you describe, I think you must have a rule that matches any sequence of ASCII letters, not just the triplets where the kerning is supposed to happen. Which means that every word using ASCII characters will be passed to the text-shaping engine. Then the shaping engine will decide on its own where to use the kerning, and I think you should get what you want, appearance-wise. But -- and it's a very significant "but" -- displaying text via the shaping engine is expensive -- it involves calls from the display engine to Lisp, which then calls back into C. So rules that match any sequence of ASCII letters will slow down redisplay of almost any text in Emacs. This is why Emacs by default doesn't use the text-shaping engine except for scripts that cannot be displayed without non-trivial character compositions. So if you go the above way, don't be surprised if you see slower redisplay.