Skip to content

Commit e907d8b

Browse files
authored
Merge pull request #5 from AnswerDotAI/add-transform-methods
add transform methods
2 parents 459038b + 64c0d78 commit e907d8b

File tree

3 files changed

+564
-28
lines changed

3 files changed

+564
-28
lines changed

fastlucide/_modidx.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
'fastlucide.core.Icon.__repr__': ('core.html#icon.__repr__', 'fastlucide/core.py'),
1515
'fastlucide.core.Icon.__str__': ('core.html#icon.__str__', 'fastlucide/core.py'),
1616
'fastlucide.core.Icon._repr_markdown_': ('core.html#icon._repr_markdown_', 'fastlucide/core.py'),
17+
'fastlucide.core.Icon._with_trans': ('core.html#icon._with_trans', 'fastlucide/core.py'),
18+
'fastlucide.core.Icon.matrix': ('core.html#icon.matrix', 'fastlucide/core.py'),
19+
'fastlucide.core.Icon.rotate': ('core.html#icon.rotate', 'fastlucide/core.py'),
20+
'fastlucide.core.Icon.scale': ('core.html#icon.scale', 'fastlucide/core.py'),
21+
'fastlucide.core.Icon.skewX': ('core.html#icon.skewx', 'fastlucide/core.py'),
22+
'fastlucide.core.Icon.skewY': ('core.html#icon.skewy', 'fastlucide/core.py'),
23+
'fastlucide.core.Icon.translate': ('core.html#icon.translate', 'fastlucide/core.py'),
1724
'fastlucide.core.SvgSprites': ('core.html#svgsprites', 'fastlucide/core.py'),
1825
'fastlucide.core.SvgSprites.__call__': ('core.html#svgsprites.__call__', 'fastlucide/core.py'),
1926
'fastlucide.core.SvgSprites.__ft__': ('core.html#svgsprites.__ft__', 'fastlucide/core.py'),

fastlucide/core.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
from copy import deepcopy
1515
import pathlib
1616

17-
# %% ../nbs/00_core.ipynb 4
17+
# %% ../nbs/00_core.ipynb 5
1818
def read_icons():
1919
path = pathlib.Path(__file__).parent
2020
fpath = path/'_icons.py'
2121
return loads(fpath.read_text())
2222

23-
# %% ../nbs/00_core.ipynb 8
23+
# %% ../nbs/00_core.ipynb 9
2424
def sz_attrs(sz, vbox=24):
2525
if isinstance(vbox, int): vbox = (vbox, vbox)
2626
if isinstance(sz, int): sz = (sz, sz)
2727
return dict(viewBox=f'0 0 {vbox[0]} {vbox[1]}', width=f'{sz[0]}px', height=f'{sz[1]}px')
2828

29-
# %% ../nbs/00_core.ipynb 10
29+
# %% ../nbs/00_core.ipynb 11
3030
def symbol(
3131
icons, # icon dict
3232
nm, # Name of icon in lucide
@@ -37,7 +37,7 @@ def symbol(
3737
sym = [ft(t, **attrs) for t,attrs in ico]
3838
return Symbol(*sym, id=pre+nm)
3939

40-
# %% ../nbs/00_core.ipynb 12
40+
# %% ../nbs/00_core.ipynb 13
4141
def sprites(
4242
icons, # icon dict
4343
nms, # List of lucide icon names
@@ -47,7 +47,7 @@ def sprites(
4747
syms = [symbol(icons, nm, pre) for nm in nms]
4848
return Svg(Defs(*syms), style="display: none")
4949

50-
# %% ../nbs/00_core.ipynb 14
50+
# %% ../nbs/00_core.ipynb 15
5151
def _style_str(stroke=None, fill=None, stroke_width=None):
5252
"Build CSS style string from stroke/fill/stroke-width"
5353
styles = []
@@ -56,7 +56,7 @@ def _style_str(stroke=None, fill=None, stroke_width=None):
5656
if stroke_width: styles.append(f'stroke-width: {stroke_width}')
5757
return '; '.join(styles) if styles else None
5858

59-
# %% ../nbs/00_core.ipynb 15
59+
# %% ../nbs/00_core.ipynb 16
6060
class Icon:
6161
def __init__(
6262
self,
@@ -84,7 +84,50 @@ def __repr__(self): return self.__ft__().__repr__()
8484
def __str__(self): return self.__ft__().__str__()
8585
def __call__(self, *args, **kwargs): return self.__ft__().__call__(*args, **kwargs)
8686

87-
# %% ../nbs/00_core.ipynb 20
87+
# %% ../nbs/00_core.ipynb 23
88+
@patch
89+
def _with_trans(self:Icon, t):
90+
"Helper function to add the transform to all underlying uses."
91+
res = deepcopy(self)
92+
for u in res.uses: u.transform = f"{u.transform or ''} {t}".strip()
93+
return res
94+
95+
# %% ../nbs/00_core.ipynb 25
96+
@patch
97+
def rotate(self:Icon, a, cx=0, cy=0):
98+
"Rotates the element by a degrees, optionally around point (cx, cy)."
99+
return self._with_trans(f'rotate({a})' if not cx else f'rotate({a},{cx},{cy})')
100+
101+
# %% ../nbs/00_core.ipynb 27
102+
@patch
103+
def scale(self:Icon, x, y=0):
104+
"Scales the element by x (and optionally y)."
105+
return self._with_trans(f'scale({x})' if not y else f'scale({x},{y})')
106+
107+
# %% ../nbs/00_core.ipynb 31
108+
@patch
109+
def translate(self:Icon, x, y=0):
110+
"Moves the element by x horizontally and optionally y vertically."
111+
return self._with_trans(f'translate({x})' if not y else f'translate({x},{y})')
112+
113+
# %% ../nbs/00_core.ipynb 33
114+
@patch
115+
def skewX(self:Icon, a):
116+
"Skews the element along the X axis by a degrees."
117+
return self._with_trans(f'skewX({a})')
118+
119+
@patch
120+
def skewY(self:Icon, a):
121+
"Skews the element along the Y axis by a degrees."
122+
return self._with_trans(f'skewY({a})')
123+
124+
# %% ../nbs/00_core.ipynb 36
125+
@patch
126+
def matrix(self:Icon, a, b, c, d, e, f):
127+
"Applies a matrix transformation with 6 values."
128+
return self._with_trans(f'matrix({a},{b},{c},{d},{e},{f})')
129+
130+
# %% ../nbs/00_core.ipynb 42
88131
@patch
89132
def __iadd__(self:Icon, b):
90133
self.uses += b.uses
@@ -96,14 +139,14 @@ def __add__(self:Icon, b):
96139
res += b
97140
return res
98141

99-
# %% ../nbs/00_core.ipynb 27
142+
# %% ../nbs/00_core.ipynb 51
100143
def SvgStyle(cls="lucide-icon"):
101144
"Styles required for lucide icons to display correctly"
102145
return Style(f'.{cls} {{ stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }}')
103146

104-
# %% ../nbs/00_core.ipynb 28
147+
# %% ../nbs/00_core.ipynb 52
105148
class SvgSprites:
106-
"Create an track used icons"
149+
"Create and track used icons"
107150
def __init__(self, pre='', vbox=24, sz=24, cls="lucide-icon", nms=(), **attrs):
108151
nms = set(nms)
109152
self.attrs = attrs

0 commit comments

Comments
 (0)