def _write_background(self, page: Page): layout = SingleColumnLayout(page) t = Table(number_of_columns=10, number_of_rows=25) for i in range(0, 25): for j in range(0, 10): put_star = random.choice([x <= 3 for x in range(0, 10)]) if i < 11 and j >= 5: t.add(Paragraph(" ", respect_spaces_in_text=True)) continue if put_star: c = random.choice( [ self.ACCENT_COLOR_1, self.ACCENT_COLOR_2, self.ACCENT_COLOR_3, self.ACCENT_COLOR_4, self.ACCENT_COLOR_5, ] ) t.add( Shape( LineArtFactory.n_pointed_star( bounding_box=Rectangle( Decimal(0), Decimal(0), Decimal(16), Decimal(16) ), n=random.choice([3, 5, 7, 12]), ), fill_color=c, stroke_color=c, line_width=Decimal(1), ) ) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) t = Table(number_of_columns=10, number_of_rows=25) for _ in range(0, 10): for _ in range(0, 25): put_star = random.choice([x <= 3 for x in range(0, 10)]) if put_star: c: Color = random.choice(self.COLORS) s: Decimal = random.choice( [ Decimal(16), Decimal(16), Decimal(16), Decimal(16), Decimal(8), Decimal(4), ] ) t.add( Shape( LineArtFactory.n_pointed_star( bounding_box=Rectangle(Decimal(0), Decimal(0), s, s), n=random.choice([3, 5, 7, 12]), ), fill_color=c, stroke_color=c, line_width=Decimal(1), ) ) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # footer rectangle_box = Rectangle( Decimal(0), Decimal(0), page.get_page_info().get_width(), page.get_page_info().get_height() * Decimal(0.1), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.COLORS[0], stroke_color=self.COLORS[0], line_width=Decimal(1), ).layout(page, rectangle_box) rectangle_box = Rectangle( Decimal(0), page.get_page_info().get_height() * Decimal(0.1), page.get_page_info().get_width(), Decimal(2), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.COLORS[1], stroke_color=self.COLORS[1], line_width=Decimal(1), ).layout(page, rectangle_box) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Match Up Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add explanation layout.add( Paragraph( """ These simple "match up" puzzles help children with observation skills. They will also need to learn a way of marking or remembering which items they have matched, so that they can identify the odd ones out. If you would like to reuse puzzles you could place counters on each "pair" that your child finds, perhaps.""", font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # random locations for each image imgs = [ "https://icons.iconarchive.com/icons/chanut/role-playing/128/Orc-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Knight-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Medusa-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Monster-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Sorceress-Witch-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Centaur-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Elf-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Poison-Spider-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Unicorn-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Viking-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Villager-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Dragon-Egg-icon.png", ] N = 10 random.shuffle(imgs) image_positions: typing.Dict[int, str] = {} for i, img_url in enumerate(imgs[0 : (N + 1)]): # place image 1 p0 = random.randint(0, N ** 2) while p0 in image_positions: p0 = random.randint(0, N ** 2) image_positions[p0] = img_url if i != 0: # place image 2 p1 = random.randint(0, N ** 2) while p1 in image_positions: p1 = random.randint(0, N ** 2) image_positions[p1] = img_url t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N ** 2): if i in image_positions: t.add(Image(image_positions[i], width=Decimal(32), height=Decimal(32))) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = MultiColumnLayout(page) # background self._write_background(page) # table avatar_urls = [ "https://avatars.githubusercontent.com/u/" + str(x) for x in self.FIRST_100_STARS ] t = Table(number_of_columns=4, number_of_rows=25) for s in avatar_urls[0 : (4 * 25)]: im = PILImage.open(requests.get(s, stream=True).raw) t.add(Image(im, width=Decimal(20), height=Decimal(20))) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) t.no_borders() layout.add(t) layout.add( Paragraph( "100 stars!", font="Helvetica-Bold", font_size=Decimal(20), font_color=self.ACCENT_COLOR_1, horizontal_alignment=Alignment.CENTERED, ) ) # next column layout.switch_to_next_column() # paragraph layout.add( Paragraph( "Thank you,", font="Helvetica-Bold", font_size=Decimal(20), font_color=self.ACCENT_COLOR_1, ) ) layout.add( Paragraph( "Your support and encouragement have always been the driving factors in the development of pText. " "I want you to know that I value your appreciation immensely!" ) ) layout.add( Paragraph( "-- Joris Schellekens", font="Helvetica-Oblique", font_size=Decimal(8), font_color=self.ACCENT_COLOR_2, ) ) layout.add( Barcode( data="https://github.com/jorisschellekens/ptext-release/stargazers", type=BarcodeType.QR, width=Decimal(128), stroke_color=self.ACCENT_COLOR_1, ) ) # footer rectangle_box = Rectangle( Decimal(0), Decimal(0), page.get_page_info().get_width(), page.get_page_info().get_height() * Decimal(0.1), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.ACCENT_COLOR_1, stroke_color=self.ACCENT_COLOR_1, line_width=Decimal(1), ).layout(page, rectangle_box) rectangle_box = Rectangle( Decimal(0), page.get_page_info().get_height() * Decimal(0.1), page.get_page_info().get_width(), Decimal(2), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.ACCENT_COLOR_2, stroke_color=self.ACCENT_COLOR_2, line_width=Decimal(1), ).layout(page, rectangle_box) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) t = Table(number_of_rows=20, number_of_columns=20) colors = [ HSVColor(Decimal(x / 360), Decimal(1), Decimal(1)) for x in range(0, 360, int(360 / 20)) ] for i in range(0, 18 * 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)], )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker(), )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker().darker(), )) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) table_rect = t.layout( page, bounding_box=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(200)), ) Paragraph( text="Love is love", font_size=Decimal(8), font_color=X11Color("Gray"), horizontal_alignment=Alignment.RIGHT, ).layout( page, bounding_box=Rectangle(Decimal(20), table_rect.y - 40, table_rect.width, Decimal(20)), ) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create empty document pdf: Document = Document() page: Page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Match The Shadow", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # add explanation layout.add( Paragraph( """ These simple "match up" puzzles help children with observation skills. They will also need to learn a way of marking or remembering which items they have matched, so that they can identify the odd ones out. If you would like to reuse puzzles you could place counters on each "pair" that your child finds, perhaps.""", font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add image imgs = [ "https://icons.iconarchive.com/icons/chanut/role-playing/128/Orc-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Knight-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Medusa-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Monster-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Sorceress-Witch-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Centaur-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Elf-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Poison-Spider-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Unicorn-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Viking-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Villager-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Dragon-Egg-icon.png", ] N = 10 imgs = imgs[0:N] random.shuffle(imgs) shadows = [ TestMatchTheShadowPuzzle._make_image_shadow(x) for x in imgs ] random.shuffle(imgs) t = Table(number_of_columns=5, number_of_rows=N) for i in range(0, N): t.add(Image(imgs[i], width=Decimal(32), height=Decimal(32))) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Image(shadows[i], width=Decimal(32), height=Decimal(32))) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create empty document pdf: Document = Document() # create empty page page: Page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Copy the picture", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) layout.add( Paragraph( """ Copy the picture using the grid lines as a guide. You might find it easier to copy one square at a time. Count the squares carefully! """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) pics: typing.List[Image] = self._split_picture_to_grid( "https://icons.iconarchive.com/icons/iconka/meow-2/128/cat-sing-icon.png" ) # fmt: off coords: typing.List[str] = [ "1A", "2A", "3A", "4A", "1B", "2B", "3B", "4B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", ] # fmt: on pics_and_coords = [x for x in zip(pics, coords)] random.shuffle(pics_and_coords) t = Table( number_of_rows=4, number_of_columns=8, column_widths=[ Decimal(1), Decimal(3), Decimal(1), Decimal(3), Decimal(1), Decimal(3), Decimal(1), Decimal(3), ], ) for p, c in pics_and_coords: t.add(Paragraph(c, font_size=Decimal(6))) t.add(Image(p)) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) t.no_borders() layout.add(t) # table 2 t2 = Table(number_of_columns=4, number_of_rows=4) for c in coords: t2.add( Paragraph( "\n" + c + "\n", respect_newlines_in_text=True, font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) t2.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t2) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) return True