Browse Source

Added extra spacing support for DynamicFont

Side effect is that label min-size will now take into account
kerning.
Zher Huei Lee 9 years ago
parent
commit
af6ef01c69
3 changed files with 77 additions and 7 deletions
  1. 2 2
      scene/gui/label.cpp
  2. 57 5
      scene/resources/dynamic_font.cpp
  3. 18 0
      scene/resources/dynamic_font.h

+ 2 - 2
scene/gui/label.cpp

@@ -334,7 +334,7 @@ int Label::get_longest_line_width() const {
 			}
 			}
 		} else {
 		} else {
 
 
-			int char_width=font->get_char_size(current).width;
+			int char_width=font->get_char_size(current,text[i+1]).width;
 			line_width+=char_width;
 			line_width+=char_width;
 		}
 		}
 
 
@@ -454,7 +454,7 @@ void Label::regenerate_word_cache() {
 				word_pos=i;
 				word_pos=i;
 			}
 			}
 
 
-			char_width=font->get_char_size(current).width;
+			char_width=font->get_char_size(current,text[i+1]).width;
 			current_word_size+=char_width;
 			current_word_size+=char_width;
 			line_width+=char_width;
 			line_width+=char_width;
 			total_char_cache++;
 			total_char_cache++;

+ 57 - 5
scene/resources/dynamic_font.cpp

@@ -703,12 +703,43 @@ void DynamicFont::set_use_filter(bool p_enable){
 	_update_texture_flags();
 	_update_texture_flags();
 }
 }
 
 
+int DynamicFont::get_spacing(int p_type) const{
+
+	if (p_type == SPACING_TOP){
+		return spacing_top;
+	}else if (p_type == SPACING_BOTTOM){
+		return spacing_bottom;
+	}else if (p_type == SPACING_CHAR){
+		return spacing_char;
+	}else if (p_type == SPACING_SPACE){
+		return spacing_space;
+	}
+
+	return 0;
+}
+
+void DynamicFont::set_spacing(int p_type, int p_value){
+
+	if (p_type == SPACING_TOP){
+		spacing_top=p_value;
+	}else if (p_type == SPACING_BOTTOM){
+		spacing_bottom=p_value;
+	}else if (p_type == SPACING_CHAR){
+		spacing_char=p_value;
+	}else if (p_type == SPACING_SPACE){
+		spacing_space=p_value;
+	}
+
+	emit_changed();
+	_change_notify();
+}
+
 float DynamicFont::get_height() const{
 float DynamicFont::get_height() const{
 
 
 	if (!data_at_size.is_valid())
 	if (!data_at_size.is_valid())
 		return 1;
 		return 1;
 
 
-	return data_at_size->get_height();
+	return data_at_size->get_height()+spacing_top+spacing_bottom;
 }
 }
 
 
 float DynamicFont::get_ascent() const{
 float DynamicFont::get_ascent() const{
@@ -716,7 +747,7 @@ float DynamicFont::get_ascent() const{
 	if (!data_at_size.is_valid())
 	if (!data_at_size.is_valid())
 		return 1;
 		return 1;
 
 
-	return data_at_size->get_ascent();
+	return data_at_size->get_ascent()+spacing_top;
 }
 }
 
 
 float DynamicFont::get_descent() const{
 float DynamicFont::get_descent() const{
@@ -724,7 +755,7 @@ float DynamicFont::get_descent() const{
 	if (!data_at_size.is_valid())
 	if (!data_at_size.is_valid())
 		return 1;
 		return 1;
 
 
-	return data_at_size->get_descent();
+	return data_at_size->get_descent()+spacing_bottom;
 
 
 }
 }
 
 
@@ -733,7 +764,13 @@ Size2 DynamicFont::get_char_size(CharType p_char,CharType p_next) const{
 	if (!data_at_size.is_valid())
 	if (!data_at_size.is_valid())
 		return Size2(1,1);
 		return Size2(1,1);
 
 
-	return data_at_size->get_char_size(p_char,p_next,fallback_data_at_size);
+	Size2 ret=data_at_size->get_char_size(p_char,p_next,fallback_data_at_size);
+	if (p_char==' ')
+		ret.width+=spacing_space+spacing_char;
+	else if (p_next)
+		ret.width+=spacing_char;
+
+	return ret;
 
 
 }
 }
 
 
@@ -747,7 +784,7 @@ float DynamicFont::draw_char(RID p_canvas_item, const Point2& p_pos, CharType p_
 	if (!data_at_size.is_valid())
 	if (!data_at_size.is_valid())
 		return 0;
 		return 0;
 
 
-	return data_at_size->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate,fallback_data_at_size);
+	return data_at_size->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate,fallback_data_at_size)+spacing_char;
 
 
 }
 }
 void DynamicFont::set_fallback(int p_idx,const Ref<DynamicFontData>& p_data) {
 void DynamicFont::set_fallback(int p_idx,const Ref<DynamicFontData>& p_data) {
@@ -854,6 +891,8 @@ void DynamicFont::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("get_use_mipmaps"),&DynamicFont::get_use_mipmaps);
 	ObjectTypeDB::bind_method(_MD("get_use_mipmaps"),&DynamicFont::get_use_mipmaps);
 	ObjectTypeDB::bind_method(_MD("set_use_filter","enable"),&DynamicFont::set_use_filter);
 	ObjectTypeDB::bind_method(_MD("set_use_filter","enable"),&DynamicFont::set_use_filter);
 	ObjectTypeDB::bind_method(_MD("get_use_filter"),&DynamicFont::get_use_filter);
 	ObjectTypeDB::bind_method(_MD("get_use_filter"),&DynamicFont::get_use_filter);
+	ObjectTypeDB::bind_method(_MD("set_spacing","type","value"),&DynamicFont::set_spacing);
+	ObjectTypeDB::bind_method(_MD("get_spacing","type"),&DynamicFont::get_spacing);
 
 
 	ObjectTypeDB::bind_method(_MD("add_fallback","data:DynamicFontData"),&DynamicFont::add_fallback);
 	ObjectTypeDB::bind_method(_MD("add_fallback","data:DynamicFontData"),&DynamicFont::add_fallback);
 	ObjectTypeDB::bind_method(_MD("set_fallback","idx","data:DynamicFontData"),&DynamicFont::set_fallback);
 	ObjectTypeDB::bind_method(_MD("set_fallback","idx","data:DynamicFontData"),&DynamicFont::set_fallback);
@@ -863,14 +902,27 @@ void DynamicFont::_bind_methods() {
 
 
 
 
 	ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size"));
 	ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size"));
+	ADD_PROPERTYINZ(PropertyInfo(Variant::INT,"extra_spacing/top"),_SCS("set_spacing"),_SCS("get_spacing"),SPACING_TOP);
+	ADD_PROPERTYINZ(PropertyInfo(Variant::INT,"extra_spacing/bottom"),_SCS("set_spacing"),_SCS("get_spacing"),SPACING_BOTTOM);
+	ADD_PROPERTYINZ(PropertyInfo(Variant::INT,"extra_spacing/char"),_SCS("set_spacing"),_SCS("get_spacing"),SPACING_CHAR);
+	ADD_PROPERTYINZ(PropertyInfo(Variant::INT,"extra_spacing/space"),_SCS("set_spacing"),_SCS("get_spacing"),SPACING_SPACE);
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_mipmaps"),_SCS("set_use_mipmaps"),_SCS("get_use_mipmaps"));
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_mipmaps"),_SCS("set_use_mipmaps"),_SCS("get_use_mipmaps"));
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_filter"),_SCS("set_use_filter"),_SCS("get_use_filter"));
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL,"font/use_filter"),_SCS("set_use_filter"),_SCS("get_use_filter"));
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data"));
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data"));
+
+	BIND_CONSTANT( SPACING_TOP );
+	BIND_CONSTANT( SPACING_BOTTOM );
+	BIND_CONSTANT( SPACING_CHAR );
+	BIND_CONSTANT( SPACING_SPACE );
 }
 }
 
 
 DynamicFont::DynamicFont() {
 DynamicFont::DynamicFont() {
 
 
 	size=16;
 	size=16;
+	spacing_top=0;
+	spacing_bottom=0;
+	spacing_char=0;
+	spacing_space=0;
 	use_mipmaps=false;
 	use_mipmaps=false;
 	use_filter=false;
 	use_filter=false;
 	texture_flags=0;
 	texture_flags=0;

+ 18 - 0
scene/resources/dynamic_font.h

@@ -159,6 +159,17 @@ class DynamicFont : public Font {
 
 
 	OBJ_TYPE( DynamicFont, Font );
 	OBJ_TYPE( DynamicFont, Font );
 
 
+public:
+
+	enum SpacingType{
+		SPACING_TOP,
+		SPACING_BOTTOM,
+		SPACING_CHAR,
+		SPACING_SPACE
+	};
+
+private:
+
 	Ref<DynamicFontData> data;
 	Ref<DynamicFontData> data;
 	Ref<DynamicFontAtSize> data_at_size;
 	Ref<DynamicFontAtSize> data_at_size;
 
 
@@ -168,6 +179,10 @@ class DynamicFont : public Font {
 
 
 	int size;
 	int size;
 	bool valid;
 	bool valid;
+	int spacing_top;
+	int spacing_bottom;
+	int spacing_char;
+	int spacing_space;
 	bool use_mipmaps;
 	bool use_mipmaps;
 	bool use_filter;
 	bool use_filter;
 	uint32_t texture_flags;
 	uint32_t texture_flags;
@@ -196,6 +211,9 @@ public:
 	bool get_use_filter() const;
 	bool get_use_filter() const;
 	void set_use_filter(bool p_enable);
 	void set_use_filter(bool p_enable);
 
 
+	int get_spacing(int p_type) const;
+	void set_spacing(int p_type, int p_value);
+
 	void add_fallback(const Ref<DynamicFontData>& p_data);
 	void add_fallback(const Ref<DynamicFontData>& p_data);
 	void set_fallback(int p_idx,const Ref<DynamicFontData>& p_data);
 	void set_fallback(int p_idx,const Ref<DynamicFontData>& p_data);
 	int get_fallback_count() const;
 	int get_fallback_count() const;