class PdfWatermark::WaterMark::Base

Public Class Methods

new(mark_string, source_path, options) click to toggle source
# File lib/pdf_watermark/water_mark/base.rb, line 9
def initialize(mark_string, source_path, options)
  @source_path = source_path
  @options = options
  source_size = page_size
  @content_width = source_size[0] - (options[:margin][1] + options[:margin][3])
  @content_height = source_size[1] - (options[:margin][0] + options[:margin][2])
  @angle = options[:angle] == :diagonal ? rad_to_deg(Math.atan(@content_height.to_f/@content_width.to_f)) : options[:angle]
  @mark_string = mark_string

  @font_size = @options[:font_size]
  if @font_size.is_a?(String)
    if @font_size =~ /(\d+[.]?\d*)%/
      @font_size = ($1.to_f / 100) * @content_width
      @font_size = [@font_size, @options[:max_font_size]].min
      @font_size = [@font_size, @options[:min_font_size]].max
    else
      @font_size = @font_size.to_i
    end
  end
  @options[:font]
  @x = @options[:x] || 0
  @y = @options[:y] || @content_height

  @font = load_font(@options[:font], :watermark_font)
end

Protected Instance Methods

document() click to toggle source
# File lib/pdf_watermark/water_mark/base.rb, line 52
def document
  @document ||= HexaPDF::Document.open(@source_path)
end
load_font(font_path, name = nil) click to toggle source
# File lib/pdf_watermark/water_mark/base.rb, line 38
def load_font(font_path, name = nil)
  font_name = name.nil? ? File.basename(font_path, '.ttf') : name
  map = {}
  map[font_name] = { none: font_path }
  document.config['font.map'].merge!(map)

  document.fonts.load(font_name).wrapped_font
end
page_size() click to toggle source
# File lib/pdf_watermark/water_mark/base.rb, line 47
def page_size
  box = document.pages[0].box.value
  [box[2] - box[0], box[3] - box[1]]
end