Complete
over 2 years ago

How to draw text with transparent background into palleted bitmap in SDK4?

Hello, I seem to have run into a thing that worked fine in SDK3 and doesn't work in SDK4.

I was having a paletted bitmap, and I have drawn some background into it, and then I wanted to add text.

The bitmap is defined like this:

var bitmapOptions = {
	:width => w,
	:height => h,
	:palette => [
		// in sdk 4, we need to explicitly add transparent color to palette?
		Gfx.COLOR_TRANSPARENT,
		// TODO: create palette from theme
		Gfx.COLOR_WHITE,
		Gfx.COLOR_LT_GRAY,
		Gfx.COLOR_DK_GRAY,
		Gfx.COLOR_BLACK,
		Gfx.COLOR_RED,
		Gfx.COLOR_DK_RED,
		Gfx.COLOR_BLUE,
		Gfx.COLOR_DK_BLUE,
		Gfx.COLOR_YELLOW,
		Gfx.COLOR_ORANGE,
		Gfx.COLOR_GREEN,
		Gfx.COLOR_DK_GREEN
	]
}

if (Graphics has :createBufferedBitmap) {
	var weatherBitmapInGraphicsPool = Gfx.createBufferedBitmap(bitmapOptions);
	weatherBitmap = weatherBitmapInGraphicsPool.get();
} else {
	weatherBitmap = new Gfx.BufferedBitmap(bitmapOptions);
}

I then draw a text from a non-antialiased bitmap font into it:

dc.setColor(Gfx.COLOR_RED, Gfx.COLOR_TRANSPARENT);
dc.drawText(x, y, font, text, Gfx.TEXT_JUSTIFY_CENTER);

In SDK 3 this has drawn a red text over the image.

In SDK 4 this draws a text on a solid black background, so the result looks really ugly.

I don't want to have a bitmap with full alpha channel, as it's rather large and barely fits to RAM, is there a way I could get back the SDK3 behavior?

I tried the new dc.setStroke(0xFFFF0000) and dc.setFill(0x00000000) methods, but they have absolutely no effect on my paletted bitmap at all:

dc.setColor(Gfx.COLOR_YELLOW, Gfx.COLOR_ORANGE);
dc.setStroke(0xFFFF0000);
dc.setFill(0x00000000);
dc.drawText(x, y, font, text, ...);

The above code draws yellow text on orange background into my paletted bitmap.

Parents
  • This prevents me in releasing an update for Weathergraph watchface, as I draw into a large bitmap that caches a forecast chart, and I can't find a way to draw a text over the bitmap without it having an ugly black rectangle around it.

Comment
  • This prevents me in releasing an update for Weathergraph watchface, as I draw into a large bitmap that caches a forecast chart, and I can't find a way to draw a text over the bitmap without it having an ugly black rectangle around it.

Children
No Data