Class: CTioga::TiogaPrimitiveMaker

Inherits:
Object
  • Object
show all
Extended by:
Debug
Includes:
Log
Defined in:
lib/CTioga/elements/tioga_primitives.rb

Overview

A class to parse the format of graphics primitives, which is something like:

text: 12,34 "nice text" angle=35

Constant Summary collapse

FloatArray =

A few predefined types to make it all more readable - and easier to maintain !!

{:type => :array, :subtype => :float}
Point =
FloatArray
Boolean =
{:type => :boolean}
Marker =

Damn useful !!

{                  # Damn useful !!
  :type => :array, 
  :subtype => {:type => :integer, :namespace => Tioga::MarkerConstants},
  :namespace => Tioga::MarkerConstants,
  :shortcuts => {'No' => 'None', 'None' => 'None',
    'no' => 'None', 'none' => 'None',
  } # For no/none to work fine
}
Number =

For no/none to work fine

{:type => :float}
Color =
{
  :type => :array, :subtype => :float, 
  :namespace => Tioga::ColorConstants
}
LineStyle =

Very bad definition, but, well…

{:type => :array, :subtype => :integer,
  :namespace => Styles,
}
Justification =
{:type => :integer, 
  :shortcuts => {
    'Left' => Tioga::FigureConstants::LEFT_JUSTIFIED,
    'left' => Tioga::FigureConstants::LEFT_JUSTIFIED,
    'l' => Tioga::FigureConstants::LEFT_JUSTIFIED,
    'Center' => Tioga::FigureConstants::CENTERED,
    'center' => Tioga::FigureConstants::CENTERED,
    'c' => Tioga::FigureConstants::CENTERED,
    'Right' => Tioga::FigureConstants::RIGHT_JUSTIFIED,
    'right' => Tioga::FigureConstants::RIGHT_JUSTIFIED,
    'r' => Tioga::FigureConstants::RIGHT_JUSTIFIED,
  }
}
Alignment =
{:type => :integer, 
  :shortcuts => {
    'Top' => Tioga::FigureConstants::ALIGNED_AT_TOP,
    'top' => Tioga::FigureConstants::ALIGNED_AT_TOP,
    't' => Tioga::FigureConstants::ALIGNED_AT_TOP,
    'Mid' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'mid' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'Midheight' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'midheight' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'm' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    # Take center too !
    'Center' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'center' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'c' => Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT,
    'Base' => Tioga::FigureConstants::ALIGNED_AT_BASELINE,
    'base' => Tioga::FigureConstants::ALIGNED_AT_BASELINE,
    'B' => Tioga::FigureConstants::ALIGNED_AT_BASELINE,
    'Bottom' => Tioga::FigureConstants::ALIGNED_AT_BOTTOM,
    'bottom' => Tioga::FigureConstants::ALIGNED_AT_BOTTOM,
    'b' => Tioga::FigureConstants::ALIGNED_AT_BOTTOM,
  }
}
PRIMITIVES =

Available primitives:

{
  "text" => self.new(:show_text, 
                     [ 
                      ['point', Point],
                      ['text']
                     ],
                     {
                       'angle' => Number,
                       'color' => Color,
                       'scale' => Number,
                       'justification' => Justification,
                       'alignment' => Alignment,
                     }),
  "arrow" => self.new(:show_arrow, 
                      [ 
                       ['tail', Point ],
                       ['head', Point ],
                      ],
                      {
                        'head_marker' => Marker,
                        'tail_marker' => Marker,
                        'head_scale'  => Number,
                        'tail_scale'  => Number,
                        'line_width'  => Number,
                        'line_style'  => LineStyle,
                        'color'       => Color,
                      }),
  "marker" => self.new(:show_marker, 
                       [ 
                        ['point', Point ],
                        ['marker', Marker ],
                       ],
                       {
                         'color'       => Color,
                         'angle'       => Number,
                         'scale'       => Number,
                       }),
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Debug

debug_figmaker, debug_patterns, debug_puts, figmaker_options, test_pattern, test_pattern_right

Methods included from Log

#identify, #init_logger, #logger, #logger_options, #spawn

Constructor Details

#initialize(symb, comp, opt) ⇒ TiogaPrimitiveMaker

Creates a TiogaPrimitiveMaker object, from the specifications



66
67
68
69
70
# File 'lib/CTioga/elements/tioga_primitives.rb', line 66

def initialize(symb, comp, opt)
  @symbol = symb
  @compulsory = comp
  @optional = opt
end

Instance Attribute Details

#compulsoryObject

An array of [name, types] for compulsory arguments



61
62
63
# File 'lib/CTioga/elements/tioga_primitives.rb', line 61

def compulsory
  @compulsory
end

#optionalObject

A hash of the optional arguments



63
64
65
# File 'lib/CTioga/elements/tioga_primitives.rb', line 63

def optional
  @optional
end

#symbolObject

The symbol for the funcall



59
60
61
# File 'lib/CTioga/elements/tioga_primitives.rb', line 59

def symbol
  @symbol
end

Class Method Details

.introspect(details = true) ⇒ Object

Returns a small descriptive text about the currently known graphics primitives.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/CTioga/elements/tioga_primitives.rb', line 226

def self.introspect(details = true)
  str = ""
  for name, spec in PRIMITIVES
    str += "\t#{name}: " +
      spec.compulsory.map do |a|
      a[0]
    end. join(' ') +
      if details
        "\n\t\toptions: #{spec.optional.keys.join(',')}\n\t\t"
      else
        " [options]\t "
      end + "see Tioga function #{spec.symbol}\n"
  end
  return str
end

.parse_spec(spec, plotmaker) ⇒ Object

Parse a specification such as

text: 12,34 "nice text" angle=35

plotmaker is a pointer to the plotmaker instance.



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/CTioga/elements/tioga_primitives.rb', line 211

def self.parse_spec(spec, plotmaker)
  spec =~ /^([^:]+):(.*)/
  name = $1
  args = Shellwords.shellwords($2)
  if PRIMITIVES.key? name
    ret = PRIMITIVES[name].make_funcall(args, plotmaker)
    debug "draw: #{name} -> #{ret.inspect}"
  else
    error "Unkown graphic primitive: #{name}"
  end
  ret
end

Instance Method Details

#make_funcall(args, plotmaker) ⇒ Object

Turns a graphics spefication into a Tioga Funcall.



102
103
104
105
# File 'lib/CTioga/elements/tioga_primitives.rb', line 102

def make_funcall(args, plotmaker)
  dict = parse_args(args)
  return TiogaFuncall.new(@symbol, dict) 
end

#parse_args(args) ⇒ Object

This function is fed with an array of Strings. The example in TiogaPrimitiveMaker would look like

["12,34", "nice text", "angle=35"]

It returns the hash that can be fed to the appropriate Tioga primitive. It is fed the plot



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/CTioga/elements/tioga_primitives.rb', line 77

def parse_args(args)
  ret = {}
  for name, type_spec in @compulsory
    type_spec ||= {:type => :string} # Absent means string
    type = MetaBuilder::ParameterType.get_type(type_spec)
    val = type.string_to_type(args.shift)
    ret[name] = val
  end
  # We now parse the rest of the arguments:
  for opt in args
    if opt =~ /^([^=]+)=(.*)/
      name = $1
      arg = $2
      type_spec = @optional[name] || {:type => :string}
      type = MetaBuilder::ParameterType.get_type(type_spec)
      val = type.string_to_type(arg)
      ret[name] = val
    else
      warn "Malformed optional argument #{opt}"
    end
  end
  return ret
end