1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
unit mainunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Graphics, Dialogs, StdCtrls, Grids, IniFiles,
LCLType, LCLIntf, LazUTF8;
type
{ TfrmMain }
TfrmMain = class(TForm)
Button1: TButton;
Button2: TButton;
BtnFontDlg: TButton;
cbCharset: TComboBox;
cbPitch: TComboBox;
chkStrike: TCheckBox;
chkUnderLine: TCheckBox;
FontDialog1: TFontDialog;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
lblCharset: TLabel;
Label6: TLabel;
Sizes: TLabel;
lbFamily: TListBox;
lbStyles: TListBox;
lbSizes: TListBox;
lbCharset: TListBox;
grid: TStringGrid;
procedure BtnFontDlgClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure chkStrikeChange(Sender: TObject);
procedure chkUnderLineChange(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbFamilyClick(Sender: TObject);
procedure lbCharsetClick(Sender: TObject);
procedure lbSizesClick(Sender: TObject);
procedure lbStylesClick(Sender: TObject);
private
FTime: LongWord;
FIniTime: LongWord;
FCurrentFamily,FCurrentStyle,FCurrentSize,FCurrentCharset: string;
procedure StartTimer;
Procedure EndTimer;
function GetCharSet: byte;
function GetPitch: integer;
procedure EnableEvents(Ok: boolean; Lb: TListbox = nil);
procedure SelectFont;
procedure ResetSampleText;
procedure SaveSelection;
procedure RestoreSelection(Sender: TListbox);
procedure LoadFontList;
procedure LoadFamilyFonts(Charset: integer);
procedure UpdateFont(F: TFont);
public
end;
var
frmMain: TfrmMain;
implementation
{.$define Debug}
{$R *.lfm}
{ TfrmMain }
var
LStyles,
LSizes: TStringList;
function EnumFontsNoDups(
var LogFont: TEnumLogFontEx;
var Metric: TNewTextMetricEx;
FontType: Longint;
Data: LParam):LongInt; stdcall;
var
L: TStringList;
S: String;
begin
L := TStringList(ptrint(Data));
S := LogFont.elfLogFont.lfFaceName;
if L.IndexOf(S)<0 then
L.Add(S);
result := 1;
end;
var
NeedTTF: boolean;
function EnumFamilyFonts(
var eLogFont: TEnumLogFontEx;
var Metric:TNewTextMetricEx;
FontType:longint;
Data:LParam):longint; stdcall;
var
s: string;
n: integer;
lcharsets: TStringList;
begin
LCharSets := TStringList(ptrint(Data));
if Lcharsets<>nil then begin
// collect charsets
// when collecting charsets no need to collect all other info
s :=CharSetToString(eLogFont.elfLogFont.lfCharSet);
if LCharsets.indexOf(s)<0 then
LCharsets.AddObject(s, TObject(ptrint(eLogFont.elfLogFont.lfCharSet)));
exit;
end;
// collect styles
s :=eLogFont.elfStyle;
if LStyles.IndexOf(s)<0 then begin
// encode bold, italic
n := eLogFont.elfLogFont.lfItalic;
if eLogFont.elfLogFont.lfWeight > FW_MEDIUM then
n := n or 2;
LStyles.AddObject(eLogFont.elfStyle, TObject(ptrint(n)));
end;
// collect sizes
if FontType=TRUETYPE_FONTTYPE then
NeedTTF := True
else
with metric.ntmentm do
if tmDigitizedAspectY <> 0 then begin
n := (tmHeight-tmInternalLeading)*72+tmDigitizedAspectY shr 1;
n := n div tmDigitizedAspectY;
if n>0 then begin
s := IntToStr(n)+'*'; // font sizes with * indicate raster fonts
if LSizes.IndexOf(s)<0 then
LSizes.AddObject(s, TObject(ptrint(n)));
end;
end;
result := 1;
end;
procedure TfrmMain.BtnFontDlgClick(Sender: TObject);
begin
if FontDialog1.Execute then
UpdateFont(FontDialog1.Font);
end;
procedure TfrmMain.Button1Click(Sender: TObject);
begin
ResetSampleText;
end;
procedure TfrmMain.Button2Click(Sender: TObject);
begin
LoadFontList;
end;
procedure TfrmMain.chkStrikeChange(Sender: TObject);
begin
SelectFont;
end;
procedure TfrmMain.chkUnderLineChange(Sender: TObject);
begin
SelectFont;
end;
procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
var
Ini: TInifile;
begin
SaveSelection;
Ini := TIniFile.Create(UTF8ToSys(ChangeFileExt(Application.ExeName,'.ini')));
try
Ini.WriteString('General','CurrentFamily', FCurrentFamily);
Ini.WriteString('General','CurrentCharset',FCurrentCharset);
Ini.WriteString('General','CurrentStyle', FCurrentStyle);
Ini.WriteString('General','CurrentSize', FCurrentSize);
finally
Ini.Free;
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
procedure Add(Charset: Integer);
begin
cbCharset.Items.AddObject(CharSetToString(CharSet), TObject(ptrint(Charset)));
end;
var
Ini: TIniFile;
begin
// populate cbcharset
cbCharset.Items.clear;
Add(ANSI_CHARSET);
Add(DEFAULT_CHARSET);
Add(SYMBOL_CHARSET);
Add(MAC_CHARSET);
Add(SHIFTJIS_CHARSET);
Add(HANGEUL_CHARSET);
Add(JOHAB_CHARSET);
Add(GB2312_CHARSET);
Add(CHINESEBIG5_CHARSET);
Add(GREEK_CHARSET);
Add(TURKISH_CHARSET);
Add(VIETNAMESE_CHARSET);
Add(HEBREW_CHARSET);
Add(ARABIC_CHARSET);
Add(BALTIC_CHARSET);
Add(RUSSIAN_CHARSET);
Add(THAI_CHARSET);
Add(EASTEUROPE_CHARSET);
Add(OEM_CHARSET);
Add(FCS_ISO_10646_1);
Add(FCS_ISO_8859_1);
Add(FCS_ISO_8859_2);
Add(FCS_ISO_8859_3);
Add(FCS_ISO_8859_4);
Add(FCS_ISO_8859_5);
Add(FCS_ISO_8859_6);
Add(FCS_ISO_8859_7);
Add(FCS_ISO_8859_8);
Add(FCS_ISO_8859_9);
Add(FCS_ISO_8859_10);
Add(FCS_ISO_8859_15);
ResetSampleText;
Ini := TIniFile.Create(UTF8ToSys(ChangeFileExt(Application.ExeName,'.ini')));
try
FCurrentFamily := Ini.ReadString('General','CurrentFamily', '');
FCurrentCharset := Ini.ReadString('General','CurrentCharset','');
FCurrentStyle := Ini.ReadString('General','CurrentStyle', '');
FCurrentSize := Ini.ReadString('General','CurrentSize', '');
finally
Ini.Free;
end;
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
LoadFontlist;
lbCharsetClick(nil);
SelectFont;
end;
procedure TfrmMain.lbFamilyClick(Sender: TObject);
begin
LoadFamilyFonts(-1);
lbCharsetClick(nil);
SelectFont;
end;
procedure TfrmMain.lbCharsetClick(Sender: TObject);
var
i: Integer;
begin
i := lbCharset.ItemIndex;
if i<0 then exit;
i := ptrint(lbCharSet.Items.Objects[i]);
LoadFamilyFonts(byte(i));
end;
procedure TfrmMain.lbSizesClick(Sender: TObject);
begin
SelectFont;
end;
procedure TfrmMain.lbStylesClick(Sender: TObject);
begin
SelectFont;
end;
procedure TfrmMain.StartTimer;
begin
FIniTime := GetTickCount;
end;
procedure TfrmMain.EndTimer;
begin
FTime := GetTickCount-FIniTime;
end;
function TfrmMain.GetCharSet: Byte;
begin
if cbCharSet.Itemindex<0 then
result := ANSI_CHARSET
else
result := byte(ptrint(cbCharset.items.Objects[CbCharset.ItemIndex]));
end;
function TfrmMain.GetPitch: integer;
begin
case cbPitch.ItemIndex of
1: result := FIXED_PITCH;
2: result := VARIABLE_PITCH;
3: result := MONO_FONT;
else
result := DEFAULT_PITCH;
end;
Button2.Caption := IntToStr(result);
end;
procedure TfrmMain.EnableEvents(Ok: boolean; Lb: TListbox = nil);
procedure SetEvent(L: TListbox);
var
Event: TNotifyEvent;
begin
Event := nil;
if ok then begin
if l=lbFamily then Event := @lbFamilyClick else
if l=lbStyles then Event := @LbStylesClick else
if l=lbCharset then Event := @lbCharsetClick else
if l=lbSizes then Event := @lbSizesClick;
end;
L.OnClick := Event;
end;
begin
if Lb<>nil then
SetEvent(Lb)
else begin
SetEvent(lbFamily);
SetEvent(lbStyles);
SetEvent(lbCharset);
SetEvent(lbSizes);
end;
end;
procedure TfrmMain.SelectFont;
var
F: TFont;
i: integer;
function GetFontSize(s: string): Integer;
begin
i := pos('*',s);
if i<>0 then
result := StrToInt(Copy(S, 1, i-1))
else
result := StrToInt(s);
end;
begin
if lbFamily.ItemIndex>=0 then
if lbCharSet.ItemIndex>=0 then
if lbStyles.ItemIndex>=0 then
if lbSizes.ItemIndex>=0 then
begin
F := TFont.Create;
try
F.Name := lbFamily.Items[lbFamily.ItemIndex];
F.CharSet := TFontCharSet(ptrint(lbCharSet.Items.Objects[lbCharset.ItemIndex]));
F.Size := GetFontSize(lbSizes.Items[lbSizes.ItemIndex]);
i := ptrint(lbStyles.Items.Objects[lbStyles.ItemIndex]);
F.Style := [];
if i and 1 <> 0 then F.Style := F.Style + [fsItalic];
if i and 2 <> 0 then F.Style := F.Style + [fsBold];
if chkUnderLine.Checked
then F.Style := F.Style + [fsUnderline]
else F.Style := F.Style - [fsUnderline];
if chkStrike.Checked
then F.Style := F.Style + [fsStrikeOut]
else F.Style := F.Style - [fsStrikeOut];
UpdateFont(F);
SaveSelection;
finally
F.Free;
end;
end;
end;
procedure TfrmMain.ResetSampleText;
var
L: TStringList;
begin
L := TStringList.Create;
L.Add('abcdefhijklmnopqrstuvwxyz');
L.Add('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
L.Add('01234567891 ўЈ¤Ґ§');
L.Add('абвгдежзийклмнопрстуфхцшщъыь');
L.add('АБВГДЕЖЗИЙКЛМНОПРСТУФХХШЩЪЫЬЭЯ');
grid.Cols[0] := L;
l.Free;
end;
procedure TfrmMain.SaveSelection;
function doGet(lb: TListbox): string;
begin
if lb.itemindex>=0 then
result := lb.Items[lb.ItemIndex]
else
result := '';
end;
begin
FCurrentFamily := doGet(LbFamily);
FCurrentCharset := doGet(LbCharset);
FCurrentStyle := doGet(LbStyles);
FCurrentSize := doGet(LbSizes);
end;
procedure TfrmMain.RestoreSelection(Sender: TListbox);
function GetSelection: string;
begin
if Sender.itemindex>=0 then
result := Sender.Items[Sender.ItemIndex]
else
result := '';
end;
function GetCurrent: string;
begin
if Sender=lbFamily then result := FCurrentFamily else
if Sender=lbCharset then result := FCurrentCharset else
if Sender=lbStyles then result := FCurrentStyle else
if Sender=lbSizes then result := FCurrentSize;
end;
var
i: Integer;
s: string;
begin
s := GetCurrent;
if GetSelection <> s then begin
i := Sender.Items.IndexOf(s);
if i>-1 then begin
{$ifdef debug}
debugln('RestoreSelection: listbox=',Sender.Name,' Old=',GetSelection,' New=',S);
{$endif}
if i<>Sender.ItemIndex then
Sender.ItemIndex := i;
end;
end;
end;
procedure TfrmMain.LoadFontList;
var
DC: HDC;
lf: TLogFont;
L: TStringList;
i: Integer;
begin
// this could be have done also with screen.fonts
// but here, we have the list filtered by Charset
lf.lfCharSet := GetCharSet;
lf.lfFaceName := '';
case cbPitch.ItemIndex of
1: i:=FIXED_PITCH;
2: i:=VARIABLE_PITCH;
3: i:=MONO_FONT;
else
i:=DEFAULT_PITCH;
end;
lf.lfPitchAndFamily := i;
{$ifdef debug}
debugln('LoadFontList: for charset=',CharSetToString(lf.lfcharset));
{$endif}
L := TStringList.create;
lbStyles.Clear;
lbCharset.Clear;
lbSizes.Clear;
DC := GetDC(0);
EnableEvents(False, lbFamily);
try
StartTimer;
EnumFontFamiliesEX(DC, @lf, @EnumFontsNoDups, ptrint(L), 0);
EndTimer;
L.Sort;
lbFamily.Items.Assign(L);
lbFamily.Itemindex := -1;
RestoreSelection(lbFamily);
if lbFamily.ItemIndex<0 then begin
if lbFamily.Items.Count>0 then
lbFamily.ItemIndex := 0;
end;
LoadFamilyFonts(-1);
Label4.Caption := format('Fontfaces, found %d, %d ms',[lbFamily.Items.Count, FTime]);
finally
EnableEvents(True, lbFamily);
ReleaseDC(0, DC);
L.Free;
end;
end;
function CompareSizes(List: TStringList; Index1, Index2: Integer): Integer;
begin
result := ptrint(List.Objects[Index1]) - ptrint(List.Objects[Index2]);
end;
procedure TfrmMain.LoadFamilyFonts(Charset: integer);
var
LCharset: TStringList;
dc: HDC;
Lf: TLogFont;
i: LongInt;
LoadingCharsets: boolean;
procedure AddScalableSizes;
procedure Add(Sz: Integer);
begin
if LSizes.IndexOfObject(TObject(ptrint(Sz)))<0 then
LSizes.AddObject(IntToStr(Sz), TObject(ptrint(Sz)));
end;
begin
add(8); add(9); add(10); add(11); add(12); add(14); add(16); add(18);
add(20); add(22); add(24); add(26); add(28); add(36); add(48); add(72);
end;
begin
i := lbFamily.ItemIndex;
if i<0 then exit;
LoadingCharsets := Charset<0;
{$ifdef debug}
Write('LoadFamilyFonts: for family=', lbFamily.Items[i],' and Charset=');
if LoadingCharsets then
debugln('ALL_CHARSETS')
else
debugln(CharsetToString(byte(Charset)));
{$endif}
// at the moment only global fonts are enumerated
// ie. fonts selected in a device context are not enumerated
DC := GetDC(0);
// create global variables, EnumFamilyFonts use them
if LoadingCharsets then begin
// need to fill charset listbox too
LCharset := TStringList.Create;
CharSet := DEFAULT_CHARSET;
end else begin
// charset listbox is already filled, so fill styles and sizes
LCharSet := nil;
LStyles := TStringList.Create;
LSizes := TStringList.Create;
end;
try
// enumerate fonts
Lf.lfFaceName := lbFamily.Items[i];
Lf.lfCharSet := byte(Charset);
Lf.lfPitchAndFamily := 0;
NeedTTF := False;
EnumFontFamiliesEX(DC, @Lf, @EnumFamilyFonts, ptrint(LCharset), 0);
// fill charset listbox if necessary
if LCharset<>nil then begin
LCharset.Sort;
EnableEvents(False, LbCharset);
LbCharset.Items.Assign(LCharset);
LbCharset.ItemIndex := -1;
EnableEvents(true, LbCharset);
end else begin
// fill styles listbox
LStyles.Sort;
EnableEvents(False, LbStyles);
LbStyles.Items.Assign(LStyles);
lbStyles.ItemIndex := -1;
EnableEvents(true, LbStyles);
RestoreSelection(lbStyles);
if lbStyles.ItemIndex<0 then begin
if LbStyles.Items.Count>0 then
LbStyles.ItemIndex := 0;
end;
// fill sizes listbox
// any raster font size is already there
if NeedTTF then
AddScalableSizes;
LSizes.CustomSort(@CompareSizes);
EnableEvents(False, lbSizes);
lbSizes.Items.Assign(LSizes);
lbSizes.ItemIndex := -1;
EnableEvents(true, LbSizes);
RestoreSelection(LbSizes);
if lbSizes.ItemIndex<0 then begin
if lbSizes.Items.Count>0 then
LbSizes.ItemIndex := 0;
end;
end;
finally
if LCharset=nil then begin
LSizes.Free;
LStyles.Free;
end else
LCharset.Free;
releaseDC(0, DC);
end;
if LoadingCharsets then begin
// make an initial charset selection
RestoreSelection(lbCharset);
if lbCharset.ItemIndex<0 then begin
if lbCharset.Items.Count>0 then
lbCharset.ItemIndex := 0;
end;
end;
end;
procedure TfrmMain.UpdateFont(F: TFont);
begin
grid.Font := F;
grid.DefaultRowHeight := grid.canvas.textHeight('Бj') + 5;
end;
end.
|