Skip to content

Commit

Permalink
Fix some bugs and make the example much more awesomer
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Jul 24, 2008
1 parent d50daf0 commit 2be79d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Binary file added data/images/pigs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions examples/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require "prawn"

Prawn::Document.generate("image.pdf") do
filename = File.join("#{Prawn::BASEDIR}/data/images/stef.jpg")
image filename, :at => [200, 400]
Prawn::Document.generate("image.pdf", :page_layout => :landscape) do
pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
image pigs, :at => [50,550]

stef = "#{Prawn::BASEDIR}/data/images/stef.jpg"
image stef, :at => [75, 75]

text "Please enjoy the pigs", :size => 36, :at => [200,15]
end
15 changes: 6 additions & 9 deletions lib/prawn/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ def image(filename, options={})
# add a reference to the image object to the current page
# resource list and give it a label
label = "I#{next_image_id}"
page_xobjects.merge!( label => image_obj )
page_xobjects.merge!( label => image_obj )

# add the image to the current page
instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
add_content instruct % [ image_info.width, image_info.height, x, y, label ]
add_content instruct % [ image_info.width, image_info.height, x,
y - image_info.height, label ]
end

private
Expand All @@ -59,7 +60,7 @@ def build_jpg_object(info, size)
:Subtype => :Image,
:ColorSpace => :DeviceRGB,
:Filter => :DCTDecode,
:BitsPerComponent => 8,
:BitsPerComponent => info.bits,
:Width => info.width,
:Height => info.height,
:Length => size
Expand All @@ -76,13 +77,9 @@ def build_jpg_object(info, size)
obj
end

def image_counter
@image_counter ||= 0
end

def next_image_id
counter = image_counter
counter += 1
@image_counter ||= 0
@image_counter += 1
end
end
end

0 comments on commit 2be79d4

Please sign in to comment.