虾:如何制作内容表的“领导点”?

问题描述 投票:2回答:2

我正在使用Prawn gem在我的Rails应用程序中生成PDF文档。我已经成功创建了PDF但是我有一个目录,我想知道是否可能或者是否有办法在Prawn enter image description here中创建领导者点效果?

ruby-on-rails prawn
2个回答
3
投票

是的,有一种方法可以在Praw中创建领导者点效果。

如果你有:

  1. 总可用宽度
  2. 内容条目表的宽度
  3. 页码的宽度
  4. 单个领导点的宽度

space_for_dots = 1. - 2. - 3.

dots = (space_for_dots / 4) * '.'

toc_entry = entry_string + dots + entry_page_number


要计算prawn中字符串的宽度,可以使用compute_width_of,它是字体类的一部分。

您添加的图片的示例可能如下所示:

def build_toc_entry(left_text, right_text, available_width, text_size)
  left_text_width = font(YOUR_FONT).compute_width_of(left_text, size: text_size)
  right_text_width = font(YOUR_FONT).compute_width_of(right_text, size: text_size)
  dot_width = font(YOUR_FONT).compute_width_of('.', size: text_size)
  space_width = font(YOUR_FONT).compute_width_of(' ', size: text_size)

  space_for_dots = available_width - left_text_width - right_text_width - space_width * 2
  dots = '.' * (space_for_dots / dot_width)

  "#{left_text} #{dots} #{right_text}" # return the finished toc entry
end

text 'Locate the information on the page indicated.'
text build_toc_entry('Leader Dots', '3', 350, 8)
text build_toc_entry('Dingbats', '5', 350, 8)
text build_toc_entry('Bullets', '8', 350, 8)

0
投票

这有点晚了但是我这样做是为了让点与左侧文本正确对齐。但我认为修改左右文本会相当容易:

def build_dots(text, available_width, text_size)
        dots_hash = {}
        current_font = font.inspect.split('<')[1].split(':')[0].strip
      text_width = font(current_font).compute_width_of(text, size: text_size)
      dot_width = font(current_font).compute_width_of('.', size: text_size)
      space_width = font(current_font).compute_width_of(' ', size: text_size)

      space_for_dots = available_width - text_width - space_width * 2
      dots = '.' * (space_for_dots / dot_width)
      dots_width = font(current_font).compute_width_of(dots, size: text_size)
      dots_start = available_width - dots_width - (space_width * 2)
      dots_hash[:dots] = dots
      dots_hash[:position] = dots_start

      return dots_hash
end

    dot_values = build_dots(officer.title,150,text_size)
    p = 0
    float {
        text "#{officer.title}", size: text_size
    }

    indent(dot_values[:position]) do
        float {
            text dot_values[:dots], size: text_size
        }
    end

    p += 150
    indent(p,0) do
        text "#{officer.name}", size: text_size
    end
© www.soinside.com 2019 - 2024. All rights reserved.