In Delphi 7, string
is an alias for AnsiString
, which encodes Unicode characters as 8-bit bytes using Windows codepages. In some MBCS codepages, Unicode characters may require multiple bytes (Turkish is not one of them, though).
Microsoft has several codepages for Turkish:
- 857 (MS-DOS)
- 1254 (Windows)
- 10081 (Macintosh)
- 28599 (ISO-8859-9)
In both codepages 1254 and 28599 (where 1254 is the most likely one you will run into), the Unicode characters in question are encoded in 8-bit as hex $FE
(ş
), $F0
(ğ
), and $FD
(ı
).
Make sure your sText
string variable actually contains those byte values to begin with, and not ASCII bytes $73
(s
), $67
(g
), and $69
(i
) instead. If it contains the latter, you are losing the Turkish data before it even reaches Canvas.TextOut()
. That would be an issue earlier in your code.
However, If sText
contains the correct bytes, then the problem has to be on the OS side, as TCanvas.TextOut()
is just a thin wrapper for the Win32 API ExtTextOutA()
function, where sText
gets passed as-is to the API. Maybe the particular font you are using doesn't support Turkish, or at least those particular characters. Or maybe there is a problem with the printer driver. Either way, you might have to resort to converting your sText
value to a WideString
using MultiByteToWideChar()
and then call ExtTextOutW()
(not ExtTextOutA()
) directly, eg:
var
wText: WideString;
size: TSize;
begin
//Printer.Canvas.TextOut(x, y, sText);
SetLength(wText, MultiByteToWideChar(1254{28599}, 0, PAnsiChar(sText), Length(sText), nil, 0));
MultiByteToWideChar(1254{28599}, 0, PAnsiChar(sText), Length(sText), PWideChar(wText), Length(wText)));
Windows.ExtTextOutW(Printer.Canvas.Handle, x, y, Printer.Canvas.TextFlags, nil, PWideChar(wText), Length(wText), nil);
size.cX := 0;
size.cY := 0;
Windows.GetTextExtentPoint32W(Printer.Canvas.Handle, PWideChar(wText), Length(wText), size);
Printer.Canvas.MoveTo(x + size.cX, Y);
end;