added a make_multi_column and variable column shape functiongit add single_column_page.py
This commit is contained in:
parent
6f18856f0e
commit
8723ac945f
BIN
asciiWriter/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
asciiWriter/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
asciiWriter/__pycache__/marks.cpython-37.pyc
Normal file
BIN
asciiWriter/__pycache__/marks.cpython-37.pyc
Normal file
Binary file not shown.
BIN
asciiWriter/__pycache__/text.cpython-37.pyc
Normal file
BIN
asciiWriter/__pycache__/text.cpython-37.pyc
Normal file
Binary file not shown.
BIN
asciiWriter/__pycache__/utils.cpython-37.pyc
Normal file
BIN
asciiWriter/__pycache__/utils.cpython-37.pyc
Normal file
Binary file not shown.
BIN
asciiWriter/__pycache__/wrap_single_line.cpython-37.pyc
Normal file
BIN
asciiWriter/__pycache__/wrap_single_line.cpython-37.pyc
Normal file
Binary file not shown.
@ -1,30 +1,55 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .wrap_single_line import wrap_single_line
|
||||
from .utils import translate, merge
|
||||
|
||||
def make_column(text, linewidth=50, height=200, use_hyphenator=None):
|
||||
def make_column(text, line_width=50, height=200, use_hyphenator=None, line_offset=0):
|
||||
|
||||
lines = []
|
||||
remaining = text
|
||||
lines = []
|
||||
remaining = text
|
||||
|
||||
while remaining and len(lines) < height:
|
||||
# print('remaining:', remaining)
|
||||
while remaining and len(lines) < height:
|
||||
|
||||
if callable(linewidth):
|
||||
width = linewidth(len(lines), height)
|
||||
else:
|
||||
width = linewidth
|
||||
if callable(line_width):
|
||||
width = line_width(len(lines), height)
|
||||
else:
|
||||
width = line_width
|
||||
|
||||
line, remaining = wrap_single_line(remaining, width, use_hyphenator=use_hyphenator, replace_whitespace=False)
|
||||
lines.append(line)
|
||||
|
||||
return lines, remaining
|
||||
if callable(line_offset):
|
||||
offset = line_offset(len(lines), height)
|
||||
else:
|
||||
offset = line_offset
|
||||
|
||||
# def position_column(column_count, width, text):
|
||||
# def f (x, y, width, height, mark, blank):
|
||||
# for i in range(column_count):
|
||||
# lines, remaining = make_column(linewidth, height, text)
|
||||
line, remaining = wrap_single_line(remaining, width, use_hyphenator=use_hyphenator, replace_whitespace=False, drop_whitespace=True)
|
||||
|
||||
# return f
|
||||
line = list(line)
|
||||
|
||||
if offset != 0:
|
||||
line = [None for _ in range(offset)] + line
|
||||
|
||||
lines.append(line)
|
||||
|
||||
return lines, remaining
|
||||
|
||||
def make_multi_column(text, height=200, column_width=40, column_count=2, column_gap=5, use_hyphenator=None, space_char=None):
|
||||
# todo: vertical offset?
|
||||
|
||||
remaining = text
|
||||
i = 0
|
||||
|
||||
columns = []
|
||||
|
||||
while remaining and i < column_count:
|
||||
column, remaining = make_column(remaining, line_width=column_width, height=height, use_hyphenator=use_hyphenator)
|
||||
if i > 0:
|
||||
offset = (column_width + column_gap) * i
|
||||
column = translate(column, x=offset, y=0)
|
||||
columns.append(column)
|
||||
i += 1
|
||||
|
||||
width = (column_width + column_gap) * column_count
|
||||
lines = merge(width, height, space_char, columns)
|
||||
return lines, remaining
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(make_column('Hello world!', linewidth=25, height=10))
|
||||
print(make_column('Hello world!', line_width=25, height=10))
|
@ -17,7 +17,7 @@ def merge(width, height, space_char, layers):
|
||||
for layer in layers:
|
||||
for y in range(min(len(layer), height)):
|
||||
for x in range(min(len(layer[y]), width)):
|
||||
if layer[y][x] != space_char:
|
||||
if layer[y][x] and layer[y][x] != space_char:
|
||||
output[y][x] = layer[y][x]
|
||||
|
||||
return output
|
||||
@ -51,11 +51,11 @@ def print_lines (lines):
|
||||
for line in lines:
|
||||
stdout.write('{}\n'.format(''.join(line)))
|
||||
|
||||
def translate(shape, x=0, y=0, space_char=' '):
|
||||
def translate(shape, x=0, y=0):
|
||||
## TODO implement a negative translation?
|
||||
translated = [[] for _ in range(y)]
|
||||
|
||||
for line in shape:
|
||||
translated.append([space_char for _ in range(x)] + line)
|
||||
translated.append([None for _ in range(x)] + line)
|
||||
|
||||
return translated
|
@ -16,8 +16,6 @@ class TextWrapper(textwrap.TextWrapper):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _wrap_chunks(self, chunks):
|
||||
# print(chunks)
|
||||
# quit()
|
||||
|
||||
lines = []
|
||||
if (chunks):
|
||||
@ -56,7 +54,7 @@ class TextWrapper(textwrap.TextWrapper):
|
||||
|
||||
# First chunk on line is whitespace -- drop it, unless this
|
||||
# is the very beginning of the text (ie. no lines started yet).
|
||||
if self.drop_whitespace and chunks[-1].strip() == '' and lines:
|
||||
if self.drop_whitespace and chunks[-1].strip() == '' and chunks[-1][0] != '\n': # and lines:
|
||||
del chunks[-1]
|
||||
|
||||
while chunks:
|
||||
@ -130,15 +128,6 @@ def wrap_single_line (text, width=70, **kwargs):
|
||||
w = TextWrapper(width=width, **kwargs)
|
||||
return w.wrap(text)
|
||||
|
||||
|
||||
# [head, tail] = text.split('\n', 1)
|
||||
# if len(head) > width:
|
||||
# w = TextWrapper(width=width, **kwargs)
|
||||
# line, remaining = w.wrap(text)
|
||||
# return line, remaining + '\n' + tail
|
||||
# else:
|
||||
# return head, tail
|
||||
|
||||
if __name__ == '__main__':
|
||||
from hyphen import Hyphenator
|
||||
h_en = Hyphenator('en_US')
|
||||
|
@ -1,16 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from asciiWriter.text import make_column
|
||||
from asciiWriter.utils import merge, print_lines, make_lines
|
||||
from asciiWriter.text import make_column, make_multi_column
|
||||
from asciiWriter.utils import merge, print_lines, make_lines, translate
|
||||
from hyphen import Hyphenator
|
||||
import math
|
||||
|
||||
# Define width and height of the output
|
||||
width = 100
|
||||
height = 50
|
||||
height = 500
|
||||
|
||||
# Import a text
|
||||
text = open('texts/language.txt').read()
|
||||
print(text)
|
||||
|
||||
# Import a hyphenator
|
||||
h_en = Hyphenator('en_US')
|
||||
@ -18,17 +18,32 @@ h_en = Hyphenator('en_US')
|
||||
# Make an empty layers list
|
||||
layers = []
|
||||
|
||||
def sin_width (line_nr, _):
|
||||
amplitude = 25
|
||||
period = 150 / (math.pi * 2)
|
||||
|
||||
return 50 + math.floor(math.sin(line_nr / period) * amplitude)
|
||||
|
||||
def cos_width (line_nr, _):
|
||||
amplitude = 5
|
||||
period = 20 / (math.pi * 2)
|
||||
half_amplitude = amplitude * .5
|
||||
|
||||
return math.floor(half_amplitude + math.cos(line_nr / period) * half_amplitude)
|
||||
|
||||
# Transform the text into a column
|
||||
lines_column1, remaining = make_column(text, linewidth=30, height=height, use_hyphenator=h_en)
|
||||
lines_column2, remaining = make_column(remaining, linewidth=30, height=height, use_hyphenator=h_en)
|
||||
lines, remaining = make_column(text, height=height, use_hyphenator=h_en, line_width=sin_width, line_offset=cos_width)
|
||||
|
||||
# Transform the text into multiple columns
|
||||
# lines, remaining = make_multi_column(text, height=height-3, use_hyphenator=h_en)
|
||||
lines = translate(lines, x=15, y=1)
|
||||
|
||||
# Create an background
|
||||
background = make_lines(width, height, '*')
|
||||
background = make_lines(width, height, ' ')
|
||||
|
||||
# Add all your layers to the layers list
|
||||
layers.append(background)
|
||||
layers.append(lines_column1)
|
||||
layers.append(lines_column2)
|
||||
layers.append(lines)
|
||||
|
||||
# Merge the layers into one layer again
|
||||
merged = merge(width, height, ' ', layers)
|
||||
|
Loading…
Reference in New Issue
Block a user