1717
1818
1919class TextStreamAppearance (DecodedStreamObject ):
20- """A class representing the appearance stream for a text-based form field."""
20+ """
21+ A class representing the appearance stream for a text-based form field.
22+
23+ This class generates the content stream (the `ap_stream_data`) that dictates
24+ how text is rendered within a form field's bounding box. It handles properties
25+ like font, font size, color, multiline text, and text selection highlighting.
26+ """
2127 def _appearance_stream_data (
2228 self ,
2329 text : str = "" ,
@@ -29,6 +35,31 @@ def _appearance_stream_data(
2935 font_color : str = "0 g" ,
3036 multiline : bool = False
3137 ) -> bytes :
38+ """
39+ Generates the raw bytes of the PDF appearance stream for a text field.
40+
41+ This private method assembles the PDF content stream operators to draw
42+ the provided text within the specified rectangle. It handles text positioning,
43+ font application, color, and special formatting like selected text.
44+
45+ Args:
46+ text: The text to be rendered in the form field.
47+ selection: An optional list of strings that should be highlighted as selected.
48+ font_glyph_byte_map: An optional dictionary mapping characters to their
49+ byte representation for glyph encoding.
50+ rect: The bounding box of the form field. Can be a `RectangleObject`
51+ or a tuple of four floats (x1, y1, x2, y2).
52+ font_name: The name of the font resource to use (e.g., "/Helv").
53+ font_size: The font size. If 0, it is automatically calculated
54+ based on whether the field is multiline or not.
55+ font_color: The color to apply to the font, represented as a PDF
56+ graphics state string (e.g., "0 g" for black).
57+ multiline: A boolean indicating if the text field is multiline.
58+
59+ Returns:
60+ A byte string containing the PDF content stream data.
61+
62+ """
3263 font_glyph_byte_map = font_glyph_byte_map or {}
3364 if isinstance (rect , tuple ):
3465 rect = RectangleObject (rect )
@@ -79,6 +110,25 @@ def __init__(
79110 font_color : str = "0 g" ,
80111 multiline : bool = False
81112 ) -> None :
113+ """
114+ Initializes a TextStreamAppearance object.
115+
116+ This constructor creates a new PDF stream object configured as an XObject
117+ of subtype Form. It uses the `_appearance_stream_data` method to generate
118+ the content for the stream.
119+
120+ Args:
121+ text: The text to be rendered in the form field.
122+ selection: An optional list of strings that should be highlighted as selected.
123+ font_glyph_byte_map: A dictionary mapping characters to their byte representation.
124+ rect: The bounding box of the form field. Can be a `RectangleObject`
125+ or a tuple of four floats (x1, y1, x2, y2).
126+ font_name: The name of the font resource, e.g., "/Helv".
127+ font_size: The font size. If 0, it's auto-calculated.
128+ font_color: The font color string.
129+ multiline: A boolean indicating if the text field is multiline.
130+
131+ """
82132 font_glyph_byte_map = font_glyph_byte_map or {}
83133 ap_stream_data = self ._appearance_stream_data (
84134 text , selection , font_glyph_byte_map , rect , font_name , font_size , font_color , multiline
@@ -99,7 +149,27 @@ def from_text_annotation(
99149 user_font_name : str = "" ,
100150 user_font_size : float = - 1 ,
101151 ) -> "TextStreamAppearance" :
102- """Creates a TextStreamAppearance object from a given text field annotation."""
152+ """
153+ Creates a TextStreamAppearance object from a text field annotation.
154+
155+ This class method is a factory for creating a `TextStreamAppearance`
156+ instance by extracting all necessary information (bounding box, font,
157+ text content, etc.) from the PDF field and annotation dictionaries.
158+ It respects inheritance for properties like default appearance (`/DA`).
159+
160+ Args:
161+ acro_form: The root AcroForm dictionary from the PDF catalog.
162+ field: The field dictionary object.
163+ annotation: The widget annotation dictionary object associated with the field.
164+ user_font_name: An optional user-provided font name to override the
165+ default. Defaults to an empty string.
166+ user_font_size: An optional user-provided font size to override the
167+ default. A value of -1 indicates no override.
168+
169+ Returns:
170+ A new `TextStreamAppearance` instance configured for the given field.
171+
172+ """
103173 # Calculate rectangle dimensions
104174 _rect = cast (RectangleObject , annotation [AnnotationDictionaryAttributes .Rect ])
105175 rect = RectangleObject ((0 , 0 , abs (_rect [2 ] - _rect [0 ]), abs (_rect [3 ] - _rect [1 ])))
0 commit comments