def test_padding_margins_with_aspect(self): params = { "class": "Diagram", "type": "triangle", "number_level": 1, "margin": "10%", "padding": "20%", "aspect_ratio": 0.5 } self._create_drawable(params) page = create_test_page() bounds = Bounds(20, 10, BOUNDS_WIDTH, BOUNDS_HEIGHT) self.drawable.update_page_parameters(page) self.drawable.layout(bounds) test_bounds = self.drawable.bounds self.assertEqual(test_bounds.x, bounds.x) self.assertEqual(test_bounds.y, bounds.y) self.assertEqual(test_bounds.width, bounds.width) self.assertEqual(test_bounds.height, 80) test_bounds = self.drawable.inner_bounds self.assertEqual(test_bounds.x, int(bounds.x + (bounds.width * 0.3))) self.assertEqual(test_bounds.y, int(bounds.y + (bounds.width * 0.3))) self.assertEqual(test_bounds.width, int(bounds.width * 0.4)) self.assertEqual(test_bounds.height, int((bounds.width // 2) * 0.4))
def inner_bounds(self): size = self._calculate_content_from_size(self._bounds.size) left = self._bounds.x + self._margin_left left += self._padding_left + self.get_section_number_width() top = self._bounds.y + self._margin_top + self._padding_top return Bounds(left, top, size.width, size.height)
def layout(self): bounds = self.bounds for child in self.children: new_bounds = Bounds(bounds.x, bounds.y, bounds.width, child.bounds.height) child.layout(new_bounds) bounds = child.bounds.move(0, child.bounds.height)
def __init__(self, parameters): super().__init__(parameters) self._children = [] self.column_width = 0 self.layout_policy = self.__get_layout_policy( Bounds(0, 0, Drawable.FILL_PARENT, Drawable.FILL_PARENT)) self.create_children(parameters)
def layout(self, bounds): super().layout(bounds) self.layout_policy.update_bounds(self.inner_bounds) self.layout_policy.layout() size = self.layout_policy.get_element_size() outer_size = self.calculate_size_from_inner_size(size) self._bounds = Bounds(bounds.x, bounds.y, bounds.width, outer_size.height)
def test_impossible_page_tile(self) : DOC_SIZE = (1000,2000) FRAMES = [Frame(Bounds(0,0,1000,1200), "0") ] self.create_tiler(DOC_SIZE, FRAMES) tile = self.image_tiler._ImageTiler__get_page_tile() self.assertIs(tile, None)
def setUp(self): random.seed(42) self.params = PageParameters("test", options) self.params.generate_random_parameters() self.draw = Draw(self.params) self.draw.init_image() self.draw.create_draw() self.diagram = Diagram(self.draw, Bounds(0, 0, 640, 480), 2)
def setUp(self): random.seed(42) self.page = create_test_page() self.bounds = Bounds(5, 10, 100, 50) self.children = [Drawable({}), Drawable({}), Drawable({})] for child in self.children: child.update_page_parameters(self.page) child.calculate_dimensions(MagicMock(), self.bounds.size)
def get_area_for_next_fragment(self): self.area_for_next_fragment = None rect = self.get_current_write_location() if rect is None: return self.area_for_next_fragment = Bounds(rect[0][0], rect[0][1], x2=rect[1][0], y2=rect[1][1])
def __convert_boxes_to_frames(self): frames = [] for box, frame in zip(self.ia_boxes.bounding_boxes, self.frames): if box.x1 == -1 and box.y1 == -1: continue bounds = Bounds(box.x1, box.y1, x2=box.x2, y2=box.y2) frames.append(Frame(bounds, frame.label)) return frames
def test_aspect_ratio(self): for diagram_type in diagram_parameters["type"]: self._create_drawable({"type": diagram_type, "aspect_ratio": 0.5}) bounds = Bounds(20, 10, 200, 100) self.drawable.layout(bounds) test_bounds = self.drawable.bounds self.assertEqual(test_bounds.x, bounds.x) self.assertEqual(test_bounds.y, bounds.y) self.assertEqual(test_bounds.width, bounds.width) self.assertEqual(test_bounds.height, bounds.width // 2)
def do_hello_world_alignment_test(self, align) : text = "Hello World" renderder = TextRenderer(self.draw, text, TEXT_COLOR) text_size = draw_attrs['text_size.side_effect'] bounds = Bounds(0,0,text_size(text)[0],100) height = renderder.calculate_text_height(bounds) self.assertEqual(height, LINE_HEIGHT * LINE_SPACING) bounds = Bounds(0,0,text_size(text)[0] * 1.5,100) height = renderder.calculate_text_height(bounds) self.assertEqual(height, LINE_HEIGHT * LINE_SPACING) bounds = Bounds(0,0,text_size(text)[0] / 2,100) height = renderder.calculate_text_height(bounds) self.assertEqual(height, LINE_HEIGHT * LINE_SPACING * 2)
def __get_fragment_tile(self, frame): mid_x = frame.x + frame.width / 2 mid_y = frame.y + frame.height / 2 max_dim = max(frame.width, frame.height) half_dim = max_dim / 2 top = mid_y - half_dim left = mid_x - half_dim right = left + max_dim bottom = top + max_dim fragment_region = Bounds(left, top, x2=right, y2=bottom) inflate_by = random.randint(max_dim // 4, max_dim // 2) fragment_region = fragment_region.inflate(inflate_by, inflate_by) frames = self.__get_frames_for_region(fragment_region) compositor = PageCompositor(self.image, fragment_region) return compositor.make_composite_image(), frames
def __get_page_tile(self): img_size = self.image.size for attempts in range(5): y_offset = random.randint(0, img_size[1] - img_size[0]) bounds = Bounds(0, y_offset, img_size[0], img_size[0]) frames = self.__get_frames_for_region(bounds) if frames: img = self.__extract_image_region(bounds) return img, frames
def calculate_dimensions(self, draw, size): self._bounds = Bounds(0, 0, size.width, size.height) self.text_size = draw.text_size(self.text + " ") formula_size = self.image.size while self.text and self.text_size[0] + formula_size[ 0] > self.inner_bounds.width: self.text = " ".join(self.text.split(' ')[:-2]) self.text_size = draw.text_size(self.text + " ") if formula_size[1] > self.text_size[1]: self.text_height = formula_size[1] self.text_y = ((formula_size[1] - self.text_size[1]) // 2 - 1) else: self.text_height = self.text_size[1] self.text_y = 0 super().update_bounds()
def layout(self): x = self.bounds.x y = self.bounds.y col = 0 max_height = 0 self.height = 0 for child in self.children: height = min(child.bounds.height, self.cell_height) child_bounds = Bounds(x, y, self.cell_width, height) child.layout(child_bounds) max_height = max(max_height, child.bounds.height) col += 1 if col % self._cols == 0: x = self.bounds.x y += max_height self.height += max_height max_height = 0 else: x += self.cell_width
def __init__(self, parameters): self._parameters = parameters self._bounds = Bounds() self._margin_left = 0 self._margin_top = 0 self._margin_right = 0 self._margin_bottom = 0 self._padding_left = 0 self._padding_top = 0 self._padding_right = 0 self._padding_bottom = 0 self._border_style = None self._border_width = 1 self._border_color = None self._foreground_color = None self._background_color = None self._section_number = None self._primary_element = None self._number_level = None
def __get_whole_page(self): rect = Bounds(0, 0, self.image.width, self.image.height) image = PageCompositor.copy_image_from_rect(self.image, rect) return image, self.frames
def __bounds_from_call(self, call) : m = re.search(r'x: ([-\d]+) y: ([-\d]+) Width: (\d+) Height: (\d+)', str(call)) self.assertFalse(m is None) bounds = Bounds(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) return bounds
from unittest.mock import MagicMock, patch, call from unittest import TestCase import re import random from augmentation.image_tiler import ImageTiler from augmentation.compositor import PageCompositor from graphics import Frame, Bounds FRAMES = [Frame(Bounds(0,0,100,100), "0"), Frame(Bounds(500,500,500,500), "1")] class ImageTilerTest(TestCase) : def setUp(self) : random.seed(42) def create_tiler(self, document_size, frames) : self.page = MagicMock(**{ "get_frames.return_value" : frames, "get_image.return_value" : MagicMock(**{ "size" : document_size }), }) self.image_tiler = ImageTiler(self.page) def __bounds_from_call(self, call) : m = re.search(r'x: ([-\d]+) y: ([-\d]+) Width: (\d+) Height: (\d+)', str(call)) self.assertFalse(m is None) bounds = Bounds(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) return bounds
def calculate_dimensions(self, draw, size): self._bounds = Bounds(0, 0, size.width, size.height) self.update_bounds()
def layout(self, bounds): self._bounds = Bounds(bounds.x, bounds.y, bounds.width, bounds.height) # self._bounds.height)
def border_bounds(self): if self._has_border(): return Bounds(self._bounds.x + self._margin_left, self._bounds.y + self._margin_top, x2=self._bounds.x2 - self._margin_right, y2=self._bounds.y2 - self._margin_bottom)
def calculate_dimensions(self, draw, size) : self._bounds = Bounds(0, 0, size.width, size.height) self.text_render = TextRenderer(draw, self.text, self.color, self.align, end_text = self.end_text) self.text_height = self.text_render.calculate_text_height(self.inner_bounds) super().update_bounds()