Recent

Author Topic: MuPDF library for Lazarus/FreePascal  (Read 1705 times)

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
MuPDF library for Lazarus/FreePascal
« on: May 27, 2020, 03:26:00 pm »
Hi all,

because I needed to incorporate PDF files in my application, I searched for a good PDF component for Lazarus.
The only one I found to be good and easy in use was using an old MuPDF library (version 1.08).  Because I needed to analyse PDF's in the background, without any user interaction, and a debug dialog sometimes popping up, I downloaded the MuPDF source, removed the popup and recompiled the library.

I have no experience in using and making good headers to use them in Lazarus, but anyway I made it up to v1.11 without any real big difficulties.  I want to go on until we are back on track with the newest version, and when done, adding the different possibilities of the library, so we can use them all from within Lazarus.

I am now working/stuck on version 1.12.  The library is compiled correctly, but loading a pdf gives an error.
If someone wants to get this to v1.17 too, please jump in.

If made a sourceforge svn:  https://sourceforge.net/projects/mupdflib-for-pascal/
I also made a little homepage for it: https://www.zittergie.be/software/mupdflib-for-pascal/

In the meantime, if you want to use PDF's in lazarus you can download version 1.11 (Only Windows 64-bit for now)
A small test app is also available
I will look into supporting Linux too.
Be the difference that makes a difference

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8491
  • FPC developer.
Re: MuPDF library for Lazarus/FreePascal
« Reply #1 on: May 27, 2020, 03:50:07 pm »
FPC comes with fcl-pdf ?

korba812

  • Full Member
  • ***
  • Posts: 126
Re: MuPDF library for Lazarus/FreePascal
« Reply #2 on: May 27, 2020, 03:54:33 pm »
FPC comes with fcl-pdf ?
fcl-pdf is for creating pdf documents. mupdf is for reading/rendering/printing pdf documents.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8491
  • FPC developer.
Re: MuPDF library for Lazarus/FreePascal
« Reply #3 on: May 27, 2020, 04:29:57 pm »
Does your problem also appear if you put

Code: [Select]
    uses
     math;
     
     SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision])

in your main program? It might be that the library assumes some floating point exceptions to be masked, while Pascal does convert them into exceptions by default.

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #4 on: May 27, 2020, 05:05:48 pm »
Doesn't help.

The code works fine for v1.11
One of the differences between v1.11 and v1.12 in the code used is a new parameter in the following code.
The variable pfz_separations doesn't yet exist in the v1.11.

v1.12
Code: Pascal  [Select][+][-]
  1. function fz_new_pixmap_with_bbox(ctx: pfz_context; cs: pfz_colorspace; var bbox: fz_irect; seps: pfz_separations; alpha: integer):pfz_pixmap;cdecl;external muLibName;
  2.  

v1.11
Code: Pascal  [Select][+][-]
  1. function fz_new_pixmap_with_bbox(ctx: pfz_context; cs: pfz_colorspace; var bbox: fz_irect; alpha: integer):pfz_pixmap;cdecl;external muLibName;
  2.  

I'v tried to add the following to v1.12:

Code: Pascal  [Select][+][-]
  1. * pfz_separations = ^fz_separations;
  2.     fz_separations = record
  3.       refs: cint;
  4.       num_separations: cint;
  5.       controllable: cint;
  6.       state: Array[0..4] of uint32;
  7.       cs: Array[0..63] of Pointer;
  8.       cs_pos: Array[0..63] of uint8;
  9.       rgba: Array[0..63] of uint32;
  10.       cmyk: Array[0..63] of uint32;
  11.       name: Pointer;
  12.     end;
  13.  

also tried a simple:
Code: Pascal  [Select][+][-]
  1. pfz_separations = pointer;

Be the difference that makes a difference

TRon

  • Sr. Member
  • ****
  • Posts: 287
Re: MuPDF library for Lazarus/FreePascal
« Reply #5 on: May 27, 2020, 08:06:56 pm »
Looking at (public headers) include-file separation.h, structure fz_separations seems to be declared as an opaque structure.

Code: C  [Select][+][-]
  1. 20 typedef struct fz_separations fz_separations;
  2.  

The definition of the structure inside structure.c (mupdf internals) seems (as expected) to be used internally only.

Have you noticed any other issues when using one of the many other (pixmap related) functions that uses that (new) structure ?

fwiw: pixmaps are reference counted, so could it be that you are confronted with an error somewhere else in your test-code ?

benohb

  • Full Member
  • ***
  • Posts: 192
Re: MuPDF library for Lazarus/FreePascal
« Reply #6 on: May 28, 2020, 12:01:51 am »
Good work

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #7 on: May 28, 2020, 08:26:08 am »
OK, One step further ...

The error has to do with:
Code: Pascal  [Select][+][-]
  1. Seps:=fz_page_separations(FCTX, FPage);

if I change this to
Code: Pascal  [Select][+][-]
  1. Seps:=nil;
it works and loads the PDF correctly.

This is of course not the right way to do it, because if we want this to be stable and supporting as much as possible functions of muPDF, it needs to be corrected before tackling v1.13

I declared pfz_separations as pointer in libmupdf112.pas.
and I declared Seps as pfz_separations.

UPDATE:  Got it to work, but still figuring out why.  Maybe because the 'Device' gets dropped after loading.
If anyone knows, let us know, because I am trying to understand how this all works.

If you get the separations just after the Page gets loaded (LoadPage) everything works fine, so adding
Code: Pascal  [Select][+][-]
  1. Seps:=fz_page_separations(FCTX, FPage);
in LOADPAGE procedure does the trick.
« Last Edit: May 28, 2020, 09:58:17 am by Zittergie »
Be the difference that makes a difference

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #8 on: May 28, 2020, 01:40:00 pm »
Compiled v1.13.0 (MuPDFLib) and upgraded the source to make use of this version.
Working fine.
Downloads available (See top)
Be the difference that makes a difference

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #9 on: June 03, 2020, 03:52:57 pm »
Updated to MuPDF 1.17.0 (latest MuPDF release)

I had trouble compiling the DLL for the versions 1.14.0 up-to 1.16.0
v1.17.0 did build without issues however.

For the moment only for Windows 64 bit.  Help is welcome to add MacOS, Linux and HaikuOS support.
Be the difference that makes a difference

jcmontherock

  • New Member
  • *
  • Posts: 34
Re: MuPDF library for Lazarus/FreePascal
« Reply #10 on: June 03, 2020, 05:29:32 pm »
Hello,
How did you build the dll without Visual Studio ?

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #11 on: June 03, 2020, 07:27:55 pm »
Hello,
How did you build the dll without Visual Studio ?

The DLL is built with Visual Studio Community 2019
Be the difference that makes a difference

jcmontherock

  • New Member
  • *
  • Posts: 34
Re: MuPDF library for Lazarus/FreePascal
« Reply #12 on: June 04, 2020, 06:05:27 pm »
Could you give us the dll ?

Zittergie

  • Full Member
  • ***
  • Posts: 112
    • XiX Music Player
Re: MuPDF library for Lazarus/FreePascal
« Reply #13 on: June 04, 2020, 08:19:00 pm »
Could you give us the dll ?

You can download the DLL for the different version at https://www.zittergie.be/software/mupdflib-for-pascal/
The .def for building the DLL yourself can also be downloaded.
Be the difference that makes a difference

af0815

  • Hero Member
  • *****
  • Posts: 553
Re: MuPDF library for Lazarus/FreePascal
« Reply #14 on: June 05, 2020, 07:20:00 am »
Thank you for this.

But the full package is not found: Oops! That page can’t be found.
regards
Andreas

 

TinyPortal © 2005-2018