Skip to content

Commit 4200f51

Browse files
authored
Expose font_aspect_ratio as parameter on SVG export (#2539)
* Expose font_aspect_ratio as parameter on SVG export * Update CHANGELOG.md
1 parent bfea226 commit 4200f51

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Add support for `FORCE_COLOR` env var https://github.com/Textualize/rich/pull/2449
1515
- Allow a `max_depth` argument to be passed to the `install()` hook https://github.com/Textualize/rich/issues/2486
1616
- Document using `None` as name in `__rich_repr__` for tuple positional args https://github.com/Textualize/rich/pull/2379
17+
- Add `font_aspect_ratio` parameter in SVG export https://github.com/Textualize/rich/pull/2539/files
1718

1819
### Fixed
1920

rich/console.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,18 +2261,22 @@ def export_svg(
22612261
theme: Optional[TerminalTheme] = None,
22622262
clear: bool = True,
22632263
code_format: str = CONSOLE_SVG_FORMAT,
2264+
font_aspect_ratio: float = 0.61,
22642265
) -> str:
22652266
"""
22662267
Generate an SVG from the console contents (requires record=True in Console constructor).
22672268
22682269
Args:
22692270
path (str): The path to write the SVG to.
2270-
title (str): The title of the tab in the output image
2271+
title (str, optional): The title of the tab in the output image
22712272
theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal
22722273
clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``
2273-
code_format (str): Format string used to generate the SVG. Rich will inject a number of variables
2274+
code_format (str, optional): Format string used to generate the SVG. Rich will inject a number of variables
22742275
into the string in order to form the final SVG output. The default template used and the variables
22752276
injected by Rich can be found by inspecting the ``console.CONSOLE_SVG_FORMAT`` variable.
2277+
font_aspect_ratio (float, optional): The width to height ratio of the font used in the ``code_format``
2278+
string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font).
2279+
If you aren't specifying a different font inside ``code_format``, you probably don't need this.
22762280
"""
22772281

22782282
from rich.cells import cell_len
@@ -2316,7 +2320,7 @@ def get_svg_style(style: Style) -> str:
23162320

23172321
width = self.width
23182322
char_height = 20
2319-
char_width = char_height * 0.61
2323+
char_width = char_height * font_aspect_ratio
23202324
line_height = char_height * 1.22
23212325

23222326
margin_top = 1
@@ -2505,23 +2509,28 @@ def save_svg(
25052509
theme: Optional[TerminalTheme] = None,
25062510
clear: bool = True,
25072511
code_format: str = CONSOLE_SVG_FORMAT,
2512+
font_aspect_ratio: float = 0.61,
25082513
) -> None:
25092514
"""Generate an SVG file from the console contents (requires record=True in Console constructor).
25102515
25112516
Args:
25122517
path (str): The path to write the SVG to.
2513-
title (str): The title of the tab in the output image
2518+
title (str, optional): The title of the tab in the output image
25142519
theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal
25152520
clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``
2516-
code_format (str): Format string used to generate the SVG. Rich will inject a number of variables
2521+
code_format (str, optional): Format string used to generate the SVG. Rich will inject a number of variables
25172522
into the string in order to form the final SVG output. The default template used and the variables
25182523
injected by Rich can be found by inspecting the ``console.CONSOLE_SVG_FORMAT`` variable.
2524+
font_aspect_ratio (float, optional): The width to height ratio of the font used in the ``code_format``
2525+
string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font).
2526+
If you aren't specifying a different font inside ``code_format``, you probably don't need this.
25192527
"""
25202528
svg = self.export_svg(
25212529
title=title,
25222530
theme=theme,
25232531
clear=clear,
25242532
code_format=code_format,
2533+
font_aspect_ratio=font_aspect_ratio,
25252534
)
25262535
with open(path, "wt", encoding="utf-8") as write_file:
25272536
write_file.write(svg)

0 commit comments

Comments
 (0)