From 68042ed86df6cfe8bec75d3933795b77ff5b1c80 Mon Sep 17 00:00:00 2001 From: zetaPRIME Date: Thu, 16 Mar 2017 08:27:13 -0400 Subject: [PATCH] clear out dead code from text rendering --- .../source/starlight/gfx/BitmapFont.cpp | 43 --------- .../source/starlight/gfx/BitmapFont.h | 7 +- libstarlight/source/starlight/gfx/Font.h | 4 +- libstarlight/source/starlight/gfx/FontBMF.cpp | 96 +------------------ libstarlight/source/starlight/gfx/FontBMF.h | 6 +- libstarlight/todo.txt | 3 + 6 files changed, 13 insertions(+), 146 deletions(-) diff --git a/libstarlight/source/starlight/gfx/BitmapFont.cpp b/libstarlight/source/starlight/gfx/BitmapFont.cpp index baf15cf..98da6c5 100644 --- a/libstarlight/source/starlight/gfx/BitmapFont.cpp +++ b/libstarlight/source/starlight/gfx/BitmapFont.cpp @@ -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(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 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(cl) | (static_cast(cr) << 8)); } diff --git a/libstarlight/source/starlight/gfx/Font.h b/libstarlight/source/starlight/gfx/Font.h index 88d2ac8..e4e3ed5 100644 --- a/libstarlight/source/starlight/gfx/Font.h +++ b/libstarlight/source/starlight/gfx/Font.h @@ -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 borderColor = nullptr) = 0; virtual void Print(VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef 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 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 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 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 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; diff --git a/libstarlight/source/starlight/gfx/FontBMF.cpp b/libstarlight/source/starlight/gfx/FontBMF.cpp index 62e0899..dc49bce 100644 --- a/libstarlight/source/starlight/gfx/FontBMF.cpp +++ b/libstarlight/source/starlight/gfx/FontBMF.cpp @@ -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(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(nullptr), &dl); { auto qn = dl.GetLastNode(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(nullptr), &dl); { auto qn = dl.GetLastNode(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 borderColor) { - -} - -void FontBMF::PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale, Color color, Vector2 justification, OptRef 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(font), c = borderColor.get()](Vector2 offset) mutable { - if (auto f = fnt.lock()) f->txBorder->Bind(c); - }); - else dl->AddFunc([fnt = std::weak_ptr(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(nullptr), dl); - if (drawBorder) { - auto vlist = dl->GetLastNode(); - dl->AddFunc([fnt = std::weak_ptr(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 borderColor, float maxWidth, Vector2* measure, DisplayList* dl) { - float lineHeight = font->lineHeight * scale; - float spaceWidth = font->Char(' ').advX * scale; - - float longest = 0; - std::vector> ptree; - std::vector lwidth; - { - std::stringstream ss(text); - std::string line, word; - - // prepare line buffer - ptree.push_back(std::list()); - 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()); - 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()); - 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); } diff --git a/libstarlight/source/starlight/gfx/FontBMF.h b/libstarlight/source/starlight/gfx/FontBMF.h index 187d734..c004f27 100644 --- a/libstarlight/source/starlight/gfx/FontBMF.h +++ b/libstarlight/source/starlight/gfx/FontBMF.h @@ -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 borderColor, float maxWidth, Vector2* measure, DisplayList* dl); + // public: std::shared_ptr 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 borderColor = nullptr) override; void Print(VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef borderColor = nullptr) override; - void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef borderColor = nullptr) override; - void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef borderColor = nullptr) override; + //void PrintDisplayList(DisplayList* dl, Vector2 position, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef borderColor = nullptr) override; + //void PrintDisplayList(DisplayList* dl, VRect rect, std::string& text, float scale = 1, Color color = Color::white, Vector2 justification = Vector2::zero, OptRef 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; diff --git a/libstarlight/todo.txt b/libstarlight/todo.txt index 7843083..67b7492 100644 --- a/libstarlight/todo.txt +++ b/libstarlight/todo.txt @@ -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 }