From 0fa588a8301b50cff5975f6c9fe8dd7969f59f16 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 28 Apr 2016 22:21:18 -0300 Subject: [PATCH] move Font to BitmapFont so we can add DynamicFont later.. --- scene/register_scene_types.cpp | 6 +- .../resources/default_theme/default_theme.cpp | 18 +- scene/resources/font.cpp | 247 ++++++++++-------- scene/resources/font.h | 74 +++--- tools/editor/editor_fonts.cpp | 10 +- tools/editor/icons/icon_bitmap_font.png | Bin 0 -> 362 bytes .../io_plugins/editor_font_import_plugin.cpp | 38 +-- .../io_plugins/editor_font_import_plugin.h | 2 +- 8 files changed, 221 insertions(+), 174 deletions(-) create mode 100644 tools/editor/icons/icon_bitmap_font.png diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 9930aa07f65..1bcb0d4fba3 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -570,13 +570,17 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); - ObjectTypeDB::register_type(); + ObjectTypeDB::register_virtual_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::add_compatibility_type("Font","BitmapFont"); + + ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 005a46c0bc1..727c8eee298 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -87,10 +87,10 @@ static Ref make_shader(const char*vertex_code,const char*fragment_code,c return shader; } -static Ref make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref &p_texture) { +static Ref make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref &p_texture) { - Ref font( memnew( Font ) ); + Ref font( memnew( BitmapFont ) ); font->add_texture( p_texture ); for (int i=0;i make_font(int p_height,int p_ascent, int p_valign, int p_charco return font; } -static Ref 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 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( memnew( Font ) ); + Ref font( memnew( BitmapFont ) ); DVector img; img.resize(p_w*p_h*2); @@ -187,10 +187,10 @@ void make_default_theme() { Ref t( memnew( Theme ) ); - //Ref 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 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 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 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 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 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 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 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 @@ -939,7 +939,7 @@ void make_default_theme() { style->set_default_margin( Margin(),8); } - Ref f = make_default_font(); + Ref f = make_default_font(); Theme::set_default( t ); Theme::set_default_icon( texture ); Theme::set_default_style( style ); diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index ef5b7a3eb03..0d8d2240378 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -31,7 +31,67 @@ #include "core/os/file_access.h" #include "core/io/resource_loader.h" -void Font::_set_chars(const DVector& 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=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& p_chars) { int len = p_chars.size(); //char 1 charsize 1 texture, 4 rect, 2 align, advance 1 @@ -50,7 +110,7 @@ void Font::_set_chars(const DVector& p_chars) { } -DVector Font::_get_chars() const { +DVector BitmapFont::_get_chars() const { DVector chars; @@ -74,7 +134,7 @@ DVector Font::_get_chars() const { return chars; } -void Font::_set_kernings(const DVector& p_kernings) { +void BitmapFont::_set_kernings(const DVector& p_kernings) { int len=p_kernings.size(); ERR_FAIL_COND(len%3); @@ -89,7 +149,7 @@ void Font::_set_kernings(const DVector& p_kernings) { } } -DVector Font::_get_kernings() const { +DVector BitmapFont::_get_kernings() const { DVector kernings; @@ -104,7 +164,7 @@ DVector Font::_get_kernings() const { } -void Font::_set_textures(const Vector & p_textures) { +void BitmapFont::_set_textures(const Vector & p_textures) { for(int i=0;i tex = p_textures[i]; @@ -114,7 +174,7 @@ void Font::_set_textures(const Vector & p_textures) { } -Vector Font::_get_textures() const { +Vector BitmapFont::_get_textures() const { Vector rtextures; for(int i=0;i Font::_get_textures() const { 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 //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; } -float Font::get_height() const{ +float BitmapFont::get_height() const{ return height; } -void Font::set_ascent(float p_ascent){ +void BitmapFont::set_ascent(float p_ascent){ ascent=p_ascent; } -float Font::get_ascent() const { +float BitmapFont::get_ascent() const { return ascent; } -float Font::get_descent() const { +float BitmapFont::get_descent() const { return height-ascent; } -void Font::add_texture(const Ref& p_texture) { +void BitmapFont::add_texture(const Ref& p_texture) { ERR_FAIL_COND( p_texture.is_null()); textures.push_back( p_texture ); } -int Font::get_texture_count() const { +int BitmapFont::get_texture_count() const { return textures.size(); }; -Ref Font::get_texture(int p_idx) const { +Ref BitmapFont::get_texture(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref()); return textures[p_idx]; }; -int Font::get_character_count() const { +int BitmapFont::get_character_count() const { return char_map.size(); }; -Vector Font::get_char_keys() const { +Vector BitmapFont::get_char_keys() const { Vector chars; chars.resize(char_map.size()); @@ -329,7 +389,7 @@ Vector Font::get_char_keys() const { 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)) { ERR_FAIL_V(Character()); @@ -338,7 +398,7 @@ Font::Character Font::get_character(CharType p_char) const { 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) 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; } -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; @@ -369,10 +429,10 @@ void Font::add_kerning_pair(CharType p_A,CharType p_B,int p_kerning) { } } -Vector Font::get_kerning_pair_keys() const { +Vector BitmapFont::get_kerning_pair_keys() const { - Vector ret; + Vector ret; ret.resize(kerning_map.size()); int i=0; @@ -385,7 +445,7 @@ Vector 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; kpk.A=p_A; @@ -398,19 +458,19 @@ int Font::get_kerning_pair(CharType p_A,CharType p_B) const { 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; emit_changed(); } -bool Font::is_distance_field_hint() const{ +bool BitmapFont::is_distance_field_hint() const{ return distance_field_hint; } -void Font::clear() { +void BitmapFont::clear() { height=1; ascent=0; @@ -426,7 +486,7 @@ Size2 Font::get_string_size(const String& p_string) const { int l = p_string.length(); if (l==0) - return Size2(0,height); + return Size2(0,get_height()); const CharType *sptr = &p_string[0]; for (int i=0;i &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::get_fallback() 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); + return fallback; } -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=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 { +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 { 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; } -void Font::set_fallback(const Ref &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(); + } + + Size2 ret(c->advance,c->rect.size.y); + + if (p_next) { + + KerningPairKey kpk; + kpk.A=p_char; + kpk.B=p_next; + + const Map::Element *E=kerning_map.find(kpk); + if (E) { + + ret.width-=E->get(); + } + } + + return ret; } -Ref Font::get_fallback() const{ +void BitmapFont::_bind_methods() { - return fallback; -} + ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&BitmapFont::create_from_fnt); + ObjectTypeDB::bind_method(_MD("set_height","px"),&BitmapFont::set_height); -void Font::_bind_methods() { + ObjectTypeDB::bind_method(_MD("set_ascent","px"),&BitmapFont::set_ascent); - ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&Font::create_from_fnt); - ObjectTypeDB::bind_method(_MD("set_height","px"),&Font::set_height); - ObjectTypeDB::bind_method(_MD("get_height"),&Font::get_height); + ObjectTypeDB::bind_method(_MD("add_kerning_pair","char_a","char_b","kerning"),&BitmapFont::add_kerning_pair); + ObjectTypeDB::bind_method(_MD("get_kerning_pair","char_a","char_b"),&BitmapFont::get_kerning_pair); - ObjectTypeDB::bind_method(_MD("set_ascent","px"),&Font::set_ascent); - ObjectTypeDB::bind_method(_MD("get_ascent"),&Font::get_ascent); - 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("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_char","character","texture","rect","align","advance"),&Font::add_char,DEFVAL(Point2()),DEFVAL(-1)); + 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:Texture","idx"),&Font::get_texture); + ObjectTypeDB::bind_method(_MD("get_texture_count"),&BitmapFont::get_texture_count); + 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_string_size","string"),&Font::get_string_size); + ObjectTypeDB::bind_method(_MD("get_char_size","char","next"),&BitmapFont::get_char_size,DEFVAL(0)); - ObjectTypeDB::bind_method(_MD("set_distance_field_hint","enable"),&Font::set_distance_field_hint); - ObjectTypeDB::bind_method(_MD("is_distance_field_hint"),&Font::is_distance_field_hint); + ObjectTypeDB::bind_method(_MD("set_distance_field_hint","enable"),&BitmapFont::set_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("_get_chars"),&Font::_get_chars); + ObjectTypeDB::bind_method(_MD("_set_chars"),&BitmapFont::_set_chars); + ObjectTypeDB::bind_method(_MD("_get_chars"),&BitmapFont::_get_chars); - ObjectTypeDB::bind_method(_MD("_set_kernings"),&Font::_set_kernings); - ObjectTypeDB::bind_method(_MD("_get_kernings"),&Font::_get_kernings); + ObjectTypeDB::bind_method(_MD("_set_kernings"),&BitmapFont::_set_kernings); + ObjectTypeDB::bind_method(_MD("_get_kernings"),&BitmapFont::_get_kernings); - ObjectTypeDB::bind_method(_MD("_set_textures"),&Font::_set_textures); - ObjectTypeDB::bind_method(_MD("_get_textures"),&Font::_get_textures); + ObjectTypeDB::bind_method(_MD("_set_textures"),&BitmapFont::_set_textures); + ObjectTypeDB::bind_method(_MD("_get_textures"),&BitmapFont::_get_textures); - ObjectTypeDB::bind_method(_MD("set_fallback","fallback"),&Font::set_fallback); - ObjectTypeDB::bind_method(_MD("get_fallback"),&Font::get_fallback); + ObjectTypeDB::bind_method(_MD("set_fallback","fallback"),&BitmapFont::set_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::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, "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::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(); @@ -569,7 +612,7 @@ Font::Font() { } -Font::~Font() { +BitmapFont::~BitmapFont() { clear(); } diff --git a/scene/resources/font.h b/scene/resources/font.h index 03b1ec5191b..91f4874932c 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -35,9 +35,40 @@ /** @author Juan Linietsky */ + + class Font : public 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"); Vector< Ref > textures; @@ -84,7 +115,7 @@ private: void _set_textures(const Vector & p_textures); Vector _get_textures() const; - Ref fallback; + Ref fallback; protected: static void _bind_methods(); @@ -114,54 +145,23 @@ public: int get_kerning_pair(CharType p_A,CharType p_B) const; Vector get_kerning_pair_keys() const; - inline Size2 get_char_size(CharType p_char,CharType p_next=0) const; - Size2 get_string_size(const String& p_string) const; + Size2 get_char_size(CharType p_char,CharType p_next=0) const; - - void set_fallback(const Ref &p_fallback); - Ref get_fallback() const; + void set_fallback(const Ref &p_fallback); + Ref get_fallback() const; void clear(); void set_distance_field_hint(bool p_distance_field); 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; - Font(); - ~Font(); + BitmapFont(); + ~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::Element *E=kerning_map.find(kpk); - if (E) { - - ret.width-=E->get(); - } - } - - return ret; -} diff --git a/tools/editor/editor_fonts.cpp b/tools/editor/editor_fonts.cpp index b12b041f16d..a3ec08f9862 100644 --- a/tools/editor/editor_fonts.cpp +++ b/tools/editor/editor_fonts.cpp @@ -31,10 +31,10 @@ #include "doc_title_font.h" #include "doc_code_font.h" -static Ref make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref &p_texture) { +static Ref make_font(int p_height,int p_ascent, int p_valign, int p_charcount, const int *p_chars,const Ref &p_texture) { - Ref font( memnew( Font ) ); + Ref font( memnew( BitmapFont ) ); font->add_texture( p_texture ); for (int i=0;i make_font(int p_height,int p_ascent, int p_valign, int p_charco void editor_register_fonts(Ref p_theme) { - Ref 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 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 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 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 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 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_code","EditorFonts",doc_code_font); p_theme->set_font("doc_title","EditorFonts",doc_title_font); diff --git a/tools/editor/icons/icon_bitmap_font.png b/tools/editor/icons/icon_bitmap_font.png new file mode 100644 index 0000000000000000000000000000000000000000..45de2b0f0af12f8001e4c1710c1396139898b458 GIT binary patch literal 362 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G^tAk28_ZrvZCAbW|YuPgfl7FkACox^-thCm_N%#er@=ltB<)VvZPmw~~#C^fMp zHASI3vm`^o-P1Q9MK6^dD1Oe<#WBR<)e#E-7Jk@!5@B3fCm zZ(<`5a7Hp}i(lsq%zyvDAguTzPoe*ghmw!x^(3x&wrJ74dk5FdnIk^o>js5l78zOZ z*4Ebkgw3%Y9xr&AnGf&Uo`3&e<=)cQZZj;pxFsYd^98;bD?ip;qu210ll6_sp=XoC zPb409$<*Q5!&q_fwEF6Q`)fA6`v2e?6Sv(-{o`*=mLKe9p0ASdru_ZAPJ!G5zrVk? xocHeEg+GrUueUYW=->YTZS|(lwf4SATnv{US~t9ZvGFL#%bu=&F6*2Ung9&Uk9z imd = get_rimd(); - Ref font = plugin->generate_font(imd); + Ref font = plugin->generate_font(imd); test_label->add_font_override("font",font); _update_text(); } @@ -454,7 +454,7 @@ class EditorFontImportDialog : public ConfirmationDialog { void _import_inc(String p_font) { - Ref font = ResourceLoader::load(p_font); + Ref font = ResourceLoader::load(p_font); if (!font.is_valid()) return; Ref tex = font->get_texture(0); @@ -471,12 +471,12 @@ class EditorFontImportDialog : public ConfirmationDialog { for(int i=0;iget_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("};"); - Vector kp=font->get_kerning_pair_keys(); + Vector 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_pairs["+itos(kp.size())+"][3]={"); for(int i=0;i fl; - Ref font= memnew(Font); + Ref font= memnew(BitmapFont); dest->get_file_dialog()->add_filter("*.fnt ; Font" ); //ResourceSaver::get_recognized_extensions(font,&fl); //for(List::Element *E=fl.front();E;E=E->next()) { @@ -875,12 +875,12 @@ static unsigned char get_SDF_radial( } -Ref EditorFontImportPlugin::generate_font(const Ref& p_from, const String &p_existing) { +Ref EditorFontImportPlugin::generate_font(const Ref& p_from, const String &p_existing) { Ref from = p_from; - ERR_FAIL_COND_V(from->get_source_count()!=1,Ref()); + ERR_FAIL_COND_V(from->get_source_count()!=1,Ref()); String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0)); @@ -888,15 +888,15 @@ Ref EditorFontImportPlugin::generate_font(const Refshow_warning("Path: "+src_path+"\nIs a Godot font file, please supply a BMFont type file instead."); - return Ref(); + return Ref(); } - Ref font; + Ref font; font.instance(); Error err = font->create_from_fnt(src_path); if (err) { EditorNode::get_singleton()->show_warning("Path: "+src_path+"\nFailed opening as BMFont file."); - return Ref(); + return Ref(); } return font; @@ -913,7 +913,7 @@ Ref EditorFontImportPlugin::generate_font(const Ref() ); + ERR_FAIL_COND_V( error !=0, Ref() ); print_line("loadfrom: "+src_path); error = FT_New_Face( library, src_path.utf8().get_data(),0,&face ); @@ -928,7 +928,7 @@ Ref EditorFontImportPlugin::generate_font(const Ref()); + ERR_FAIL_COND_V(error,Ref()); int height=0; @@ -940,7 +940,7 @@ Ref EditorFontImportPlugin::generate_font(const Ref() ); + ERR_FAIL_COND_V( error,Ref() ); } @@ -987,7 +987,7 @@ Ref EditorFontImportPlugin::generate_font(const Ref() ); + ERR_FAIL_COND_V( !fa,Ref() ); } @@ -1546,15 +1546,15 @@ Ref EditorFontImportPlugin::generate_font(const Refget_option("extra_space/bottom"); bool disable_filter = from->get_option("advanced/disable_filter"); - Ref font; + Ref font; if (p_existing!=String() && ResourceCache::has(p_existing)) { - font = Ref( ResourceCache::get(p_existing)->cast_to()); + font = Ref( ResourceCache::get(p_existing)->cast_to()); } if (font.is_null()) { - font = Ref( memnew( Font ) ); + font = Ref( memnew( BitmapFont ) ); } font->clear(); @@ -1596,7 +1596,7 @@ Ref EditorFontImportPlugin::generate_font(const Ref(); + return Ref(); #endif } @@ -1616,7 +1616,7 @@ void EditorFontImportPlugin::import_dialog(const String& p_from){ Error EditorFontImportPlugin::import(const String& p_path, const Ref& p_from){ - Ref font = EditorFontImportPlugin::generate_font(p_from,p_path); + Ref font = EditorFontImportPlugin::generate_font(p_from,p_path); if (!font.is_valid()) return ERR_CANT_CREATE; diff --git a/tools/editor/io_plugins/editor_font_import_plugin.h b/tools/editor/io_plugins/editor_font_import_plugin.h index 814897c5f04..ce26ef91e2d 100644 --- a/tools/editor/io_plugins/editor_font_import_plugin.h +++ b/tools/editor/io_plugins/editor_font_import_plugin.h @@ -42,7 +42,7 @@ class EditorFontImportPlugin : public EditorImportPlugin { EditorFontImportDialog *dialog; public: - Ref generate_font(const Ref& p_from,const String& p_existing=String()); //used by editor + Ref generate_font(const Ref& p_from,const String& p_existing=String()); //used by editor virtual String get_name() const; virtual String get_visible_name() const;