Class: Frameit::Trimbox

Inherits:
Object
  • Object
show all
Defined in:
frameit/lib/frameit/trim_box.rb

Overview

Represents the MiniMagick trim bounding box for cropping a text image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identify_string) ⇒ Trimbox

identify_string: A string with syntax “<width>x<height>+<offset_x>+<offset_y>”. This is returned by MiniMagick when using function .identify with format(“%@”). It is also required for the MiniMagick .crop function.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'frameit/lib/frameit/trim_box.rb', line 12

def initialize(identify_string)
  UI.user_error!("Trimbox cannot be initialised with an empty 'identify_string'.") unless identify_string.length > 0

  # Parse the input syntax "<width>x<height>+<offset_x>+<offset_y>".
  # Extract these 4 parameters into an integer array, by using multiple string separators: "x" and "+":
  trim_values = identify_string.split(/[x+]/).map(&:to_i)

  # If 'identify_string' doesn't have the expected syntax with 4 parameters, show error:
  UI.user_error!("Trimbox is initialised with an invalid value for 'identify_string'.") unless trim_values.length == 4

  # Assign instance variables:
  @width = trim_values[0]
  @height = trim_values[1]
  @offset_x = trim_values[2]
  @offset_y = trim_values[3]
end

Instance Attribute Details

#heightObject

height of the trim box



7
8
9
# File 'frameit/lib/frameit/trim_box.rb', line 7

def height
  @height
end

#offset_xObject

horizontal offset from the canvas to the trim box



8
9
10
# File 'frameit/lib/frameit/trim_box.rb', line 8

def offset_x
  @offset_x
end

#offset_yObject

vertical offset from the canvas to the trim box



9
10
11
# File 'frameit/lib/frameit/trim_box.rb', line 9

def offset_y
  @offset_y
end

#widthObject

width of the trim box



6
7
8
# File 'frameit/lib/frameit/trim_box.rb', line 6

def width
  @width
end

Instance Method Details

#json_string_formatObject

Get the trimbox parameters in a human readable JSON string format



36
37
38
39
# File 'frameit/lib/frameit/trim_box.rb', line 36

def json_string_format
  # Create a JSON string from the trim box parameters:
  return "{\"width\" : #{@width}, \"height\" : #{@height} , \"offset_x\" : #{@offset_x}, \"offset_y\" : #{@offset_y}}"
end

#string_formatObject

Get the trimbox parameters in the MiniMagick string format



30
31
32
33
# File 'frameit/lib/frameit/trim_box.rb', line 30

def string_format
  # Convert trim box parameters to string with syntax: "<width>x<height>+<offset_x>+<offset_y>":
  return "#{@width}x#{@height}+#{@offset_x}+#{@offset_y}"
end