PurePDF Version 2.0 Unicode update / Font Loading

Just starting out? Need help? Post your questions and find answers here.
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

PurePDF Version 2.0 Unicode update / Font Loading

Post by normeus »

I am now using adobe reader to open PDF files created with PurePDF
If you add a font which is Unicode the text is displayed fine but if you try to search for text in Adobe it wont find any text.

To fix this an Object which maps Unicode must be added. It is not a big change in code if you define the lower Unicode set to match the ASCII set.

This might be an English only thing, Adobe might work fine in a different language.

This code replaces the unicode font test: from "If Fonts()\unicode" all the way to "else"

Code: Select all

     If Fonts()\unicode
        ; Page 279
        ipdf_Out("<</Type /Font")
        ipdf_Out("/Subtype /Type0")
        ipdf_Out("/BaseFont /"+tempfontname$+"-UCS")
        ipdf_Out("/Encoding /Identity-H")
        ipdf_Out("/DescendantFonts ["+Str(pdfN+1)+" 0 R]")
        ipdf_Out("/ToUnicode "+Str(pdfN+2)+" 0 R") 
        ipdf_Out(">>")
        ipdf_Out("endobj")
        
        ipdf_NewObj()
        ; PDF-Reference : PDF32000_2008.pdf page 269
        ipdf_Out("<</Type /Font")
        ipdf_Out("/Subtype /CIDFontType2")
        ipdf_Out("/BaseFont /"+tempfontname$)
        ipdf_Out("/CIDSystemInfo <</Registry (Adobe) /Ordering (UCS) /Supplement 0>>")
        ipdf_Out("/FontDescriptor "+Str(pdfN+2)+" 0 R")
        ; Font-Width
        HexData$="/W ["
        For i=0 To fonts()\numglyphs-1
          ForEach Fonts()\glyph_list(i)\char_no()
            HexData$+StrU(Fonts()\glyph_list(i)\char_no(),#PB_Long)+" ["+StrU(Fonts()\glyph_list(i)\scaledwidth,#PB_Long)+"] "
          Next
        Next
        HexData$+"]"
        ipdf_Out(HexData$)
        ipdf_Out("/CIDToGIDMap "+Str(CIDToGIDMap)+" 0 R")
        ipdf_Out(">>")
        ipdf_Out("endobj")
        
        ipdf_NewObj()
        ipdf_Out("<<")
        ipdf_Out("/Length 345")
        ipdf_Out(">>")        
        ipdf_Out("stream")
        ipdf_Out("/CIDInit /ProcSet findresource begin")
        ipdf_Out("12 dict begin")
        ipdf_Out("begincmap")
        ipdf_Out("/CIDSystemInfo")
        ipdf_Out("<</Registry (Adobe)")
        ipdf_Out("/Ordering (UCS)")
        ipdf_Out("/Supplement 0")
        ipdf_Out(">> def")
        ipdf_Out("/CMapName /Adobe-Identity-UCS def")
        ipdf_Out("/CMapType 2 def")
        ipdf_Out("1 begincodespacerange")
        ipdf_Out("<0000> <FFFF>")
        ipdf_Out("endcodespacerange")
        ipdf_Out("1 beginbfrange")
        ipdf_Out("<0000> <FFFF> <0000>")
        ipdf_Out("endbfrange")
        ipdf_Out("endcmap")
        ipdf_Out("CMapName currentdict /CMap defineresource pop")
        ipdf_Out("end")
        ipdf_Out("end")
        ipdf_Out("endstream")
        ipdf_Out("endobj")
      Else
 
I really don't have time to create a nice download package with examples. I want to add transparency to images but this will take time. I think some people would make use of the changes I've made so far.

I created a page so you can download PurePDF.pb with the changes I've made and you can read a list of changes.
None of the changes should break your code. Most of them just repair stuff that was not working.

http://normeus.com/data/uploads/pb/PurePDF.pb
http://normeus.com/data/uploads/pb/purepdf_res.pb

http://normeus.com/purepdf-changes/

Thank you.
Norm
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by Lord »

Hi normeus!

I just switched to the version you offer on your new web site
for testing this new version.

Previously I used PurePDF.pb v2.25

I just changed the include path to the new PDF include.
Now I encounter a problem in my project using your code.

After saving the a generated PDF (which is fine) I get an error
at line 3732 in PurePDF.pb saying that there is no actual element
in list Offsets():

Code: Select all

  ; Pages root 
  SelectElement(Offsets(), 0)
  Offsets() = pdfBuffer\lCurSize;               <---- marked error
  ipdf_Out("1 0 obj")
  ipdf_Out("<</Type /Pages")
  ipdf_Out("/Kids ["+Trim(lsKids)+"]")
  ipdf_Out("/Count "+Str(ListSize(Pages())))
  ipdf_Out(">>")
  ipdf_Out("endobj") 
List Offset() is not used nor defined in main program.
And I couldn't find the definition in the include either.

Any idea what went wrong?
Image
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by normeus »

Lord,
Post a small sample of your code with the problem.
offsets() contains a list of objects created in your PDF. This code has not changed since 2.25.

Make sure you are using the include file.
If you installed 2.25 as a library then you might still be using the library from
"C:\Program Files\PureBasic\" or "C:\Program Files (x86)\PureBasic"

P.S.
After reading the whole message you posted, It might be that after saving you tried to create a new pdf without adding font
or something of that nature.

or you saved the file twice and it fails the 2nd time because as soon as you save the file it clears all the LISTS

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by Lord »

Hi normeus!

I think I got my error.
When in my program a formatting error occurs, the respective procedure will
end the creation of a pdf file and instead of saving, it calls ipdf_Close() and
cleans some other stuff up.
I had still also a call to ipdf_Close() after saving the pdf file.
This call didn't hurt with v2.25 and before but now it does.
After I removed this one call after saving my program works again.
So it was my fault.
Thanks for trying to help. At least you did. :wink:
And thanks for keeping this good work alive.
Image
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by IdeasVacuum »

Really excellent stuff Norm. I think all languages are likely to be affected. Fixing issues in PurePDF is not a trivial task, your work is very much appreciated.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by normeus »

@lord
Glad to be of help.

@IdeasVacuum
Thank you for the kind words. I might be working on PurePDF if I get some free time. We need to add transparency for images.


Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: PurePDF Version 2.0 Unicode update / Font Loading

Post by normeus »

It never occurred to me that you might want to stop creating a PDF in the middle of creation.
This is how you would test if the file was save and if not the PDF is still in memory so figure out what the problem is
and save again.

Code: Select all

If pdf_save("Bad?name") 
  Debug "saved file ready to start new one"
Else
  Debug "Couldn't save, ask for a new filename then try to save again since contents are in memory"
  ; use a message requester to get new file name and save again
endif

If AN ERROR happens in the middle of creation ( something that never happens to me :wink: )
then you have to cleanup memory

Code: Select all

;   middle of creating PDF
;   some more PDF stuff....
; GIVE UP ON PDF, STOP CREATING !!
  If pdfBuffer\pData
    FreeMemory(pdfBuffer\pData)
  EndIf
  pdfBuffer\pData = 0
  pdfOBJNames=0
  pdfJavaScript$=""
  ClearList(Pages())
  ClearList(Offsets())
  ClearList(PageLinks())
  ClearList(Fonts())
  ClearList(FontList())
  ClearList(Images())
  ClearList(FileList())
  ClearList(pEndPage())
  ClearList(pPutCatalog())
  ClearList(pPutResources())
  ClearList(pEndDoc())
  ClearList(pdfTOC())
  pdfState=0
  pdferror=0
  pFooter=0
  pHeader=0

Thank you @lord for pointing out the lack of a cleanup routine.
( There might be some other stuff to delete but these are the main offenders, I will take a look over the weekend )
[edit] @lord is looking into what else needs to be cleared Oct,10,2017
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply