added fallbacks proprty to fonts

This commit is contained in:
Juan Linietsky 2015-12-10 23:02:40 -03:00
parent 5a60edf1ff
commit 46497c4508
2 changed files with 32 additions and 23 deletions

View File

@ -462,31 +462,16 @@ void Font::draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,fl
void Font::draw(RID p_canvas_item, const Point2& p_pos, const String& p_text, const Color& p_modulate,int p_clip_w) const { void Font::draw(RID p_canvas_item, const Point2& p_pos, const String& p_text, const Color& p_modulate,int p_clip_w) const {
Point2 pos=p_pos; Vector2 ofs;
float ofs=0;
VisualServer *vs = VisualServer::get_singleton();
for (int i=0;i<p_text.length();i++) { for (int i=0;i<p_text.length();i++) {
const Character * c = char_map.getptr(p_text[i]); int width = get_char_size(p_text[i]).width;
if (!c) if (p_clip_w>=0 && (ofs.x+width)>p_clip_w)
continue;
// if (p_clip_w>=0 && (ofs+c->rect.size.width)>(p_clip_w))
// break; //width exceeded
if (p_clip_w>=0 && (ofs+c->rect.size.width)>p_clip_w)
break; //clip break; //clip
Point2 cpos=pos;
cpos.x+=ofs+c->h_align;
cpos.y-=ascent;
cpos.y+=c->v_align;
ERR_CONTINUE( c->texture_idx<-1 || c->texture_idx>=textures.size());
if (c->texture_idx!=-1)
textures[c->texture_idx]->draw_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), c->rect, p_modulate );
ofs+=get_char_size(p_text[i],p_text[i+1]).width; ofs.x+=draw_char(p_canvas_item,p_pos+ofs,p_text[i],p_text[i+1],p_modulate);
} }
} }
@ -494,8 +479,11 @@ float Font::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_
const Character * c = char_map.getptr(p_char); const Character * c = char_map.getptr(p_char);
if (!c) if (!c) {
if (fallback.is_valid())
return fallback->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate);
return 0; return 0;
}
Point2 cpos=p_pos; Point2 cpos=p_pos;
cpos.x+=c->h_align; cpos.x+=c->h_align;
@ -508,6 +496,16 @@ 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;
}
Ref<Font> Font::get_fallback() const{
return fallback;
}
void Font::_bind_methods() { void Font::_bind_methods() {
ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&Font::create_from_fnt); ObjectTypeDB::bind_method(_MD("create_from_fnt","path"),&Font::create_from_fnt);
@ -548,6 +546,8 @@ void Font::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_set_textures"),&Font::_set_textures); ObjectTypeDB::bind_method(_MD("_set_textures"),&Font::_set_textures);
ObjectTypeDB::bind_method(_MD("_get_textures"),&Font::_get_textures); ObjectTypeDB::bind_method(_MD("_get_textures"),&Font::_get_textures);
ObjectTypeDB::bind_method(_MD("set_fallback","fallback"),&Font::set_fallback);
ObjectTypeDB::bind_method(_MD("get_fallback"),&Font::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,6 +556,7 @@ 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") );
} }

View File

@ -84,6 +84,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;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -113,9 +114,13 @@ 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;
_FORCE_INLINE_ Size2 get_char_size(CharType p_char,CharType p_next=0) 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_string_size(const String& p_string) const;
void set_fallback(const Ref<Font> &p_fallback);
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);
@ -134,8 +139,11 @@ Size2 Font::get_char_size(CharType p_char,CharType p_next) const {
const Character * c = char_map.getptr(p_char); const Character * c = char_map.getptr(p_char);
if (!c) if (!c) {
if (fallback.is_valid())
return fallback->get_char_size(p_char,p_next);
return Size2(); return Size2();
}
Size2 ret(c->advance,c->rect.size.y); Size2 ret(c->advance,c->rect.size.y);