Class: Suma::Jsdai::Figure

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/jsdai/figure.rb

Overview

Main class for JSDAI figure conversion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_file, image_file) ⇒ Figure

Returns a new instance of Figure.



12
13
14
15
16
# File 'lib/suma/jsdai/figure.rb', line 12

def initialize(xml_file, image_file)
  @xml = FigureXml.from_xml(File.read(xml_file))
  @image = FigureImage.new(image_file)
  @xml_file = xml_file
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



10
11
12
# File 'lib/suma/jsdai/figure.rb', line 10

def image
  @image
end

#xmlObject (readonly)

Returns the value of attribute xml.



10
11
12
# File 'lib/suma/jsdai/figure.rb', line 10

def xml
  @xml
end

Instance Method Details

#to_expObject

rubocop:disable Metrics/MethodLength



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/suma/jsdai/figure.rb', line 55

def to_exp
  basename = File.basename(@xml_file, ".xml")
  schema_name = extract_schema_name(basename)
  anchor_id = extract_anchor_id(basename)

  exp_parts = []
  exp_parts << "(*\"#{schema_name}.__expressg\""
  exp_parts << "\n[[#{anchor_id}]]"
  exp_parts << "\n[.svgmap]"
  exp_parts << "\n===="
  exp_parts << "\nimage::#{basename}.svg[]"

  if @xml.img.areas && !@xml.img.areas.empty?
    exp_parts << "\n"
    @xml.img.areas.each_with_index do |area, index|
      target = extract_target_from_href(area.href)
      exp_parts << "\n* <<#{target}>>; #{index + 1}"
    end
  end

  exp_parts << "\n===="
  exp_parts << "\n*)\n"
  exp_parts.join
end

#to_svgObject

rubocop:disable Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/suma/jsdai/figure.rb', line 19

def to_svg
  width, height = @image.dimensions
  svg_parts = []

  svg_parts << '<?xml version="1.0" encoding="UTF-8"?>'
  svg_parts << '<svg xmlns="http://www.w3.org/2000/svg" '
  svg_parts << 'xml:space="preserve" '
  svg_parts << 'style="enable-background:new 0 0 595.28 841.89;" '
  svg_parts << "height=\"#{height}\" "
  svg_parts << "width=\"#{width}\" "
  svg_parts << "viewBox=\"0 0 #{width} #{height}\" "
  svg_parts << 'y="0px" x="0px" id="Layer_1" version="1.1">'
  svg_parts << "\n\t\t\t\n\t\t\t"

  # Add embedded image
  svg_parts << "<image href=\"#{@image.to_base64}\" "
  svg_parts << "height=\"#{height}\" "
  svg_parts << "width=\"#{width}\" "
  svg_parts << 'style="overflow:visible;">'
  svg_parts << "\n\t\t\t</image>\n\t\t\t"

  # Add clickable areas
  if @xml.img.areas && !@xml.img.areas.empty?
    area_parts = @xml.img.areas.each_with_index.map do |area, index|
      shape_element = generate_shape_element(area)
      "<a href=\"#{index + 1}\">#{shape_element}</a>"
    end

    svg_parts << area_parts.join
  end
  svg_parts << "\n\t\t</svg>"
  svg_parts.join
end