|
| 1 | +""" |
| 2 | +paragraph - Typeset one or multiple paragraphs. |
| 3 | +""" |
| 4 | + |
| 5 | +import io |
| 6 | +from collections.abc import Sequence |
| 7 | +from typing import Literal |
| 8 | + |
| 9 | +from pygmt._typing import AnchorCode |
| 10 | +from pygmt.alias import Alias, AliasSystem |
| 11 | +from pygmt.clib import Session |
| 12 | +from pygmt.exceptions import GMTValueError |
| 13 | +from pygmt.helpers import ( |
| 14 | + _check_encoding, |
| 15 | + build_arg_list, |
| 16 | + is_nonstr_iter, |
| 17 | + non_ascii_to_octal, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def paragraph( |
| 22 | + self, |
| 23 | + x: float | str, |
| 24 | + y: float | str, |
| 25 | + text: str | Sequence[str], |
| 26 | + parwidth: float | str, |
| 27 | + linespacing: float | str, |
| 28 | + font: float | str | None = None, |
| 29 | + angle: float | None = None, |
| 30 | + justify: AnchorCode | None = None, |
| 31 | + alignment: Literal["left", "center", "right", "justified"] = "left", |
| 32 | +): |
| 33 | + """ |
| 34 | + Typeset one or multiple paragraphs. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + x/y |
| 39 | + The x, y coordinates of the paragraph. |
| 40 | + text |
| 41 | + The paragraph text to typeset. If a sequence of strings is provided, each string |
| 42 | + is treated as a separate paragraph. |
| 43 | + parwidth |
| 44 | + The width of the paragraph. |
| 45 | + linespacing |
| 46 | + The spacing between lines. |
| 47 | + font |
| 48 | + The font of the text. |
| 49 | + angle |
| 50 | + The angle of the text. |
| 51 | + justify |
| 52 | + The justification of the block of text, relative to the given x, y position. |
| 53 | + alignment |
| 54 | + The alignment of the text. Valid values are ``"left"``, ``"center"``, |
| 55 | + ``"right"``, and ``"justified"``. |
| 56 | + """ |
| 57 | + self._activate_figure() |
| 58 | + |
| 59 | + _valid_alignments = {"left", "center", "right", "justified"} |
| 60 | + if alignment not in _valid_alignments: |
| 61 | + raise GMTValueError( |
| 62 | + alignment, |
| 63 | + description="value for parameter 'alignment'", |
| 64 | + choices=_valid_alignments, |
| 65 | + ) |
| 66 | + |
| 67 | + aliasdict = AliasSystem( |
| 68 | + F=[ |
| 69 | + Alias(font, name="font", prefix="+f"), |
| 70 | + Alias(angle, name="angle", prefix="+a"), |
| 71 | + Alias(justify, name="justify", prefix="+j"), |
| 72 | + ] |
| 73 | + ).merge({"M": True}) |
| 74 | + |
| 75 | + confdict = {} |
| 76 | + # Prepare the text string that will be passed to an io.StringIO object. |
| 77 | + # Multiple paragraphs are separated by a blank line "\n\n". |
| 78 | + _textstr: str = "\n\n".join(text) if is_nonstr_iter(text) else str(text) |
| 79 | + # Check the encoding of the text string and convert it to octal if necessary. |
| 80 | + if (encoding := _check_encoding(_textstr)) != "ascii": |
| 81 | + _textstr = non_ascii_to_octal(_textstr, encoding=encoding) |
| 82 | + confdict["PS_CHAR_ENCODING"] = encoding |
| 83 | + |
| 84 | + with Session() as lib: |
| 85 | + with io.StringIO() as buffer: # Prepare the StringIO input. |
| 86 | + buffer.write(f"> {x} {y} {linespacing} {parwidth} {alignment[0]}\n") |
| 87 | + buffer.write(_textstr) |
| 88 | + with lib.virtualfile_in(data=buffer) as vfile: |
| 89 | + lib.call_module( |
| 90 | + "text", |
| 91 | + args=build_arg_list(aliasdict, infile=vfile, confdict=confdict), |
| 92 | + ) |
0 commit comments