Class: Acrofill::Appearance

Inherits:
Object
  • Object
show all
Defined in:
lib/acrofill/appearance.rb

Overview

Builds the /AP /N appearance stream (a Form XObject) for a filled text-field widget, honouring the field's /DA string and /Q alignment.

Defined Under Namespace

Classes: Box

Constant Summary collapse

PADDING =
2.0
TOP_OFFSET =

pdftk offsets the first multiline row by 1pt from the box top.

1.0
COLOR_OP_ARITY =

Colour-setting operators allowed in a /DA string, and their operand counts.

{ 'g' => 1, 'rg' => 3, 'k' => 4 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(doc, acroform) ⇒ Appearance

Returns a new instance of Appearance.



16
17
18
19
20
# File 'lib/acrofill/appearance.rb', line 16

def initialize(doc, acroform)
  @doc = doc
  @acroform = acroform
  @fonts = Fonts.new(doc, acroform)
end

Instance Method Details

#build(field_node, widget, value, multiline: false, comb: nil) ⇒ Object

Returns a Reference to the new appearance XObject, or nil when the widget geometry is unusable.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/acrofill/appearance.rb', line 24

def build(field_node, widget, value, multiline: false, comb: nil)
  rect = normalized_rect(widget[:Rect] || @doc.inherited_value(field_node, :Rect))
  return nil unless rect

  box = Box.new(rect[2] - rect[0], rect[3] - rect[1], alignment(field_node))
  return nil if box.width <= 0 || box.height <= 0

  font_name, size, color_ops = parse_da(field_node)
  font = @fonts.metrics(font_name)
  size, body = draw(value, font, size, box, multiline: multiline, comb: comb)

  @doc.add(StreamObject.new(appearance_dict(font_name, box),
                            content(font_name, size, color_ops, body).b))
end