Skip to content

Commit

Permalink
Fixed #11 Upgrade to Prawn 1.3.0!
Browse files Browse the repository at this point in the history
  • Loading branch information
hidakatsuya committed Mar 11, 2015
1 parent c7e9148 commit 4b2e4e5
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 110 deletions.
4 changes: 4 additions & 0 deletions Gemfile
@@ -1,3 +1,7 @@
source 'http://rubygems.org'

gemspec

group :example do
gem 'pdf-inspector'
end
7 changes: 3 additions & 4 deletions lib/thinreports/core.rb
@@ -1,17 +1,16 @@
# coding: utf-8

module ThinReports

ROOTDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))

module Core
end

end

require 'thinreports/core/ext'
require 'thinreports/core/utils'
require 'thinreports/core/ordered_hash'

require 'thinreports/core/errors'
require 'thinreports/core/events'
Expand Down
6 changes: 3 additions & 3 deletions lib/thinreports/generator/pdf.rb
@@ -1,11 +1,11 @@
# coding: utf-8

begin
gem 'prawn', '0.12.0'
gem 'prawn', '1.3.0'
require 'prawn'
rescue LoadError
puts 'ThinReports requires Prawn = 0.12.0. ' +
'Please `gem install prawn -v 0.12.0` and try again.'
puts 'ThinReports requires Prawn = 1.3.0. ' +
'Please `gem install prawn -v 1.3.0` and try again.'
end

module ThinReports
Expand Down
18 changes: 9 additions & 9 deletions lib/thinreports/generator/pdf/document/graphics/image.rb
Expand Up @@ -5,7 +5,7 @@

module ThinReports
module Generator

module PDF::Graphics
# @param [String] filename
# @param [Numeric, Strng] x
Expand All @@ -16,7 +16,7 @@ def image(filename, x, y, w, h)
w, h = s2f(w, h)
pdf.image(filename, at: pos(x, y), width: w, height: h)
end

# @param [String] base64
# @param [Numeric, Strng] x
# @param [Numeric, Strng] y
Expand All @@ -26,7 +26,7 @@ def base64image(base64, x, y, w, h)
image = create_temp_imagefile(base64)
image(image.path, x, y, w, h)
end

# @param file (see Prawn#image)
# @param [Numeric, Strng] x
# @param [Numeric, Strng] y
Expand All @@ -43,17 +43,17 @@ def image_box(file, x, y, w, h, options = {})
auto_fit: [w, h])
end
end

private

def clean_temp_images
temp_image_registry.each {|tmp| tmp.unlink }
end

def temp_image_registry
@temp_image_registry ||= []
end

# @param [String] base64
# @return [Tempfile]
def create_temp_imagefile(base64)
Expand All @@ -64,8 +64,8 @@ def create_temp_imagefile(base64)
file
ensure
file.close
end
end
end

end
end
2 changes: 1 addition & 1 deletion lib/thinreports/generator/pdf/document/graphics/text.rb
Expand Up @@ -34,7 +34,7 @@ def text_box(contents, x, y, w, h, attrs = {})

with_text_styles(attrs) do |built_attrs, font_styles|
contents = if inline_format
parsed_contents = ::Prawn::Text::Formatted::Parser.to_array(contents)
parsed_contents = ::Prawn::Text::Formatted::Parser.format(contents)
parsed_contents.each {|c| c[:styles] |= font_styles }
else
[{ text: contents, styles: font_styles }]
Expand Down
24 changes: 12 additions & 12 deletions lib/thinreports/generator/pdf/document/page.rb
Expand Up @@ -2,21 +2,21 @@

module ThinReports
module Generator

# @private
module PDF::Page
# Add JIS-B4,B5 page geometry
Prawn::Document::PageGeometry::SIZES.update(
::PDF::Core::PageGeometry::SIZES.update(
'B4_JIS' => [728.5, 1031.8],
'B5_JIS' => [515.9, 728.5]
)

# @param [ThinReports::Layout::Format] format
def start_new_page(format)
format_id = if change_page_format?(format)
pdf.start_new_page(new_basic_page_options(format))
@current_page_format = format

unless format_stamp_registry.include?(format.identifier)
create_format_stamp(format)
end
Expand All @@ -25,16 +25,16 @@ def start_new_page(format)
pdf.start_new_page(new_basic_page_options(current_page_format))
current_page_format.identifier
end

stamp(format_id.to_s)
end

def add_blank_page
pdf.start_new_page(pdf.page_count.zero? ? {size: 'A4'} : {})
end

private

# @return [ThinReports::Layout::Format]
attr_reader :current_page_format

Expand All @@ -44,20 +44,20 @@ def change_page_format?(new_format)
!current_page_format ||
current_page_format.identifier != new_format.identifier
end

# @param [ThinReports::Layout::Format] format
def create_format_stamp(format)
create_stamp(format.identifier.to_s) do
parse_svg(format.layout, '/svg/g')
end
format_stamp_registry << format.identifier
end

# @return [Array]
def format_stamp_registry
@format_stamp_registry ||= []
end

# @param [ThinReports::Layout::Format] format
# @return [Hash]
def new_basic_page_options(format)
Expand All @@ -80,6 +80,6 @@ def new_basic_page_options(format)
options
end
end

end
end
45 changes: 14 additions & 31 deletions lib/thinreports/generator/pdf/prawn_ext.rb
Expand Up @@ -3,46 +3,29 @@
# @private
module Prawn
class Document
# Create around alias.
alias_method :original_width_of, :width_of

def width_of(string, options={})
font.compute_width_of(string, options) +
(character_spacing * (font.character_count(string) - 1))
end
private
# Create around alias.
end

class Images::Image
alias_method :original_calc_image_dimensions, :calc_image_dimensions
def calc_image_dimensions(info, options)

def calc_image_dimensions(options)
if options[:auto_fit]
w, h = info.width, info.height
sw, sh = options.delete(:auto_fit)

if w > sw || h > sh
options[:fit] = [sw, sh]
end
end
original_calc_image_dimensions(info, options)
end
end
w = options[:width] || width
h = options[:height] || height

# Patch: https://github.com/prawnpdf/prawn/commit/34039d13b7886692debca11e85b9a572a20d57ee
module Core
class Reference
def <<(data)
(@stream ||= "") << data
@data[:Length] = @stream.length
@stream
end

def compress_stream
@stream = Zlib::Deflate.deflate(@stream)
@data[:Filter] = :FlateDecode
@data[:Length] = @stream.length
@compressed = true
box_width, box_height = options.delete(:auto_fit)

if w > box_width || h > box_height
options[:fit] = [box_width, box_height]
end
end
original_calc_image_dimensions(options)
end
end
end

0 comments on commit 4b2e4e5

Please sign in to comment.