Fix #105891: Faster Font Fallback Finding #106011

Merged
Harley Acheson merged 2 commits from Harley/blender:FallbackStrategy into main 2023-03-24 19:55:38 +01:00
Member

An improved strategy for finding glyphs within our font fallback stack.


When a text character is requested to be be drawn in the UI, but is not found (or cached) in our regular font, we look in a list of fallback fonts. This allows us to support far more glyphs than any one font can contain.

However this bug report indicates that our current strategy for looking through those fonts can be improved. In the example in the report there are strings containing "🙂" and "🍔". Our current strategy "works" in that not all the fonts need to be searched to find them, but more are searched than necessary. First we look in our monospaced font, because that font indicates that it contains some characters in that range. Then it looks in our big "CJK" font because that also gives that indication. Then it finally finds them in our "NotoEmoji" font.

Current code goes through our list of fonts in one go, mostly just checking the font's coverage bits. But not all of our fonts should be treated the same and it helps a lot to break this into multiple searches. First it looks through the fonts that are currently loaded. Our font caching system only loads fonts that are needed and can unload fonts once they are no longer so. It is MUCH faster to look in the loaded fonts first because the glyph lookups are also cached. Basically if we have already printed an emoji we might as well look in that font for any others, rather than looking through all fonts that hint that they might have one - especially since these other files might need to be loaded to check.

If the above fails only then do we look in the fonts that are NOT loaded (currently without a face) but that also match the coverage bit of the needed character. Basically if we are looking for a character that is in the Unicode block for Phoenician we only look in unloaded fonts that say that they include Phoenician.

In the unlikely event that both searches above find nothing then we look through fonts that weren't included in the above two searches. Then if not found we reply with a last-resort character.

This also makes a change that when looking for a character while outputting proportion text, do not look in the monospaced font. We want to do the reverse (include proportional fonts when outputting monospaced), but there are no times when we want to use a fixed-pitch glyph for a proportional use. Our mono font does not contain many characters, but no need to even look in most cases.

Because of the changed strategy this PR also changes the coverage bits for our "CJK" font so as to not include bit 57. This bit is called the "Surrogates" bit and is very "blunt" in that it only indicates that the font contains any codepoints above U+10000. I don't want to look in this big font just because the codepoint is somewhere in the Supplementary Multilingual Plane. Checking on this bit was useful with the old strategy, but not now. Now when we loaded the CJK font for any Chinese, Japanese, or Korean characters we are going to look in that font first anyway for anything with a regular coverage bit. Anything without a specific bit will still be loaded and cached.

With these changes the example in this bug report is substantially faster. The first emoji character is found immediately in the unloaded emoji font (and loaded) without checking any others. And the second emoji is then immediately loaded from that same emoji font rather than searching anywhere else.

An improved strategy for finding glyphs within our font fallback stack. --- When a text character is requested to be be drawn in the UI, but is not found (or cached) in our regular font, we look in a list of fallback fonts. This allows us to support far more glyphs than any one font can contain. However this bug report indicates that our current strategy for looking through those fonts can be improved. In the example in the report there are strings containing "🙂" and "🍔". Our current strategy "works" in that not all the fonts need to be searched to find them, but more are searched than necessary. First we look in our monospaced font, because that font indicates that it contains some characters in that range. Then it looks in our big "CJK" font because that also gives that indication. Then it finally finds them in our "NotoEmoji" font. Current code goes through our list of fonts in one go, mostly just checking the font's coverage bits. But not all of our fonts should be treated the same and it helps a lot to break this into multiple searches. First it looks through the fonts that are **currently loaded**. Our font caching system only loads fonts that are needed and can unload fonts once they are no longer so. It is MUCH faster to look in the loaded fonts first because the glyph lookups are also cached. Basically if we have already printed an emoji we might as well look in that font for any others, rather than looking through all fonts that hint that they might have one - especially since these other files might need to be loaded to check. If the above fails only then do we look in the fonts that are NOT loaded (currently without a face) but that also match the coverage bit of the needed character. Basically if we are looking for a character that is in the Unicode block for Phoenician we only look in unloaded fonts that say that they include Phoenician. In the unlikely event that both searches above find nothing then we look through fonts that weren't included in the above two searches. Then if not found we reply with a last-resort character. This also makes a change that when looking for a character while outputting proportion text, do not look in the monospaced font. We want to do the reverse (include proportional fonts when outputting monospaced), but there are no times when we want to use a fixed-pitch glyph for a proportional use. Our mono font does not contain many characters, but no need to even look in most cases. Because of the changed strategy this PR also changes the coverage bits for our "CJK" font so as to not include bit 57. This bit is called the "Surrogates" bit and is very "blunt" in that it only indicates that the font contains any codepoints above U+10000. I don't want to look in this big font just because the codepoint is somewhere in the Supplementary Multilingual Plane. Checking on this bit was useful with the old strategy, but not now. Now when we loaded the CJK font for any Chinese, Japanese, or Korean characters we are going to look in that font first anyway for anything with a regular coverage bit. Anything without a specific bit will still be loaded and cached. With these changes the example in this bug report is substantially faster. The first emoji character is found immediately in the unloaded emoji font (and loaded) without checking any others. And the second emoji is then immediately loaded from that same emoji font rather than searching anywhere else.
Harley Acheson added 1 commit 2023-03-22 20:04:40 +01:00
9a04d57f3c Fix #105891: Faster Font Fallback Finding
An improved strategy for finding glyphs within our font fallback stack.
Harley Acheson requested review from Brecht Van Lommel 2023-03-22 20:05:11 +01:00
Brecht Van Lommel approved these changes 2023-03-24 11:51:50 +01:00
Harley Acheson added 1 commit 2023-03-24 19:27:16 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
cfa8d3d4d9
Merge branch 'main' into FallbackStrategy
Author
Member

@blender-bot build

@blender-bot build
Harley Acheson merged commit 1d84216c09 into main 2023-03-24 19:55:38 +01:00
Harley Acheson deleted branch FallbackStrategy 2023-03-24 19:55:38 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#106011
No description provided.