Class: DynamicImageElements::BlockElement

Inherits:
Object
  • Object
show all
Includes:
ElementInterface
Defined in:
lib/elements/block_element.rb

Overview

Element is used for compositing another elements in it. All inner elements are positioned relative to block element position.

Direct Known Subclasses

DynamicImage, TableCellElement

Instance Method Summary collapse

Methods included from ElementInterface

#final_size, #set_height, #set_width

Constructor Details

#initialize(options, parent = nil, &block) ⇒ BlockElement

Block element accepts options Hash. Block can be given and class provides element self to composite elements in it.

Use methods provided by class to add elements.

Options

You can use also aliases provided by ElementInterface::OPTIONS_ALIASES in all options Hashes.

:align

Sets the align of inner elements. Valid values are :left, :center and :right. Default is :left.

It’s automatically propagated to directly inner TextElements.

:background

Sets background of element. Accepts value for DynamicImageSources::SourceFactory.

Default is transparent.

:border, :border_top, :border_right, :border_bottom, :border_left

Creates border around element. You can specify all sides same by :border or each side separately. It’s possible to set all same by :border and override one or more sides by f.e. :border_top.

Specify border by Array or String separated by space chars in format [line_width line_style *color]. See accepted value for DynamicImageSources::SourceFactory.parse for accepted color sources.

Valid line_styles are:

:solid                    ───────────────────────────────────
:dotted                   ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪
:dashed                   ─── ─── ─── ─── ─── ─── ─── ─── ───
:dashed_bigger            ───── ───── ───── ───── ───── ─────
:dashed_big               ──────── ──────── ──────── ────────
:dashed_dot               ─── ▪ ─── ▪ ─── ▪ ─── ▪ ─── ▪ ─── ▪
:dashed_double_dot        ─── ▪ ▪ ─── ▪ ▪ ─── ▪ ▪ ─── ▪ ▪ ───
:dashed_big_bigger        ───── ─── ───── ─── ───── ─── ─────
:double_dotted            ▪ ▪   ▪ ▪   ▪ ▪   ▪ ▪   ▪ ▪   ▪ ▪
:triple_dotted            ▪ ▪ ▪   ▪ ▪ ▪   ▪ ▪ ▪   ▪ ▪ ▪   ▪ ▪
:double_dashed_dot        ─── ─── ▪ ─── ─── ▪ ─── ─── ▪ ─── ─
:double_dashed_double_dot ─── ─── ▪ ▪ ─── ─── ▪ ▪ ─── ─── ▪ ▪
:double_dashed_triple_dot ─── ─── ▪ ▪ ▪ ─── ─── ▪ ▪ ▪ ─── ───
:triple_dashed_dot        ─── ─── ─── ▪ ─── ─── ─── ▪ ─── ───
:triple_dashed_double_dot ─── ─── ─── ▪ ▪ ─── ─── ─── ▪ ▪ ───

You can add number to line_style name to multiple space size. F.e.: [1, dotted3, :red].

:color

Sets foreground of inner text elements. Accepts value for DynamicImageSources::SourceFactory.

:padding, :padding_top, :padding_right, :padding_bottom, :padding_left

Creates gap between element’s border and its content (inner elements). You can specify all sides same by :padding or each side separately.

You can specify all sides by :padding key too as an Array or String separated by space chars. Values has to be in this order: top, right, bottom, left. When top equals bottom and right equals left you can use only two numbers to specify both pairs.

See :margin for examples. It’s similar.

:vertical_align

Sets vertical align of inner elements. Valid values are :top, :middle and :bottom. Default is :top.

Common options

These options can be given to any element in composition.

:height

Sets height of element. Please note that real height is calculated as height + paddings + margins.

You can use percentage as String or Float. It will calculate height according to parent’s element height. F.e.: "100%" or 1.0.

:margin, :margin_top, :margin_right, :margin_bottom, :margin_left

Creates gap around element and other elements in canvas. You can specify all sides same by :margin or each side separately.

You can specify all sides by :margin key too as an Array or String separated by space chars. Values has to be in this order: top, right, bottom, left. When top equals bottom and right equals left you can use only two numbers to specify both pairs.

Example

  • :margin_top => 10 will create 10 px gap above element, other sides will have no gap

  • :margin => 10 will create gap 10 px at all sides

  • :margin => [5, 10, 10, 5] will create gaps 5 px above, 10 px at right side and below, 5 px at left side

  • :margin => [5, 10, 10, 5] is same as :margin => "5 10 10 5"

  • :margin => [5, 10, 5, 10] is same as :margin => [5, 10] and :margin => "5 10"

:position

Moves element from its position. Valid values are :static, :relative, :absolute. Default is :static.

Static position doesn’t move element from its position. Even if x or y is given.

Relative position moves element from its position. Amount of move is given by x and y options and it’s added to original position. Other elements are not affected by it.

Absolute position removes element from document flow and places element to the position given by x and y into parent element.

:width

Sets width of element. Please note that real width is calculated as width + paddings + margins.

You can use percentage as String or Float. It will calculate width according to parent’s element width. F.e.: "100%" or 1.0.

:x

Horizontal position in parent container (block element)

:y

Vertical position in parent container

:z

Z-order of objects in parent element. Default is 0 and elements are ordered by order they was created in.



98
99
100
101
102
103
104
105
106
# File 'lib/elements/block_element.rb', line 98

def initialize(options, parent = nil, &block) # :yields: block_element
  @options = options
  @parent = parent
  @elements = [] #should looks like [[:x => int, :y => int, z => int, :obj => Element], ...]
  use_options :margin
  use_options :padding
  use_options :border
  process self, &block if block
end

Instance Method Details

#block(options = {}, &block) ⇒ Object

Creates new BlockElement as a child of its composite. See BlockElement.new for arguments information.



194
195
196
197
# File 'lib/elements/block_element.rb', line 194

def block(options = {}, &block) # :yields: block_element
  treat_options options
  add_element BlockElement.new(options, self, &block), options
end

#heightObject

Gets height for inner elements



113
114
115
# File 'lib/elements/block_element.rb', line 113

def height #:nodoc:
  @drawing ? inner_size[1] : (@options[:height] || (@parent && @parent.height ? @parent.height - @padding[0] - @padding[2] - @margin[0] - @margin[2] : nil))
end

#image(source, options = {}) ⇒ Object

Creates new ImageElement as a child of its composite. See ImageElement.new for arguments information.



200
201
202
203
# File 'lib/elements/block_element.rb', line 200

def image(source, options = {})
  treat_options options
  add_element ImageElement.new(source, options, self), options
end

#table(options = {}, &block) ⇒ Object

Creates new TableElement as a child of its composite. See TableElement.new for arguments information.



206
207
208
209
# File 'lib/elements/block_element.rb', line 206

def table(options = {}, &block) # :yields: table_element
  treat_options options
  add_element TableElement.new(options, self, &block), options
end

#text(content, options = {}, &block) ⇒ Object

Creates new TextElement as a child of its composite. See TextElement.new for arguments information.



212
213
214
215
216
# File 'lib/elements/block_element.rb', line 212

def text(content, options = {}, &block) # :yields: pango_layout
  treat_options options
  options[:align] = @options[:align] unless options[:align] # propagate align if no one is given
  add_element TextElement.new(content, options, self, &block), options
end

#widthObject

Gets width for inner elements



109
110
111
# File 'lib/elements/block_element.rb', line 109

def width #:nodoc:
  @drawing ? inner_size[0] : (@options[:width] || (@parent && @parent.width ? @parent.width - @padding[1] - @padding[3] - @margin[1] - @margin[3] : nil))
end