wxPdfDoc - Probblem with WriteXml and align=justify

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
dschmeer
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Apr 13, 2018 10:04 pm

wxPdfDoc - Probblem with WriteXml and align=justify

Post by dschmeer »

I use WriteXml to draw some paragraphs of text with align=justify. For the standard fonts this works fine, but for other fonts it simply draws the text left-aligned.

Here is the code I use:

Code: Select all

	wxPdfFontManager::GetFontManager()->RegisterSystemFonts();
	wxPdfDocument pdf;
	pdf.SetFont("Helvetica");
	pdf.AddPage();
	pdf.SetMargins(25, 20, 120);
	pdf.SetXY(25, 20);
	pdf.Rect(25, 20, 65, 150);
	pdf.WriteXml("<p align=\"justify\"><font face=\"Helvetica\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Times\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Arial\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Calibri\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Verdana\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Times New Roman\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.WriteXml("<p align=\"justify\"><font face=\"Georgia\">The quick brown fox jumps over the lazy dog</font></p>");
	pdf.SaveAsFile("test.pdf");
The first two paragraphs are justified, the rest are left aligned.

Has anyone an idea why this is and what I can do to fix this?

Versions:
wxWidgets 3.1.0
wxPdfDoc 0.9.6
Visual C++ 2015
Platform Windows 10

Thanks a lot
Dieter
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDoc - Probblem with WriteXml and align=justify

Post by utelle »

dschmeer wrote:I use WriteXml to draw some paragraphs of text with align=justify. For the standard fonts this works fine, but for other fonts it simply draws the text left-aligned.

Has anyone an idea why this is and what I can do to fix this?
I used your code sample to investigate the issue. Unfortunately, I have to confirm that justifying text works only for the standard fonts.

In principle, the calculations in wxPdfDocument for adjusting the word spacing are correct. However, I was not aware of a limitation of the PDF specification - the word spacing operator (that is currently used by wxPdfDocument) is only applied to simple fonts. Following the respective paragraph from the PDF reference:
Word spacing shall be applied to every occurrence of the single-byte character code 32 in a string when using a simple font (including Type 3) or a composite font that defines code 32 as a single-byte code. It shall not apply to occurrences of the byte value 32 in multiple-byte codes.
The fonts you are using are handled as TrueType Unicode strings ... and therefore the word spacing operator is not applied.

Unfortunately, it will not be trivial to overcome this limitation, because it will require to break up the text string at every space character and to explicitly adjust the word spacing.

Regards,

Ulrich
dschmeer
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Apr 13, 2018 10:04 pm

Re: wxPdfDoc - Probblem with WriteXml and align=justify

Post by dschmeer »

Hello Ulrich,

some weeks ago I finally found some time for looking into this and implemented a solution that works for me without problems since then.

I attached my solution based on wxPdfDoc 0.9.6.

The new code is only used with wxPDF_ALIGN_JUSTIFY and Unicode Fonts, otherwise the old code is used. This should minimize any problems that might be introduced by my code, as it is only used in cases where justify didn't work until now anyway.

It would be nice if you could have a look at the code and maybe even integrate it into wxPdfDoc, so that others can use it too and I don't have to merge it into new versions ;-)

Regards,

Dieter
Attachments
pdfxml.cpp
(58.9 KiB) Downloaded 250 times
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDoc - Probblem with WriteXml and align=justify

Post by utelle »

Hi Dieter,
dschmeer wrote:some weeks ago I finally found some time for looking into this and implemented a solution that works for me without problems since then.
Thanks for addressing this issue.
dschmeer wrote:I attached my solution based on wxPdfDoc 0.9.6.

The new code is only used with wxPDF_ALIGN_JUSTIFY and Unicode Fonts, otherwise the old code is used. This should minimize any problems that might be introduced by my code, as it is only used in cases where justify didn't work until now anyway.

It would be nice if you could have a look at the code and maybe even integrate it into wxPdfDoc, so that others can use it too and I don't have to merge it into new versions ;-)
I will take a closer look at your solution. If I don't find any show stopper I will most likely integrate it in wxPdfDocument. I'll drop a note.

Regards,

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

Re: wxPdfDoc - Probblem with WriteXml and align=justify

Post by utelle »

utelle wrote:I will take a closer look at your solution. If I don't find any show stopper I will most likely integrate it in wxPdfDocument. I'll drop a note.
Just a short update:

In the meantime I implemented a generic solution to this justification problem (see wxPdfDocument github repository).

Regards,

Ulrich
Post Reply