Skip to content

Commit a084b95

Browse files
committed
docs(example): update code-to-image example, change colors, code, and use firecode font with ligatures to show pictex shaping support
1 parent af8ca0c commit a084b95

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed
259 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pictex import Canvas, Row, Text
2+
3+
def transform(values: list[int]) -> list[int]:
4+
"""Applies a transformation."""
5+
return [v * 2 for v in values if v >= 0 and v != 5]
6+
7+
def render_result(values: list[int]) -> Row:
8+
result = transform(values)
9+
arrow = Text("->").color("#C678DD")
10+
text = Text(f"{result}").color("#98C379")
11+
return Row(Text("Result").color("#61AFEF"), arrow, text).gap(6)
12+
13+
canvas = Canvas().font_family("FiraCode-VariableFont_wght.ttf")
14+
canvas.render(render_result([1, -2, 5, 10])).show()

examples/code_to_image/code_to_image.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
from pygments import lex
33
from pygments.lexers import get_lexer_by_name
44
from pygments.token import Token
5+
from itertools import groupby
56

67
COLORS = {
7-
"background": "#282C34",
8-
"line_number": "#636D83",
9-
"text": "#ABB2BF",
10-
"keyword": "#C678DD",
11-
"string": "#98C379",
12-
"comment": "#7F848E",
13-
"builtin": "#7058a8",
14-
"name": "#61AFEF",
15-
"literal": "#D19A66",
8+
"background": "#1E222A",
9+
"line_number": "#4B5263",
10+
"text": "#D7DAE0",
11+
"keyword": "#C792EA",
12+
"string": "#C3E88D",
13+
"comment": "#5C6370",
14+
"builtin": "#82AAFF",
15+
"name": "#89DDFF",
16+
"literal": "#F78C6C",
1617
}
1718

1819
TOKEN_MAP = {
@@ -28,21 +29,7 @@
2829
Token.Literal: COLORS["literal"],
2930
}
3031

31-
CODE_SNIPPET = """
32-
from pictex import Row, Text
33-
34-
# Create a simple, styled component
35-
def create_banner(text: str) -> Row:
36-
'''This function demonstrates PicTex's power!'''
37-
banner = Row(
38-
Text(text).font_size(40).color("white")
39-
).padding(20).background_color("#5D3FD3")
40-
41-
return banner
42-
43-
# Render the final image
44-
banner.render(create_banner("Hello, World! ✨")).show()
45-
"""
32+
CODE_SNIPPET = open("code_snippet.py", encoding="utf-8").read()
4633

4734
def get_token_color(token) -> str:
4835
current_token = token
@@ -56,8 +43,12 @@ def get_token_color(token) -> str:
5643
def parse_python_line(line: str) -> Row:
5744
python_lexer = get_lexer_by_name("python")
5845
tokens = lex(line, python_lexer)
46+
grouped_tokens = [
47+
(token_type, ''.join(token_text for _, token_text in group))
48+
for token_type, group in groupby(tokens, key=lambda t: t[0])
49+
]
5950
line_children = []
60-
for token_type, token_text in tokens:
51+
for token_type, token_text in grouped_tokens:
6152
color = get_token_color(token_type)
6253
line_children.append(Text(token_text).color(color))
6354

@@ -88,7 +79,7 @@ def parse_python_line(line: str) -> Row:
8879

8980
canvas = (
9081
Canvas()
91-
.font_family("Consolas")
82+
.font_family("FiraCode-VariableFont_wght.ttf")
9283
.font_size(16)
9384
.padding(60)
9485
.background_color("#757F9A")

examples/code_to_image/result.png

45.2 KB
Loading

0 commit comments

Comments
 (0)