2022-05-18 10:15:54 +02:00
|
|
|
from api.Generateur import Generateur
|
|
|
|
from fontTools.ttLib import TTFont
|
|
|
|
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
|
|
|
|
|
|
|
|
|
2022-06-24 13:42:16 +02:00
|
|
|
def pdf_settings(path, number_in_serie, consigne):
|
|
|
|
|
2022-05-18 10:15:54 +02:00
|
|
|
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
|
2022-06-24 13:42:16 +02:00
|
|
|
|
2022-05-18 10:15:54 +02:00
|
|
|
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):
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
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)):
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
for e in ex:
|
|
|
|
if (total) + (len(e['exercice'][0])) <= max:
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
#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
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
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
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
pages[len(pages) - 1]['exos'][len(pages[len(pages) - 1]
|
|
|
|
['exos']) - 1]['notLast'] = False
|
2022-06-24 17:29:24 +02:00
|
|
|
#
|
2022-05-18 10:15:54 +02:00
|
|
|
return pages
|