Skip to content

Commit 9913878

Browse files
committed
Turbo Badger: Adjusted backend to new integration interface
Added resources for fonty skin Added stb font renderer to the backend so we can use nebula's copy of stb_truetype
1 parent 739da61 commit 9913878

17 files changed

+1095
-48
lines changed

code/addons/tbui/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ add_shaders(tbui.fx)
2222

2323
fips_dir(backend)
2424
fips_files(
25+
tbuifontrenderer.cc
2526
tbuibatch.h
2627
tbuibitmap.cc
2728
tbuibitmap.h
28-
tbuiclipboard.cc
29-
tbuifile.cc
30-
tbuifile.h
29+
tbuiclipboardinterface.cc
30+
tbuiclipboardinterface.h
31+
tbuifileinterface.cc
32+
tbuifileinterface.h
3133
tbuirenderer.cc
3234
tbuirenderer.h
33-
tbuisystem.cc
35+
tbuisysteminterface.cc
36+
tbuisysteminterface.h
3437
tbuivertex.h
3538
)
3639

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//------------------------------------------------------------------------------
2+
// backend/tbuiclipboard.cc
3+
// (C) 2024 Individual contributors, see AUTHORS file
4+
//------------------------------------------------------------------------------
5+
#include "render/stdneb.h"
6+
#include "tbuiclipboardinterface.h"
7+
8+
namespace TBUI
9+
{
10+
11+
// == TBClipboard =====================================
12+
13+
void
14+
TBUIClipboardInterface::Empty()
15+
{
16+
clipboard.Clear();
17+
}
18+
19+
bool
20+
TBUIClipboardInterface::HasText()
21+
{
22+
return !clipboard.IsEmpty();
23+
}
24+
25+
bool
26+
TBUIClipboardInterface::SetText(const char* text)
27+
{
28+
return clipboard.Set(text);
29+
}
30+
31+
bool
32+
TBUIClipboardInterface::GetText(tb::TBStr& text)
33+
{
34+
return text.Set(clipboard);
35+
}
36+
37+
} // namespace tb
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//------------------------------------------------------------------------------
2+
// backend/tbuiclipboard.cc
3+
// (C) 2024 Individual contributors, see AUTHORS file
4+
//------------------------------------------------------------------------------
5+
#include "render/stdneb.h"
6+
#include "platform/tb_clipboard_interface.h"
7+
8+
namespace TBUI
9+
{
10+
11+
class TBUIClipboardInterface : public tb::TBClipboardInterface
12+
{
13+
public:
14+
void Empty() override;
15+
bool HasText() override;
16+
bool SetText(const char* text) override;
17+
bool GetText(tb::TBStr& text) override;
18+
19+
private:
20+
tb::TBStr clipboard;
21+
};
22+
23+
} // namespace tb
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//------------------------------------------------------------------------------
2+
// backend/tbuifile.cc
3+
// (C) 2024 Individual contributors, see AUTHORS file
4+
//------------------------------------------------------------------------------
5+
#include "render/stdneb.h"
6+
#include "io/ioserver.h"
7+
#include "io/stream.h"
8+
#include "tbuifileinterface.h"
9+
10+
namespace
11+
{
12+
size_t
13+
allocateFileId()
14+
{
15+
static size_t s_fileId = 0;
16+
return ++s_fileId;
17+
}
18+
}
19+
20+
namespace TBUI
21+
{
22+
tb::TBFileHandle
23+
TBUIFileInterface::Open(const char* filename, TBFileMode mode)
24+
{
25+
if (mode != TBFileMode::MODE_READ)
26+
return 0;
27+
28+
Ptr<IO::FileStream> stream = IO::IoServer::Instance()->CreateStream(filename).downcast<IO::FileStream>();
29+
stream->SetAccessMode(IO::Stream::AccessMode::ReadAccess);
30+
if (!stream->Open())
31+
{
32+
return 0;
33+
}
34+
35+
size_t fileId = allocateFileId();
36+
openFiles.Add(fileId, stream);
37+
return static_cast<tb::TBFileHandle>(fileId);
38+
}
39+
40+
void
41+
TBUIFileInterface::Close(tb::TBFileHandle file)
42+
{
43+
size_t fileId = static_cast<size_t>(file);
44+
45+
if (openFiles.Contains(fileId))
46+
{
47+
openFiles[fileId]->Close();
48+
openFiles.Erase(fileId);
49+
}
50+
}
51+
52+
long
53+
TBUIFileInterface::Size(tb::TBFileHandle file)
54+
{
55+
size_t fileId = static_cast<size_t>(file);
56+
57+
if (openFiles.Contains(fileId))
58+
{
59+
return openFiles[fileId]->GetSize();
60+
}
61+
62+
return 0;
63+
}
64+
65+
size_t
66+
TBUIFileInterface::Read(tb::TBFileHandle file, void* buf, size_t elemSize, size_t count)
67+
{
68+
size_t fileId = static_cast<size_t>(file);
69+
70+
if (openFiles.Contains(fileId))
71+
{
72+
return openFiles[fileId]->Read(buf, count);
73+
}
74+
75+
return 0;
76+
}
77+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
//------------------------------------------------------------------------------
3+
/**
4+
Turbobadger UI File interface
5+
6+
@copyright
7+
(C) 2024 Individual contributors, see AUTHORS file
8+
*/
9+
//------------------------------------------------------------------------------
10+
11+
#include "io/filestream.h"
12+
#include "util/string.h"
13+
#include "util/dictionary.h"
14+
#include "platform/tb_file_interface.h"
15+
16+
namespace TBUI
17+
{
18+
class TBUIFileInterface : public tb::TBFileInterface
19+
{
20+
public:
21+
tb::TBFileHandle Open(const char* filename, TBFileMode mode) override;
22+
void Close(tb::TBFileHandle file) override;
23+
24+
long Size(tb::TBFileHandle file) override;
25+
size_t Read(tb::TBFileHandle file, void* buf, size_t elemSize, size_t count) override;
26+
27+
bool IsOpen(tb::TBFileHandle file) const;
28+
29+
private:
30+
Util::Dictionary<size_t, Ptr<IO::FileStream>> openFiles;
31+
};
32+
33+
inline bool
34+
TBUIFileInterface::IsOpen(tb::TBFileHandle file) const
35+
{
36+
return openFiles.Contains(static_cast<size_t>(file));
37+
}
38+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// ================================================================================
2+
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
3+
// == See tb_core.h for more information. ==
4+
// ================================================================================
5+
6+
#include <math.h>
7+
#include "tb_renderer.h"
8+
#include "platform/tb_system_interface.h"
9+
#include "tb_tempbuffer.h"
10+
#include "tbuifontrenderer.h"
11+
12+
namespace TBUI
13+
{
14+
//#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate
15+
#include "stb_truetype.h"
16+
17+
TBUISTBFontRenderer::TBUISTBFontRenderer()
18+
: render_data(nullptr)
19+
{
20+
}
21+
22+
TBUISTBFontRenderer::~TBUISTBFontRenderer()
23+
{
24+
stbtt_FreeBitmap(render_data, &font);
25+
}
26+
27+
tb::TBFontMetrics
28+
TBUISTBFontRenderer::GetMetrics()
29+
{
30+
tb::TBFontMetrics metrics;
31+
int ascent, descent, lineGap;
32+
stbtt_GetFontVMetrics(&font, &ascent, &descent, &lineGap);
33+
metrics.ascent = (int)(ascent * scale + 0.5f);
34+
metrics.descent = (int)((-descent) * scale + 0.5f);
35+
metrics.height = (int)((ascent - descent + lineGap) * scale + 0.5f);
36+
return metrics;
37+
}
38+
39+
bool
40+
TBUISTBFontRenderer::RenderGlyph(tb::TBFontGlyphData* data, UCS4 cp)
41+
{
42+
stbtt_FreeBitmap(render_data, &font);
43+
render_data = stbtt_GetCodepointBitmap(&font, 0, scale, cp, &data->w, &data->h, 0, 0);
44+
data->data8 = render_data;
45+
data->stride = data->w;
46+
data->rgb = false;
47+
return data->data8 ? true : false;
48+
}
49+
50+
void
51+
TBUISTBFontRenderer::GetGlyphMetrics(tb::TBGlyphMetrics* metrics, UCS4 cp)
52+
{
53+
int advance, leftSideBearing;
54+
const int gi = stbtt_FindGlyphIndex(&font, cp);
55+
stbtt_GetGlyphHMetrics(&font, gi, &advance, &leftSideBearing);
56+
metrics->advance = (int)roundf(advance * scale);
57+
58+
int ix0, iy0;
59+
stbtt_GetGlyphBitmapBoxSubpixel(&font, gi, scale, scale, 0.f, 0.f, &ix0, &iy0, 0, 0);
60+
metrics->x = ix0;
61+
metrics->y = iy0;
62+
}
63+
64+
int
65+
TBUISTBFontRenderer::GetAdvance(UCS4 cp1, UCS4 cp2)
66+
{
67+
int advance, leftSideBearing;
68+
const int gi1 = stbtt_FindGlyphIndex(&font, cp1);
69+
stbtt_GetGlyphHMetrics(&font, gi1, &advance, &leftSideBearing);
70+
if (font.kern)
71+
advance += stbtt_GetGlyphKernAdvance(&font, gi1, stbtt_FindGlyphIndex(&font, cp2));
72+
return (int)roundf(advance * scale);
73+
}
74+
75+
bool
76+
TBUISTBFontRenderer::Load(const char* filename, int size)
77+
{
78+
if (!ttf_buffer.AppendFile(filename))
79+
return false;
80+
81+
const unsigned char* ttf_ptr = (const unsigned char*)ttf_buffer.GetData();
82+
stbtt_InitFont(&font, ttf_ptr, stbtt_GetFontOffsetForIndex(ttf_ptr, 0));
83+
84+
scale = stbtt_ScaleForPixelHeight(&font, (float)size);
85+
return true;
86+
}
87+
88+
tb::TBFontFace*
89+
TBUISTBFontRenderer::Create(tb::TBFontManager* font_manager, const char* filename, const tb::TBFontDescription& font_desc)
90+
{
91+
if (TBUISTBFontRenderer* fr = new TBUISTBFontRenderer())
92+
{
93+
if (fr->Load(filename, (int)font_desc.GetSize()))
94+
if (tb::TBFontFace* font = new tb::TBFontFace(font_manager->GetGlyphCache(), fr, font_desc))
95+
return font;
96+
delete fr;
97+
}
98+
return nullptr;
99+
}
100+
101+
} // namespace TBUI
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ================================================================================
2+
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
3+
// == See tb_core.h for more information. ==
4+
// ================================================================================
5+
6+
#include <math.h>
7+
#include "tb_font_renderer.h"
8+
#include "tb_renderer.h"
9+
#include "platform/tb_system_interface.h"
10+
#include "tb_tempbuffer.h"
11+
12+
namespace TBUI
13+
{
14+
/** STBFontRenderer renders fonts using stb_truetype.h (http://nothings.org/) */
15+
16+
class TBUISTBFontRenderer : public tb::TBFontRenderer
17+
{
18+
public:
19+
TBUISTBFontRenderer();
20+
~TBUISTBFontRenderer();
21+
22+
bool Load(const char* filename, int size);
23+
24+
virtual tb::TBFontFace* Create(tb::TBFontManager* font_manager, const char* filename, const tb::TBFontDescription& font_desc);
25+
26+
virtual tb::TBFontMetrics GetMetrics();
27+
virtual bool RenderGlyph(tb::TBFontGlyphData* dst_bitmap, UCS4 cp);
28+
virtual void GetGlyphMetrics(tb::TBGlyphMetrics* metrics, UCS4 cp);
29+
virtual int GetAdvance(UCS4 cp1, UCS4 cp2);
30+
31+
private:
32+
stbtt_fontinfo font;
33+
tb::TBTempBuffer ttf_buffer;
34+
unsigned char* render_data;
35+
float scale;
36+
};
37+
}

0 commit comments

Comments
 (0)