Skip to content

Commit f7b0049

Browse files
committed
skip named glyphs if code point already present in charmap
1 parent 8ef96ff commit f7b0049

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/FontEngine.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,36 @@ void FontEngine::buildGidToCharCodeMap (RangeMap &charmap) const {
169169
// In case the Unicode map of the font doesn't cover all characters, some
170170
// of them could still be identified by their names if present in the font.
171171
if (FT_HAS_GLYPH_NAMES(_currentFace)) {
172+
NumericRanges<uint32_t> usedCodepoints;
173+
for (size_t i=0; i < charmap.numRanges(); i++)
174+
usedCodepoints.addRange(charmap.getRange(i).minval(), charmap.getRange(i).maxval());
172175
if (charmap.empty())
173-
addCharsByGlyphNames(1, getNumGlyphs(), charmap);
176+
addCharsByGlyphNames(1, getNumGlyphs(), charmap, usedCodepoints);
174177
else {
175-
addCharsByGlyphNames(1, charmap.minKey()-1, charmap);
178+
addCharsByGlyphNames(1, charmap.minKey()-1, charmap, usedCodepoints);
176179
for (size_t i=1; i < charmap.numRanges(); i++)
177-
addCharsByGlyphNames(charmap.getRange(i-1).max()+1, charmap.getRange(i).min()-1, charmap);
178-
addCharsByGlyphNames(charmap.maxKey()+1, getNumGlyphs(), charmap);
180+
addCharsByGlyphNames(charmap.getRange(i-1).max()+1, charmap.getRange(i).min()-1, charmap, usedCodepoints);
181+
addCharsByGlyphNames(charmap.maxKey()+1, getNumGlyphs(), charmap, usedCodepoints);
179182
}
180183
}
181184
}
182185

183186

184187
/** Looks for known glyph names in a given GID range and adds the corresponding
185-
* GID->code point mapping to a character map.
188+
* GID->code point mapping to a character map if the code point is not already present.
186189
* @param[in] minGID minimum GID of range to iterate
187190
* @param[in] maxGID maximum GID of range to iterate
188-
* @param[in,out] charmap character map taking the new mappings */
189-
void FontEngine::addCharsByGlyphNames (uint32_t minGID, uint32_t maxGID, RangeMap &charmap) const {
191+
* @param[in,out] charmap character map taking the new mappings
192+
* @param[in,out] ucp collection of code points already present in the character map */
193+
void FontEngine::addCharsByGlyphNames (uint32_t minGID, uint32_t maxGID, RangeMap &charmap, CodepointRanges &ucp) const {
190194
for (uint32_t gid=minGID; gid <= maxGID; gid++) {
191195
char glyphName[64];
192196
if (FT_Get_Glyph_Name(_currentFace, gid, glyphName, 64) == 0) {
193-
if (int32_t cp = Unicode::aglNameToCodepoint(glyphName))
197+
const int32_t cp = Unicode::aglNameToCodepoint(glyphName);
198+
if (cp && !ucp.valueExists(cp)) {
194199
charmap.addRange(gid, gid, cp);
200+
ucp.addRange(cp);
201+
}
195202
}
196203
}
197204
}

src/FontEngine.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
#include "Character.hpp"
3232
#include "CharMapID.hpp"
3333
#include "Glyph.hpp"
34+
#include "NumericRanges.hpp"
3435
#include "RangeMap.hpp"
3536

3637
class Font;
3738

3839
/** This class provides methods to handle font files and font data.
3940
* It's a wrapper for the Freetype font library. */
4041
class FontEngine {
42+
using CodepointRanges = NumericRanges<uint32_t>;
4143
public:
4244
~FontEngine ();
4345
static FontEngine& instance ();
@@ -75,7 +77,7 @@ class FontEngine {
7577
FontEngine ();
7678
bool setFont (const std::string &fname, int fontindex, const CharMapID &charmapID);
7779
int charIndex (const Character &c) const;
78-
void addCharsByGlyphNames (uint32_t minGID, uint32_t maxGID, RangeMap &charmap) const;
80+
void addCharsByGlyphNames (uint32_t minGID, uint32_t maxGID, RangeMap &charmap, CodepointRanges &ucp) const;
7981

8082
private:
8183
FT_Face _currentFace = nullptr;

src/ToUnicodeMap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ bool ToUnicodeMap::addMissingMappings (uint32_t maxIndex) {
3636
// collect Unicode points already in assigned
3737
NumericRanges<uint32_t> codepoints;
3838
for (size_t i=0; i < numRanges(); i++)
39-
codepoints.addRange(rangeAt(i).minval(), rangeAt(i).maxval());
39+
codepoints.addRange(getRange(i).minval(), getRange(i).maxval());
4040
// fill unmapped ranges
4141
bool success;
4242
if (empty()) // no Unicode mapping present at all?
4343
success = fillRange(1, maxIndex, 1, codepoints, true);
4444
else { // (partial) Unicode mapping present?
45-
success = fillRange(1, rangeAt(0).min()-1, rangeAt(0).minval()-1, codepoints, false);
45+
success = fillRange(1, getRange(0).min()-1, getRange(0).minval()-1, codepoints, false);
4646
for (size_t i=0; i < numRanges()-1 && success; i++)
47-
success = fillRange(rangeAt(i).max()+1, rangeAt(i+1).min()-1, rangeAt(i).maxval()+1, codepoints, true);
47+
success = fillRange(getRange(i).max()+1, getRange(i+1).min()-1, getRange(i).maxval()+1, codepoints, true);
4848
if (success)
49-
success = fillRange(rangeAt(numRanges()-1).max()+1, maxIndex, rangeAt(numRanges()-1).maxval()+1, codepoints, true);
49+
success = fillRange(getRange(numRanges()-1).max()+1, maxIndex, getRange(numRanges()-1).maxval()+1, codepoints, true);
5050
}
5151
return success;
5252
}

src/ToUnicodeMap.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
#include "RangeMap.hpp"
2626

2727

28-
/** Represents a mapping from character indexes to unicode points. */
29-
class ToUnicodeMap : public RangeMap
30-
{
28+
/** Represents a mapping from character indexes to Unicode points. */
29+
class ToUnicodeMap : public RangeMap {
3130
public:
3231
bool addMissingMappings (uint32_t maxIndex);
3332

0 commit comments

Comments
 (0)