Recent

Author Topic: Change BGRABitmap Font in NOGui App  (Read 4822 times)

Sanem

  • Full Member
  • ***
  • Posts: 173
Change BGRABitmap Font in NOGui App
« on: May 15, 2016, 03:41:44 pm »
Hi
I have a NoGui app and i am trying to change Font of bgrabitmap from default which is arial to some thing else like "Times New Roman" or "Segoe_UI", as you can see in my code, i am trying to add the font file (.ttf file) to font collection, im not sure what im missing that the default font dont change!
you can see my font files, which are in project directory in screenshot,
i am using Lazarus 1.6 in windows 10.

any help much appreciated prior

Code: Pascal  [Select][+][-]
  1. program Test1NoGui;
  2.  
  3. uses
  4.   Classes,
  5.   BGRABitmap,
  6.   BGRABitmapTypes,
  7.   LazFreeTypeFontCollection,
  8.   SysUtils;
  9.  
  10. type
  11.   Test1NoGuiClass = class
  12.   protected
  13.   public
  14.     procedure DrawPic;
  15.   end;
  16.  
  17.   procedure Test1NoGuiClass.DrawPic;
  18.   var
  19.     bmp: TBGRABitmap;
  20.     FFontCollection: TFreeTypeFontCollection;
  21.   begin
  22.     FFontCollection := TFreeTypeFontCollection.Create;
  23.     FFontCollection.AddFile('times.ttf');
  24.     //FFontCollection.AddFile('arial.ttf');
  25.     //FFontCollection.AddFile('Segoe_UI.ttf');
  26.     SetDefaultFreeTypeFontCollection(FFontCollection);
  27.     TBGRABitmap.AddFreeTypeFontFolder(GetCurrentDir);
  28.     bmp.FontName := 'Times New Roman';
  29.     //bmp.FontName := 'arial';
  30.     //bmp.FontName := 'Segoe_UI';
  31.     bmp := TBGRABitmap.Create(400, 200);
  32.     bmp.FillEllipseAntialias(200, 100, 190, 90, BGRA(26, 22, 169));
  33.     bmp.FontHeight := bmp.Height div 10;
  34.     with bmp.FontPixelMetric do
  35.       bmp.TextOut(bmp.Width / 2, bmp.Height / 2 - (CapLine + Baseline) / 2,
  36.         'Too little too late!', BGRA(206, 205, 232), taCenter);
  37.     bmp.SaveToFile('Pics/Times New Roman.png');
  38.     bmp.Free;
  39.   end;
  40.  
  41. var
  42.   App: Test1NoGuiClass;
  43. begin
  44.   App.DrawPic;
  45. end.
  46.  

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Change BGRABitmap Font in NOGui App
« Reply #1 on: May 15, 2016, 06:19:25 pm »
I would say that it is because the line "bmp.FontName := " is before the line "bmp := TBGRABitmap.Create"
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: Change BGRABitmap Font in NOGui App
« Reply #2 on: May 16, 2016, 12:19:18 pm »
Yes you are right, my fault!
i fixed it but still i cant change the font of bgrabitmap, but this time i got an error, which i will attach in screenshot, i am not sure how to set font in no gui app, is it enough to put the font file(Segoe_ui.ttf) in project directory?? 


Code: Pascal  [Select][+][-]
  1. program Test1NoGui;
  2.  
  3. uses
  4.   Classes,
  5.   BGRABitmap,
  6.   BGRABitmapTypes,
  7.   LazFreeTypeFontCollection,
  8.   SysUtils;
  9.  
  10. type
  11.   Test1NoGuiClass = class
  12.   protected
  13.   public
  14.     procedure DrawPic;
  15.   end;
  16.  
  17.   procedure Test1NoGuiClass.DrawPic;
  18.   var
  19.     bmp: TBGRABitmap;
  20.     FFontCollection: TFreeTypeFontCollection;
  21.   begin
  22.     FFontCollection := TFreeTypeFontCollection.Create;
  23.     FFontCollection.AddFile('Segoe_UI.ttf');
  24.     SetDefaultFreeTypeFontCollection(FFontCollection);
  25.     TBGRABitmap.AddFreeTypeFontFolder(GetCurrentDir);
  26.     bmp := TBGRABitmap.Create(400, 200);
  27.     bmp.FontName := 'Segoe_UI';
  28.     bmp.FillEllipseAntialias(200, 100, 190, 90, BGRA(26, 22, 169));
  29.     bmp.FontHeight := bmp.Height div 10;
  30.     with bmp.FontPixelMetric do
  31.       bmp.TextOut(bmp.Width / 2, bmp.Height / 2 - (CapLine + Baseline) / 2,
  32.         'Too little too late!', BGRA(206, 205, 232), taCenter);
  33.     bmp.SaveToFile('Pics/Segoe_UI.png');
  34.     bmp.Free;
  35.   end;
  36.  
  37. var
  38.   App: Test1NoGuiClass;
  39. begin
  40.   App.DrawPic;
  41. end.
  42.  

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Change BGRABitmap Font in NOGui App
« Reply #3 on: May 16, 2016, 07:23:55 pm »
Probably the name of the font is "Segoe UI" (it is often different from the file name)
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: Change BGRABitmap Font in NOGui App
« Reply #4 on: May 17, 2016, 09:35:37 am »
It worked
Thank you so much

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Change BGRABitmap Font in NOGui App
« Reply #5 on: May 17, 2016, 10:31:08 pm »
You're very welcome  :)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018