Skip to content

Commit

Permalink
Version 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
sukop committed Jul 27, 2024
1 parent 43bae3d commit 918f040
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
10 changes: 6 additions & 4 deletions flat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# https://xxyxyz.org/flat
#
# Copyright (c) 2013-2020 Juraj Sukop
# Copyright (c) 2013-2024 Juraj Sukop
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
Expand All @@ -25,11 +25,12 @@
#

__all__ = [
'ean13',
'gray', 'ga', 'rgb', 'rgba', 'cmyk', 'spot', 'overprint',
'moveto', 'lineto', 'quadto', 'curveto', 'closepath',
'document',
'view',
'tree',
'tree', 'binpacker'
'font',
'group',
'image', 'raw',
Expand All @@ -39,13 +40,14 @@
'shape',
'parsepath',
'strike', 'paragraph', 'text', 'outlines']
__version__ = '0.3.2'
__version__ = '0.4.0'

from .barcode import ean13
from .color import gray, ga, rgb, rgba, cmyk, spot, overprint
from .command import moveto, lineto, quadto, curveto, closepath
from .document import document
from .even import view
from .extra import tree
from .extra import tree, binpacker
from .font import font
from .group import group
from .image import image, raw
Expand Down
60 changes: 60 additions & 0 deletions flat/barcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from itertools import groupby




edge, center = (1, 0, 1), (0, 1, 0, 1, 0)

patterns = [
((0, 0, 0, 1, 1, 0, 1), (0, 1, 0, 0, 1, 1, 1)),
((0, 0, 1, 1, 0, 0, 1), (0, 1, 1, 0, 0, 1, 1)),
((0, 0, 1, 0, 0, 1, 1), (0, 0, 1, 1, 0, 1, 1)),
((0, 1, 1, 1, 1, 0, 1), (0, 1, 0, 0, 0, 0, 1)),
((0, 1, 0, 0, 0, 1, 1), (0, 0, 1, 1, 1, 0, 1)),
((0, 1, 1, 0, 0, 0, 1), (0, 1, 1, 1, 0, 0, 1)),
((0, 1, 0, 1, 1, 1, 1), (0, 0, 0, 0, 1, 0, 1)),
((0, 1, 1, 1, 0, 1, 1), (0, 0, 1, 0, 0, 0, 1)),
((0, 1, 1, 0, 1, 1, 1), (0, 0, 0, 1, 0, 0, 1)),
((0, 0, 0, 1, 0, 1, 1), (0, 0, 1, 0, 1, 1, 1))]

encodings = [
(0, 0, 0, 0, 0, 0),
(0, 0, 1, 0, 1, 1),
(0, 0, 1, 1, 0, 1),
(0, 0, 1, 1, 1, 0),
(0, 1, 0, 0, 1, 1),
(0, 1, 1, 0, 0, 1),
(0, 1, 1, 1, 0, 0),
(0, 1, 0, 1, 0, 1),
(0, 1, 0, 1, 1, 0),
(0, 1, 1, 0, 1, 0)]




def checksum(digits):
even, odd = sum(digits[0::2]), sum(digits[1::2])
return (10 - (3*even + odd))%10

def structure(digits):
encoding, first, second = encodings[digits[0]], digits[1:7], digits[7:13]
yield from edge
for digit, selector in zip(first, encoding):
yield from patterns[digit][selector]
yield from center
for digit in second:
yield from (1-x for x in patterns[digit][0])
yield from edge

def ean13(string):
digits = list(map(int, string))
if len(digits) != 13:
raise ValueError('Invalid number of digits.')
if checksum(digits[:12]) != digits[12]:
raise ValueError('Invalid checksum.')
for key, group in groupby(structure(digits)):
yield len(list(group))




54 changes: 54 additions & 0 deletions flat/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,57 @@ def _second_walk(v, m, level):



class _bin_partition(object):

__slots__ = 'x', 'y', 'width', 'height'

def __init__(self, x, y, width, height):
self.x, self.y, self.width, self.height = x, y, width, height

def admits(self, width, height):
return self.width >= width and self.height >= height

def overlaps(self, x, y, width, height):
return self.x < x+width and self.x+self.width > x and self.y < y+height and self.y+self.height > y

def contains(self, x, y, width, height):
return self.x <= x and self.x+self.width >= x+width and self.y <= y and self.y+self.height >= y+height

def subdivide(self, x, y, width, height):
if self.x < x:
yield _bin_partition(self.x, self.y, x-self.x, self.height)
if self.x+self.width > x+width:
yield _bin_partition(x+width, self.y, self.x+self.width-(x+width), self.height)
if self.y < y:
yield _bin_partition(self.x, self.y, self.width, y-self.y)
if self.y+self.height > y+height:
yield _bin_partition(self.x, y+height, self.width, self.y+self.height-(y+height))




class binpacker(object):

def __init__(self, width, height):
self.partitions = [_bin_partition(0, 0, width, height)]

def pack(self, width, height):
for partition in self.partitions:
if partition.admits(width, height):
break
else:
return 0, 0, False
x, y = partition.x, partition.y
partitions = []
for partition in self.partitions:
if partition.overlaps(x, y, width, height):
partitions.extend(partition.subdivide(x, y, width, height))
else:
partitions.append(partition)
self.partitions = [p for p in partitions if not any(
q.contains(p.x, p.y, p.width, p.height) for q in partitions if q != p)]
return x, y, True




2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013-2020 Juraj Sukop
Copyright (c) 2013-2024 Juraj Sukop

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'Flat',
description = 'Generative infrastructure for Python',
version = '0.3.2',
version = '0.4',
packages = ['flat'],
author = 'Juraj Sukop',
author_email = '[email protected]',
Expand Down

0 comments on commit 918f040

Please sign in to comment.