From 804ce7dcff1c7b40993fcf9d22f3bd33c535a38c Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Fri, 14 Oct 2022 11:05:37 +0200 Subject: [PATCH] Add callbacks and extensions --- fourmi-prawn-utils.gemspec | 14 +++-- .../prawn/utils/callbacks/border_callback.rb | 14 +++++ .../utils/callbacks/highlight_callback.rb | 19 +++++++ lib/fourmi/prawn/utils/extensions/box.rb | 53 +++++++++++++++++++ .../utils/extensions/stroke_rounded_bounds.rb | 12 +++++ .../utils/extensions/strokes_and_colors.rb | 30 +++++++++++ .../prawn/utils/extensions/text_align.rb | 21 ++++++++ .../prawn/utils/extensions/visual_form.rb | 32 +++++++++++ 8 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 lib/fourmi/prawn/utils/callbacks/border_callback.rb create mode 100644 lib/fourmi/prawn/utils/callbacks/highlight_callback.rb create mode 100644 lib/fourmi/prawn/utils/extensions/box.rb create mode 100644 lib/fourmi/prawn/utils/extensions/stroke_rounded_bounds.rb create mode 100644 lib/fourmi/prawn/utils/extensions/strokes_and_colors.rb create mode 100644 lib/fourmi/prawn/utils/extensions/text_align.rb create mode 100644 lib/fourmi/prawn/utils/extensions/visual_form.rb diff --git a/fourmi-prawn-utils.gemspec b/fourmi-prawn-utils.gemspec index 730ba33..5227b15 100644 --- a/fourmi-prawn-utils.gemspec +++ b/fourmi-prawn-utils.gemspec @@ -8,17 +8,14 @@ Gem::Specification.new do |spec| spec.authors = ["Guillaume Dott"] spec.email = ["guillaume+github@dott.fr"] - spec.summary = "TODO: Write a short summary, because RubyGems requires one." - spec.description = "TODO: Write a longer description or delete this line." - spec.homepage = "TODO: Put your gem's website or public repo URL here." + spec.summary = "Some methods to simplify PDF creation with Prawn" + spec.description = "Some methods to simplify PDF creation with Prawn" + spec.homepage = "https://code.lafourmi-immo.com/lafourmi-immo/prawn-utils" spec.license = "MIT" spec.required_ruby_version = ">= 2.6.0" - spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" - spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here." - spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." + spec.metadata["source_code_uri"] = "https://code.lafourmi-immo.com/lafourmi-immo/prawn-utils" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. @@ -32,7 +29,8 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] # Uncomment to register a new dependency of your gem - # spec.add_dependency "example-gem", "~> 1.0" + spec.add_dependency "prawn" + spec.add_dependency "prawn-blank" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/lib/fourmi/prawn/utils/callbacks/border_callback.rb b/lib/fourmi/prawn/utils/callbacks/border_callback.rb new file mode 100644 index 0000000..8f7e824 --- /dev/null +++ b/lib/fourmi/prawn/utils/callbacks/border_callback.rb @@ -0,0 +1,14 @@ +module Fourmi::Prawn::Utils + module Callbacks + class BorderCallback + def initialize(document) + @document = document + end + + def render_in_front(fragment) + @document.stroke_polygon(fragment.top_left, fragment.top_right, + fragment.bottom_right, fragment.bottom_left) + end + end + end +end diff --git a/lib/fourmi/prawn/utils/callbacks/highlight_callback.rb b/lib/fourmi/prawn/utils/callbacks/highlight_callback.rb new file mode 100644 index 0000000..8b0c491 --- /dev/null +++ b/lib/fourmi/prawn/utils/callbacks/highlight_callback.rb @@ -0,0 +1,19 @@ +module Fourmi::Prawn::Utils + module Callbacks + class HighlightCallback + def initialize(color, document, padding_top = 0, padding_left = 0) + @color = color + @document = document + @padding_top = padding_top + @padding_left = padding_left + end + + def render_behind(fragment) + original_color = @document.fill_color + @document.fill_color = @color + @document.fill_rectangle([fragment.left - @padding_left, fragment.top + @padding_top], fragment.width + 2 * @padding_left, fragment.height + 2 * @padding_top) + @document.fill_color = original_color + end + end + end +end diff --git a/lib/fourmi/prawn/utils/extensions/box.rb b/lib/fourmi/prawn/utils/extensions/box.rb new file mode 100644 index 0000000..7314442 --- /dev/null +++ b/lib/fourmi/prawn/utils/extensions/box.rb @@ -0,0 +1,53 @@ +require 'fourmi/prawn/utils/extensions/text_align' + +module Fourmi::Prawn::Utils + module Extensions + module Box + def textbox(content = nil, height = 40) + bounding_box([0, cursor], height: content.blank? && !block_given? ? height : nil, width: bounds.width) do + pad(5) do + indent(5) do + text content unless content.blank? + yield if block_given? + end + end + stroke_bounds + end + end + + def padded_box(position, padding, **options, &block) + rounded = options.delete(:rounded) + rounded = true if rounded.nil? + + bounding_box position, options do + bounding_box [padding, bounds.top - padding], width: bounds.width - 2 * padding, height: bounds.height > 2 * padding ? bounds.height - 2 * padding : nil do + yield block + move_down padding + end + if rounded + stroke_rounded_bounds + else + stroke_bounds + end + end + end + + def filled_box(color, x: 0, y: cursor, width: bounds.width, height: 150, &block) + fill_color(color) do + fill_rectangle [x, y], width, height + end + bounding_box([x, y], width: width, height: height, &block) + end + + def list(text, options = {}) + float { indent(5) { text '- ' } } + indent 15 do + textj text, options + end + end + end + end +end + +require 'prawn/document' +Prawn::Document.extensions << Fourmi::Prawn::Utils::Extensions::Box diff --git a/lib/fourmi/prawn/utils/extensions/stroke_rounded_bounds.rb b/lib/fourmi/prawn/utils/extensions/stroke_rounded_bounds.rb new file mode 100644 index 0000000..88f7021 --- /dev/null +++ b/lib/fourmi/prawn/utils/extensions/stroke_rounded_bounds.rb @@ -0,0 +1,12 @@ +module Fourmi::Prawn::Utils + module Extensions + module StrokeRoundedBounds + def stroke_rounded_bounds(radius = 2) + stroke_rounded_rectangle [0, bounds.top], bounds.width, bounds.height, radius + end + end + end +end + +require 'prawn/document' +Prawn::Document.extensions << Fourmi::Prawn::Utils::Extensions::StrokeRoundedBounds diff --git a/lib/fourmi/prawn/utils/extensions/strokes_and_colors.rb b/lib/fourmi/prawn/utils/extensions/strokes_and_colors.rb new file mode 100644 index 0000000..63eb74a --- /dev/null +++ b/lib/fourmi/prawn/utils/extensions/strokes_and_colors.rb @@ -0,0 +1,30 @@ +module Fourmi::Prawn::Utils + module Extensions + module StrokesAndColors + def with_fill_color(color) + original_color = fill_color + self.fill_color = color + + yield if block_given? + ensure + self.fill_color = original_color + end + + def with_stroke_properties(color: nil, width: nil) + original_color = stroke_color + original_width = line_width + + self.stroke_color = color unless color.nil? + self.line_width = width unless width.nil? + + yield if block_given? + ensure + self.stroke_color = original_color + self.line_width = original_width + end + end + end +end + +require 'prawn/document' +Prawn::Document.extensions << Fourmi::Prawn::Utils::Extensions::StrokesAndColors diff --git a/lib/fourmi/prawn/utils/extensions/text_align.rb b/lib/fourmi/prawn/utils/extensions/text_align.rb new file mode 100644 index 0000000..4fb2556 --- /dev/null +++ b/lib/fourmi/prawn/utils/extensions/text_align.rb @@ -0,0 +1,21 @@ +module Fourmi::Prawn::Utils + module Extensions + module TextAlign + def paragraph(string, options = {}) + textj string, options + move_down 4 + end + + def textc(string, options = {}) + text string, {align: :center}.merge(options) + end + + def textj(string, options = {}) + text string, {align: :justify}.merge(options) + end + end + end +end + +require 'prawn/document' +Prawn::Document.extensions << Fourmi::Prawn::Utils::Extensions::TextAlign diff --git a/lib/fourmi/prawn/utils/extensions/visual_form.rb b/lib/fourmi/prawn/utils/extensions/visual_form.rb new file mode 100644 index 0000000..495dba0 --- /dev/null +++ b/lib/fourmi/prawn/utils/extensions/visual_form.rb @@ -0,0 +1,32 @@ +require 'fourmi/prawn/utils/extensions/strokes_and_colors' +require 'prawn/blank' + +module Fourmi::Prawn::Utils + module Extensions + module VisualForm + def visual_checkbox(name, checked = false, size: 8, white: false, form: false) + bounding_box([0, cursor], width: bounds.width) do + float do + bounding_box([0, cursor], width: size, height: size) do + if form + checkbox name: name, at: [0, bounds.top], checked: checked, width: bounds.width, height: bounds.height + else + with_fill_color 'FFFFFF' do + fill_rectangle [0, bounds.top], bounds.width, bounds.height if white + end + fill_rectangle [2, bounds.height - 2], bounds.width - 4, bounds.height - 4 if checked + end + stroke_bounds + end + end + indent(size + 7) do + yield + end if block_given? + end + end + end + end +end + +require 'prawn/document' +Prawn::Document.extensions << Fourmi::Prawn::Utils::Extensions::VisualForm