Recent

Author Topic: Persian Text in BGRABitmap No Gui  (Read 5358 times)

simin_sh

  • Full Member
  • ***
  • Posts: 143
Persian Text in BGRABitmap No Gui
« on: May 23, 2016, 02:28:06 pm »
Hi, im trying to write a persian word in BGRABitmap No Gui, but as you see in screenshots, the font is not like what it should be! i mean the word is completely Vice versa and its characters are separated which shouldnt be in persian!
any idea about Persian characters in BGRABitmap? is it supported??
I use BTitr font which downloaded from this address:
http://iraniangraphic.com/product/745/مجموعه-فونت-های-قلم-فارسی-B-Titr-و-B-Yagut-و-B-Zar-و-.html

any help 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('BTitrBd.ttf');
  24.     SetDefaultFreeTypeFontCollection(FFontCollection);
  25.     TBGRABitmap.AddFreeTypeFontFolder(GetCurrentDir);
  26.     bmp := TBGRABitmap.Create(400, 200);
  27.     bmp.FontName := 'B Titr';
  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.         'سلام', BGRA(206, 205, 232), taRightJustify);
  33.     bmp.SaveToFile('Pics/PersianText.png');
  34.     bmp.Free;
  35.   end;
  36.  
  37. var
  38.   App: Test1NoGuiClass;
  39. begin
  40.   App.DrawPic;
  41. end.
  42.  
« Last Edit: May 23, 2016, 02:51:10 pm by simin_sh »
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

Windsurfer

  • Sr. Member
  • ****
  • Posts: 364
    • Windsurfer
Re: Persian Text in BGRABitmap No Gui
« Reply #1 on: May 23, 2016, 04:03:16 pm »
I am a long way from my PC, but look at UTF8 commands and bidimode. I assume you are using 1.4 or 1.6.
« Last Edit: May 23, 2016, 04:09:06 pm by Windsurfer »

circular

  • Hero Member
  • *****
  • Posts: 3508
    • Personal webpage
Re: Persian Text in BGRABitmap No Gui
« Reply #2 on: May 23, 2016, 08:18:13 pm »
Hello!

The FreeType font renderer in Lazarus does not seem to handle right to left.

Thanks for posting this example. I will have a look when I have some time to see if it is possible to add right-to-left writing.

Regards

Conscience is the debugger of the mind

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #3 on: May 24, 2016, 08:45:54 am »
You are welcome Circular :)
And Thank you for Paying attention to my questions.
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #4 on: May 24, 2016, 08:49:59 am »
Windsurfer, its a No Gui program, im not sure check what for Bidi mode and UTF8 which you said maybe fixes the problem.

Thank you for your reply
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #5 on: June 04, 2016, 04:58:38 pm »
I tried to use Fpimage instead of Bgrabitmap NoGui for displaying Persian characters, to use just Fcl as dependencies, as you can see in this topic:
http://forum.lazarus.freepascal.org/index.php/topic,32747.0.html

then when i did all the needs in this topic discussed, i got Persian character displaying correct in Fpimage, as you can see the result in my test project, which uploaded in this address:
https://www.dropbox.com/s/3e3ujln54q4d1tw/MinibidiTest2%20%28Ok%29.zip?dl=0

but when i use Image of this Fpimage as a TFPCustomImage for create the Bgrabotmap that i wanna use in my project, i got this error which i explained in this post:
http://forum.lazarus.freepascal.org/index.php/topic,32747.msg212224.html#msg212224

and my test project for this(use image of Fpimage as a TFPCustomImage for create the Bgrabotmap)is uploaded in this address:
https://www.dropbox.com/s/um50yy4t0b8vhim/TestFclBgraCanvas.zip?dl=0

any help appreciated prior
« Last Edit: June 04, 2016, 05:00:09 pm by simin_sh »
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

circular

  • Hero Member
  • *****
  • Posts: 3508
    • Personal webpage
Re: Persian Text in BGRABitmap No Gui
« Reply #6 on: June 04, 2016, 07:00:52 pm »
Thanks simin_sh, in your example I found the gem MiniBidi. That's what's missing from LazUtils.

As a temporary fix, so, to display a text that contains right-to-left characters, you can add MiniBidi in the unit clauses and use the following function:
Code: Pascal  [Select][+][-]
  1. function BidiStringUTF8(AText: string): string;
  2. var wText: widestring;
  3. begin
  4.   wText := UTF8Decode(AText);
  5.   BidiString(wText);
  6.   result := UTF8Encode(wText);
  7. end;

So the example becomes:
Code: Pascal  [Select][+][-]
  1. program Test1NoGui;
  2.  
  3. uses
  4.   Classes,
  5.   BGRABitmap,
  6.   BGRABitmapTypes,
  7.   LazFreeTypeFontCollection,
  8.   SysUtils, MiniBidi;
  9.  
  10. function BidiStringUTF8(AText: string): string;
  11. var wText: widestring;
  12. begin
  13.   wText := UTF8Decode(AText);
  14.   BidiString(wText);
  15.   result := UTF8Encode(wText);
  16. end;
  17.  
  18. type
  19.   Test1NoGuiClass = class
  20.   protected
  21.   public
  22.     procedure DrawPic;
  23.   end;
  24.  
  25.   procedure Test1NoGuiClass.DrawPic;
  26.   var
  27.     bmp: TBGRABitmap;
  28.     FFontCollection: TFreeTypeFontCollection;
  29.   begin
  30.     FFontCollection := TFreeTypeFontCollection.Create;
  31.     FFontCollection.AddFile('BTitrBd.ttf');
  32.     SetDefaultFreeTypeFontCollection(FFontCollection);
  33.     TBGRABitmap.AddFreeTypeFontFolder(GetCurrentDir);
  34.     bmp := TBGRABitmap.Create(400, 200);
  35.     bmp.FontName := 'B Titr';
  36.     bmp.FillEllipseAntialias(200, 100, 190, 90, BGRA(26, 22, 169));
  37.     bmp.FontHeight := bmp.Height div 10;
  38.     with bmp.FontPixelMetric do
  39.       bmp.TextOut(bmp.Width / 2, bmp.Height / 2 - (CapLine + Baseline) / 2,
  40.        BidiStringUTF8('سلام'), BGRA(206, 205, 232), taRightJustify);
  41.     bmp.SaveToFile('Pics/PersianText.png');
  42.     bmp.Free;
  43.   end;
  44.  
  45. var
  46.   App: Test1NoGuiClass;
  47. begin
  48.   App.DrawPic;
  49. end.

That's temporary in the sense that I have an idea on how to patch LazUtils so that it would handle Bidi on its own.
Conscience is the debugger of the mind

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #7 on: June 05, 2016, 04:35:51 pm »
Hi circular
Thank you for your replay, and you are welcome.
I tested the code you mentioned, yes it can now display Persian characters in Bgrabitmap NoGui, but now i have a problem with TextRect in Bgrabitmap NoGui,(which is not related to font problem at all!) as you can see in screenshot attached, it seems that lazarus can not specify the method i call(there is 4 methods with the same name TextRect and i wanna cal one with certain params), i need this method because i wanted to display a text with multiple sentences.
my test project is uploaded here:
https://www.dropbox.com/s/jky9xyv5ou1imz4/TestBGRAPersianTextRect.zip?dl=0

any help appreciated prior
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

circular

  • Hero Member
  • *****
  • Posts: 3508
    • Personal webpage
Re: Persian Text in BGRABitmap No Gui
« Reply #8 on: June 06, 2016, 01:25:37 am »
I suggest to add BGRAGraphics to the uses clause.
Conscience is the debugger of the mind

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #9 on: June 06, 2016, 09:14:42 am »
Thank you, it solved that problem, but now there is another problem with texts in Bgrabitmap,
as you can see in screenshots (First is what i want, second is the result of my code in Bgrabitmap as a .Png file) i am trying to display a paragraph of Persian sentences, and when i write it in the code it displays correct (except the point should stay in the end of my sentence  because the language is right to left but it stays in begin of it which means end of string!) but the result in the Bgrabitmap is Messed up(it seems that it started the sentences from end!) in Microsoft office word application, it is all about alignment to be set as "justified" and direction to be set as "Right-To-Left" and the Persian sentences will be Stacking in the correct way, and by the way, in BgraBitmap with LCL (Gui) there is not such problem!

here is my code:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   Classes,
  5.   BGRABitmap,
  6.   BGRABitmapTypes,
  7.   LazFreeTypeFontCollection,
  8.   SysUtils,
  9.   MiniBidi,
  10.   BGRAGraphics;
  11.  
  12.   function BidiStringUTF8(AText: string): string;
  13.   var
  14.     wText: WideString;
  15.   begin
  16.     wText := UTF8Decode(AText);
  17.     BidiString(wText);
  18.     Result := UTF8Encode(wText);
  19.   end;
  20.  
  21. type
  22.   Test1NoGuiClass = class
  23.   protected
  24.   public
  25.     procedure DrawPic;
  26.   end;
  27.  
  28.   procedure Test1NoGuiClass.DrawPic;
  29.   var
  30.     bmp: TBGRABitmap;
  31.     FFontCollection: TFreeTypeFontCollection;
  32.     s: string;
  33.   begin
  34.     FFontCollection := TFreeTypeFontCollection.Create;
  35.     FFontCollection.AddFile('Fonts/segoeui.ttf');
  36.     SetDefaultFreeTypeFontCollection(FFontCollection);
  37.     bmp := TBGRABitmap.Create(600, 400, BGRA(255, 255, 255));
  38.     bmp.FontName := 'Segoe UI';
  39.     bmp.FontHeight := bmp.Height div 30;
  40.     s:= 'شما با داشتن این نرم افزار دیگر نیازی ندارید که به سایت های ترجمه ی آنلاین مراجعه کنید زیرا به طور مستقیم از کامپیوتر خود می توانید متون مورد نظر خود را ترجمه کنید. همچنین این نرم افزار قادر است تا صفحات وب سایت هایی را که در فایل ذخیره شده اند را اجرا کند و سپس ترجمه ی کلمات و متنهای موجود در آن را در اختیارتان قرار دهد. ';
  41.     bmp.TextRect(rect(0, 0, 600, 400), BidiStringUTF8(s), taRightJustify,
  42.       tlTop, BGRA(0, 0, 0));
  43.     bmp.SaveToFile('Pics/1.png');
  44.     bmp.Free;
  45.   end;
  46.  
  47. var
  48.   App: Test1NoGuiClass;
  49. begin
  50.   App.DrawPic;
  51. end.
  52.  


any help appreciated prior
« Last Edit: June 06, 2016, 10:29:37 am by simin_sh »
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

circular

  • Hero Member
  • *****
  • Posts: 3508
    • Personal webpage
Re: Persian Text in BGRABitmap No Gui
« Reply #10 on: June 27, 2016, 11:36:05 am »
Oh I see the order of the lines is reversed. The thing is this cannot be solved by the method we are using now, because when reversing the string using BidiString, we don't know when the line breaks are. So the string is reversed as if it was on one line.
Conscience is the debugger of the mind

simin_sh

  • Full Member
  • ***
  • Posts: 143
Re: Persian Text in BGRABitmap No Gui
« Reply #11 on: June 28, 2016, 09:13:43 am »
yes, that is the problem!
OS Windows 10 - Lazarus 1.7 - FPC 3.1.1

 

TinyPortal © 2005-2018