如果它们的大小大于框的高度,是否可以调整xy坐标以适合Prawn PDF的边框?

我正在使用gem'signature-pad-rails'捕获签名,然后存储以下内容:

[{"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}]

以下是我在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

但是在某些情况下,签名不在页面上显示,不是居中,而是通常运行得很困惑。

最佳答案

您的问题很模糊,所以我猜您是什么意思。

要重新缩放一系列坐标(x[i], y[i]), i = 1..n以适合给定大小为(width, height)的边界框(如Postscript中那样带有原始(0,0)),首先确定是否保留原始图像的长宽比。安装在盒子上通常不会这样做。由于您可能不想使签名失真,因此答案为"is"。

当将图像缩放为保留框的宽高比时,x轴或y轴都会确定比例因子,除非框恰好恰好具有图像的宽高比。因此,下一步是决定如何处理备用轴上的“多余空间”。例如。如果图像与边界框相比又高又薄,则多余的空间将位于x轴上;如果又短又胖,则是y轴。

假设图片在额外空间内居中;似乎适合签名。

然后,这里是伪代码以重新缩放点以适合框:

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

关于ruby-on-rails - Prawn PDF线条适合边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43534586/

10-12 18:57