utelle / wxpdfdoc

wxPdfDocument - Generation of PDF documents from wxWidgets applications
http://utelle.github.io/wxpdfdoc/
Other
71 stars 27 forks source link

text: background colour not working #52

Closed pele65 closed 5 years ago

pele65 commented 5 years ago

On a standard wxDC drawing text with a background colour ist working fine, also with rotated text. On a wxPdfDC the background colour has no function. Is this by design or is this a bug? At the moment I'm thinking about drawing a box by my own.

utelle commented 5 years ago

In principle, drawing on a wxPdfDC should give the same results as drawing on a screen wxDC. However, since a screen wxDC is based on bitmap graphics while a wxPdfDC is based on vector graphics, not for all wxDC operations exist equivalent wxPdfDC operations.

Since I do not know which wxDC methods you actually used, I can't tell whether the issue you described is a bug or simply a limitation of wxPdfDC.

Could you please provide some sample code that works when using wxDC, but does not deliver the expected results when using wxPdfDC? TIA.

pele65 commented 5 years ago

I tested this code snippet in MyFrame::Draw of your sample file printing.cpp:

    dc.SetTextForeground( wxColour( 0, 100, 0 ) );
    dc.DrawText( wxS( "This is text with a dark green foreground color (0,100,0)." ), 10, 100 );
    dc.SetBackgroundMode( wxSOLID );
    dc.SetTextBackground( wxColour( 255, 255, 0 ) );
    dc.DrawText( wxS( "This is text also with a yellow background color (255,255,0)." ), 10, 130 );

The result is a yellow background in the window, but no yellow background in the pdf file.

Besides, I managed to draw the background in own code. wxDC::GetMultiLineTextExtent works fine for that. It's just drawing a rectangle or a rotated polygon for rotated text. If there is no better (more elegant) way to do it in wxPdfDocument, I could implement this way.

pele65 commented 5 years ago

After thinking it over: The will be a difference when using a simple implementation. A multiline text with several lines will have a rectangle as background, while on the screen there will be a fitting background for each line. To avoid this I would have to copy part of the code of wxTextMeasureBase::GetMultiLineTextExtent to split the text in lines in the same way. I'm not sure this is a good thing, but I'm not the maintainer. So you decide ;-) Maybe the simple way is the right one to start.

utelle commented 5 years ago

Thanks for providing your sample code.

Currently the method DrawText of wxPdfDC has no provisions to handle text background colours. As you say it will be necessary to draw filled rectangles as background. The implementation should closely follow the route of other wxDC implementations to get matching results. The difficulty will be to determine the vertical extent of the rectangle, since text alignment is handled very differently in PDF than on screen.

I will look into this, but I can't predict when I will be able to provide a solution.

pele65 commented 5 years ago

Solved with issue #53.

utelle commented 5 years ago

Commit 48af68eb8aca7749285750d00ccd8f86e531fd47 fixes the wrong calculation of the background rectangle on applying a text background color.