[cairo] [rcairo] a problem in placing surface with "set_source"

Binzo binzo.ml at gmail.com
Sat Dec 29 03:16:23 PST 2007


Hello,

I found a problem in placing surface with "set_source(sfc, x, y)".
If you give float numbers as x, y, they seems to be forced to integer numbers.
As a result, the surface placed will be misaligned at most one pixel from the
expected position.

I think the cause of this problem is the following code in the routine
 "cr_set_source_surface" in "rb_cairo_context.c".

  cairo_set_source_surface (_SELF,
                            RVAL2CRSURFACE (surface),
                            NUM2INT (width),
                            NUM2INT (height));

NUM2INT should be NUM2DBL, isn't it?

Here is a test script.

---
require "cairo"

# surface for black rectangle image
img = Cairo::ImageSurface.new(:argb32, 15, 15)
ci = Cairo::Context.new(img)
ci.set_source_color("black")
ci.paint

# surface for output
output = Cairo::ImageSurface.new(:argb32, 35, 75)
cr = Cairo::Context.new(output)
cr.set_source_color("white")
cr.paint

# normal placing
cr.save {
  cr.translate(10.5, 10.5)
  cr.set_source(img, 0, 0)
  cr.paint
}

# invalid placing
cr.save {
  cr.set_source(img, 10.5, 30.5)
  cr.paint
}

# normal placing
cr.save {
  cr.translate(0.5, 0.5)
  cr.set_source(img, 10, 50)
  cr.paint
}

output.write_to_png("output.png")
---

Please look closely at "output.png".
Only the center rectangle is NOT placed at (10.5, 30.5) as expected.

Thanks.


More information about the cairo mailing list