/
document.rb
59 lines (46 loc) · 1.48 KB
/
document.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
57
58
59
class Prawn::SVG::Document
begin
require 'css_parser'
CSS_PARSER_LOADED = true
rescue LoadError
CSS_PARSER_LOADED = false
end
DEFAULT_FALLBACK_FONT_NAME = "Times-Roman"
# An +Array+ of warnings that occurred while parsing the SVG data.
attr_reader :warnings
attr_writer :url_cache
attr_reader :root,
:sizing,
:cache_images, :fallback_font_name,
:css_parser, :elements_by_id
def initialize(data, bounds, options)
@css_parser = CssParser::Parser.new if CSS_PARSER_LOADED
@root = REXML::Document.new(data).root
@warnings = []
@options = options
@elements_by_id = {}
@cache_images = options[:cache_images]
@fallback_font_name = options.fetch(:fallback_font_name, DEFAULT_FALLBACK_FONT_NAME)
@sizing = Prawn::SVG::Calculators::DocumentSizing.new(bounds, @root.attributes)
sizing.requested_width = options[:width]
sizing.requested_height = options[:height]
sizing.calculate
@axis_to_size = {:x => sizing.viewport_width, :y => sizing.viewport_height}
yield self if block_given?
end
def x(value)
points(value, :x)
end
def y(value)
sizing.output_height - points(value, :y)
end
def distance(value, axis = nil)
value && points(value, axis)
end
def points(value, axis = nil)
Prawn::SVG::Calculators::Pixels.to_pixels(value, @axis_to_size.fetch(axis, sizing.viewport_diagonal))
end
def url_loader
@url_loader ||= Prawn::SVG::UrlLoader.new(:enable_cache => cache_images)
end
end