Skip to content

Commit

Permalink
Rename Prawn::Svg to Prawn::SVG
Browse files Browse the repository at this point in the history
An alias is included so that Prawn::Svg continues to work for backwards
compatibility.
  • Loading branch information
Roger Nesbitt committed Aug 14, 2015
1 parent 09e94b3 commit e71bd2e
Show file tree
Hide file tree
Showing 31 changed files with 108 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -83,7 +83,7 @@ By default, prawn-svg has a fonts path of <tt>["/Library/Fonts", "/System/Librar
Mac OS X and Debian Linux users. You can add to the font path:

```ruby
Prawn::Svg::Interface.font_path << "/my/font/directory"
Prawn::SVG::Interface.font_path << "/my/font/directory"
```


Expand Down
6 changes: 5 additions & 1 deletion lib/prawn-svg.rb
Expand Up @@ -16,4 +16,8 @@
require 'prawn/svg/parser/text'
require 'prawn/svg/parser/image'

Prawn::Document.extensions << Prawn::Svg::Extension
module Prawn
Svg = SVG # backwards compatibility
end

Prawn::Document.extensions << Prawn::SVG::Extension
2 changes: 1 addition & 1 deletion lib/prawn/svg/calculators/aspect_ratio.rb
@@ -1,4 +1,4 @@
module Prawn::Svg::Calculators
module Prawn::SVG::Calculators
class AspectRatio
attr_reader :align, :defer
attr_reader :width, :height, :x, :y
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/svg/calculators/document_sizing.rb
@@ -1,4 +1,4 @@
module Prawn::Svg::Calculators
module Prawn::SVG::Calculators
class DocumentSizing
DEFAULT_WIDTH = 300
DEFAULT_HEIGHT = 150
Expand Down Expand Up @@ -34,7 +34,7 @@ def calculate
@output_height = Pixels.to_pixels(@document_height || @requested_height, container_height)

if @view_box
values = @view_box.strip.split(Prawn::Svg::Parser::COMMA_WSP_REGEXP)
values = @view_box.strip.split(Prawn::SVG::Parser::COMMA_WSP_REGEXP)
@x_offset, @y_offset, @viewport_width, @viewport_height = values.map {|value| value.to_f}
@x_offset = -@x_offset

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/svg/calculators/pixels.rb
@@ -1,4 +1,4 @@
module Prawn::Svg::Calculators
module Prawn::SVG::Calculators
class Pixels
extend Prawn::Measurements

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/svg/color.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Color
class Prawn::SVG::Color
UnresolvableURLWithNoFallbackError = Class.new(StandardError)

HTML_COLORS = {
Expand Down
8 changes: 4 additions & 4 deletions lib/prawn/svg/document.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Document
class Prawn::SVG::Document
begin
require 'css_parser'
CSS_PARSER_LOADED = true
Expand Down Expand Up @@ -27,7 +27,7 @@ def initialize(data, bounds, options)
@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 = Prawn::SVG::Calculators::DocumentSizing.new(bounds, @root.attributes)
sizing.requested_width = options[:width]
sizing.requested_height = options[:height]
sizing.calculate
Expand All @@ -50,10 +50,10 @@ def distance(value, axis = nil)
end

def points(value, axis = nil)
Prawn::Svg::Calculators::Pixels.to_pixels(value, @axis_to_size.fetch(axis, sizing.viewport_diagonal))
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)
@url_loader ||= Prawn::SVG::UrlLoader.new(:enable_cache => cache_images)
end
end
14 changes: 7 additions & 7 deletions lib/prawn/svg/element.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Element
class Prawn::SVG::Element
attr_reader :document, :source, :parent_calls, :base_calls, :state, :attributes
attr_accessor :calls

Expand Down Expand Up @@ -72,7 +72,7 @@ def apply_drawing_call(draw_types)
end

def container?
Prawn::Svg::Parser::CONTAINER_TAGS.include?(name)
Prawn::SVG::Parser::CONTAINER_TAGS.include?(name)
end

def parse_transform_attribute_and_call
Expand Down Expand Up @@ -155,10 +155,10 @@ def parse_fill_and_stroke_attributes_and_call
color = @attributes[color_attribute]

begin
hex = Prawn::Svg::Color.color_to_hex(color)
hex = Prawn::SVG::Color.color_to_hex(color)
state[type.to_sym] = true
add_call "#{type}_color", hex || '000000'
rescue Prawn::Svg::Color::UnresolvableURLWithNoFallbackError
rescue Prawn::SVG::Color::UnresolvableURLWithNoFallbackError
state[type.to_sym] = false
end
end
Expand All @@ -185,7 +185,7 @@ def parse_stroke_attributes_and_call
when 'none'
add_call('undash')
else
array = dasharray.split(Prawn::Svg::Parser::COMMA_WSP_REGEXP)
array = dasharray.split(Prawn::SVG::Parser::COMMA_WSP_REGEXP)
array *= 2 if array.length % 2 == 1
number_array = array.map {|value| @document.distance(value)}

Expand All @@ -207,7 +207,7 @@ def parse_font_attributes_and_call
end
if weight = @attributes['font-weight']
font_updated = true
@state[:font_weight] = Prawn::Svg::Font.weight_for_css_font_weight(weight)
@state[:font_weight] = Prawn::SVG::Font.weight_for_css_font_weight(weight)
end
if style = @attributes['font-style']
font_updated = true
Expand All @@ -225,7 +225,7 @@ def parse_font_attributes_and_call
usable_font_families = [@state[:font_family], document.fallback_font_name]

font_used = usable_font_families.compact.detect do |name|
if font = Prawn::Svg::Font.load(name, @state[:font_weight], @state[:font_style])
if font = Prawn::SVG::Font.load(name, @state[:font_weight], @state[:font_style])
@state[:font_subfamily] = font.subfamily
add_call_and_enter 'font', font.name, :style => @state[:font_subfamily]
true
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/svg/extension.rb
@@ -1,5 +1,5 @@
module Prawn
module Svg
module SVG
module Extension
#
# Draws an SVG document into the PDF.
Expand All @@ -15,7 +15,7 @@ module Extension
# svg IO.read("example.svg"), :at => [100, 300], :width => 600
#
def svg(data, options = {}, &block)
svg = Prawn::Svg::Interface.new(data, self, options, &block)
svg = Prawn::SVG::Interface.new(data, self, options, &block)
svg.draw
{:warnings => svg.document.warnings, :width => svg.document.sizing.output_width, :height => svg.document.sizing.output_height}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/svg/font.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Font
class Prawn::SVG::Font
GENERIC_CSS_FONT_MAPPING = {
"serif" => "Times-Roman",
"sans-serif" => "Helvetica",
Expand Down Expand Up @@ -34,7 +34,7 @@ def self.weight_for_css_font_weight(weight)
# This method is passed prawn's font_families hash. It'll be pre-populated with the fonts that prawn natively
# supports. We'll add fonts we find in the font path to this hash.
def self.load_external_fonts(fonts)
Prawn::Svg::Interface.font_path.uniq.collect {|path| Dir["#{path}/**/*"]}.flatten.each do |filename|
Prawn::SVG::Interface.font_path.uniq.collect {|path| Dir["#{path}/**/*"]}.flatten.each do |filename|
information = font_information(filename) rescue nil
if information && font_name = (information[16] || information[1])
subfamily = (information[17] || information[2] || "normal").gsub(/\s+/, "_").downcase.to_sym
Expand Down
8 changes: 4 additions & 4 deletions lib/prawn/svg/interface.rb
@@ -1,9 +1,9 @@
#
# Prawn::Svg::Interface makes a Prawn::Svg::Document instance, uses that object to parse the supplied
# Prawn::SVG::Interface makes a Prawn::SVG::Document instance, uses that object to parse the supplied
# SVG into Prawn-compatible method calls, and then calls the Prawn methods.
#
module Prawn
module Svg
module SVG
class Interface
VALID_OPTIONS = [:at, :position, :vposition, :width, :height, :cache_images, :fallback_font_name]

Expand All @@ -17,7 +17,7 @@ class << self; attr_accessor :font_path; end
attr_reader :data, :prawn, :document, :options

#
# Creates a Prawn::Svg object.
# Creates a Prawn::SVG object.
#
# +data+ is the SVG data to convert. +prawn+ is your Prawn::Document object.
#
Expand All @@ -40,7 +40,7 @@ def initialize(data, prawn, options, &block)
@prawn = prawn
@options = options

Prawn::Svg::Font.load_external_fonts(prawn.font_families)
Prawn::SVG::Font.load_external_fonts(prawn.font_families)

@document = Document.new(data, [prawn.bounds.width, prawn.bounds.height], options, &block)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/prawn/svg/parser.rb
@@ -1,17 +1,17 @@
require 'rexml/document'

#
# Prawn::Svg::Parser is responsible for parsing an SVG file and converting it into a tree of
# Prawn::SVG::Parser is responsible for parsing an SVG file and converting it into a tree of
# prawn-compatible method calls.
#
# You probably do not want to use this class directly. Instead, use Prawn::Svg to draw
# You probably do not want to use this class directly. Instead, use Prawn::SVG to draw
# SVG data to your Prawn::Document object.
#
# This class is not passed the prawn object, so knows nothing about
# prawn specifically - this might be useful if you want to take this code and use it to convert
# SVG to another format.
#
class Prawn::Svg::Parser
class Prawn::SVG::Parser
CONTAINER_TAGS = %w(g svg symbol defs clipPath)
COMMA_WSP_REGEXP = /(?:\s+,?\s*|,\s*)/

Expand Down Expand Up @@ -56,7 +56,7 @@ def parse
[]
]

root_element = Prawn::Svg::Element.new(@document, @document.root, calls, :ids => {}, :fill => true)
root_element = Prawn::SVG::Element.new(@document, @document.root, calls, :ids => {}, :fill => true)

parse_element(root_element)
calls
Expand Down Expand Up @@ -184,7 +184,7 @@ def parse_path(element)

begin
commands = @svg_path.parse(element.attributes['d'])
rescue Prawn::Svg::Parser::Path::InvalidError => e
rescue Prawn::SVG::Parser::Path::InvalidError => e
commands = []
@document.warnings << e.message
end
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/svg/parser/image.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Parser::Image
class Prawn::SVG::Parser::Image
Error = Class.new(StandardError)

class FakeIO
Expand Down Expand Up @@ -44,7 +44,7 @@ def parse(element)
return if width.zero? || height.zero?
raise Error, "width and height must be 0 or higher" if width < 0 || height < 0

aspect = Prawn::Svg::Calculators::AspectRatio.new(attrs['preserveAspectRatio'], [width, height], image_dimensions(image))
aspect = Prawn::SVG::Calculators::AspectRatio.new(attrs['preserveAspectRatio'], [width, height], image_dimensions(image))

if aspect.slice?
element.add_call "save"
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/svg/parser/path.rb
@@ -1,5 +1,5 @@
module Prawn
module Svg
module SVG
class Parser::Path
# Raised if the SVG path cannot be parsed.
InvalidError = Class.new(StandardError)
Expand Down
6 changes: 3 additions & 3 deletions lib/prawn/svg/parser/text.rb
@@ -1,4 +1,4 @@
class Prawn::Svg::Parser::Text
class Prawn::SVG::Parser::Text
def parse(element)
element.add_call_and_enter "text_group"
internal_parse(element, [element.document.x(0)], [element.document.y(0)])
Expand Down Expand Up @@ -34,7 +34,7 @@ def internal_parse(element, x_positions, y_positions, relative = false, preserve
opts[:style] = element.state[:font_subfamily]

# This is not a prawn option but we can't work out how to render it here -
# it's handled by Svg#rewrite_call_arguments
# it's handled by SVG#rewrite_call_arguments
if (anchor = attrs['text-anchor'] || element.state[:text_anchor]) &&
['start', 'middle', 'end'].include?(anchor)
opts[:text_anchor] = anchor
Expand Down Expand Up @@ -67,7 +67,7 @@ def internal_parse(element, x_positions, y_positions, relative = false, preserve
elsif child.name == "tspan"
element.add_call 'save'
child.attributes['text-anchor'] ||= opts[:text_anchor] if opts[:text_anchor]
child_element = Prawn::Svg::Element.new(element.document, child, element.calls, element.state.dup)
child_element = Prawn::SVG::Element.new(element.document, child, element.calls, element.state.dup)
internal_parse(child_element, x_positions, y_positions, relative, preserve_space)
child_element.append_calls_to_parent
element.add_call 'restore'
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/svg/url_loader.rb
@@ -1,7 +1,7 @@
require 'open-uri'
require 'base64'

class Prawn::Svg::UrlLoader
class Prawn::SVG::UrlLoader
attr_accessor :enable_cache, :enable_web
attr_reader :url_cache

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/svg/version.rb
@@ -1,5 +1,5 @@
module Prawn
module Svg
module SVG
VERSION = '0.21.0'
end
end
2 changes: 1 addition & 1 deletion prawn-svg.gemspec
Expand Up @@ -3,7 +3,7 @@ require File.expand_path('../lib/prawn/svg/version', __FILE__)

spec = Gem::Specification.new do |gem|
gem.name = 'prawn-svg'
gem.version = Prawn::Svg::VERSION
gem.version = Prawn::SVG::VERSION
gem.summary = "SVG renderer for Prawn PDF library"
gem.description = "This gem allows you to render SVG directly into a PDF using the 'prawn' gem. Since PDF is vector-based, you'll get nice scaled graphics if you use SVG instead of an image."
gem.has_rdoc = false
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Prawn::Svg::Interface do
describe Prawn::SVG::Interface do
root = "#{File.dirname(__FILE__)}/.."

context "with option :position" do
Expand Down
4 changes: 2 additions & 2 deletions spec/prawn/svg/calculators/aspect_ratio_spec.rb
@@ -1,8 +1,8 @@
require File.dirname(__FILE__) + '/../../../spec_helper'

describe Prawn::Svg::Calculators::AspectRatio do
describe Prawn::SVG::Calculators::AspectRatio do
def test(*args)
aspect = Prawn::Svg::Calculators::AspectRatio.new(*args)
aspect = Prawn::SVG::Calculators::AspectRatio.new(*args)
[[aspect.width, aspect.height], [aspect.x, aspect.y]]
end

Expand Down
6 changes: 3 additions & 3 deletions spec/prawn/svg/calculators/document_sizing_spec.rb
@@ -1,13 +1,13 @@
require File.dirname(__FILE__) + '/../../../spec_helper'

describe Prawn::Svg::Calculators::DocumentSizing do
describe Prawn::SVG::Calculators::DocumentSizing do
let(:attributes) do
{"width" => "150", "height" => "200", "viewBox" => "0 -30 300 800", "preserveAspectRatio" => "xMaxYMid meet"}
end

let(:bounds) { [1200, 800] }

let(:sizing) { Prawn::Svg::Calculators::DocumentSizing.new(bounds, attributes) }
let(:sizing) { Prawn::SVG::Calculators::DocumentSizing.new(bounds, attributes) }

describe "#initialize" do
it "takes bounds and a set of attributes and calls set_from_attributes" do
Expand All @@ -17,7 +17,7 @@
end

describe "#set_from_attributes" do
let(:sizing) { Prawn::Svg::Calculators::DocumentSizing.new(bounds) }
let(:sizing) { Prawn::SVG::Calculators::DocumentSizing.new(bounds) }

it "sets ivars from the passed-in attributes hash" do
sizing.set_from_attributes(attributes)
Expand Down

0 comments on commit e71bd2e

Please sign in to comment.