Class: Uniword::Watermark::Manager
- Inherits:
-
Object
- Object
- Uniword::Watermark::Manager
- Defined in:
- lib/uniword/watermark/manager.rb
Overview
Manages watermarks in a document.
Watermarks are implemented as shape elements in the default header. This manager adds/removes watermark shapes from the document’s header section.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
-
#add(text, color: "#808080", font_size: 72, font: "Segoe UI", opacity: ".5") ⇒ void
Add a text watermark to the document.
-
#initialize(document) ⇒ Manager
constructor
Initialize with a document.
-
#list ⇒ Array<String>
List all watermarks in the document.
-
#present? ⇒ Boolean
Check if the document has any watermarks.
-
#remove ⇒ Integer
Remove all watermarks from the document.
Constructor Details
#initialize(document) ⇒ Manager
Initialize with a document.
23 24 25 |
# File 'lib/uniword/watermark/manager.rb', line 23 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
18 19 20 |
# File 'lib/uniword/watermark/manager.rb', line 18 def document @document end |
Instance Method Details
#add(text, color: "#808080", font_size: 72, font: "Segoe UI", opacity: ".5") ⇒ void
This method returns an undefined value.
Add a text watermark to the document.
Creates a shape element in the default header with diagonal text at 50% opacity.
38 39 40 41 42 43 |
# File 'lib/uniword/watermark/manager.rb', line 38 def add(text, color: "#808080", font_size: 72, font: "Segoe UI", opacity: ".5") header = find_or_create_default_header mark = build_watermark(text, color, font_size, font, opacity) header.paragraphs << mark end |
#list ⇒ Array<String>
List all watermarks in the document.
72 73 74 75 76 77 78 79 80 |
# File 'lib/uniword/watermark/manager.rb', line 72 def list marks = [] (document.headers || []).each do |header| header.paragraphs.each do |p| marks << extract_watermark_text(p) if watermark?(p) end end marks end |
#present? ⇒ Boolean
Check if the document has any watermarks.
63 64 65 66 67 |
# File 'lib/uniword/watermark/manager.rb', line 63 def present? (document.headers || []).any? do |header| header.paragraphs.any? { |p| watermark?(p) } end end |
#remove ⇒ Integer
Remove all watermarks from the document.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/uniword/watermark/manager.rb', line 48 def remove count = 0 (document.headers || []).each do |header| before = header.paragraphs.size header.paragraphs.reject! do |p| watermark?(p) end count += before - header.paragraphs.size end count end |