wxPdfDc semi transparent brush Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 374
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxPdfDc semi transparent brush

Post by mael15 » Fri Aug 23, 2019 2:01 pm

Hi,
On screen I paint a rectangle with 10% transparency using

Code: Select all

dc->SetBrush(wxColour(col.Red(), col.Green(), col.Blue(), 0x19));
When I create a pdf with wxPdfDc using the same code, this rectangle is not transparent. How can I make it 10% transparent?
Thank you. :)

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

Re: wxPdfDc semi transparent brush

Post by utelle » Thu Sep 05, 2019 7:40 pm

Sorry for the late response. This post simply dropped out of sight too fast, since I don't check the wxForum that often. In urgent cases post to the wxCode subforum, or even better open an issue on GitHub.
mael15 wrote:
Fri Aug 23, 2019 2:01 pm
On screen I paint a rectangle with 10% transparency using

Code: Select all

dc->SetBrush(wxColour(col.Red(), col.Green(), col.Blue(), 0x19));
When I create a pdf with wxPdfDc using the same code, this rectangle is not transparent. How can I make it 10% transparent?
Unfortunately, wxPdfDC currently doesn't support alpha transparency, although the underlying wxPdfDocument class does. It shouldn't be too hard to add this capability. However, it would need testing.

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

Re: wxPdfDc semi transparent brush

Post by utelle » Thu Sep 05, 2019 9:01 pm

utelle wrote:
Thu Sep 05, 2019 7:40 pm
Unfortunately, wxPdfDC currently doesn't support alpha transparency, although the underlying wxPdfDocument class does. It shouldn't be too hard to add this capability. However, it would need testing.
In the meantime I have implemented the alpha transparency support for wxPdfDC. Commit a03399b on GitHub provides the updated source code. Please give it a try.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 374
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxPdfDc semi transparent brush

Post by mael15 » Fri Sep 06, 2019 9:36 am

Thank you!
It does not work yet. I saw the alpha value was there in SetupAlpha() in
> wxpdfdocd.dll!wxPdfDCImpl::DoDrawRectangle(int x, int y, int width, int height) Zeile 791 C++
wxmsw313ud_core_vc_x64_custom.dll!wxDC::DrawRectangle(int x, int y, int width, int height) Zeile 1170 C++
but it does not show up in the pdf.

On more sidenote:
It was a bit difficult to build with Visual Studio 2019 debug dll x64. The vc15 project uses wxCompilerPrefix in the include paths, but the wxWidgets build does not. Quite a bit of tweaking was necessary to get rid of all the "142" everywhere, i.a. vc_x64_dll instead of vc142_x64_dll.

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

Re: wxPdfDc semi transparent brush

Post by utelle » Fri Sep 06, 2019 11:18 am

mael15 wrote:
Fri Sep 06, 2019 9:36 am
Thank you!
It does not work yet. I saw the alpha value was there in SetupAlpha() in
> wxpdfdocd.dll!wxPdfDCImpl::DoDrawRectangle(int x, int y, int width, int height) Zeile 791 C++
wxmsw313ud_core_vc_x64_custom.dll!wxDC::DrawRectangle(int x, int y, int width, int height) Zeile 1170 C++
but it does not show up in the pdf.
Could you please provide a small code sample for testing? TIA
mael15 wrote:
Fri Sep 06, 2019 9:36 am
On more sidenote:
It was a bit difficult to build with Visual Studio 2019 debug dll x64. The vc15 project uses wxCompilerPrefix in the include paths, but the wxWidgets build does not. Quite a bit of tweaking was necessary to get rid of all the "142" everywhere, i.a. vc_x64_dll instead of vc142_x64_dll.
You don't have to tweak the project files. All what is required is to adjust the settings in the file wx_setup.props. Change the lines defining the options wxCompilerPrefix and wxMsvcVersionAuto, so that they look like this:

Code: Select all

<wxCompilerPrefix>vc</wxCompilerPrefix>
<wxMsvcVersionAuto></wxMsvcVersionAuto>
Thereafter building the project should work like expected.

Build files for Visual C++ 2019 will be provided in the near future, too. However, the process to adjust settings by modifying wx_setup.props will stay the same.

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

Re: wxPdfDc semi transparent brush

Post by utelle » Fri Sep 06, 2019 2:29 pm

mael15 wrote:
Fri Sep 06, 2019 9:36 am
It does not work yet. I saw the alpha value was there in SetupAlpha() in
> wxpdfdocd.dll!wxPdfDCImpl::DoDrawRectangle(int x, int y, int width, int height) Zeile 791 C++
wxmsw313ud_core_vc_x64_custom.dll!wxDC::DrawRectangle(int x, int y, int width, int height) Zeile 1170 C++
but it does not show up in the pdf.
Sorry, most likely that has to be some improper use of the wxDC API. I just tested myself drawing transparent rectangles on a wxPdfDC ... and the resulting PDF clearly showed transparent rectangles.

Please show the code for which you think you get wrong results.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 374
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxPdfDc semi transparent brush

Post by mael15 » Fri Sep 06, 2019 3:35 pm

Sorry, took a while to extract only the relevant code:

Code: Select all

#pragma once

#include <wx/frame.h>
#include <wx/app.h>
#include <wx/dcbuffer.h>
#include <wx/dcgraph.h>
#include <wx/pdfdocument.h>
#include "wx/pdfdc.h"

class MyPanel : public wxPanel {
public:
	MyPanel(wxWindow* par) : wxPanel(par) {
		Connect(wxEVT_PAINT, wxPaintEventHandler(MyPanel::OnPaint));
	}

	void OnPaint(wxPaintEvent& event) {
		wxBufferedPaintDC dc(this);
		dc.SetBrush(*wxGREY_BRUSH);
		dc.Clear();
		wxGCDC gcdc(dc);
		draw(&gcdc);
	}

	void draw(wxDC *dc);
};

class MyFrame1 : public wxFrame 
{
public:
	MyFrame1( wxWindow* parent = NULL, 
		wxWindowID id = wxID_ANY, 
		const wxString& title = wxEmptyString, 
		const wxPoint& pos = wxDefaultPosition, 
		const wxSize& size = wxSize( 500,300 ), 
		long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
	~MyFrame1(){}
	void onButtonClicked(wxCommandEvent &evt) {
		wxPdfDC* dc = nullptr;
		wxPdfDocument pdfDcmt;
		pdfDcmt.AddPage(wxPORTRAIT, wxPAPER_A4);
		int tpl = pdfDcmt.BeginTemplate();

		dc = new wxPdfDC(&pdfDcmt, 210, 297);
		dc->SetResolution(600);
		dc->SetBackground(*wxWHITE_BRUSH);

		if (dc->StartDoc(wxT("Printing ...")))
		{
			//OutputDebugString(wxString::Format(wxT("Seite %i wird gedruckt\n"), i));
			myPan->draw(dc);
			dc->EndDoc();
		}

		pdfDcmt.EndTemplate();
		pdfDcmt.UseTemplate(tpl);
		pdfDcmt.SaveAsFile(wxGetUserHome() + wxT("\\Desktop\\test.pdf"));
	}

private:
	MyPanel* myPan = nullptr;
};

class MyApp : public wxApp {
	virtual bool OnInit();
};

Code: Select all

#include "app.h"
#include <wx/sizer.h>
#include <wx/button.h>

bool MyApp::OnInit() {
	MyFrame1 *mainFrame = new MyFrame1();
	mainFrame->Show();
	SetTopWindow(mainFrame);

	return TRUE;
}

wxIMPLEMENT_APP(MyApp);

MyFrame1::MyFrame1( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : 
	wxFrame( parent, id, title, pos, size, style )
{
	SetSizer(new wxBoxSizer(wxVERTICAL));
	wxButton *but = new wxButton(this, wxID_ANY, wxT("create pdf"));
	Bind(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame1::onButtonClicked), this);
	GetSizer()->Add(but);

	myPan = new MyPanel(this);
	GetSizer()->Add(myPan, 1, wxEXPAND);

	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
	this->Centre( wxBOTH );
}

void MyPanel::draw(wxDC *dc)
{
	wxSize dcSize = dc->GetSize();

	dc->SetBrush(wxColour(255, 0, 0, 25));
	dc->DrawCircle(dcSize.x * 0.3, dcSize.y * 0.5, dcSize.GetHeight() * 0.5);
	dc->SetBrush(wxColour(0, 255, 0, 25));
	dc->DrawCircle(dcSize.x * 0.7, dcSize.y * 0.5, dcSize.GetHeight() * 0.5);
}
:?

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

Re: wxPdfDc semi transparent brush

Post by utelle » Fri Sep 06, 2019 6:19 pm

Well, you did not reveal earlier that you were using the wxPdfDC template mode.

Indeed this did not work due to missing references to the required extended graphics state information. This has been fixed in the latest commit on GitHub.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 374
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxPdfDc semi transparent brush

Post by mael15 » Fri Sep 06, 2019 6:34 pm

utelle wrote:
Fri Sep 06, 2019 6:19 pm
Well, you did not reveal earlier that you were using the wxPdfDC template mode.

Indeed this did not work due to missing references to the required extended graphics state information. This has been fixed in the latest commit on GitHub.
Aaaaaah, okay, nice! I will check it first thing Monday. Thank you very much for the quick fix and have a nice weekend! :)

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 374
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxPdfDc semi transparent brush

Post by mael15 » Tue Sep 10, 2019 2:53 pm

utelle wrote:
Fri Sep 06, 2019 6:19 pm
This has been fixed in the latest commit on GitHub.
Yes, Works now! =D> :D
utelle wrote:
Fri Sep 06, 2019 11:18 am
You don't have to tweak the project files. All what is required is to adjust the settings in the file wx_setup.props. Change the lines defining the options wxCompilerPrefix and wxMsvcVersionAuto, so that they look like this:

Code: Select all

<wxCompilerPrefix>vc</wxCompilerPrefix>
<wxMsvcVersionAuto></wxMsvcVersionAuto>

Thereafter building the project should work like expected.
Also very helpful =D>

Thank you very much for the fast fix!

Post Reply