mirror of
https://github.com/zetaPRIME/libstarlight.git
synced 2025-06-26 13:42:46 +00:00
clear out dead code from text rendering
This commit is contained in:
parent
5960a56f60
commit
68042ed86d
@ -63,29 +63,6 @@ BitmapFont::CharInfo& BitmapFont::Char(char c) {
|
||||
return cdefault;
|
||||
}
|
||||
|
||||
float BitmapFont::DrawText(const Vector2& penStart, std::string& msg, float scale, DisplayList* dl) {
|
||||
Vector2 pen = penStart;
|
||||
Vector2 pad(padX, 0);//-padY); // compensate for padding around glyphs
|
||||
pen -= pad;
|
||||
Vector2 uvScale = Vector2::one / txMain->txSize;
|
||||
|
||||
auto qn = dl ? dl->GetLastNode<DLNode_Quads>(true) : nullptr;
|
||||
|
||||
char cl = 0xFF;//' ';
|
||||
for (auto c : msg) {
|
||||
pen.x += GetKerning(cl, c) * scale;
|
||||
auto& ci = Char(c);
|
||||
//printf("%c w %f h %f adv %f sc %f\n", c, ci.width, ci.height, ci.advX, scale);
|
||||
VRect crect(ci.imgX, ci.imgY, ci.width, ci.height);
|
||||
if (dl) qn->Add(VRect(pen, crect.size * scale), crect * uvScale);
|
||||
pen.x += ci.advX * scale;
|
||||
|
||||
cl = c;
|
||||
}
|
||||
pen += pad;
|
||||
return pen.x - penStart.x;
|
||||
}
|
||||
|
||||
Vector2 BitmapFont::MeasureTo(std::string& msg, bool total, unsigned int end, float maxWidth) {
|
||||
if (total) {
|
||||
Vector2 measure = Vector2::zero;
|
||||
@ -243,23 +220,3 @@ void BitmapFont::ForChar(const std::string& msg, std::function<bool(CharLoopStat
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
|
@ -59,13 +59,12 @@ namespace starlight {
|
||||
|
||||
float GetKerning(char cl, char cr);
|
||||
CharInfo& Char(char c);
|
||||
float DrawText(const Vector2& penStart, std::string& msg, float scale = 1, DisplayList* dl = nullptr);
|
||||
// what to put in the bitmapfont class itself?
|
||||
Vector2 MeasureTo(std::string& msg, bool total = true, unsigned int end = 4294967295, float maxWidth = 65536*64);
|
||||
unsigned int PointToIndex(std::string& msg, Vector2 pt, float maxWidth = 65536*64);
|
||||
|
||||
void ForChar(const std::string& msg, std::function<bool(CharLoopState&)> func, float maxWidth = 65536*64);
|
||||
|
||||
Vector2 MeasureTo(std::string& msg, bool total = true, unsigned int end = 4294967295, float maxWidth = 65536*64);
|
||||
unsigned int PointToIndex(std::string& msg, Vector2 pt, float maxWidth = 65536*64);
|
||||
|
||||
static inline constexpr unsigned int KerningKey(char cl, char cr) {
|
||||
return (static_cast<unsigned int>(cl) | (static_cast<unsigned int>(cr) << 8));
|
||||
}
|
||||
|
@ -22,8 +22,8 @@ namespace starlight {
|
||||
virtual Vector2 Measure(std::string& text, float scale = 1, float maxWidth = 400) = 0;
|
||||
virtual void Print(Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
virtual void Print(VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
virtual void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
virtual void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
//virtual void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
//virtual void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) = 0;
|
||||
|
||||
virtual Vector2 GetCursorPosition(VRect rect, std::string& text, unsigned int end, float scale = 1) = 0;
|
||||
virtual unsigned int GetCursorFromPoint(VRect rect, std::string& text, Vector2 pt, float scale = 1) = 0;
|
||||
|
@ -14,13 +14,10 @@ using starlight::GFXManager;
|
||||
using starlight::gfx::Font;
|
||||
using starlight::gfx::FontBMF;
|
||||
|
||||
#define err(nth, wat) *((unsigned int*)0x00100000+(nth))=wat;
|
||||
#define ded(wat) err(0,wat)
|
||||
//#define err(nth, wat) *((unsigned int*)0x00100000+(nth))=wat;
|
||||
//#define ded(wat) err(0,wat)
|
||||
Vector2 FontBMF::Measure(std::string& text, float scale, float maxWidth) {
|
||||
if (text == "") return Vector2::zero;
|
||||
/*Vector2 v;
|
||||
PrintOp(Vector2(), text, scale, Color(), Vector2(), nullptr, maxWidth, &v, static_cast<DisplayList*>(nullptr));
|
||||
return v;*/
|
||||
return font->MeasureTo(text, true, text.length(), maxWidth) * scale;
|
||||
}
|
||||
|
||||
@ -28,7 +25,6 @@ void FontBMF::Print(Vector2 position, std::string& text, float scale, Color colo
|
||||
if (text == "") return;
|
||||
if (GFXManager::PrepareForDrawing()) {
|
||||
DisplayList dl = DisplayList();
|
||||
//PrintOp(position, text, scale, color, justification, borderColor, 2147483647, static_cast<Vector2*>(nullptr), &dl);
|
||||
{
|
||||
auto qn = dl.GetLastNode<DLNode_Quads>(true);
|
||||
Vector2 uvScale = Vector2::one / font->txMain->txSize;
|
||||
@ -58,7 +54,6 @@ void FontBMF::Print(VRect rect, std::string& text, float scale, Color color, Vec
|
||||
if (borderColor && borderColor.get() != Color::transparent) rect = rect.Expand(-1, -1);
|
||||
Vector2 position = rect.pos + rect.size * justification;
|
||||
DisplayList dl = DisplayList();
|
||||
//PrintOp(pos, text, scale, color, justification, borderColor, rect.size.x, static_cast<Vector2*>(nullptr), &dl);
|
||||
{
|
||||
auto qn = dl.GetLastNode<DLNode_Quads>(true);
|
||||
Vector2 uvScale = Vector2::one / font->txMain->txSize;
|
||||
@ -83,93 +78,6 @@ void FontBMF::Print(VRect rect, std::string& text, float scale, Color color, Vec
|
||||
}
|
||||
}
|
||||
|
||||
void FontBMF::PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale, Color color, Vector2 justification, OptRef<Color> borderColor) {
|
||||
|
||||
}
|
||||
|
||||
void FontBMF::PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale, Color color, Vector2 justification, OptRef<Color> borderColor) {
|
||||
// this bind is super kludge ;.;
|
||||
bool drawBorder = (borderColor && borderColor.get() != Color::transparent);
|
||||
if (drawBorder) rect = rect.Expand(-1, -1);
|
||||
Vector2 pos = rect.pos + rect.size * justification;
|
||||
dl->offset = pos - rect.pos;
|
||||
if (drawBorder) dl->AddFunc([fnt = std::weak_ptr<BitmapFont>(font), c = borderColor.get()](Vector2 offset) mutable {
|
||||
if (auto f = fnt.lock()) f->txBorder->Bind(c);
|
||||
});
|
||||
else dl->AddFunc([fnt = std::weak_ptr<BitmapFont>(font), c = color](Vector2 offset) mutable {
|
||||
if (auto f = fnt.lock()) f->txMain->Bind(c);
|
||||
});
|
||||
PrintOp(pos, text, scale, color, justification, borderColor, rect.size.x, static_cast<Vector2*>(nullptr), dl);
|
||||
if (drawBorder) {
|
||||
auto vlist = dl->GetLastNode<DLNode_Quads>();
|
||||
dl->AddFunc([fnt = std::weak_ptr<BitmapFont>(font), c = color](Vector2 offset) mutable {
|
||||
if (auto f = fnt.lock()) f->txMain->Bind(c);
|
||||
});
|
||||
dl->AddNode(vlist);
|
||||
}
|
||||
}
|
||||
|
||||
void FontBMF::PrintOp(Vector2 position, std::string& text, float scale, const Color& color, Vector2 justification, OptRef<Color> borderColor, float maxWidth, Vector2* measure, DisplayList* dl) {
|
||||
float lineHeight = font->lineHeight * scale;
|
||||
float spaceWidth = font->Char(' ').advX * scale;
|
||||
|
||||
float longest = 0;
|
||||
std::vector<std::list<std::string>> ptree;
|
||||
std::vector<float> lwidth;
|
||||
{
|
||||
std::stringstream ss(text);
|
||||
std::string line, word;
|
||||
|
||||
// prepare line buffer
|
||||
ptree.push_back(std::list<std::string>());
|
||||
auto* cline = &ptree.back();
|
||||
float cLineLen = -spaceWidth;
|
||||
|
||||
// split into words and lines
|
||||
while(getline(ss, line, '\n')) {
|
||||
std::stringstream ls(line);
|
||||
while (getline(ls, word, ' ')) {
|
||||
float ww = spaceWidth + font->DrawText(Vector2(), word, scale);
|
||||
if (cLineLen + ww > maxWidth) {
|
||||
lwidth.push_back(cLineLen);
|
||||
ptree.push_back(std::list<std::string>());
|
||||
cline = &ptree.back();
|
||||
if (cLineLen > longest) longest = cLineLen;
|
||||
cLineLen = -spaceWidth;
|
||||
}
|
||||
cLineLen += ww;
|
||||
cline->push_back(word);
|
||||
//
|
||||
}
|
||||
lwidth.push_back(cLineLen);
|
||||
ptree.push_back(std::list<std::string>());
|
||||
cline = &ptree.back();
|
||||
if (cLineLen > longest) longest = cLineLen;
|
||||
cLineLen = -spaceWidth;
|
||||
}
|
||||
}
|
||||
|
||||
if (measure) { // measurement operation
|
||||
*measure = Vector2(longest, lwidth.size() * lineHeight);
|
||||
}
|
||||
if (!dl) return;
|
||||
|
||||
//Vector2 lp = position - Vector2(0, lwidth.size() * lineHeight) * justification;
|
||||
Vector2 lp = -Vector2(0, lwidth.size() * lineHeight) * justification;
|
||||
lp.y -= lineHeight - font->baseY * scale;
|
||||
lp.x = floor(lp.x); lp.y = floor(lp.y); // normalize across zero-crossings
|
||||
int li = 0;
|
||||
|
||||
for (auto& line : ptree) {
|
||||
Vector2 wp = lp - Vector2(lwidth[li], 0) * justification;
|
||||
for (auto& word : line) {
|
||||
wp.x += spaceWidth + font->DrawText(wp, word, scale, dl);
|
||||
}
|
||||
lp += Vector2(0, lineHeight);
|
||||
li++;
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 FontBMF::GetCursorPosition(VRect rect, std::string& text, unsigned int end, float scale) {
|
||||
return rect.pos + (font->MeasureTo(text, false, end, rect.size.x / scale)* scale);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace starlight {
|
||||
namespace gfx {
|
||||
class FontBMF : public Font {
|
||||
private:
|
||||
void PrintOp(Vector2 position, std::string& text, float scale, const Color& color, Vector2 justification, OptRef<Color> borderColor, float maxWidth, Vector2* measure, DisplayList* dl);
|
||||
//
|
||||
|
||||
public:
|
||||
std::shared_ptr<BitmapFont> font;
|
||||
@ -21,8 +21,8 @@ namespace starlight {
|
||||
Vector2 Measure(std::string& text, float scale = 1, float maxWidth = 400) override;
|
||||
void Print(Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
void Print(VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
//void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
//void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef<Color> borderColor = nullptr) override;
|
||||
|
||||
Vector2 GetCursorPosition(VRect rect, std::string& text, unsigned int end, float scale) override;
|
||||
unsigned int GetCursorFromPoint(VRect rect, std::string& text, Vector2 pt, float scale) override;
|
||||
|
@ -8,6 +8,9 @@ roadmap to first release, in no particular order {
|
||||
- abstract osk input actions into a separate object/class heirarchy
|
||||
- preview where applicable
|
||||
fix font glyph padding to eliminate slight "crosstalk" in bordered variants
|
||||
fix lowercase j running into things
|
||||
adjust monospace line height (and maybe offset) to closer match the vwf of the same size
|
||||
add justification to TextConfig as a default
|
||||
polish!
|
||||
InputManager::OpenKeyboard
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user