diff options
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/bit_mask.h | 2 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.cpp | 275 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.h | 2 | ||||
-rw-r--r-- | scene/resources/dynamic_font.cpp | 582 | ||||
-rw-r--r-- | scene/resources/dynamic_font.h | 99 | ||||
-rw-r--r-- | scene/resources/dynamic_font_stb.cpp | 527 | ||||
-rw-r--r-- | scene/resources/dynamic_font_stb.h | 178 | ||||
-rw-r--r-- | scene/resources/mesh.cpp | 5 | ||||
-rw-r--r-- | scene/resources/style_box.cpp | 21 | ||||
-rw-r--r-- | scene/resources/style_box.h | 4 | ||||
-rw-r--r-- | scene/resources/world_2d.cpp | 10 |
11 files changed, 1372 insertions, 333 deletions
diff --git a/scene/resources/bit_mask.h b/scene/resources/bit_mask.h index b245ea1542..e75a2aa332 100644 --- a/scene/resources/bit_mask.h +++ b/scene/resources/bit_mask.h @@ -36,6 +36,8 @@ class BitMap : public Resource { OBJ_TYPE(BitMap,Resource); + OBJ_SAVE_TYPE(BitMap); + RES_BASE_EXTENSION("pbm"); Vector<uint8_t> bitmask; int width; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index cd90bf52b6..9ebb7e7561 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -28,6 +28,7 @@ typedef Map<const void*,Ref<ImageTexture> > TexCacheMap; static TexCacheMap *tex_cache; +static int scale=1; template<class T> static Ref<StyleBoxTexture> make_stylebox(T p_src,float p_left, float p_top, float p_right, float p_botton,float p_margin_left=-1, float p_margin_top=-1, float p_margin_right=-1, float p_margin_botton=-1, bool p_draw_center=true) { @@ -40,21 +41,24 @@ static Ref<StyleBoxTexture> make_stylebox(T p_src,float p_left, float p_top, flo } else { texture = Ref<ImageTexture>( memnew( ImageTexture ) ); - texture->create_from_image( Image(p_src),ImageTexture::FLAG_FILTER ); + Image img(p_src); + if (scale>1) + img.expand_x2_hq2x(); + texture->create_from_image( img,ImageTexture::FLAG_FILTER ); (*tex_cache)[p_src]=texture; } Ref<StyleBoxTexture> style( memnew( StyleBoxTexture ) ); style->set_texture(texture); - style->set_margin_size( MARGIN_LEFT, p_left ); - style->set_margin_size( MARGIN_RIGHT, p_right ); - style->set_margin_size( MARGIN_BOTTOM, p_botton ); - style->set_margin_size( MARGIN_TOP, p_top ); - style->set_default_margin( MARGIN_LEFT, p_margin_left ); - style->set_default_margin( MARGIN_RIGHT, p_margin_right ); - style->set_default_margin( MARGIN_BOTTOM, p_margin_botton ); - style->set_default_margin( MARGIN_TOP, p_margin_top ); + style->set_margin_size( MARGIN_LEFT, p_left * scale); + style->set_margin_size( MARGIN_RIGHT, p_right * scale); + style->set_margin_size( MARGIN_BOTTOM, p_botton * scale); + style->set_margin_size( MARGIN_TOP, p_top * scale); + style->set_default_margin( MARGIN_LEFT, p_margin_left * scale); + style->set_default_margin( MARGIN_RIGHT, p_margin_right * scale); + style->set_default_margin( MARGIN_BOTTOM, p_margin_botton * scale); + style->set_default_margin( MARGIN_TOP, p_margin_top * scale); style->set_draw_center(p_draw_center); return style; @@ -63,10 +67,10 @@ static Ref<StyleBoxTexture> make_stylebox(T p_src,float p_left, float p_top, flo static Ref<StyleBoxTexture> sb_expand(Ref<StyleBoxTexture> p_sbox,float p_left, float p_top, float p_right, float p_botton) { - p_sbox->set_expand_margin_size(MARGIN_LEFT,p_left); - p_sbox->set_expand_margin_size(MARGIN_TOP,p_top); - p_sbox->set_expand_margin_size(MARGIN_RIGHT,p_right); - p_sbox->set_expand_margin_size(MARGIN_BOTTOM,p_botton); + p_sbox->set_expand_margin_size(MARGIN_LEFT,p_left * scale); + p_sbox->set_expand_margin_size(MARGIN_TOP,p_top * scale); + p_sbox->set_expand_margin_size(MARGIN_RIGHT,p_right * scale); + p_sbox->set_expand_margin_size(MARGIN_BOTTOM,p_botton * scale); return p_sbox; } @@ -75,7 +79,10 @@ static Ref<Texture> make_icon(T p_src) { Ref<ImageTexture> texture( memnew( ImageTexture ) ); - texture->create_from_image( Image(p_src),ImageTexture::FLAG_FILTER ); + Image img = Image(p_src); + if (scale>1) + img.expand_x2_hq2x(); + texture->create_from_image( img,ImageTexture::FLAG_FILTER ); return texture; } @@ -170,27 +177,24 @@ static Ref<StyleBox> make_empty_stylebox(float p_margin_left=-1, float p_margin_ Ref<StyleBox> style( memnew( StyleBoxEmpty) ); - style->set_default_margin( MARGIN_LEFT, p_margin_left ); - style->set_default_margin( MARGIN_RIGHT, p_margin_right ); - style->set_default_margin( MARGIN_BOTTOM, p_margin_botton ); - style->set_default_margin( MARGIN_TOP, p_margin_top ); + style->set_default_margin( MARGIN_LEFT, p_margin_left * scale); + style->set_default_margin( MARGIN_RIGHT, p_margin_right * scale); + style->set_default_margin( MARGIN_BOTTOM, p_margin_botton * scale); + style->set_default_margin( MARGIN_TOP, p_margin_top * scale); return style; } -#ifndef DEFAULT_THEME_DISABLED - -void make_default_theme() { +void fill_default_theme(Ref<Theme>& t,const Ref<Font> & default_font,const Ref<Font> & large_font,Ref<Texture>& default_icon, Ref<StyleBox>& default_style,bool p_hidpi) { + if (p_hidpi) + scale=2; + else + scale=1; tex_cache = memnew( TexCacheMap ); - Ref<Theme> t( memnew( Theme ) ); - //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<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<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<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 @@ -213,7 +217,7 @@ void make_default_theme() { Ref<StyleBoxTexture> focus = make_stylebox( focus_png,5,5,5,5); for(int i=0;i<4;i++) { - focus->set_expand_margin_size(Margin(i),1); + focus->set_expand_margin_size(Margin(i),1 *scale); } @@ -239,7 +243,7 @@ void make_default_theme() { t->set_color("font_color_hover","Button", control_font_color_hover ); t->set_color("font_color_disabled","Button", control_font_color_disabled ); - t->set_constant("hseparation","Button", 2); + t->set_constant("hseparation","Button", 2 *scale); // LinkButton @@ -249,7 +253,7 @@ void make_default_theme() { t->set_color("font_color_pressed","LinkButton", control_font_color_pressed ); t->set_color("font_color_hover","LinkButton", control_font_color_hover ); - t->set_constant("underline_spacing","LinkButton", 2 ); + t->set_constant("underline_spacing","LinkButton", 2 *scale); // ColorPickerButton @@ -266,16 +270,16 @@ void make_default_theme() { t->set_color("font_color_hover","ColorPickerButton", Color(1,1,1,1) ); t->set_color("font_color_disabled","ColorPickerButton", Color(0.9,0.9,0.9,0.3) ); - t->set_constant("hseparation","ColorPickerButton", 2 ); + t->set_constant("hseparation","ColorPickerButton", 2 *scale); // ToolButton Ref<StyleBox> tb_empty = memnew( StyleBoxEmpty ); - tb_empty->set_default_margin(MARGIN_LEFT,6); - tb_empty->set_default_margin(MARGIN_RIGHT,6); - tb_empty->set_default_margin(MARGIN_TOP,4); - tb_empty->set_default_margin(MARGIN_BOTTOM,4); + tb_empty->set_default_margin(MARGIN_LEFT,6 *scale); + tb_empty->set_default_margin(MARGIN_RIGHT,6 *scale); + tb_empty->set_default_margin(MARGIN_TOP,4 *scale); + tb_empty->set_default_margin(MARGIN_BOTTOM,4 *scale); t->set_stylebox("normal","ToolButton", tb_empty); t->set_stylebox("pressed","ToolButton", make_stylebox( button_pressed_png,4,4,4,4) ); @@ -316,8 +320,8 @@ void make_default_theme() { t->set_color("font_color_hover","OptionButton", control_font_color_hover ); t->set_color("font_color_disabled","OptionButton", control_font_color_disabled ); - t->set_constant("hseparation","OptionButton", 2 ); - t->set_constant("arrow_margin","OptionButton", 2 ); + t->set_constant("hseparation","OptionButton", 2 *scale); + t->set_constant("arrow_margin","OptionButton", 2 *scale); @@ -336,7 +340,7 @@ void make_default_theme() { t->set_color("font_color_hover","MenuButton", control_font_color_hover ); t->set_color("font_color_disabled","MenuButton", Color(1,1,1,0.3) ); - t->set_constant("hseparation","MenuButton", 3 ); + t->set_constant("hseparation","MenuButton", 3 *scale); // ButtonGroup @@ -345,15 +349,15 @@ void make_default_theme() { // CheckBox Ref<StyleBox> cbx_empty = memnew( StyleBoxEmpty ); - cbx_empty->set_default_margin(MARGIN_LEFT,22); - cbx_empty->set_default_margin(MARGIN_RIGHT,4); - cbx_empty->set_default_margin(MARGIN_TOP,4); - cbx_empty->set_default_margin(MARGIN_BOTTOM,5); + cbx_empty->set_default_margin(MARGIN_LEFT,22 *scale); + cbx_empty->set_default_margin(MARGIN_RIGHT,4 *scale); + cbx_empty->set_default_margin(MARGIN_TOP,4 *scale); + cbx_empty->set_default_margin(MARGIN_BOTTOM,5 *scale); Ref<StyleBox> cbx_focus = focus; - cbx_focus->set_default_margin(MARGIN_LEFT,4); - cbx_focus->set_default_margin(MARGIN_RIGHT,22); - cbx_focus->set_default_margin(MARGIN_TOP,4); - cbx_focus->set_default_margin(MARGIN_BOTTOM,5); + cbx_focus->set_default_margin(MARGIN_LEFT,4 *scale); + cbx_focus->set_default_margin(MARGIN_RIGHT,22 *scale); + cbx_focus->set_default_margin(MARGIN_TOP,4 *scale); + cbx_focus->set_default_margin(MARGIN_BOTTOM,5 *scale); t->set_stylebox("normal","CheckBox", cbx_empty ); t->set_stylebox("pressed","CheckBox", cbx_empty ); @@ -373,18 +377,18 @@ void make_default_theme() { t->set_color("font_color_hover","CheckBox", control_font_color_hover ); t->set_color("font_color_disabled","CheckBox", control_font_color_disabled ); - t->set_constant("hseparation","CheckBox",4); - t->set_constant("check_vadjust","CheckBox",0); + t->set_constant("hseparation","CheckBox",4 *scale); + t->set_constant("check_vadjust","CheckBox",0 *scale); // CheckButton Ref<StyleBox> cb_empty = memnew( StyleBoxEmpty ); - cb_empty->set_default_margin(MARGIN_LEFT,6); - cb_empty->set_default_margin(MARGIN_RIGHT,70); - cb_empty->set_default_margin(MARGIN_TOP,4); - cb_empty->set_default_margin(MARGIN_BOTTOM,4); + cb_empty->set_default_margin(MARGIN_LEFT,6 *scale); + cb_empty->set_default_margin(MARGIN_RIGHT,70 *scale); + cb_empty->set_default_margin(MARGIN_TOP,4 *scale); + cb_empty->set_default_margin(MARGIN_BOTTOM,4 *scale); t->set_stylebox("normal","CheckButton", cb_empty ); t->set_stylebox("pressed","CheckButton", cb_empty ); @@ -402,8 +406,8 @@ void make_default_theme() { t->set_color("font_color_hover","CheckButton", control_font_color_hover ); t->set_color("font_color_disabled","CheckButton", control_font_color_disabled ); - t->set_constant("hseparation","CheckButton",4); - t->set_constant("check_vadjust","CheckButton",0); + t->set_constant("hseparation","CheckButton",4 *scale); + t->set_constant("check_vadjust","CheckButton",0 *scale); @@ -414,10 +418,10 @@ void make_default_theme() { t->set_color("font_color","Label", Color(1,1,1) ); t->set_color("font_color_shadow","Label", Color(0,0,0,0) ); - t->set_constant("shadow_offset_x","Label", 1 ); - t->set_constant("shadow_offset_y","Label", 1 ); - t->set_constant("shadow_as_outline","Label", 0 ); - t->set_constant("line_spacing","Label", 3 ); + t->set_constant("shadow_offset_x","Label", 1 *scale); + t->set_constant("shadow_offset_y","Label", 1 *scale); + t->set_constant("shadow_as_outline","Label", 0 *scale); + t->set_constant("line_spacing","Label", 3 *scale); @@ -434,7 +438,7 @@ void make_default_theme() { t->set_color("cursor_color","LineEdit", control_font_color_hover ); t->set_color("selection_color","LineEdit", font_color_selection ); - t->set_constant("minimum_spaces","LineEdit", 12 ); + t->set_constant("minimum_spaces","LineEdit", 12 *scale); @@ -475,7 +479,7 @@ void make_default_theme() { t->set_constant("completion_lines","TextEdit", 7 ); t->set_constant("completion_max_width","TextEdit", 50 ); t->set_constant("completion_scroll_width","TextEdit", 3 ); - t->set_constant("line_spacing","TextEdit",4 ); + t->set_constant("line_spacing","TextEdit",4 *scale); Ref<Texture> empty_icon = memnew( ImageTexture ); @@ -555,10 +559,10 @@ void make_default_theme() { t->set_color("title_color","WindowDialog", Color(0,0,0) ); - t->set_constant("close_h_ofs","WindowDialog", 22 ); - t->set_constant("close_v_ofs","WindowDialog", 20 ); - t->set_constant("titlebar_height","WindowDialog", 18 ); - t->set_constant("title_height","WindowDialog", 20 ); + t->set_constant("close_h_ofs","WindowDialog", 22 *scale); + t->set_constant("close_v_ofs","WindowDialog", 20 *scale); + t->set_constant("titlebar_height","WindowDialog", 18 *scale); + t->set_constant("title_height","WindowDialog", 20 *scale); // File Dialog @@ -572,7 +576,7 @@ void make_default_theme() { Ref<StyleBoxTexture> selected = make_stylebox( selection_png,6,6,6,6); for(int i=0;i<4;i++) { - selected->set_expand_margin_size(Margin(i),2); + selected->set_expand_margin_size(Margin(i),2 *scale); } t->set_stylebox("panel","PopupPanel", style_pp ); @@ -598,8 +602,8 @@ void make_default_theme() { t->set_color("font_color_disabled","PopupMenu", Color(0.4,0.4,0.4,0.8) ); t->set_color("font_color_hover","PopupMenu", control_font_color ); - t->set_constant("hseparation","PopupMenu",4); - t->set_constant("vseparation","PopupMenu",4); + t->set_constant("hseparation","PopupMenu",4 *scale); + t->set_constant("vseparation","PopupMenu",4 *scale); // GraphNode @@ -614,14 +618,14 @@ void make_default_theme() { t->set_stylebox("selectedframe","GraphNode", graphsbselected ); t->set_stylebox("defaultframe", "GraphNode", graphsbdefault ); t->set_stylebox("defaultfocus", "GraphNode", graphsbdeffocus ); - t->set_constant("separation","GraphNode", 1 ); + t->set_constant("separation","GraphNode", 1 *scale); t->set_icon("port","GraphNode", make_icon( graph_port_png ) ); t->set_icon("close","GraphNode", make_icon( graph_node_close_png ) ); t->set_font("title_font","GraphNode", default_font ); t->set_color("title_color","GraphNode", Color(0,0,0,1)); - t->set_constant("title_offset","GraphNode", 18); - t->set_constant("close_offset","GraphNode", 18); - t->set_constant("port_offset","GraphNode", 3); + t->set_constant("title_offset","GraphNode", 18 *scale); + t->set_constant("close_offset","GraphNode", 18 *scale); + t->set_constant("port_offset","GraphNode", 3 *scale); // Tree @@ -658,11 +662,11 @@ void make_default_theme() { t->set_color("guide_color","Tree", Color(0,0,0,0.1) ); t->set_color("drop_position_color","Tree", Color(1,0.3,0.2) ); - t->set_constant("hseparation","Tree",4); - t->set_constant("vseparation","Tree",4); - t->set_constant("guide_width","Tree",2); - t->set_constant("item_margin","Tree",12); - t->set_constant("button_margin","Tree",4); + t->set_constant("hseparation","Tree",4 *scale); + t->set_constant("vseparation","Tree",4 *scale); + t->set_constant("guide_width","Tree",2 *scale); + t->set_constant("item_margin","Tree",12 *scale); + t->set_constant("button_margin","Tree",4 *scale); // ItemList @@ -674,7 +678,7 @@ void make_default_theme() { t->set_constant("hseparation","ItemList",4); t->set_constant("vseparation","ItemList",2); t->set_constant("icon_margin","ItemList",4); - t->set_constant("line_separation","ItemList",2); + t->set_constant("line_separation","ItemList",2 *scale); t->set_font("font","ItemList", default_font ); t->set_color("font_color","ItemList", control_font_color_lower ); t->set_color("font_color_selected","ItemList", control_font_color_pressed ); @@ -695,8 +699,8 @@ void make_default_theme() { Ref<StyleBoxTexture> tc_sb = sb_expand( make_stylebox( tab_container_bg_png,4,4,4,4,4,4,4,4),3,3,3,3); - tc_sb->set_expand_margin_size(MARGIN_TOP,2); - tc_sb->set_default_margin(MARGIN_TOP,8); + tc_sb->set_expand_margin_size(MARGIN_TOP,2 *scale); + tc_sb->set_default_margin(MARGIN_TOP,8 *scale); t->set_stylebox("tab_fg","TabContainer", sb_expand( make_stylebox( tab_current_png,4,4,4,1,16,4,16,4),2,2,2,2) ); t->set_stylebox("tab_bg","TabContainer", sb_expand( make_stylebox( tab_behind_png,5,5,5,1,16,6,16,4),3,0,3,3) ); @@ -714,11 +718,11 @@ void make_default_theme() { t->set_color("font_color_fg","TabContainer", control_font_color_hover ); t->set_color("font_color_bg","TabContainer", control_font_color_low ); - t->set_constant("side_margin","TabContainer", 8 ); - t->set_constant("top_margin","TabContainer", 24); - t->set_constant("label_valign_fg","TabContainer", 0); - t->set_constant("label_valign_bg","TabContainer", 2); - t->set_constant("hseparation","TabContainer", 4); + t->set_constant("side_margin","TabContainer", 8 *scale); + t->set_constant("top_margin","TabContainer", 24 *scale); + t->set_constant("label_valign_fg","TabContainer", 0 *scale); + t->set_constant("label_valign_bg","TabContainer", 2 *scale); + t->set_constant("hseparation","TabContainer", 4 *scale); @@ -741,10 +745,10 @@ void make_default_theme() { t->set_color("font_color_fg","Tabs", control_font_color_hover ); t->set_color("font_color_bg","Tabs", control_font_color_low ); - t->set_constant("top_margin","Tabs", 24); - t->set_constant("label_valign_fg","Tabs", 0); - t->set_constant("label_valign_bg","Tabs", 2); - t->set_constant("hseparation","Tabs", 4); + t->set_constant("top_margin","Tabs", 24 *scale); + t->set_constant("label_valign_fg","Tabs", 0 *scale); + t->set_constant("label_valign_bg","Tabs", 2 *scale); + t->set_constant("hseparation","Tabs", 4 *scale); @@ -754,18 +758,17 @@ void make_default_theme() { t->set_stylebox("separator","VSeparator", make_stylebox( hseparator_png,3,3,3,3) ); t->set_icon("close","Icons", make_icon(icon_close_png)); - t->set_font("source","Fonts", source_font); t->set_font("normal","Fonts", default_font ); t->set_font("large","Fonts", large_font ); - t->set_constant("separation","HSeparator", 4); - t->set_constant("separation","VSeparator", 4); + t->set_constant("separation","HSeparator", 4 *scale); + t->set_constant("separation","VSeparator", 4 *scale); // Dialogs - t->set_constant("margin","Dialogs",8); - t->set_constant("button_margin","Dialogs",32); + t->set_constant("margin","Dialogs",8 *scale); + t->set_constant("button_margin","Dialogs",32 *scale); @@ -778,11 +781,11 @@ void make_default_theme() { // colorPicker - t->set_constant("value_height","ColorPicker", 23 ); - t->set_constant("value_width","ColorPicker", 50); - t->set_constant("color_width","ColorPicker", 100); - t->set_constant("label_width","ColorPicker", 20); - t->set_constant("hseparator","ColorPicker", 4); + t->set_constant("value_height","ColorPicker", 23 *scale); + t->set_constant("value_width","ColorPicker", 50 *scale); + t->set_constant("color_width","ColorPicker", 100 *scale); + t->set_constant("label_width","ColorPicker", 20 *scale); + t->set_constant("hseparator","ColorPicker", 4 *scale); t->set_icon("screen_picker","ColorPicker", make_icon( icon_color_pick_png ) ); t->set_icon("add_preset","ColorPicker", make_icon( icon_add_png ) ); @@ -794,7 +797,7 @@ void make_default_theme() { Ref<StyleBoxTexture> style_tt = make_stylebox( tooltip_bg_png,4,4,4,4); for(int i=0;i<4;i++) - style_tt->set_expand_margin_size((Margin)i,4); + style_tt->set_expand_margin_size((Margin)i,4 *scale); t->set_stylebox("panel","TooltipPanel", style_tt ); @@ -822,9 +825,9 @@ void make_default_theme() { t->set_color("font_color_selected","RichTextLabel", font_color_selection ); t->set_color("selection_color","RichTextLabel", Color(0.1,0.1,1,0.8) ); - t->set_constant("line_separation","RichTextLabel", 1 ); - t->set_constant("table_hseparation","RichTextLabel", 3 ); - t->set_constant("table_vseparation","RichTextLabel", 3 ); + t->set_constant("line_separation","RichTextLabel", 1 *scale); + t->set_constant("table_hseparation","RichTextLabel", 3 *scale); + t->set_constant("table_vseparation","RichTextLabel", 3 *scale); @@ -836,18 +839,18 @@ void make_default_theme() { t->set_icon("grabber","VSplitContainer",make_icon(vsplitter_png)); t->set_icon("grabber","HSplitContainer",make_icon(hsplitter_png)); - t->set_constant("separation","HBoxContainer",4); - t->set_constant("separation","VBoxContainer",4); - t->set_constant("margin_left","MarginContainer",8); - t->set_constant("margin_top","MarginContainer",0); - t->set_constant("margin_right","MarginContainer",0); - t->set_constant("margin_bottom","MarginContainer",0); - t->set_constant("hseparation","GridContainer",4); - t->set_constant("vseparation","GridContainer",4); - t->set_constant("separation","HSplitContainer",12); - t->set_constant("separation","VSplitContainer",12); - t->set_constant("autohide","HSplitContainer",1); - t->set_constant("autohide","VSplitContainer",1); + t->set_constant("separation","HBoxContainer",4 *scale); + t->set_constant("separation","VBoxContainer",4 *scale); + t->set_constant("margin_left","MarginContainer",8 *scale); + t->set_constant("margin_top","MarginContainer",0 *scale); + t->set_constant("margin_right","MarginContainer",0 *scale); + t->set_constant("margin_bottom","MarginContainer",0 *scale); + t->set_constant("hseparation","GridContainer",4 *scale); + t->set_constant("vseparation","GridContainer",4 *scale); + t->set_constant("separation","HSplitContainer",12 *scale); + t->set_constant("separation","VSplitContainer",12 *scale); + t->set_constant("autohide","HSplitContainer",1 *scale); + t->set_constant("autohide","VSplitContainer",1 *scale); @@ -863,8 +866,8 @@ void make_default_theme() { t->set_color("font_color","HButtonArray", control_font_color_low ); t->set_color("font_color_selected","HButtonArray", control_font_color_hover ); - t->set_constant("icon_separator","HButtonArray", 4 ); - t->set_constant("button_separator","HButtonArray", 8 ); + t->set_constant("icon_separator","HButtonArray", 4 *scale ); + t->set_constant("button_separator","HButtonArray", 8 *scale ); t->set_stylebox("focus","HButtonArray", focus ); @@ -881,8 +884,8 @@ void make_default_theme() { t->set_color("font_color","VButtonArray", control_font_color_low ); t->set_color("font_color_selected","VButtonArray", control_font_color_hover ); - t->set_constant("icon_separator","VButtonArray", 4); - t->set_constant("button_separator","VButtonArray", 8); + t->set_constant("icon_separator","VButtonArray", 4 *scale); + t->set_constant("button_separator","VButtonArray", 8 *scale); t->set_stylebox("focus","VButtonArray", focus ); @@ -914,45 +917,31 @@ void make_default_theme() { // Theme - Theme::set_default( t ); - Theme::set_default_icon( make_icon(error_icon_png) ); - Theme::set_default_style( make_stylebox( error_icon_png,2,2,2,2) ); - Theme::set_default_font( default_font ); + default_icon= make_icon(error_icon_png) ; + default_style = make_stylebox( error_icon_png,2,2,2,2) ; memdelete( tex_cache ); } -#else - -#include "error_icon.xpm" - void make_default_theme() { - Ref<Theme> t( memnew( Theme ) ); - + Ref<Theme> t; + t.instance(); - Image error_img(error_icon_xpm); - Ref<Texture> texture( memnew( Texture ) ); - texture->create_from_image( error_img ); - - Ref<StyleBoxTexture> style( memnew( StyleBoxTexture ) ); - style->set_texture(texture); - - for(int i=0;i<4;i++) { - style->set_margin_size( Margin(),8); - style->set_default_margin( Margin(),8); - } + Ref<StyleBox> default_style; + Ref<Texture> default_icon; + 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<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); + fill_default_theme(t,default_font,large_font,default_icon,default_style,false); - Ref<BitmapFont> f = make_default_font(); Theme::set_default( t ); - Theme::set_default_icon( texture ); - Theme::set_default_style( style ); - Theme::set_default_font( f ); + Theme::set_default_icon( default_icon ); + Theme::set_default_style( default_style ); + Theme::set_default_font( default_font ); } -#endif void clear_default_theme() { Theme::set_default( Ref<Theme>() ); diff --git a/scene/resources/default_theme/default_theme.h b/scene/resources/default_theme/default_theme.h index 44569ba192..1e3b4b4081 100644 --- a/scene/resources/default_theme/default_theme.h +++ b/scene/resources/default_theme/default_theme.h @@ -12,10 +12,12 @@ #ifndef DEFAULT_THEME_H #define DEFAULT_THEME_H +#include "scene/resources/theme.h" /** @author Juan Linietsky <reduzio@gmail.com> */ +void fill_default_theme(Ref<Theme>& theme,const Ref<Font> & default_font,const Ref<Font> & large_font,Ref<Texture>& default_icon, Ref<StyleBox>& default_style,bool p_hidpi); void make_default_theme(); void clear_default_theme(); diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index 19aa0e79cc..78a5571bf0 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -1,65 +1,11 @@ +#ifdef FREETYPE_ENABLED #include "dynamic_font.h" -#define STB_TRUETYPE_IMPLEMENTATION -#include "stb_truetype.h" #include "os/file_access.h" -void DynamicFontData::lock() { - fr=font_data.read(); - - if (fr.ptr()!=last_data_ptr) { - - last_data_ptr=fr.ptr(); - - if (!stbtt_InitFont(&info, last_data_ptr, 0)) { - valid=false; - } else { - valid=true; - } - - last_data_ptr=fr.ptr(); - } -} - -void DynamicFontData::unlock() { - - fr = DVector<uint8_t>::Read(); -} - -void DynamicFontData::set_font_data(const DVector<uint8_t>& p_font) { - //clear caches and stuff - ERR_FAIL_COND(font_data.size()) ; - font_data=p_font; - - lock(); - - if (valid) { - stbtt_GetFontVMetrics(&info, &ascent, &descent, &linegap); - descent=-descent + linegap; - - for(int i=32;i<1024;i++) { - for(int j=32;j<1024;j++) { - - int kern = stbtt_GetCodepointKernAdvance(&info, i,j); - if (kern!=0) { - KerningPairKey kpk; - kpk.A=i; - kpk.B=j; - kerning_map[kpk]=kern; - } - } - } - } - - unlock(); - //clear existing stuff - - ERR_FAIL_COND(!valid); -} Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) { - ERR_FAIL_COND_V(!valid,Ref<DynamicFontAtSize>()); if (size_cache.has(p_size)) { return Ref<DynamicFontAtSize>( size_cache[p_size] ); @@ -67,6 +13,7 @@ Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) { Ref<DynamicFontAtSize> dfas; + dfas.instance(); dfas->font=Ref<DynamicFontData>( this ); @@ -74,21 +21,34 @@ Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) { size_cache[p_size]=dfas.ptr(); dfas->size=p_size; + dfas->_load(); - lock(); + return dfas; - dfas->scale = stbtt_ScaleForPixelHeight(&info, p_size); +} - unlock(); +void DynamicFontData::set_font_ptr(const uint8_t* p_font_mem,int p_font_mem_size) { - return dfas; + font_mem=p_font_mem; + font_mem_size=p_font_mem_size; +} +void DynamicFontData::set_font_path(const String& p_path) { + + font_path=p_path; +} + +void DynamicFontData::set_force_autohinter(bool p_force) { + + force_autohinter=p_force; } DynamicFontData::DynamicFontData() { - last_data_ptr=NULL; - valid=false; + + force_autohinter=false; + font_mem=NULL; + font_mem_size=0; } DynamicFontData::~DynamicFontData() @@ -100,79 +60,291 @@ DynamicFontData::~DynamicFontData() //////////////////// +Error DynamicFontAtSize::_load() { + + + int error = FT_Init_FreeType( &library ); + + ERR_EXPLAIN(TTR("Error initializing FreeType.")); + ERR_FAIL_COND_V( error !=0, ERR_CANT_CREATE ); + + if (font->font_path!=String()) { + + FileAccess *f=FileAccess::open(font->font_path,FileAccess::READ); + ERR_FAIL_COND_V(!f,ERR_CANT_OPEN); + + memset(&stream,0,sizeof(FT_StreamRec)); + stream.base=NULL; + stream.size=f->get_len(); + stream.pos=0; + stream.descriptor.pointer=f; + stream.read=_ft_stream_io; + stream.close=_ft_stream_close; + + FT_Open_Args fargs; + memset(&fargs,0,sizeof(FT_Open_Args)); + fargs.flags=FT_OPEN_STREAM; + fargs.stream=&stream; + error = FT_Open_Face( library,&fargs,0,&face); + } else if (font->font_mem) { + + memset(&stream,0,sizeof(FT_StreamRec)); + stream.base=(unsigned char*)font->font_mem; + stream.size=font->font_mem_size; + stream.pos=0; + + FT_Open_Args fargs; + memset(&fargs,0,sizeof(FT_Open_Args)); + fargs.memory_base=(unsigned char*)font->font_mem; + fargs.memory_size=font->font_mem_size; + fargs.flags= FT_OPEN_MEMORY; + fargs.stream=&stream; + error = FT_Open_Face( library,&fargs,0,&face); + + } else { + ERR_EXPLAIN("DynamicFont uninitialized"); + ERR_FAIL_V(ERR_UNCONFIGURED); + } + + //error = FT_New_Face( library, src_path.utf8().get_data(),0,&face ); + + if ( error == FT_Err_Unknown_File_Format ) { + ERR_EXPLAIN(TTR("Unknown font format.")); + FT_Done_FreeType( library ); + + } else if ( error ) { + + ERR_EXPLAIN(TTR("Error loading font.")); + FT_Done_FreeType( library ); + + } + + ERR_FAIL_COND_V(error,ERR_FILE_CANT_OPEN); + + + /*error = FT_Set_Char_Size(face,0,64*size,512,512); + + if ( error ) { + FT_Done_FreeType( library ); + ERR_EXPLAIN(TTR("Invalid font size.")); + ERR_FAIL_COND_V( error, ERR_INVALID_PARAMETER ); + }*/ + + error = FT_Set_Pixel_Sizes(face,0,size); + + ascent=face->size->metrics.ascender>>6; + descent=-face->size->metrics.descender>>6; + linegap=0; + + //print_line("ASCENT: "+itos(ascent)+" descent "+itos(descent)+" hinted: "+itos(face->face_flags&FT_FACE_FLAG_HINTER)); + + valid=true; + return OK; +} + float DynamicFontAtSize::get_height() const { - return (font->ascent+font->descent)*scale; + return ascent+descent; } float DynamicFontAtSize::get_ascent() const { - return font->ascent*scale; + return ascent; } float DynamicFontAtSize::get_descent() const { - return font->descent*scale; + return descent; } -Size2 DynamicFontAtSize::get_char_size(CharType p_char,CharType p_next) const { +Size2 DynamicFontAtSize::get_char_size(CharType p_char,CharType p_next,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const { + if (!valid) + return Size2(1,1); const_cast<DynamicFontAtSize*>(this)->_update_char(p_char); const Character *c = char_map.getptr(p_char); ERR_FAIL_COND_V(!c,Size2()); - Size2 ret( c->advance, get_height()); + Size2 ret(0,get_height()); + + if (!c->found) { + + //not found, try in fallbacks + for(int i=0;i<p_fallbacks.size();i++) { + + DynamicFontAtSize *fb = const_cast<DynamicFontAtSize*>(p_fallbacks[i].ptr()); + if (!fb->valid) + continue; + + fb->_update_char(p_char); + const Character *ch = fb->char_map.getptr(p_char); + ERR_CONTINUE(!ch); + + if (!ch->found) + continue; + + c=ch; + break; + } + //not found, try 0xFFFD to display 'not found'. + + if (!c->found) { + + const_cast<DynamicFontAtSize*>(this)->_update_char(0xFFFD); + c = char_map.getptr(0xFFFD); + ERR_FAIL_COND_V(!c,Size2()); + + } + } + + if (c->found) { + ret.x=c->advance; + } + if (p_next) { - DynamicFontData::KerningPairKey kpk; - kpk.A=p_char; - kpk.B=p_next; + FT_Vector delta; + FT_Get_Kerning( face, p_char,p_next, FT_KERNING_DEFAULT, &delta ); + + if (delta.x==0) { + for(int i=0;i<p_fallbacks.size();i++) { + + DynamicFontAtSize *fb = const_cast<DynamicFontAtSize*>(p_fallbacks[i].ptr()); + if (!fb->valid) + continue; - const Map<DynamicFontData::KerningPairKey,int>::Element *K=font->kerning_map.find(kpk); - if (K) { - ret.x+=K->get()*scale; + FT_Get_Kerning( fb->face, p_char,p_next, FT_KERNING_DEFAULT, &delta ); + + if (delta.x==0) + continue; + + ret.x+=delta.x>>6; + break; + } + } else { + ret.x+=delta.x>>6; } + } return ret; } -float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const { +float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const { + + if (!valid) + return 0; const_cast<DynamicFontAtSize*>(this)->_update_char(p_char); const Character * c = char_map.getptr(p_char); - if (!c) { - return 0; + float advance=0; + + if (!c->found) { + + //not found, try in fallbacks + bool used_fallback=false; + + for(int i=0;i<p_fallbacks.size();i++) { + + DynamicFontAtSize *fb = const_cast<DynamicFontAtSize*>(p_fallbacks[i].ptr()); + if (!fb->valid) + continue; + + fb->_update_char(p_char); + const Character *ch = fb->char_map.getptr(p_char); + ERR_CONTINUE(!ch); + + if (!ch->found) + continue; + + Point2 cpos=p_pos; + cpos.x+=ch->h_align; + cpos.y-=get_ascent(); + cpos.y+=ch->v_align; + ERR_FAIL_COND_V( ch->texture_idx<-1 || ch->texture_idx>=fb->textures.size(),0); + if (ch->texture_idx!=-1) + VisualServer::get_singleton()->canvas_item_add_texture_rect_region( p_canvas_item, Rect2( cpos, ch->rect.size ), fb->textures[ch->texture_idx].texture->get_rid(),ch->rect, p_modulate ); + advance=ch->advance; + used_fallback=true; + break; + } + //not found, try 0xFFFD to display 'not found'. + + if (!used_fallback) { + + const_cast<DynamicFontAtSize*>(this)->_update_char(0xFFFD); + c = char_map.getptr(0xFFFD); + + } } - Point2 cpos=p_pos; - cpos.x+=c->h_align; - cpos.y-=get_ascent(); - cpos.y+=c->v_align; - ERR_FAIL_COND_V( c->texture_idx<-1 || c->texture_idx>=textures.size(),0); - if (c->texture_idx!=-1) - VisualServer::get_singleton()->canvas_item_add_texture_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), textures[c->texture_idx].texture->get_rid(),c->rect, p_modulate ); + if (c->found) { + + + Point2 cpos=p_pos; + cpos.x+=c->h_align; + cpos.y-=get_ascent(); + cpos.y+=c->v_align; + ERR_FAIL_COND_V( c->texture_idx<-1 || c->texture_idx>=textures.size(),0); + if (c->texture_idx!=-1) + VisualServer::get_singleton()->canvas_item_add_texture_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), textures[c->texture_idx].texture->get_rid(),c->rect, p_modulate ); + advance=c->advance; + //textures[c->texture_idx].texture->draw(p_canvas_item,Vector2()); + } - //textures[c->texture_idx].texture->draw(p_canvas_item,Vector2()); - float ret = c->advance; if (p_next) { - DynamicFontData::KerningPairKey kpk; - kpk.A=p_char; - kpk.B=p_next; - const Map<DynamicFontData::KerningPairKey,int>::Element *K=font->kerning_map.find(kpk); - if (K) { - ret+=K->get()*scale; + FT_Vector delta; + FT_Get_Kerning( face, p_char,p_next, FT_KERNING_DEFAULT, &delta ); + + if (delta.x==0) { + for(int i=0;i<p_fallbacks.size();i++) { + + DynamicFontAtSize *fb = const_cast<DynamicFontAtSize*>(p_fallbacks[i].ptr()); + if (!fb->valid) + continue; + + FT_Get_Kerning( fb->face, p_char,p_next, FT_KERNING_DEFAULT, &delta ); + + if (delta.x==0) + continue; + + advance+=delta.x>>6; + break; + } + } else { + advance+=delta.x>>6; } + } + + return advance; +} + +unsigned long DynamicFontAtSize::_ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char* buffer, unsigned long count ) { + + + FileAccess *f=(FileAccess*)stream->descriptor.pointer; + + if (f->get_pos()!=offset) { + f->seek(offset); } - return ret; + if (count==0) + return 0; + + return f->get_buffer(buffer,count); +} +void DynamicFontAtSize::_ft_stream_close(FT_Stream stream) { + + FileAccess *f=(FileAccess*)stream->descriptor.pointer; + f->close(); + memdelete(f); } @@ -181,38 +353,59 @@ void DynamicFontAtSize::_update_char(CharType p_char) { if (char_map.has(p_char)) return; - font->lock(); + _THREAD_SAFE_METHOD_ + FT_GlyphSlot slot = face->glyph; - int w,h,xofs,yofs; - unsigned char * cpbitmap = stbtt_GetCodepointBitmap(&font->info, scale, scale, p_char, &w, &h, &xofs, &yofs ); + if (FT_Get_Char_Index( face, p_char)==0) { + //not found + Character ch; + ch.texture_idx=-1; + ch.advance=0; + ch.h_align=0; + ch.v_align=0; + ch.found=false; - if (!cpbitmap) { - //no glyph + char_map[p_char]=ch; + return; + } + int error = FT_Load_Char( face, p_char, FT_LOAD_RENDER|(font->force_autohinter?FT_LOAD_FORCE_AUTOHINT:0) ); + if (!error) { + error = FT_Render_Glyph( face->glyph, ft_render_mode_normal ); + } + if (error) { - int advance; - stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0); + int advance=0; + //stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0); //print_line("char has no bitmap: "+itos(p_char)+" but advance is "+itos(advance*scale)); Character ch; ch.texture_idx=-1; - ch.advance=advance*scale; + ch.advance=advance; ch.h_align=0; ch.v_align=0; + ch.found=false; char_map[p_char]=ch; - font->unlock(); return; } + + + int w = slot->bitmap.width; + int h = slot->bitmap.rows; + int p = slot->bitmap.pitch; + int yofs=slot->bitmap_top; + int xofs=slot->bitmap_left; + int advance=slot->advance.x>>6; + + int mw=w+rect_margin*2; int mh=h+rect_margin*2; if (mw>4096 || mh>4096) { - stbtt_FreeBitmap(cpbitmap,NULL); - font->unlock(); ERR_FAIL_COND(mw>4096); ERR_FAIL_COND(mh>4096); } @@ -304,13 +497,14 @@ void DynamicFontAtSize::_update_char(CharType p_char) { { DVector<uint8_t>::Write wr = tex.imgdata.write(); + for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { int ofs = ( (i+tex_y+rect_margin)*tex.texture_size+j+tex_x+rect_margin)*2; ERR_FAIL_COND(ofs >= tex.imgdata.size()); wr[ofs+0]=255; //grayscale as 1 - wr[ofs+1]=cpbitmap[i*w+j]; //alpha as 0 + wr[ofs+1]=slot->bitmap.buffer[i*slot->bitmap.width+j]; } } } @@ -322,7 +516,7 @@ void DynamicFontAtSize::_update_char(CharType p_char) { if (tex.texture.is_null()) { tex.texture.instance(); - tex.texture->create_from_image(img,Texture::FLAG_FILTER); + tex.texture->create_from_image(img,0/*Texture::FLAG_FILTER*/); } else { tex.texture->set_data(img); //update } @@ -337,14 +531,13 @@ void DynamicFontAtSize::_update_char(CharType p_char) { tex.offsets[k]=tex_y+mh; } - int advance; - stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0); Character chr; chr.h_align=xofs; - chr.v_align=yofs + get_ascent(); - chr.advance=advance*scale; + chr.v_align=ascent-yofs;// + ascent - descent; + chr.advance=advance; chr.texture_idx=tex_index; + chr.found=true; chr.rect=Rect2(tex_x+rect_margin,tex_y+rect_margin,w,h); @@ -353,45 +546,38 @@ void DynamicFontAtSize::_update_char(CharType p_char) { char_map[p_char]=chr; - stbtt_FreeBitmap(cpbitmap,NULL); - - font->unlock(); } DynamicFontAtSize::DynamicFontAtSize() { + valid=false; rect_margin=1; + ascent=1; + descent=1; + linegap=1; } DynamicFontAtSize::~DynamicFontAtSize(){ - ERR_FAIL_COND(!font.ptr()); - font->size_cache.erase(size); + if (valid) { + FT_Done_FreeType( library ); + font->size_cache.erase(size); + } } ///////////////////////// -void DynamicFont::_bind_methods() { - - ObjectTypeDB::bind_method(_MD("set_font_data","data:DynamicFontData"),&DynamicFont::set_font_data); - ObjectTypeDB::bind_method(_MD("get_font_data:DynamicFontData"),&DynamicFont::get_font_data); - - ObjectTypeDB::bind_method(_MD("set_size","data"),&DynamicFont::set_size); - ObjectTypeDB::bind_method(_MD("get_size"),&DynamicFont::get_size); - - ADD_PROPERTY(PropertyInfo(Variant::INT,"size"),_SCS("set_size"),_SCS("get_size")); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data")); -} - void DynamicFont::set_font_data(const Ref<DynamicFontData>& p_data) { data=p_data; data_at_size=data->_get_dynamic_font_at_size(size); + emit_changed(); } + Ref<DynamicFontData> DynamicFont::get_font_data() const{ return data; @@ -406,7 +592,12 @@ void DynamicFont::set_size(int p_size){ if (!data.is_valid()) return; data_at_size=data->_get_dynamic_font_at_size(size); + for(int i=0;i<fallbacks.size();i++) { + fallback_data_at_size[i]=fallbacks[i]->_get_dynamic_font_at_size(size); + } + emit_changed(); + _change_notify(); } int DynamicFont::get_size() const{ @@ -443,7 +634,7 @@ Size2 DynamicFont::get_char_size(CharType p_char,CharType p_next) const{ if (!data_at_size.is_valid()) return Size2(1,1); - return data_at_size->get_char_size(p_char,p_next); + return data_at_size->get_char_size(p_char,p_next,fallback_data_at_size); } @@ -457,10 +648,120 @@ float DynamicFont::draw_char(RID p_canvas_item, const Point2& p_pos, const CharT if (!data_at_size.is_valid()) return 0; - return data_at_size->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate); + return data_at_size->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate,fallback_data_at_size); + +} +void DynamicFont::set_fallback(int p_idx,const Ref<DynamicFontData>& p_data) { + + ERR_FAIL_COND(p_data.is_null()); + ERR_FAIL_INDEX(p_idx,fallbacks.size()); + fallbacks[p_idx]=p_data; + fallback_data_at_size[p_idx]=fallbacks[p_idx]->_get_dynamic_font_at_size(size); } +void DynamicFont::add_fallback(const Ref<DynamicFontData>& p_data) { + + ERR_FAIL_COND(p_data.is_null()); + fallbacks.push_back(p_data); + fallback_data_at_size.push_back(fallbacks[fallbacks.size()-1]->_get_dynamic_font_at_size(size)); //const.. + + _change_notify(); + emit_changed(); + _change_notify(); + +} + +int DynamicFont::get_fallback_count() const { + return fallbacks.size(); +} +Ref<DynamicFontData> DynamicFont::get_fallback(int p_idx) const { + + ERR_FAIL_INDEX_V(p_idx,fallbacks.size(),Ref<DynamicFontData>()); + + return fallbacks[p_idx]; +} +void DynamicFont::remove_fallback(int p_idx) { + + ERR_FAIL_INDEX(p_idx,fallbacks.size()); + fallbacks.remove(p_idx); + fallback_data_at_size.remove(p_idx); + emit_changed(); + _change_notify(); +} + +bool DynamicFont::_set(const StringName& p_name, const Variant& p_value) { + + String str = p_name; + if (str.begins_with("fallback/")) { + int idx = str.get_slicec('/',1).to_int(); + Ref<DynamicFontData> fd = p_value; + + if (fd.is_valid()) { + if (idx==fallbacks.size()) { + add_fallback(fd); + return true; + } else if (idx>=0 && idx<fallbacks.size()) { + set_fallback(idx,fd); + return true; + } else { + return false; + } + } else if (idx>=0 && idx<fallbacks.size()) { + remove_fallback(idx); + return true; + } + } + + return false; +} + +bool DynamicFont::_get(const StringName& p_name,Variant &r_ret) const{ + + String str = p_name; + if (str.begins_with("fallback/")) { + int idx = str.get_slicec('/',1).to_int(); + + if (idx==fallbacks.size()) { + r_ret=Ref<DynamicFontData>(); + return true; + } else if (idx>=0 && idx<fallbacks.size()) { + r_ret=get_fallback(idx); + return true; + } + } + + return false; +} +void DynamicFont::_get_property_list( List<PropertyInfo> *p_list) const{ + + for(int i=0;i<fallbacks.size();i++) { + p_list->push_back(PropertyInfo(Variant::OBJECT,"fallback/"+itos(i),PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData")); + } + + p_list->push_back(PropertyInfo(Variant::OBJECT,"fallback/"+itos(fallbacks.size()),PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData")); +} + + +void DynamicFont::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_font_data","data:DynamicFontData"),&DynamicFont::set_font_data); + ObjectTypeDB::bind_method(_MD("get_font_data:DynamicFontData"),&DynamicFont::get_font_data); + + ObjectTypeDB::bind_method(_MD("set_size","data"),&DynamicFont::set_size); + ObjectTypeDB::bind_method(_MD("get_size"),&DynamicFont::get_size); + + 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("get_fallback:DynamicFontData","idx"),&DynamicFont::get_fallback); + ObjectTypeDB::bind_method(_MD("remove_fallback","idx"),&DynamicFont::remove_fallback); + ObjectTypeDB::bind_method(_MD("get_fallback_count"),&DynamicFont::get_fallback_count); + + + ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size")); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data")); +} + DynamicFont::DynamicFont() { size=16; @@ -478,34 +779,21 @@ RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String& p_ if (r_error) *r_error=ERR_FILE_CANT_OPEN; + Ref<DynamicFontData> dfont; + dfont.instance();; + dfont->set_font_path(p_path); - FileAccess *f = FileAccess::open(p_path,FileAccess::READ); - ERR_FAIL_COND_V(!f,RES()); - - DVector<uint8_t> data; - - data.resize(f->get_len()); - - ERR_FAIL_COND_V(data.size()==0,RES()); - - { - DVector<uint8_t>::Write w = data.write(); - f->get_buffer(w.ptr(),data.size()); - } - - Ref<DynamicFontData> dfd; - dfd.instance(); - dfd->set_font_data(data); if (r_error) *r_error=OK; - return dfd; + return dfont; } void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List<String> *p_extensions) const { p_extensions->push_back("ttf"); + p_extensions->push_back("otf"); } bool ResourceFormatLoaderDynamicFont::handles_type(const String& p_type) const { @@ -516,8 +804,10 @@ bool ResourceFormatLoaderDynamicFont::handles_type(const String& p_type) const { String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const { String el = p_path.extension().to_lower(); - if (el=="ttf") + if (el=="ttf" || el=="otf") return "DynamicFontData"; return ""; } + +#endif diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h index ba7249a7b7..1a46e1e468 100644 --- a/scene/resources/dynamic_font.h +++ b/scene/resources/dynamic_font.h @@ -1,10 +1,14 @@ -#ifndef DYNAMICFONT_H -#define DYNAMICFONT_H +#ifndef DYNAMIC_FONT_H +#define DYNAMIC_FONT_H -#include "font.h" -#include "stb_truetype.h" +#ifdef FREETYPE_ENABLED +#include "scene/resources/font.h" +#include "os/thread_safe.h" #include "io/resource_loader.h" +#include <ft2build.h> +#include FT_FREETYPE_H + class DynamicFontAtSize; class DynamicFont; @@ -13,39 +17,16 @@ class DynamicFontData : public Resource { OBJ_TYPE(DynamicFontData,Resource); - bool valid; - - DVector<uint8_t> font_data; - DVector<uint8_t>::Read fr; - const uint8_t* last_data_ptr; - struct KerningPairKey { - - union { - struct { - uint32_t A,B; - }; - - uint64_t pair; - }; - - _FORCE_INLINE_ bool operator<(const KerningPairKey& p_r) const { return pair<p_r.pair; } - }; - - Map<KerningPairKey,int> kerning_map; + const uint8_t *font_mem; + int font_mem_size; + bool force_autohinter; + String font_path; Map<int,DynamicFontAtSize*> size_cache; -friend class DynamicFontAtSize; - - stbtt_fontinfo info; - int ascent; - int descent; - int linegap; - - void lock(); - void unlock(); + friend class DynamicFontAtSize; friend class DynamicFont; @@ -53,7 +34,10 @@ friend class DynamicFont; Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size); public: - void set_font_data(const DVector<uint8_t>& p_font); + void set_font_ptr(const uint8_t* p_font_mem,int p_font_mem_size); + void set_font_path(const String& p_path); + void set_force_autohinter(bool p_force); + DynamicFontData(); ~DynamicFontData(); }; @@ -61,11 +45,21 @@ public: class DynamicFontAtSize : public Reference { - OBJ_TYPE(DynamicFontAtSize,Reference); + OBJ_TYPE(DynamicFontAtSize,Reference) + + _THREAD_SAFE_CLASS_ + FT_Library library; /* handle to library */ + FT_Face face; /* handle to face object */ + FT_StreamRec stream; + int ascent; + int descent; + int linegap; int rect_margin; + bool valid; + struct CharTexture { DVector<uint8_t> imgdata; @@ -78,6 +72,7 @@ class DynamicFontAtSize : public Reference { struct Character { + bool found; int texture_idx; Rect2 rect; float v_align; @@ -88,6 +83,8 @@ class DynamicFontAtSize : public Reference { }; + static unsigned long _ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char* buffer, unsigned long count ); + static void _ft_stream_close(FT_Stream stream); HashMap< CharType, Character > char_map; @@ -95,21 +92,26 @@ class DynamicFontAtSize : public Reference { friend class DynamicFontData; Ref<DynamicFontData> font; - float scale; int size; + + + Error _load(); protected: + + public: + float get_height() const; float get_ascent() const; float get_descent() const; - Size2 get_char_size(CharType p_char,CharType p_next=0) const; + Size2 get_char_size(CharType p_char,CharType p_next,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) 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,const Color& p_modulate,const Vector<Ref<DynamicFontAtSize> >& p_fallbacks) const; @@ -123,13 +125,22 @@ class DynamicFont : public Font { OBJ_TYPE( DynamicFont, Font ); - Ref<DynamicFontData> data; + Ref<DynamicFontData> data; Ref<DynamicFontAtSize> data_at_size; - int size; + Vector< Ref<DynamicFontData> > fallbacks; + Vector< Ref<DynamicFontAtSize> > fallback_data_at_size; + + + int size; + bool valid; protected: + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List<PropertyInfo> *p_list) const; + static void _bind_methods(); public: @@ -140,6 +151,13 @@ public: void set_size(int p_size); int get_size() const; + + void add_fallback(const Ref<DynamicFontData>& p_data); + void set_fallback(int p_idx,const Ref<DynamicFontData>& p_data); + int get_fallback_count() const; + Ref<DynamicFontData> get_fallback(int p_idx) const; + void remove_fallback(int p_idx); + virtual float get_height() const; virtual float get_ascent() const; @@ -171,5 +189,6 @@ public: }; +#endif -#endif // DYNAMICFONT_H +#endif diff --git a/scene/resources/dynamic_font_stb.cpp b/scene/resources/dynamic_font_stb.cpp new file mode 100644 index 0000000000..0b9f95da4f --- /dev/null +++ b/scene/resources/dynamic_font_stb.cpp @@ -0,0 +1,527 @@ +#include "dynamic_font_stb.h" + +#ifndef FREETYPE_ENABLED + +#define STB_TRUETYPE_IMPLEMENTATION +#include "stb_truetype.h" +#include "os/file_access.h" + +void DynamicFontData::lock() { + + fr=font_data.read(); + + if (fr.ptr()!=last_data_ptr) { + + last_data_ptr=fr.ptr(); + + if (!stbtt_InitFont(&info, last_data_ptr, 0)) { + valid=false; + } else { + valid=true; + } + + last_data_ptr=fr.ptr(); + } +} + +void DynamicFontData::unlock() { + + fr = DVector<uint8_t>::Read(); +} + +void DynamicFontData::set_font_data(const DVector<uint8_t>& p_font) { + //clear caches and stuff + ERR_FAIL_COND(font_data.size()) ; + font_data=p_font; + + lock(); + + if (valid) { + stbtt_GetFontVMetrics(&info, &ascent, &descent, &linegap); + descent=-descent + linegap; + + for(int i=32;i<1024;i++) { + for(int j=32;j<1024;j++) { + + int kern = stbtt_GetCodepointKernAdvance(&info, i,j); + if (kern!=0) { + KerningPairKey kpk; + kpk.A=i; + kpk.B=j; + kerning_map[kpk]=kern; + } + } + } + } + + unlock(); + //clear existing stuff + + ERR_FAIL_COND(!valid); +} + +Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(int p_size) { + + ERR_FAIL_COND_V(!valid,Ref<DynamicFontAtSize>()); + + if (size_cache.has(p_size)) { + return Ref<DynamicFontAtSize>( size_cache[p_size] ); + } + + + Ref<DynamicFontAtSize> dfas; + dfas.instance(); + + dfas->font=Ref<DynamicFontData>( this ); + + size_cache[p_size]=dfas.ptr(); + + dfas->size=p_size; + + lock(); + + dfas->scale = stbtt_ScaleForPixelHeight(&info, p_size); + + unlock(); + + return dfas; + +} + +DynamicFontData::DynamicFontData() +{ + last_data_ptr=NULL; + valid=false; +} + +DynamicFontData::~DynamicFontData() +{ + +} + + + +//////////////////// + +float DynamicFontAtSize::get_height() const { + + return (font->ascent+font->descent)*scale; +} + +float DynamicFontAtSize::get_ascent() const { + + return font->ascent*scale; +} +float DynamicFontAtSize::get_descent() const { + + return font->descent*scale; +} + +Size2 DynamicFontAtSize::get_char_size(CharType p_char,CharType p_next) const { + + const_cast<DynamicFontAtSize*>(this)->_update_char(p_char); + + const Character *c = char_map.getptr(p_char); + ERR_FAIL_COND_V(!c,Size2()); + + Size2 ret( c->advance, get_height()); + + if (p_next) { + DynamicFontData::KerningPairKey kpk; + kpk.A=p_char; + kpk.B=p_next; + + const Map<DynamicFontData::KerningPairKey,int>::Element *K=font->kerning_map.find(kpk); + if (K) { + ret.x+=K->get()*scale; + } + + } + + return ret; +} + + +float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const { + + const_cast<DynamicFontAtSize*>(this)->_update_char(p_char); + + const Character * c = char_map.getptr(p_char); + + if (!c) { + return 0; + } + + Point2 cpos=p_pos; + cpos.x+=c->h_align; + cpos.y-=get_ascent(); + cpos.y+=c->v_align; + ERR_FAIL_COND_V( c->texture_idx<-1 || c->texture_idx>=textures.size(),0); + if (c->texture_idx!=-1) + VisualServer::get_singleton()->canvas_item_add_texture_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), textures[c->texture_idx].texture->get_rid(),c->rect, p_modulate ); + + //textures[c->texture_idx].texture->draw(p_canvas_item,Vector2()); + + float ret = c->advance; + if (p_next) { + DynamicFontData::KerningPairKey kpk; + kpk.A=p_char; + kpk.B=p_next; + + const Map<DynamicFontData::KerningPairKey,int>::Element *K=font->kerning_map.find(kpk); + if (K) { + ret+=K->get()*scale; + } + + } + + return ret; +} + + +void DynamicFontAtSize::_update_char(CharType p_char) { + + if (char_map.has(p_char)) + return; + + font->lock(); + + + int w,h,xofs,yofs; + unsigned char * cpbitmap = stbtt_GetCodepointBitmap(&font->info, scale, scale, p_char, &w, &h, &xofs, &yofs ); + + if (!cpbitmap) { + //no glyph + + int advance; + stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0); + //print_line("char has no bitmap: "+itos(p_char)+" but advance is "+itos(advance*scale)); + Character ch; + ch.texture_idx=-1; + ch.advance=advance*scale; + ch.h_align=0; + ch.v_align=0; + + char_map[p_char]=ch; + + font->unlock(); + + return; + } + + int mw=w+rect_margin*2; + int mh=h+rect_margin*2; + + if (mw>4096 || mh>4096) { + + stbtt_FreeBitmap(cpbitmap,NULL); + font->unlock(); + ERR_FAIL_COND(mw>4096); + ERR_FAIL_COND(mh>4096); + } + + //find a texture to fit this... + + int tex_index=-1; + int tex_x=0; + int tex_y=0; + + for(int i=0;i<textures.size();i++) { + + CharTexture &ct=textures[i]; + + if (mw > ct.texture_size || mh > ct.texture_size) //too big for this texture + continue; + + tex_y=0x7FFFFFFF; + tex_x=0; + + for(int j=0;j<ct.texture_size-mw;j++) { + + int max_y=0; + + for(int k=j;k<j+mw;k++) { + + int y = ct.offsets[k]; + if (y>max_y) + max_y=y; + } + + if (max_y<tex_y) { + tex_y=max_y; + tex_x=j; + } + } + + if (tex_y==0x7FFFFFFF || tex_y+mh > ct.texture_size) + continue; //fail, could not fit it here + + tex_index=i; + break; + } + +// print_line("CHAR: "+String::chr(p_char)+" TEX INDEX: "+itos(tex_index)+" X: "+itos(tex_x)+" Y: "+itos(tex_y)); + + if (tex_index==-1) { + //could not find texture to fit, create one + tex_x = 0; + tex_y = 0; + + int texsize = MAX(size*8,256); + if (mw>texsize) + texsize=mw; //special case, adapt to it? + if (mh>texsize) + texsize=mh; //special case, adapt to it? + + texsize=nearest_power_of_2(texsize); + + texsize=MIN(texsize,4096); + + + CharTexture tex; + tex.texture_size=texsize; + tex.imgdata.resize(texsize*texsize*2); //grayscale alpha + + { + //zero texture + DVector<uint8_t>::Write w = tex.imgdata.write(); + ERR_FAIL_COND(texsize*texsize*2 > tex.imgdata.size()); + for(int i=0;i<texsize*texsize*2;i++) { + w[i]=0; + } + } + tex.offsets.resize(texsize); + for(int i=0;i<texsize;i++) //zero offsets + tex.offsets[i]=0; + + textures.push_back(tex); + tex_index=textures.size()-1; + + } + + + //fit character in char texture + + CharTexture &tex=textures[tex_index]; + + { + DVector<uint8_t>::Write wr = tex.imgdata.write(); + + for(int i=0;i<h;i++) { + for(int j=0;j<w;j++) { + + int ofs = ( (i+tex_y+rect_margin)*tex.texture_size+j+tex_x+rect_margin)*2; + ERR_FAIL_COND(ofs >= tex.imgdata.size()); + wr[ofs+0]=255; //grayscale as 1 + wr[ofs+1]=cpbitmap[i*w+j]; //alpha as 0 + } + } + } + + //blit to image and texture + { + + Image img(tex.texture_size,tex.texture_size,0,Image::FORMAT_GRAYSCALE_ALPHA,tex.imgdata); + + if (tex.texture.is_null()) { + tex.texture.instance(); + tex.texture->create_from_image(img,Texture::FLAG_FILTER); + } else { + tex.texture->set_data(img); //update + } + + } + + + // update height array + + for(int k=tex_x;k<tex_x+mw;k++) { + + tex.offsets[k]=tex_y+mh; + } + + int advance; + stbtt_GetCodepointHMetrics(&font->info, p_char, &advance, 0); + + Character chr; + chr.h_align=xofs; + chr.v_align=yofs + get_ascent(); + chr.advance=advance*scale; + chr.texture_idx=tex_index; + + + chr.rect=Rect2(tex_x+rect_margin,tex_y+rect_margin,w,h); + + //print_line("CHAR: "+String::chr(p_char)+" TEX INDEX: "+itos(tex_index)+" RECT: "+chr.rect+" X OFS: "+itos(xofs)+" Y OFS: "+itos(yofs)); + + char_map[p_char]=chr; + + stbtt_FreeBitmap(cpbitmap,NULL); + + font->unlock(); + +} + +DynamicFontAtSize::DynamicFontAtSize() { + + rect_margin=1; +} + +DynamicFontAtSize::~DynamicFontAtSize(){ + + ERR_FAIL_COND(!font.ptr()); + font->size_cache.erase(size); +} + +///////////////////////// + + +void DynamicFont::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_font_data","data:DynamicFontData"),&DynamicFont::set_font_data); + ObjectTypeDB::bind_method(_MD("get_font_data:DynamicFontData"),&DynamicFont::get_font_data); + + ObjectTypeDB::bind_method(_MD("set_size","data"),&DynamicFont::set_size); + ObjectTypeDB::bind_method(_MD("get_size"),&DynamicFont::get_size); + + ADD_PROPERTY(PropertyInfo(Variant::INT,"font/size"),_SCS("set_size"),_SCS("get_size")); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"font/font",PROPERTY_HINT_RESOURCE_TYPE,"DynamicFontData"),_SCS("set_font_data"),_SCS("get_font_data")); +} + + +void DynamicFont::set_font_data(const Ref<DynamicFontData>& p_data) { + + data=p_data; + data_at_size=data->_get_dynamic_font_at_size(size); +} + +Ref<DynamicFontData> DynamicFont::get_font_data() const{ + + return data; +} + +void DynamicFont::set_size(int p_size){ + + if (size==p_size) + return; + size=p_size; + ERR_FAIL_COND(p_size<1); + if (!data.is_valid()) + return; + data_at_size=data->_get_dynamic_font_at_size(size); + +} +int DynamicFont::get_size() const{ + + return size; +} + +float DynamicFont::get_height() const{ + + if (!data_at_size.is_valid()) + return 1; + + return data_at_size->get_height(); +} + +float DynamicFont::get_ascent() const{ + + if (!data_at_size.is_valid()) + return 1; + + return data_at_size->get_ascent(); +} + +float DynamicFont::get_descent() const{ + + if (!data_at_size.is_valid()) + return 1; + + return data_at_size->get_descent(); + +} + +Size2 DynamicFont::get_char_size(CharType p_char,CharType p_next) const{ + + if (!data_at_size.is_valid()) + return Size2(1,1); + + return data_at_size->get_char_size(p_char,p_next); + +} + +bool DynamicFont::is_distance_field_hint() const{ + + return false; +} + +float DynamicFont::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const { + + if (!data_at_size.is_valid()) + return 0; + + return data_at_size->draw_char(p_canvas_item,p_pos,p_char,p_next,p_modulate); + +} + +DynamicFont::DynamicFont() { + + size=16; +} + +DynamicFont::~DynamicFont() { + +} + +///////////////////////// + + +RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String& p_original_path, Error *r_error) { + + if (r_error) + *r_error=ERR_FILE_CANT_OPEN; + + + FileAccess *f = FileAccess::open(p_path,FileAccess::READ); + ERR_FAIL_COND_V(!f,RES()); + + DVector<uint8_t> data; + + data.resize(f->get_len()); + + ERR_FAIL_COND_V(data.size()==0,RES()); + + { + DVector<uint8_t>::Write w = data.write(); + f->get_buffer(w.ptr(),data.size()); + } + + Ref<DynamicFontData> dfd; + dfd.instance(); + dfd->set_font_data(data); + + if (r_error) + *r_error=OK; + + return dfd; +} + +void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List<String> *p_extensions) const { + + p_extensions->push_back("ttf"); +} + +bool ResourceFormatLoaderDynamicFont::handles_type(const String& p_type) const { + + return (p_type=="DynamicFontData"); +} + +String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const { + + String el = p_path.extension().to_lower(); + if (el=="ttf") + return "DynamicFontData"; + return ""; +} + +#endif diff --git a/scene/resources/dynamic_font_stb.h b/scene/resources/dynamic_font_stb.h new file mode 100644 index 0000000000..6b72fb3703 --- /dev/null +++ b/scene/resources/dynamic_font_stb.h @@ -0,0 +1,178 @@ +#ifndef DYNAMICFONT_STB_H +#define DYNAMICFONT_STB_H + +#ifndef FREETYPE_ENABLED + +#include "font.h" +#include "stb_truetype.h" +#include "io/resource_loader.h" + + + +class DynamicFontAtSize; +class DynamicFont; + +class DynamicFontData : public Resource { + + OBJ_TYPE(DynamicFontData,Resource); + + bool valid; + + DVector<uint8_t> font_data; + DVector<uint8_t>::Read fr; + const uint8_t* last_data_ptr; + + struct KerningPairKey { + + union { + struct { + uint32_t A,B; + }; + + uint64_t pair; + }; + + _FORCE_INLINE_ bool operator<(const KerningPairKey& p_r) const { return pair<p_r.pair; } + }; + + Map<KerningPairKey,int> kerning_map; + + + Map<int,DynamicFontAtSize*> size_cache; + +friend class DynamicFontAtSize; + + stbtt_fontinfo info; + int ascent; + int descent; + int linegap; + + void lock(); + void unlock(); + +friend class DynamicFont; + + + Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size); +public: + + void set_font_data(const DVector<uint8_t>& p_font); + DynamicFontData(); + ~DynamicFontData(); +}; + + +class DynamicFontAtSize : public Reference { + + OBJ_TYPE(DynamicFontAtSize,Reference); + + + int rect_margin; + + struct CharTexture { + + DVector<uint8_t> imgdata; + int texture_size; + Vector<int> offsets; + Ref<ImageTexture> texture; + }; + + Vector<CharTexture> textures; + + struct Character { + + int texture_idx; + Rect2 rect; + float v_align; + float h_align; + float advance; + + Character() { texture_idx=0; v_align=0; } + }; + + + + HashMap< CharType, Character > char_map; + + _FORCE_INLINE_ void _update_char(CharType p_char); + +friend class DynamicFontData; + Ref<DynamicFontData> font; + float scale; + int size; + +protected: + +public: + + float get_height() const; + + float get_ascent() const; + float get_descent() const; + + Size2 get_char_size(CharType p_char,CharType p_next=0) 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; + + + + DynamicFontAtSize(); + ~DynamicFontAtSize(); +}; + +/////////////// + +class DynamicFont : public Font { + + OBJ_TYPE( DynamicFont, Font ); + + Ref<DynamicFontData> data; + Ref<DynamicFontAtSize> data_at_size; + int size; + + +protected: + + static void _bind_methods(); + +public: + + void set_font_data(const Ref<DynamicFontData>& p_data); + Ref<DynamicFontData> get_font_data() const; + + void set_size(int p_size); + int get_size() const; + + virtual float get_height() const; + + virtual float get_ascent() const; + virtual float get_descent() const; + + virtual Size2 get_char_size(CharType p_char,CharType p_next=0) const; + + virtual bool is_distance_field_hint() 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; + + DynamicFont(); + ~DynamicFont(); + +}; + + + +///////////// + +class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader { +public: + + virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + virtual bool handles_type(const String& p_type) const; + virtual String get_resource_type(const String &p_path) const; + +}; + + +#endif +#endif // DYNAMICFONT_H diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index 09c0a21f53..e6356d3366 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -30,7 +30,6 @@ #include "scene/resources/concave_polygon_shape.h" #include "scene/resources/convex_polygon_shape.h" #include "surface_tool.h" - static const char*_array_name[]={ "vertex_array", "normal_array", @@ -288,6 +287,7 @@ void Mesh::add_surface(PrimitiveType p_primitive,const Array& p_arrays,const Arr triangle_mesh=Ref<TriangleMesh>(); _change_notify(); + emit_changed(); } @@ -387,6 +387,7 @@ void Mesh::surface_remove(int p_idx) { triangle_mesh=Ref<TriangleMesh>(); _recompute_aabb(); _change_notify(); + emit_changed(); } @@ -491,6 +492,8 @@ void Mesh::add_surface_from_mesh_data(const Geometry::MeshData& p_mesh_data) { surfaces.push_back(s); _change_notify(); + + emit_changed(); } RID Mesh::get_rid() const { diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp index 75e38f9701..a61ffe8e97 100644 --- a/scene/resources/style_box.cpp +++ b/scene/resources/style_box.cpp @@ -138,7 +138,7 @@ void StyleBoxTexture::draw(RID p_canvas_item,const Rect2& p_rect) const { r.pos.y-=expand_margin[MARGIN_TOP]; r.size.x+=expand_margin[MARGIN_LEFT]+expand_margin[MARGIN_RIGHT]; r.size.y+=expand_margin[MARGIN_TOP]+expand_margin[MARGIN_BOTTOM]; - VisualServer::get_singleton()->canvas_item_add_style_box( p_canvas_item,r,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center); + VisualServer::get_singleton()->canvas_item_add_style_box( p_canvas_item,r,region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center); } void StyleBoxTexture::set_draw_center(bool p_draw) { @@ -175,6 +175,20 @@ float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const { return expand_margin[p_expand_margin]; } +void StyleBoxTexture::set_region_rect(const Rect2& p_region_rect) { + + if (region_rect==p_region_rect) + return; + + region_rect=p_region_rect; + emit_changed(); +} + +Rect2 StyleBoxTexture::get_region_rect() const { + + return region_rect; +} + void StyleBoxTexture::_bind_methods() { @@ -187,10 +201,14 @@ void StyleBoxTexture::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_expand_margin_size","margin","size"),&StyleBoxTexture::set_expand_margin_size); ObjectTypeDB::bind_method(_MD("get_expand_margin_size","margin"),&StyleBoxTexture::get_expand_margin_size); + ObjectTypeDB::bind_method(_MD("set_region_rect","region"),&StyleBoxTexture::set_region_rect); + ObjectTypeDB::bind_method(_MD("get_region_rect"),&StyleBoxTexture::get_region_rect); + ObjectTypeDB::bind_method(_MD("set_draw_center","enable"),&StyleBoxTexture::set_draw_center); ObjectTypeDB::bind_method(_MD("get_draw_center"),&StyleBoxTexture::get_draw_center); ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), _SCS("set_texture"),_SCS("get_texture") ); + ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect")); ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/left", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_LEFT ); ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/right", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_RIGHT ); ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/top", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_TOP); @@ -505,4 +523,3 @@ StyleBoxImageMask::StyleBoxImageMask() { } expand=true; } - diff --git a/scene/resources/style_box.h b/scene/resources/style_box.h index 02d79bc2ac..98aaee754b 100644 --- a/scene/resources/style_box.h +++ b/scene/resources/style_box.h @@ -81,6 +81,7 @@ class StyleBoxTexture : public StyleBox { float expand_margin[4]; float margin[4]; + Rect2 region_rect; Ref<Texture> texture; bool draw_center; @@ -98,6 +99,9 @@ public: void set_margin_size(Margin p_margin,float p_size); float get_margin_size(Margin p_margin) const; + void set_region_rect(const Rect2& p_region_rect); + Rect2 get_region_rect() const; + void set_texture(RES p_texture); RES get_texture() const; diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp index 8cacc0fce7..e96cac170b 100644 --- a/scene/resources/world_2d.cpp +++ b/scene/resources/world_2d.cpp @@ -373,7 +373,15 @@ World2D::World2D() { Physics2DServer::get_singleton()->space_set_active(space,true); Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY,GLOBAL_DEF("physics_2d/default_gravity",98)); Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY_VECTOR,GLOBAL_DEF("physics_2d/default_gravity_vector",Vector2(0,1))); - Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_LINEAR_DAMP,GLOBAL_DEF("physics_2d/default_density",0.1)); + // TODO: Remove this deprecation warning and compatibility code for 2.2 or 3.0 + if (Globals::get_singleton()->get("physics_2d/default_density") && !Globals::get_singleton()->get("physics_2d/default_linear_damp)")) { + WARN_PRINT("Deprecated parameter 'physics_2d/default_density'. It was renamed to 'physics_2d/default_linear_damp', adjusting your project settings accordingly (make sure to adjust scripts that potentially rely on 'physics_2d/default_density'."); + Globals::get_singleton()->set("physics_2d/default_linear_damp", Globals::get_singleton()->get("physics_2d/default_density")); + Globals::get_singleton()->set_persisting("physics_2d/default_linear_damp", true); + Globals::get_singleton()->set_persisting("physics_2d/default_density", false); + Globals::get_singleton()->save(); + } + Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_LINEAR_DAMP,GLOBAL_DEF("physics_2d/default_linear_damp",0.1)); Physics2DServer::get_singleton()->area_set_param(space,Physics2DServer::AREA_PARAM_ANGULAR_DAMP,GLOBAL_DEF("physics_2d/default_angular_damp",1)); Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS,1.0); Physics2DServer::get_singleton()->space_set_param(space,Physics2DServer::SPACE_PARAM_CONTACT_MAX_SEPARATION,1.5); |