Commit 47961cbf by gijs

Make label per line in multline labels to have more precise control over lineheight

parent 7b4526ca
......@@ -6,6 +6,8 @@ import json
from chiplotle import *
from chiplotle.hpgl import commands
DRAW_GRID = False
##################
## VIRTUAL ##
##################
......@@ -53,55 +55,59 @@ def header(input_text):
return header_text
def body(input_text, text_box, char_width, char_height, line_height):
text = input_text
fixed_text = ""
width_counter = 0
line_spacing = ((line_height)/(char_height * 2.0))-1.0
line_h = (line_height - (2.0 * char_height))/char_height
print(line_spacing, line_h)
def line_length(txt, char_width):
return len(txt) * char_width * 1.3
def make_label (text, char_width, char_height, offset):
line = shapes.label(text, units_to_cm(char_width), units_to_cm(char_height), -.1, 1)
transforms.offset(line, (0, -offset))
return line
def make_labels (lines, char_width, char_height, line_height):
return Group([make_label(line, char_width, char_height, i * line_height) for i, line in enumerate(lines)])
def makeLines (text, width, char_width, prefix = ""):
lines = []
buff = prefix
separator = " "
for word in text.split(" "):
word = word + " "
if width_counter + len(word) * (char_width + char_width * 0.25) < text_box:
fixed_text += word
width_counter += len(word) * (char_width + char_width * 0.25)
word += separator
if line_length(buff + word, char_width) < width:
buff += word
else:
fixed_text += "\r\n" + word
width_counter = 0
width_counter += len(word) * (char_width + char_width * 0.25)
body_text = shapes.label(fixed_text, units_to_cm(char_width), units_to_cm(char_height), -0.1, line_h)
lines.append(buff)
buff = prefix + word
return body_text
if buff:
lines.append(buff)
return lines
def body(input_text, width, char_width, char_height, line_height):
return make_labels(makeLines(input_text, width, char_width), char_width, char_height, line_height)
def list_body(input_text, text_box, char_width, char_height, line_height):
lines = []
text = input_text
indent = " "
fixed_text = ""
line_spacing = ((line_height)/(char_height * 2.0))-1.0
for line in text.split('"newline"'):
width_counter = 0
line_spacing = (line_height - (2 * char_height))/float(2 * char_height)
for line in text.split('"newline"'):
sentence = line.split(": ")
identifier = sentence[0].upper()
content = indent + sentence[1]
if len(identifier + content) * (char_width + char_width * 0.25) > text_box:
fixed_content = ""
width_counter = len(identifier) * (char_width + char_width * 0.25)
for word in content.split(" "):
word = word + " "
if width_counter + len(word) * (char_width + char_width * 0.25) < text_box:
fixed_content += word
width_counter += len(word) * (char_width + char_width * 0.25)
else:
fixed_content += "\r\n" + indent + word
width_counter = 0
width_counter += len(word) * (char_width + char_width * 0.25)
content = fixed_content
fixed_text += identifier + ":"
fixed_text += content + "\r\n"
list_body_text = shapes.label(fixed_text, units_to_cm(char_width), units_to_cm(char_height), -0.1, line_spacing)
return list_body_text
if line_length(identifier + content, char_width) > text_box:
tmp = makeLines(identifier + content, text_box, char_width)
lines.append(tmp[0])
lines += makeLines("".join(tmp[1:]), text_box, char_width, indent)
else:
lines.append(identifier + ":" + content)
return make_labels(lines, char_width, char_height, line_height)
with open("formatted_content.json", "r") as read_file:
......@@ -110,18 +116,44 @@ with open("formatted_content.json", "r") as read_file:
plotter.set_origin_bottom_left()
width = plotter.margins.hard.width
height = plotter.margins.hard.height
left = plotter.margins.hard.left
right = plotter.margins.hard.right
top = plotter.margins.hard.top
bottom = plotter.margins.hard.bottom
print(width)
print(height)
x_unit = width/3.0/3.0/8.0
y_unit = height/2.0/3.0/8.0
x_unit = width / 72.0
y_unit = height / 48.0
print(x_unit)
print(y_unit)
def x (c):
return c * x_unit
def y (c):
return c * y_unit
if DRAW_GRID:
from chiplotle.geometry.shapes.line import line
g = Group()
## add horizontal lines
for i in range(48):
g.append(line((left, y(i)), (right, y(i))))
## add vertical lines
for i in range(72):
g.append(line((x(i), top), (x(i), bottom)))
plotter.select_pen(2)
plotter.write(g)
plotter.select_pen(1)
x_movement = 2 * (32*x_unit)
y_movement = 0
......@@ -148,8 +180,8 @@ pig_header = header(p_header)
transforms.offset(bio_header, (3 * x_unit, 20*y_unit))
transforms.offset(pig_header, (14 * x_unit, 19*y_unit))
bio_body = body(b_body, 10*x_unit, 0.28 * x_unit, 0.28 * y_unit, 0.5 * y_unit)
pig_body = body(p_body, 10*x_unit, 0.28 * x_unit, 0.28 * y_unit, 0.5 * y_unit)
bio_body = body(b_body, 10*x_unit, 0.28 * x_unit, 0.3 * y_unit, 0.5 * y_unit)
pig_body = body(p_body, 10*x_unit, 0.28 * x_unit, 0.3 * y_unit, 0.5 * y_unit)
transforms.offset(bio_body, (3 * x_unit, 19*y_unit))
transforms.offset(pig_body, (14 * x_unit, 18*y_unit))
......@@ -237,12 +269,11 @@ base_y = 0
# bio_header = header(b_header)
# pig_header = header(p_header)
# transforms.offset(bio_header, ((base_x + 3) * x_unit, (base_y + 20) * y_unit))
# transforms.offset(pig_header, ((base_x + 14) * x_unit, (base_y + 20) * y_unit))
# bio_body = body(b_body, 10*x_unit, 0.3 * x_unit, 0.3 * y_unit)
# pig_body = body(p_body, 10*x_unit, 0.3 * x_unit, 0.3 * y_unit)
# transforms.offset(bio_body, ((base_x + 3) * x_unit, (base_y + 19)*y_unit))
# transforms.offset(bline_hio_header, ((base_x + 3) * x_unit, (base_y + 20) * y_unit))
# transforms.offset(pline_hig_header, ((base_x + 14) * x_unit, (base_y + 20) * y_unit))
# bio_body = body(b_bline_hody, 10*x_unit, 0.3 * x_unit, 0.3 * y_unit)
# pig_body = body(p_bline_hody, 10*x_unit, 0.3 * x_unit, 0.3 * y_unit)
# transforms.offset(bline_hio_body, ((base_x + 3) * x_unit, (base_y + 19)*y_unit))
# transforms.offset(pig_body, ((base_x + 14) * x_unit, (base_y + 19)*y_unit))
# main_text = body(text, 11*x_unit, 0.25 * x_unit, 0.3 * y_unit)
......@@ -269,10 +300,7 @@ base_y = 0
# ### PAGE 6 ###
plotter.select_pen(0)
io.view(plotter)
Markdown is supported
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