Strange behavior with DrawTextA, courier New and Japanese

I found strange behavior when using DrawTextA in combination with the Courier New-font with Japanese locale. Consider the following Delphi XE2 code:

procedure PaintTexts(aPaintBox: TPaintBox; aCharset: Byte);
var
  A: AnsiString;
  S: string;
  R: TRect;
begin
  aPaintBox.Font.Charset := aCharset;

  A := '[DrawTextA] The word "Japan" in Japanese: 日本';
  R := Rect(0, 0, aPaintBox.Width, aPaintBox.Height);
  DrawTextA(aPaintBox.Canvas.Handle, PAnsiChar(A), Length(A), R, 0);

  S := '[DrawTextW] The word "Japan" in Japanese: 日本';
  R := Rect(0, 20, aPaintBox.Width, aPaintBox.Height);
  DrawTextW(aPaintBox.Canvas.Handle, PWideChar(S), Length(S), R, 0);
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintTexts(PaintBox1, DEFAULT_CHARSET);
end;

procedure TForm1.PaintBox2Paint(Sender: TObject);
begin
  PaintTexts(PaintBox2, SHIFTJIS_CHARSET);
end;

In this code, Form1 contains two paintboxes (PaintBox1 and PaintBox2). Font Form1 is set to Courier New, two paintboxes set ParentFont to True. Unicode for Windows is installed in Japanese (Japan), so it works with codepage 932.

Here is the result:

Screenshots of the output

The first paintbox shows the output of DrawTextA and the call to DrawTextW with the Charset property CHARSET_DEFAULT. This is the default value for the font charset property. Note that the Japanese word 日本 does not display correctly when passed to DrawTextA. However, DrawTextW draws great.

, Charset, SHIFTJIS_CHARSET. . !

Form1 Tahoma, DrawTextA DrawTextW .

- , DrawTextA , DrawTextW, -Unicode ​​ , Courier New?

, Ansi- Wide- Windows API , Ansi- Unicode .

Windows XP Windows 7, Delphi 7 Delphi XE2. .

Update: , , Micheal Kaplan. , .

+4
1

DrawTextA . . , API A W.

charset , Unicode, . Unicode- -, Unicode .

, DEFAULT_FONTSET . :

DEFAULT_CHARSET . .

, :

  • GetACP .
  • TranslateCharsetInfo, TCI_SRCCODEPAGE.

- . :

function CharsetFromCP(CP: UINT): UINT;
var
  csi: TCharsetInfo;
begin
  Win32Check(TranslateCharsetInfo(CP, csi, TCI_SRCCODEPAGE));
  Result := csi.ciCharset;
end;

:

aPaintBox.Font.Charset := CharsetFromCP(GetACP);

, , , SHIFTJIS_CHARSET. , , API .

+1

Source: https://habr.com/ru/post/1544171/


All Articles