Repository navigation
BUG: Load the embedded fonts once and check each font load - #74
Closed
imikejackson wants to merge 1 commit into
Closed
imikejackson wants to merge 1 commit into
imikejackson wants to merge 1 commit into
Conversation
* GetFiraSansRegular, GetLatoRegular and GetLatoBold filled a static vector on first use without synchronization. Two threads that called a getter for the first time at the same time could race on it. Each font is now decoded once in a thread-safe static, and each getter returns a const reference instead of a copy of the font. * The return value of canvas_ity set_font was ignored. Add fonts::SetFont, which reports failure, and fonts::LoadEmbeddedFont, which asserts on failure in Debug builds. Use LoadEmbeddedFont at every place that sets a font. Remove the const_cast around the font data, because set_font takes a const pointer and copies the data. * Fix the include lists of the changed files. * Add tests: each embedded font loads and measures a nonzero width, an empty or invalid buffer is rejected, and concurrent calls get the same font instance. Signed-off-by: Michael Jackson <mike.jackson@bluequartz.net>
This was referenced Nov 13, 2026
Collaborator
Author
|
Closed in favor of #86, which merges this branch (with its history) together with the other open EbsdLib fixes for the next release, so simplnx can build against one EbsdLib branch. The branch is kept. 🤖 Generated with Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The embedded-font getters can race when two threads call them for the first time, and every call copies the font. The return value of
canvas_ity::canvas::set_fontis ignored everywhere. This change decodes each font once in a thread-safe static, returns it by const reference, and checks every font load. 8 files changed, +231 / −74 lines.Overlap: #71 changes the measurement canvas in
PoleFigureCompositor::computeLayoutMetrics, next to lines that this change also edits. The two pull requests do not depend on each other. The one that merges second can need a small conflict resolution.1. Thread-safe font getters
Source/EbsdLib/Utilities/Fonts.hpp,Source/EbsdLib/Utilities/Fonts.cppGetFiraSansRegular,GetLatoRegularandGetLatoBoldfilled astatic std::vectorwhen it was empty, without synchronization. Two threads that called a getter for the first time at the same time, for example two pole-figure images made in parallel, could both write to that vector. Each getter now initializes astatic constvector once (thread-safe function-local static initialization) and returnsconst std::vector<unsigned char>&. Callers bind a const reference, so a call no longer copies the font (about 100 KB or more). This changes the return type of three exported functions. simplnx and DREAM3D-NX do not call them.2. Checked font loading
Source/EbsdLib/Utilities/Fonts.cpp—fonts::SetFontandfonts::LoadEmbeddedFont.Source/EbsdLib/Utilities/CanvasUtilities.cpp,PoleFigureChrome.cpp,PoleFigureCompositor.cpp,DiscretePoleFigureCompositor.cpp,Source/EbsdLib/LaueOps/LaueOps.cpp— callers.set_fontreturns false for font data that is not valid, and it returns true for 0 bytes without loading a font. Before, a failed load mademeasure_textreturn 0 and removed the text without an error.fonts::SetFontrejects an empty or oversized buffer and returns the result ofset_font.fonts::LoadEmbeddedFontcalls it and asserts on failure in Debug builds. The drawing functions have no error path, so Release builds continue as before. Every font load inSource/usesLoadEmbeddedFont. Theconst_castaround the font data is removed, becauseset_fonttakesunsigned char const*and copies the data into the canvas.3. Include lists
The changed files include the headers that they use directly, and each file includes its own header first.
Test plan
Source/Test/CanvasUtilitiesTest.cpp: each of the three embedded fonts decodes, loads, and gives a nonzero width for "X"; an invalid buffer and an empty buffer (also on a canvas that already has a font) are rejected; eight threads that call each getter at the same time get the same instance with identical bytes.SetFontthat ignores the load result fails the invalid-buffer test.Related Issue