Example #1
0
#!/usr/bin/env python3
"""Example of text written with the default parameters."""
from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Write text with default parameters
es.text.write(
	("This is one text block written with the default parameters.\n\n"
	 "Line breaks are supported and\n"
	 "give\n"
	 "more\n"
	 "control\n"
	 "over how the text looks.\n\n"
	 "With the font size not specified, the text will fill as much of "
	 "the area as it's allowed to (in this case, the entire page)."),
	0, 0,
	"arial.ttf"
	)

# Write our drawing to a PNG file
es.write_to_png("example_write_1.png")
#!/usr/bin/env python3
"""Examples of various ways to draw a line."""
import cairo

from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Draw a black line from (50, 250) to (200, 50)
es.draw.line(50, 250, 200, 50)

# Draw a blue line 10 pixels thick from (350, 350) to (500, 100)
es.draw.line(350, 350, 500, 100, color=(0, 0, 255), line_width=10)

# Draw a red line 20 pixels thick from (50, 500) to (250, 700) with a rounded
# line cap.
es.draw.line(50,
             500,
             250,
             700,
             line_cap=cairo.LINE_CAP_ROUND,
             color=(255, 0, 0),
             line_width=20)

# Draw an X in the bottom-right quadrant
es.draw.line("center", "center", "right", "bottom")
es.draw.line("center", "bottom", "right", "center")

# Draw gridlines for reference (outline + vertical/horizontal center lines)
Example #3
0
#!/usr/bin/env python3
"""Examples of various ways to draw a rectangle."""
from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to yellow
es = ExtendedSurface(600, 800)
es.set_background((255, 255, 0))

# Draw a black rectangle at (50, 50) and measuring 100x200 pixels
es.draw.rectangle(50, 50, 100, 200)

# Draw a blue rectangle at ("left", 650) and measuring 200x100 pixels
es.draw.rectangle("left", 650, width=200, height=100, color=(0, 0, 255))

# Draw an empty rectangle at (200, "top") measuring 200x250 pixels
es.draw.rectangle(200, "top", 200, 250, fill=False)

# Draw a pink rectangle at ("right", "bottom") that fills in the entire
# bottom-right quadrant with a purple outline that is 20 pixels thick
es.draw.rectangle(
	x="right", y="bottom",
	width=es.get_width()/2, height=es.get_height()/2,
	color=(255, 192, 203), outline=20, outline_color=(128, 0, 128)
	)

# Draw a transparent lime-green rectangle at ("center", "center") measuring
# 250x250 pixels and with a red outline
es.draw.rectangle("center", "center", 250, 250,
	color=(50, 205, 50, 128), outline_color=(255, 0, 0))

# Draw gridlines for reference (outline + vertical/horizontal center lines)
Example #4
0
from src.ExtendedSurface import ExtendedSurface


def random_rectangles(surface, num_rectangles):
    """Draw rectangles randomly all over the ExtendedSurface object."""
    for _ in range(num_rectangles):
        width = random.randint(0, surface.get_width() / 2)
        height = random.randint(0, surface.get_height() / 2)
        x = random.randint(0, surface.get_width() - width)
        y = random.randint(0, surface.get_height() - height)
        color = random.choices(range(256), k=4)
        surface.draw.rectangle(x, y, width, height, color=color)


# Create an ExtendedSurface object, sized 600x800 pixels
es = ExtendedSurface(600, 800)

# Set the background to red
es.set_background((255, 0, 0))

# Draw 10 rectangles randomly placed on it
random_rectangles(es, 10)

# Crop it to (400, 500) starting at (50, 50)
es.crop(50, 50, 400, 500)

# Create a second ExtendedSurface object, sized 100x200 pixels
es2 = ExtendedSurface(100, 200)

# Set the background to yellow
es2.set_background((255, 255, 0))
Example #5
0
#!/usr/bin/env python3
"""Examples of text written with typical parameters sent in."""
from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Write left-aligned text in the top-left quadrant
x, y = "left", "top"
text = ("This text has the default alignment (left) and is bound by the "
		"parameters max_width and max_height to be contained to this quadrant "
		"of the page. It has the line spacing set to 2.0, but the font size "
		"isn't specified so this text will fill up as much space as it's "
		"allowed.")
bb_width, bb_height = es.text.write(
	text,
	x, y,
	font="arial.ttf",
	line_spacing=2.0,
	max_width=es.get_width()/2,
	max_height=es.get_height()/2
	)
es.draw.rectangle(x, y, bb_width, bb_height, fill=False)

# Write right-aligned text in the top-right quadrant with a font size of 25 pts
x, y = "right", "top"
text = ("This text is right-aligned. Its font size is set to 25 pts so it "
		"will not automatically adjust, even if the text goes off the page.")
bb_width, bb_height = es.text.write(
	text,
Example #6
0
#!/usr/bin/env python3
"""Examples of various ways to draw a polygon."""
import math

from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Draw a black triangle
es.draw.polygon([(250, 60), (200, 350), (50, 200)])

# Draw an empty blue quadrilateral that touches all four sides of the
# upper-right quadrant
quadrilateral = [("center", 50), (500, "center"), ("right", 200), (400, "top")]
es.draw.polygon(points=quadrilateral, fill=False, color=(0, 0, 255))

# Draw a green pentagon with a red outline that is 10 pixels thick
radius = 100
angle = 72  #degrees
pentagon = []
origin = (es.get_width() * 1 / 4, es.get_height() * 3 / 4)
for i in range(5):
    x = origin[0] + radius * math.cos(math.radians(-90 + i * angle))
    y = origin[1] + radius * math.sin(math.radians(-90 + i * angle))
    pentagon.append((x, y))

es.draw.polygon(points=pentagon,
                color=(0, 255, 0),
                outline=10,
#!/usr/bin/env python3
"""Examples of text written with more specialized parameters sent in."""
from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Write text to demonstrate automatic line breaks
text_buffer = 30
x = "center"
y = 10
text = ("This is an example of text that has line breaks automatically put "
        "into it in order to best fit its constraints.\n\n"
        "This is useful if you know the area that you want the text to stay "
        "within, but you don't know what size to make the font. This default "
        "makes the text look as nice as possible.")
bb_width, bb_height = es.text.write(text,
                                    x,
                                    y,
                                    font="arial.ttf",
                                    max_height=es.get_height() / 6,
                                    padding={
                                        "top": 5,
                                        "right": 5,
                                        "bottom": 5,
                                        "left": 5
                                    })
es.draw.rectangle(x, y, bb_width, bb_height, fill=False)

# Write text to demonstrate what happens without automatic line breaks