Recent

Author Topic: Two issues related to fcl-image  (Read 304 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 374
Two issues related to fcl-image
« on: June 03, 2023, 10:30:46 am »
1/2 In order to show the bug I'm referring at point 2/2, I've tried to write the simplest program.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses sysutils, fpimage, fpimgCanv, ftfont, fpcanvas;
  4.  
  5. procedure ExportBitmaps(FontParameter:TFreeTypeFont; ExportDirectory:string);
  6. var
  7.   ByteIndex:byte;
  8.   LocalImage:TFPMemoryImage;
  9.   LocalCanvas:TFPImageCanvas;
  10. begin
  11.   ExportDirectory:=IncludeTrailingPathDelimiter(ExportDirectory);
  12.   LocalImage:=TFPMemoryImage.Create(0,0);
  13.  
  14.   LocalCanvas:=TFPImageCanvas.create(LocalImage);
  15.   LocalCanvas.Brush.FPColor:=colwhite;
  16.   LocalCanvas.Brush.Style:=bssolid;
  17.   LocalCanvas.Font:=FontParameter;
  18.  
  19.   for ByteIndex:=0 to 255 do
  20.   begin//Write the character
  21.     LocalImage.SetSize(LocalCanvas.TextWidth(chr(ByteIndex)),LocalCanvas.TextHeight(chr(ByteIndex)));
  22.     LocalCanvas.Rectangle(-1,-1,LocalImage.Width,LocalImage.Height);
  23.     LocalCanvas.TextOut(0,LocalImage.Height,chr(ByteIndex));
  24.     LocalImage.SaveToFile(ExportDirectory+inttostr(ByteIndex)+'.bmp');//IT DOESN'T SAVE THE FILE AND RETURNS FALSE!!!
  25.   end;
  26.   LocalCanvas.Free;LocalImage.Free;
  27. end;
  28.  
  29. var
  30.   AFont: TFreeTypeFont;
  31.   AFontAbsolutePath:string = '/tmp/PublicPixel.ttf';
  32.   //AFontAbsolutePath:string = '/tmp/MasaoFree.otf';
  33.  
  34. begin
  35.   ftfont.InitEngine;
  36.   AFont:=TFreeTypeFont.Create;
  37.   AFont.Name:=AFontAbsolutePath;
  38.   AFont.FPColor:=colblack;
  39.   AFont.Size:=60;//Changing the value to 12 won't make a difference
  40.   //AFont.AntiAliased:=FALSE;//Changing the default AntiAliased value to false won't make a difference
  41.   ExportBitmaps(AFont,'/tmp/a/');
  42.   AFont.Free;
  43.   ftfont.DoneEngine;
  44. end.

The above program didn't saved the files using "function SaveToFile (const filename:String): Boolean;", so I had to modify it to use "procedure SaveToFile (const filename:String; Handler:TFPCustomImageWriter);". Both routines are in TFPCustomImage, so it looks strange to me. A working code is:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses sysutils, fpimage, fpimgCanv, ftfont, fpcanvas, FPWriteBMP;
  4.  
  5. procedure ExportBitmaps(FontParameter:TFreeTypeFont; ExportDirectory:string);
  6. var
  7.   ByteIndex:byte;
  8.   LocalImage:TFPMemoryImage;
  9.   LocalCanvas:TFPImageCanvas;
  10.   BMPWriter: TFPWriterBMP;
  11. begin
  12.   ExportDirectory:=IncludeTrailingPathDelimiter(ExportDirectory);
  13.   LocalImage:=TFPMemoryImage.Create(0,0);
  14.   BMPWriter:=TFPWriterBMP.Create;
  15.  
  16.   LocalCanvas:=TFPImageCanvas.create(LocalImage);
  17.   LocalCanvas.Brush.FPColor:=colwhite;
  18.   LocalCanvas.Brush.Style:=bssolid;
  19.   LocalCanvas.Font:=FontParameter;
  20.  
  21.   for ByteIndex:=0 to 255 do
  22.   begin//Write the character
  23.     LocalImage.SetSize(LocalCanvas.TextWidth(chr(ByteIndex)),LocalCanvas.TextHeight(chr(ByteIndex)));
  24.     LocalCanvas.Rectangle(-1,-1,LocalImage.Width,LocalImage.Height);
  25.     LocalCanvas.TextOut(0,LocalImage.Height,chr(ByteIndex));
  26.     LocalImage.SaveToFile(ExportDirectory+inttostr(ByteIndex)+'.bmp',BMPWriter);
  27.   end;
  28.  
  29.   BMPWriter.Free;
  30.   LocalCanvas.Free;
  31.   LocalImage.Free;
  32. end;
  33.  
  34. var
  35.   AFont: TFreeTypeFont;
  36.   AFontAbsolutePath:string = '/tmp/PublicPixel.ttf';
  37.   //AFontAbsolutePath:string = '/tmp/MasaoFree.otf';
  38.  
  39. begin
  40.   ftfont.InitEngine;
  41.   AFont:=TFreeTypeFont.Create;
  42.   AFont.Name:=AFontAbsolutePath;
  43.   AFont.FPColor:=colblack;
  44.   AFont.Size:=60;//Changing the value to 12 won't make a difference
  45.   //AFont.AntiAliased:=FALSE;//Changing the default AntiAliased value to false won't make a difference
  46.   ExportBitmaps(AFont,'/tmp/a/');
  47.   AFont.Free;
  48.   ftfont.DoneEngine;
  49. end.

The above code saves the files but it looks strange to me why the first program code didn't worked. There is no error at compile time and I would have expected the TFPWriterBMP to be created and used automatically inside the SaveToFile function.

2/2 The following program shows a font issue. Read the comment at the end.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses sysutils, fpimage, fpimgCanv, ftfont, fpcanvas, FPWriteBMP;
  4.  
  5. procedure ExportBitmaps(FontParameter:TFreeTypeFont; ExportDirectory:string);
  6. var
  7.   ByteIndex:byte;
  8.   LocalImage:TFPMemoryImage;
  9.   LocalCanvas:TFPImageCanvas;
  10.   BMPWriter: TFPWriterBMP;
  11. begin
  12.   ExportDirectory:=IncludeTrailingPathDelimiter(ExportDirectory);
  13.   LocalImage:=TFPMemoryImage.Create(0,0);
  14.   BMPWriter:=TFPWriterBMP.Create;
  15.  
  16.   LocalCanvas:=TFPImageCanvas.create(LocalImage);
  17.   LocalCanvas.Brush.FPColor:=colwhite;
  18.   LocalCanvas.Brush.Style:=bssolid;
  19.   LocalCanvas.Font:=FontParameter;
  20.  
  21.   for ByteIndex:=0 to 255 do
  22.   begin//Write the character
  23.     LocalImage.SetSize(LocalCanvas.TextWidth(chr(ByteIndex)),LocalCanvas.TextHeight(chr(ByteIndex)));
  24.     LocalCanvas.Rectangle(-1,-1,LocalImage.Width,LocalImage.Height);
  25.  
  26.     LocalCanvas.TextOut(0,LocalImage.Height,chr(ByteIndex));
  27.     //LocalCanvas.TextOut(0,0,chr(ByteIndex));
  28.  
  29.     LocalImage.SaveToFile(ExportDirectory+inttostr(ByteIndex)+'.bmp',BMPWriter);
  30.   end;
  31.  
  32.   BMPWriter.Free;
  33.   LocalCanvas.Free;
  34.   LocalImage.Free;
  35. end;
  36.  
  37. var
  38.   AFont: TFreeTypeFont;
  39.   AFontAbsolutePath:string = '/tmp/PublicPixel.ttf';//Make sure you use a valid absolute path
  40.   //AFontAbsolutePath:string = '/tmp/MasaoFree.otf';//Make sure you use a valid absolute path
  41.  
  42. begin
  43.   ftfont.InitEngine;
  44.   AFont:=TFreeTypeFont.Create;
  45.   AFont.Name:=AFontAbsolutePath;
  46.   AFont.FPColor:=colblack;
  47.   AFont.Size:=60;//Changing the value to 12 won't make a significant difference
  48.   //AFont.AntiAliased:=FALSE;//Changing the default AntiAliased value to false won't make a difference
  49.   ExportBitmaps(AFont,'/tmp/a/');//Make sure you use an existing temporary directory
  50.   AFont.Free;
  51.   ftfont.DoneEngine;
  52.   {
  53.   Using PublicPixel ttf file and "LocalCanvas.TextOut(0,LocalImage.Height,chr(ByteIndex));", notice that:
  54.   42.bmp doesn't show the asterisk;
  55.   45.bmp doesn't show the minus sign;
  56.   61.bmp shows a minus instead of equal sign.
  57.   No matter the TFreeTypeFont.Size value, it appears like an amount of percent of the glyph height is trimmed at top.
  58.   Now, if we change the font from PublicPixel to MasaoFree and at the same time we modify
  59.   LocalCanvas.TextOut(0,LocalImage.Height,chr(ByteIndex)); with
  60.   LocalCanvas.TextOut(0,0,chr(ByteIndex)); in procedure ExportBitmaps, we notice that there is a delay, like a delta value of glyph bytes, for both x and y axes. Look at the 40.bmp"(" and 41.bmp")".
  61.   Using the MasaoFree font it appears like an amount of percent of the glyph is trimmed at bottom or left.
  62.   Overall, it might be that the bitmap size is computed ok but when it comes to filling it, the origin of the coordinate system of the glyph bytes is computed wrong.
  63.   }
  64. end.
I attach the font files to the message.

wp

  • Hero Member
  • *****
  • Posts: 11221
Re: Two issues related to fcl-image
« Reply #1 on: June 03, 2023, 11:07:07 am »
The above program didn't saved the files using "function SaveToFile (const filename:String): Boolean;", so I had to modify it to use "procedure SaveToFile (const filename:String; Handler:TFPCustomImageWriter);". Both routines are in TFPCustomImage, so it looks strange to me.
No, that's fine because you did not specify any image writer unit in the uses clause. Doing so registers the writer so that fcl-image can determine from the file extension the image format to be written. From fpwritebmp (and any other reader/writer unit of fcl-image):
Code: Pascal  [Select][+][-]
  1. initialization
  2.   ImageHandlers.RegisterImageWriter ('BMP Format', 'bmp', TFPWriterBMP);

 

TinyPortal © 2005-2018