wxpdfdoc library how prints unicode?

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
dkaip
Experienced Solver
Experienced Solver
Posts: 66
Joined: Wed Jan 20, 2010 1:15 pm

wxpdfdoc library how prints unicode?

Postby dkaip » Tue Nov 07, 2017 4:04 pm

Hello. In Code bellow, adding unicode chars to pdf page with wxpdfdoc lib, the unicode chars appears as ??????
But in Libre Office appears ok. What i must do?

Code: Select all

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(wxS("Times"),wxS("B"),16);
  pdf.Cell(40,10,wxS("Hello World!"));
  pdf.Cell(40,10,wxS("Στὸν ἱστοχῶρο αὐτὸν ..."));
Attachments
Επιλογή_062.png
Επιλογή_062.png (25.98 KiB) Viewed 188 times
Επιλογή_061.png
Επιλογή_061.png (2.52 KiB) Viewed 188 times

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2019
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxpdfdoc library how prints unicode?

Postby ONEEYEMAN » Tue Nov 07, 2017 4:08 pm

Hi,
Try to use wxT() macro, not the wxS() one.
Also, check the wxPdfDoc samples.

Thank you.

utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxpdfdoc library how prints unicode?

Postby utelle » Tue Nov 07, 2017 5:18 pm

dkaip wrote:Hello. In Code bellow, adding unicode chars to pdf page with wxpdfdoc lib, the unicode chars appears as ??????
But in Libre Office appears ok. What i must do?

When you have Unicode resp Non-ASCII characters in the source code of your application, all depends on the encoding of your source code files. If you take care of that your source files are all encoded in UTF-8 encoding, then the code below would work ... if the selected font would support the requested characters. The problem here is that the PDF core font Times does not contain Greek characters.

Code: Select all

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(wxS("Times"),wxS("B"),16);
  pdf.Cell(40,10,wxS("Hello World!"));
  pdf.Cell(40,10,wxString::FromUTF8("Στὸν ἱστοχῶρο αὐτὸν ...");

To get the intended result you will have to set a font for wxPdfDocument that contains Greek characters. For example, you could use the DejaVu fonts.

Regards,

Ulrich

dkaip
Experienced Solver
Experienced Solver
Posts: 66
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxpdfdoc library how prints unicode?

Postby dkaip » Tue Nov 07, 2017 6:11 pm

On my computer I do not have the font Helvetica, but the program prints "Hello world" normally, and no print Greek text.
In Times that exist does not print "Hello world".
I installed Dejavu font and it does not print either the "Hello world" or the Greek text.
I do not understand.
Thanks for reply.
Jim

User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxpdfdoc library how prints unicode?

Postby xaviou » Tue Nov 07, 2017 8:57 pm

Hi

You can also try to register your font before use it :

Code: Select all

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  wxFont fontTimes(16, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T("Times"), wxFONTENCODING_DEFAULT);
  wxPdfFontManager* fntMngr = wxPdfFontManager::GetFontManager();
  fntMngr->RegisterFont(fontTimes);
  pdf.SetFont(wxS("Times"),wxS("B"),16);
  pdf.Cell(40,10,wxS("Hello World!"));
  pdf.Cell(40,10,wxS("Στὸν ἱστοχῶρο αὐτὸν ..."));


But as utelle said, if the font doesn't contain the characters you want to display, they can't be displayed.

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff

utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxpdfdoc library how prints unicode?

Postby utelle » Tue Nov 07, 2017 11:46 pm

dkaip wrote:On my computer I do not have the font Helvetica, but the program prints "Hello world" normally, and no print Greek text.

The PDF format supports a number of so-called core fonts - Helvetica, Times Roman, Courier, and Symbol. These fonts can always be used to create PDF documents with wxPdfDocument without explicitly registering them. The font Helvetica is identical to Arial. Therefore Arial is an alias for Helvetica in wxPdfDocument. However, the PDF core fonts do not contain Greek characters - except for the Symbol font.

dkaip wrote:In Times that exist does not print "Hello world".

Sorry, no idea why that is so for your application.

dkaip wrote:I installed Dejavu font and it does not print either the "Hello world" or the Greek text.

Installing the DejaVu fonts on your system is not enough. You have to register the font(s) for wxPdfDocument. You should use method wxPdfFontManager::GetFontManager()->RegisterFont to do this (see sample tutorial7 for an example).

Regards,

Ulrich

dkaip
Experienced Solver
Experienced Solver
Posts: 66
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxpdfdoc library how prints unicode?

Postby dkaip » Wed Nov 08, 2017 9:33 pm

I will try it. Now i understand the mechanism.
If i want to use for example Tahoma, that has all Unicode chars i want, how i choose font?
Great, i just did it.
Tahoma have family ... Tahoma, when open file tahomabd.ttf with Fontforge.
Trying wxFont fontTahoma(16, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T("Tahoma"), wxFONTENCODING_DEFAULT); i did nothing.
So with code bellow runs fine.

Code: Select all

  wxFont fontTahoma("/home/a/.fonts/tahomabd.ttf");
  wxPdfFontManager* fntTahoma = wxPdfFontManager::GetFontManager();
  fntTahoma->RegisterFont(fontTahoma,_T("Tahoma"));
  pdf.SetFont(wxS("Tahoma"),wxS(""),16);
  pdf.Cell(40,10,wxS("Hello World!"));
  pdf.Cell(40,10,wxS("Στὸν ἱστοχῶρο αὐτὸν ..."));


But if i want not to link to a certain font file, how i can choose it from OS with current settings automaticaly?
How i can know the FONTFAMILY and FONTSTYLE etc?
Suppose i must use wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL and wxFONTWEIGHT_NORMAL?
The solution is ...
wxFont fontTahoma;
wxPdfFontManager* fntTahoma = wxPdfFontManager::GetFontManager();
fntTahoma->RegisterFont(fontTahoma,_T("Tahoma"));

Also if i try...

Code: Select all

  wxFont fontTahoma(16, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T("Tahoma132432"), wxFONTENCODING_DEFAULT);
  wxPdfFontManager* fntTahoma = wxPdfFontManager::GetFontManager();
  fntTahoma->RegisterFont(fontTahoma,_T("Tahoma"));
  pdf.SetFont(wxS("Tahoma"),wxS(""),16);
  pdf.Cell(40,10,wxS("Hello World!"));
  pdf.Cell(40,10,wxS("Στὸν ἱστοχῶρο αὐτὸν ..."));

fntTahoma->RegisterFont(fontTahoma,_T("Tahoma")); corrects font init and text put just fine

Thanks very mush.

Jim

utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxpdfdoc library how prints unicode?

Postby utelle » Thu Nov 09, 2017 12:25 pm

dkaip wrote:If i want to use for example Tahoma, that has all Unicode chars i want, how i choose font?
Great, i just did it.
Tahoma have family ... Tahoma, when open file tahomabd.ttf with Fontforge.
Trying wxFont fontTahoma(16, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T("Tahoma"), wxFONTENCODING_DEFAULT); i did nothing.

From your description it's not totally clear unfortunately, what your problem is.

wxPdfDocument offers several ways to use fonts:

  1. PDF core fonts - no explicit font registration required, but only Latin script support plus special Symbol font
  2. Explicitly registered font by using TrueType or OpenType font files - like tahoma.ttf (regular font face) or tahomabd.ttf (bold font face)
  3. Explicitly registered font by using wxFont instances - this allows for example to use the wxWidgets font selection dialog in your application to let the user select a font of his choice
The question you have to answer is whether you want your application to use a certain fixed font, or whether you want to allow the user to select a font of his choice.

dkaip wrote:But if i want not to link to a certain font file, how i can choose it from OS with current settings automaticaly?
How i can know the FONTFAMILY and FONTSTYLE etc?
Suppose i must use wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL and wxFONTWEIGHT_NORMAL?

Well, if you know the font family and style, then create a wxFont instance using these font attributes and register it for use with wxPdfDocument by calling the RegisterFont method of the wxPdfFontManager.

Regards,

Ulrich


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 2 guests