def get_segmented_line(layer_number): if layer_number > self.number_of_layers: layer_number = self.number_of_layers layer_nodes = flatten(self.get_layer(layer_number)) lengths = [node.value * unit for node in layer_nodes] hsl = HorizontalSegmentedLine(lengths) for segment, node in zip(hsl.segments, layer_nodes): segment.node = node segment.start_mark_line.length = get_ml_length(node) return hsl
def test_with_clipping_area(self): c = DrawObjectColumn() c.bottom_margin = 60 c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5)) c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2])) ca = ClippingArea(self.pdf, draw_object=c) self.pdf.translate_page_margins() ca.draw() with self.file_path(path, 'with_clipping_area', 'pdf') as pdf_path: self.pdf.write(pdf_path)
def test_below_with_different_mark_line_lengths(self): hsl = HorizontalSegmentedLine(lengths=[10, 15, 20]) hsl.segments[0].start_mark_line.length = 6 hsl.segments[0].start_mark_line.add_label(1, placement='below', font_size=8) hsl.segments[1].start_mark_line.add_label(2, placement='below', font_size=8) hsl.segments[2].start_mark_line.add_label(3, placement='below', font_size=8) with self.file_path(path, 'below_with_different_mark_line_lengths', 'pdf') as pdf_path: self.pdf.translate_page_margins() self.pdf.draw_ruler('h') self.pdf.draw_ruler('v') self.pdf.translate(10, 10) hsl.draw(self.pdf) self.pdf.write(pdf_path)
def test_column_line_break(self): def draw_with_clip(index): with self.pdf.saved_state(): self.pdf.clip_rect(-1, -5, 196, 50) self.pdf.translate(index * -190, 0) c.draw(self.pdf) c = DrawObjectColumn() c.bottom_margin = 10 c.add_draw_object(HorizontalRuler(length=800)) c.add_draw_object(HorizontalSegmentedLine(lengths=400 * [2])) with self.file_path(path, 'column_line_break', 'pdf') as pdf_path: self.pdf.translate_page_margins() number_of_rows = int(ceil(c.get_width() / 190)) for index in range(number_of_rows): if index != 0: self.pdf.translate(0, c.get_height()) draw_with_clip(index) self.pdf.write(pdf_path)
def test_column_page_break(self): self.pdf.t_margin = 15 def _prepare_page(): self.pdf.translate_page_margins() self.pdf.draw_ruler('v') self.pdf.draw_ruler('h') self.pdf.translate(clip_area_left_margin, clip_area_top_margin) def _add_page(): self.pdf.add_page() _prepare_page() def draw_with_clip(index): with self.pdf.saved_state(): self.pdf.clip_rect(-1, -5, clip_area_width + 1.14, clip_area_height) self.pdf.translate(index * -clip_area_width, 0) c.draw(self.pdf) c = DrawObjectColumn() c.bottom_margin = 60 c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5)) c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2])) clip_area_left_margin = 10 clip_area_top_margin = 10 clip_area_width = self.pdf.w - self.pdf.l_margin - self.pdf.r_margin - clip_area_left_margin clip_area_height = c.get_height() with self.file_path(path, 'column_page_break', 'pdf') as pdf_path: _prepare_page() number_of_rows = int(ceil(c.get_width() / clip_area_width)) for index in range(number_of_rows): if index != 0: self.pdf.translate(0, c.get_height()) if self.pdf.absolute_y > self.pdf.h - self.pdf.b_margin: _add_page() draw_with_clip(index) self.pdf.write(pdf_path)
def test_clipped_draw(self): c = DrawObjectColumn() c.bottom_margin = 60 c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5)) c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2])) self.pdf.translate_page_margins() title = PageText('A very nice title', v_position='center', font_weight='bold', font_size=12, top_margin=10) title.draw(self.pdf) self.pdf.translate(0, title.get_height()) c.clipped_draw(self.pdf) for page in self.pdf.pages: self.pdf.page = page self.pdf.reset_position() self.pdf.translate_page_margins() self.pdf.draw_ruler('h') self.pdf.draw_ruler('v') with self.file_path(path, 'clipped_draw', 'pdf') as pdf_path: self.pdf.write(pdf_path)
class TestHorizontalSegmentedLine(TestCase): def setUp(self) -> None: self.pdf = Pdf() self.hsl = HorizontalSegmentedLine(lengths=[10, 15, 20, 25]) def test_draw(self): with self.file_path(path, 'draw', 'pdf') as pdf_path: with self.file_path(parent_path=path, name='draw', extension='pdf') as pdf_path: self.pdf.translate_page_margins() self.pdf.draw_ruler('h') self.pdf.draw_ruler('v') self.pdf.translate(10, 10) self.hsl.draw(self.pdf) self.pdf.write(pdf_path) def test_get_height(self): self.hsl.segments[1].start_mark_line.length = 5 actual = self.hsl.get_height() expected = 5 self.assertEqual(expected, actual) def test_add_label_left(self): self.hsl.add_label('first left label', placement='left') self.hsl.add_label('second left label', placement='left') self.hsl.add_label('third left label', placement='left') self.hsl.add_label('fourth left label', placement='left') self.hsl.segments[0].start_mark_line.length = 10 self.pdf.translate_page_margins() self.pdf.draw_ruler('h') self.pdf.draw_ruler('v') self.pdf.translate(20, 20) self.hsl.draw(self.pdf) # print(self.hsl.get_relative_position()) # print(self.hsl.text_labels[0].get_relative_position()) # print(self.hsl.get_relative_y2()) # print(self.hsl.get_height()) with self.file_path(path, 'add_label_left', 'pdf') as pdf_path: self.pdf.write(pdf_path)
def setUp(self) -> None: self.pdf = Pdf() self.hsl = HorizontalSegmentedLine(lengths=[10, 15, 20, 25])