class CTioga2::Graphics::Styles::BaseTextStyle

The style of a text object. This class is suitable for inclusion as a Hash to FigureMaker#show_axis, for the tick labels.

@todo alignement and justification are poor names. halign and/or valign would be better.

Public Instance Methods

draw_text(t, text, x_or_loc, y = nil, measure = nil) click to toggle source

Draw the text at the given location with the given style. If y is nil, or [:pos, value] then x_or_loc is taken to be a location (see FigureMaker#show_text).

# File lib/ctioga2/graphics/styles/texts.rb, line 64
def draw_text(t, text, x_or_loc, y = nil, measure = nil)
  t.context do
    dict = prepare_show_text_dict(t, text, x_or_loc, y, measure)
    t.show_text(dict)
  end
end
hash_for_tioga(t) click to toggle source
# File lib/ctioga2/graphics/styles/texts.rb, line 85
def hash_for_tioga(t)
  dict = self.to_hash
  if dict.key? 'shift'
    dim = dict['shift']
    dict['shift'] = dim.to_dy(t)
  end
  if dict.key? 'scale'
    dim = dict['scale']
    dict['scale'] = dim.to_dy(t)
  end
  dict.delete('text_width')
  return dict
end
scale_dy(t) click to toggle source
# File lib/ctioga2/graphics/styles/texts.rb, line 78
def scale_dy(t)
  if @scale
    return @scale.to_dy(t)
  end
  return nil
end
shift_dy(t) click to toggle source
# File lib/ctioga2/graphics/styles/texts.rb, line 71
def shift_dy(t)
  if @shift
    return @shift.to_dy(t)
  end
  return nil
end
vertical?(loc) click to toggle source
# File lib/ctioga2/graphics/styles/texts.rb, line 99
def vertical?(loc)
  if loc
    return Types::PlotLocation.new(loc).vertical?
  else
    ang = angle || 0
    return Math.sin(ang*3.14/180)**2 > 0.5
  end
end

Protected Instance Methods

prepare_show_text_dict(t, text, x_or_loc, y = nil, measure = nil) click to toggle source

Prepares the dictionnary for use with show_text

# File lib/ctioga2/graphics/styles/texts.rb, line 111
def prepare_show_text_dict(t, text, x_or_loc, y = nil, measure = nil)
  dict = self.hash_for_tioga(t)
  # get rid of uncommented stuff problems
  text = "#{text}%\n"

  loc = nil

  if y && (! y.is_a?(Array))
    dict['at'] = [x_or_loc, y]
  else
    # Perform automatic conversion on the location
    case x_or_loc
    when Symbol, Types::PlotLocation
      ## @todo It won't be easy to implement shifts for this,
      ## though it may be useful eventually.
      loc = x_or_loc
      x_or_loc = Types::PlotLocation.new(x_or_loc).tioga_location
    end
    dict['loc'] = x_or_loc
    if y
      dict['position'] = y[1]
    end
  end
  if measure
    dict['measure'] = measure
  end

  if @text_width
    dir = (vertical?(loc) ? :y : :x)
    dim = @text_width.to_bp(t, dir)
    dim /= t.default_text_scale

    aln = @text_align || @justification || Tioga::FigureConstants::CENTERED
    cmd = 
      case aln
      when Tioga::FigureConstants::CENTERED
        '\centering{}'
      when Tioga::FigureConstants::LEFT_JUSTIFIED
        '\raggedright{}'
      when Tioga::FigureConstants::RIGHT_JUSTIFIED
        '\raggedleft{}'
      else
        ''
      end
    text = "\\parbox{#{dim}bp}{#{cmd}#{text}}"
  end

  dict['text'] = text

  return dict
end