I use the fonts "Consolas" and/or "Courier New" in a project to draw an MS-DOS-looking environment. In this project if I use TextOut (of the TCanvas) to print for Box Drawing characters sequentially in one statement, everything is fine, for example it prints "────────" but if I address each character to print them separately, there would be a gap between each character, something like this: "-----------". Here is an example for you to test it manually:
...
Canvas.Font.Size := 12;
w := Canvas.TextWidth('╬');
h := Canvas.TextHeight('╬');
Canvas.TextOut(100, 100, '╬╬');
Canvas.TextOut(100, 100 + h, '╬');
Canvas.TextOut(100 + w, 100 + h, '╬');
Canvas.TextOut(100, 100 + h * 2, '╬');
Canvas.TextOut(100 + w, 100 + h * 2, '╬');
The output is:
As you can see, vertically they are connected fine but horizontally there is a gap.
How can I fix it? Note that I draw what I want in an array, and then a procedure prints the array as follows:
th := Canvas.TextHeight('A');
tw := Canvas.TextWidth('A');
for i := 0 to MaxWidth - 1 do
for j := 0 to MaxHeight - 1 do
begin
Canvas.Brush.Color := fChars[i, j].BGColor;
Canvas.Font.Color := fChars[i, j].FGColor;
Canvas.TextOut(i * tw, j * th, fChars[i, j].Character);
end;
If you use
DrawText()
instead ofCanvas.TextOut()
it works. The reason is explained in this SO answer. It is related to character kerning applied by the different windows API's on certain fonts.here is a full working example: