Commit 5c088215 authored by Mara Karagianni's avatar Mara Karagianni
Browse files

move old code to tests folder

parent cdb4fad7
Pipeline #41 failed with stages
......@@ -6,4 +6,5 @@ covers/
finals/
chapters/
images/
tests/
from fpdf import FPDF
"""
A script for generating A5 size pdf zines
with a text-file input, under development,
GPL3 Licence, Mara Karagianni 22 March 2021
"""
# Variables
header_font = 'helvetica'
header_font_size = 14
text_font = 'helvetica'
text_font_size = 10
top_margin = 15
left_margin = 20
right_margin = 20
max_left_margin = 160
max_height = 166
current_x = 20
current_y = 30
max_y = 166
max_x = 160
cell_width = 120
cell_header_height = 16
cell_height = 8
pdf = FPDF(orientation="L", unit="mm", format="A4")
pdf.set_font(header_font, 'B', header_font_size)
pdf.set_margins(top=top_margin, left=left_margin, right=right_margin)
pdf.add_page()
# Header
title = 'Painful Mailman Migrations'
pdf.cell(cell_width, cell_header_height, title, 0, ln=1, align='C')
pdf.dashed_line(
current_x, current_y, current_x+cell_width, current_y,
dash_length=3, space_length=3)
# set font for all text
pdf.set_font(text_font, size=text_font_size)
# file to get the text
filename = "./notes_sample"
text = open(filename).readlines()
# start x and y position
pdf.set_xy(current_x, current_y+9)
class Zine(FPDF):
def __init__(self):
super().__init__()
self.cur_x = current_x
self.cur_y = current_y
self.max_x = max_left_margin
self.max_y = max_height
self.w_cell = cell_width
self.text = text
def new_signature(self):
# create new page/signature
self.add_page()
self.set_xy(self.cur_x, self.cur_.y)
self.set_margins(top=15, left=20, right=20)
print('NEW SIGNATURE')
def new_page(self):
print('*****')
print("BOTTOM OF PAGE, height is {}".format(pdf.get_y()))
print('*****')
self.set_xy(self.x, self.y)
self.set_margins(
top=15, left=160, right=20)
def create_pages(self):
index = 0
for line in self.text:
# check current page height and if we are in the right side of page
if self.get_y() >= self.max_y and self.get_x() == self.max_x:
self.new_signature()
if self.get_y() >= self.max_y:
self.new_page()
self.multi_cell(
self.w_cell, 166, self.text[index], 0, align='L')
index += 1
# def create_pages(self):
# index = 0
# for line in self.text:
# # check current page height and if we are in the right side of page
# if pdf.get_y() >= self.max_y and pdf.get_x() == self.max_x:
# self.new_signature()
#
# if pdf.get_y() >= self.max_y:
# self.new_page()
#
# pdf.multi_cell(
# self.w_cell, cell_height, self.text[index], 0, align='L')
# index += 1
zine = Zine()
zine.create_pages()
pdf.output('new_trial.pdf')
# index = 0
# for line in text:
# # check current page height
# if pdf.get_y() >= max_height and pdf.get_x() == max_left_margin:
# # create new page/signature
# pdf.add_page()
# pdf.set_xy(current_x, current_y)
# pdf.set_margins(top=top_margin, left=left_margin, right=right_margin)
# print('NEW SIGNATURE')
#
# if pdf.get_y() >= max_height:
# print('*****')
# print("BOTTOM OF PAGE, height is {}".format(pdf.get_y()))
# print('*****')
#
# pdf.set_xy(max_left_margin, current_y)
# pdf.set_margins(
# top=top_margin, left=max_left_margin, right=right_margin)
#
# if line == "\n":
# # debug if empty lines are detected
# print('EMPTY LINE {}'.format(line))
#
# print("x {}, y {}".format(pdf.get_x(), pdf.get_y()))
# print('============')
#
# pdf.multi_cell(cell_width, cell_height, text[index], 0, align='L')
# index += 1
# save pdf file
from fpdf import FPDF
"""
A script for generating A5 size pdf zines
with a text-file input, under development,
GPL3 Licence, Mara Karagianni April 2021
"""
# Variables
header_font = 'helvetica'
header_font_size = 14
text_font = 'helvetica'
text_font_size = 10
top_margin = 15
left_margin = 20
right_margin = 20
left_max_margin = 160
max_height = 166
left_x = 20
top_y = 30
cell_width = 120
cell_header_height = 16
cell_height = 8
# Image variables
img_x = 20
img_y = 80
img_len = 120
# file to get the text
filename = "./notes_sample"
class Zine(FPDF):
def add_new_signature(self, top_margin, left_margin, right_margin):
# create new page/signature
self.add_page()
self.set_margins(top_margin, left_margin, right_margin)
self.set_xy(left_margin, top_margin)
print('NEW SIGNATURE')
def move_to_page_right(self, top_margin, left_max_margin, right_margin):
print('*****')
print("BOTTOM OF PAGE, height is {}".format(self.get_y()))
print('*****')
self.set_margins(top_margin, left_max_margin, right_margin)
self.set_xy(left_max_margin, top_margin)
def position_img(self, img_filename, img_x, img_y):
import subprocess
img_size = subprocess.run(
["identify", "-format",
"%[fx:w/72] by %[fx:h/72] inches",
"./%s" % img_filename],
stdout=subprocess.PIPE, text=True)
img_height_inches = img_size.stdout.split(' ')[2]
img_width_inches = img_size.stdout.split(' ')[0]
img_height_mm = float(img_height_inches) * float(24.5)
img_width_mm = float(img_width_inches) * float(24.5)
previous_y = self.get_y()
self.image(img_filename, img_x, img_y, img_width_mm)
# use identify to get the image height and move page height
# to bottom of the image
self.set_xy(self.get_x(), previous_y+img_height_mm)
def create_pages(self, filename, max_height, left_margin,
left_max_margin, top_margin, right_margin,
cell_width, cell_height):
index = 0
text = open(filename).readlines()
for line in text:
if index > len(text)-1:
break
try:
# check if we have an image
if "<img>" in line:
img_filename = line.split("<img>")[1]
self.position_img(img_filename, self.get_x(), self.get_y())
# Ommit the image line from writing it as text input
index += 1
# check current page height and if we are in the right side of page
if self.get_y() >= max_height and self.get_x() == left_max_margin:
print("ADD SIGNATURE")
self.add_new_signature(top_margin, left_margin, right_margin)
if self.get_y() >= max_height:
self.move_to_page_right(top_margin, left_max_margin, right_margin)
except AttributeError:
# first page
print("FIRST PAGE")
self.set_margins(top=top_margin, left=left_margin,
right=right_margin)
self.set_xy(left_margin, top_margin+9)
self.add_page()
variable_x = self.get_x()
self.multi_cell(cell_width, cell_height, text[index], 0, align='L')
self.set_xy(variable_x, self.get_y())
index += 1
# set font for all text
zine = Zine(orientation="L", unit="mm", format="A4")
# TODO move to a head function the below lines
# Header
title = 'Python Mailman Migrations'
#zine.set_font(header_font, 'B', header_font_size)
#zine.cell(cell_width, cell_header_height, title, 0, ln=1, align='C')
#zine.dashed_line(
# left_x, top_y, left_x+cell_width, top_y,
# dash_length=3, space_length=3)
zine.set_font(text_font, size=text_font_size)
zine.create_pages(filename, max_height, left_margin, left_max_margin,
top_margin, right_margin, cell_width, cell_height)
zine.output('latest_trial.pdf')
#!/bin/python
"""
A script for generating A5 size pdf zines
with a text-file input, under development,
GPL3 Licence, Mara Karagianni May 2021
"""
import glob
import re
import sys
import subprocess
from fpdf import FPDF
# ref https://pyfpdf.readthedocs.io/en/latest/
# Variables
header_font = 'helvetica'
header_font_size = 14
text_font_size = 12
top_margin = 20
left_margin = 20
right_margin = 20
left_max_margin = 160
max_height = 190
left_x = 20
top_y = 30
cell_width = 110
cell_header_height = 16
cell_height = 6
# Image variables
img_x = 20
img_y = 80
img_len = 120
# file to get the text
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = "./notes_sample"
class Zine(FPDF):
def add_new_signature(self, top_margin, left_margin, right_margin):
# create new page/signature
self.add_page()
self.set_margins(left_margin, top_margin, right_margin)
self.set_xy(left_margin, top_margin)
print('NEW SIGNATURE')
def move_to_page_right(self, top_margin, left_max_margin, right_margin):
print('*****')
print("BOTTOM OF LEFT PAGE, height is {}".format(self.get_y()))
print('*****')
self.set_margins(top_margin, left_max_margin, right_margin)
self.set_xy(left_max_margin, top_margin)
def position_img(self, img_filename, img_x, img_y, **kwargs):
img_size = subprocess.run(
["identify", "-format",
"%[fx:w/72] by %[fx:h/72] inches",
"./%s" % img_filename],
stdout=subprocess.PIPE, text=True)
img_height_inches = img_size.stdout.split(' ')[2]
img_width_inches = img_size.stdout.split(' ')[0]
img_height_mm = float(img_height_inches) * float(24.5)
img_width_mm = float(img_width_inches) * float(24.5)
if kwargs:
max_height = kwargs["max_height"]
left_margin = kwargs["left_margin"]
top_margin = kwargs["top_margin"]
if self.get_y() + img_height_mm >= max_height:
self.add_page()
self.set_xy(left_margin, top_margin)
img_y = top_margin
previous_y = self.get_y()
# center image
img_x = (150 - img_width_mm) / 2
self.image(img_filename, img_x, img_y, img_width_mm)
# move y at the bottom of the image
# TODO check if get_y() would do the same job
self.set_xy(self.get_x(), previous_y+img_height_mm)
def shuffle_chapters(self, pdfinput):
subprocess.run(
["pdfseparate", "./%s" % pdfinput, "./%02d_chapter.pdf"],
stdout=subprocess.PIPE, text=True)
pages = glob.glob("./*_chapter.pdf")
pages.sort()
print("CHAPTERS {}".format(pages))
shuffled_list = []
count = 0
while count < (len(pages))/2:
shuffled_list.append(pages[-(count+1)])
shuffled_list.append(pages[count])
count += 1
# reverse
if count != len(pages)/2:
shuffled_list.append(pages[count])
shuffled_list.append(pages[-(count+1)])
count += 1
return shuffled_list
def create_pages(self, filename, max_height, left_margin,
left_max_margin, top_margin, right_margin,
cell_width, cell_height, cell_header_height,
header_font, text_font):
chapter = 0
lines = open(filename).readlines()
print(filename)
# first page
print("FIRST PAGE")
self.set_margins(left_margin, top_margin,
right_margin)
self.set_xy(left_margin, top_margin)
self.add_page()
for line in lines:
if ">>" in line:
self.set_font(text_font, '', size=16)
self.cell(cell_width, cell_height, line,
0, ln=1, align='L')
self.set_font(text_font, '', size=text_font_size)
# check if we have an image
elif line.startswith("<img>"):
img_filename = line.split("<img>")[1]
kwargs = {
"max_height": max_height,
"left_margin": left_margin,
"top_margin": top_margin
}
self.position_img(
img_filename, self.get_x(), self.get_y(), **kwargs)
# check if we have a title
elif line.startswith("<h2>"):
line = re.sub('((<h2>)|(</h2>$))', '', line)
self.set_text_color(255, 0, 255)
self.set_font(text_font, size=22)
if self.get_y() > top_margin:
self.set_xy(left_margin, top_margin+9)
self.add_page()
self.cell(cell_width, cell_header_height, line,
0, ln=1, align='C')
left_x = self.get_x()
top_y = self.get_y()
self.dashed_line(
left_x, top_y, left_x+cell_width, top_y,
dash_length=3, space_length=3)
self.set_text_color(0, 0, 0)
self.set_font(text_font, size=text_font_size)
elif line.startswith("<b>"):
line = re.sub('((<b>)|(</b>$))', '', line)
self.set_font(text_font, '', size=18)
self.cell(cell_width, cell_height, line,
0, ln=1, align='L')
self.set_font(text_font, '', size=text_font_size)
elif line.startswith("#"):
self.set_font('helvetica', 'B', size=9)
self.set_text_color(209, 17, 65)
self.multi_cell(cell_width, cell_height, line,
0, align='L')
# go back to text font
self.set_text_color(0, 0, 0)
self.set_font(text_font, '', size=text_font_size)
elif line.startswith("$") or line.startswith("(venv)"):
self.set_font('helvetica', 'B', size=9)
self.set_text_color(0, 30, 255)
self.multi_cell(cell_width, cell_height, line,
0, align='L')
# go back to text font
self.set_text_color(0, 0, 0)
self.set_font(text_font, '', size=text_font_size)
elif line.startswith("<conf>"):
#line = re.sub('(<conf>$)', '', line)
self.set_font('helvetica', 'B', size=9)
self.set_text_color(0,80,115)
self.multi_cell(cell_width, cell_height, line,
0, align='L')
elif line.startswith("</conf>"):
#line = re.sub('(</conf>$)', '', line)
self.multi_cell(cell_width, cell_height, line,
0, align='L')
self.set_font(text_font, '', text_font_size)
self.set_text_color(0, 0, 0)
elif "<index>" in line:
line = re.sub('(<index>$)', '', line)
self.set_font(text_font, '', size=18)
self.set_text_color(0, 0, 0)
self.set_text_color(41, 98, 255)
self.cell(cell_width/8, cell_height, line, 0, align='C')
elif "</index>" in line:
line = re.sub('(</index>$)', '', line)
self.set_font(text_font, '', size=text_font_size)
self.set_text_color(0, 0, 0)
self.multi_cell(cell_width, cell_height, line,
0, align='L')
else:
# check if we need the following
variable_x = self.get_x()
self.multi_cell(cell_width, cell_height, line, 0, align='L')
self.set_xy(variable_x, self.get_y())
self.footer()
def cover(self, title, cover_font, max_height):
col = 10
margin = 15
import random
self.set_margins(margin, margin, margin)
self.add_page()
for letter in title:
if self.get_y() >= max_height:
self.set_xy(margin+col, margin)
col += 40
size = random.randrange(30, 50, 15)
self.set_font(cover_font, '', size)
variable_x = margin+col
print("LETTER {}, POSITION Y {}".format(letter, self.get_y()))
print("VAR X {}".format(variable_x))
self.set_xy(variable_x, self.get_y())
self.cell(size, size, letter)
self.line(size, size, self.get_x(), self.get_y())
if(size % 2 == 0):
var = "DF"
#r = 138, 43, 226
R = random.randrange(30, 255, 40)
G = random.randrange(0, 55, 55)
B = random.randrange(0, 155, 50)
self.set_fill_color(R, G, 255)
else:
var = "D"
print(var)
self.rect(
float(self.get_x()), float(self.get_y()),
float(size/2), float(size*4), style = var)
self.ln(size/2)
def colophon(self, text):
lines = open(text, 'r').readlines()
top_margin = 20
margin = 25
self.add_font(
'CasaleNBP', '', r"/home/mara/.fonts/CasaletwoNbp-Bp4V.ttf",
uni=True)
self.set_margins(margin, top_margin, margin)
size = 13
cover_font = 'CasaleNBP'
self.set_font(cover_font, '', size)
self.add_page()
for line in lines:
self.multi_cell(100, size, line, 0, align='C')
def footer(self):
# Go to 1.5 cm from bottom
self.set_y(-15)
# Select Arial italic 8
#self.set_font('Arial', 'I', 8)
self.set_text_color(0, 0, 0)
self.set_font(text_font, '', size=text_font_size)
# Print current and total page numbers
self.cell(0, 10, '%s' % self.page_no(), 0, 0, 'C')
# set font for all text
zine = Zine(orientation="P", unit="mm", format="A5")
# cover font
zine.add_font('CasaleNBP', '', r"/home/mara/.fonts/CasaletwoNbp-Bp4V.ttf", uni=True)
cover_font = 'CasaleNBP'
# text font
zine.add_font(
'Kpalter', '', r"/home/mara/.fonts/KpProgrammerAlternatesNbp-Zg1q.ttf", uni=True)
text_font = 'Kpalter'
zine.set_font(text_font, '', size=text_font_size)
zine.create_pages(filename, max_height, left_margin, left_max_margin,
top_margin, right_margin, cell_width,
cell_height, cell_header_height, header_font, text_font)
pdf = './finals/aug_trial1.pdf'
zine.output(pdf)
#chapter_list = zine.shuffle_chapters(pdf)
#print(chapter_list)
#subprocess.Popen(["pdfunite"] + chapter_list + ["final_aug.pdf"])
#zine.cover("deconstruct mailman", cover_font, max_height=140)
#zine.output("cover.pdf")
#zine.colophon("./colophon.txt")
#zine.output("colophon.pdf")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment