1

I use simple GDI DrawText to output blocks of text to a printer.

The font used in the sample is Segoe UI. But you can use Arial or others too. It doesn't matter.

The algorithm for large text blocks is simple. DrawText is called with DT_CALCRECT with a kind of binary search for the length to get the largest possible text to print. Than DrawText is called without DT_CALCRECT to print the block.

Simple one line text column text is written with one call to DrawText with the given coordinates of the rectangle.

The result is real strange and can be seen in this sample PDF. Just look on the first line after the header. You can see the text "Test, Test" and you can see the strange kerning here perfectly. The kerning os sometimes so bad, that you can't even read the words.

How to get around this? Is it a problem with the used printer? Is it a problem with DrawText?

The distance between some chars in a word seem to be random in some case. Some spacing are wide other to narrow. The letter combination looks strange unreadable and ugly.

I tried different fonts and printers but the problem just varies but it is always present.

I know about ExTextOut and the capabilities to define the distance/kerning between all chars, but frankly I don't want to care about this. I just want that DrawText behaves on the printer like on the screen. The stuff works on the screen perfectly.

  • Added 2018-08-23 08:49 GMT+2* To the code (it is a complex printing engine).

1.Fonts to print are created simply with CFont::CreatePointFont, so the LOGFONT structure is cleared to zero and no additional flags are used except point and face.

2.The mapping mode is MM_ANISOTROPIC. To scale what is seen on the screen and what is to be printed I just use the size of a komparable object (textblock) on the printer and the same size on the screen. The real values for the sample printout to the Microsoft PDF Printer are as follows, the real way I calculate them is not of interest:

m_pDC->SetMapMode(MM_ANISOTROPIC);
m_pDC->SetViewportExt(2363,100);
m_pDC->SetWindowExt(355,13);

This has the effect that the height of a line in LPs is 13, the average character width in LPs is 6...

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • I think you might experience some DPI related problems. Can you show the printer DC setup code? – VuVirt Aug 22 '18 at 14:48
  • I create the DC with PrinterDlg and the mapping mode is anisotropic. – xMRi Aug 22 '18 at 16:45
  • How do you load the font? Do you set antialiasing quality? How do you calculate the font height? – VuVirt Aug 22 '18 at 18:35
  • Your printer uses kerning. What problem are you trying to solve? – IInspectable Aug 23 '18 at 02:28
  • Printing the same text from Word or even Notepad with the same font size looks not so randomly condensed and stretched at some points (Test,Test in first line after heading). Screen looks fine ;) – xMRi Aug 23 '18 at 06:04
  • And it is not an effect only on normal printers it happens also to XPS and PDF printers. – xMRi Aug 23 '18 at 06:14
  • I added more information about the mapping mode, and I changed the question to be more precise – xMRi Aug 23 '18 at 06:53
  • Could you provide a [MCVE], just a simple program that prints "Hello World" to the printer. – Jabberwocky Aug 23 '18 at 06:58
  • @Jabberwocky I will try. But I have to write new code that has nothing to do with my codebase... will take a view days. – xMRi Aug 23 '18 at 07:01
  • 1
    @xMRi yes I suppose it can't be done in a couple of minutes, but that's what I would do in order to pin point the problem. BTW I've already seen PDFs with strage spacing like in your sample a couple of years ago, but I really cannot remember where they came from. – Jabberwocky Aug 23 '18 at 07:04
  • Is there a reason you're still using the ancient GDI API instead of the modern Direct2D/DirectWrite APIs, intended to be used when things like "good looking text" matter? (GDI is from the DOS/Win95/98 era, XP introduced GDI+, with better text rendering as major improvement, Direct2D and DirectWrite then replaced GDI/GDI+ entirely once windows 7 came around, again with vastly improved shaping and rendering. If you're writing any new code, you really shouldn't be using GDI/GDI+, they're legacy APIs) – Mike 'Pomax' Kamermans Aug 23 '18 at 15:59
  • This is no new code. It is real old code. It is a printing engine that allows easy Excel like printing of data tables... but a customer complained about the strange character spacing... – xMRi Aug 23 '18 at 16:19

0 Answers0