Class: HexaPDF::Type::AcroForm::VariableTextField
- Inherits:
-
Field
- Object
- Object
- Dictionary
- Field
- HexaPDF::Type::AcroForm::VariableTextField
- Defined in:
- lib/hexapdf/type/acro_form/variable_text_field.rb
Overview
An AcroForm variable text field defines how text that it is not known at generation time should be rendered. For example, AcroForm text fields (normally) don’t have an initial value; the value is entered by the user and needs to be rendered correctly by the PDF reader.
See: PDF2.0 s12.7.4.3
Direct Known Subclasses
Constant Summary collapse
- INHERITABLE_FIELDS =
All inheritable dictionary fields for text fields.
(superclass::INHERITABLE_FIELDS + [:DA, :Q]).freeze
- UNSET_ARG =
:nodoc:
::Object.new
Constants included from DictionaryFields
DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate
Instance Attribute Summary
Attributes inherited from Object
#data, #document, #must_be_indirect
Class Method Summary collapse
-
.create_appearance_string(document, font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) ⇒ Object
Creates an AcroForm appearance string for the HexaPDF
document
from the given arguments and returns it. -
.parse_appearance_string(appearance_string, &block) ⇒ Object
:call-seq: VariableTextField.parse_appearance_string(string) -> [font_name, font_size, font_color] VariableTextField.parse_appearance_string(string) {|obj, params| block } -> nil.
Instance Method Summary collapse
-
#parse_default_appearance_string ⇒ Object
Parses the default appearance string and returns an array containing [font_name, font_size, font_color].
-
#set_default_appearance_string(font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) ⇒ Object
Sets the default appearance string using the provided values or the default values which provide a sane default.
-
#text_alignment(alignment = UNSET_ARG) ⇒ Object
:call-seq: field.text_alignment -> alignment field.text_alignment(alignment) -> field.
Methods inherited from Field
#[], #alternate_field_name, #alternate_field_name=, #concrete_field_type, #create_widget, #delete_widget, #each_widget, #embedded_widget?, #field_name, #field_type, #flags, #full_field_name, inherited_value, #must_be_indirect?, #terminal_field?
Methods included from Utils::BitField
Methods inherited from Dictionary
#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_h, type, #type
Methods inherited from Object
#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=
Constructor Details
This class inherits a constructor from HexaPDF::Object
Class Method Details
.create_appearance_string(document, font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) ⇒ Object
Creates an AcroForm appearance string for the HexaPDF document
from the given arguments and returns it.
font
-
The name of the font.
font_options
-
Additional font options like :variant used when loading the font. See HexaPDF::Document::Fonts#add
font_size
-
The font size. If this is set to 0, the font size is calculated using the height/width of the field.
font_color
-
The font color. See HexaPDF::Content::ColorSpace.device_color_from_specification for allowed values.
81 82 83 84 85 86 87 88 |
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 81 def self.create_appearance_string(document, font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) name = document.acro_form(create: true).default_resources. add_font(document.fonts.add(font, **).pdf_object) font_color = HexaPDF::Content::ColorSpace.device_color_from_specification(font_color) color_string = HexaPDF::Content::ColorSpace.serialize_device_color(font_color) "#{color_string.chomp} /#{name} #{font_size} Tf" end |
.parse_appearance_string(appearance_string, &block) ⇒ Object
:call-seq:
VariableTextField.parse_appearance_string(string) -> [font_name, font_size, font_color]
VariableTextField.parse_appearance_string(string) {|obj, params| block } -> nil
Parses the given appearance string.
If no block is given, the appearance string is searched for font name, font size and font color all of which are returned. Otherwise the block is called with each found content stream operator and has to handle them itself.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 99 def self.parse_appearance_string(appearance_string, &block) # :yield: obj, params font_params = [nil, nil, nil] block ||= lambda do |obj, params| case obj when :Tf font_params[0, 2] = params when :rg, :g, :k font_params[2] = HexaPDF::Content::ColorSpace.prenormalized_device_color(params) end end HexaPDF::Content::Parser.parse(appearance_string.sub(/\/\//, '/'), &block) block_given? ? nil : font_params end |
Instance Method Details
#parse_default_appearance_string ⇒ Object
Parses the default appearance string and returns an array containing [font_name, font_size, font_color].
The default appearance string is taken from the field or, if not set, the default appearance string of the form.
158 159 160 161 162 |
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 158 def parse_default_appearance_string da = self[:DA] || (document.acro_form && document.acro_form[:DA]) raise HexaPDF::Error, "No default appearance string set" unless da self.class.parse_appearance_string(da) end |
#set_default_appearance_string(font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) ⇒ Object
Sets the default appearance string using the provided values or the default values which provide a sane default.
See ::create_appearance_string for information on the arguments.
145 146 147 148 149 150 151 |
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 145 def set_default_appearance_string(font: 'Helvetica', font_options: {}, font_size: 0, font_color: 0) self[:DA] = self.class.create_appearance_string(document, font: font, font_options: , font_size: font_size, font_color: font_color) end |
#text_alignment(alignment = UNSET_ARG) ⇒ Object
:call-seq:
field.text_alignment -> alignment
field.text_alignment(alignment) -> field
Sets or returns the text alignment that should be used when displaying text.
With no argument, the current text alignment is returned. When a value is provided, the text alignment is set accordingly.
The alignment value is one of :left, :center or :right.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 123 def text_alignment(alignment = UNSET_ARG) if alignment == UNSET_ARG case self[:Q] when 0 then :left when 1 then :center when 2 then :right end else self[:Q] = case alignment when :left then 0 when :center then 1 when :right then 2 else raise ArgumentError, "Invalid variable text field alignment #{alignment}" end end end |