Prawn PDF line suitable for bounding box

Is there a way to adjust the xy coordinates to fit in the bounding box in the prawn pdf if they are larger than the window height?

I use gem 'signature-pad-rails' to capture signatures, which then save the following:

[{"lx":98,"ly":23,"mx":98,"my":22},{"lx":98,"ly":21,"mx":98,"my":23},{"lx":98,"ly":18,"mx":98,"my":21}, ... {"lx":405,"ly":68,"mx":403,"my":67},{"lx":406,"ly":69,"mx":405,"my":68}] 

I have the following to show the signature in my pdf:

 bounding_box([0, cursor], width: 540, height: 100) do stroke_bounds @witness_signature.each do |e| stroke { line [e["lx"], 100 - e["ly"]], [e["mx"], 100 - e["my"] ] } end end 

But the signature in some cases runs off the page, is not the center and just starts up altogether.

+7
ruby-on-rails pdf prawn
source share
1 answer

Your question is pretty vague, so I guess what you mean.

To rescale the sequence of coordinates (x[i], y[i]), i = 1..n , to fit in the specified bounding box of size (width, height) with the beginning (0,0) , as in Postscript, first decide whether to keep the aspect ratio of the original image. The adaptation to the box, as a rule, does not do this. Since you probably do not want to distort the signature, say the answer is yes.

When scaling an image in a proportional rectangle, either the x or y axis determines the scale factor, unless the field has exactly that aspect of the image. So, you need to decide what to do with the "extra space" on the alternative axis. For example. if the image is tall and thin compared to the bounding box, the extra space will be on the x axis; if short and thick, this is the y axis.

Let's center the image in extra space; which seems suitable for signature.

Then here the pseudo-code rescales the points to fit the square:

 x_min = y_min = +infty, x_max = y_max = -infty for i in 1 to n if x[i] < x_min, x_min = x[i] if x[i] > x_max, x_max = x[i] if y[i] < y_min, y_min = y[i] if y[i] > y_max, y_max = y[i] end for dx = x_max - x_min dy = y_max - y_min x_scale = width / dx y_scale = height / dy if x_scale < y_scale then // extra space is on the y-dimension scale = x_scale x_org = 0 y_org = 0.5 * (height - dy * scale) // equal top and bottom extra space else // extra space is on the x_dimension scale = y_scale x_org = 0.5 * (width - dx * scale) // equal left and right extra space y_org = 0 end for i in 1 to n x[i] = x_org + scale * (x[i] - x_min) y[i] = y_org + scale * (y[i] - y_min) end 
+1
source share

All Articles