Bitmap font to custom font conversion

This commit is contained in:
Ilari Liusvaara 2013-12-11 21:12:34 +02:00
parent 00b8a193a0
commit cd80fa3ebf
7 changed files with 78 additions and 4 deletions

View file

@ -28,6 +28,7 @@ struct custom_font
public: public:
custom_font(); custom_font();
custom_font(const std::string& file); custom_font(const std::string& file);
custom_font(struct bitmap_font& bfont);
void add(const std::u32string& key, const font_glyph_data& glyph) throw(std::bad_alloc); void add(const std::u32string& key, const font_glyph_data& glyph) throw(std::bad_alloc);
std::u32string best_ligature_match(const std::u32string& codepoints, size_t start) const std::u32string best_ligature_match(const std::u32string& codepoints, size_t start) const
throw(std::bad_alloc); throw(std::bad_alloc);

View file

@ -5,6 +5,7 @@
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include <map> #include <map>
#include <set>
template<bool X> struct framebufferelem {}; template<bool X> struct framebufferelem {};
template<> struct framebufferelem<false> { typedef uint32_t t; }; template<> struct framebufferelem<false> { typedef uint32_t t; };
@ -513,6 +514,14 @@ struct bitmap_font
* Returns: String layout. * Returns: String layout.
*/ */
std::vector<layout> dolayout(const std::string& string) throw(std::bad_alloc); std::vector<layout> dolayout(const std::string& string) throw(std::bad_alloc);
/**
* Get set of all glyph numbers.
*/
std::set<uint32_t> get_glyphs_set();
/**
* Get bad glyph.
*/
const glyph& get_bad_glyph() throw();
/** /**
* Render string to framebuffer. * Render string to framebuffer.
* *

View file

@ -1633,11 +1633,12 @@ gui.loadfont: Load a font file
\end_layout \end_layout
\begin_layout Itemize \begin_layout Itemize
Syntax: CUSTOMFONT gui.loadfont(string filename) Syntax: CUSTOMFONT gui.loadfont([string filename])
\end_layout \end_layout
\begin_layout Standard \begin_layout Standard
Loads font from specified file (CUSTOMFONT object). Loads font from specified file (CUSTOMFONT object).
If filename is not given, loads the system default font.
\end_layout \end_layout
\begin_layout Subsection \begin_layout Subsection

BIN
lua.pdf

Binary file not shown.

View file

@ -236,6 +236,36 @@ custom_font::custom_font(const std::string& file)
} }
} }
custom_font::custom_font(struct bitmap_font& bfont)
{
auto s = bfont.get_glyphs_set();
for(auto i = s.begin();;i++) {
const bitmap_font::glyph& j = (i != s.end()) ? bfont.get_glyph(*i) : bfont.get_bad_glyph();
font_glyph_data k;
k.width = j.wide ? 16 : 8;
k.height = 16;
k.stride = 1;
k.glyph.resize(16);
for(size_t y = 0; y < 16; y++) {
k.glyph[y] = 0;
uint32_t r = j.data[y / (j.wide ? 2 : 4)];
if(j.wide)
r >>= 16 - ((y & 1) << 4);
else
r >>= 24 - ((y & 3) << 3);
for(size_t x = 0; x < k.width; x++) {
uint32_t b = (j.wide ? 15 : 7) - x;
if(((r >> b) & 1) != 0)
k.glyph[y] |= 1UL << (31 - x);
}
}
std::u32string key = (i != s.end()) ? std::u32string(1, *i) : std::u32string();
glyphs[key] = k;
if(i == s.end()) break;
}
rowadvance = 16;
}
std::ostream& operator<<(std::ostream& os, const std::u32string& lkey) std::ostream& operator<<(std::ostream& os, const std::u32string& lkey)
{ {
if(!lkey.length()) if(!lkey.length())

View file

@ -687,6 +687,19 @@ const bitmap_font::glyph& bitmap_font::get_glyph(uint32_t glyph) throw()
return bad_glyph; return bad_glyph;
} }
std::set<uint32_t> bitmap_font::get_glyphs_set()
{
std::set<uint32_t> out;
for(auto& i : glyphs)
out.insert(i.first);
return out;
}
const bitmap_font::glyph& bitmap_font::get_bad_glyph() throw()
{
return bad_glyph;
}
std::pair<size_t, size_t> bitmap_font::get_metrics(const std::string& string) throw() std::pair<size_t, size_t> bitmap_font::get_metrics(const std::string& string) throw()
{ {
size_t commit_width = 0; size_t commit_width = 0;

View file

@ -13,14 +13,17 @@ namespace
{ {
public: public:
lua_customfont(lua_state& L, const std::string& filename); lua_customfont(lua_state& L, const std::string& filename);
lua_customfont(lua_state& L);
~lua_customfont() throw(); ~lua_customfont() throw();
int draw(lua_state& L, const std::string& fname); int draw(lua_state& L, const std::string& fname);
const custom_font& get_font() { return font; } const custom_font& get_font() { return font; }
std::string print() std::string print()
{ {
return ""; return orig_filename;
} }
private: private:
void init(lua_state& L);
std::string orig_filename;
custom_font font; custom_font font;
}; };
} }
@ -89,14 +92,27 @@ namespace
lua_obj_pin<lua_customfont> font; lua_obj_pin<lua_customfont> font;
}; };
lua_customfont::lua_customfont(lua_state& L, const std::string& filename) void lua_customfont::init(lua_state& L)
: font(filename)
{ {
objclass<lua_customfont>().bind_multi(L, { objclass<lua_customfont>().bind_multi(L, {
{"__call", &lua_customfont::draw}, {"__call", &lua_customfont::draw},
}); });
} }
lua_customfont::lua_customfont(lua_state& L, const std::string& filename)
: font(filename)
{
orig_filename = filename;
init(L);
}
lua_customfont::lua_customfont(lua_state& L)
: font(main_font)
{
orig_filename = "<builtin>";
init(L);
}
lua_customfont::~lua_customfont() throw() lua_customfont::~lua_customfont() throw()
{ {
render_kill_request(this); render_kill_request(this);
@ -125,6 +141,10 @@ namespace
function_ptr_luafun gui_text_cf(lua_func_misc, "gui.loadfont", [](lua_state& L, const std::string& fname) function_ptr_luafun gui_text_cf(lua_func_misc, "gui.loadfont", [](lua_state& L, const std::string& fname)
-> int { -> int {
if(L.type(1) == LUA_TNONE || L.type(1) == LUA_TNIL) {
lua_class<lua_customfont>::create(L);
return 1;
}
std::string filename = L.get_string(1, fname.c_str()); std::string filename = L.get_string(1, fname.c_str());
lua_class<lua_customfont>::create(L, filename); lua_class<lua_customfont>::create(L, filename);
return 1; return 1;