rebible
rebible

Reputation: 99

Fixed: canvas.filltext on android with fmx font is surrounded by block backgrounds

fixed see answer.

I found the posts regarding how to get text working with firemonkey. I have a graphics image and I want to put a label on it. I used .filltext and tested wtih win32 and everything worked great. But when I ran on the android the text is just blocks of white on the background. Obviously I need to set the brush that the font is using but it is not clear to me how. ( I have correctly sized the bitmap via the android scaling and my drawpolygon is working as expected)

Segment of the code is below.

  Image1.bitmap.Canvas.BeginScene;
  Image1.bitmap.canvas.Clear(TAlphaColors.Black);
  Image1.bitmap.Canvas.Stroke.Thickness := 1;
  Image1.bitmap.Canvas.Stroke.Color := TAlphaColorRec.Yellow;
  Image1.bitmap.Canvas.DrawPolygon(FPoints2, 1);               // polygon
  // now testing text
    Canvas.Font.Size := 40;
   Image1.bitmap.mRect.Create(0, 0, (image1.width),( image1.height));
   Image1.bitmap.Canvas.filltext(mRect, 'Hello Text!', false, 1,
        [TFillTextFlag.RightToLeft],TTextAlign.Center, TTextAlign.Trailing);
   Image1.bitmap.Canvas.EndScene;

Upvotes: 2

Views: 1256

Answers (1)

rebible
rebible

Reputation: 99

Bottom line... If you are using filltext on the android the text color and the background around it are set as such:

Image1.bitmap.Canvas.Fill.Color := TAlphaColors.Yellow;//text color
Image1.Bitmap.Canvas.Fill.DefaultColor:=TAlphaColors.black; //background

This is different from the win32 platform where Image1.bitmap.Canvas.Stroke.Color sets the text color and it appears to have a transparent background around the text...

So for anyone else fighting this stuff here is working code on both android and win32. If someone has helpful comments I would appreciate them especially being able to set the background as transparent has to be documented. I could not find this info anywhere on the internet. thanks robert

procedure TMainFrm.draw_waveform;
var 
  mrect:trect;     //yellow waveform on black background with yellow text

begin
   waveformunit.init(image1);  // the two steps commented below done elsewhere
   // VERY important to do this for android otherwise it doesn't work!!!
   // Image.Bitmap.SetSize(Trunc(Image.Width * Image.Canvas.Scale),
                               //  Trunc(Image.Height * Image.Canvas.Scale));
   //   Image.Bitmap.canvas.Clear(TAlphaColors.black);
   to_polygon;

   Image1.bitmap.Canvas.BeginScene;
   Image1.Bitmap.canvas.Clear(TAlphaColors.black);
   Image1.bitmap.Canvas.Stroke.Thickness := 1;
   Image1.bitmap.Canvas.Stroke.Color := TAlphaColorRec.Yellow; //polygon line color
   Image1.bitmap.Canvas.DrawPolygon(FPoints2, 1);               // polygon
   //now test text
   Image1.Bitmap.canvas.Stroke.Kind := TBrushKind.bkSolid;
   Image1.Bitmap.canvas.Stroke.Thickness := 1;
   Image1.bitmap.Canvas.Fill.Color := TAlphaColors.Yellow;     //text color
   Image1.Bitmap.Canvas.Fill.DefaultColor:=TAlphaColors.black; // to match background
   Image1.Bitmap.Canvas.Font.Size:=36;
   Image1.Bitmap.Canvas.Font.Family:='Arial';
   Image1.Bitmap.Canvas.Font.Style:=[TFontStyle.fsbold];
   Image1.bitmap.canvas.Blending:=false;
   Image1.bitmap.Canvas.Font.Size := 40;
        mRect.Create(0, 0,round(image1.width),round( image1.height));
   Image1.bitmap.Canvas.filltext(mRect, 'Hello Text!', false, 1,
         [TFillTextFlag.RightToLeft],TTextAlign.Center, TTextAlign.Trailing);
   Image1.bitmap.Canvas.EndScene;

  //inc(numberdrawn);
end;

Upvotes: 2

Related Questions