Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created January 28, 2025 12:02
Show Gist options
  • Select an option

  • Save Krewn/06f1085a861d8659c78f58bd13a53a02 to your computer and use it in GitHub Desktop.

Select an option

Save Krewn/06f1085a861d8659c78f58bd13a53a02 to your computer and use it in GitHub Desktop.
Make a long svg for printing from the browser.
import svgwrite
def vecadd(*args):
return [sum(a) for a in zip(*args)]
vadd = vecadd
class pen:
def __init__(self,dwg=None,x=None,y=None):
self.stroke=svgwrite.rgb(10, 10, 16, '%')
self.location = (0 if x == None else x,0 if y == None else y)
if dwg==None:
self.dwg = svgwrite.Drawing('line.svg', size = (f"40px", f"40px"),insert=(100,450))
else:
self.dwg=dwg
def draw(self,v,noInk = False):
destination = vecadd(self.location,v)
if not noInk:
self.dwg.add(self.dwg.line(self.location, destination, stroke=svgwrite.rgb(10, 10, 16, '%')))
self.location = destination
def move(self,v):
self.draw(v,noInk=True)
class svgDocument:
def __init__(self, nPages= 10,numbered=True,frames=False):
self.pageHeight = 1414
self.pageWidth = int(self.pageHeight/2**0.5)-43
print(f"{self.pageHeight=}")
print(f"{self.pageWidth=}")
self.pages = [svgwrite.Drawing(f'{_}.svg',size = (f"{self.pageWidth}px",f"{self.pageHeight}px"),insert=(0,self.pageHeight*_+_//2)) for _ in range(nPages)]
if numbered:
self.drawPageNumbers()
if frames:
self.drawPageFrames()
def drawPageFrames(self):
h = self.pageHeight - 2
w = self.pageWidth - 2
for _ in self.pages:
p = pen(dwg=_,x = 1, y = 1)
p.draw((w,0))
p.draw((0,1*h))
p.draw((-1*w,0))
p.draw((0,-1*h))
_.add(_.line((0, 0), (self.pageWidth, self.pageHeight), stroke=svgwrite.rgb(10, 10, 16, '%')))
def drawPageNumbers(self):
h = self.pageHeight - 40
w = self.pageWidth - 60
for n,_ in enumerate(self.pages):
_.add(_.text(str(n+1), insert=(w, h), fill="black", font_size="23px"))
def save(self,fn):
dwg = svgwrite.Drawing(fn,size = (f"{self.pageWidth}px",f"{self.pageHeight*len(self.pages)}px"))
for _ in self.pages:
dwg.add(_)
dwg.save(pretty=True)
"""
with open(fn,"w") as outFile:
outFile.write()
"""
svgDoc = svgDocument(frames = True)
svgDoc.save("test.svg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment