molyvi 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
molyvi/__main__.py ADDED
@@ -0,0 +1,40 @@
1
+ import argparse
2
+ from pathlib import Path
3
+ from matplotlib import font_manager
4
+ import csv
5
+ from jinja2 import Template
6
+ from .generateSVG import textToSVG
7
+
8
+ def main():
9
+ parser = argparse.ArgumentParser(description="The Molyvi generator.")
10
+
11
+ parser.add_argument("template", type=Path, help="The Jinja file containing the template to generate from.")
12
+ parser.add_argument("data", type=Path, help="The CSV file containing the data to fill in the template with.")
13
+ parser.add_argument("output", type=Path, help="The path to put the generated SVGs.")
14
+ parser.add_argument("typeface", type=str, help="The typeface to use.", default="Patrick Hand")
15
+ parser.add_argument("-s", "--size", type=int, help="The font size to use.", default=50)
16
+ parser.add_argument("-w", "--wobble", type=float, help="The amount of wobble for each point.", default=8.0)
17
+ parser.add_argument("-j", "--jitter", type=float, help="The amount to shift each character.", default=2.5)
18
+
19
+ args = parser.parse_args()
20
+
21
+ if not args.template.is_file():
22
+ raise FileNotFoundError(f"{args.template} doesn't exist.")
23
+ if not args.data.is_file():
24
+ raise FileNotFoundError(f"{args.data} doesn't exist.")
25
+
26
+
27
+ template = Template(args.template.read_text())
28
+
29
+ with args.data.open(mode="r", newline="") as file:
30
+ data = list(csv.DictReader(file))
31
+
32
+ args.output.mkdir(exist_ok=True, parents=True)
33
+
34
+ typeface = Path(font_manager.findfont(font_manager.FontProperties(family=args.typeface), fallback_to_default=False))
35
+
36
+ for i in range(len(data)):
37
+ (args.output / f"{i}.svg").write_text(textToSVG(template.render(**data[i]), typeface, args.size, args.wobble, args.jitter))
38
+
39
+ if __name__ == "__main__":
40
+ main()
molyvi/generateSVG.py ADDED
@@ -0,0 +1,70 @@
1
+ from fontTools.ttLib import TTFont
2
+ from fontTools.pens.svgPathPen import SVGPathPen
3
+ from fontTools.pens.transformPen import TransformPen
4
+ import random
5
+ from pathlib import Path
6
+
7
+ def textToSVG(text: str, typeface: Path, size: int, wobble: float = 8.0, jitter: float = 2.5) -> str:
8
+ font = TTFont(typeface)
9
+ cmap = font.getBestCmap()
10
+ glyphSet = font.getGlyphSet()
11
+ unitsPerEm = font["head"].unitsPerEm
12
+ scale = size / unitsPerEm
13
+
14
+ svgPaths = []
15
+ cursorX = 20.0
16
+ baselineY = size * 1.2
17
+ lineHeight = size * 1.4
18
+ maxX = cursorX
19
+
20
+ for char in text:
21
+ if char == " ":
22
+ cursorX += glyphSet[cmap[ord(" ")]].width * scale
23
+ continue
24
+ if char == "\n":
25
+ maxX = max(maxX, cursorX)
26
+ cursorX = 20.0
27
+ baselineY += lineHeight
28
+ continue
29
+
30
+ glyphName = cmap.get(ord(char))
31
+ if not glyphName:
32
+ continue
33
+
34
+ glyph = glyphSet[glyphName]
35
+
36
+ pen = SVGPathPen(glyphSet)
37
+ transformedPen = TransformPen(pen, (scale, 0, 0, -scale, 0, 0))
38
+ glyph.draw(transformedPen)
39
+ rawPath = pen.getCommands()
40
+
41
+ maxOffset = wobble * scale
42
+
43
+ tokens = rawPath.replace(",", " ").split()
44
+ newTokens = []
45
+ for token in tokens:
46
+ try:
47
+ val = float(token)
48
+ val += random.uniform(-maxOffset, maxOffset)
49
+ newTokens.append(f"{val:.2f}")
50
+ except ValueError:
51
+ newTokens.append(token)
52
+ jitteredPath = " ".join(newTokens)
53
+
54
+ rot = random.uniform(-jitter, jitter)
55
+ yOffset = baselineY + random.uniform(-jitter, jitter)
56
+
57
+ svgPaths.append(
58
+ f"""<path d="{jitteredPath}"
59
+ transform="translate({cursorX:.2f}, {yOffset:.2f}) rotate({rot:.2f})"
60
+ fill="#1a1a1a" />"""
61
+ )
62
+
63
+ cursorX += (glyph.width * scale) + random.uniform(-1.0, 1.5)
64
+
65
+ maxX = max(maxX, cursorX)
66
+ width = maxX + 20
67
+ height = baselineY + (size * 0.6)
68
+ return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{width:.0f}" height="{height:.0f}" viewBox="0 0 {width:.0f} {height:.0f}">
69
+ {"".join(svgPaths)}
70
+ </svg>"""
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: molyvi
3
+ Version: 1.0.0
4
+ Summary: A tool for creating handwritten SVGs in bulk.
5
+ Author-email: Jack Huey <jhhuey@icloud.com>
6
+ Requires-Python: >=3.14
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: fonttools~=4.68
9
+ Requires-Dist: jinja2~=3.1
10
+ Requires-Dist: matplotlib~=3.11
11
+
12
+ # Molyvi
13
+ ## A tool for creating handwritten SVGs in bulk.
14
+
15
+ ---
16
+
17
+ ![PyPI Version](https://img.shields.io/pypi/v/molyvi?style=for-the-badge)
18
+
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install molyvi
24
+ ```
25
+
26
+ ## Usage
27
+ `molyvi TEMPLATE DATA OUTPUT TYPEFACE [-s] [-w] [-j]`
28
+
29
+ ### Positional arguments
30
+ |Argument|Description|
31
+ |----------|----------------------------------------|
32
+ | TEMPLATE | The Jinja file containing the template to generate from.|
33
+ | DATA | The CSV file containing the data to fill in the template with. |
34
+ | OUTPUT | The path to put the generated SVGs. |
35
+ | TYPEFACE | The typeface to use. |
36
+
37
+ ### Options
38
+ |Option|Description| Default |
39
+ |---------------------|--------------------------------------|:--------:|
40
+ | -s, --size SIZE | The font size to use. |50|
41
+ | -w, --wobble WOBBLE | The amount of wobble for each point. |8.0|
42
+ | -j, --jitter JITTER | The amount to shift each character. |2.5|
43
+
44
+ ## Example
45
+ See [my example](example/).
@@ -0,0 +1,7 @@
1
+ molyvi/__main__.py,sha256=BablsYncvKpTpM1-MvNImVndOARhkAqiGpWzgZ6bv64,1726
2
+ molyvi/generateSVG.py,sha256=doKSIjG30z4HjXus7HXS4DjuWqOGFoiHVwYCR03SrZU,2155
3
+ molyvi-1.0.0.dist-info/METADATA,sha256=PwQdFSmowIFlCRtFHmu6HhO2sZPjn_qAaEil5QbX6vQ,1280
4
+ molyvi-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ molyvi-1.0.0.dist-info/entry_points.txt,sha256=ThhAYlp1nRjJmwv8cWdXY58lKuqjHyn1nbXJlOQAejM,48
6
+ molyvi-1.0.0.dist-info/top_level.txt,sha256=d4l2EJjk6MNhrmPr3-Jhr5M-2Sfxemx788q-vcLXQL4,7
7
+ molyvi-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ molyvi = molyvi.__main__:main
@@ -0,0 +1 @@
1
+ molyvi