<commit_before>require "pdf_watermark/version"
require 'pdf_watermark/water_mark'
require 'combine_pdf'
module PdfWatermark
  file = __FILE__
  file = File.readlink(file) if File.symlink?(file)
  dir = File.dirname(file)


  BASEDIR = File.expand_path(File.join(dir, '..'))
  FONT_DIR = File.expand_path(File.join(dir, '..', 'fonts'))
  MAX_FONT_SIZE= 75

  def self.watermark(mark_string, source, destination, options={})
    default={
        angle: :diagonal,
        width: -1,
        height: -1,
        margin: 50,
        font: "#{FONT_DIR}/wqyzenhei.ttf",
        font_size: -1,
        font_color: "999999",
        transparent: 0.2
    }
    options = default.merge(options)

    source_pdf = CombinePDF.load source
    source_size = page_size(source_pdf.pages[0])

    @width = options[:width] <=0 ? source_size[0] : options[:width]
    @height = options[:height] <=0 ? source_size[1] : options[:height]
    wm = WaterMark.new(mark_string, options[:angle], @width, @height,
                       {:margin => options[:margin], :font => options[:font], :font_size => options[:font_size],
                        :transparent => options[:transparent], :font_color => options[:font_color]})
    wm.text_watermark

    water_mark_pdf = CombinePDF.parse(wm.render()).pages[0]

    source_pdf.pages.each do |page|
      page << water_mark_pdf
    end
    source_pdf.save destination


  end

  def self.page_size(page)
    mediabox = page.mediabox
    [mediabox[2]-mediabox[0], mediabox[3] - mediabox[1]]
  end
end<commit_msg>if destination nil return the pdf binary string<commit_after>require "pdf_watermark/version"
require 'pdf_watermark/water_mark'
require 'combine_pdf'
module PdfWatermark
  file = __FILE__
  file = File.readlink(file) if File.symlink?(file)
  dir = File.dirname(file)


  BASEDIR = File.expand_path(File.join(dir, '..'))
  FONT_DIR = File.expand_path(File.join(dir, '..', 'fonts'))
  MAX_FONT_SIZE= 75

  def self.watermark(mark_string, source, destination=nil, options={})
    default={
        angle: :diagonal,
        width: -1,
        height: -1,
        margin: 50,
        font: "#{FONT_DIR}/wqyzenhei.ttf",
        font_size: -1,
        font_color: "999999",
        transparent: 0.2
    }
    options = default.merge(options)

    source_pdf = CombinePDF.load source
    source_size = page_size(source_pdf.pages[0])

    @width = options[:width] <=0 ? source_size[0] : options[:width]
    @height = options[:height] <=0 ? source_size[1] : options[:height]
    wm = WaterMark.new(mark_string, options[:angle], @width, @height,
                       {:margin => options[:margin], :font => options[:font], :font_size => options[:font_size],
                        :transparent => options[:transparent], :font_color => options[:font_color]})
    wm.text_watermark

    water_mark_pdf = CombinePDF.parse(wm.render()).pages[0]

    source_pdf.pages.each do |page|
      page << water_mark_pdf
    end
    if destination.nil?
      source_pdf.to_pdf
    else
      source_pdf.save destination
    end
  end

  def self.page_size(page)
    mediabox = page.mediabox
    [mediabox[2]-mediabox[0], mediabox[3] - mediabox[1]]
  end
end
{'fraction_non_alphanumeric': 0.13893274392169244, 'fraction_numerical': 0.015787811809283233, 'mean_word_length': 3.1411764705882352, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6918897', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1065, 'n_words': 207}