|
| 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 | + fmt_docstring, |
| 17 | + is_nonstr_iter, |
| 18 | + non_ascii_to_octal, |
| 19 | +) |
| 20 | + |
| 21 | +__doctest_skip__ = ["paragraph"] |
| 22 | + |
| 23 | + |
| 24 | +@fmt_docstring |
| 25 | +def paragraph( # noqa: PLR0913 |
| 26 | + self, |
| 27 | + x: float | str, |
| 28 | + y: float | str, |
| 29 | + text: str | Sequence[str], |
| 30 | + parwidth: float | str, |
| 31 | + linespacing: float | str, |
| 32 | + font: str | None = None, |
| 33 | + angle: float | None = None, |
| 34 | + justify: AnchorCode | None = None, |
| 35 | + fill: str | None = None, |
| 36 | + pen: str | None = None, |
| 37 | + alignment: Literal["left", "center", "right", "justified"] = "left", |
| 38 | + verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] |
| 39 | + | bool = False, |
| 40 | + panel: int | Sequence[int] | bool = False, |
| 41 | + transparency: float | Sequence[float] | bool | None = None, |
| 42 | +): |
| 43 | + r""" |
| 44 | + Typeset one or multiple paragraphs. |
| 45 | +
|
| 46 | + This method typesets one or multiple paragraphs of text at a given position on the |
| 47 | + figure. The text is flowed within a given paragraph width and with a specified line |
| 48 | + spacing. The text can be aligned left, center, right, or justified. |
| 49 | +
|
| 50 | + Multiple paragraphs can be provided as a sequence of strings, where each string |
| 51 | + represents a separate paragraph, or as a single string with a blank line (``\n\n``) |
| 52 | + separating the paragraphs. |
| 53 | +
|
| 54 | + Full GMT docs at :gmt-docs:`text.html`. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + x/y |
| 59 | + The x, y coordinates of the paragraph. |
| 60 | + text |
| 61 | + The paragraph text to typeset. If a sequence of strings is provided, each string |
| 62 | + is treated as a separate paragraph. |
| 63 | + parwidth |
| 64 | + The width of the paragraph. |
| 65 | + linespacing |
| 66 | + The spacing between lines. |
| 67 | + font |
| 68 | + The font of the text. |
| 69 | + angle |
| 70 | + The angle of the text. |
| 71 | + justify |
| 72 | + Set the alignment of the block of text, relative to the given x, y position. |
| 73 | + Choose a :doc:`2-character justification code </techref/justification_codes>`. |
| 74 | + fill |
| 75 | + Set color for filling the paragraph box [Default is no fill]. |
| 76 | + pen |
| 77 | + Set the pen used to draw a rectangle around the paragraph [Default is |
| 78 | + ``"0.25p,black,solid"``]. |
| 79 | + alignment |
| 80 | + Set the alignment of the text. Valid values are ``"left"``, ``"center"``, |
| 81 | + ``"right"``, and ``"justified"``. |
| 82 | + $verbose |
| 83 | + $panel |
| 84 | + $transparency |
| 85 | +
|
| 86 | + Examples |
| 87 | + -------- |
| 88 | + >>> import pygmt |
| 89 | + >>> |
| 90 | + >>> fig = pygmt.Figure() |
| 91 | + >>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) |
| 92 | + >>> fig.paragraph( |
| 93 | + ... x=4, |
| 94 | + ... y=4, |
| 95 | + ... text="This is a long paragraph. " * 10, |
| 96 | + ... parwidth="5c", |
| 97 | + ... linespacing="12p", |
| 98 | + ... font="12p", |
| 99 | + ... ) |
| 100 | + >>> fig.show() |
| 101 | + """ |
| 102 | + self._activate_figure() |
| 103 | + |
| 104 | + _valid_alignments = ("left", "center", "right", "justified") |
| 105 | + if alignment not in _valid_alignments: |
| 106 | + raise GMTValueError( |
| 107 | + alignment, |
| 108 | + description="value for parameter 'alignment'", |
| 109 | + choices=_valid_alignments, |
| 110 | + ) |
| 111 | + |
| 112 | + aliasdict = AliasSystem( |
| 113 | + F=[ |
| 114 | + Alias(font, name="font", prefix="+f"), |
| 115 | + Alias(angle, name="angle", prefix="+a"), |
| 116 | + Alias(justify, name="justify", prefix="+j"), |
| 117 | + ], |
| 118 | + G=Alias(fill, name="fill"), |
| 119 | + W=Alias(pen, name="pen"), |
| 120 | + ).add_common( |
| 121 | + V=verbose, |
| 122 | + c=panel, |
| 123 | + t=transparency, |
| 124 | + ) |
| 125 | + aliasdict.merge({"M": True}) |
| 126 | + |
| 127 | + confdict = {} |
| 128 | + # Prepare the text string that will be passed to an io.StringIO object. |
| 129 | + # Multiple paragraphs are separated by a blank line "\n\n". |
| 130 | + _textstr: str = "\n\n".join(text) if is_nonstr_iter(text) else str(text) |
| 131 | + |
| 132 | + if _textstr == "": |
| 133 | + raise GMTValueError( |
| 134 | + text, |
| 135 | + description="text", |
| 136 | + reason="'text' must be a non-empty string or sequence of strings.", |
| 137 | + ) |
| 138 | + |
| 139 | + # Check the encoding of the text string and convert it to octal if necessary. |
| 140 | + if (encoding := _check_encoding(_textstr)) != "ascii": |
| 141 | + _textstr = non_ascii_to_octal(_textstr, encoding=encoding) |
| 142 | + confdict["PS_CHAR_ENCODING"] = encoding |
| 143 | + |
| 144 | + with Session() as lib: |
| 145 | + with io.StringIO() as buffer: # Prepare the StringIO input. |
| 146 | + buffer.write(f"> {x} {y} {linespacing} {parwidth} {alignment[0]}\n") |
| 147 | + buffer.write(_textstr) |
| 148 | + with lib.virtualfile_in(data=buffer) as vfile: |
| 149 | + lib.call_module( |
| 150 | + "text", |
| 151 | + args=build_arg_list(aliasdict, infile=vfile, confdict=confdict), |
| 152 | + ) |
0 commit comments