Class: SVGUtilities

Inherits:
Object
  • Object
show all
Defined in:
lib/IFMapper/SVGMapExporter.rb

Class Method Summary collapse

Class Method Details

.add_background(svg, width, height, svgfilename) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/IFMapper/SVGMapExporter.rb', line 204

def self.add_background(svg, width, height, svgfilename)
  if DEBUG_OUTPUT; puts "svg::SVGUtilities::add_background" end

  svgbgroundimagefilename = "#{svgfilename}#{DEFAULT_BGROUND_IMAGE_FORMAT}"

  if svg.root.elements["defs"] == nil
    svg.root.add_element "defs"
  end

  defs = svg.root.elements["defs"]

  defs[0,0] = REXML::Comment.new(" #{MSG_SVG_BGROUND_IMAGE_SECT_BEGINS} ")

  bgpattern = REXML::Element.new('pattern')
  bgpattern.add_attributes({
    'id'           => 'backgroundimg',
    'patternUnits' => 'userSpaceOnUse',
    'x'            => '0',
    'y'            => '0',
    'height'       => height,
    'width'        => width
  })
  bgpattern.add_element 'image', {
    'xlink:href'   => svgbgroundimagefilename,
    'height'       => height,
    'width'        => width
  }
  defs[1,0] = bgpattern

  defs[2,0] = REXML::Comment.new(" #{MSG_SVG_BGROUND_IMAGE_SECT_ENDS} ")

  bgpath = REXML::Comment.new(" #{MSG_SVG_BGROUND_IMAGE_ENABLE_COMMENT_START} #{svgbgroundimagefilename} #{MSG_SVG_BGROUND_IMAGE_ENABLE_COMMENT_END} -->\n"\
    "  <!--<path"\
    " d='M5,5 l0," + height + " l" + width + ",0 l0,-" + height + " l-" + width + ",0" + "'"\
    " fill='url(#backgroundimg)'/>"
  )
  svg.root.insert_after(defs, bgpath)

  return svg
end

.add_compass(svg) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/IFMapper/SVGMapExporter.rb', line 124

def self.add_compass(svg)
  compassgroup = SVGUtilities::get_compass_svg_group()

  defs = svg.root.add_element "defs"

  defs.add_element compassgroup

  return svg
end

.add_text(svg, x, y, font_size, text, opts, fill_colour) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/IFMapper/SVGMapExporter.rb', line 41

def self.add_text( svg, x, y, font_size, text, opts, fill_colour )
  if DEBUG_OUTPUT; puts "svg::FXMap::svg_draw_name_text:" end
  y = y + font_size

  t = svg.root.add_element "text", {
  "x"            => x,
  "y"            => y,
  "style"        => "font-size:" + font_size.to_s() + "pt;fill:#" + fill_colour.to_s()}

  t.text = text

  return [svg, x, y]
end

.add_titles(svg, opts, x, y, font_size, mapname, mapcreator) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/IFMapper/SVGMapExporter.rb', line 180

def self.add_titles( svg, opts, x, y, font_size, mapname, mapcreator )
  if DEBUG_OUTPUT; puts "svg::SVGUtilities::add_titles" end

  if opts['print_title'] == true
    if not mapname or mapname == ''
      if DEBUG_OUTPUT; puts "svg::SVGUtilities::add_titles:name is empty, not printing" end
    else
      svg, x, y = SVGUtilities::add_text(svg, x, y, (font_size*1.5).to_i, mapname, opts, '000000')
      y = y + (opts['name_line_spacing'] * 4)
    end
  end

  if opts['print_creator'] == true
    if not mapcreator or mapcreator == ''
      if DEBUG_OUTPUT; puts "svg::SVGUtilities::add_titles:creator is empty, not printing" end
    else
      svg, x, y = SVGUtilities::add_text(svg, x, y, (font_size*0.85).to_i, 'Map ' + opts['creator_prefix'] + mapcreator, opts, '000000')
      y = y + (opts['name_line_spacing'] * 4)
    end
  end

  return [svg, x, y]
end

.break_text_lines(text, maxlinelength) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/IFMapper/SVGMapExporter.rb', line 63

def self.break_text_lines( text, maxlinelength )
  if DEBUG_OUTPUT; printf("SVGUtilities::break_text_lines:text=%s\r\n", text) end

  out = Array.new

  max_chars = maxlinelength
  lines = text.split("\n")
  if DEBUG_OUTPUT; printf("SVGUtilities::break_text_lines:lines=%s\r\n", lines) end
  words = text.split(" ")
  if DEBUG_OUTPUT; printf("SVGUtilities::break_text_lines:words=%s\r\n", words) end
  cur_str = ""

  lines.each { |line|
      words = line.split(" ");
      words.each { |word|
        new_len = cur_str.length + word.length
        if DEBUG_OUTPUT; printf("SVGUtilities::break_text_lines:current word: %s :: new_len: %d\r\n", word, new_len) end
        new_len = cur_str.length + word.length
        if new_len <= max_chars
          cur_str = cur_str + word + " "
        else
          out << cur_str
          cur_str = "" + word + " "
        end
      }
      out << cur_str
      cur_str = ""
  }

  return out
end

.get_compass_svg_groupObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/IFMapper/SVGMapExporter.rb', line 95

def self.get_compass_svg_group()

  file = File.new( "icons/compass.svg" )
  doc = REXML::Document.new file

  newgroup = REXML::Element.new "g"
  newgroup.attributes["id"] = "compass"

  doc.root.elements.each {|elem|
    if elem.elements['tspan'] != nil
      if elem.elements['tspan'].text =~ /^N$/
        elem.elements['tspan'].text = Room::DIRECTIONS[0].upcase
      elsif elem.elements['tspan'].text =~ /^E$/
        elem.elements['tspan'].text = Room::DIRECTIONS[2].upcase
      elsif elem.elements['tspan'].text =~ /^S$/
        elem.elements['tspan'].text = Room::DIRECTIONS[4].upcase
      elsif elem.elements['tspan'].text =~ /^W$/
        elem.elements['tspan'].text = Room::DIRECTIONS[6].upcase
      end
      if DEBUG_OUTPUT; puts "svg::SVGUtilities::get_compass_svg_group:translated_compass_dir:#{elem.elements['tspan'].text}" end
    end

    newgroup.add_element(elem)
  }

  return newgroup

end

.new_svg_doc(width, height) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/IFMapper/SVGMapExporter.rb', line 23

def self.new_svg_doc ( width, height)
  svg = REXML::Document.new
  svg << REXML::XMLDecl.new( version=1.0, encoding=REXML::XMLDecl::DEFAULT_ENCODING )
  svg << REXML::DocType.new( 'svg', REXML::DocType::PUBLIC + ' "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"' )
  svg << REXML::Comment.new(" #{MSG_SVG_GENERATOR} ")
  svg << REXML::Comment.new(" #{MSG_SVG_GENERATION_DATE} " + Time.now.utc.to_s + ' ')
  svg << REXML::Comment.new(" #{HOMEPAGE} ")

  svg.add_element "svg", {
      "width"       => width,
      "height"      => height,
      "version"     => "1.1",
      "xmlns"       => "http://www.w3.org/2000/svg",
      "xmlns:xlink" => "http://www.w3.org/1999/xlink"}
  return svg

end

.num_text_lines(text, maxlinelength) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/IFMapper/SVGMapExporter.rb', line 55

def self.num_text_lines ( text, maxlinelength )
  if text and not text == ''
    return SVGUtilities::break_text_lines(text.chomp().strip(), maxlinelength).length
  else
    return 0
  end
end

.should_draw_interactive(opts) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/IFMapper/SVGMapExporter.rb', line 15

def self.should_draw_interactive(opts)
  if opts['draw_tasks'] or opts['draw_objects'] or opts['draw_comments'] or opts['draw_description']
    return true
  else
    return false
  end
end

.svg_add_script(svg, opts) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/IFMapper/SVGMapExporter.rb', line 135

def self.svg_add_script( svg, opts )
  svg.root.attributes["onload"] = "Init(evt)"

  script = svg.root.add_element "script", {
  "type"            => "text/ecmascript" }

  script.add( REXML::CData.new("
  var SVGDocument = null;
  var SVGRoot = null;

  function Init(evt)
  {
     SVGDocument = evt.target.ownerDocument;
     SVGRoot = SVGDocument.documentElement;
  }

  function ToggleOpacity(evt, targetId)
  {
     var newTarget = evt.target;
     if (targetId)
     {
        newTarget = SVGDocument.getElementById(targetId);
     }
     var newValue = newTarget.getAttributeNS(null, 'opacity')

     if ('0' != newValue)
     {
        newValue = '0';
     }
     else
     {
        newValue = '1';
     }
     newTarget.setAttributeNS(null, 'opacity', newValue);

     if (targetId)
     {
        SVGDocument.getElementById(targetId + 'Exception').setAttributeNS(null, 'opacity', '1');
     }
  }
  "))

  return svg;
end