Recent

Author Topic: Text to images Generator (Console app generating bitmaps with text)  (Read 37620 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5545
  • My goal: build my own game engine using Lazarus
Re: To Create images with text with GDI+ or what is used by linux
« Reply #30 on: November 03, 2023, 01:38:16 pm »
Lazarus := Pascal + IDE + libraries

Because Lazarus offers a lot of extra things than a simple tool for writing a small program, it needs to separate the project into multiple smaller files. A medium size project usually can contain more than thousands lines of code, it is not wise to store them in a single pas file.

Read more:
https://wiki.freepascal.org/File_extensions#Lazarus
« Last Edit: November 03, 2023, 01:49:27 pm by Handoko »

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #31 on: November 03, 2023, 01:44:11 pm »
I have saved the old project of mine, so it was suggested to add interface unit. I've just added this code:

Code: Pascal  [Select][+][-]
  1. writeln(folderName);
  2. if not DirectoryExists(folderName) then
  3.           mkdir(folderName);
  4.  

before the line:
Code: Pascal  [Select][+][-]
  1. fileName := IncludeTrailingPathDelimiter(folderName)

So the nice thing is that everything compiled without errors and generated the directories and images. All looks great. I think I have one mistake in the algo, because the directories with Helvetica should not be create because I don't have this commertial font (I am not sure if it is possible to download it for free).

I am gonna to check your posted programs too.

wp

  • Hero Member
  • *****
  • Posts: 13555
Re: To Create images with text with GDI+ or what is used by linux
« Reply #32 on: November 03, 2023, 02:57:27 pm »
Stupid question, but why the code is saved in .lpr file and not in .pas? Does .lpr mean that this is the main .pas file, the main app code? I dont know the meaning of LPR abbr.
There are no "stupid questions"...

This project was written with Lazarus, and Lazarus is organized in projects. The program file by default gets the extension .lpr ("lazarus project"), you could use .pas or .pp as well (or - I think - any other extension), but the different extension makes it easier to find the main project file in the file manager. There is also an xml file with the extension .lpi which lists which file is the project file and which other files belong to the project - it can be missing, but then it has to be recreated manually by the user which may requite quite some effort when there are lots of other units.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #33 on: November 03, 2023, 02:57:37 pm »
Where can I save My program command line options (paramStr) of current project?
E.g. I will call the program like this:
createimages.exe drive=a: color=black bgcolor=white
so for purposes of debuging this should be saved: "drive=a: color=black bgcolor=white"
« Last Edit: November 03, 2023, 03:17:46 pm by barracuda »

wp

  • Hero Member
  • *****
  • Posts: 13555
Re: To Create images with text with GDI+ or what is used by linux
« Reply #34 on: November 03, 2023, 03:24:33 pm »
Command line options are entered by the user, the are not stored in the application. But if, for testing, you need the same parameters again and again and you do not want to type them in every time, you can go to "Run" > "Run Parameters" and specify the parameters in the line "Command line parameters". This is a history list, btw, i.e. you can keep several sets of parameters here. But just to make sure: they are accessible only while working in the IDE. Outside the IDE the parameters must be typed in the command line.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #35 on: November 03, 2023, 04:26:16 pm »
How to convert String color to TGraphics.Color . This is because now I am processing the arguments from command line and this generatles color (of sure):

Code: Pascal  [Select][+][-]
  1.         if s.color <> '' then
  2.           bmp.Canvas.Font.Color := s.color;
  3.         else
  4.           bmp.Canvas.Font.Color := clBlack;
  5.  
  6.         if s.backgrounColor <> '' then
  7.           bmp.Canvas.Font.Color := s.backgroundColor;
  8.         else
  9.           bmp.Canvas.Font.Color := clBlack;
  10.  
  11.  

Handoko

  • Hero Member
  • *****
  • Posts: 5545
  • My goal: build my own game engine using Lazarus

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #37 on: November 03, 2023, 05:19:47 pm »
I am adding some improvements to code (not fully finnished):

Code: Pascal  [Select][+][-]
  1. type
  2.   TSetInfo = record
  3.     Drive: string;
  4.     Path: string;
  5.     Color: TColor;
  6.     BackgroundColor: TColor;
  7.     Italic: Boolean;
  8.     Bold: Boolean;
  9.   end;
  10.  
  11. procedure TMyApplication.ProccessArguments();
  12.  var i: integer;
  13.  strA: Array of String;
  14. begin
  15.   s.Italic := False;
  16.   s.Bold := False;
  17.   s.Color:=clWhite;
  18.   s.BackgroundColor:=clBlack;
  19.   for i := 1 to paramCount do
  20.     begin
  21.       // writeLn(i:2, '. argument: ', paramStr(i));
  22.       strA := paramStr(i).split(':');
  23.       Case strA[1] of
  24.        'drive':
  25.          s.drive:=paramStr(i);
  26.        'color':
  27.          s.color:=StringToColor(paramStr(i));
  28.        'bgcolor':
  29.          s.backgroundColor:=StringToColor(paramStr(i));
  30.        'bold': s.Bold := True;
  31.        'italic': s.Italic := True
  32.       end;
  33.     end;
  34. end;
  35.  

Drawing code:
Code: Pascal  [Select][+][-]
  1.   try
  2.     for i := 0 to High(fonts) do
  3.     begin
  4.       folderName := copy(folderNameShort,3,100);
  5.       if not FontIsAvailable(fonts[i]) then
  6.         continue;
  7.       folderName := fonts[i]+#32+folderName;
  8.       for j := 0 to High(texts) do
  9.       begin
  10.         bmp.Canvas.Font.Name := fonts[i];
  11.         bmp.Canvas.Font.Size := fontSize;
  12.         bmp.Canvas.Font.Color := s.color;
  13.         if s.Italic then
  14.           bmp.Canvas.Font.Style := [fsItalic];
  15.         if s.Bold then
  16.           bmp.Canvas.Font.Style := [fsBold];
  17.         R.TopLeft := Point(0, 0);
  18.         R.BottomRight := TPoint(bmp.Canvas.TextExtent(texts[j]));
  19.         bmp.SetSize(R.Right + 2*MARGIN, R.Bottom + 2*MARGIN);
  20.         writeln(folderName);
  21.         if not DirectoryExists(folderName) then
  22.           mkdir(folderName);
  23.         if s.path<>'' then
  24.            folderName := s.path + '/' + folderName
  25.         else if s.drive<>'' then
  26.             folderName := s.drive + '/' + folderName;
  27.         fileName := IncludeTrailingPathDelimiter(folderName) + fonts[i] + ' ' + Copy(texts[j], 1, Pos(' ', texts[j]) - 1);
  28.         if s.Italic then
  29.           fileName := fileName + '_i'
  30.         if s.Bold then
  31.           fileName := fileName + '_b'
  32.         fileName := fileName + '.bmp'
  33.  

Here I have a question. When I want to combine both styles bold + italic, is this like that?
[fsItalic|fsBold];

I will probably add yet another nested loop to create more images with specified styles (bold, italic or bold and italic and normal).

wp

  • Hero Member
  • *****
  • Posts: 13555
Re: To Create images with text with GDI+ or what is used by linux
« Reply #38 on: November 03, 2023, 05:26:31 pm »
When I want to combine both styles bold + italic, is this like that?
[fsItalic|fsBold]
No. Use a comma as separator between the elements of a set.

Handoko

  • Hero Member
  • *****
  • Posts: 5545
  • My goal: build my own game engine using Lazarus
Re: To Create images with text with GDI+ or what is used by linux
« Reply #39 on: November 03, 2023, 06:02:58 pm »
Just pick one you like:

Code: Pascal  [Select][+][-]
  1.   Font.Style := [fsBold, fsItalic, fsUnderline];
  2.   Font.Style := [fsBold] + [fsItalic] + [fsUnderline];

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: To Create images with text with GDI+ or what is used by linux
« Reply #40 on: November 03, 2023, 06:05:40 pm »
Here I have a question. When I want to combine both styles bold + italic, is this like that?
[fsItalic|fsBold];
No, this is a set see font style.

more information about set types (wp's and Handoko's first example) and set operators (Handoko's second example)
Today is tomorrow's yesterday.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #41 on: November 03, 2023, 06:15:38 pm »
I think I have what I need to use:
Code: [Select]
type TMyStyles = (TSNormal, TSBold, TSItalic, TSBoldItalic);
type
  TSetInfo = record
    Drive: string;
    Path: string;
    Color: TColor;
    BackgroundColor: TColor;
    Italic: Boolean;
    Bold: Boolean;
    Styles: Set of TMyStyles;
  end;
« Last Edit: November 03, 2023, 06:22:52 pm by barracuda »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: To Create images with text with GDI+ or what is used by linux
« Reply #42 on: November 03, 2023, 06:21:21 pm »
I think I need to create my own type for styles:
No, not really unless you insist or can't live without.

Quote
But this is not exactly what I need. Because this should support up to 4 styles so I can get combinations.
...
And then how to find out if the style is set.

I've already posted the corresponding links:

Here I have a question. When I want to combine both styles bold + italic, is this like that?
[fsItalic|fsBold];
No, this is a set see font style.

more information about set types (wp's and Handoko's first example) and set operators (Handoko's second example)

So read about set types and set operators and try to figure out how your code should be adopted. That is imho more helpful than us adjusting your code.
Today is tomorrow's yesterday.

wp

  • Hero Member
  • *****
  • Posts: 13555
Re: To Create images with text with GDI+ or what is used by linux
« Reply #43 on: November 03, 2023, 06:29:37 pm »
Learn about the pascal type "set", e.g. https://wiki.freepascal.org/Set

A set is a combination of several elements. In case of the font styles, the elements are fsBold, fsItalic, fsUnderline and fsStrikeout; they are defined in an enumeration TFontStyle (singular!) = (fsBold, fsItalic, fsUnderline, fsStrikeout). Conversely to this there is the set type TFontStyles (plural) = set of TFontStyle, it contains the some or all TFontstyle elements, or even none ("empty set"). The elements in the set are enclosed by square parenthesis: [fsBold, fsItalic] means that the font is bold and italic, [fsBold] means that it is bold only, [] is the empty set, and this means that the font is neither bold, nor italic, nor underlined, not striked-out. To check whether an element is included in the set we have the "in" operator: Assume that "FontStyle" is a set variable of the TFontStyles type. Then the expression "(fsBold in FontStyle)" is true when the font is bold, or "not (fsBold in FontStyle)" is true when the font is not bold. Checking for multiple elements can be done by several "in" calls: "(fsBold in FontStyle) or (fsItalic in FontStyle)" = font is either bold or italic, or by a "multiplication" expression: "[fsBold, fsItalic] * FontStyle <> []" - the intersection between the set [fsBold, fsItalic] and FontStyle is not empty, i.e. the FontStyle is either bold or italic. I hope you remember some elemental maths...
« Last Edit: November 03, 2023, 06:37:52 pm by wp »

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #44 on: November 03, 2023, 06:44:49 pm »
I don't remember math, I only remember the logic of binary things like
00
01
10
01
I think I need to start chatGPT to ask things. I already have some code but sure there are mistakes... I have many questions.

 

TinyPortal © 2005-2018