(Gnome) wxPdfDocument minimal example - GP fault

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

(Gnome) wxPdfDocument minimal example - GP fault

Postby Radek » Thu Jun 28, 2012 5:37 am

Having a library compiled, I am trying the minimal example:

Code: Select all

int main()
{
  wxPdfDocument pdf;

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(wxT("Helvetica"),wxT("B"),16);  // GP fault
  ...


I have found no hints on the web so I tried to replace SetFon() by its wxFont variant. wxFont works for sure:

Code: Select all

int main()
{
  wxPdfDocument pdf;
  wxFont        fnt(16,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL);

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(fnt);   // GP fault
  ...


The font should be okay, the fnt statement has been copied from another project, which works. So it is not a misunderstanding in font names, families, etc. The crash looks the following way:

Code: Select all

#0 0xb75cdf7b   wxPdfFontManager::GetFont(wxString const&, int) const() (/usr/local/lib/libwxcode_gtk2u_pdfdoc-2.8.so.0:??)
#1 0xb7621ec8   wxPdfDocument::SelectFont(wxFont const&, bool) () (/usr/local/lib/libwxcode_gtk2u_pdfdoc-2.8.so.0:??)
#2 0xb759a813   wxPdfDocument::SetFont(wxFont const&) () (/usr/local/lib/libwxcode_gtk2u_pdfdoc-2.8.so.0:??)
#3 0x8049491   main() (/home/radek/Desktop/C++/project/wxpdf1/main.cpp:14)


What should I do?

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

Re: (Gnome) wxPdfDocument minimal example - GP fault

Postby utelle » Thu Jun 28, 2012 5:58 pm

Radek wrote:Having a library compiled, I am trying the minimal example:

Code: Select all

int main()
{
  wxPdfDocument pdf;

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(wxT("Helvetica"),wxT("B"),16);  // GP fault
  ...


I have found no hints on the web so I tried to replace SetFon() by its wxFont variant. wxFont works for sure:

[...]

What should I do?

How is this supposed to work? Keep in mind that wxPdfDocument is a component for use with wxWidgets. You are using it in a pure C++ main application without initializing wxWidgets. wxPdfDocument presumes that certain wxWidgets data structures are intialized. Therefore it is not very surprising that it doesn't work when these data structures are not initialzed.

Take a look at the minimal sample coming with wxPdfDocument (minimal.cpp in subfolder samples/minimal). For a console application you have to derive your own main application class from wxAppConsole and have to initialize it with the macro IMPLEMENT_APP_CONSOLE. Then inspect methods OnInit and OnExit.

For GUI applications see the GUI sample in subfolder samples/pdfdc.

Regards,

Ulrich

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: (Gnome) wxPdfDocument minimal example - GP fault

Postby Radek » Fri Jun 29, 2012 9:11 am

I see and my apologies. wxWidgets classes which do not need event handling usually work with "int main". Sure, there is no reason why they should. So my first attempt was "int main". The second attempt was making the wxAppConsole code templates finally :mrgreen: and redoing my first attempt:

Code: Select all

#include <wx/wx.h>
#include <wx/pdfdoc.h>
#include <wx/pdffontmanager.h>


class MyPdf : public wxAppConsole
{
  public :

    int           TryPdf();

    virtual bool  OnInit();
    virtual int   OnRun();
};

IMPLEMENT_APP_CONSOLE(MyPdf)
DECLARE_APP(MyPdf)


bool MyPdf::OnInit()
{
  wxPdfFontManager::GetFontManager()->AddSearchPath(wxT("/usr/share/fonts"));

  return true;
}


int MyPdf::OnRun()
{
  TryPdf();

  return 0;
}


int MyPdf::TryPdf()
{
  wxPdfDocument pdf;

  pdf.AddPage(wxPORTRAIT,wxPAPER_A4);
  pdf.SetFont(wxT("Helvetica"),wxT("B"),16);
  ...
}


It works. A few questions:

(1) wxStockGDI::DeleteAll(). This is not documented even in wx29. Is it necessary?
(2) When I installed wxPdfDocument, I had two secret wishings, both of them concern manipulating existing PDFs. Is it possible in general? I am interested in
(a) adding indexes to PDFs without index. Such PDFs are hard to use but they can contain useful info. The most horrible example of this is - wxWidgets reference in PDF. More than 2000 pages, no index. wxPdfParser seems to be able to load an existing PDF.
(b) displaying the contents of an existing PDF using wxWidgets. wxPdfDC seems to be the right tool for it.

But all this is far ahead, I will ask later. Right now, I would like to know "yes" or "no".

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

Re: (Gnome) wxPdfDocument minimal example - GP fault

Postby utelle » Fri Jun 29, 2012 11:55 am

Radek wrote:I see and my apologies. wxWidgets classes which do not need event handling usually work with "int main". Sure, there is no reason why they should. So my first attempt was "int main". The second attempt was making the wxAppConsole code templates finally :mrgreen: and redoing my first attempt:

wxPdfDocument doesn't require event handling, but there are other data structures like color "database", paper "database" and others of which wxPdfDocument makes use. Therefore initializing wxWidgets is important. As you see it works in console mode where wxWidgets turns off event handling by itself.

Radek wrote:A few questions:

(1) wxStockGDI::DeleteAll(). This is not documented even in wx29. Is it necessary?

As far as I can remember I introduced this call in OnExit because I experienced memory leaks. But that was for wxWidgets 2.8. Maybe it's not needed for wx 2.9.

Radek wrote:(2) When I installed wxPdfDocument, I had two secret wishings, both of them concern manipulating existing PDFs. Is it possible in general?

In general yes, although support is limited.

Radek wrote:I am interested in
(a) adding indexes to PDFs without index. Such PDFs are hard to use but they can contain useful info. The most horrible example of this is - wxWidgets reference in PDF. More than 2000 pages, no index. wxPdfParser seems to be able to load an existing PDF.

Yes, wxPdfParser is able to extract all objects from a PDF file, but it doesn't handle page content. Parsing page content is a major task which I do not intend to add to wxPdfDocument.

Manipulating existing PDF files (adding an index, adding or removing pages and so on) is currently not supported by wxPdfDocument.

Radek wrote:(b) displaying the contents of an existing PDF using wxWidgets. wxPdfDC seems to be the right tool for it.

Definitely not. wxPdfDC allows to integrate PDF output into the wxWidgets printing framework - see the sample coming with wxPdfDocument. This allows to easily "print" to PDF from a wxWidgets application.

wxPdfDocument is not a PDF renderer. I would recommend to use the system PDF viewer for displaying PDF files.

If you really need to render a PDF to screen yourself you should take a look at poppler or similar libraries.

Regards,

Ulrich


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 1 guest