Prawn image position - ruby

I'm trying to layout 6 images per page with prawn in Ruby:
case (idx % 6) # ugly
when 0 : (pdf.start_new_page; pdf.image img, :position => :left, :vposition => :top, :width => 270)
when 1 : pdf.image img, :position => :right, :vposition => :top, :width => 270
when 2 : pdf.image img, :position => :left, :vposition => :center, :width => 270
when 3 : pdf.image img, :position => :right, :vposition => :center, :width => 270
when 4 : pdf.image img, :position => :left, :vposition => :bottom, :width => 270
when 5 : pdf.image img, :position => :right, :vposition => :bottom, :width => 270
end
Not sure what I'm doing wrong, but it prints the first 3 images to the PDF, then creates a new page and prints the last three:
Page 1:
<img> <img>
<blank> <blank>
<blank> <blank>
Page 2:
<blank> <blank>
<blank> <img>
<img> <img>
Any suggestions would help.

Image is going to flow (like text does) when you aren't explicitly positioning items.
Wrap each call in a float() { ... } and that will do the trick.
Alternatively, use prawn/grid for positioning.

Related

Retrieve first column value from TK::Tile::Treeview in Ruby

I try to retrieve the value of the first column from an item retrieved from a treeview using "item.get("#0")" but I got this error "RuntimeError: Display column #0 cannot be set". This method works for the other columns.
Can anyone help to figure out a solution?
Regards,
Marc
Here is a standalone example code:
require 'tk'
require 'tkextlib/tile'
$root = TkRoot.new
$frame = Tk::Tile::Frame.new($root)
$tree = Tk::Tile::Treeview.new($frame)
$tree['columns'] = ['action_text','action_description']
$tree.column_configure("#0", :width => 100)
$tree.heading_configure("#0", :text => 'la 1er colonne')
$tree.column_configure('action_text', :width => 100, :anchor => 'center')
$tree.heading_configure('action_text', :text => 'Text')
$tree.column_configure('action_description', :width => 100, :anchor => 'w')
$tree.heading_configure('action_description', :text => 'Description')
# Inserted at the root, program chooses id:
$tree.insert('', 'end', :id => 'widgets', :text => 'Widget Tour')
# Same thing, but inserted as first child:
$tree.insert('', 0, :id => 'gallery', :text => 'Applications')
# Treeview chooses the id:
item = $tree.insert('', 'end', :text => 'Tutorial')
# Inserted underneath an existing node:
$tree.insert( 'widgets', 'end', :text => 'Canvas')
$tree.insert( item, 'end', :text => 'Tree')
$tree.insert('', 2, :text => 'tata')
$tree.insert('', 'end', :text => 'envolee', :values => ['le centre', 'la description'])
$frame.grid(:column => 0, :row => 0, :sticky => 'nsew') {padding "3 3 12 12"}
$tree.grid(:column => 0, :row => 0, :sticky => 'nsew')
TkGrid.columnconfigure($root, 0, :weight => 1)
TkGrid.rowconfigure($root, 0, :weight => 1)
TkGrid.columnconfigure($frame, 0, :weight => 1)
TkGrid.rowconfigure($frame, 0, :weight => 1)
def create_action
item = $tree.selection_get[0]
puts item
puts "ID:"
puts item.id
puts "Index:"
puts item.index
puts "action_text:"
puts item.get('action_text')
puts "1:"
puts item.get('#1')
puts $tree.get(item.id, '#0')
puts "0:"
puts item.get("#0")
end
$tree.bind("Double-1") { create_action }
Tk.mainloop
I do not know if I understand exactly what you want to do,
item.text
might be a solution.

Prawn: Remove bottom padding for text_box

Here is an example where i draw text via text_box 3 times with different fonts.
anchor_point = {x: some_x, y: some_y}
pdf.fill_color '000000'
pdf.stroke_color 'FF0000'
pdf.font Consts::Fonts::VENEER do
p1 = {at: [anchor_point[:x], anchor_point[:y] + 30], w: 60, h: 17}
pdf.stroke_rectangle p1[:at], p1[:w], p1[:h]
pdf.text_box 'TEXT',
:at => p1[:at],
:width => p1[:w],
:height => p1[:h],
:min_font_size => 1,
:single_line => true,
:overflow => :shrink_to_fit,
:size => 17
end
pdf.font Consts::Fonts::ROBOTO do
p1 = {at: [anchor_point[:x], anchor_point[:y]], w: 60, h: 17}
pdf.stroke_rectangle p1[:at], p1[:w], p1[:h]
pdf.text_box 'TEXT',
:at => p1[:at],
:width => p1[:w],
:height => p1[:h],
:min_font_size => 1,
:single_line => true,
:overflow => :shrink_to_fit,
:size => 17
end
pdf.font Consts::Fonts::BEBAS do
p1 = {at: [anchor_point[:x], anchor_point[:y] - 30], w: 60, h: 17}
pdf.stroke_rectangle p1[:at], p1[:w], p1[:h]
pdf.text_box 'TEXT',
:at => p1[:at],
:width => p1[:w],
:height => p1[:h],
:min_font_size => 1,
:single_line => true,
:overflow => :shrink_to_fit,
:size => 17
end
And here is the result draw
I use stroke_rectangle to debug text_box bounds.
The question is:
How to remove bottom padding from text box. I tried change :leading in different ways but id does not have any effect.

Ruby Shoes Hover element

Hover element dosen't work. Where is problem? How fix it?
Shoes.app :width => 635, :height => 410 do
background image "http://PATH_TO_IMAGE"
a = stack :width => 360, :height => 200, :margin_left => 250, :margin_top => 200 do
hover do
a.clear { para "Some Text qwe qwe qwerty", :stroke => black }
end
leave do
a.clear { background image "http://PATH_TO_IMAGE" }
end
end
end
I think in principle you cannot use image as the argument for background.
I also am not sure if that is effect you want to achieve but it shows what you did wrong:
Shoes.app :width => 635, :height => 410 do
a = stack :width => 360, :height => 200 do
image "http://PATH_TO_IMAGE"
hover do
a.clear {para "Some Text qwe qwe qwerty", :stroke => black }
end
leave do
a.clear { image "http://PATH_TO_IMAGE" }
end
end
end

Can't align middle a list_box

I can't align middle a list_box in ruby shoes. I have test a few things, including :right => "50" or :left => "50", but it still not work.
Shoes.app do
stack :width => "100%", :height => "45%" do
a = list_box :items => ["lol","b"], :width => 50, :align => "center"
end
end
I did not find a direct way but you can use this hack
module Shoes::Types
def centr
left=(self.parent.width-self.style[:width])/2
self.move(left,self.top)
end
def middle
top=(self.parent.height-self.style[:height])/2
self.move(self.left,top)
end
end
Shoes.app do
#c=stack :width=>50, :height=>30 do
a=list_box :items => ["lol","b"], :width => 50, :height=>30
end
#c.centr.middle
end
What is done is actually first to extend the functionality of slots so that you can place them in the center or middle of the containing slot. Then you wrap your listbox in a tiny slot, which you center.

Line not rendering

I am developing some prawn reports and running into an issue where any line I draw with a code like the following will render only in the last page.
horizontal_line(0, 200, :at => y)
It is called once per page.
My code is relatively complex now so I tried to isolate the problem to post here, the isolated code follows
require 'prawn'
a = Prawn::Document.new(:page_size => 'A4', :margin => [20,20,20,20])
a.font('Times-Roman')
a.horizontal_line(10, 400, :at => 140)
a.text_box('Test Text', :size => 50, :at => [2, 100], :width => 400)
puts a.render
For my surprise, it didnĀ“t work even with a single page document. Only the "Test Text" is being rendered. It makes me think I am doing something wrong in the page setup or something like that.
Fond out the problem.
The correct use would be:
require 'prawn'
a = Prawn::Document.new(:page_size => 'A4', :margin => [20,20,20,20])
a.font('Times-Roman')
a.stroke do
a.horizontal_line(10, 400, :at => 140)
end
a.text_box('Test Text', :size => 50, :at => [2, 100], :width => 400)
puts a.render

Resources