Search code examples
c++barcodegdi

Printing a barcode font on a POS printer


I am trying to use CreateFont to load a Code 128 font so I can output a barcode on a slip. CreateFont is not returning NULL but seems to be failing to find the correct font and is loading something else as I get the code printed as plain text.

This is the barcode function.

void EpicSlipPrinter::addBarcode ( const QString& text )
{
  HFONT tempFont = CreateFont ( 60 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , L"Code 128" );
  oFont = (HFONT)SelectObject ( mPrinterHandle , tempFont );

  INT ixPosition = iPaperWidth / 2;

  SIZE sz = getTextDimensions ( text , 3 , 3 , 600 , 0 , 0 );

  SetTextAlign ( mPrinterHandle , TA_CENTER );

  TextOut ( mPrinterHandle , ixPosition , iyPosition , text , text.length () );

  SelectObject ( mPrinterHandle , oFont );
  DeleteObject( tempFont );
}

The text parameter is definitely correct so what I think is happening is that CreateFont can't find the specified font and is defaulting to something else. The font is correctly loaded and can be selected from Windows applications, if I type the code into Word then I get a barcode that can be read from a scanner.

My question is does anyone know why CreateFont is not loading Code 128 or if there is another problem I am missing?


Solution

  • You've specified 0 for the fdwCharSet, which corresponds to ANSI_CHARSET, so the font mapper is looking for something that supports the current code page. Try using SYMBOL_CHARSET for these kinds of specialty fonts.