VASmalltalk – pdf creation using libHaru

I’ve just published a first, not ready code for a wrapper around libHaru – an external ANSI c library to produce PDF documents.

The actual work is just the plain procedural 1:1 conversion of the C-API into Smalltalk – nothing more.

Due to the large numbers of API calls I just wrap only the API calls needed to make the font demo working.

Initial procedural code:

| haruInterface  aHPDFDoc aHPDFPage aHPDFFont height width  tw pageTitle |
		
pageTitle := 'Font Demo'. 
		
haruInterface := MSKHaruInterface new initialize.
aHPDFDoc := haruInterface new: nil userData: nil.

"add a new page"
aHPDFPage := haruInterface addPage: aHPDFDoc.

height := haruInterface apiGetPageHeight: aHPDFPage.
width := haruInterface apiGetPageWidth: aHPDFPage.
	
"Print the lines of the page"
haruInterface
  apiPageSetLineWidth: aHPDFPage value: 1 ;
  apiPageRectangle: aHPDFPage x: 50 y: 50 width: width - 100 height: height - 110 ;
  apiPageStroke: aHPDFPage.
		
"Print the title of the page (with positioning center)."		
aHPDFFont := haruInterface apiGetFont: aHPDFDoc fontName: 'Helvetica'.
haruInterface 
  apiPageSetFontAndSize: aHPDFPage font: aHPDFFont size: 24.
		
tw :=  haruInterface 	apiPageTextWidth: aHPDFPage value: pageTitle.
		
haruInterface
  apiPageBeginText: aHPDFPage ;
  apiPageTextOut: aHPDFPage x: (width - tw) / 2 y: (height - 50) value: pageTitle ;
  apiPageEndText: aHPDFPage.
			
"output subtitle."
haruInterface
  apiPageBeginText: aHPDFPage ;
  apiPageSetFontAndSize: aHPDFPage font: aHPDFFont size: 16;
  apiPageTextOut: aHPDFPage x: 60 y: (height - 80) value: '' ;
  apiPageEndText: aHPDFPage.

haruInterface
  apiPageBeginText: aHPDFPage ;
  apiPageMoveTextPos: aHPDFPage x: 60 y: (height - 105).
			
self getFontList do: [ :eachFontName |
  | sampText font |
			
  sampText := 'abcdefgABCDEFG12345!#$%&+-@?'.
  font := haruInterface apiGetFont: aHPDFDoc fontName: eachFontName.
			
  " print a label of text"
  haruInterface
    apiPageSetFontAndSize: aHPDFPage font: aHPDFFont size: 9;
    apiPageShowText: aHPDFPage value: eachFontName ;
    apiPageMoveTextPos: aHPDFPage x: 0 y: -18.

  "print a sample text"
  haruInterface
    apiPageSetFontAndSize: aHPDFPage font: font size: 20;
    apiPageShowText: aHPDFPage value: sampText ;
    apiPageMoveTextPos: aHPDFPage x: 0 y: -20.
].
			
  haruInterface
    apiPageEndText: aHPDFPage;		
    safeToFile: aHPDFDoc filename: 'm:\test.pdf';
    apiFree: aHPDFDoc

The result should be similiar to FontDemoResult

This entry was posted in Smalltalk and tagged , , , . Bookmark the permalink.

2 Responses to VASmalltalk – pdf creation using libHaru

  1. Torsten says:

    I already wrapped LibHaru for Squeak/Pharo with a more natural Smalltalk interface.


    | document page image |
    document := PDFDocument new.
    page := document addPage.
    image := document loadPNGImage: 'Image.png'.
    page drawImage: image rectangle: (Rectangle origin: 72@72 extent: 144@144).
    document saveToFile: 'documentWithImage.pdf'

    So anything you have to do is to port it to VAST since I released the code as MIT:

    http://www.squeaksource.com/HPDF.html

  2. schrievkrom says:

    I’ve not said, that I’ve finished the wrapper – it’s just the first initial code stamp, which is ready to fullfill the first demo. Nothing more and for one day work – ok.

    And yes, with this low-level 1:1 c-api wrapper this is not what I really like.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s