Generateurv2/backend/api/exercices/pdfmaker.py
Kilton937342 bf62ac718b logs
2022-06-24 20:03:01 +02:00

73 lines
2.5 KiB
Python

from api.Generateur import Generateur
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
def pdf_settings(path, number_in_serie, consigne):
font = TTFont(
'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf')
cmap = font['cmap']
t = cmap.getcmap(3, 1).cmap
s = font.getGlyphSet()
units_per_em = font['head'].unitsPerEm
def getTextWidth(text, pointSize):
total = 0
for c in text:
if ord(c) in t and t[ord(c)] in s:
total += s[t[ord(c)]].width
else:
total += s['.notdef'].width
total = total*float(pointSize)/units_per_em
return total
exemple_exo = sorted(Generateur(path, 5, 'pdf'),
key=len, reverse=True)
margin = 1.5
column_sep = 30 * 2.54 / 72
page_width = 21 - 2 * margin
columns = int(
page_width / (int(getTextWidth(exemple_exo[0]['calcul'], 12)*2.54/72) + 1))
if columns * (getTextWidth(exemple_exo[0]['calcul'], 12)*2.54/72) + columns * column_sep + columns * (15*2.54/72) > page_width:
columns -= 2
columns -= 2
exo_list = []
exos = Generateur(path, number_in_serie, 'pdf')
return {'columns': columns, 'exos': exos, 'consigne': consigne, 'correction': exos[0]['correction'] != None}
def page_settings(exos):
#
max = 100
total = 10 + 3 + 8
pages = [{'exos': [], 'index': 1, 'jump': 0}]
page_num = 0
for ex in list(map(lambda e: e['exos'], exos)):
#
for e in ex:
if (total) + (len(e['exercice'][0])) <= max:
#
#total + len(e['exercice'][0]) + 2 < max, e['index'])
total += (len(e['exercice'][0]) * 2 * 2) + 4 + 3
e['notLast'] = True
pages[page_num]['exos'].append(e)
else:
pages[page_num]['exos'][len(
pages[page_num]['exos']) - 1]['notLast'] = False
rest = max - (total - 1)
pages[page_num]['jump'] = rest
#
id = pages[page_num]['index']
page_num += 1
pages.append({'exos': [], 'index': id + 1, 'jump': 0})
pages[page_num]['exos'].append(e)
total = (len(e['exercice'][0]) * 2 * 2) + 4 + 10 + 3 + 3 + 8
#
pages[len(pages) - 1]['exos'][len(pages[len(pages) - 1]
['exos']) - 1]['notLast'] = False
#
return pages