shairontoledo / rghost

RGhost is a document creation and conversion API. It uses the Ghostscript framework for the format conversion, utilizes EPS templates and is optimized to work with larger documents. Support(PDF,PS,GIF,TIF,PNG,JPG,etc)
http://rghost.rubyforge.org
MIT License
187 stars 46 forks source link

Changing text area alignment changes height between text rows #63

Open hiemanshu opened 9 years ago

hiemanshu commented 9 years ago

Changing the :text_align given to text_area produces a different entry tag. Left align starts with :text_area _default_font (abc) :text_proc, but any other alignment(center or right) starts with _default_font (abc) :text_proc_cr instead. Why is there a difference in the entry points?

The effects (or side effects) of this is that the row height is different on different alignments. Right align looks like: Right align Left align looks like Left Align

shairontoledo commented 9 years ago

Please include the code snippet you're using

hiemanshu commented 9 years ago
document.define_tags do
  tag font_tag.to_sym, :name => font.rghost_font_name, :size => FONT_SIZE, :color => color
end
formatted_text = format_multiline_text text, font_tag
document.newpath do |path|
  path.moveto :x => 1, :y => 49
  path.text_area formatted_text, :text_align => text_alignment.to_sym
end

This is what the code snippet looks like. The only thing that has changed between the two images in the text_alignment variable.

shairontoledo commented 9 years ago

@hiemanshu hard to figure out what's in format_multiline_text, rghost_font_name etc, can you create a file .rb which I can simply run it via ruby yourfile.rb ?

hiemanshu commented 9 years ago

@shairontoledo Thats something we have to format text in the right format, and using fonts stored in the database. The important part of the code is below:

require 'rghost'

doc = RGhost::Document.new
doc.newpath do |path|
  path.moveto :x => 1, :y => 49
  path.text_area "abc", :text_align => :right
end
puts doc.inspect

doc2 = RGhost::Document.new
doc2.newpath do |path|
  path.moveto :x => 1, :y => 49
  path.text_area "abc", :text_align => :left
end
puts doc2.inspect

Just running this will show you the difference in the entry points. https://github.com/shairontoledo/rghost/blob/master/lib/rghost/parse_text.rb#L46 is the line which determines the entry point.

hiemanshu commented 9 years ago

@shairontoledo Any update on this?

shairontoledo commented 9 years ago

Got a free time now, going to debug that.

shairontoledo commented 9 years ago

It's something else, RGhost uses different objects to align text at left and right, regardless of that, text_area is behaving as expected. I did simple test

doc = RGhost::Document.new row_height: 1
doc.define_tags do 
  tag :big, name: 'Helvetica', size: 24
end

text1 = '<big>%s</big>' % 12.times.map{|i| 'right'}.join(' ')
text2 = '<big>%s</big>' % 12.times.map{|i| 'left'}.join(' ')

doc.text_area text1, width: 6, text_align: :left, x: 12
doc.text_area text2, width: 6, text_align: :right, x: 6

doc.render(:pdf, filename: '/tmp/test.pdf')

It resulted in:

page