You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height) to obtain the true width and height of drawn text, but it keeps giving me 0 values.
require'libui'UI=LibUIdefappend_with_attribute(attr_str,what,attr1,attr2)start_pos=UI.attributed_string_len(attr_str)end_pos=start_pos + what.lengthUI.attributed_string_append_unattributed(attr_str,what)UI.attributed_string_set_attribute(attr_str,attr1,start_pos,end_pos)UI.attributed_string_set_attribute(attr_str,attr2,start_pos,end_pos)ifattr2enddefmake_attribute_stringattr_str=UI.new_attributed_string("Drawing strings with libui is done with the uiAttributedString and uiDrawTextLayout objects.\n" \
'uiAttributedString lets you have a variety of attributes: ')attr1=UI.new_family_attribute('Courier New')append_with_attribute(attr_str,'font family',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_size_attribute(18)append_with_attribute(attr_str,'font size',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_weight_attribute(UI::TextWeightBold)append_with_attribute(attr_str,'font weight',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_italic_attribute(UI::TextItalicItalic)append_with_attribute(attr_str,'font italicness',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_stretch_attribute(UI::TextStretchCondensed)append_with_attribute(attr_str,'font stretch',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_color_attribute(0.75,0.25,0.5,0.75)append_with_attribute(attr_str,'text color',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_background_attribute(0.5,0.5,0.25,0.5)append_with_attribute(attr_str,'text background color',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')attr1=UI.new_underline_attribute(UI::UnderlineSingle)append_with_attribute(attr_str,'underline style',attr1,nil)UI.attributed_string_append_unattributed(attr_str,', ')UI.attributed_string_append_unattributed(attr_str,'and ')attr1=UI.new_underline_attribute(UI::UnderlineDouble)attr2=UI.new_underline_color_attribute(UI::UnderlineColorCustom,1.0,0.0,0.5,1.0)append_with_attribute(attr_str,'underline color',attr1,attr2)UI.attributed_string_append_unattributed(attr_str,'. ')UI.attributed_string_append_unattributed(attr_str,'Furthermore, there are attributes allowing for ')attr1=UI.new_underline_attribute(UI::UnderlineSuggestion)attr2=UI.new_underline_color_attribute(UI::UnderlineColorSpelling,0,0,0,0)append_with_attribute(attr_str,'special underlines for indicating spelling errors',attr1,attr2)UI.attributed_string_append_unattributed(attr_str,' (and other types of errors) ')UI.attributed_string_append_unattributed(attr_str,'and control over OpenType features such as ligatures (for instance, ')otf=UI.new_open_type_featuresUI.open_type_features_add(otf,'l','i','g','a',0)attr1=UI.new_features_attribute(otf)append_with_attribute(attr_str,'afford',attr1,nil)UI.attributed_string_append_unattributed(attr_str,' vs. ')UI.open_type_features_add(otf,'l','i','g','a',1)attr1=UI.new_features_attribute(otf)append_with_attribute(attr_str,'afford',attr1,nil)UI.free_open_type_features(otf)UI.attributed_string_append_unattributed(attr_str,").\n")UI.attributed_string_append_unattributed(attr_str,'Use the controls opposite to the text to control properties of the text.')attr_strenddefon_font_changed(area)UI.area_queue_redraw_all(area)enddefon_combobox_selected(area)UI.area_queue_redraw_all(area)enddefdraw_event(adp,attr_str,font_button,alignment)area_draw_params=UI::FFI::AreaDrawParams.new(adp)default_font=UI::FFI::FontDescriptor.mallocdefault_font.to_ptr.free=Fiddle::RUBY_FREEparams=UI::FFI::DrawTextLayoutParams.mallocparams.to_ptr.free=Fiddle::RUBY_FREEparams.String=attr_strUI.font_button_font(font_button,default_font)params.DefaultFont=default_fontparams.Width=area_draw_params.AreaWidthputsparams.Widthparams.Align=UI.combobox_selected(alignment)text_layout=UI.draw_new_text_layout(params)UI.draw_text(area_draw_params.Context,text_layout,0,0)extent_width=Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE*8)extent_height=Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE*8)
::LibUI.draw_text_layout_extents(text_layout,extent_width,extent_height)extent_width=extent_width[0,Fiddle::SIZEOF_DOUBLE*8].unpack1('i')extent_height=extent_height[0,Fiddle::SIZEOF_DOUBLE*8].unpack1('i')putsextent_widthputsextent_heightUI.draw_free_text_layout(text_layout)UI.free_font_button_font(default_font)endUI.inithandler=UI::FFI::AreaHandler.mallochandler.to_ptr.free=Fiddle::RUBY_FREEhandler_draw_event=Fiddle::Closure::BlockCaller.new(0,[1,1,1])do |_,_area,adp|
draw_event(adp,@attr_str,@font_button,@alignment)endhandler.Draw=handler_draw_eventdo_nothing=Fiddle::Closure::BlockCaller.new(0,[0]){}key_event=Fiddle::Closure::BlockCaller.new(1,[0]){0}handler.MouseEvent=do_nothinghandler.MouseCrossed=do_nothinghandler.DragBroken=do_nothinghandler.KeyEvent=key_event@attr_str=make_attribute_stringmain_window=UI.new_window('Text-Drawing Example',640,480,1)UI.window_set_margined(main_window,1)UI.window_on_closing(main_window)doUI.quit1endhbox=UI.new_horizontal_boxUI.box_set_padded(hbox,1)UI.window_set_child(main_window,hbox)vbox=UI.new_vertical_boxUI.box_set_padded(vbox,1)UI.box_append(hbox,vbox,0)@font_button=UI.new_font_buttonUI.font_button_on_changed(@font_button){on_font_changed(@area)}UI.box_append(vbox,@font_button,0)form=UI.new_formUI.form_set_padded(form,1)UI.box_append(vbox,form,0)@alignment=UI.new_comboboxUI.combobox_append(@alignment,'Left')UI.combobox_append(@alignment,'Center')UI.combobox_append(@alignment,'Right')UI.combobox_set_selected(@alignment,0)UI.combobox_on_selected(@alignment){on_combobox_selected(@area)}UI.form_append(form,'Alignment',@alignment,0)@area=UI.new_area(handler)UI.box_append(hbox,@area,1)UI.control_show(main_window)UI.mainUI.free_attributed_string(@attr_str)UI.uninit
Hi,
I am trying to use
uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height)
to obtain the true width and height of drawn text, but it keeps giving me 0 values.I added this code to basic_draw_text.rb:
Full code example:
I get the following printout:
Maybe I am doing something wrong. Do you have code that works in using
LibUI.draw_text_layout_extents
to print the width and height of drawn text?The text was updated successfully, but these errors were encountered: