Skip to content

BUG: Load the embedded fonts once and check each font load - #74

Closed
imikejackson wants to merge 1 commit into
BlueQuartzSoftware:developfrom
imikejackson:topic/font_loading_fixes
Closed

imikejackson wants to merge 1 commit into
BlueQuartzSoftware:developfrom
imikejackson:topic/font_loading_fixes

Conversation

@imikejackson

Copy link
Copy Markdown
Collaborator

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_font is 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.cpp

GetFiraSansRegular, GetLatoRegular and GetLatoBold filled a static std::vector when 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 a static const vector once (thread-safe function-local static initialization) and returns const 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::SetFont and fonts::LoadEmbeddedFont.
  • Source/EbsdLib/Utilities/CanvasUtilities.cpp, PoleFigureChrome.cpp, PoleFigureCompositor.cpp, DiscretePoleFigureCompositor.cpp, Source/EbsdLib/LaueOps/LaueOps.cpp — callers.

set_font returns false for font data that is not valid, and it returns true for 0 bytes without loading a font. Before, a failed load made measure_text return 0 and removed the text without an error. fonts::SetFont rejects an empty or oversized buffer and returns the result of set_font. fonts::LoadEmbeddedFont calls it and asserts on failure in Debug builds. The drawing functions have no error path, so Release builds continue as before. Every font load in Source/ uses LoadEmbeddedFont. The const_cast around the font data is removed, because set_font takes unsigned 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.
  • A getter that returns a copy fails to compile against the tests, and a SetFont that ignores the load result fails the invalid-buffer test.
  • The font tests pass under ThreadSanitizer with no reports. The concurrency test cannot detect the old race without ThreadSanitizer.
  • macOS Release: the full EbsdLib test suite passes (431/431).
  • CI passes on Windows, Linux and macOS.

Related Issue

  • This PR has no related issue

* 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>
@imikejackson

Copy link
Copy Markdown
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

@imikejackson
imikejackson deleted the topic/font_loading_fixes branch November 13, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant