* Some reworking by Graeme Geldenhuys:

ttf: renamed SearchForFont() to SearchForFonts(). Plural makes more sense.
  ttf: extra sanity check before calling SearchForFont()
  ttf: new AssignFontList() method introduced.
       This populates AStrings with a list of found PostScript names. 
       Useful for fpReport and probably a visual report designer too.
  ttf: new overloaded Find() method, and Find-by-PostScript name.
       FamilyName is normally the base font name only. 
       PostScriptName is the base name plus an attribute suffix. 
       eg: Calibri vs Calibri-Bold

git-svn-id: trunk@33563 -
This commit is contained in:
michael 2016-04-27 15:34:00 +00:00
parent c365576841
commit 5ac352bc3c

View File

@ -37,6 +37,7 @@ type
FStyleFlags: TTrueTypeFontStyles; FStyleFlags: TTrueTypeFontStyles;
FFileInfo: TTFFileInfo; FFileInfo: TTFFileInfo;
FOwner: TFPFontCacheList; // reference to FontCacheList that owns this instance FOwner: TFPFontCacheList; // reference to FontCacheList that owns this instance
FPostScriptName: string;
procedure BuildFontCacheItem; procedure BuildFontCacheItem;
procedure SetStyleIfExists(var AText: string; var AStyleFlags: TTrueTypeFontStyles; const AStyleName: String; const AStyle: TTrueTypeFontStyle); procedure SetStyleIfExists(var AText: string; var AStyleFlags: TTrueTypeFontStyles; const AStyleName: String; const AStyle: TTrueTypeFontStyle);
function GetIsBold: boolean; function GetIsBold: boolean;
@ -50,6 +51,7 @@ type
function TextWidth(AStr: utf8string; APointSize: single): single; function TextWidth(AStr: utf8string; APointSize: single): single;
property FileName: String read FFileName; property FileName: String read FFileName;
property FamilyName: String read FFamilyName; property FamilyName: String read FFamilyName;
property PostScriptName: string read FPostScriptName;
property FontData: TTFFileInfo read FFileInfo; property FontData: TTFFileInfo read FFileInfo;
{ A bitmasked value describing the full font style } { A bitmasked value describing the full font style }
property StyleFlags: TTrueTypeFontStyles read FStyleFlags; property StyleFlags: TTrueTypeFontStyles read FStyleFlags;
@ -66,7 +68,7 @@ type
FList: TObjectList; FList: TObjectList;
FSearchPath: TStringList; FSearchPath: TStringList;
FDPI: integer; FDPI: integer;
procedure SearchForFont(const AFontPath: String); procedure SearchForFonts(const AFontPath: String);
procedure SetDPI(AValue: integer); procedure SetDPI(AValue: integer);
protected protected
function GetCount: integer; virtual; function GetCount: integer; virtual;
@ -77,11 +79,13 @@ type
destructor Destroy; override; destructor Destroy; override;
procedure BuildFontCache; procedure BuildFontCache;
function Add(const AObject: TFPFontCacheItem): integer; function Add(const AObject: TFPFontCacheItem): integer;
procedure AssignFontList(const AStrings: TStrings);
procedure Clear; procedure Clear;
property Count: integer read GetCount; property Count: integer read GetCount;
function IndexOf(const AObject: TFPFontCacheItem): integer; function IndexOf(const AObject: TFPFontCacheItem): integer;
function Find(const AFontCacheItem: TFPFontCacheItem): integer; function Find(const AFontCacheItem: TFPFontCacheItem): integer; overload;
function Find(const AFamilyName: string; ABold: boolean = False; AItalic: boolean = False): TFPFontCacheItem; function Find(const AFamilyName: string; ABold: boolean; AItalic: boolean): TFPFontCacheItem; overload;
function Find(const APostScriptName: string): TFPFontCacheItem; overload;
{ not used: utility function doing a conversion for us. } { not used: utility function doing a conversion for us. }
function PointSizeInPixels(const APointSize: single): single; function PointSizeInPixels(const APointSize: single): single;
property Items[AIndex: Integer]: TFPFontCacheItem read GetItem write SetItem; default; property Items[AIndex: Integer]: TFPFontCacheItem read GetItem write SetItem; default;
@ -138,6 +142,7 @@ var
s: string; s: string;
begin begin
s := FFileInfo.PostScriptName; s := FFileInfo.PostScriptName;
FPostScriptName := s;
FFamilyName := FFileInfo.FamilyName; FFamilyName := FFileInfo.FamilyName;
if Pos(s, FFamilyName) = 1 then if Pos(s, FFamilyName) = 1 then
Delete(s, 1, Length(FFamilyName)); Delete(s, 1, Length(FFamilyName));
@ -272,7 +277,7 @@ end;
{ TFPFontCacheList } { TFPFontCacheList }
procedure TFPFontCacheList.SearchForFont(const AFontPath: String); procedure TFPFontCacheList.SearchForFonts(const AFontPath: String);
var var
sr: TSearchRec; sr: TSearchRec;
lFont: TFPFontCacheItem; lFont: TFPFontCacheItem;
@ -287,7 +292,7 @@ begin
// We got something, so lets continue // We got something, so lets continue
s := sr.Name; s := sr.Name;
if (sr.Attr and faDirectory) <> 0 then // found a directory if (sr.Attr and faDirectory) <> 0 then // found a directory
SearchForFont(IncludeTrailingPathDelimiter(AFontPath + s)) SearchForFonts(IncludeTrailingPathDelimiter(AFontPath + s))
else else
begin // we have a file begin // we have a file
if (lowercase(ExtractFileExt(s)) = '.ttf') or if (lowercase(ExtractFileExt(s)) = '.ttf') or
@ -349,7 +354,8 @@ begin
for i := 0 to FSearchPath.Count-1 do for i := 0 to FSearchPath.Count-1 do
begin begin
lPath := FSearchPath[i]; lPath := FSearchPath[i];
SearchForFont(IncludeTrailingPathDelimiter(lPath)); if DirectoryExists(lPath) then
SearchForFonts(IncludeTrailingPathDelimiter(lPath));
end; end;
end; end;
@ -359,6 +365,17 @@ begin
AObject.FOwner := self; AObject.FOwner := self;
end; end;
procedure TFPFontCacheList.AssignFontList(const AStrings: TStrings);
var
i: integer;
begin
if not Assigned(AStrings) then
Exit;
AStrings.Clear;
for i := 0 to FList.Count-1 do
AStrings.Add(TFPFontCacheItem(FList.Items[i]).PostScriptName);
end;
procedure TFPFontCacheList.Clear; procedure TFPFontCacheList.Clear;
begin begin
FList.Clear; FList.Clear;
@ -400,6 +417,19 @@ begin
Result := nil; Result := nil;
end; end;
function TFPFontCacheList.Find(const APostScriptName: string): TFPFontCacheItem;
var
i: integer;
begin
for i := 0 to Count-1 do
begin
Result := Items[i];
if (Result.PostScriptName = APostScriptName) then
Exit;
end;
Result := nil;
end;
function TFPFontCacheList.PointSizeInPixels(const APointSize: single): single; function TFPFontCacheList.PointSizeInPixels(const APointSize: single): single;
begin begin
Result := APointSize * DPI / 72; Result := APointSize * DPI / 72;