gamba 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -1
- data/README.md +26 -4
- data/lib/gamba/document.rb +5 -2
- data/lib/gamba/invoice/cross_industry_invoice.rb +255 -0
- data/lib/gamba/invoice/rules.rb +183 -0
- data/lib/gamba/invoice.rb +272 -0
- data/lib/gamba/native/document.rb +7 -1
- data/lib/gamba/pdf/decryption.rb +1 -20
- data/lib/gamba/pdf/writer.rb +34 -5
- data/lib/gamba/pdf.rb +15 -1
- data/lib/gamba/pdf_a.rb +4 -1
- data/lib/gamba/security.rb +133 -5
- data/lib/gamba/version.rb +1 -1
- data/lib/gamba/watermark.rb +108 -0
- data/lib/gamba.rb +4 -0
- metadata +294 -278
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gamba
|
|
4
|
+
# A text across every page, beneath its content: watermark: "Draft", or a hash with the text and any of font, style,
|
|
5
|
+
# size, angle (degrees), color and opacity. Each page calls its size's stamp as it opens, so the watermark lies
|
|
6
|
+
# above a background and beneath what the page draws next; the stamp is drawn when the document renders, once its
|
|
7
|
+
# fonts are registered.
|
|
8
|
+
class Watermark
|
|
9
|
+
OPTIONS = %i[text font style size angle color opacity].freeze
|
|
10
|
+
STYLES = %i[normal bold italic bold_italic].freeze
|
|
11
|
+
# Without a size, the rotated text spans at most this share of the page's width and height.
|
|
12
|
+
FILL_RATIO = 0.8
|
|
13
|
+
ARTIFACT = %i[Pagination Watermark].freeze
|
|
14
|
+
# A page can open inside a block that changed the text state, which a form XObject inherits.
|
|
15
|
+
TEXT_STATE_RESET = "0 Tr 0 Tc 0 Tw 100 Tz 0 Ts"
|
|
16
|
+
|
|
17
|
+
def self.from_option(option)
|
|
18
|
+
return unless option
|
|
19
|
+
raise ArgumentError, "watermark is #{option.inspect}: give its text, or a hash with the text and its options" unless option.is_a?(String) || option.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
option = {text: option} if option.is_a?(String)
|
|
22
|
+
unknown = option.keys - OPTIONS
|
|
23
|
+
raise ArgumentError, "unknown watermark option#{"s" if unknown.size > 1} #{unknown.map(&:inspect).join(", ")}: accepted options are #{OPTIONS.map(&:inspect).join(", ")}" if unknown.any?
|
|
24
|
+
|
|
25
|
+
new(**option.compact)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(text:, font: nil, style: nil, size: nil, angle: nil, color: "808080", opacity: 0.2)
|
|
29
|
+
@text = text.to_s
|
|
30
|
+
raise ArgumentError, "the watermark text #{text.inspect} shows nothing" unless Tagging.visible_text?(@text)
|
|
31
|
+
raise ArgumentError, "the watermark text #{text.inspect} holds a line break or a control character: it is one line" if @text.match?(/[[:cntrl:]
]/)
|
|
32
|
+
raise ArgumentError, "the watermark style is #{style.inspect}: give one of #{STYLES.map(&:inspect).join(", ")}" unless style.nil? || STYLES.include?(style)
|
|
33
|
+
raise ArgumentError, "the watermark size is #{size.inspect}: give a positive number of points" if size && !(size.is_a?(Numeric) && size.finite? && size.positive?)
|
|
34
|
+
raise ArgumentError, "the watermark angle is #{angle.inspect}: give a number of degrees" if angle && !(angle.is_a?(Numeric) && angle.finite?)
|
|
35
|
+
raise ArgumentError, "the watermark opacity is #{opacity.inspect}: give a number from 0 to 1" unless opacity.is_a?(Numeric) && opacity.between?(0, 1)
|
|
36
|
+
raise ArgumentError, "the watermark color is #{color.inspect}: give RGB as \"808080\" or CMYK as [0, 0, 0, 50]" unless color?(color)
|
|
37
|
+
|
|
38
|
+
@font = font
|
|
39
|
+
@style = style
|
|
40
|
+
@size = size
|
|
41
|
+
@angle = angle
|
|
42
|
+
@color = color
|
|
43
|
+
@opacity = opacity
|
|
44
|
+
@unfilled_stamps = []
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def open_page(document)
|
|
48
|
+
_left, _bottom, width, height = document.page.dimensions
|
|
49
|
+
name = "Gamba watermark #{width}x#{height}"
|
|
50
|
+
@unfilled_stamps << [document.__send__(:create_stamp_dictionary, name), width, height] unless document.__send__(:stamp_dictionary_registry).key?(name)
|
|
51
|
+
document.tag_artifact(*ARTIFACT) { document.stamp(name) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def fill_stamps(document)
|
|
55
|
+
@unfilled_stamps.each do |dictionary, width, height|
|
|
56
|
+
document.page.stamp_stream(dictionary) { document.tag_artifact(*ARTIFACT) { draw(document, width, height) } }
|
|
57
|
+
end
|
|
58
|
+
@unfilled_stamps.clear
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def standard_font(document)
|
|
64
|
+
message = "the watermark needs font: a registered TrueType or OpenType family, as PDF/A and PDF/UA embed every font"
|
|
65
|
+
raise Prawn::Errors::PdfAViolation, message if document.pdf_a?
|
|
66
|
+
raise Prawn::Errors::PdfUaViolation, message if document.pdf_ua?
|
|
67
|
+
|
|
68
|
+
"Helvetica"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def color?(color)
|
|
72
|
+
(color.is_a?(String) && color.match?(/\A\h{6}\z/)) || (color.is_a?(Array) && color.size == 4 && color.all?(Numeric))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# By default it runs along the page's diagonal, from the bottom left corner to the top right one.
|
|
76
|
+
def draw(document, width, height)
|
|
77
|
+
family = @font || document.default_font_family || standard_font(document)
|
|
78
|
+
style = @style || (document.font_families.fetch(family, {}).key?(:bold) ? :bold : :normal)
|
|
79
|
+
angle = @angle || (Math.atan2(height, width) * 180 / Math::PI)
|
|
80
|
+
center_x = width / 2.0
|
|
81
|
+
center_y = height / 2.0
|
|
82
|
+
document.add_content(TEXT_STATE_RESET)
|
|
83
|
+
document.canvas do
|
|
84
|
+
document.font(family, style:) do
|
|
85
|
+
document.font_size(@size || fitted_size(document, angle, width, height)) do
|
|
86
|
+
kerning = document.default_kerning?
|
|
87
|
+
font = document.font
|
|
88
|
+
baseline = [center_x - (document.width_of(@text, kerning:) / 2), center_y - ((font.ascender - font.descender) / 2)]
|
|
89
|
+
document.fill_color(@color)
|
|
90
|
+
document.transparent(@opacity) { document.rotate(angle, origin: [center_x, center_y]) { document.draw_text(@text, at: baseline, kerning:) } }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The rotated box of the text, its width by the font's height, fits the share of the page FILL_RATIO leaves.
|
|
97
|
+
def fitted_size(document, angle, width, height)
|
|
98
|
+
radians = angle * Math::PI / 180
|
|
99
|
+
cosine = Math.cos(radians).abs
|
|
100
|
+
sine = Math.sin(radians).abs
|
|
101
|
+
text_width = document.width_of(@text, size: 1, kerning: document.default_kerning?)
|
|
102
|
+
raise ArgumentError, "the watermark text #{@text.inspect} has no width in #{document.font.name}" unless text_width.positive?
|
|
103
|
+
|
|
104
|
+
text_height = document.font.height_at(1)
|
|
105
|
+
[FILL_RATIO * width / ((text_width * cosine) + (text_height * sine)), FILL_RATIO * height / ((text_width * sine) + (text_height * cosine))].min
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
data/lib/gamba.rb
CHANGED
|
@@ -50,6 +50,7 @@ require_relative "gamba/text/formatted/box"
|
|
|
50
50
|
require_relative "gamba/text/box"
|
|
51
51
|
require_relative "gamba/annotations"
|
|
52
52
|
require_relative "gamba/stamp"
|
|
53
|
+
require_relative "gamba/watermark"
|
|
53
54
|
require_relative "gamba/measurements"
|
|
54
55
|
require_relative "gamba/view"
|
|
55
56
|
require_relative "gamba/images"
|
|
@@ -65,6 +66,9 @@ require_relative "gamba/table/cell"
|
|
|
65
66
|
require_relative "gamba/table/cell/kinds"
|
|
66
67
|
require_relative "gamba/security"
|
|
67
68
|
require_relative "gamba/pdf_a"
|
|
69
|
+
require_relative "gamba/invoice"
|
|
70
|
+
require_relative "gamba/invoice/cross_industry_invoice"
|
|
71
|
+
require_relative "gamba/invoice/rules"
|
|
68
72
|
require_relative "gamba/tagging"
|
|
69
73
|
require_relative "gamba/pdf_a/srgb_profile"
|
|
70
74
|
require_relative "gamba/document/internals"
|