prawn-svg 0.9.1.5 → 0.9.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -40,7 +40,14 @@ prawn-svg is in its infancy and does not support the full SVG specifications. I
40
40
 
41
41
  - colors: html standard names, #xxx, #xxxxxx, rgb(1, 2, 3), rgb(1%, 2%, 3%)
42
42
 
43
- - measurements specified in pt, cm, dm, ft, in, m, mm, yd
43
+ - measurements specified in pt, cm, dm, ft, in, m, mm, yd, %
44
+
45
+ - fonts: generic CSS fonts, built in PDF fonts, and any TTF fonts in your fonts path
46
+
47
+ By default, prawn-svg has a fonts path of ["/Library/Fonts", "/usr/share/fonts/truetype/**"] to catch
48
+ Mac OS X and Debian Linux users. You can add to the font path:
49
+
50
+ Prawn::Svg.font_path << "/my/font/directory"
44
51
 
45
52
  prawn-svg does NOT support named elements, external references, the tspan tag, gradients/patterns or markers.
46
53
 
@@ -124,11 +124,12 @@ class Prawn::Svg::Parser
124
124
  # Very primitive support for font-family; it won't work in most cases because
125
125
  # PDF only has a few built-in fonts, and they're not the same as the names
126
126
  # used typically with the web fonts.
127
- if (font = style_attrs['font-family']) && !font.match(/[\/\\]/)
128
- font = font.strip
129
- if font != ""
130
- calls << ['font', [font], []]
127
+ if font_family = style_attrs["font-family"]
128
+ if font_family != "" && pdf_font = map_font_family_to_pdf_font(font_family)
129
+ calls << ['font', [pdf_font], []]
131
130
  calls = calls.last.last
131
+ else
132
+ @warnings << "#{font_family} is not a known font."
132
133
  end
133
134
  end
134
135
 
@@ -336,6 +337,35 @@ class Prawn::Svg::Parser
336
337
  [name, arguments]
337
338
  end
338
339
  end
340
+
341
+ BUILT_IN_FONTS = ["Courier", "Helvetica", "Times-Roman", "Symbol", "ZapfDingbats"]
342
+ GENERIC_CSS_FONT_MAPPING = {
343
+ "serif" => "Times-Roman",
344
+ "sans-serif" => "Helvetica",
345
+ "cursive" => "Times-Roman",
346
+ "fantasy" => "Times-Roman",
347
+ "monospace" => "Courier"}
348
+
349
+ def installed_fonts
350
+ @installed_fonts ||= Prawn::Svg.font_path.uniq.collect {|path| Dir["#{path}/*"]}.flatten
351
+ end
352
+
353
+ def map_font_family_to_pdf_font(font_family)
354
+ font_family.split(",").detect do |font|
355
+ font = font.gsub(/['"]/, '').gsub(/\s{2,}/, ' ').strip.downcase
356
+
357
+ built_in_font = BUILT_IN_FONTS.detect {|f| f.downcase == font}
358
+ break built_in_font if built_in_font
359
+
360
+ generic_font = GENERIC_CSS_FONT_MAPPING[font]
361
+ break generic_font if generic_font
362
+
363
+ installed_font = installed_fonts.detect do |file|
364
+ (matches = File.basename(file).match(/(.+)\./)) && matches[1].downcase == font
365
+ end
366
+ break installed_font if installed_font
367
+ end
368
+ end
339
369
 
340
370
  # TODO : use http://www.w3.org/TR/SVG11/types.html#ColorKeywords
341
371
  HTML_COLORS = {
data/lib/prawn/svg/svg.rb CHANGED
@@ -3,7 +3,14 @@
3
3
  # SVG into Prawn-compatible method calls, and then calls the Prawn methods.
4
4
  #
5
5
  class Prawn::Svg
6
- attr_reader :data, :prawn, :options
6
+ DEFAULT_FONT_PATHS = ["/Library/Fonts", "/usr/share/fonts/truetype/**"]
7
+
8
+ @font_path = []
9
+ DEFAULT_FONT_PATHS.each {|path| @font_path << path if File.exists?(path)}
10
+
11
+ class << self; attr_accessor :font_path; end
12
+
13
+ attr_reader :data, :prawn, :parser, :options
7
14
 
8
15
  # An +Array+ of warnings that occurred while parsing the SVG data. If this array is non-empty,
9
16
  # it's likely that the SVG failed to render correctly.
@@ -71,12 +78,6 @@ class Prawn::Svg
71
78
  end
72
79
 
73
80
  arguments.last[:at][1] += prawn.height_of(*arguments) / 3 * 2
74
-
75
- when 'font'
76
- unless prawn.font_families.member?(arguments.first)
77
- @parser_warnings << "#{arguments.first} is not a known font."
78
- false
79
- end
80
81
  end
81
82
  end
82
83
  end
@@ -16,7 +16,7 @@ module Prawn
16
16
  def svg(data, options={})
17
17
  svg = Prawn::Svg.new(data, self, options)
18
18
  svg.draw
19
- {:warnings => svg.parser_warnings}
19
+ {:warnings => svg.parser_warnings, :width => svg.parser.width, :height => svg.parser.height}
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 9
8
8
  - 1
9
- - 5
10
- version: 0.9.1.5
9
+ - 6
10
+ version: 0.9.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Roger Nesbitt