Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in CentOS 7
if multi_fonts:
fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(25)
box = context.text_extents(text)
border_w_h = (4, 4)
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
raise IOError('Could not fit string into image. Max char count is too large for given image width.')
# teach the RNN translational invariance by
# fitting text box randomly on canvas, with some room to rotate
max_shift_x = w - box[2] - border_w_h[0]
max_shift_y = h - box[3] - border_w_h[1]
top_left_x = np.random.randint(0, int(max_shift_x))
if ud:
top_left_y = np.random.randint(0, int(max_shift_y))
else:
top_left_y = h // 2
context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
def paint_text(self, text, w, h, rotate=False, ud=False, multi_fonts=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in CentOS 7
if multi_fonts:
fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(25)
box = context.text_extents(text)
border_w_h = (4, 4)
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
raise IOError('Could not fit string into image. Max char count is too large for given image width.')
# teach the RNN translational invariance by
# fitting text box randomly on canvas, with some room to rotate
max_shift_x = w - box[2] - border_w_h[0]
max_shift_y = h - box[3] - border_w_h[1]
top_left_x = np.random.randint(0, int(max_shift_x))
if ud:
top_left_y = np.random.randint(0, int(max_shift_y))
else:
top_left_y = h // 2
context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in CentOS 7
if multi_fonts:
fonts = [
'Century Schoolbook', 'Courier', 'STIX',
'URW Chancery L', 'FreeMono']
context.select_font_face(
np.random.choice(fonts),
cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier',
cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_BOLD)
context.set_font_size(25)
box = context.text_extents(text)
border_w_h = (4, 4)
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
raise IOError(('Could not fit string into image.'
'Max char count is too large for given image width.'))
# teach the RNN translational invariance by
# fitting text box randomly on canvas, with some room to rotate
max_shift_x = w - box[2] - border_w_h[0]
max_shift_y = h - box[3] - border_w_h[1]
top_left_x = np.random.randint(0, int(max_shift_x))
if ud:
top_left_y = np.random.randint(0, int(max_shift_y))
else:
top_left_y = h // 2
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in Centos 7
if multi_fonts:
fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(25)
box = context.text_extents(text)
border_w_h = (4, 4)
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
raise IOError('Could not fit string into image. Max char count is too large for given image width.')
# teach the RNN translational invariance by
# fitting text box randomly on canvas, with some room to rotate
max_shift_x = w - box[2] - border_w_h[0]
max_shift_y = h - box[3] - border_w_h[1]
top_left_x = np.random.randint(0, int(max_shift_x))
if ud:
top_left_y = np.random.randint(0, int(max_shift_y))
else:
top_left_y = h // 2
context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
context.rectangle(xP, yP, cellSize, cellSize)
context.stroke()
context.translate(pageWidth / 2, pageHeight / 2)
context.rotate(-angle)
context.translate(-pageWidth / 2, -pageHeight / 2)
if showLabel:
log('Drawing label...')
pageSize = min(pageWidth, pageHeight) # enables a kind of responsive design
# format fun facts and compute height of label based on number of lines
funFactsLines = []
labelHeight = 0.14*pageSize
if funFacts:
context.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(0.015*pageSize)
context.set_source_rgb(0, 0, 0)
funFacts = ' '.join(funFacts).split(' ')
# split long string into lines depending on page width
i = 0
while funFacts:
rest = []
lineWidth = context.text_extents(' '.join(funFacts))[2]
while lineWidth > pageWidth - 0.2 * pageSize:
rest.insert(0, funFacts[-1])
funFacts = funFacts[:-1]
lineWidth = context.text_extents(' '.join(funFacts))[2]
funFactsLines.append(' '.join(funFacts))
funFacts = rest
i += 1
gradient.add_color_stop_rgba(0, 0, 0, 0, 0.21)
gradient.add_color_stop_rgba(1, 0, 0, 0, 0.0)
context.rectangle(0, 0.9*pageHeight, pageWidth, 0.006*pageSize)
context.set_source(gradient)
context.fill()
# draw text
context.move_to(0.1*pageSize, 0.9*pageHeight-labelHeight+0.0895*pageSize)
context.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(0.054*pageSize)
context.set_source_rgb(0, 0, 0)
context.show_text(u'RULE {}'.format(rule))
# draw fun facts
if funFactsLines:
context.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(0.015*pageSize)
context.set_source_rgb(0, 0, 0)
for i, l in enumerate(funFactsLines):
context.move_to(0.1*pageSize, 0.9*pageHeight - labelHeight + 0.14*pageSize + i*funFactLineHeight*1.5)
context.show_text(l)
# draw rule icons (only for simple rules, would not be not legible for
# "higher" rules)
if currentStateWidth == 3:
xOffset = pageWidth-0.115*pageSize
yOffset = 0.9*pageHeight-labelHeight+0.056*pageSize
cellSize = pageSize/78
context.set_line_width(cellSize / 10)
context.set_source_rgb(0, 0, 0)
for neighbors in sorted(transistions.keys()):
def textwidth(self, text, config):
"""Calculates the width of the specified text.
"""
surface = cairo.SVGSurface(None, 1280, 200)
ctx = cairo.Context(surface)
ctx.select_font_face(config['font_face'],
cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_BOLD)
ctx.set_font_size(int(config['font_size']))
return ctx.text_extents(text)[2] + 2
def drawText():
fill = Image.colorRGB(50, 50, 50)
title_font_size = cover_width * 0.08
subtitle_font_size = cover_width * 0.05
title_font_properties = (title_font_size, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
subtitle_font_properties = (subtitle_font_size, cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_NORMAL)
title_font_family = select_font(title)
subtitle_font_family = select_font(subtitle)
title_font_properties = scale_font(title, title_font_family, title_font_properties)
subtitle_font_properties = scale_font(
subtitle,
subtitle_font_family,
subtitle_font_properties
)
title_font = cover_image.font(title_font_family, title_font_properties)
subtitle_font = cover_image.font(subtitle_font_family, subtitle_font_properties)
title_height = (cover_height - cover_width - (cover_height * cover_margin / 100)) * 0.75
x = cover_height * cover_margin / 100
y = cover_height * cover_margin / 100 * 2