@@ -33,13 +33,15 @@ def _parse_font_angle_justify(
33
33
'+a45'
34
34
>>> _parse_font_angle_justify(None, None, "CM")
35
35
'+jCM'
36
+ >>> _parse_font_angle_justify("10p", 45, None)
37
+ '+f10p+a45'
36
38
>>> _parse_font_angle_justify("10p,Helvetica-Bold", 45, "CM")
37
39
'+f10p,Helvetica-Bold+a45+jCM'
38
40
"""
39
- args = (( font , "+f" ), ( angle , "+a" ), ( justify , "+j" ))
40
- if all (arg is None for arg , _ in args ):
41
+ args = { "+f" : font , "+a" : angle , "+j" : justify }
42
+ if all (arg is None for arg in args . values () ):
41
43
return None
42
- return "" .join (f"{ flag } { arg } " for arg , flag in args if arg is not None )
44
+ return "" .join (f"{ prefix } { arg } " for prefix , arg in args . items () if arg is not None )
43
45
44
46
45
47
def paragraph (
@@ -62,8 +64,8 @@ def paragraph(
62
64
x/y
63
65
The x, y coordinates of the paragraph.
64
66
text
65
- The paragraph text to typeset. If a sequence of strings is provided, each
66
- string is treated as a separate paragraph.
67
+ The paragraph text to typeset. If a sequence of strings is provided, each string
68
+ is treated as a separate paragraph.
67
69
parwidth
68
70
The width of the paragraph.
69
71
linespacing
@@ -91,23 +93,19 @@ def paragraph(
91
93
confdict = {}
92
94
# Prepare the keyword dictionary for the module options
93
95
kwdict = {"M" : True , "F" : _parse_font_angle_justify (font , angle , justify )}
94
-
95
- # Initialize a stringio object for storing the input data.
96
- stringio = io .StringIO ()
97
- # The header line.
98
- stringio .write (f"> { x } { y } { linespacing } { parwidth } { alignment [0 ]} \n " )
99
- # The text string to be written to the stringio object.
96
+ # Prepare the text string that will be passed to an io.StringIO object.
100
97
# Multiple paragraphs are separated by a blank line "\n\n".
101
98
_textstr : str = "\n \n " .join (text ) if is_nonstr_iter (text ) else str (text )
102
99
# Check the encoding of the text string and convert it to octal if necessary.
103
100
if (encoding := _check_encoding (_textstr )) != "ascii" :
104
101
_textstr = non_ascii_to_octal (_textstr , encoding = encoding )
105
102
confdict ["PS_CHAR_ENCODING" ] = encoding
106
- # Write the text string to the stringio object.
107
- stringio .write (_textstr )
108
103
109
104
with Session () as lib :
110
- with lib .virtualfile_from_stringio (stringio ) as vfile :
111
- lib .call_module (
112
- "text" , args = build_arg_list (kwdict , infile = vfile , confdict = confdict )
113
- )
105
+ with io .StringIO () as buffer : # Prepare the StringIO input.
106
+ buffer .write (f"> { x } { y } { linespacing } { parwidth } { alignment [0 ]} \n " )
107
+ buffer .write (_textstr )
108
+ with lib .virtualfile_in (data = buffer ) as vfile :
109
+ lib .call_module (
110
+ "text" , args = build_arg_list (kwdict , infile = vfile , confdict = confdict )
111
+ )
0 commit comments