TurboPack / SynEdit

SynEdit is a syntax highlighting edit control, not based on the Windows common controls.
216 stars 72 forks source link

Assert parameter code removed in release cause Font not found error #217

Closed iglood closed 1 year ago

iglood commented 2 years ago

Hello all,

In SynDWrite.pas unit, the IsFontMonospacedAndValid() method contain an Assert :

Assert(GetObject(Font.Handle, SizeOf(TLogFont), @LogFont));
CheckOSError(TSynDWrite.GDIInterop.CreateFontFromLOGFONT(LogFont, DWFont));

unfortunately, when compiled in release mode (with asserts disabled), the code in assert parameter is removed and there is an exception on the next line (Font not found or something like that)

When dropping a TSynEdit on a sample project, it doesn't cause error (unless assert is unchecked in options), but in the big project I'm working on, it cause Exception in release mode (I prefer not leaving assert enabled ;-) )

I've corrected like that :

function IsFontMonospacedAndValid(Font: TFont): Boolean;
var
  LogFont: TLogFont;
  DWFont: IDWriteFont;
  IsFontFound : integer;
begin
  try
    IsFontFound := GetObject(Font.Handle, SizeOf(TLogFont), @LogFont);
    Assert(IsFontFound <> 0);
    CheckOSError(TSynDWrite.GDIInterop.CreateFontFromLOGFONT(LogFont, DWFont));
    Result := (DWFont as IDWriteFont1).IsMonospacedFont;
    if (FontFamilyName(DWFont) <> Font.Name) and (fsBold in Font.Style) then
      Font.Style := Font.Style - [fsBold];
  except
    Exit(False);
  end;
end;

Best regards