line wrapping

This commit is contained in:
Ansis Brammanis
2014-05-30 01:32:15 -04:00
parent 63729a5009
commit 9057c4980d
3 changed files with 51 additions and 8 deletions

View File

@@ -765,8 +765,8 @@ module.exports = {
"path": "horizontal",
"font": "Open Sans Semibold, Arial Unicode MS Bold",
"fontSize": 18,
"text-max-width": 2,
"text-line-height": 1.4,
"maxWidth": 2,
"lineHeight": 1.4,
"feature_type": "point",
"type": "text"
},

View File

@@ -53,8 +53,8 @@ public:
uint32_t face = 0;
uint32_t glyph = 0;
uint32_t x = 0;
uint32_t y = 0;
int32_t x = 0;
int32_t y = 0;
};
typedef std::vector<GlyphPlacement> Shaping;

View File

@@ -47,11 +47,54 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float &m
return shaped;
}
void alignHorizontally(Shaping &shaping, const std::map<uint32_t, GlyphMetrics> &metrics,
const uint32_t &start, const uint32_t &end, const float &alignment) {
uint32_t lastAdvance = metrics.find(shaping[end].glyph)->second.advance;
int32_t lineIndent = (shaping[end].x + lastAdvance) * alignment;
for (uint32_t j = start; j <= end; j++) {
shaping[j].x -= lineIndent;
}
}
Shaping FontStack::lineWrap(Shaping shaped, const float &lineHeight, const float &maxWidth, const float &alignment) const {
std::cerr << "\nlineHeight: " << lineHeight <<
"\nmaxWidth: " << maxWidth <<
"\nalignment: " << alignment <<
"\n";
uint32_t lastSafeBreak = 0;
uint32_t lengthBeforeCurrentLine = 0;
uint32_t lineStartIndex = 0;
uint32_t line = 0;
for (uint32_t i = 0; i < shaped.size(); i++) {
GlyphPlacement &shape = shaped[i];
shape.x -= lengthBeforeCurrentLine;
shape.y += lineHeight * line;
if (shape.x > maxWidth && lastSafeBreak != 0) {
uint32_t lineLength = shaped[lastSafeBreak + 1].x;
for (uint32_t k = lastSafeBreak + 1; k <= i; k++) {
shaped[k].y += lineHeight;
shaped[k].x -= lineLength;
}
if (alignment) {
alignHorizontally(shaped, metrics, lineStartIndex, lastSafeBreak - 1, alignment);
}
lineStartIndex = lastSafeBreak + 1;
lastSafeBreak = 0;
lengthBeforeCurrentLine += lineLength;
line++;
}
if (shape.glyph == 32) {
lastSafeBreak = i;
}
}
alignHorizontally(shaped, metrics, lineStartIndex, shaped.size() - 1, alignment);
return shaped;
}