6using namespace Tempest;
12 :pfnt(std::make_shared<zenkit::Font>()), color(cl) {
19 return int(fntHeight);
24 fntHeight = uint32_t(std::max(
float(pfnt->height)*scale, 1.f));
28 std::string_view txt, Tempest::AlignFlag align,
int firstLine)
const {
29 if(tex==
nullptr || txt.empty())
33 p.setBrush(Brush(*tex,color));
34 processText(&p,bx,by,bw,bh,txt,align,firstLine);
38Size GthFont::processText(Painter* p,
int bx,
int by,
int bw,
int bh,
39 std::string_view txtView, AlignFlag align,
int firstLine)
const {
40 const uint8_t* txt =
reinterpret_cast<const uint8_t*
>(txtView.data());
41 const auto& fnt = *pfnt;
45 float tw = float(tex->w());
46 float th = float(tex->h());
52 auto t = getLine(txt,bw,lwidth);
66 if(ret.h+sz.h>bh && bh>0) {
70 ret.w = std::max(ret.w,sz.w);
73 if(align!=NoAlign && align!=AlignLeft) {
75 if(align & AlignHCenter)
77 if(align & AlignRight)
82 for(
auto i=txt; i!=t; ++i) {
84 auto& uv1 = fnt.glyphs[id].uv[0];
85 auto& uv2 = fnt.glyphs[id].uv[1];
86 int w = int(fnt.glyphs[
id].width * scale);
90 tw*uv1.x,th*uv1.y, tw*uv2.x,th*uv2.y);
104 if(tex==
nullptr || txtChar.empty())
107 const uint8_t* txt =
reinterpret_cast<const uint8_t*
>(txtChar.data());
108 const auto& fnt = *pfnt;
111 p.setBrush(Brush(*tex,color));
115 float tw = float(tex->w());
116 float th = float(tex->h());
118 for(
size_t i=0;txt[i];++i) {
120 auto& uv1 = fnt.glyphs[id].uv[0];
121 auto& uv2 = fnt.glyphs[id].uv[1];
122 int w = int(fnt.glyphs[
id].width * scale);
125 tw*uv1.x,th*uv1.y, tw*uv2.x,th*uv2.y);
134 return textSize(txt.data(),txt.data()+txt.size());
138 const uint8_t* b =
reinterpret_cast<const uint8_t*
>(cb);
139 const uint8_t* e =
reinterpret_cast<const uint8_t*
>(ce);
144 const auto& fnt = *pfnt;
153 totalW = std::max(totalW,x);
157 totalW = std::max(totalW,x);
159 while(b[i]==
' ' && b+i!=e)
164 else if(
id>=fnt.glyphs.size()) {
169 int w = int(fnt.glyphs[
id].width * scale);
175 return Size(totalW,y);
179 if(tex==
nullptr || txt.empty())
181 return processText(
nullptr,0,0,bw,0,txt,NoAlign,0);
185 if(tex==
nullptr || txt.empty())
187 auto ret = processText(
nullptr,0,0,bw,0,txt,NoAlign,0);
191const uint8_t* GthFont::getLine(
const uint8_t *txt,
int bw,
int& width)
const {
194 int wwidth = 0, wspace = 0;
197 txt = getWord(txt,wwidth,wspace);
200 while(*txt!=
'\0' && *txt!=
'\n') {
201 auto t = getWord(txt,wwidth,wspace);
215const uint8_t* GthFont::getWord(
const uint8_t *txt,
int& width,
int& space)
const {
219 const auto& fnt = *pfnt;
222 if(
id==
'\0' ||
id==
'\n')
226 space += int(fnt.glyphs[
id].width * scale);
232 if(
id==
'\0' ||
id==
'\n' ||
id==
' ')
235 width += int(fnt.glyphs[
id].width * scale);
240bool GthFont::isSpace(uint8_t ch) {
241 return ch==
' ' || ch==
'\n';
auto lineCount(int w, std::string_view txt) const -> int32_t
void drawText(Tempest::Painter &p, int x, int y, int w, int h, std::string_view txt, Tempest::AlignFlag align, int firstLine=0) const
auto textSize(const std::string_view txt) const -> Tempest::Size
static const Tempest::Texture2d * loadTexture(std::string_view name, bool forceMips=false)