its.printing selectfont error

Started by chalky, July 17, 2024, 20:01:05

Previous topic - Next topic

chalky

The its.printing module is brilliant in that it provides [the only] print functionality for BlitzMax.

I have been using parts of it to add printing to my IDE, but encountered a frustrating problem where setting the printer font (via the selectfont() function within the module) failed to actually change the printer font despite everything seemingly returning values correctly. After several hours of testing and debugging, I checked the Microsoft definition of the windows CreateFontA function:

HFONT CreateFontA(
  [in] int    cHeight,
  [in] int    cWidth,
  [in] int    cEscapement,
  [in] int    cOrientation,
  [in] int    cWeight,
  [in] DWORD  bItalic,
  [in] DWORD  bUnderline,
  [in] DWORD  bStrikeOut,
  [in] DWORD  iCharSet,
  [in] DWORD  iOutPrecision,
  [in] DWORD  iClipPrecision,
  [in] DWORD  iQuality,
  [in] DWORD  iPitchAndFamily,
  [in] LPCSTR pszFaceName
);

This specifies the pszFaceName variable as a CString, and I was therefore able to track the error down to the Create:TPrinterFont() function within its.printing, which was defined as:

Function Create:TPrinterFont( page:TPrintPage, name:String, points:Int, weight%=0, italic%=False, Underline%=False, Strikeout%=False )
    If Not page Or Not name Then Return Null
    Local pixels% = points * page._DPIy / 72
    Local sName:String = name
    Local pName:Byte Ptr = sName.toWString() ' <-- SHOULD BE: sName.toCString()
    Local fontkey$ = Upper( name ) + points +"."+weight+"."+italic+"."+Underline+"."+Strikeout
    Local font:TPrinterFont
    Local hFont%
    ' check if already loaded...
    font = TPrinterFont( MapValueForKey( fonts, fontkey ) )
    If font Then
        Return font
    EndIf
    ' no - so create instead...
    If weight = 1 Then weight = FW_BOLD        '# Allow TRUE to represent BOLD
    hFont=__CreateFont(pixels, 0, 0, 0, ..
                       weight, italic, underline, strikeout, ..
                       DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FF_DONTCARE, ..
                       pName )
    If Not hFont Then
        Return Null
    EndIf
    font=New TPrinterFont
    font.hFont=hFont       
    '# Add to font list
    MapInsert( fonts, fontkey, font )
    Return font
End Function

As can be seen above, string variable pName is created as a WString before being passed to pszFaceName when it should be a CString. Once this was corrected the function worked correctly and I was able to change the printer font.

I thought I'd post this here in case anyone else encounters the same issue and hasn't been able to resolve it.

NB: This is on old-skool BlitzMax and not BlitzMax NG...

Scaremonger

Hi,
I wrote that module years back for a custom app for a customer and never expected it to still be used. 

Great to see that you found the problem and documented it here.

Si...


fielder

very thank you chalky!!! (i'm also an user of it's printing... thank you Scaremonger for doing this!)

Hardcoal

I dont understand how to properly use this module..
I try to get page width and Size, or to change page properties..

Also after one printing it stops working.. even that the printer.close() was executed..

I dont understand the coordinates of the page.. I do page.DrawImage(Img,x,y) but its not persistant .. each image disobey the given coords.

Also does anyone know how to set the draw buffer into a pixmap if possible?
If i want to draw an image to a pixmax instead of copying pixel by pixel.

Some of the things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]
My Google Drive https://drive.google.com/drive/folders/1vOgkOxA_YwdMaomM2-K4NQh0mpwgpkii?usp=drive_link