move Font to BitmapFont

so we can add DynamicFont later..
This commit is contained in:
Juan Linietsky 2016-04-28 22:21:18 -03:00
parent ea0231a56c
commit 0fa588a830
8 changed files with 221 additions and 174 deletions

View File

@ -570,13 +570,17 @@ void register_scene_types() {
ObjectTypeDB::register_type<LargeTexture>(); ObjectTypeDB::register_type<LargeTexture>();
ObjectTypeDB::register_type<CubeMap>(); ObjectTypeDB::register_type<CubeMap>();
ObjectTypeDB::register_type<Animation>(); ObjectTypeDB::register_type<Animation>();
ObjectTypeDB::register_type<Font>(); ObjectTypeDB::register_virtual_type<Font>();
ObjectTypeDB::register_type<BitmapFont>();
ObjectTypeDB::register_type<StyleBoxEmpty>(); ObjectTypeDB::register_type<StyleBoxEmpty>();
ObjectTypeDB::register_type<StyleBoxTexture>(); ObjectTypeDB::register_type<StyleBoxTexture>();
ObjectTypeDB::register_type<StyleBoxFlat>(); ObjectTypeDB::register_type<StyleBoxFlat>();
ObjectTypeDB::register_type<StyleBoxImageMask>(); ObjectTypeDB::register_type<StyleBoxImageMask>();
ObjectTypeDB::register_type<Theme>(); ObjectTypeDB::register_type<Theme>();
ObjectTypeDB::add_compatibility_type("Font","BitmapFont");
ObjectTypeDB::register_type<PolygonPathFinder>(); ObjectTypeDB::register_type<PolygonPathFinder>();
ObjectTypeDB::register_type<BitMap>(); ObjectTypeDB::register_type<BitMap>();
ObjectTypeDB::register_type<ColorRamp>(); ObjectTypeDB::register_type<ColorRamp>();

View File

@ -87,10 +87,10 @@ static Ref<Shader> make_shader(const char*vertex_code,const char*fragment_code,c
return shader; return shader;
} }
static Ref<Font> make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref<Texture> &p_texture) { static Ref<BitmapFont> make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref<Texture> &p_texture) {
Ref<Font> font( memnew( Font ) ); Ref<BitmapFont> font( memnew( BitmapFont ) );
font->add_texture( p_texture ); font->add_texture( p_texture );
for (int i=0;i<p_charcount;i++) { for (int i=0;i<p_charcount;i++) {
@ -117,10 +117,10 @@ static Ref<Font> make_font(int p_height,int p_ascent, int p_valign, int p_charco
return font; return font;
} }
static Ref<Font> make_font2(int p_height,int p_ascent, int p_charcount, const int *p_char_rects,int p_kerning_count,const int *p_kernings,int p_w, int p_h, const unsigned char *p_img) { static Ref<BitmapFont> make_font2(int p_height,int p_ascent, int p_charcount, const int *p_char_rects,int p_kerning_count,const int *p_kernings,int p_w, int p_h, const unsigned char *p_img) {
Ref<Font> font( memnew( Font ) ); Ref<BitmapFont> font( memnew( BitmapFont ) );
DVector<uint8_t> img; DVector<uint8_t> img;
img.resize(p_w*p_h*2); img.resize(p_w*p_h*2);
@ -187,10 +187,10 @@ void make_default_theme() {
Ref<Theme> t( memnew( Theme ) ); Ref<Theme> t( memnew( Theme ) );
//Ref<Font> default_font = make_font(_bi_font_normal_height,_bi_font_normal_ascent,_bi_font_normal_valign,_bi_font_normal_charcount,_bi_font_normal_characters,make_icon(font_normal_png)); //Ref<BitmapFont> default_font = make_font(_bi_font_normal_height,_bi_font_normal_ascent,_bi_font_normal_valign,_bi_font_normal_charcount,_bi_font_normal_characters,make_icon(font_normal_png));
Ref<Font> default_font=make_font2(_builtin_normal_font_height,_builtin_normal_font_ascent,_builtin_normal_font_charcount,&_builtin_normal_font_charrects[0][0],_builtin_normal_font_kerning_pair_count,&_builtin_normal_font_kerning_pairs[0][0],_builtin_normal_font_img_width,_builtin_normal_font_img_height,_builtin_normal_font_img_data); Ref<BitmapFont> default_font=make_font2(_builtin_normal_font_height,_builtin_normal_font_ascent,_builtin_normal_font_charcount,&_builtin_normal_font_charrects[0][0],_builtin_normal_font_kerning_pair_count,&_builtin_normal_font_kerning_pairs[0][0],_builtin_normal_font_img_width,_builtin_normal_font_img_height,_builtin_normal_font_img_data);
Ref<Font> source_font=make_font2(_builtin_source_font_height,_builtin_source_font_ascent,_builtin_source_font_charcount,&_builtin_source_font_charrects[0][0],_builtin_source_font_kerning_pair_count,&_builtin_source_font_kerning_pairs[0][0],_builtin_source_font_img_width,_builtin_source_font_img_height,_builtin_source_font_img_data); Ref<BitmapFont> source_font=make_font2(_builtin_source_font_height,_builtin_source_font_ascent,_builtin_source_font_charcount,&_builtin_source_font_charrects[0][0],_builtin_source_font_kerning_pair_count,&_builtin_source_font_kerning_pairs[0][0],_builtin_source_font_img_width,_builtin_source_font_img_height,_builtin_source_font_img_data);
Ref<Font> large_font=make_font2(_builtin_large_font_height,_builtin_large_font_ascent,_builtin_large_font_charcount,&_builtin_large_font_charrects[0][0],_builtin_large_font_kerning_pair_count,&_builtin_large_font_kerning_pairs[0][0],_builtin_large_font_img_width,_builtin_large_font_img_height,_builtin_large_font_img_data); Ref<BitmapFont> large_font=make_font2(_builtin_large_font_height,_builtin_large_font_ascent,_builtin_large_font_charcount,&_builtin_large_font_charrects[0][0],_builtin_large_font_kerning_pair_count,&_builtin_large_font_kerning_pairs[0][0],_builtin_large_font_img_width,_builtin_large_font_img_height,_builtin_large_font_img_data);
// Font Colors // Font Colors
@ -939,7 +939,7 @@ void make_default_theme() {
style->set_default_margin( Margin(),8); style->set_default_margin( Margin(),8);
} }
Ref<Font> f = make_default_font(); Ref<BitmapFont> f = make_default_font();
Theme::set_default( t ); Theme::set_default( t );
Theme::set_default_icon( texture ); Theme::set_default_icon( texture );
Theme::set_default_style( style ); Theme::set_default_style( style );

View File

@ -31,7 +31,67 @@
#include "core/os/file_access.h" #include "core/os/file_access.h"
#include "core/io/resource_loader.h" #include "core/io/resource_loader.h"
void Font::_set_chars(const DVector<int>& p_chars) {
void Font::draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,float p_width,const String& p_text,const Color& p_modulate) const {
float length=get_string_size(p_text).width;
if (length>=p_width) {
draw(p_canvas_item,p_pos,p_text,p_modulate,p_width);
return;
}
float ofs;
switch(p_align) {
case HALIGN_LEFT: {
ofs=0;
} break;
case HALIGN_CENTER: {
ofs = Math::floor( (p_width-length) / 2.0 );
} break;
case HALIGN_RIGHT: {
ofs=p_width-length;
} break;
}
draw(p_canvas_item,p_pos+Point2(ofs,0),p_text,p_modulate,p_width);
}
void Font::draw(RID p_canvas_item, const Point2& p_pos, const String& p_text, const Color& p_modulate,int p_clip_w) const {
Vector2 ofs;
for (int i=0;i<p_text.length();i++) {
int width = get_char_size(p_text[i]).width;
if (p_clip_w>=0 && (ofs.x+width)>p_clip_w)
break; //clip
ofs.x+=draw_char(p_canvas_item,p_pos+ofs,p_text[i],p_text[i+1],p_modulate);
}
}
void Font::_bind_methods() {
ObjectTypeDB::bind_method(_MD("draw","canvas_item","pos","string","modulate","clip_w"),&Font::draw,DEFVAL(Color(1,1,1)),DEFVAL(-1));
ObjectTypeDB::bind_method(_MD("get_ascent"),&Font::get_ascent);
ObjectTypeDB::bind_method(_MD("get_descent"),&Font::get_descent);
ObjectTypeDB::bind_method(_MD("get_height"),&Font::get_height);
ObjectTypeDB::bind_method(_MD("is_distance_field_hint"),&Font::is_distance_field_hint);
ObjectTypeDB::bind_method(_MD("get_string_size","string"),&Font::get_string_size);
ObjectTypeDB::bind_method(_MD("draw_char","canvas_item","pos","char","next","modulate"),&Font::draw_char,DEFVAL(-1),DEFVAL(Color(1,1,1)));
}
Font::Font() {
}
/////////////////////////////////////////////////////////////////
void BitmapFont::_set_chars(const DVector<int>& p_chars) {
int len = p_chars.size(); int len = p_chars.size();
//char 1 charsize 1 texture, 4 rect, 2 align, advance 1 //char 1 charsize 1 texture, 4 rect, 2 align, advance 1
@ -50,7 +110,7 @@ void Font::_set_chars(const DVector<int>& p_chars) {
} }
DVector<int> Font::_get_chars() const { DVector<int> BitmapFont::_get_chars() const {
DVector<int> chars; DVector<int> chars;
@ -74,7 +134,7 @@ DVector<int> Font::_get_chars() const {
return chars; return chars;
} }
void Font::_set_kernings(const DVector<int>& p_kernings) { void BitmapFont::_set_kernings(const DVector<int>& p_kernings) {
int len=p_kernings.size(); int len=p_kernings.size();
ERR_FAIL_COND(len%3); ERR_FAIL_COND(len%3);
@ -89,7 +149,7 @@ void Font::_set_kernings(const DVector<int>& p_kernings) {
} }
} }
DVector<int> Font::_get_kernings() const { DVector<int> BitmapFont::_get_kernings() const {
DVector<int> kernings; DVector<int> kernings;
@ -104,7 +164,7 @@ DVector<int> Font::_get_kernings() const {
} }
void Font::_set_textures(const Vector<Variant> & p_textures) { void BitmapFont::_set_textures(const Vector<Variant> & p_textures) {
for(int i=0;i<p_textures.size();i++) { for(int i=0;i<p_textures.size();i++) {
Ref<Texture> tex = p_textures[i]; Ref<Texture> tex = p_textures[i];
@ -114,7 +174,7 @@ void Font::_set_textures(const Vector<Variant> & p_textures) {
} }
Vector<Variant> Font::_get_textures() const { Vector<Variant> BitmapFont::_get_textures() const {
Vector<Variant> rtextures; Vector<Variant> rtextures;
for(int i=0;i<textures.size();i++) for(int i=0;i<textures.size();i++)
@ -122,7 +182,7 @@ Vector<Variant> Font::_get_textures() const {
return rtextures; return rtextures;
} }
Error Font::create_from_fnt(const String& p_string) { Error BitmapFont::create_from_fnt(const String& p_string) {
//fnt format used by angelcode bmfont //fnt format used by angelcode bmfont
//http://www.angelcode.com/products/bmfont/ //http://www.angelcode.com/products/bmfont/
@ -271,51 +331,51 @@ Error Font::create_from_fnt(const String& p_string) {
void Font::set_height(float p_height) { void BitmapFont::set_height(float p_height) {
height=p_height; height=p_height;
} }
float Font::get_height() const{ float BitmapFont::get_height() const{
return height; return height;
} }
void Font::set_ascent(float p_ascent){ void BitmapFont::set_ascent(float p_ascent){
ascent=p_ascent; ascent=p_ascent;
} }
float Font::get_ascent() const { float BitmapFont::get_ascent() const {
return ascent; return ascent;
} }
float Font::get_descent() const { float BitmapFont::get_descent() const {
return height-ascent; return height-ascent;
} }
void Font::add_texture(const Ref<Texture>& p_texture) { void BitmapFont::add_texture(const Ref<Texture>& p_texture) {
ERR_FAIL_COND( p_texture.is_null()); ERR_FAIL_COND( p_texture.is_null());
textures.push_back( p_texture ); textures.push_back( p_texture );
} }
int Font::get_texture_count() const { int BitmapFont::get_texture_count() const {
return textures.size(); return textures.size();
}; };
Ref<Texture> Font::get_texture(int p_idx) const { Ref<Texture> BitmapFont::get_texture(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>()); ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
return textures[p_idx]; return textures[p_idx];
}; };
int Font::get_character_count() const { int BitmapFont::get_character_count() const {
return char_map.size(); return char_map.size();
}; };
Vector<CharType> Font::get_char_keys() const { Vector<CharType> BitmapFont::get_char_keys() const {
Vector<CharType> chars; Vector<CharType> chars;
chars.resize(char_map.size()); chars.resize(char_map.size());
@ -329,7 +389,7 @@ Vector<CharType> Font::get_char_keys() const {
return chars; return chars;
}; };
Font::Character Font::get_character(CharType p_char) const { BitmapFont::Character BitmapFont::get_character(CharType p_char) const {
if (!char_map.has(p_char)) { if (!char_map.has(p_char)) {
ERR_FAIL_V(Character()); ERR_FAIL_V(Character());
@ -338,7 +398,7 @@ Font::Character Font::get_character(CharType p_char) const {
return char_map[p_char]; return char_map[p_char];
}; };
void Font::add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, const Size2& p_align, float p_advance) { void BitmapFont::add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, const Size2& p_align, float p_advance) {
if (p_advance<0) if (p_advance<0)
p_advance=p_rect.size.width; p_advance=p_rect.size.width;
@ -353,7 +413,7 @@ void Font::add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, con
char_map[p_char]=c; char_map[p_char]=c;
} }
void Font::add_kerning_pair(CharType p_A,CharType p_B,int p_kerning) { void BitmapFont::add_kerning_pair(CharType p_A,CharType p_B,int p_kerning) {
KerningPairKey kpk; KerningPairKey kpk;
@ -369,10 +429,10 @@ void Font::add_kerning_pair(CharType p_A,CharType p_B,int p_kerning) {
} }
} }
Vector<Font::KerningPairKey> Font::get_kerning_pair_keys() const { Vector<BitmapFont::KerningPairKey> BitmapFont::get_kerning_pair_keys() const {
Vector<Font::KerningPairKey> ret; Vector<BitmapFont::KerningPairKey> ret;
ret.resize(kerning_map.size()); ret.resize(kerning_map.size());
int i=0; int i=0;
@ -385,7 +445,7 @@ Vector<Font::KerningPairKey> Font::get_kerning_pair_keys() const {
} }
int Font::get_kerning_pair(CharType p_A,CharType p_B) const { int BitmapFont::get_kerning_pair(CharType p_A,CharType p_B) const {
KerningPairKey kpk; KerningPairKey kpk;
kpk.A=p_A; kpk.A=p_A;
@ -398,19 +458,19 @@ int Font::get_kerning_pair(CharType p_A,CharType p_B) const {
return 0; return 0;
} }
void Font::set_distance_field_hint(bool p_distance_field) { void BitmapFont::set_distance_field_hint(bool p_distance_field) {
distance_field_hint=p_distance_field; distance_field_hint=p_distance_field;
emit_changed(); emit_changed();
} }
bool Font::is_distance_field_hint() const{ bool BitmapFont::is_distance_field_hint() const{
return distance_field_hint; return distance_field_hint;
} }
void Font::clear() { void BitmapFont::clear() {
height=1; height=1;
ascent=0; ascent=0;
@ -426,7 +486,7 @@ Size2 Font::get_string_size(const String& p_string) const {
int l = p_string.length(); int l = p_string.length();
if (l==0) if (l==0)
return Size2(0,height); return Size2(0,get_height());
const CharType *sptr = &p_string[0]; const CharType *sptr = &p_string[0];
for (int i=0;i<l;i++) { for (int i=0;i<l;i++) {
@ -434,48 +494,19 @@ Size2 Font::get_string_size(const String& p_string) const {
w+=get_char_size(sptr[i],sptr[i+1]).width; w+=get_char_size(sptr[i],sptr[i+1]).width;
} }
return Size2(w,height); return Size2(w,get_height());
}
void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
fallback=p_fallback;
} }
void Font::draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,float p_width,const String& p_text,const Color& p_modulate) const { Ref<BitmapFont> BitmapFont::get_fallback() const{
float length=get_string_size(p_text).width; return fallback;
if (length>=p_width) {
draw(p_canvas_item,p_pos,p_text,p_modulate,p_width);
return;
} }
float ofs; float BitmapFont::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const {
switch(p_align) {
case HALIGN_LEFT: {
ofs=0;
} break;
case HALIGN_CENTER: {
ofs = Math::floor( (p_width-length) / 2.0 );
} break;
case HALIGN_RIGHT: {
ofs=p_width-length;
} break;
}
draw(p_canvas_item,p_pos+Point2(ofs,0),p_text,p_modulate,p_width);
}
void Font::draw(RID p_canvas_item, const Point2& p_pos, const String& p_text, const Color& p_modulate,int p_clip_w) const {
Vector2 ofs;
for (int i=0;i<p_text.length();i++) {
int width = get_char_size(p_text[i]).width;
if (p_clip_w>=0 && (ofs.x+width)>p_clip_w)
break; //clip
ofs.x+=draw_char(p_canvas_item,p_pos+ofs,p_text[i],p_text[i+1],p_modulate);
}
}
float Font::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const {
const Character * c = char_map.getptr(p_char); const Character * c = char_map.getptr(p_char);
@ -496,58 +527,70 @@ float Font::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_
return get_char_size(p_char,p_next).width; return get_char_size(p_char,p_next).width;
} }
void Font::set_fallback(const Ref<Font> &p_fallback) {
fallback=p_fallback; Size2 BitmapFont::get_char_size(CharType p_char,CharType p_next) const {
const Character * c = char_map.getptr(p_char);
if (!c) {
if (fallback.is_valid())
return fallback->get_char_size(p_char,p_next);
return Size2();
} }
Ref<Font> Font::get_fallback() const{ Size2 ret(c->advance,c->rect.size.y);
return fallback; if (p_next) {
KerningPairKey kpk;
kpk.A=p_char;
kpk.B=p_next;
const Map<KerningPairKey,int>::Element *E=kerning_map.find(kpk);
if (E) {
ret.width-=E->get();
}
} }
void Font::_bind_methods() { return ret;
}
ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&Font::create_from_fnt); void BitmapFont::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_height","px"),&Font::set_height);
ObjectTypeDB::bind_method(_MD("get_height"),&Font::get_height);
ObjectTypeDB::bind_method(_MD("set_ascent","px"),&Font::set_ascent); ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&BitmapFont::create_from_fnt);
ObjectTypeDB::bind_method(_MD("get_ascent"),&Font::get_ascent); ObjectTypeDB::bind_method(_MD("set_height","px"),&BitmapFont::set_height);
ObjectTypeDB::bind_method(_MD("get_descent"),&Font::get_descent);
ObjectTypeDB::bind_method(_MD("add_kerning_pair","char_a","char_b","kerning"),&Font::add_kerning_pair); ObjectTypeDB::bind_method(_MD("set_ascent","px"),&BitmapFont::set_ascent);
ObjectTypeDB::bind_method(_MD("get_kerning_pair","char_a","char_b"),&Font::get_kerning_pair);
ObjectTypeDB::bind_method(_MD("add_texture","texture:Texture"),&Font::add_texture); ObjectTypeDB::bind_method(_MD("add_kerning_pair","char_a","char_b","kerning"),&BitmapFont::add_kerning_pair);
ObjectTypeDB::bind_method(_MD("add_char","character","texture","rect","align","advance"),&Font::add_char,DEFVAL(Point2()),DEFVAL(-1)); ObjectTypeDB::bind_method(_MD("get_kerning_pair","char_a","char_b"),&BitmapFont::get_kerning_pair);
ObjectTypeDB::bind_method(_MD("add_texture","texture:Texture"),&BitmapFont::add_texture);
ObjectTypeDB::bind_method(_MD("add_char","character","texture","rect","align","advance"),&BitmapFont::add_char,DEFVAL(Point2()),DEFVAL(-1));
ObjectTypeDB::bind_method(_MD("get_texture_count"),&Font::get_texture_count); ObjectTypeDB::bind_method(_MD("get_texture_count"),&BitmapFont::get_texture_count);
ObjectTypeDB::bind_method(_MD("get_texture:Texture","idx"),&Font::get_texture); ObjectTypeDB::bind_method(_MD("get_texture:Texture","idx"),&BitmapFont::get_texture);
ObjectTypeDB::bind_method(_MD("get_char_size","char","next"),&Font::get_char_size,DEFVAL(0)); ObjectTypeDB::bind_method(_MD("get_char_size","char","next"),&BitmapFont::get_char_size,DEFVAL(0));
ObjectTypeDB::bind_method(_MD("get_string_size","string"),&Font::get_string_size);
ObjectTypeDB::bind_method(_MD("set_distance_field_hint","enable"),&Font::set_distance_field_hint); ObjectTypeDB::bind_method(_MD("set_distance_field_hint","enable"),&BitmapFont::set_distance_field_hint);
ObjectTypeDB::bind_method(_MD("is_distance_field_hint"),&Font::is_distance_field_hint);
ObjectTypeDB::bind_method(_MD("clear"),&Font::clear); ObjectTypeDB::bind_method(_MD("clear"),&BitmapFont::clear);
ObjectTypeDB::bind_method(_MD("draw","canvas_item","pos","string","modulate","clip_w"),&Font::draw,DEFVAL(Color(1,1,1)),DEFVAL(-1));
ObjectTypeDB::bind_method(_MD("draw_char","canvas_item","pos","char","next","modulate"),&Font::draw_char,DEFVAL(-1),DEFVAL(Color(1,1,1)));
ObjectTypeDB::bind_method(_MD("_set_chars"),&Font::_set_chars); ObjectTypeDB::bind_method(_MD("_set_chars"),&BitmapFont::_set_chars);
ObjectTypeDB::bind_method(_MD("_get_chars"),&Font::_get_chars); ObjectTypeDB::bind_method(_MD("_get_chars"),&BitmapFont::_get_chars);
ObjectTypeDB::bind_method(_MD("_set_kernings"),&Font::_set_kernings); ObjectTypeDB::bind_method(_MD("_set_kernings"),&BitmapFont::_set_kernings);
ObjectTypeDB::bind_method(_MD("_get_kernings"),&Font::_get_kernings); ObjectTypeDB::bind_method(_MD("_get_kernings"),&BitmapFont::_get_kernings);
ObjectTypeDB::bind_method(_MD("_set_textures"),&Font::_set_textures); ObjectTypeDB::bind_method(_MD("_set_textures"),&BitmapFont::_set_textures);
ObjectTypeDB::bind_method(_MD("_get_textures"),&Font::_get_textures); ObjectTypeDB::bind_method(_MD("_get_textures"),&BitmapFont::_get_textures);
ObjectTypeDB::bind_method(_MD("set_fallback","fallback"),&Font::set_fallback); ObjectTypeDB::bind_method(_MD("set_fallback","fallback"),&BitmapFont::set_fallback);
ObjectTypeDB::bind_method(_MD("get_fallback"),&Font::get_fallback); ObjectTypeDB::bind_method(_MD("get_fallback"),&BitmapFont::get_fallback);
ADD_PROPERTY( PropertyInfo( Variant::ARRAY, "textures", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_textures"), _SCS("_get_textures") ); ADD_PROPERTY( PropertyInfo( Variant::ARRAY, "textures", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_textures"), _SCS("_get_textures") );
ADD_PROPERTY( PropertyInfo( Variant::INT_ARRAY, "chars", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_chars"), _SCS("_get_chars") ); ADD_PROPERTY( PropertyInfo( Variant::INT_ARRAY, "chars", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_chars"), _SCS("_get_chars") );
@ -556,11 +599,11 @@ void Font::_bind_methods() {
ADD_PROPERTY( PropertyInfo( Variant::REAL, "height", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_height"), _SCS("get_height") ); ADD_PROPERTY( PropertyInfo( Variant::REAL, "height", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_height"), _SCS("get_height") );
ADD_PROPERTY( PropertyInfo( Variant::REAL, "ascent", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_ascent"), _SCS("get_ascent") ); ADD_PROPERTY( PropertyInfo( Variant::REAL, "ascent", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_ascent"), _SCS("get_ascent") );
ADD_PROPERTY( PropertyInfo( Variant::BOOL, "distance_field" ), _SCS("set_distance_field_hint"), _SCS("is_distance_field_hint") ); ADD_PROPERTY( PropertyInfo( Variant::BOOL, "distance_field" ), _SCS("set_distance_field_hint"), _SCS("is_distance_field_hint") );
ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "fallback", PROPERTY_HINT_RESOURCE_TYPE,"Font" ), _SCS("set_fallback"), _SCS("get_fallback") ); ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "fallback", PROPERTY_HINT_RESOURCE_TYPE,"BitmapFont" ), _SCS("set_fallback"), _SCS("get_fallback") );
} }
Font::Font() { BitmapFont::BitmapFont() {
clear(); clear();
@ -569,7 +612,7 @@ Font::Font() {
} }
Font::~Font() { BitmapFont::~BitmapFont() {
clear(); clear();
} }

View File

@ -35,9 +35,40 @@
/** /**
@author Juan Linietsky <reduzio@gmail.com> @author Juan Linietsky <reduzio@gmail.com>
*/ */
class Font : public Resource { class Font : public Resource {
OBJ_TYPE( Font, Resource ); OBJ_TYPE( Font, Resource );
protected:
static void _bind_methods();
public:
virtual float get_height() const=0;
virtual float get_ascent() const=0;
virtual float get_descent() const=0;
virtual Size2 get_char_size(CharType p_char,CharType p_next=0) const=0;
Size2 get_string_size(const String& p_string) const;
virtual bool is_distance_field_hint() const=0;
void draw(RID p_canvas_item, const Point2& p_pos, const String& p_text,const Color& p_modulate=Color(1,1,1),int p_clip_w=-1) const;
void draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,float p_width,const String& p_text,const Color& p_modulate=Color(1,1,1)) const;
virtual float draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next=0,const Color& p_modulate=Color(1,1,1)) const=0;
Font();
};
class BitmapFont : public Font {
OBJ_TYPE( BitmapFont, Font );
RES_BASE_EXTENSION("fnt"); RES_BASE_EXTENSION("fnt");
Vector< Ref<Texture> > textures; Vector< Ref<Texture> > textures;
@ -84,7 +115,7 @@ private:
void _set_textures(const Vector<Variant> & p_textures); void _set_textures(const Vector<Variant> & p_textures);
Vector<Variant> _get_textures() const; Vector<Variant> _get_textures() const;
Ref<Font> fallback; Ref<BitmapFont> fallback;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -114,54 +145,23 @@ public:
int get_kerning_pair(CharType p_A,CharType p_B) const; int get_kerning_pair(CharType p_A,CharType p_B) const;
Vector<KerningPairKey> get_kerning_pair_keys() const; Vector<KerningPairKey> get_kerning_pair_keys() const;
inline Size2 get_char_size(CharType p_char,CharType p_next=0) const; Size2 get_char_size(CharType p_char,CharType p_next=0) const;
Size2 get_string_size(const String& p_string) const;
void set_fallback(const Ref<BitmapFont> &p_fallback);
void set_fallback(const Ref<Font> &p_fallback); Ref<BitmapFont> get_fallback() const;
Ref<Font> get_fallback() const;
void clear(); void clear();
void set_distance_field_hint(bool p_distance_field); void set_distance_field_hint(bool p_distance_field);
bool is_distance_field_hint() const; bool is_distance_field_hint() const;
void draw(RID p_canvas_item, const Point2& p_pos, const String& p_text,const Color& p_modulate=Color(1,1,1),int p_clip_w=-1) const;
void draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,float p_width,const String& p_text,const Color& p_modulate=Color(1,1,1)) const;
float draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next=0,const Color& p_modulate=Color(1,1,1)) const; float draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next=0,const Color& p_modulate=Color(1,1,1)) const;
Font(); BitmapFont();
~Font(); ~BitmapFont();
}; };
Size2 Font::get_char_size(CharType p_char,CharType p_next) const {
const Character * c = char_map.getptr(p_char);
if (!c) {
if (fallback.is_valid())
return fallback->get_char_size(p_char,p_next);
return Size2();
}
Size2 ret(c->advance,c->rect.size.y);
if (p_next) {
KerningPairKey kpk;
kpk.A=p_char;
kpk.B=p_next;
const Map<KerningPairKey,int>::Element *E=kerning_map.find(kpk);
if (E) {
ret.width-=E->get();
}
}
return ret;
}

View File

@ -31,10 +31,10 @@
#include "doc_title_font.h" #include "doc_title_font.h"
#include "doc_code_font.h" #include "doc_code_font.h"
static Ref<Font> make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref<Texture> &p_texture) { static Ref<BitmapFont> make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref<Texture> &p_texture) {
Ref<Font> font( memnew( Font ) ); Ref<BitmapFont> font( memnew( BitmapFont ) );
font->add_texture( p_texture ); font->add_texture( p_texture );
for (int i=0;i<p_charcount;i++) { for (int i=0;i<p_charcount;i++) {
@ -65,9 +65,9 @@ static Ref<Font> make_font(int p_height,int p_ascent, int p_valign, int p_charco
void editor_register_fonts(Ref<Theme> p_theme) { void editor_register_fonts(Ref<Theme> p_theme) {
Ref<Font> doc_font = make_font(_bi_font_doc_font_height,_bi_font_doc_font_ascent,0,_bi_font_doc_font_charcount,_bi_font_doc_font_characters,p_theme->get_icon("DocFont","EditorIcons")); Ref<BitmapFont> doc_font = make_font(_bi_font_doc_font_height,_bi_font_doc_font_ascent,0,_bi_font_doc_font_charcount,_bi_font_doc_font_characters,p_theme->get_icon("DocFont","EditorIcons"));
Ref<Font> doc_code_font = make_font(_bi_font_doc_code_font_height,_bi_font_doc_code_font_ascent,0,_bi_font_doc_code_font_charcount,_bi_font_doc_code_font_characters,p_theme->get_icon("DocCodeFont","EditorIcons")); Ref<BitmapFont> doc_code_font = make_font(_bi_font_doc_code_font_height,_bi_font_doc_code_font_ascent,0,_bi_font_doc_code_font_charcount,_bi_font_doc_code_font_characters,p_theme->get_icon("DocCodeFont","EditorIcons"));
Ref<Font> doc_title_font = make_font(_bi_font_doc_title_font_height,_bi_font_doc_title_font_ascent,0,_bi_font_doc_title_font_charcount,_bi_font_doc_title_font_characters,p_theme->get_icon("DocTitleFont","EditorIcons")); Ref<BitmapFont> doc_title_font = make_font(_bi_font_doc_title_font_height,_bi_font_doc_title_font_ascent,0,_bi_font_doc_title_font_charcount,_bi_font_doc_title_font_characters,p_theme->get_icon("DocTitleFont","EditorIcons"));
p_theme->set_font("doc","EditorFonts",doc_font); p_theme->set_font("doc","EditorFonts",doc_font);
p_theme->set_font("doc_code","EditorFonts",doc_code_font); p_theme->set_font("doc_code","EditorFonts",doc_code_font);
p_theme->set_font("doc_title","EditorFonts",doc_title_font); p_theme->set_font("doc_title","EditorFonts",doc_title_font);

Binary file not shown.

After

Width:  |  Height:  |  Size: 362 B

View File

@ -437,7 +437,7 @@ class EditorFontImportDialog : public ConfirmationDialog {
void _update() { void _update() {
Ref<ResourceImportMetadata> imd = get_rimd(); Ref<ResourceImportMetadata> imd = get_rimd();
Ref<Font> font = plugin->generate_font(imd); Ref<BitmapFont> font = plugin->generate_font(imd);
test_label->add_font_override("font",font); test_label->add_font_override("font",font);
_update_text(); _update_text();
} }
@ -454,7 +454,7 @@ class EditorFontImportDialog : public ConfirmationDialog {
void _import_inc(String p_font) { void _import_inc(String p_font) {
Ref<Font> font = ResourceLoader::load(p_font); Ref<BitmapFont> font = ResourceLoader::load(p_font);
if (!font.is_valid()) if (!font.is_valid())
return; return;
Ref<ImageTexture> tex = font->get_texture(0); Ref<ImageTexture> tex = font->get_texture(0);
@ -471,12 +471,12 @@ class EditorFontImportDialog : public ConfirmationDialog {
for(int i=0;i<ck.size();i++) { for(int i=0;i<ck.size();i++) {
CharType k=ck[i]; CharType k=ck[i];
Font::Character c=font->get_character(k); BitmapFont::Character c=font->get_character(k);
f->store_line("{"+itos(k)+","+rtos(c.rect.pos.x)+","+rtos(c.rect.pos.y)+","+rtos(c.rect.size.x)+","+rtos(c.rect.size.y)+","+rtos(c.v_align)+","+rtos(c.h_align)+","+rtos(c.advance)+"},"); f->store_line("{"+itos(k)+","+rtos(c.rect.pos.x)+","+rtos(c.rect.pos.y)+","+rtos(c.rect.size.x)+","+rtos(c.rect.size.y)+","+rtos(c.v_align)+","+rtos(c.h_align)+","+rtos(c.advance)+"},");
} }
f->store_line("};"); f->store_line("};");
Vector<Font::KerningPairKey> kp=font->get_kerning_pair_keys(); Vector<BitmapFont::KerningPairKey> kp=font->get_kerning_pair_keys();
f->store_line("static const int _builtin_font_kerning_pair_count="+itos(kp.size())+";"); f->store_line("static const int _builtin_font_kerning_pair_count="+itos(kp.size())+";");
f->store_line("static const int _builtin_font_kerning_pairs["+itos(kp.size())+"][3]={"); f->store_line("static const int _builtin_font_kerning_pairs["+itos(kp.size())+"][3]={");
for(int i=0;i<kp.size();i++) { for(int i=0;i<kp.size();i++) {
@ -634,7 +634,7 @@ public:
dest = memnew( EditorLineEditFileChooser ); dest = memnew( EditorLineEditFileChooser );
// //
List<String> fl; List<String> fl;
Ref<Font> font= memnew(Font); Ref<BitmapFont> font= memnew(BitmapFont);
dest->get_file_dialog()->add_filter("*.fnt ; Font" ); dest->get_file_dialog()->add_filter("*.fnt ; Font" );
//ResourceSaver::get_recognized_extensions(font,&fl); //ResourceSaver::get_recognized_extensions(font,&fl);
//for(List<String>::Element *E=fl.front();E;E=E->next()) { //for(List<String>::Element *E=fl.front();E;E=E->next()) {
@ -875,12 +875,12 @@ static unsigned char get_SDF_radial(
} }
Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata>& p_from, const String &p_existing) { Ref<BitmapFont> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata>& p_from, const String &p_existing) {
Ref<ResourceImportMetadata> from = p_from; Ref<ResourceImportMetadata> from = p_from;
ERR_FAIL_COND_V(from->get_source_count()!=1,Ref<Font>()); ERR_FAIL_COND_V(from->get_source_count()!=1,Ref<BitmapFont>());
String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0)); String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0));
@ -888,15 +888,15 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
if (ResourceLoader::load(src_path).is_valid()) { if (ResourceLoader::load(src_path).is_valid()) {
EditorNode::get_singleton()->show_warning("Path: "+src_path+"\nIs a Godot font file, please supply a BMFont type file instead."); EditorNode::get_singleton()->show_warning("Path: "+src_path+"\nIs a Godot font file, please supply a BMFont type file instead.");
return Ref<Font>(); return Ref<BitmapFont>();
} }
Ref<Font> font; Ref<BitmapFont> font;
font.instance(); font.instance();
Error err = font->create_from_fnt(src_path); Error err = font->create_from_fnt(src_path);
if (err) { if (err) {
EditorNode::get_singleton()->show_warning("Path: "+src_path+"\nFailed opening as BMFont file."); EditorNode::get_singleton()->show_warning("Path: "+src_path+"\nFailed opening as BMFont file.");
return Ref<Font>(); return Ref<BitmapFont>();
} }
return font; return font;
@ -913,7 +913,7 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
int error = FT_Init_FreeType( &library ); int error = FT_Init_FreeType( &library );
ERR_EXPLAIN("Error initializing FreeType."); ERR_EXPLAIN("Error initializing FreeType.");
ERR_FAIL_COND_V( error !=0, Ref<Font>() ); ERR_FAIL_COND_V( error !=0, Ref<BitmapFont>() );
print_line("loadfrom: "+src_path); print_line("loadfrom: "+src_path);
error = FT_New_Face( library, src_path.utf8().get_data(),0,&face ); error = FT_New_Face( library, src_path.utf8().get_data(),0,&face );
@ -928,7 +928,7 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
} }
ERR_FAIL_COND_V(error,Ref<Font>()); ERR_FAIL_COND_V(error,Ref<BitmapFont>());
int height=0; int height=0;
@ -940,7 +940,7 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
if ( error ) { if ( error ) {
FT_Done_FreeType( library ); FT_Done_FreeType( library );
ERR_EXPLAIN("Invalid font size. "); ERR_EXPLAIN("Invalid font size. ");
ERR_FAIL_COND_V( error,Ref<Font>() ); ERR_FAIL_COND_V( error,Ref<BitmapFont>() );
} }
@ -987,7 +987,7 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
FT_Done_FreeType( library ); FT_Done_FreeType( library );
ERR_EXPLAIN("Invalid font custom source. "); ERR_EXPLAIN("Invalid font custom source. ");
ERR_FAIL_COND_V( !fa,Ref<Font>() ); ERR_FAIL_COND_V( !fa,Ref<BitmapFont>() );
} }
@ -1546,15 +1546,15 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
int bottom_space = from->get_option("extra_space/bottom"); int bottom_space = from->get_option("extra_space/bottom");
bool disable_filter = from->get_option("advanced/disable_filter"); bool disable_filter = from->get_option("advanced/disable_filter");
Ref<Font> font; Ref<BitmapFont> font;
if (p_existing!=String() && ResourceCache::has(p_existing)) { if (p_existing!=String() && ResourceCache::has(p_existing)) {
font = Ref<Font>( ResourceCache::get(p_existing)->cast_to<Font>()); font = Ref<BitmapFont>( ResourceCache::get(p_existing)->cast_to<BitmapFont>());
} }
if (font.is_null()) { if (font.is_null()) {
font = Ref<Font>( memnew( Font ) ); font = Ref<BitmapFont>( memnew( BitmapFont ) );
} }
font->clear(); font->clear();
@ -1596,7 +1596,7 @@ Ref<Font> EditorFontImportPlugin::generate_font(const Ref<ResourceImportMetadata
return font; return font;
#else #else
return Ref<Font>(); return Ref<BitmapFont>();
#endif #endif
} }
@ -1616,7 +1616,7 @@ void EditorFontImportPlugin::import_dialog(const String& p_from){
Error EditorFontImportPlugin::import(const String& p_path, const Ref<ResourceImportMetadata>& p_from){ Error EditorFontImportPlugin::import(const String& p_path, const Ref<ResourceImportMetadata>& p_from){
Ref<Font> font = EditorFontImportPlugin::generate_font(p_from,p_path); Ref<BitmapFont> font = EditorFontImportPlugin::generate_font(p_from,p_path);
if (!font.is_valid()) if (!font.is_valid())
return ERR_CANT_CREATE; return ERR_CANT_CREATE;

View File

@ -42,7 +42,7 @@ class EditorFontImportPlugin : public EditorImportPlugin {
EditorFontImportDialog *dialog; EditorFontImportDialog *dialog;
public: public:
Ref<Font> generate_font(const Ref<ResourceImportMetadata>& p_from,const String& p_existing=String()); //used by editor Ref<BitmapFont> generate_font(const Ref<ResourceImportMetadata>& p_from,const String& p_existing=String()); //used by editor
virtual String get_name() const; virtual String get_name() const;
virtual String get_visible_name() const; virtual String get_visible_name() const;