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.



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

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.



7
8
9
# File 'lib/suma/jsdai/figure.rb', line 7

def image
  @image
end

#xmlObject (readonly)

Returns the value of attribute xml.



7
8
9
# File 'lib/suma/jsdai/figure.rb', line 7

def xml
  @xml
end

Instance Method Details

#to_expObject

rubocop:disable Metrics/MethodLength



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

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



16
17
18
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
# File 'lib/suma/jsdai/figure.rb', line 16

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