Class: FontExample

Inherits:
Object
  • Object
show all
Includes:
LibuiParadise::BaseModule
Defined in:
lib/libui_paradise/examples/complex/021_font_example.rb

Overview

#

021_font_example.rb

#

This example shows how to display coloured text.

An AttributedString is a string which also contains information about styles such as text color, font, font size. It will be drawn in an UiArea element.

#

require ‘libui_paradise/examples/complex/021_font_example.rb’

#

Constant Summary collapse

USE_THIS_FONT =
#

USE_THIS_FONT

Specify a certain font to use.

#
'Georgia'
USE_THIS_FONT_SIZE =
#

USE_THIS_FONT_SIZE

#
25
TITLE =
#

TITLE

#
'Font Example'
WIDTH =
#

WIDTH

#
500

Constants included from LibuiParadise::BaseModule

LibuiParadise::BaseModule::HEIGHT

Instance Method Summary collapse

Methods included from LibuiParadise::BaseModule

#abort_on_exception, #append_this_array_to_that_combobox, #area, #assumed_height?, #assumed_width?, #bold_button, #bold_label, #bold_text_left_aligned, #button, #chdir, #checkbox, #checked_checkbox, #close_properly, #colour_button, #colour_to_rgb, #combobox, #commandline_arguments?, #connect_skeleton, #copy, #copy_file, #create_directory, #create_grid, #create_skeleton_then_connect_skeleton, #create_table, #create_the_skeleton, #current_widget_pointer_type?, #delete_file, #do_quit, #editable_combobox, #entry, #error_msg, #esystem, #exit_from, #fancy_text, #font, #font_button, #free_table_model, #gtk3?, #hbox, #height?, #horizontal_separator, #image, #is_on_roebe?, #is_on_windows?, #label, #last_pointer?, #left_arrow?, #main_hash?, #main_then_quit, #menu, #message_box_error, #multiline_entry, #new_brush, #non_wrapping_multiline_entry, #parse_this_file_into_a_table, #password_entry, #quit_button, #radio_buttons, #register_this_fiddle_pointer_widget, #reset, #reset_the_internal_hash, #return_button_for_opening_a_local_file, #return_default_window, #return_pwd, #return_the_resolution_using_xrandr, #run_main, #scrolling_area, #search_entry, #selected?, #set_commandline_arguments, #set_height, #set_main_window, #set_title, #set_width, #set_window, #sfancy, #sfile, #slider, #spinbox, #tab, #table, #text?, #text_layout, #title?, #title_width_height, #title_width_height_font, #try_to_parse_this_config_file, #try_to_use_this_font, #two_elements_hbox, #ui_draw_text_layout_params, #ui_font_descriptor, #ui_margined_main_window, #ui_msg_box, #ui_open_file, #ui_padded_grid, #ui_padded_hbox, #ui_padded_vbox, #ui_sync_connect, #ui_table_params_malloc, #ui_text_then_entry, #use_gtk3?, #use_gtk?, #use_libui?, #vbox, #vertical_separator, #width?, #window, #window?, #without_trailing_comment, #wrapper_new_progress_bar

Constructor Details

#initialize(run_already = true) ⇒ FontExample

#

initialize

#


45
46
47
48
49
50
# File 'lib/libui_paradise/examples/complex/021_font_example.rb', line 45

def initialize(
    run_already = true
  )
  reset
  run if run_already
end

Instance Method Details

#runObject

#

run

#


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/libui_paradise/examples/complex/021_font_example.rb', line 55

def run
  handler = LibUI::FFI::AreaHandler.malloc
  area    = LibUI.new_area(handler)
  str1 = 'At last Ygramul sensed that something was coming toward her.'
  attr_str = LibuiParadise.fancy_text('')
  def attr_str.append(what)
    color_attribute = LibUI.new_color_attribute(0.5, 0.0, 0.25, 0.7)
    start = LibUI.attributed_string_len(self)
    LibUI.attributed_string_append_unattributed(self, what)
    LibUI.attributed_string_set_attribute(self, color_attribute, start, start + what.size)
    LibUI.attributed_string_append_unattributed(self, "\n\n")
  end
  attr_str.append(str1)
  handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) { |_, _, adp|
    area_draw_params = LibUI::FFI::AreaDrawParams.new(adp)
    default_font = font {{
      font_size:   USE_THIS_FONT_SIZE, # Pass in the font-size here.
      font_family: USE_THIS_FONT,
      italic:      false,
      stretch:     2,
      weight:      500
    }}

    params = ui_draw_text_layout_params

    params.String = attr_str
    params.DefaultFont = default_font
    params.Width = area_draw_params.AreaWidth
    params.Align = 0
    text_layout = LibUI.draw_new_text_layout(params)
    LibUI.draw_text(area_draw_params.Context, text_layout, 0, 0)
    LibUI.draw_free_text_layout(text_layout)
  }
  # =========================================================================== #
  # Assigning to local variables:
  #
  # This is intended to protect Fiddle::Closure from garbage collection.
  #
  # =========================================================================== #
  handler.Draw         = handler_draw_event
  handler.MouseEvent   = (c1 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
  handler.MouseCrossed = (c2 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
  # handler.DragBroken   = (c3 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
  # handler.KeyEvent     = (c4 = Fiddle::Closure::BlockCaller.new(0, [0]) {})

  box = ui_padded_vbox
  box.maximal(area)
  box.maximal(ui_hseparator)

  main_window = ui_main_window(TITLE, WIDTH, 400, 1)
  LibUI.window_set_margined(main_window, 1)
  main_window << box
  main_window.simple_exit
  main_window.control_show
  main_window.intelligent_exit
end