powerpoint-squircles 0.1.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.
- powerpoint_squircles/__init__.py +15 -0
- powerpoint_squircles/cli.py +150 -0
- powerpoint_squircles/convert.py +163 -0
- powerpoint_squircles/geometry.py +102 -0
- powerpoint_squircles/py.typed +0 -0
- powerpoint_squircles-0.1.0.dist-info/METADATA +243 -0
- powerpoint_squircles-0.1.0.dist-info/RECORD +10 -0
- powerpoint_squircles-0.1.0.dist-info/WHEEL +4 -0
- powerpoint_squircles-0.1.0.dist-info/entry_points.txt +2 -0
- powerpoint_squircles-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""powerpoint-squircles: curvature-continuous (G2) squircles for PowerPoint."""
|
|
2
|
+
|
|
3
|
+
from .convert import count_candidates, set_squircle, squircle_ify, squircle_ify_report
|
|
4
|
+
from .geometry import DEFAULT_ADJ, MAX_ADJ, custgeom
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
__all__ = [
|
|
8
|
+
"custgeom",
|
|
9
|
+
"set_squircle",
|
|
10
|
+
"squircle_ify",
|
|
11
|
+
"squircle_ify_report",
|
|
12
|
+
"count_candidates",
|
|
13
|
+
"DEFAULT_ADJ",
|
|
14
|
+
"MAX_ADJ",
|
|
15
|
+
]
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""``squircle-ify``: convert rounded rectangles in a .pptx into G2 squircles."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import os
|
|
7
|
+
import pathlib
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .geometry import MAX_ADJ
|
|
12
|
+
|
|
13
|
+
MAX_PERCENT = MAX_ADJ / 1000
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _corner(text: str) -> int:
|
|
17
|
+
"""Percent of the shorter side -> DrawingML's 1/1000-of-a-percent value.
|
|
18
|
+
|
|
19
|
+
The flag takes a percentage because that is the only unit a human types.
|
|
20
|
+
``adj=`` in the library keeps PowerPoint's own convention, where the same
|
|
21
|
+
corner is ``40000`` -- a number nobody guesses from "40%".
|
|
22
|
+
"""
|
|
23
|
+
try:
|
|
24
|
+
pct = float(text.rstrip("%"))
|
|
25
|
+
except ValueError:
|
|
26
|
+
raise argparse.ArgumentTypeError(f"{text!r} is not a number") from None
|
|
27
|
+
if not 0 <= pct <= MAX_PERCENT:
|
|
28
|
+
raise argparse.ArgumentTypeError(
|
|
29
|
+
f"{pct:g}% is outside 0-{MAX_PERCENT:g}%: a corner cannot exceed half the shorter side"
|
|
30
|
+
)
|
|
31
|
+
return round(pct * 1000)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _save_atomically(prs, out: pathlib.Path) -> None:
|
|
35
|
+
"""A crash mid-save must not leave a truncated deck where a good one was.
|
|
36
|
+
|
|
37
|
+
Matters most under ``--in-place``, where the half-written file would be the
|
|
38
|
+
user's only copy.
|
|
39
|
+
"""
|
|
40
|
+
tmp = out.with_name(out.name + ".tmp")
|
|
41
|
+
try:
|
|
42
|
+
prs.save(str(tmp))
|
|
43
|
+
os.replace(tmp, out)
|
|
44
|
+
except BaseException:
|
|
45
|
+
tmp.unlink(missing_ok=True)
|
|
46
|
+
raise
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def main(argv: list[str] | None = None) -> int:
|
|
50
|
+
p = argparse.ArgumentParser(
|
|
51
|
+
prog="squircle-ify",
|
|
52
|
+
description="Convert rounded rectangles in a PowerPoint deck into "
|
|
53
|
+
"curvature-continuous (G2) squircles.",
|
|
54
|
+
)
|
|
55
|
+
p.add_argument("input", type=pathlib.Path, help="input .pptx file")
|
|
56
|
+
p.add_argument(
|
|
57
|
+
"-o",
|
|
58
|
+
"--output",
|
|
59
|
+
type=pathlib.Path,
|
|
60
|
+
default=None,
|
|
61
|
+
help="output path (default: <input>_squircle.pptx)",
|
|
62
|
+
)
|
|
63
|
+
p.add_argument(
|
|
64
|
+
"--corner",
|
|
65
|
+
type=_corner,
|
|
66
|
+
default=None,
|
|
67
|
+
metavar="PERCENT",
|
|
68
|
+
help=f"corner size as a percent of the shorter side, 0-{MAX_PERCENT:g} "
|
|
69
|
+
"(default: preserve each shape's own radius)",
|
|
70
|
+
)
|
|
71
|
+
p.add_argument(
|
|
72
|
+
"--all-rects",
|
|
73
|
+
action="store_true",
|
|
74
|
+
help="also convert sharp rectangles, not just rounded ones",
|
|
75
|
+
)
|
|
76
|
+
p.add_argument(
|
|
77
|
+
"--layouts",
|
|
78
|
+
action="store_true",
|
|
79
|
+
help="also convert shapes on slide layouts",
|
|
80
|
+
)
|
|
81
|
+
p.add_argument(
|
|
82
|
+
"--masters",
|
|
83
|
+
action="store_true",
|
|
84
|
+
help="also convert shapes on slide masters",
|
|
85
|
+
)
|
|
86
|
+
p.add_argument("--in-place", action="store_true", help="overwrite the input file")
|
|
87
|
+
p.add_argument(
|
|
88
|
+
"-n",
|
|
89
|
+
"--dry-run",
|
|
90
|
+
action="store_true",
|
|
91
|
+
help="report what would change and write nothing",
|
|
92
|
+
)
|
|
93
|
+
p.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
94
|
+
args = p.parse_args(argv)
|
|
95
|
+
|
|
96
|
+
if not args.input.exists():
|
|
97
|
+
p.error(f"no such file: {args.input}")
|
|
98
|
+
if args.in_place and args.output is not None:
|
|
99
|
+
p.error("--in-place and --output are mutually exclusive")
|
|
100
|
+
out = (
|
|
101
|
+
args.input
|
|
102
|
+
if args.in_place
|
|
103
|
+
else (args.output or args.input.with_stem(args.input.stem + "_squircle"))
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
from pptx import Presentation
|
|
107
|
+
|
|
108
|
+
from .convert import count_candidates, squircle_ify_report
|
|
109
|
+
|
|
110
|
+
prs = Presentation(str(args.input))
|
|
111
|
+
only_roundrect = not args.all_rects
|
|
112
|
+
available = count_candidates(prs, only_roundrect=only_roundrect)
|
|
113
|
+
report = squircle_ify_report(
|
|
114
|
+
prs,
|
|
115
|
+
only_roundrect=only_roundrect,
|
|
116
|
+
adj=args.corner,
|
|
117
|
+
layouts=args.layouts,
|
|
118
|
+
masters=args.masters,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
# Save before announcing success, so a failure here can't be preceded by a
|
|
122
|
+
# line telling the user it worked.
|
|
123
|
+
if not args.dry_run:
|
|
124
|
+
_save_atomically(prs, out)
|
|
125
|
+
|
|
126
|
+
n = sum(report.values())
|
|
127
|
+
detail = ", ".join(f"{v} in {k}" for k, v in report.items() if v)
|
|
128
|
+
print(
|
|
129
|
+
f"{'would convert' if args.dry_run else 'converted'} {n} shape(s)"
|
|
130
|
+
f"{' (' + detail + ')' if detail else ''}"
|
|
131
|
+
f"{'' if args.dry_run else f' -> {out}'}"
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
# "0 converted" on a deck full of rounded rectangles almost always means
|
|
135
|
+
# they live on the layouts or the master; say so instead of looking broken.
|
|
136
|
+
skipped = {
|
|
137
|
+
scope: available[scope]
|
|
138
|
+
for scope, enabled in (("layouts", args.layouts), ("masters", args.masters))
|
|
139
|
+
if not enabled and available.get(scope)
|
|
140
|
+
}
|
|
141
|
+
if skipped:
|
|
142
|
+
where = " and ".join(f"{v} in {k}" for k, v in skipped.items())
|
|
143
|
+
flags = " ".join(f"--{k}" for k in skipped)
|
|
144
|
+
print(f"note: {where} left untouched; re-run with {flags} to include them")
|
|
145
|
+
|
|
146
|
+
return 0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
if __name__ == "__main__":
|
|
150
|
+
sys.exit(main())
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Apply squircle geometry to python-pptx shapes and whole decks."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from lxml import etree
|
|
6
|
+
|
|
7
|
+
from .geometry import DEFAULT_ADJ, MAX_ADJ, NS, custgeom
|
|
8
|
+
|
|
9
|
+
_PRST = f"{{{NS}}}prstGeom"
|
|
10
|
+
_CUST = f"{{{NS}}}custGeom"
|
|
11
|
+
_XFRM = f"{{{NS}}}xfrm"
|
|
12
|
+
_AVLST_GD = f"{{{NS}}}avLst/{{{NS}}}gd"
|
|
13
|
+
|
|
14
|
+
#: Preset geometries this package knows how to reproduce as a squircle.
|
|
15
|
+
ROUNDRECT_ONLY = frozenset({"roundRect"})
|
|
16
|
+
ROUNDRECT_AND_RECT = frozenset({"roundRect", "rect"})
|
|
17
|
+
|
|
18
|
+
#: What PowerPoint draws when a preset's ``avLst`` is empty, which is how it
|
|
19
|
+
#: saves an untouched shape. ``rect`` has no adjustment; it gets ours.
|
|
20
|
+
_PRESET_DEFAULT_ADJ = {"roundRect": 16667}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _adj_of(prst) -> int:
|
|
24
|
+
"""Read a preset's own corner adjustment, clamped; the preset's default when absent."""
|
|
25
|
+
default = _PRESET_DEFAULT_ADJ.get(prst.get("prst"), DEFAULT_ADJ)
|
|
26
|
+
gd = prst.find(_AVLST_GD)
|
|
27
|
+
if gd is None:
|
|
28
|
+
return default
|
|
29
|
+
try:
|
|
30
|
+
return max(0, min(int(gd.get("fmla", "").split()[-1]), MAX_ADJ))
|
|
31
|
+
except (AttributeError, IndexError, ValueError):
|
|
32
|
+
return default
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _swap_prstgeom(prst, adj: int):
|
|
36
|
+
"""Replace an ``<a:prstGeom>`` with the squircle ``<a:custGeom>``, in place.
|
|
37
|
+
|
|
38
|
+
Replacing rather than remove-then-insert keeps the new element at exactly
|
|
39
|
+
the position the old one held, which is what guarantees DrawingML's
|
|
40
|
+
required child order inside ``spPr`` without having to reason about it.
|
|
41
|
+
"""
|
|
42
|
+
prst.getparent().replace(prst, etree.fromstring(custgeom(adj)))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def set_squircle(shape, adj: int = DEFAULT_ADJ):
|
|
46
|
+
"""Replace a python-pptx autoshape's geometry with the squircle (in place)."""
|
|
47
|
+
spPr = shape._element.spPr
|
|
48
|
+
prst = spPr.find(_PRST)
|
|
49
|
+
if prst is not None:
|
|
50
|
+
_swap_prstgeom(prst, adj)
|
|
51
|
+
return shape
|
|
52
|
+
|
|
53
|
+
old = spPr.find(_CUST)
|
|
54
|
+
if old is not None:
|
|
55
|
+
spPr.remove(old)
|
|
56
|
+
cg = etree.fromstring(custgeom(adj))
|
|
57
|
+
xfrm = spPr.find(_XFRM)
|
|
58
|
+
if xfrm is not None:
|
|
59
|
+
xfrm.addnext(cg) # geometry belongs right after the transform
|
|
60
|
+
else:
|
|
61
|
+
spPr.insert(0, cg)
|
|
62
|
+
return shape
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _convert_tree(sp_tree, wanted: frozenset, adj: int | None) -> int:
|
|
66
|
+
"""Convert every matching preset shape anywhere under ``sp_tree``.
|
|
67
|
+
|
|
68
|
+
Uses ``iter()`` rather than python-pptx's shape collections, which do not
|
|
69
|
+
descend into ``<p:grpSp>`` -- grouped shapes were previously skipped
|
|
70
|
+
silently. This also picks up pictures whose ``spPr`` carries a preset
|
|
71
|
+
geometry, i.e. images cropped to a rounded rectangle.
|
|
72
|
+
"""
|
|
73
|
+
n = 0
|
|
74
|
+
for prst in list(sp_tree.iter(_PRST)): # snapshot: we mutate while walking
|
|
75
|
+
if prst.get("prst") not in wanted:
|
|
76
|
+
continue
|
|
77
|
+
_swap_prstgeom(prst, adj if adj is not None else _adj_of(prst))
|
|
78
|
+
n += 1
|
|
79
|
+
return n
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _count_tree(sp_tree, wanted: frozenset) -> int:
|
|
83
|
+
return sum(1 for p in sp_tree.iter(_PRST) if p.get("prst") in wanted)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _sp_trees(prs_or_slide, layouts: bool, masters: bool):
|
|
87
|
+
"""-> list of (scope_name, [spTree, ...]) for the requested scopes."""
|
|
88
|
+
from pptx.presentation import Presentation as _Presentation
|
|
89
|
+
|
|
90
|
+
if not isinstance(prs_or_slide, _Presentation):
|
|
91
|
+
return [("slides", [prs_or_slide.shapes._spTree])]
|
|
92
|
+
|
|
93
|
+
prs = prs_or_slide
|
|
94
|
+
out = [("slides", [s.shapes._spTree for s in prs.slides])]
|
|
95
|
+
if layouts:
|
|
96
|
+
out.append(
|
|
97
|
+
(
|
|
98
|
+
"layouts",
|
|
99
|
+
[lay.shapes._spTree for m in prs.slide_masters for lay in m.slide_layouts],
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
if masters:
|
|
103
|
+
out.append(("masters", [m.shapes._spTree for m in prs.slide_masters]))
|
|
104
|
+
return out
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def squircle_ify_report(
|
|
108
|
+
prs_or_slide,
|
|
109
|
+
only_roundrect: bool = True,
|
|
110
|
+
adj: int | None = None,
|
|
111
|
+
layouts: bool = False,
|
|
112
|
+
masters: bool = False,
|
|
113
|
+
) -> dict[str, int]:
|
|
114
|
+
"""Convert shapes and return a per-scope breakdown of how many changed.
|
|
115
|
+
|
|
116
|
+
Keys are ``"slides"`` and, when requested, ``"layouts"`` / ``"masters"``.
|
|
117
|
+
"""
|
|
118
|
+
wanted = ROUNDRECT_ONLY if only_roundrect else ROUNDRECT_AND_RECT
|
|
119
|
+
return {
|
|
120
|
+
scope: sum(_convert_tree(t, wanted, adj) for t in trees)
|
|
121
|
+
for scope, trees in _sp_trees(prs_or_slide, layouts, masters)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def squircle_ify(
|
|
126
|
+
prs_or_slide,
|
|
127
|
+
only_roundrect: bool = True,
|
|
128
|
+
adj: int | None = None,
|
|
129
|
+
layouts: bool = False,
|
|
130
|
+
masters: bool = False,
|
|
131
|
+
) -> int:
|
|
132
|
+
"""Convert shapes in a Presentation or a single slide to squircles.
|
|
133
|
+
|
|
134
|
+
only_roundrect:
|
|
135
|
+
True (default) converts just ``roundRect`` autoshapes, preserving each
|
|
136
|
+
shape's own corner-adjustment value unless ``adj`` overrides it.
|
|
137
|
+
False also converts plain rectangles (``rect``), using ``adj`` or the
|
|
138
|
+
default corner size.
|
|
139
|
+
layouts / masters:
|
|
140
|
+
Also convert shapes on slide layouts and slide masters. Both default to
|
|
141
|
+
False: a master's placeholder frames are usually plain ``rect``, and
|
|
142
|
+
rewriting them under ``only_roundrect=False`` would mangle the
|
|
143
|
+
template. Use :func:`count_candidates` to find out what is there first.
|
|
144
|
+
|
|
145
|
+
Grouped shapes are always included, at any nesting depth.
|
|
146
|
+
|
|
147
|
+
Returns the number of shapes converted.
|
|
148
|
+
"""
|
|
149
|
+
return sum(squircle_ify_report(prs_or_slide, only_roundrect, adj, layouts, masters).values())
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def count_candidates(prs_or_slide, only_roundrect: bool = True) -> dict[str, int]:
|
|
153
|
+
"""Count convertible shapes per scope without changing anything.
|
|
154
|
+
|
|
155
|
+
Lets a caller tell the difference between "this deck has no rounded
|
|
156
|
+
rectangles" and "they are all in the layouts", which is the common case in
|
|
157
|
+
corporate templates and previously looked identical from the outside.
|
|
158
|
+
"""
|
|
159
|
+
wanted = ROUNDRECT_ONLY if only_roundrect else ROUNDRECT_AND_RECT
|
|
160
|
+
return {
|
|
161
|
+
scope: sum(_count_tree(t, wanted) for t in trees)
|
|
162
|
+
for scope, trees in _sp_trees(prs_or_slide, layouts=True, masters=True)
|
|
163
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Core squircle geometry for PowerPoint DrawingML.
|
|
2
|
+
|
|
3
|
+
Each corner is two cubic Beziers fit to a true n=4 superellipse
|
|
4
|
+
(|x|^4 + |y|^4 = 1), joining the straight edges at ZERO curvature (G2) --
|
|
5
|
+
unlike PowerPoint's roundRect, whose circular arcs join with a curvature
|
|
6
|
+
jump (G1). Corner size is expressed through guide formulas relative to
|
|
7
|
+
``ss`` (the shorter side), so the shape stays squircular under
|
|
8
|
+
non-uniform resize and carries a draggable adjustment handle.
|
|
9
|
+
|
|
10
|
+
Fit quality: max deviation from the analytic superellipse is 0.00085*R.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
NS = "http://schemas.openxmlformats.org/drawingml/2006/main"
|
|
16
|
+
|
|
17
|
+
# Bezier control offsets as fractions of the corner radius R (x100000),
|
|
18
|
+
# least-squares fit to the n=4 superellipse with zero-curvature edge joins.
|
|
19
|
+
_A1, _A2, _AM = 43593, 68179, 84090
|
|
20
|
+
|
|
21
|
+
# Text-rect inset, same units. This is roundRect's own constant, 1-cos(pi/4):
|
|
22
|
+
# matching it exactly means converting a rounded rectangle never reflows the
|
|
23
|
+
# text inside it. It also sits clear of the curve, since the squircle's 45 deg
|
|
24
|
+
# point is nearer the corner (0.15910*R) than the circle's.
|
|
25
|
+
_ATR = 29289
|
|
26
|
+
|
|
27
|
+
DEFAULT_ADJ = 40000 # corner size: 1/1000 % of the shorter side (40%)
|
|
28
|
+
MAX_ADJ = 50000
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def custgeom(adj: int = DEFAULT_ADJ) -> str:
|
|
32
|
+
"""Return the ``<a:custGeom>`` XML for a squircle.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
adj:
|
|
37
|
+
Corner size as 1/1000 of a percent of the shorter side, in
|
|
38
|
+
``0..50000`` (PowerPoint's own adjustment-value convention;
|
|
39
|
+
``40000`` = corners spanning 40% of the short side).
|
|
40
|
+
"""
|
|
41
|
+
adj = max(0, min(int(adj), MAX_ADJ))
|
|
42
|
+
G: list[tuple[str, str]] = []
|
|
43
|
+
|
|
44
|
+
def gd(n, f):
|
|
45
|
+
G.append((n, f))
|
|
46
|
+
|
|
47
|
+
gd("a", "pin 0 adj 50000")
|
|
48
|
+
gd("R", "*/ ss a 100000")
|
|
49
|
+
gd("c1", f"*/ R {_A1} 100000")
|
|
50
|
+
gd("c2", f"*/ R {_A2} 100000")
|
|
51
|
+
gd("cM", f"*/ R {_AM} 100000")
|
|
52
|
+
gd("xR", "+- r 0 R")
|
|
53
|
+
gd("xRc1", "+- xR c1 0")
|
|
54
|
+
gd("xRc2", "+- xR c2 0")
|
|
55
|
+
gd("xRcM", "+- xR cM 0")
|
|
56
|
+
gd("xLc1", "+- R 0 c1")
|
|
57
|
+
gd("xLc2", "+- R 0 c2")
|
|
58
|
+
gd("xLcM", "+- R 0 cM")
|
|
59
|
+
gd("yTc1", "+- R 0 c1")
|
|
60
|
+
gd("yTc2", "+- R 0 c2")
|
|
61
|
+
gd("yTcM", "+- R 0 cM")
|
|
62
|
+
gd("yB", "+- b 0 R")
|
|
63
|
+
gd("yBc1", "+- yB c1 0")
|
|
64
|
+
gd("yBc2", "+- yB c2 0")
|
|
65
|
+
gd("yBcM", "+- yB cM 0")
|
|
66
|
+
gd("il", f"*/ R {_ATR} 100000")
|
|
67
|
+
gd("ir", "+- r 0 il")
|
|
68
|
+
gd("ib", "+- b 0 il")
|
|
69
|
+
|
|
70
|
+
def pt(x, y):
|
|
71
|
+
return f'<a:pt x="{x}" y="{y}"/>'
|
|
72
|
+
|
|
73
|
+
def cub(a, b, c):
|
|
74
|
+
return f"<a:cubicBezTo>{pt(*a)}{pt(*b)}{pt(*c)}</a:cubicBezTo>"
|
|
75
|
+
|
|
76
|
+
P = [
|
|
77
|
+
f"<a:moveTo>{pt('R', 't')}</a:moveTo>",
|
|
78
|
+
f"<a:lnTo>{pt('xR', 't')}</a:lnTo>",
|
|
79
|
+
cub(("xRc1", "t"), ("xRc2", "t"), ("xRcM", "yTcM")),
|
|
80
|
+
cub(("r", "yTc2"), ("r", "yTc1"), ("r", "R")),
|
|
81
|
+
f"<a:lnTo>{pt('r', 'yB')}</a:lnTo>",
|
|
82
|
+
cub(("r", "yBc1"), ("r", "yBc2"), ("xRcM", "yBcM")),
|
|
83
|
+
cub(("xRc2", "b"), ("xRc1", "b"), ("xR", "b")),
|
|
84
|
+
f"<a:lnTo>{pt('R', 'b')}</a:lnTo>",
|
|
85
|
+
cub(("xLc1", "b"), ("xLc2", "b"), ("xLcM", "yBcM")),
|
|
86
|
+
cub(("l", "yBc2"), ("l", "yBc1"), ("l", "yB")),
|
|
87
|
+
f"<a:lnTo>{pt('l', 'R')}</a:lnTo>",
|
|
88
|
+
cub(("l", "yTc1"), ("l", "yTc2"), ("xLcM", "yTcM")),
|
|
89
|
+
cub(("xLc2", "t"), ("xLc1", "t"), ("R", "t")),
|
|
90
|
+
"<a:close/>",
|
|
91
|
+
]
|
|
92
|
+
gdxml = "".join(f'<a:gd name="{n}" fmla="{f}"/>' for n, f in G)
|
|
93
|
+
return (
|
|
94
|
+
f'<a:custGeom xmlns:a="{NS}">'
|
|
95
|
+
f'<a:avLst><a:gd name="adj" fmla="val {adj}"/></a:avLst>'
|
|
96
|
+
f"<a:gdLst>{gdxml}</a:gdLst>"
|
|
97
|
+
f'<a:ahLst><a:ahXY gdRefX="adj" minX="0" maxX="50000">'
|
|
98
|
+
f'<a:pos x="R" y="t"/></a:ahXY></a:ahLst>'
|
|
99
|
+
f'<a:rect l="il" t="il" r="ir" b="ib"/>'
|
|
100
|
+
f"<a:pathLst><a:path>{''.join(P)}</a:path></a:pathLst>"
|
|
101
|
+
f"</a:custGeom>"
|
|
102
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: powerpoint-squircles
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Curvature-continuous (G2) squircles for PowerPoint - the corner geometry roundRect should have had.
|
|
5
|
+
Project-URL: Homepage, https://github.com/WeinbergMalte/powerpoint-squircles
|
|
6
|
+
Project-URL: Issues, https://github.com/WeinbergMalte/powerpoint-squircles/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/WeinbergMalte/powerpoint-squircles/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Malte Weinberg
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: design,drawingml,ooxml,powerpoint,pptx,squircle,superellipse
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
24
|
+
Classifier: Topic :: Office/Business :: Office Suites
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Requires-Dist: lxml>=4.9
|
|
28
|
+
Requires-Dist: python-pptx>=0.6.21
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# powerpoint-squircles
|
|
32
|
+
|
|
33
|
+
A squircle for PowerPoint, as a native shape you can edit like any other.
|
|
34
|
+
|
|
35
|
+
<p align="center"><img src="https://raw.githubusercontent.com/WeinbergMalte/powerpoint-squircles/main/assets/hero.png" height="300" alt="PowerPoint's rounded rectangle next to a squircle, both drawn by PowerPoint"></p>
|
|
36
|
+
|
|
37
|
+
PowerPoint's rounded rectangle has quarter circles for corners. A circle turns
|
|
38
|
+
at a constant rate and a straight edge doesn't turn at all, so where the two
|
|
39
|
+
meet the outline goes from no turning to full turning with nothing in between.
|
|
40
|
+
Few people notice this consciously. Put the two shapes side by side, though,
|
|
41
|
+
and the rounded rectangle looks slightly as if its corners had been attached
|
|
42
|
+
afterwards.
|
|
43
|
+
|
|
44
|
+
A **superellipse** corner (|x|⁴ + |y|⁴ = 1) begins to turn gradually and stops
|
|
45
|
+
gradually. This repo ships that corner as geometry which PowerPoint computes
|
|
46
|
+
itself, and that has a few practical consequences:
|
|
47
|
+
|
|
48
|
+
- **It is an ordinary shape.** Plain OOXML `custGeom`. No macro, no add-in, no
|
|
49
|
+
security prompt, and nothing the recipient of your deck has to install.
|
|
50
|
+
- **The yellow handle works.** It sets the corner size from 0 to 50% of the
|
|
51
|
+
shorter side, as it does on a rounded rectangle.
|
|
52
|
+
- **It can be stretched.** The corner size is a formula over the shorter side,
|
|
53
|
+
so a wide box keeps properly shaped corners instead of smeared ones.
|
|
54
|
+
- **There are no arcs in it.** Each corner is two cubic Béziers fitted to a
|
|
55
|
+
true superellipse (maximum deviation 0.00085·R), and they meet the straight
|
|
56
|
+
edges at exactly zero curvature.
|
|
57
|
+
|
|
58
|
+
PowerPoint has no squircle among its presets and offers no way of adding one:
|
|
59
|
+
the list of preset shapes is fixed, both in the file format and in the
|
|
60
|
+
application. Custom geometry is the one route left. That is less of a
|
|
61
|
+
compromise than it sounds, because custom geometry is written in the same
|
|
62
|
+
formula language as the built-in presets, and PowerPoint re-evaluates it at
|
|
63
|
+
every size.
|
|
64
|
+
|
|
65
|
+
## Get it
|
|
66
|
+
|
|
67
|
+
### Web generator: one shape to copy
|
|
68
|
+
|
|
69
|
+
**[weinbergmalte.github.io/powerpoint-squircles](https://weinbergmalte.github.io/powerpoint-squircles)**
|
|
70
|
+
|
|
71
|
+
Pick a corner size and download a `.pptx`. The file holds a comparison slide:
|
|
72
|
+
each squircle sits next to PowerPoint's own `roundRect` at the *same*
|
|
73
|
+
adjustment value, at two aspect ratios, with a short explanation on the slide.
|
|
74
|
+
Both are drawn by your copy of PowerPoint, so you don't have to take a
|
|
75
|
+
screenshot's word for the difference. Copy a squircle into your own deck and
|
|
76
|
+
the handle comes along.
|
|
77
|
+
|
|
78
|
+
There is also a **Copy as SVG** button, in case you would rather not open a
|
|
79
|
+
downloaded file. It has a cost: PowerPoint's *Convert to Shape* turns an SVG
|
|
80
|
+
into a fixed freeform. The curve is the same, but the handle is gone and the
|
|
81
|
+
corners distort when you resize.
|
|
82
|
+
|
|
83
|
+
The page is a single static HTML file with no dependencies and no build step.
|
|
84
|
+
It doesn't even need a zip library, because an OPC package may store its
|
|
85
|
+
entries uncompressed. View source if you are curious.
|
|
86
|
+
|
|
87
|
+
The same deck is checked in as
|
|
88
|
+
[`assets/squircle_library.pptx`](https://github.com/WeinbergMalte/powerpoint-squircles/blob/main/assets/squircle_library.pptx), for offline use
|
|
89
|
+
or for keeping in a template.
|
|
90
|
+
|
|
91
|
+
### Python converter: decks you already have
|
|
92
|
+
|
|
93
|
+
To run it once, without installing anything:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
uvx --from powerpoint-squircles squircle-ify deck.pptx
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
To install `squircle-ify` and use the other flags:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
uv tool install powerpoint-squircles
|
|
103
|
+
squircle-ify deck.pptx # every rounded rectangle -> squircle (radius preserved)
|
|
104
|
+
squircle-ify deck.pptx --corner 42 --all-rects # everything, uniform 42% corners
|
|
105
|
+
squircle-ify deck.pptx --layouts --masters # reach into layouts and masters too
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Shapes inside groups are converted at any depth. Layouts and masters are only
|
|
109
|
+
touched on request, since the placeholder frames on a master are usually plain
|
|
110
|
+
rectangles that should stay that way. They are always counted in the report,
|
|
111
|
+
though: when a conversion seems to have done nothing, the rounded rectangles
|
|
112
|
+
are usually sitting in the layouts.
|
|
113
|
+
|
|
114
|
+
One thing to expect: a converted corner looks a little tighter than the one it
|
|
115
|
+
replaces. At the same corner size a superellipse reaches further into the
|
|
116
|
+
corner than a circle does (the overlay further down shows by how much). The
|
|
117
|
+
converter keeps each shape's value and doesn't try to compensate. Pass
|
|
118
|
+
`--corner` if you want them rounder.
|
|
119
|
+
|
|
120
|
+
As a library (`uv add powerpoint-squircles`, or `pip install powerpoint-squircles`)
|
|
121
|
+
it works on top of [python-pptx](https://github.com/scanny/python-pptx):
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from pptx import Presentation
|
|
125
|
+
from pptx.util import Inches
|
|
126
|
+
from pptx.enum.shapes import MSO_SHAPE
|
|
127
|
+
from powerpoint_squircles import set_squircle, squircle_ify
|
|
128
|
+
|
|
129
|
+
prs = Presentation()
|
|
130
|
+
slide = prs.slides.add_slide(prs.slide_layouts[6])
|
|
131
|
+
box = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(1), Inches(3), Inches(2))
|
|
132
|
+
set_squircle(box, adj=42000) # one shape...
|
|
133
|
+
squircle_ify(prs) # ...or every roundRect in the deck
|
|
134
|
+
prs.save("out.pptx")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Why the corner looks different
|
|
138
|
+
|
|
139
|
+
Curvature measures how sharply a curve is turning: zero on a straight edge,
|
|
140
|
+
1/r everywhere on a circle of radius r. A quarter-circle corner therefore has
|
|
141
|
+
to jump from zero to 1/r at the join. Edge and arc agree on direction there,
|
|
142
|
+
but not on the rate of turning. In the jargon that is a **G1** join.
|
|
143
|
+
|
|
144
|
+
A superellipse has zero curvature where it crosses the axes, so it joins the
|
|
145
|
+
straight edge without a step: **G2**. The curvature rises, peaks in the middle
|
|
146
|
+
of the corner and falls again.
|
|
147
|
+
|
|
148
|
+
<p align="center"><picture>
|
|
149
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/WeinbergMalte/powerpoint-squircles/main/assets/curvature-dark.png">
|
|
150
|
+
<img src="https://raw.githubusercontent.com/WeinbergMalte/powerpoint-squircles/main/assets/curvature.png" width="820" alt="Curvature combs: PowerPoint's circular arc vs an n=4 superellipse">
|
|
151
|
+
</picture></p>
|
|
152
|
+
|
|
153
|
+
*Curvature combs for both corners, each taking up the same length of straight
|
|
154
|
+
edge. **Left:** the circular arc. The teeth are at full length from the first
|
|
155
|
+
point of the corner. **Right:** the superellipse. The teeth grow from nothing
|
|
156
|
+
and shrink back. The bottom panel shows the same thing as a graph of curvature
|
|
157
|
+
against arc length. Regenerate with* `task figures`.
|
|
158
|
+
|
|
159
|
+
How much this matters depends on the corner. On a box with slightly softened
|
|
160
|
+
corners you will struggle to see it. On large, deliberately round shapes such
|
|
161
|
+
as cards, callouts and buttons it is visible, and that is where the shape is
|
|
162
|
+
worth using.
|
|
163
|
+
|
|
164
|
+
## The geometry
|
|
165
|
+
|
|
166
|
+
A quadrant of `x⁴ + y⁴ = 1` is approximated by two cubic Béziers that meet at
|
|
167
|
+
the 45° point. The constraints settle nearly everything. The curve has to pass
|
|
168
|
+
through the endpoints and the 45° point, leave the edges in exactly the edge
|
|
169
|
+
direction, have **zero curvature at the edge joins** (done by placing each
|
|
170
|
+
cubic's first three control points on the edge line), and have matching
|
|
171
|
+
tangents at the seam, which follows from symmetry. That leaves one free
|
|
172
|
+
parameter, fitted by least squares against the algebraic residual
|
|
173
|
+
`x⁴ + y⁴ − 1`.
|
|
174
|
+
|
|
175
|
+
Of the three control offsets, then, only the first is a fitted number, and the
|
|
176
|
+
result is not sensitive to it. The other two follow from the construction:
|
|
177
|
+
|
|
178
|
+
| offset | value | how it's determined |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| first control | 0.43593 | the single free parameter, least-squares fit |
|
|
181
|
+
| second control | 0.68179 | **forced**: 2·cos(π/4)^½ − 1, the only value putting the seam tangent on (1,−1) |
|
|
182
|
+
| 45° seam point | 0.84090 | **forced**: cos(π/4)^½, the exact 45° point of x⁴+y⁴=1 |
|
|
183
|
+
|
|
184
|
+
<p align="center"><picture>
|
|
185
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/WeinbergMalte/powerpoint-squircles/main/assets/corner-overlay-dark.png">
|
|
186
|
+
<img src="https://raw.githubusercontent.com/WeinbergMalte/powerpoint-squircles/main/assets/corner-overlay.png" width="440" alt="The same corner drawn four ways">
|
|
187
|
+
</picture></p>
|
|
188
|
+
|
|
189
|
+
*Could a circle have done the job? At the same corner size (grey) it is clearly
|
|
190
|
+
a different shape. A fairer comparison is the circle whose outline differs
|
|
191
|
+
least from the superellipse, `r = 0.56·R` (red), and that one follows the curve
|
|
192
|
+
closely. It still cannot reach the straight edge without a step in curvature.*
|
|
193
|
+
|
|
194
|
+
In DrawingML these become guide formulas over `ss`, the shorter side, so the
|
|
195
|
+
construction is re-evaluated by PowerPoint at every size. See
|
|
196
|
+
[`assets/squircle_custgeom.xml`](https://github.com/WeinbergMalte/powerpoint-squircles/blob/main/assets/squircle_custgeom.xml), the primitive
|
|
197
|
+
that everything else here wraps.
|
|
198
|
+
|
|
199
|
+
A caveat: the fit is G2 but not G3. At the 45° seam the curvature has a shallow
|
|
200
|
+
local minimum, 4.7% below its peak, where the true superellipse has a single
|
|
201
|
+
maximum. The curvature stays continuous, so there is no step, but a curvature
|
|
202
|
+
comb will show the dip, and I would rather mention it here than have someone
|
|
203
|
+
else find it.
|
|
204
|
+
|
|
205
|
+
## Working on it
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
task check # lint + tests, the same thing CI runs
|
|
209
|
+
task figures # regenerate everything in assets/
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The geometry exists in four places on purpose: Python, the JS in `index.html`,
|
|
213
|
+
the XML asset and the library deck. A test pins each copy, and the numbers
|
|
214
|
+
quoted above, against `src/powerpoint_squircles/geometry.py`, so changing a
|
|
215
|
+
constant in one place fails the suite until the others follow. Details in
|
|
216
|
+
[`AGENTS.md`](https://github.com/WeinbergMalte/powerpoint-squircles/blob/main/AGENTS.md).
|
|
217
|
+
|
|
218
|
+
### Publishing
|
|
219
|
+
|
|
220
|
+
Releases go out on a tag (`git tag v0.1.0 && git push origin v0.1.0`) via
|
|
221
|
+
[`.github/workflows/release.yml`](https://github.com/WeinbergMalte/powerpoint-squircles/blob/main/.github/workflows/release.yml), which checks
|
|
222
|
+
the tag against `__version__`, re-runs the suite, builds, publishes, and cuts a
|
|
223
|
+
GitHub release. Authentication is PyPI Trusted Publishing, so there is no API
|
|
224
|
+
token in this repo; it needs a publisher registered on PyPI once, under
|
|
225
|
+
*Your projects → Publishing*, with these values:
|
|
226
|
+
|
|
227
|
+
| field | value |
|
|
228
|
+
|---|---|
|
|
229
|
+
| PyPI project name | `powerpoint-squircles` |
|
|
230
|
+
| owner | `WeinbergMalte` |
|
|
231
|
+
| repository | `powerpoint-squircles` |
|
|
232
|
+
| workflow | `release.yml` |
|
|
233
|
+
| environment | `pypi` |
|
|
234
|
+
|
|
235
|
+
## Roadmap
|
|
236
|
+
|
|
237
|
+
- [x] Publish to PyPI (`pip install powerpoint-squircles`)
|
|
238
|
+
- [ ] SVG/Figma export of the identical geometry, for cross-tool consistency
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT. The constants and the XML are small enough to embed in your own project.
|
|
243
|
+
Please do. A credit is appreciated.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
powerpoint_squircles/__init__.py,sha256=1OveHoUxBerpGuxbx024fNLrTs50DWaQS55kqPinwDU,399
|
|
2
|
+
powerpoint_squircles/cli.py,sha256=5Q3pv96vGAIGIy-5fXDaUXYceVoSXeQEJee8hQeqPUc,4754
|
|
3
|
+
powerpoint_squircles/convert.py,sha256=lzncpy6xAljEy2YYARbDWytiDrDGj210C1DB0xUTG7g,5842
|
|
4
|
+
powerpoint_squircles/geometry.py,sha256=hwAc1fXp0Ab3AbjtccXeSV0gV2eXKzgpA3mYHLJ7Y8E,3698
|
|
5
|
+
powerpoint_squircles/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
powerpoint_squircles-0.1.0.dist-info/METADATA,sha256=50FrNaAbivdBqKVyFbL-P_DcRI97Kcoy6kqu24mHN4Q,11829
|
|
7
|
+
powerpoint_squircles-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
8
|
+
powerpoint_squircles-0.1.0.dist-info/entry_points.txt,sha256=WY6SjuIJNpXTzv62hRokk2eao2TXOR28D5GXJN0BBbE,63
|
|
9
|
+
powerpoint_squircles-0.1.0.dist-info/licenses/LICENSE,sha256=txrn9ewAMLWc_APMF6tun6jxRlulb-ihbiwnFfJfqlw,1071
|
|
10
|
+
powerpoint_squircles-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Malte Weinberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|