Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Moved more character classing into util::i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Nov 12, 2016
1 parent 3c5445e commit f1e7a51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
14 changes: 2 additions & 12 deletions src/mbgl/text/glyph_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ void GlyphSet::lineWrap(Shaping &shaping, const float lineHeight, float maxWidth
// Collapse invisible characters.
uint32_t breakGlyph = positionedGlyphs[lastSafeBreak].glyph;
uint32_t lineEnd = lastSafeBreak;
if (breakGlyph == 0x20 /* space */
|| breakGlyph == 0x200b /* zero-width space */) {
if (util::i18n::isVisible(breakGlyph)) {
lineEnd--;
}

Expand All @@ -144,16 +143,7 @@ void GlyphSet::lineWrap(Shaping &shaping, const float lineHeight, float maxWidth

// Ideographic characters, spaces, and word-breaking punctuation that often appear without surrounding spaces.
if (useBalancedIdeographicBreaking
|| shape.glyph == 0x20 /* space */
|| shape.glyph == 0x26 /* ampersand */
|| shape.glyph == 0x2b /* plus sign */
|| shape.glyph == 0x2d /* hyphen-minus */
|| shape.glyph == 0x2f /* solidus */
|| shape.glyph == 0xad /* soft hyphen */
|| shape.glyph == 0xb7 /* middle dot */
|| shape.glyph == 0x200b /* zero-width space */
|| shape.glyph == 0x2010 /* hyphen */
|| shape.glyph == 0x2013 /* en dash */
|| util::i18n::allowsWordBreaking(shape.glyph)
|| util::i18n::allowsIdeographicBreaking(shape.glyph)) {
lastSafeBreak = i;
}
Expand Down
20 changes: 20 additions & 0 deletions src/mbgl/util/i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ namespace mbgl {
namespace util {
namespace i18n {

bool isVisible(uint32_t chr) {
return (chr == 0x0a /* newline */
|| chr == 0x20 /* space */
|| chr == 0x200b /* zero-width space */);
}

bool allowsWordBreaking(uint32_t chr) {
return (chr == 0x0a /* newline */
|| chr == 0x20 /* space */
|| chr == 0x26 /* ampersand */
|| chr == 0x2b /* plus sign */
|| chr == 0x2d /* hyphen-minus */
|| chr == 0x2f /* solidus */
|| chr == 0xad /* soft hyphen */
|| chr == 0xb7 /* middle dot */
|| chr == 0x200b /* zero-width space */
|| chr == 0x2010 /* hyphen */
|| chr == 0x2013 /* en dash */);
}

bool allowsIdeographicBreaking(const std::u32string& string) {
for (uint32_t chr : string) {
if (!allowsIdeographicBreaking(chr)) {
Expand Down
15 changes: 12 additions & 3 deletions src/mbgl/util/i18n.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ namespace mbgl {
namespace util {
namespace i18n {

/// Returns whether a line break can be inserted after any character in the given string.
/// If false, line breaking should occur on word boundaries instead.
/** Returns whether a character is a visible character. */
bool isVisible(uint32_t chr);

/** Returns whether a line break can be inserted after the character indicated
by the given Unicode codepoint due to word breaking. */
bool allowsWordBreaking(uint32_t chr);

/** Returns whether a line break can be inserted after any character in the
given string. If false, line breaking should occur on word boundaries
instead. */
bool allowsIdeographicBreaking(const std::u32string& string);

/// Returns whether a line break can be inserted after the character indicated by the given Unicode codepoint.
/** Returns whether a line break can be inserted after the character indicated
by the given Unicode codepoint due to ideographic breaking. */
bool allowsIdeographicBreaking(uint32_t chr);

} // namespace i18n
Expand Down

0 comments on commit f1e7a51

Please sign in to comment.