Skip to content

Commit 59a6b09

Browse files
committed
fix: improve Linux font handling
1 parent 04d50c5 commit 59a6b09

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

source/apphelpers.pas

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,13 +3617,22 @@ constructor TAppSettings.Create;
36173617
InitSetting(asLogFileDdl, 'LogFileDdl', 0, False, '', True);
36183618
InitSetting(asLogFileDml, 'LogFileDml', 0, False, '', True);
36193619
InitSetting(asLogFilePath, 'LogFilePath', 0, False, DirnameUserAppData + 'Logs'+PathDelim+'%session'+PathDelim+'%db'+PathDelim+'%y%m%d.sql', True);
3620+
{$IFDEF LINUX}
3621+
// Use generic fontconfig families on Linux.
3622+
InitSetting(asFontName, 'FontName', 0, False, 'monospace');
3623+
{$ELSE}
36203624
if Screen.Fonts.IndexOf('Consolas') > -1 then
36213625
InitSetting(asFontName, 'FontName', 0, False, 'Consolas')
36223626
else
36233627
InitSetting(asFontName, 'FontName', 0, False, 'Courier New');
3628+
{$ENDIF}
36243629
InitSetting(asFontSize, 'FontSize', 9);
36253630
InitSetting(asTabWidth, 'TabWidth', 3);
3631+
{$IFDEF LINUX}
3632+
InitSetting(asDataFontName, 'DataFontName', 0, False, 'sans-serif');
3633+
{$ELSE}
36263634
InitSetting(asDataFontName, 'DataFontName', 0, False, 'Tahoma');
3635+
{$ENDIF}
36273636
InitSetting(asDataFontSize, 'DataFontSize', 8);
36283637
InitSetting(asDataLocalNumberFormat, 'DataLocalNumberFormat', 0, True);
36293638
InitSetting(asLowercaseHex, 'LowercaseHex', 0, True);
@@ -4191,6 +4200,15 @@ function TAppSettings.ReadString(Index: TAppSettingIndex; FormatName: String='';
41914200
B: Boolean;
41924201
begin
41934202
Read(Index, FormatName, adString, I, B, Result, 0, False, Default);
4203+
{$IFDEF LINUX}
4204+
// Keep imported font settings portable.
4205+
if FormatName.IsEmpty then begin
4206+
if (Index = asFontName) and (not SameText(Result, 'monospace')) and (Screen.Fonts.IndexOf(Result) < 0) then
4207+
Result := 'monospace'
4208+
else if (Index = asDataFontName) and (not SameText(Result, 'sans-serif')) and (Screen.Fonts.IndexOf(Result) < 0) then
4209+
Result := 'sans-serif';
4210+
end;
4211+
{$ENDIF}
41944212
end;
41954213

41964214

source/preferences.pas

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,34 @@ procedure TfrmPreferences.Apply(Sender: TObject);
438438
end;
439439

440440

441+
{$IFDEF LINUX}
442+
function EnumMonoFontFamilies(
443+
var LogFont: TEnumLogFontEx;
444+
var Metric: TNewTextMetricEx;
445+
FontType: LongInt;
446+
Data: LParam
447+
): LongInt; stdcall;
448+
var
449+
FontList: TStringList;
450+
FontName: String;
451+
begin
452+
FontList := TStringList(PtrInt(Data));
453+
FontName := LogFont.elfLogFont.lfFaceName;
454+
if not FontName.IsEmpty then
455+
FontList.Add(FontName);
456+
Result := 1;
457+
end;
458+
{$ENDIF}
459+
460+
441461
// List monospace fonts
442462
procedure TfrmPreferences.GetMonoFonts(const AList: TStrings);
463+
{$IFDEF LINUX}
464+
var
465+
DC: HDC;
466+
LogFont: TLogFont;
467+
FontList: TStringList;
468+
{$ELSE}
443469
const
444470
TestChars = 'ilMW 0';
445471
var
@@ -448,8 +474,29 @@ procedure TfrmPreferences.GetMonoFonts(const AList: TStrings);
448474
i: Integer;
449475
w0, w: Integer;
450476
IsMono: Boolean;
477+
{$ENDIF}
451478
begin
452479
AList.Clear;
480+
{$IFDEF LINUX}
481+
FillChar(LogFont, SizeOf(LogFont), 0);
482+
LogFont.lfCharSet := DEFAULT_CHARSET;
483+
LogFont.lfPitchAndFamily := FIXED_PITCH;
484+
485+
FontList := TStringList.Create;
486+
FontList.Sorted := True;
487+
FontList.Duplicates := dupIgnore;
488+
DC := GetDC(0);
489+
try
490+
EnumFontFamiliesEx(DC, @LogFont, @EnumMonoFontFamilies, PtrInt(FontList), 0);
491+
AList.Assign(FontList);
492+
finally
493+
ReleaseDC(0, DC);
494+
FontList.Free;
495+
end;
496+
497+
if AList.IndexOf('monospace') < 0 then
498+
AList.Insert(0, 'monospace');
499+
{$ELSE}
453500
Bmp := TBitmap.Create;
454501
try
455502
for FName in Screen.Fonts do begin
@@ -474,6 +521,7 @@ procedure TfrmPreferences.GetMonoFonts(const AList: TStrings);
474521
finally
475522
Bmp.Free;
476523
end;
524+
{$ENDIF}
477525
end;
478526

479527

@@ -681,6 +729,10 @@ procedure TfrmPreferences.FormShow(Sender: TObject);
681729
if not Screen.Fonts[i].StartsWith('@') then
682730
comboDataFontName.Items.Add(Screen.Fonts[i]);
683731
end;
732+
{$IFDEF LINUX}
733+
if comboDataFontName.Items.IndexOf('sans-serif') < 0 then
734+
comboDataFontName.Items.Insert(0, 'sans-serif');
735+
{$ENDIF}
684736
comboDataFontName.ItemIndex := comboDataFontName.Items.IndexOf(AppSettings.ReadString(asDataFontName));
685737
spinDataFontSize.Value := AppSettings.ReadInt(asDataFontSize);
686738
spinMaxQueryResults.Value := AppSettings.ReadINt(asMaxQueryResults);

0 commit comments

Comments
 (0)