Add callbacks and extensions
parent
de7de8f35c
commit
804ce7dcff
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue