Is there a way to disable font anti aliasing when using TextRect (aka ExtTextOut in GDI32) in Delphi?
Asked Answered
X

1

4

I'm using a custom gauge, based on the example that came with Delphi (5 Enterprise). For those that don't know, it's like a smooth progress bar, but displays the percentage or value in the centre (vertically and horizontally) of the component.

To make sure the text is readable both when the gauge is filled and when it's empty, the text is displayed using inverted colours.

When font anti-aliasing is used, these inverted colours cause the edge of the font to appear in really crazy colours, ruining the look of the component.

Is there any way to disable font smoothing / anti aliasing for just this one component, or disable it, draw the text, then re-enable it?

My current workaround is to use a font that doesn't get smoothed, like "MS Sans Serif", but I'd like to use the same font as the rest of the UI for consistency.

Xylina answered 15/9, 2010 at 9:23 Comment(3)
Not using subpixel anti-aliasing ruins the component. Redesign!Palinode
Merely inverting the colors don't mean the text will be readable. The inverse of gray is still gray. You have four colors, right? The user can choose the control's background color and progress-bar color, but I'll be you only offer one color setting for text when there clearly need to be two. You need to offer settings for text that appears over the colored progress bar as well as for the text that appears over the background. Let the user choose good contrasting colors. (There are ways of automatically choosing contrasting colors, too.)Camarena
There are no "users" of this component except for myself and one other developer in one solitary application. The gauges are filled with black at present, so there's no need to hugely over-complicate the thing with calculating where the overlap is. There's no need for condescending comments, guys.Xylina
H
14

Specifying NONANTIALIASED_QUALITY in the LOGFONT structure should turn antialiasing off:

procedure SetFontQuality(Font: TFont; Quality: Byte);
var
  LogFont: TLogFont;
begin
  if GetObject(Font.Handle, SizeOf(TLogFont), @LogFont) = 0 then
    RaiseLastOSError;
  LogFont.lfQuality := Quality;
  Font.Handle := CreateFontIndirect(LogFont);
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  FontQualities: array[Boolean] of Byte = (DEFAULT_QUALITY, NONANTIALIASED_QUALITY);
var
  Canvas: TCanvas;
begin
  Canvas := (Sender as TPaintBox).Canvas;
  SetFontQuality(Canvas.Font, FontQualities[CheckBox1.Checked]);
  Canvas.TextOut(12, 12, 'Hello, world!');
end;
Heuristic answered 15/9, 2010 at 11:14 Comment(2)
Cool! The difference is more evident with larger fonts (and a CheckBox1 OnClik event).Latoya
+1. This is indeed the way to disable font smoothing, which is what the question asked for. Too bad font smoothing isn't really Drarok's problem.Camarena

© 2022 - 2024 — McMap. All rights reserved.