0

I am using the following in Wand

 with Image(width=width, height=height) as canvas:
        left, top, width, height = 53, 247, 918, 78
        with Drawing() as context:
            context.fill_color = 'transparent'
            context.rectangle(left=left, top=top, width=width, height=height)
            font = Font('Montserrat-Regular.ttf', 0, "white")
            context(canvas)
            canvas.caption(row['DetailText'],
                           left=left, top=top, width=width, height=height, font=font, gravity='center')
        canvas.save(filename='result.png')

Which works great. I would like to prefix the text in row['DetailText'] with the word "NOTES" and have it in bold. Is there a way of doing this?

user3572565
  • 754
  • 7
  • 15

1 Answers1

2

Yes you can define multiple fonts on Wand's Drawing context. A little planning is needed as you have to isolate each text's styls from each other. Use Drawing.get_font_metrics() method to record the dimensions of text that will be rendered, and offset the remaining text.

with Image(width=300, height=100, pseudo='plasma:') as img:
    with Drawing() as ctx:
        # Global property shared between two styles.
        ctx.font_size = 25
        # Isolate single style.
        ctx.push()
        ctx.font = 'STIX-Bold-Italic'
        ctx.fill_color = 'orange'
        # Grab metrics about the rendered text.
        font_metrics = ctx.get_font_metrics(img, 'Hello ')
        # Draw first "bold" part.
        ctx.text(50, 75, 'Hello')
        ctx.pop()
        # Create another isolated style context.
        ctx.push()
        ctx.font = 'UMTypewriter'
        ctx.fill_color = 'red'
        ctx.text(50 + int(font_metrics.text_width), 75, 'world')
        ctx.pop()
        # Render context.
        ctx(img)
    img.save(filename='output.png')

output.png

emcconville
  • 23,800
  • 4
  • 50
  • 66