Skip to content

Commit f8548e6

Browse files
committed
embed fonts in deterministic order
(fixes #288)
1 parent 9ec4b36 commit f8548e6

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/DVIToSVG.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,25 @@ void DVIToSVG::embedFonts () {
351351
if (!_actions) // no dvi actions => no chars written => no fonts to embed
352352
return;
353353

354+
auto usedFonts = FontManager::instance().getUniqueFonts();
354355
auto &usedCharsMap = FontManager::instance().getUsedChars();
355356
collect_chars(usedCharsMap);
356357

357358
GlyphTracerMessages messages;
358359
unordered_set<const Font*> tracedFonts; // collect unique fonts already traced
359-
for (const auto &fontchar : usedCharsMap) {
360-
const Font *font = fontchar.first;
360+
for (auto font : usedFonts) {
361361
if (auto ph_font = font_cast<const PhysicalFont*>(font)) {
362362
// Check if glyphs should be traced. Only trace the glyphs of unique fonts, i.e.
363363
// avoid retracing the same glyphs again if they are referenced in various sizes.
364364
if (TRACE_MODE != 0 && tracedFonts.find(ph_font->uniqueFont()) == tracedFonts.end()) {
365365
ph_font->traceAllGlyphs(TRACE_MODE == 'a', &messages);
366366
tracedFonts.insert(ph_font->uniqueFont());
367367
}
368-
if (font->path()) // does font file exist?
369-
_svg.append(*ph_font, fontchar.second, &messages);
368+
if (font->path()) { // does font file exist?
369+
auto it = usedCharsMap.find(font);
370+
if (it != usedCharsMap.end())
371+
_svg.append(*ph_font, it->second, &messages);
372+
}
370373
else
371374
Message::wstream(true) << "can't embed font '" << font->name() << "'\n";
372375
}

src/FontManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ Font* FontManager::getFontById (int id) const {
159159
}
160160

161161

162+
vector<const Font*> FontManager::getUniqueFonts () const {
163+
vector<const Font*> fonts;
164+
fonts.reserve(_fonts.size());
165+
for (auto &font : _fonts) {
166+
auto it = _usedChars.find(font.get());
167+
if (it != _usedChars.end())
168+
fonts.push_back(font.get());
169+
}
170+
return fonts;
171+
}
172+
173+
162174
/** Returns the current active virtual font. */
163175
const VirtualFont* FontManager::getVF () const {
164176
return _vfStack.empty() ? nullptr : _vfStack.top();

src/FontManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class FontManager {
6262
Font* getFont (const std::string &name) const;
6363
Font* getFont (const std::string &name, double ptsize);
6464
Font* getFontById (int id) const;
65+
std::vector<const Font*> getUniqueFonts () const;
6566
const VirtualFont* getVF () const;
6667
int fontID (int n) const;
6768
int fontID (const Font *font) const;

0 commit comments

Comments
 (0)