Cairo test if text overlaps

Using Cairo, I am placing some text at random positions, and I need to know if they overlap any previously drawn arbitrary shapes. I could copy the path to the previous drawings and if any clipping happens it means there is an overlap. However, Cairo does not seem to have any function to tell if the clipping has occurred or not.

Is there a way to easily accomplish what I want? I guess I just want to see if the two shapes intersect or not.

+2


source to share


4 answers


I solved the problem by using Qt for rendering instead. It looks like it has quite extensive support for various path operations and supports both PDF and SVG output.



+1


source


Depending on the quality that you want, you can use cairo_stroke_extents

, cairo_fill_extents

and cairo_text_extents

and work with boundary fields.

Your best bet would be to calculate just the text border box and check the four corners against the last path with cairo_in_fill

or cairo_in_stroke

. The maximum error would be the distance from the shape of one symbol to its bounding rectangle, but perhaps this is sufficient for your purpose.



The last option is to smooth the text and check any point, as in the previous step.

+3


source


I don't see anything - at least nothing simple - and I wouldn't be surprised if nothing happens. Cairo focuses on rasterizing vector drawing operations rather than testing intersection.

However, if I was going to see if the two pieces of text overlap, here's what I would do:

  • Choose a Cairo backup that's right for my test environment - like Xlib, etc. and use the outside surface so i can use pixel pixel analysis.
  • Draw the first piece of text in solid blue at 100% alpha.
  • Draw the second piece of text in solid red at 50% alpha.
  • Scan surface for pixels where red and blue are nonzero.

It's more of a brute force, but it can even handle anti-aliasing. I've done something similar before (for a different purpose) from GTK to X, which Cairo uses indirectly.

If you don't want to do this and Cairo does not provide an API, you can add one. This will probably be difficult; you can talk to Karl Worth before doing this.

+2


source


I have an almost identical situation and I think there is a way to do it.

My problem is to determine if a text rectangle intersects with a complex set of lines (possibly antialiased) drawn on the surface. Currently this bottleneck and fast cross-movement will speed up sw by perhaps 100x. Who knows.

Anyway, thanks to the mention of ntd cairo_in_stroke I started twisting the problem. That's what.

cairo_in_stroke himself reports

"if the given point is within the area that cairo_stroke () is, given the current path and stroking options"

It's pretty useless. Unless we make the line width so wide that the path starts to gain area.

  • set the line width to half the height of the bounding rectangle (let that X)
  • check positions X, 2X, ... in the bounding box on both sides until they meet in the center.

The area under test is not a bounding box, but an intersecting chain of circles that approximate it. If you want to be safe by adding line width, make sure the bounding box never touches.

I'll let you know if this method got the actual trick (and how quickly it got).

0


source







All Articles