class CTioga2::TextSizeWatcher

This class watches over a list of named texts and can be queried for size/position information.

Attributes

bb[RW]

A left, bottom, right, up bounding box (postscript points)

watched_names[RW]

Watched text names

Public Class Methods

new() click to toggle source
# File lib/ctioga2/utils.rb, line 605
def initialize
  @watched_names = Set.new
end

Public Instance Methods

compute_bb(t) click to toggle source
# File lib/ctioga2/utils.rb, line 649
def compute_bb(t)

  @bb = nil

  for w in @watched_names
    info = t.get_text_size(w)
    if info.key? 'points'
      # We need to take every single point, since for rotated
      # text, potentially all coordinates are different
      for p in info['points']
        update_bb(*p)
      end
    end
  end
end
update_margins(t, margins, padding = 2, min = 4) click to toggle source

Given the MarginsBox with which the text was drawn, returns another MarginsBox item that specifies how much the text extends from the previous box. Works using the current frame coordinates.

Padding in big points

Min is the minimum size, also in big points.

# File lib/ctioga2/utils.rb, line 621
def update_margins(t, margins, padding = 2, min = 4)
  compute_bb(t)
  if ! @bb
    # Don't change anything if the bounding box does not exist
    return margins
  end
  left, top, right, bottom = *margins.to_frame_coordinates(t)
  scl = 1/t.scaling_factor
  xl = scl * t.convert_page_to_output_x(t.convert_frame_to_page_x(left))
  xr = scl * t.convert_page_to_output_x(t.convert_frame_to_page_x(right))
  yt = scl * t.convert_page_to_output_y(t.convert_frame_to_page_y(top))
  yb = scl * t.convert_page_to_output_y(t.convert_frame_to_page_y(bottom))

  vals = [ xl - @bb[0], @bb[2] - xr, @bb[3] - yt, yb - @bb[1]].map do |x|
    x += padding
    x = if x > min
          x
        else
          min
        end
    Graphics::Types::Dimension.new(:bp, x)
  end

  return Graphics::Types::MarginsBox.
    new(*vals)
end
watch(*names) click to toggle source
# File lib/ctioga2/utils.rb, line 609
def watch(*names)
  @watched_names += names
end

Protected Instance Methods

update_bb(x, y) click to toggle source

update the current bounding box to take into account the given point

# File lib/ctioga2/utils.rb, line 669
def update_bb(x, y)
  if ! @bb
    @bb = [x,y,x,y]
  else
    if x < @bb[0]
      @bb[0] = x
    elsif x > @bb[2]
      @bb[2] = x
    end
    if y < @bb[1]
      @bb[1] = y
    elsif y > @bb[3]
      @bb[3] = y
    end
  end
end