Cairo Fill with Transparency
Asked Answered
O

1

0

I'm new to Cairo, trying to create text with transparent color and stroke.
stroke color's transparency works but text fill color transparency transparency_value doesn't work. If i reduce transparency_value , text color just gets darker(black) and increasing transparency_value makes text color brighter (green in my case)

 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 640, 480);
 cairo_t* cairo = cairo_create(surface);
 cairo_set_font_face(cairo, myfont_face);
 cairo_set_font_size(cairo, 25);
 cairo_text_extents_t extents;  
 cairo_text_extents(cairo, "Hello World", &extents);
 cairo_move_to(cairo, 200, 200);        
 cairo_text_path(cairo, "Hello World"); 
 double transparency_value = 0.5;
 cairo_set_source_rgba(cairo, 0,1,0,transparency_value ); //transparency doesn't work
 //cairo_fill(cairo);  //this didn't make a difference
 cairo_fill_preserve(cairo);
 cairo_set_source_rgba(cairo, 0.56, 0.76, 0.96, 0.5); //transparency works
 cairo_set_line_width(cairo, 1.5);
 cairo_stroke(cairo);
Objectify answered 8/4, 2017 at 19:45 Comment(0)
C
1

Could it be that you are drawing your text outside of the surface? In the following example I added a call to cairo_move_to(cr, 200, 200) and now I get the following result. (This is written in Lua and uses https://github.com/pavouk/lgi to call into cairo; comments indicate things that I changed compared to your version)

local cairo = require("lgi").cairo
local surface = cairo.ImageSurface.create(cairo.Format.ARGB32, 640, 480)
local cr = cairo.Context(surface)
local myfont_face = cr:get_font_face() -- I have to get this from somewhere
cr:move_to(200, 200) -- I added this line to make something appear
cr:set_font_face(myfont_face)
cr:set_font_size(25)
cr:text_path("Hello World")
local transparency_value = 0.5
cr:set_source_rgba(0, 1, 0, transparency_value)
-- cr:fill()
cr:fill_preserve()
cr:set_source_rgba(0.65, 0.76, 0.96, 0.5)
cr:set_line_width(5) -- changed from 1.5 to 5 to make it more prominent
cr:stroke()
surface:write_to_png("/tmp/out.png")

enter image description here

Edit: And this is the result when I change transparency_value to 0.1. Clearly, the result is different and transparency works correctly (when zooming in, you still see some faint green in the middle).

enter image description here

Cutworm answered 12/4, 2017 at 17:35 Comment(4)
Thanks for your reply. I had used cairo_move_to, or else i wouldn't see output. to make code short i had skipped cairo_move_to.I have updated code. am i doing any mistake in the order of function calls? and did you try changing transparency_value in your code?Objectify
See my edit. I added another image showing the result with transparency_value = 0.1. The result still looks as expected and I cannot reproduce the black that you are seeing. What am I doing "wrong"?Cutworm
hmm! is this a bug in C++ or is it possible that i didn't build cairo with freeType library properly - so getting wrong output?Objectify
A bug in C++? As in, the ISO standard? Why? And Freetype is not involved in the transparency here. So, what are you seeing on your end exactly? Can you produce a minimal reproducing example that I could run here?Cutworm

© 2022 - 2024 — McMap. All rights reserved.