-
Notifications
You must be signed in to change notification settings - Fork 244
Add Figure.paragraph to typeset one or multiple paragraph of text strings #3709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
seisman
wants to merge
12
commits into
main
Choose a base branch
from
feature/paragraph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
961785d
Figure.paragraph: Initial implementation focusing on input data
seisman 3d34663
Update pygmt/src/text.py
seisman 4da6010
Merge two tests into one
seisman 539fd0c
Apply copilot suggestions
seisman 4a0552c
Use a tuple instead of a set
seisman 83218fd
Need to set filename in tests
seisman d11a9be
Fix a test
seisman bce2d2d
Add two more tests
seisman 815b69b
Fix type hints for font
seisman 9589ecf
Fix test
seisman e494328
Merge branch 'main' into feature/paragraph
seisman 42c617a
Add support for -G and -W
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,6 +426,7 @@ def _repr_html_(self) -> str: | |
| logo, | ||
| magnetic_rose, | ||
| meca, | ||
| paragraph, | ||
| plot, | ||
| plot3d, | ||
| psconvert, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| """ | ||
| paragraph - Typeset one or multiple paragraphs. | ||
| """ | ||
|
|
||
| import io | ||
| from collections.abc import Sequence | ||
| from typing import Literal | ||
|
|
||
| from pygmt._typing import AnchorCode | ||
| from pygmt.alias import Alias, AliasSystem | ||
| from pygmt.clib import Session | ||
| from pygmt.exceptions import GMTValueError | ||
| from pygmt.helpers import ( | ||
| _check_encoding, | ||
| build_arg_list, | ||
| is_nonstr_iter, | ||
| non_ascii_to_octal, | ||
| ) | ||
|
|
||
| __doctest_skip__ = ["paragraph"] | ||
|
|
||
|
|
||
| def paragraph( # noqa: PLR0913 | ||
| self, | ||
| x: float | str, | ||
| y: float | str, | ||
| text: str | Sequence[str], | ||
| parwidth: float | str, | ||
| linespacing: float | str, | ||
| font: str | None = None, | ||
| angle: float | None = None, | ||
| justify: AnchorCode | None = None, | ||
| fill: str | None = None, | ||
| pen: str | None = None, | ||
| alignment: Literal["left", "center", "right", "justified"] = "left", | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ): | ||
| r""" | ||
| Typeset one or multiple paragraphs. | ||
|
|
||
| This method typesets one or multiple paragraphs of text at a given position on the | ||
| figure. The text is flowed within a given paragraph width and with a specified line | ||
| spacing. The text can be aligned left, center, right, or justified. | ||
|
|
||
| Multiple paragraphs can be provided as a sequence of strings, where each string | ||
| represents a separate paragraph, or as a single string with a blank line (``\n\n``) | ||
| separating the paragraphs. | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Full GMT docs at :gmt-docs:`text.html`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x/y | ||
| The x, y coordinates of the paragraph. | ||
| text | ||
| The paragraph text to typeset. If a sequence of strings is provided, each string | ||
| is treated as a separate paragraph. | ||
| parwidth | ||
| The width of the paragraph. | ||
| linespacing | ||
| The spacing between lines. | ||
| font | ||
| The font of the text. | ||
| angle | ||
| The angle of the text. | ||
| justify | ||
| Set the alignment of the block of text, relative to the given x, y position. | ||
| Choose a :doc:`2-character justification code </techref/justification_codes>`. | ||
| fill | ||
| Set color for filling the paragraph box [Default is no fill]. | ||
| pen | ||
| Set the pen used to draw a rectangle around the paragraph [Default is | ||
| ``"0.25p,black,solid"``]. | ||
| alignment | ||
| Set the alignment of the text. Valid values are ``"left"``, ``"center"``, | ||
| ``"right"``, and ``"justified"``. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> import pygmt | ||
| >>> | ||
| >>> fig = pygmt.Figure() | ||
| >>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) | ||
| >>> fig.paragraph( | ||
| ... x=4, | ||
| ... y=4, | ||
| ... text="This is a long paragraph. " * 10, | ||
| ... parwidth="5c", | ||
| ... linespacing="12p", | ||
| ... font="12p", | ||
| ... ) | ||
| >>> fig.show() | ||
| """ | ||
| self._activate_figure() | ||
|
|
||
| _valid_alignments = ("left", "center", "right", "justified") | ||
| if alignment not in _valid_alignments: | ||
| raise GMTValueError( | ||
| alignment, | ||
| description="value for parameter 'alignment'", | ||
| choices=_valid_alignments, | ||
| ) | ||
|
|
||
| aliasdict = AliasSystem( | ||
| F=[ | ||
| Alias(font, name="font", prefix="+f"), | ||
| Alias(angle, name="angle", prefix="+a"), | ||
| Alias(justify, name="justify", prefix="+j"), | ||
| ], | ||
| G=fill, | ||
| W=pen, | ||
| ) | ||
| aliasdict.merge({"M": True}) | ||
|
|
||
| confdict = {} | ||
| # Prepare the text string that will be passed to an io.StringIO object. | ||
| # Multiple paragraphs are separated by a blank line "\n\n". | ||
| _textstr: str = "\n\n".join(text) if is_nonstr_iter(text) else str(text) | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if _textstr == "": | ||
| raise GMTValueError( | ||
| text, | ||
| description="text", | ||
| reason="'text' must be a non-empty string or sequence of strings.", | ||
| ) | ||
|
|
||
| # Check the encoding of the text string and convert it to octal if necessary. | ||
| if (encoding := _check_encoding(_textstr)) != "ascii": | ||
| _textstr = non_ascii_to_octal(_textstr, encoding=encoding) | ||
| confdict["PS_CHAR_ENCODING"] = encoding | ||
|
|
||
| with Session() as lib: | ||
| with io.StringIO() as buffer: # Prepare the StringIO input. | ||
| buffer.write(f"> {x} {y} {linespacing} {parwidth} {alignment[0]}\n") | ||
| buffer.write(_textstr) | ||
| with lib.virtualfile_in(data=buffer) as vfile: | ||
| lib.call_module( | ||
| "text", | ||
| args=build_arg_list(aliasdict, infile=vfile, confdict=confdict), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| outs: | ||
| - md5: c5b1df47e811475defb0db79e49cab3d | ||
| size: 27632 | ||
| hash: md5 | ||
| path: test_paragraph.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| outs: | ||
| - md5: a0ef6e989b11a252ec2a7ef497f3c789 | ||
| size: 36274 | ||
| hash: md5 | ||
| path: test_paragraph_alignment.png |
5 changes: 5 additions & 0 deletions
5
pygmt/tests/baseline/test_paragraph_font_angle_justify.png.dvc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| outs: | ||
| - md5: 6f55167eb6bc626b2bfee89ffe73faad | ||
| size: 48604 | ||
| hash: md5 | ||
| path: test_paragraph_font_angle_justify.png |
5 changes: 5 additions & 0 deletions
5
pygmt/tests/baseline/test_paragraph_multiple_paragraphs.png.dvc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| outs: | ||
| - md5: 167d4be24bca4e287b2056ecbfbb629a | ||
| size: 29076 | ||
| hash: md5 | ||
| path: test_paragraph_multiple_paragraphs.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """ | ||
| Tests for Figure.paragraph. | ||
| """ | ||
|
|
||
| import pytest | ||
| from pygmt import Figure | ||
|
|
||
|
|
||
| @pytest.mark.mpl_image_compare | ||
| def test_paragraph(): | ||
| """ | ||
| Test typesetting a single paragraph. | ||
| """ | ||
| fig = Figure() | ||
| fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) | ||
| fig.paragraph( | ||
| x=4, | ||
| y=4, | ||
| text="This is a long paragraph. " * 10, | ||
| parwidth="5c", | ||
| linespacing="12p", | ||
| ) | ||
| return fig | ||
|
|
||
|
|
||
| @pytest.mark.mpl_image_compare(filename="test_paragraph_multiple_paragraphs.png") | ||
| @pytest.mark.parametrize("inputtype", ["list", "string"]) | ||
| def test_paragraph_multiple_paragraphs(inputtype): | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Test typesetting multiple paragraphs. | ||
| """ | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if inputtype == "list": | ||
| text = [ | ||
| "This is the first paragraph. " * 5, | ||
| "This is the second paragraph. " * 5, | ||
| ] | ||
| else: | ||
| text = ( | ||
| "This is the first paragraph. " * 5 | ||
| + "\n\n" # Separate the paragraphs with a blank line. | ||
| + "This is the second paragraph. " * 5 | ||
| ) | ||
|
|
||
| fig = Figure() | ||
| fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) | ||
| fig.paragraph( | ||
| x=4, | ||
| y=4, | ||
| text=text, | ||
| parwidth="5c", | ||
| linespacing="12p", | ||
| ) | ||
| return fig | ||
|
|
||
|
|
||
| @pytest.mark.mpl_image_compare | ||
| def test_paragraph_alignment(): | ||
| """ | ||
| Test typesetting a single paragraph with different alignments. | ||
| """ | ||
| fig = Figure() | ||
| fig.basemap(region=[0, 10, 0, 8], projection="X10c/8c", frame=True) | ||
| for x, y, alignment in [ | ||
| (5, 1, "left"), | ||
| (5, 3, "right"), | ||
| (5, 5, "center"), | ||
| (5, 7, "justified"), | ||
| ]: | ||
| fig.paragraph( | ||
| x=x, | ||
| y=y, | ||
| text=alignment.upper() + " : " + "This is a long paragraph. " * 5, | ||
| parwidth="8c", | ||
| linespacing="12p", | ||
| alignment=alignment, | ||
| ) | ||
| return fig | ||
|
|
||
|
|
||
| @pytest.mark.mpl_image_compare | ||
| def test_paragraph_font_angle_justify(): | ||
| """ | ||
| Test typesetting a single paragraph with font, angle, and justify options. | ||
| """ | ||
| fig = Figure() | ||
| fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) | ||
| fig.paragraph( | ||
| x=1, | ||
| y=4, | ||
| text="This is a long paragraph. " * 10, | ||
| parwidth="8c", | ||
| linespacing="12p", | ||
| font="10p,Helvetica-Bold,red", | ||
| angle=45, | ||
| justify="TL", | ||
| ) | ||
| return fig | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.