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): # print(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)): #print(ex) for e in ex: if (total) + (len(e['exercice'][0])) <= max: #print(total + len(e['exercice'][0]) + 2, #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 #print('tot', pages[page_num]['jump']) 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 #print('pgages', len(pages)) pages[len(pages) - 1]['exos'][len(pages[len(pages) - 1] ['exos']) - 1]['notLast'] = False #print(list(map(lambda e:e['exos'],exos))) return pages