|
| 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.clib import Session |
| 11 | +from pygmt.exceptions import GMTInvalidInput |
| 12 | +from pygmt.helpers import ( |
| 13 | + _check_encoding, |
| 14 | + build_arg_list, |
| 15 | + is_nonstr_iter, |
| 16 | + non_ascii_to_octal, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def _parse_option_f_upper( |
| 21 | + font: float | str | None, angle: float | None, justify: AnchorCode | None |
| 22 | +) -> str | None: |
| 23 | + """ |
| 24 | + Parse the font, angle, and justification arguments and return the string to be |
| 25 | + appended to the module options. |
| 26 | +
|
| 27 | + Examples |
| 28 | + -------- |
| 29 | + >>> _parse_font_angle_justify(None, None, None) |
| 30 | + >>> _parse_font_angle_justify("10p", None, None) |
| 31 | + '+f10p' |
| 32 | + >>> _parse_font_angle_justify(None, 45, None) |
| 33 | + '+a45' |
| 34 | + >>> _parse_font_angle_justify(None, None, "CM") |
| 35 | + '+jCM' |
| 36 | + >>> _parse_font_angle_justify("10p,Helvetica-Bold", 45, "CM") |
| 37 | + '+f10p,Helvetica-Bold+a45+jCM' |
| 38 | + """ |
| 39 | + args = ((font, "+f"), (angle, "+a"), (justify, "+j")) |
| 40 | + if all(arg is None for arg, _ in args): |
| 41 | + return None |
| 42 | + return "".join(f"{flag}{arg}" for arg, flag in args if arg is not None) |
| 43 | + |
| 44 | + |
| 45 | +def paragraph( |
| 46 | + self, |
| 47 | + x: float | str, |
| 48 | + y: float | str, |
| 49 | + text: str | Sequence[str], |
| 50 | + parwidth: float | str, |
| 51 | + linespacing: float | str, |
| 52 | + font: float | str | None = None, |
| 53 | + angle: float | None = None, |
| 54 | + justify: AnchorCode | None = None, |
| 55 | + alignment: Literal["left", "center", "right", "justified"] = "left", |
| 56 | +): |
| 57 | + """ |
| 58 | + Typeset one or multiple paragraphs. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + x/y |
| 63 | + The x, y coordinates of the paragraph. |
| 64 | + text |
| 65 | + The paragraph text to typeset. If a sequence of strings is provided, each |
| 66 | + string is treated as a separate paragraph. |
| 67 | + parwidth |
| 68 | + The width of the paragraph. |
| 69 | + linespacing |
| 70 | + The spacing between lines. |
| 71 | + font |
| 72 | + The font of the text. |
| 73 | + angle |
| 74 | + The angle of the text. |
| 75 | + justify |
| 76 | + The justification of the block of text, relative to the given x, y position. |
| 77 | + alignment |
| 78 | + The alignment of the text. Valid values are ``"left"``, ``"center"``, |
| 79 | + ``"right"``, and ``"justified"``. |
| 80 | + """ |
| 81 | + self._preprocess() |
| 82 | + |
| 83 | + # Validate 'alignment' argument. |
| 84 | + if alignment not in {"left", "center", "right", "justified"}: |
| 85 | + msg = ( |
| 86 | + "Invalid value for 'alignment': {alignment}. " |
| 87 | + "Valid values are 'left', 'center', 'right', and 'justified'." |
| 88 | + ) |
| 89 | + raise GMTInvalidInput(msg) |
| 90 | + |
| 91 | + confdict = {} |
| 92 | + # Prepare the keyword dictionary for the module options |
| 93 | + kwdict = {"M": True, "F": _parse_option_f_upper(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. |
| 100 | + # Multiple paragraphs are separated by a blank line "\n\n". |
| 101 | + _textstr: str = "\n\n".join(text) if is_nonstr_iter(text) else str(text) |
| 102 | + # Check the encoding of the text string and convert it to octal if necessary. |
| 103 | + if (encoding := _check_encoding(_textstr)) != "ascii": |
| 104 | + _textstr = non_ascii_to_octal(_textstr, encoding=encoding) |
| 105 | + confdict["PS_CHAR_ENCODING"] = encoding |
| 106 | + # Write the text string to the stringio object. |
| 107 | + stringio.write(_textstr) |
| 108 | + |
| 109 | + 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 | + ) |
0 commit comments