Class: OoxmlParser::OoxmlSize

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb

Overview

Size of some object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0, unit = :dxa) ⇒ OoxmlSize

Returns a new instance of OoxmlSize.



11
12
13
14
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 11

def initialize(value = 0, unit = :dxa)
  @unit = unit
  @value = value
end

Instance Attribute Details

#unitSymbol

Returns units of measurement.

Returns:

  • (Symbol)

    units of measurement



9
10
11
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 9

def unit
  @unit
end

#valueFloat

Returns value of size.

Returns:

  • (Float)

    value of size



7
8
9
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 7

def value
  @value
end

Instance Method Details

#==(other) ⇒ True, False

Compare this object to other

Parameters:

  • other (Object)

    any other object

Returns:

  • (True, False)

    result of comparision



34
35
36
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 34

def ==(other)
  (to_base_unit.value - other.to_base_unit.value).abs < 10**(OoxmlParser.configuration.accuracy + 2)
end

#parse(node) ⇒ OoxmlSize

Parse OoxmlSize

Parameters:

  • node (Nokogiri::XML:Node)

    with OoxmlSize

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 19

def parse(node)
  node.attributes.each do |key, value|
    case key
    when 'type'
      @unit = value.value.to_sym
    when 'w', 'val'
      @value = value.value.to_f
    end
  end
  self
end

#to_base_unitOoxmlSize

Convert all values to one same base unit

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 51

def to_base_unit
  case @unit
  when :centimeter
    OoxmlSize.new(@value * 360_000)
  when :point
    OoxmlSize.new(@value * 12_700)
  when :half_point
    OoxmlSize.new(@value * (12_700 / 2))
  when :one_eighth_point
    OoxmlSize.new(@value * (12_700 / 8))
  when :one_100th_point
    OoxmlSize.new(@value * (12_700 / 100))
  when :one_240th_cm
    OoxmlSize.new(@value * 1500)
  when :dxa, :twip
    OoxmlSize.new(@value * 635, :emu)
  when :inch
    OoxmlSize.new(@value * 914_400, :emu)
  when :spacing_point
    OoxmlSize.new(@value * (12_700 / 100), :emu)
  when :percent
    OoxmlSize.new(@value * 100_000, :one_100000th_percent)
  when :pct
    OoxmlSize.new(@value * 100_000 / 50, :one_100000th_percent)
  when :one_1000th_percent
    OoxmlSize.new(@value * 100, :one_100000th_percent)
  when :one_60000th_degree
    OoxmlSize.new(@value, :one_60000th_degree)
  when :degree
    OoxmlSize.new(@value * 60_000, :one_60000th_degree)
  else
    self
  end
end

#to_s(unit = :centimeter) ⇒ String

Returns string representation of size.

Parameters:

  • unit (Symbol) (defaults to: :centimeter)

    unit in which output should be

Returns:

  • (String)

    string representation of size



44
45
46
47
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 44

def to_s(unit = :centimeter)
  converted = to_unit(unit)
  "#{converted.value.to_f} #{converted.unit}"
end

#to_unit(output_unit) ⇒ OoxmlSize

Returns converted unit.

Parameters:

  • output_unit (Symbol)

    output unit of convertion

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 88

def to_unit(output_unit)
  base_unit = to_base_unit
  case output_unit
  when :centimeter
    OoxmlSize.new((base_unit.value / 360_000).round(OoxmlParser.configuration.accuracy), output_unit)
  when :point
    OoxmlSize.new((base_unit.value / 12_700).round(OoxmlParser.configuration.accuracy), output_unit)
  when :half_point
    OoxmlSize.new((base_unit.value / (12_700 * 2)).round(OoxmlParser.configuration.accuracy), output_unit)
  when :one_eighth_point
    OoxmlSize.new((base_unit.value / (12_700 * 8)).round(OoxmlParser.configuration.accuracy), output_unit)
  when :one_100th_point
    OoxmlSize.new((base_unit.value / (12_700 / 100)).round(OoxmlParser.configuration.accuracy), output_unit)
  when :one_240th_cm
    OoxmlSize.new((base_unit.value / 1500).round(OoxmlParser.configuration.accuracy), output_unit)
  when :dxa, :twip
    OoxmlSize.new((base_unit.value / 635).round(OoxmlParser.configuration.accuracy), output_unit)
  when :inch
    OoxmlSize.new((base_unit.value / 914_400).round(OoxmlParser.configuration.accuracy), output_unit)
  when :percent
    OoxmlSize.new((base_unit.value / 50).round(OoxmlParser.configuration.accuracy), output_unit)
  when :spacing_point
    OoxmlSize.new((base_unit.value / (12_700 * 100)).round(OoxmlParser.configuration.accuracy), output_unit)
  else
    base_unit
  end
end

#zero?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_size.rb', line 38

def zero?
  @value.zero?
end