[cairo] “stroke” required after “showing” Pango layout?

Sebastian M. Ernst ernst at pleiszenburg.de
Mon Jun 15 06:44:36 UTC 2020


Hi all,

I have been chasing a problem between PyCairo and PangoCairo. The
following code illustrates it:

```python
import math
import cairo
import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango, PangoCairo

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 400)
ctx = cairo.Context(surface)

# TOP LEFT CIRCLE
ctx.save()
ctx.arc(100.0, 100.0, 50, 0, 2 * math.pi)
ctx.set_source_rgba(0.0, 0.0, 1.0, 1.0)
ctx.set_line_width(2.0)
ctx.stroke()
ctx.restore()

# CENTER TEXT
ctx.save()
layout = PangoCairo.create_layout(ctx)
layout.set_font_description(Pango.font_description_from_string('Arial
10.0'))
layout.set_markup('<b>Foo Bar</b>', -1)
ctx.set_source_rgba(0.0, 1.0, 0.0, 1.0)
_, text_extents = layout.get_pixel_extents()
text_width, text_height = text_extents.width, text_extents.height
ctx.translate(200.0, 200.0)
ctx.translate(-text_width / 2, -text_height / 2)
ctx.move_to(0.0, 0.0)
PangoCairo.show_layout(ctx, layout)
# ctx.stroke() # WHY?
ctx.restore()

# BOTTOM RIGHT CIRCLE
ctx.save()
ctx.arc(300.0, 300.0, 50, 0, 2 * math.pi)
ctx.set_source_rgba(1.0, 0.0, 0.0, 1.0)
ctx.set_line_width(2.0)
ctx.stroke()
ctx.restore()

surface.write_to_png('test.png')
```

It results in the attached picture, also see here:
https://i.stack.imgur.com/damCs.png

My intention is to draw two circles and text. The line between the text
and the bottom right circle is not intended to exist. I can make the
line disappear by adding / uncommenting the ctx.stroke() call directly
underneath PangoCairo.show_layout in the center text code block.

It works, but it does not feel right. The text does not require a line
stroke. What is going wrong? Is the stroke actually required or have I
made another mistake?

Full disclosure: I have also posted this question on SO [1].

Best regards,
Sebastian


1: https://stackoverflow.com/q/62376532/1672565
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.png
Type: image/png
Size: 7138 bytes
Desc: not available
URL: <https://lists.cairographics.org/archives/cairo/attachments/20200615/a7af8d22/attachment.png>


More information about the cairo mailing list