Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Something wrong in text drawing (https://www.delphipraxis.net/162390-something-wrong-text-drawing.html)

WojTec 20. Aug 2011 14:35

Delphi-Version: 2010

Something wrong in text drawing
 
Liste der Anhänge anzeigen (Anzahl: 1)
I'm using TCanvas.TextOut() to output text on bitmap. Attached image better describe problem (please zoom it).

Simply, I want to text in pen color (green) without this terrible effect (red).

Fusel 20. Aug 2011 15:07

AW: Something wrong in text drawing
 
Set your specific Fontname with TCanvas.Font

WojTec 20. Aug 2011 15:26

Re: Something wrong in text drawing
 
I already did it, Arial. Do you mean problem is with font?

Namenloser 20. Aug 2011 17:35

AW: Something wrong in text drawing
 
It has nothing to do with the font. It’s just windows’ subpixel rendering (ClearType) which smoothes out the edges of your text and makes it appear more defined. This technology was introduced with Win XP and is turned on globally by default since Vista.

You can however disable ClearType for a specific canvas.

WojTec 20. Aug 2011 18:44

Re: Something wrong in text drawing
 
Hm, for me don't working, ClearType is as was.

Namenloser 20. Aug 2011 20:34

AW: Something wrong in text drawing
 
It should work, though. Post some code, please.

WojTec 21. Aug 2011 10:37

Re: Something wrong in text drawing
 
Delphi-Quellcode:
begin
  with TBitmap.Create do
  begin
    SetSize(100, 100);

    Canvas.Brush.Color := clWhite;
    Canvas.Pen.Color := clBlack;

    Canvas.FillRect(BoundsRect);

    Canvas.Font.Name := 'Arial';
    Canvas.Font.Size := 36;
    ChangeCleartype(Canvas, False);

    Canvas.TextOut(10, 10, '123'); // Without CT

    Canvas.Font.Size := 20;
    Canvas.TextOut(10, 60, '1234567'); // With CT

    SaveToFile('C:\0.bmp');
  end;
end;
I know!, need to call it before render text and after last setting! Ach.
Is possible to set it permanently for specified font?

himitsu 21. Aug 2011 11:19

AW: Something wrong in text drawing
 
Ja.

Leite dir TFont ab und baue diese Funktionalität dort ein.

WojTec 21. Aug 2011 11:35

Re: Something wrong in text drawing
 
I don't understand too much, you mean I should write new class with this function inside? I alread made helper class, but need to set property before render text.

I see Delphi disallow add fields in helpers, very bad :(

If I create extended font class TFontEx = class(TFont), how to use it in canvas?

DeddyH 21. Aug 2011 12:35

AW: Something wrong in text drawing
 
I tried a little bit and this seems to work (only a form with a button on it):
Delphi-Quellcode:
unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFont = class(Graphics.TFont)
  private
    procedure SetClearType(Value: Boolean);
    function GetClearType: Boolean;
  public
    property ClearType: Boolean read GetClearType write SetClearType;
  end;

  TfrmFontTest = class(TForm)
    Button1: TButton;
    procedure FormPaint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    FClearType: Boolean;
    procedure ToggleClearType;
  public
    { Public-Deklarationen }
  end;

var
  frmFontTest: TfrmFontTest;

implementation

{$R *.dfm}
{ TFont }

function TFont.GetClearType: Boolean;
var
  lf: TLogFont;
begin
  GetObject(Handle, sizeof(lf), @lf);
  Result := lf.lfQuality = DEFAULT_QUALITY;
end;

procedure TFont.SetClearType(Value: Boolean);
var
  lf: TLogFont;
begin
  GetObject(Handle, sizeof(lf), @lf);
  if Value then
    lf.lfQuality := DEFAULT_QUALITY
  else
    lf.lfQuality := NONANTIALIASED_QUALITY;
  Handle := CreateFontIndirect(lf);
end;

procedure TfrmFontTest.Button1Click(Sender: TObject);
begin
  ToggleClearType;
end;

procedure TfrmFontTest.ToggleClearType;
var
  ft: TFont;
begin
  FClearType := not FClearType;
  ft := TFont.Create;
  try
    ft.Assign(Canvas.Font);
    ft.ClearType := FClearType;
    Canvas.Font.Assign(ft);
    Invalidate;
  finally
    ft.Free;
  end;
end;

procedure TfrmFontTest.FormCreate(Sender: TObject);
begin
  FClearType := true;
end;

procedure TfrmFontTest.FormPaint(Sender: TObject);
begin
  Canvas.TextOut(10, 10, 'And now for something completely different...');
end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:05 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz