class Etti::Element::Text

Constants

GROUP_KEYS
TEXT_KEYS

Public Class Methods

new(text, x=nil, y=nil, size=nil) click to toggle source
Calls superclass method Etti::Element::Base::new
# File lib/etti/elements/text.rb, line 11
def initialize text, x=nil, y=nil, size=nil
    super x, y
    @text = text || ''
    @size = size || 4.0
end

Public Instance Methods

at_pos(view, x, y) click to toggle source
# File lib/etti/elements/text.rb, line 101
def at_pos view, x, y
    ext = get_extent view
    a = -@rotation * Math::PI / 180.0
    xr = (x - @x) * Math.cos(a) - (y - @y) * Math.sin(a)
    yr = (y - @y) * Math.cos(a) + (x - @x) * Math.sin(a)
    ext.in(xr + @x, yr + @y)
end
draw_selection(view) click to toggle source
# File lib/etti/elements/text.rb, line 83
def draw_selection view
    cc = Cairo::Context.new view.backBuffer
    cc.antialias = :none
    cc.line_width = 1.0
    cc.set_dash [6, 6], 0
    cc.set_source_rgb *$colors.selection

    cc.font_size = @size * view.scale
    ext = cc.text_extents @text
    xd, yd = *view.pos_to_view(@x, @y)

    cc.translate xd, yd
    cc.rotate @rotation * Math::PI / 180.0 unless @rotation.near 0.0
    cc.rectangle 0, ext.y_bearing, ext.width, ext.height
    cc.stroke
end
from_h(hs) click to toggle source
# File lib/etti/elements/text.rb, line 73
def from_h hs
    hs.each_pair {|k, v| set_by_name k, v}
end
get_by_name(name) click to toggle source
# File lib/etti/elements/text.rb, line 28
def get_by_name name
    if name == 'text'               then return @text;
    elsif name == 'pos_x'           then return @x;
    elsif name == 'pos_y'           then return @y;
    elsif name == 'size'            then return @size;
    elsif name == 'rotation'        then return @rotation;
    elsif name == 'color'           then return @color;     end
    nil
end
get_extent(view) click to toggle source
# File lib/etti/elements/text.rb, line 110
def get_extent view
    cc = Cairo::Context.new view.backBuffer
    cc.font_size = @size * view.scale
    ext = cc.text_extents @text
    w = ext.width() / view.scale
    y = @y + (ext.y_bearing() / view.scale)
    Rectangle.new @x, y, w, @size
end
redraw() click to toggle source
# File lib/etti/elements/text.rb, line 78
def redraw

end
set_by_name(name, val) click to toggle source

x, y, rotation, color have to be moved to the Etti::Element::Base class

# File lib/etti/elements/text.rb, line 18
def set_by_name name, val
    if name == 'text'               then @text = val;               return; end
    if name == 'pos_x'              then @x = val.to_f;             return; end
    if name == 'pos_y'              then @y = val.to_f;             return; end
    if name == 'size'               then @size = val.to_f;          return; end
    if name == 'rotation'           then @rotation = val.to_f;      return; end
    if name == 'color'              then @color = val;              return; end
end
to_h() click to toggle source
# File lib/etti/elements/text.rb, line 39
            def to_h
#                hs = super() # use this if we take data from Element::Base
                hs = {}
                hs['elementType'] = 'text'
                TEXT_KEYS.each {|name| hs[name] = get_by_name name}
                hs
            end
validate_from_h(hs) click to toggle source
# File lib/etti/elements/text.rb, line 48
def validate_from_h hs
    data = Etti::PropertyEditorData::TEXTS_PROPS
    # stop if hs has a key that is not in TEXT_KEYS
    return _('ElementText invalid property keys') if (hs.keys - GROUP_KEYS - TEXT_KEYS).length() > 0
    # check the value of the keys
    TEXT_KEYS.each do |name|
        next if hs[name].nil?
        entry = data.select {|dat| dat[:id0] == name or dat[:id1] == name}
        if entry[0][:range]
            return _("ElementText %s is out of range") % name unless entry[0][:range].cover? hs[name]
        elsif name == 'text'
            # we limit the text length to 10_000 chars; for a label thats more than enough
            return _("ElementText %s length is out of range") % name if hs[name].length() < 1 or hs[name].length > 10_000
        elsif name == 'color'
            col = hs[name]
            return _('ElementText color is invalid') if !col.is_a? Array or col.length() != 4
            col.each do |val| 
                return _('ElementText invalid value in color') if val < 0.0 or val > 1.0
            end
        end
    end
    nil
end