Improve text sharpness (#5838)

This improves the sharpness of text slightly, thanks to
<https://hikogui.org/2022/10/24/the-trouble-with-anti-aliasing.html> by
@hikogui (🙏 ).

The difference is very small, but in dark mode (bright text on dark
background) text is sometimes significantly sharper, and also slightly
brighter.

The difference in light mode (dark text on light background) is much
smaller (barely perceptable).

To compare the before/after I suggest you open both in new tabs, then
quickly change between them.

### Low-DPI
#### Before
![Code Editor
old](https://github.com/user-attachments/assets/e10a3cad-932f-48cd-b7d6-5bfe70954c5e)

#### After
![Code
Editor](https://github.com/user-attachments/assets/2e7383fe-8023-4425-91c8-93df3c22c0fe)

#### Before

![old-white](https://github.com/user-attachments/assets/51c41c59-e897-4831-857a-f3ffe17ce7d4)

#### After

![new-white](https://github.com/user-attachments/assets/4ac6f951-8c57-4bcc-a5d5-788cf52ea7d8)

### High-DPI
The difference is starkest on low-DPI screens (like the above
screenshots). On high-DPI screens, the difference is much smaller

#### Before
![widget_gallery
old](https://github.com/user-attachments/assets/f2149a30-aef8-4383-b48c-73d33a03ca02)

#### After

![widget_gallery](https://github.com/user-attachments/assets/c9ceb8be-8a32-490c-9364-2c6562b741f6)
This commit is contained in:
Emil Ernerfeldt 2025-03-21 12:56:47 +01:00 committed by GitHub
parent 3f731ec794
commit 2058dcb881
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
60 changed files with 134 additions and 130 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2c15a74a1b1ed3b52a53966a3df2901ca520b92fbfbd10503e32ddb8431e1467
size 335399
oid sha256:61db3807f755ac832ba069e1adaf8aeb550c88737b4907748667a271ae29863d
size 334792

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0e4a90792a9876da549f3d1da9b057a078400ad15db2cc6e35f4324851137d4e
size 93115
oid sha256:21e0a6cdf175606a513ddf410ae1b873a9817305ecad403116fad3c6ff795fa3
size 92185

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d8f1046ee5d50d73a17009fd1f11f056b5828fedc62908d00730a6aa77125473
size 182900
oid sha256:fb4ac08fb40dd1413feee549ba977906160c82d0aba427d6d79d2e56080aa04e
size 178975

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d17f0693c6288f87d4a0bb009ea03911e8a9baf3efa81445a3ed7849df0313e9
size 102920
oid sha256:f7572ec2dad9038c24beb9949e4c05155cd0f5479153de6647c38911ec5c67a0
size 100779

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2faddafd5f6fc445d15ec39248326d607d14838692201503a178ae1da2c0127d
size 31675
oid sha256:cbe9f58cce2466360b4b93b03afaaee36711b3017ddff1b2b56bfe49ea91a076
size 31306

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c129436a0b1dbfae999adfe0dcc6f5c4e0683c4e9b9a1e52f4b7bbb85ce3a462
size 27162
oid sha256:7224afc6e728f60c28c027bf4be03d1f598dc70977274bcd32b7398d11dd36c7
size 26416

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0466198f14d15f011e16d16efcc28aeaaf80978ea4e46b5d9a1282304c192c4c
size 80907
oid sha256:5cfc3ee54a0e64fb8b72d55e9fc2079aa2517b200665684076d63b87c381cdb9
size 78704

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d6ba28dacacf5b6f67746fb5187b601e222fd6baf190af2248fdc98909fc17fd
size 25921
oid sha256:49b08c1fb7878d8670d96de9f9791e2db5cf7206812da1d9102c4dd1758cb803
size 25833

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8196e08717f16c5ad17d0f84a4e57e63bb5a51c8f2b171071bf983af18ec161d
size 20834
oid sha256:1e3e0330de3f68593329d2f36649127d5ac70109232c68f5c7ce310fa919fda5
size 20348

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:df029c69651ee452cc4b265828280e47ffbcafb2958d71d67a5fe38f5211afe7
size 10788
oid sha256:2882a9842f51a7c3e9642a9a3d260407e1194648f47574608822a293bd3b1d56
size 10465

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0a62d309912501be8a5de7af4f1039a2a5731b1ed76fad17527f5783a5375f42
size 133230
oid sha256:af99bd49ceee6bbd96cc813cd96ac01f5c135ed7d94b8ff4010fa45feac5359a
size 127703

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f299fb3c7c66a0fde7a30916bb4be1bb14c43f5eb139268309aa8b46f86caede
size 24388
oid sha256:a0b999914adab3d44c614bdf3b28abd268a4ff6162c5680b43035b3f71cb69bb
size 23999

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bae5f410ed30ef4dba6f3b529ae20e34a26f6c15c4cafd197899cf876271f5f1
size 17828
oid sha256:57e09bcf48541af11e44ff07122f09640e0329db0c2bc7a6ecb406a3ece572ac
size 17608

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:68ded8dccceb3da2764243f2a554c2b4cf825fca09008d60dd520c7fbb2c5d3e
size 22445
oid sha256:641e5c7d4deccc8eb0374db4707dc356285a5c72186f9021d0d601c22bc5115f
size 21894

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d55baa6e3d4af44a35ec847639c35f968b05ad907352c45b3eb09cce6cd24280
size 64357
oid sha256:880344367ed65f83898ceca4843b1b6259d1690242ced0d29ac8dc48100a8faa
size 62956

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:98f7210fa72bdb00364e3576aefca126a6f31eff52870d116ba74c167354b13b
size 32533
oid sha256:2f467edf4a84c8a98d96f168d843edb201ad2ee067dcd9d8d9ea214a02a41b1f
size 32182

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7868d662bd61d490dce9c049fca6c6e6b978255664fa709e959891bb40a7d434
size 36577
oid sha256:01705a1a49350278f524bbc5dbd47ae9da4b57ee7f6f34fb20186e1aa9b9f1d4
size 35714

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fb3d031b8f658a90cf98e7a7bc5e0d7a3b601d742e2a9469cd115e7466e06524
size 17628
oid sha256:03cb424100e99a141daeacc78036c4334d74cace3fae19bb878565ccda68457d
size 17448

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7ace9a6626446f8e29ec4c3f688e60cbeb86e79ad962044858aabe33a9c3d0e9
size 264538
oid sha256:df1e4a1e355100056713e751a8979d4201d0e4aab5513ba2f7a3e4852e1347dd
size 264340

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:17dc2a2f98d4cc52f6c6337dcc2e40f22d7310a933de91bb60576b893926193c
size 58674
oid sha256:6ed78a559488474487c0a434a941e434b22354e4374d13059076d76da93bc609
size 57051

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b007380b5ce761ff5d23665dcaa2729e1795c5192efdf366007ddbcea0ed64a5
size 35463
oid sha256:16ee44708adbe6e0ac3ce58617a5d63fb3bde357c07611815376518950e056b0
size 34763

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:89efd018caac097a5f9be37dcae15fc60b1475c72fc913ec9940540344e0b09f
size 23622
oid sha256:63f5c3be15164e6f008fb09b4ff37eff2af0ab361de28d1994d595789c379df5
size 23205

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:22c89f7b9b84563d6ee7db0d9a66f6b95c9034261fdffca53ae9737d70d2b376
size 183881
oid sha256:b236fe02f6cd52041359cf4b1a00e9812b95560353ce5df4fa6cb20fdbb45307
size 179653

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:316c172a936f215afdcc45e7f5b32400e6acd759551adb2cc741f7121b9d83eb
size 117790
oid sha256:1579351658875af48ad9aafeb08d928d83f1bda42bf092fdcceecd0aa6730e26
size 115313

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:88913690a2b225ca634e38406a6a852250019a19d9bb33a4242e77c10fe88422
size 26142
oid sha256:eb7c844f6b745f66304ad036790a5121e4827fa91569b28ffa301794aecd0c66
size 25592

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:43b8dae4a936bf56b92368fcef64ff2ce2518aabc534a77fa578730493034f0f
size 70536
oid sha256:13115759157beb57febcff4be6f1710340736108b520e9ad3efb04be3cedcf7b
size 68767

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a307ac48abc79548c16468b3606a5df283ab2a5ac28345bd801bcc3887063414
size 66384
oid sha256:e177888e10f357f1be8ad80f7a0a33c93798c1e7c43cfe382119eeb12f21279f
size 64732

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6311be2b850b5e41ac6dadf639b00584438b56f651a3c8d75ac8f5e06c9ad6fa
size 21224
oid sha256:e177e2631414784161a5556bdd1420ce8432f9859faede1a2e6f791a02814412
size 20918

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:92b70683a685869274749d057de174896e18dae5cb67e70221c3efdb7106cdda
size 63684
oid sha256:c49c489fe1bb00512c9d08e8d8454fce786744f4ebff0bfd27dac68b7e67b815
size 62317

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3584f16229bae50cc04b31df6bf5ccf43288fd05b447b34b29f118eb7435a090
size 13103
oid sha256:c4d6a15094eee5d96a8af5c44ea9d0c962d650ee9b867344c86d1229e526dcb5
size 12822

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e67b1e676ff994cb9557939db3dca5ddd15c69d167afd96c0957a2a3b75c0fd8
size 36007
oid sha256:02abc0cbab97e572218f422f4b167957869d4e2b4b388355444c20148d998015
size 35200

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1ac48ec9f7bde9869f1b3097e9f897b5e8df96cd6159a6ded542582dc69ab32c
size 47913
oid sha256:e954bf915d562abc69269cd10a4df8fbd0e5603929e6446fefa694099e2494a4
size 47542

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:795e16389b31ad719050247eb9e736782380a83fa71b5b35b50e17812c8d9bdd
size 47886
oid sha256:1c7bd1a65b6c33eff2fe17f7af2dd731a03658abc2419f8722c0e9395b26fdef
size 47515

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a62a286e29aa0e0f949088ddefe01137535877408ba88778f61cbfe8d50c2261
size 43750
oid sha256:e89cd220a925150384b9f9987b178036ffacfe29cdb36ed688205524dbb731fd
size 43803

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9c1bc8e22aa1050a4e7d1b2abe407251e22d338c38a7e41c045a384c9139b4de
size 43895
oid sha256:b71da58f5c0178517f9e0cc97753a0a5d1653cc5d094b5a35ffe050499bcd569
size 43679

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1b618443ba6e8483425972bd95fced23d2cd5ff4ad05277a6171eac14c255302
size 572382
oid sha256:5a1f0d0759458017127d93278b89278af20fdc57c7747652ac6554f24cc708f2
size 552939

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:00681d206ae05c2135dcc61e87a31f18248fc972804a01bc3440faf4fdd1a50e
size 796392
oid sha256:7948ced20e3f62b8356fc978ca7b12f8522b7c15716409cc661c0ebd2f12047f
size 770062

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a21e2cc32a032ee44516c495c68aa5f6e168da1bea44396db6e67889d5714e7f
size 948339
oid sha256:e6f394c2beb51d95edaf8c7ddc9ff62d3f95913ea88a3840245b6bacf8b850cc
size 907997

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7907a5851f4b5a986cbd74b494b65cff7a469038af63ac957d55e848bb3391a8
size 1072165
oid sha256:d31f018cdabf92966b5636d9aef7f11ef1a0383884867e819e7ec99c0474e872
size 1025013

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b220801c49d7d1f4364cf6c4f2098123e34ce782bb1439b2896d8adf9215a0be
size 1166343
oid sha256:e18f7ddadd53e16d04f191268747f244c98d0ea0f4cb9c0ea299f5da7affbc58
size 1139723

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0ff8e54c66f64396b42bb962297eab966089318b1a75e65accef7abbeb7d6cee
size 1353321
oid sha256:09a49cb9da7269bec6eef30c46a0bc85df6538d6e31dc0d0ff2758dbee45f3d8
size 1291804

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5524138c3cb98aa71ef67083ad2d01813ab2394f93f9a7897f2e465ef5a1d0bc
size 46270
oid sha256:4ac90da596084a880487035b276177e98d711854143373d59860f01733b1c0cd
size 45592

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bdf06c41b69eef1eadc8b46020e6e2a7b985a54e1cf75646ca47caaaea525b95
size 88092
oid sha256:e412d424aac7b9cbdfdb8e36bd598e6cbc77183da7733c94c5f20e70699b8b4a
size 87263

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c5ca9c97cef8242ee6ff73d571479be12a8d4e9b3508b3eb6cdf93abda62f4e6
size 120314
oid sha256:222a32da21c69ee46e847e29fb05fd5e1d2de6bb7a22358549bc426f8243fdcb
size 119671

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f0481c97c34693b32575d96b1d4bc1238cbb0eb75a934661072f1b52ffee71cf
size 52171
oid sha256:d42e11f50a9522dd5ae73e8f8336bfb01493751705055a63abea3f5258f7c9c1
size 51626

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:883fdf81e51bfe6333ddcad7998458db251f9cf513c9433179061d7d086eebe0
size 55367
oid sha256:b567d4038fd73986c80d2bd12197a6df037fde043545993fa9fe4160d0af446c
size 54829

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4294949669042e009ac6825ea599dc96e33cdde25e21174b01e3ef108ad478d5
size 55944
oid sha256:fbf40a1f56a6e280002719c6556fe477c93fa7fe88d398372ed36efaa1b83a62
size 55282

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:39e5d196ddcaa213b30b0655fe29881a1551c3036c2262f84af8960f66365300
size 37207
oid sha256:33621731155ebb463fb01ea41ab20272885250efcd7d5c7683c10936b296e14d
size 36446

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f9cf9d7f1921bfc0d61a2ae31e69a98d28280e4699823de5e732cdb102aee5ac
size 37253
oid sha256:186bd8a3146ad8f1977955e3f7fa593877ad1bf1e8376d32f446c67f36a2aafe
size 36493

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:78335f9233990c5622d1f6f0f18a3b44e33b0e68061e865641b0b316072489ba
size 158496
oid sha256:af75f773e9e4ad2615893babce5b99e7fd127c76dd0976ac8dc95307f38a59dc
size 152854

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:202091634d4483949cb1c5c4c5ec02faa23f4d19e7e833aba135887b77e3188d
size 4485
oid sha256:bab1f08160bc43410b9d49ebfaae8a471309e670fccd31456a09176513361e6e
size 4417

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f65efbf60e190d83d187ec51f3f7811eb55135ef4feb9586e931e8498bc05d64
size 7430
oid sha256:21f70bc7146e43b6b10fe1e4cb32597d6f3507b42a6aa4c619c4e8c688ea4c85
size 7290

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:31faeb4e5f488b8bcee5e090accd326d7e43b264e81768ae7c1907e3b6d0f739
size 2121
oid sha256:c618906fe1ff781c20cb89747879fa1ac63a115c624a2695adb9eb6ae157cd40
size 2095

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d96ef60e9cd8767c0a95cc1fb0e240014248d7b0d6f67777a5b6ca4f23e91380
size 10732
oid sha256:6ca504cc7ef988f122fcc099914e5b6f7c39a3a86c5869a0a982c4342c48058a
size 10516

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:23dced7849e2bdd436e0552b2d351fef5693dd688c875816aaba3607a3aa1197
size 21756
oid sha256:763447271686242b8a2deaa63fa1a5a0d57009ef93dd1bcb0ae906541cd7a6ea
size 21052

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6b8790dbd01409496ca6dc6d322d69c59a02f552b244264c8f6d7ea847846d5a
size 28979
oid sha256:4e54f7a1ea9ac74b62241c8b662579fd3c8442857b4569ce818342fb56dc30ae
size 28218

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6f0b04c42dfb257ca650a2fe332509f847cc94f4666cc29d830086627b91fc25
size 33737
oid sha256:f8443523a671d3c83456c6ee0503fdb59127a33d866c45635a84eee3596985fd
size 32831

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:31bd906040fcc356c19dc36036fbfd2a28dfcef54c7a073f584f4a9abddbdb4c
size 1699
oid sha256:b1d172484712e3e12038f8ff427db8c0073aba124aa1b6be17edcc7dccb12f74
size 1656

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b5aa7db1bb52481607069ee4a81209ece81b0c70801969b33bc2d1b2f7087de7
size 2911
oid sha256:be6f0caa911d93543edb39ba6d07d7617ec283b37bc62622a68a18960eb840ab
size 2871

View File

@ -306,14 +306,23 @@ impl FontImage {
/// If you are having problems with text looking skinny and pixelated, try using a low gamma, e.g. `0.4`.
#[inline]
pub fn srgba_pixels(&self, gamma: Option<f32>) -> impl ExactSizeIterator<Item = Color32> + '_ {
// TODO(emilk): this default coverage gamma is a magic constant, chosen by eye. I don't even know why we need it.
// Maybe we need to implement the ideas in https://hikogui.org/2022/10/24/the-trouble-with-anti-aliasing.html
let gamma = gamma.unwrap_or(0.55);
// This whole function is less than rigorous.
// Ideally we should do this in a shader instead, and use different computations
// for different text colors.
// See https://hikogui.org/2022/10/24/the-trouble-with-anti-aliasing.html for an in-depth analysis.
self.pixels.iter().map(move |coverage| {
let alpha = coverage.powf(gamma);
// We want to multiply with `vec4(alpha)` in the fragment shader:
let a = fast_round(alpha * 255.0);
Color32::from_rgba_premultiplied(a, a, a, a)
let alpha = if let Some(gamma) = gamma {
coverage.powf(gamma)
} else {
// alpha = coverage * coverage; // recommended by the article for WHITE text (using linear blending)
// The following is recommended by the article for BLACK text (using linear blending).
// Very similar to a gamma of 0.5, but produces sharper text.
// In practice it works well for all text colors (better than a gamma of 0.5, for instance).
// See https://www.desmos.com/calculator/w0ndf5blmn for a visual comparison.
2.0 * coverage - coverage * coverage
};
Color32::from_white_alpha(ecolor::linear_u8_from_linear_f32(alpha))
})
}
@ -362,11 +371,6 @@ impl From<FontImage> for ImageData {
}
}
#[inline]
fn fast_round(r: f32) -> u8 {
(r + 0.5) as _ // rust does a saturating cast since 1.45
}
// ----------------------------------------------------------------------------
/// A change to an image.