Fix splitting bug and optimize a little
This commit is contained in:
parent
5a56f8b81a
commit
210d6326fc
@ -129,10 +129,7 @@ TextPainter::TextPainter(RendererPtr renderer, TextureGroupPtr textureGroup)
|
|||||||
m_fontTextureGroup(textureGroup),
|
m_fontTextureGroup(textureGroup),
|
||||||
m_fontSize(8),
|
m_fontSize(8),
|
||||||
m_lineSpacing(1.30f),
|
m_lineSpacing(1.30f),
|
||||||
m_renderSettings({FontMode::Normal, Vec4B::filled(255), "hobo", ""}),
|
m_renderSettings({FontMode::Normal, Vec4B::filled(255), "hobo", ""}) {
|
||||||
m_splitIgnore(" \t"),
|
|
||||||
m_splitForce("\n\v"),
|
|
||||||
m_nonRenderedCharacters("\n\v\r") {
|
|
||||||
reloadFonts();
|
reloadFonts();
|
||||||
m_reloadTracker = make_shared<TrackerListener>();
|
m_reloadTracker = make_shared<TrackerListener>();
|
||||||
Root::singleton().registerReloadListener(m_reloadTracker);
|
Root::singleton().registerReloadListener(m_reloadTracker);
|
||||||
@ -214,13 +211,9 @@ bool TextPainter::processWrapText(StringView text, Maybe<unsigned> wrapWidth, Wr
|
|||||||
m_fontTextureGroup.switchFont(font);
|
m_fontTextureGroup.switchFont(font);
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
|
|
||||||
StringView splitIgnore(m_splitIgnore);
|
|
||||||
StringView splitForce(m_splitForce);
|
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
auto it = text.begin();
|
auto it = text.begin();
|
||||||
auto end = text.end();
|
auto end = text.end();
|
||||||
auto prevIt = it;
|
|
||||||
|
|
||||||
unsigned lineStart = 0; // Where does this line start ?
|
unsigned lineStart = 0; // Where does this line start ?
|
||||||
unsigned linePixelWidth = 0; // How wide is this line so far
|
unsigned linePixelWidth = 0; // How wide is this line so far
|
||||||
@ -265,30 +258,32 @@ bool TextPainter::processWrapText(StringView text, Maybe<unsigned> wrapWidth, Wr
|
|||||||
lineCharSize++; // assume at least one character if we get here.
|
lineCharSize++; // assume at least one character if we get here.
|
||||||
|
|
||||||
// is this a linefeed / cr / whatever that forces a line split ?
|
// is this a linefeed / cr / whatever that forces a line split ?
|
||||||
if (splitForce.find(character) != NPos) {
|
if (character == '\n' || character == '\v') {
|
||||||
// knock one off the end because we don't render the CR
|
// knock one off the end because we don't render the CR
|
||||||
if (!textFunc(slice(lineStartIt, it), lines++))
|
if (!textFunc(slice(lineStartIt, it), lines++))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lineStart += lineCharSize;
|
lineStart += lineCharSize;
|
||||||
lineStartIt = it;
|
lineStartIt = it;
|
||||||
++lineStartIt; // next line starts after the CR...
|
++lineStartIt;
|
||||||
|
|
||||||
lineCharSize = linePixelWidth = splitPos = 0; // ...with no characters in it and no known splits.
|
lineCharSize = linePixelWidth = splitPos = 0; // ...with no characters in it and no known splits.
|
||||||
} else {
|
} else {
|
||||||
int charWidth = glyphWidth(character);
|
int charWidth = glyphWidth(character);
|
||||||
|
|
||||||
// is it a place where we might want to split the line ?
|
// is it a place where we might want to split the line ?
|
||||||
if (splitIgnore.find(character) != NPos) {
|
if (character == ' ' || character == '\t') {
|
||||||
splitPos = lineStart + lineCharSize;
|
splitPos = lineStart + lineCharSize; // this is the character after the space.
|
||||||
splitWidth = linePixelWidth + charWidth; // the width of the string at
|
splitWidth = linePixelWidth + charWidth; // the width of the string at
|
||||||
splitIt = it;
|
splitIt = it;
|
||||||
|
++splitIt;
|
||||||
// the split point, i.e. after the space.
|
// the split point, i.e. after the space.
|
||||||
}
|
}
|
||||||
|
|
||||||
// would the line be too long if we render this next character ?
|
// would the line be too long if we render this next character ?
|
||||||
if (wrapWidth && (linePixelWidth + charWidth) > *wrapWidth) {
|
if (wrapWidth && (linePixelWidth + charWidth) > *wrapWidth) {
|
||||||
// did we find somewhere to split the line ?
|
// did we find somewhere to split the line ?
|
||||||
if (splitIt != end) {
|
if (splitPos) {
|
||||||
if (!textFunc(slice(lineStartIt, splitIt), lines++))
|
if (!textFunc(slice(lineStartIt, splitIt), lines++))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -300,16 +295,15 @@ bool TextPainter::processWrapText(StringView text, Maybe<unsigned> wrapWidth, Wr
|
|||||||
|
|
||||||
lineStart = splitPos;
|
lineStart = splitPos;
|
||||||
lineStartIt = splitIt;
|
lineStartIt = splitIt;
|
||||||
splitIt = end;
|
|
||||||
splitPos = 0;
|
splitPos = 0;
|
||||||
} else {
|
} else {
|
||||||
if (!textFunc(slice(lineStartIt, it), lines++))
|
if (!textFunc(slice(lineStartIt, it), lines++))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lineStart += lineCharSize - 1; // skip back by one to include that
|
lineStart += lineCharSize - 1;
|
||||||
// character on the next line.
|
lineStartIt = it; // include that character on the next line.
|
||||||
lineStartIt = it;
|
|
||||||
--lineStartIt;
|
|
||||||
|
|
||||||
lineCharSize = 1; // next line has that character in
|
lineCharSize = 1; // next line has that character in
|
||||||
linePixelWidth = charWidth; // and is as wide as that character
|
linePixelWidth = charWidth; // and is as wide as that character
|
||||||
@ -320,7 +314,7 @@ bool TextPainter::processWrapText(StringView text, Maybe<unsigned> wrapWidth, Wr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
prevIt = it++;
|
++it;
|
||||||
};
|
};
|
||||||
|
|
||||||
// if we hit the end of the string before hitting the end of the line.
|
// if we hit the end of the string before hitting the end of the line.
|
||||||
@ -404,10 +398,6 @@ void TextPainter::setMode(FontMode mode) {
|
|||||||
m_renderSettings.mode = mode;
|
m_renderSettings.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextPainter::setSplitIgnore(String const& splitIgnore) {
|
|
||||||
m_splitIgnore = splitIgnore;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextPainter::setFontColor(Vec4B color) {
|
void TextPainter::setFontColor(Vec4B color) {
|
||||||
m_renderSettings.color = move(color);
|
m_renderSettings.color = move(color);
|
||||||
}
|
}
|
||||||
@ -551,7 +541,7 @@ RectF TextPainter::doRenderLine(StringView text, TextPositioning const& position
|
|||||||
}
|
}
|
||||||
|
|
||||||
RectF TextPainter::doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender) {
|
RectF TextPainter::doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender) {
|
||||||
if (m_nonRenderedCharacters.find(String(c)) != NPos)
|
if (c == '\n' || c == '\v' || c == '\r')
|
||||||
return RectF();
|
return RectF();
|
||||||
m_fontTextureGroup.switchFont(m_renderSettings.font);
|
m_fontTextureGroup.switchFont(m_renderSettings.font);
|
||||||
|
|
||||||
|
@ -77,7 +77,6 @@ public:
|
|||||||
void setFontSize(unsigned size);
|
void setFontSize(unsigned size);
|
||||||
void setLineSpacing(float lineSpacing);
|
void setLineSpacing(float lineSpacing);
|
||||||
void setMode(FontMode mode);
|
void setMode(FontMode mode);
|
||||||
void setSplitIgnore(String const& splitIgnore);
|
|
||||||
void setFontColor(Vec4B color);
|
void setFontColor(Vec4B color);
|
||||||
void setProcessingDirectives(String directives);
|
void setProcessingDirectives(String directives);
|
||||||
void setFont(String const& font);
|
void setFont(String const& font);
|
||||||
@ -109,8 +108,6 @@ private:
|
|||||||
RenderSettings m_renderSettings;
|
RenderSettings m_renderSettings;
|
||||||
RenderSettings m_savedRenderSettings;
|
RenderSettings m_savedRenderSettings;
|
||||||
|
|
||||||
String m_splitIgnore;
|
|
||||||
String m_splitForce;
|
|
||||||
String m_nonRenderedCharacters;
|
String m_nonRenderedCharacters;
|
||||||
|
|
||||||
TrackerListenerPtr m_reloadTracker;
|
TrackerListenerPtr m_reloadTracker;
|
||||||
|
Loading…
Reference in New Issue
Block a user