summaryrefslogtreecommitdiff
path: root/src/corelib/render/software/fpg_fontcache.pas
blob: 3de3a7f66980b7593556116da61667fb87810b5a (plain)
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
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2013 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      This is a homegrown font cache, or font translation system. AggPas
      references font files (eg: *.ttf) directly, whereas the rest
      of fpGUI doesn't. Under X11 for example, the translation of
      'Aria-12' to the actual *.ttf file will be done by the fontconfig
      library. Unfortunately fontconfig doesn't have an API to give
      use that *.ttf font file it resolved too. So for AggPas (or rather
      the AggPas backend in fpGUI) we had to implement our own
      font translation system.
}

unit fpg_fontcache;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, contnrs, fpg_base;

type
  TFontCacheItem = class(TObject)
  private
    FAngle: double;
    FFamilyName: TfpgString;
    FFileName: TfpgString;
    FFixedWidth: boolean;
    FStyleFlags: Integer;
    function    GetIsBold: boolean;
    function    GetIsFixedWidth: boolean;
    function    GetIsItalic: boolean;
    function    GetIsRegular: boolean;
    procedure   SetIsBold(AValue: boolean);
    procedure   SetIsFixedWidth(AValue: boolean);
    procedure   SetIsItalic(AValue: boolean);
    procedure   SetIsRegular(AValue: boolean);
  public
    constructor Create(const AFilename: TfpgString);
    property    FileName: TfpgString read FFileName write FFileName;
    property    FamilyName: TfpgString read FFamilyName write FFamilyName;
    property    StyleFlags: Integer read FStyleFlags write FStyleFlags;
    property    IsFixedWidth: boolean read GetIsFixedWidth write SetIsFixedWidth;
    property    IsRegular: boolean read GetIsRegular write SetIsRegular;
    property    IsItalic: boolean read GetIsItalic write SetIsItalic;
    property    IsBold: boolean read GetIsBold write SetIsBold;
    { following properties are used by FontCacheItemFromFontDesc() only }
    property    Angle: double read FAngle write FAngle;
  end;


  TFontCacheList = class(TObject)
  private
    FList: TObjectList;
    procedure   SearchForFont(const AFontPath: TfpgString);
    function    BuildFontCacheItem(const AFontFile: TfpgString): TFontCacheItem;
    procedure   SetStyleIfExists(var AText: Ansistring; var AStyleFlags: integer; const AStyleName: AnsiString; const AStyleBit: integer);
  protected
    function    GetCount: integer; virtual;
    function    GetItem(AIndex: Integer): TFontCacheItem; virtual;
    procedure   SetItem(AIndex: Integer; AValue: TFontCacheItem); virtual;
  public
    constructor Create;
    destructor  Destroy; override;
    procedure   BuildFontCache;
    function    Add(const AObject: TFontCacheItem): integer;
    procedure   Clear;
    property    Count: integer read GetCount;
    function    IndexOf(const AObject: TFontCacheItem): integer;
    function    Find(const AFontCacheItem: TFontCacheItem): integer;
    property    Items[AIndex: Integer]: TFontCacheItem read GetItem write SetItem; default;
  end;


function gFontCache: TFontCacheList;

implementation

uses
  fpg_utils,
  agg_font_freetype_lib;

const
  FPG_FONT_STYLE_REGULAR = 1 shl 0;     { Regular, Plain, Book }
  FPG_FONT_STYLE_ITALIC = 1 shl 1;      { Itelic }
  FPG_FONT_STYLE_BOLD = 1 shl 2;        { Bold }
  FPG_FONT_STYLE_CONDENSED = 1 shl 3;   { Condensed }
  FPG_FONT_STYLE_EXTRALIGHT = 1 shl 4;  { ExtraLight }
  FPG_FONT_STYLE_LIGHT = 1 shl 5;       { Light }
  FPG_FONT_STYLE_SEMIBOLD = 1 shl 6;    { Semibold }
  FPG_FONT_STYLE_MEDIUM = 1 shl 7;      { Medium }
  FPG_FONT_STYLE_BLACK = 1 shl 8;       { Black }
  FPG_FONT_STYLE_FIXEDWIDTH = 1 shl 9;  { Fixedwidth }

var
  m_library: FT_Library_ptr;
  uFontCacheList: TFontCacheList;

function gFontCache: TFontCacheList;
begin
 if not Assigned(uFontCacheList) then
 begin
   uFontCacheList := TFontCacheList.Create;
   uFontCacheList.BuildFontCache;
 end;
 Result := uFontCacheList;
end;

{ TFontCacheItem }

function TFontCacheItem.GetIsBold: boolean;
begin
  Result := (FStyleFlags and FPG_FONT_STYLE_BOLD) <> 0;
end;

function TFontCacheItem.GetIsFixedWidth: boolean;
begin
  Result := (FStyleFlags and FPG_FONT_STYLE_FIXEDWIDTH) <> 0;
end;

function TFontCacheItem.GetIsItalic: boolean;
begin
  Result := (FStyleFlags and FPG_FONT_STYLE_ITALIC) <> 0;
end;

function TFontCacheItem.GetIsRegular: boolean;
begin
  Result := (FStyleFlags and FPG_FONT_STYLE_REGULAR) <> 0;
end;

procedure TFontCacheItem.SetIsBold(AValue: boolean);
begin
  FStyleFlags := FStyleFlags or FPG_FONT_STYLE_BOLD;
end;

procedure TFontCacheItem.SetIsFixedWidth(AValue: boolean);
begin
  FStyleFlags := FStyleFlags or FPG_FONT_STYLE_FIXEDWIDTH;
  FStyleFlags := FStyleFlags and (not FPG_FONT_STYLE_REGULAR);
end;

procedure TFontCacheItem.SetIsItalic(AValue: boolean);
begin
  FStyleFlags := FStyleFlags or FPG_FONT_STYLE_ITALIC;
end;

procedure TFontCacheItem.SetIsRegular(AValue: boolean);
begin
  FStyleFlags := FStyleFlags or FPG_FONT_STYLE_REGULAR;
  FStyleFlags := FStyleFlags and (not FPG_FONT_STYLE_FIXEDWIDTH);
end;

constructor TFontCacheItem.Create(const AFilename: TfpgString);
begin
  inherited Create;
  FFileName := AFilename;
  FStyleFlags := FPG_FONT_STYLE_REGULAR;
  FAngle := 0.0;
end;

{ TFontCacheList }

procedure TFontCacheList.SearchForFont(const AFontPath: TfpgString);
var
  sr: TSearchRec;
  lFont: TFontCacheItem;
  s: TfpgString;
begin
  // The extra 'or' includes Normal attribute files under Windows. faAnyFile doesn't return those.
  // Reported to FPC as bug 9440 in Mantis.
  if fpgFindFirst(AFontPath + AllFilesMask, faAnyFile or $00000080, sr) = 0 then
  begin
    repeat
      // check if special files to skip
      if (sr.Name = '.') or (sr.Name = '..') or (sr.Name = '') then
        Continue;
      // We got something, so lets continue
      s := fpgFromOSEncoding(sr.Name);
      if (sr.Attr and faDirectory) <> 0 then // found a directory
        SearchForFont(fpgAppendPathDelim(AFontPath + s))
      else
      begin // we have a file
        if (lowercase(fpgExtractFileExt(s)) = '.ttf') or
           (lowercase(fpgExtractFileExt(s)) = '.otf') then
        begin
          lFont := BuildFontCacheItem(AFontPath + s);
          Add(lFont);
        end;
      end;
    until fpgFindNext(sr) <> 0;
  end;
end;

function TFontCacheList.BuildFontCacheItem(const AFontFile: TfpgString): TFontCacheItem;
var
  face_ptr: FT_Face_ptr;
  s: Ansistring;
  flags: integer;
begin
  FT_New_Face(m_library, PChar(AFontFile), 0, face_ptr);
  Result := TFontCacheItem.Create(AFontFile);
  Result.FamilyName := face_ptr^.family_name;

  // extract simple styles first
//  if (face_ptr^.face_flags and FT_FACE_FLAG_FIXED_WIDTH) <> 0 then
//    Result.StyleFlags := FPG_FONT_STYLE_FIXEDWIDTH; // this should overwrite Regular style

  if (face_ptr^.style_flags and FT_STYLE_FLAG_ITALIC) <> 0 then
    Result.StyleFlags := Result.StyleFlags or FPG_FONT_STYLE_ITALIC;

  if (face_ptr^.style_flags and FT_STYLE_FLAG_BOLD) <> 0 then
    Result.StyleFlags := Result.StyleFlags or FPG_FONT_STYLE_BOLD;

  // Now to more complex styles stored in StyleName field. eg: 'Condensed Medium'
  s := face_ptr^.style_name;
  flags := Result.StyleFlags;
  SetStyleIfExists(s, flags, 'Condensed', FPG_FONT_STYLE_CONDENSED);
  SetStyleIfExists(s, flags, 'ExtraLight', FPG_FONT_STYLE_EXTRALIGHT);
  SetStyleIfExists(s, flags, 'Light', FPG_FONT_STYLE_LIGHT);
  SetStyleIfExists(s, flags, 'Semibold', FPG_FONT_STYLE_SEMIBOLD);
  SetStyleIfExists(s, flags, 'Medium', FPG_FONT_STYLE_MEDIUM);
  SetStyleIfExists(s, flags, 'Black', FPG_FONT_STYLE_BLACK);
  Result.StyleFlags := flags;

  FT_Done_Face(face_ptr);
end;

procedure TFontCacheList.SetStyleIfExists(var AText: Ansistring; var AStyleFlags: integer;
  const AStyleName: AnsiString; const AStyleBit: integer);
var
  i: integer;
begin
  i := Pos(AStyleName, AText);
  if i > 0 then
  begin
    AStyleFlags := AStyleFlags or AStyleBit;
    Delete(AText, Length(AStyleName), i);
  end;
end;

function TFontCacheList.GetCount: integer;
begin
  Result := FList.Count;
end;

function TFontCacheList.GetItem(AIndex: Integer): TFontCacheItem;
begin
  Result := TFontCacheItem(FList.Items[AIndex]);
end;

procedure TFontCacheList.SetItem(AIndex: Integer; AValue: TFontCacheItem);
begin
  FList.Items[AIndex] := AValue;
end;

constructor TFontCacheList.Create;
begin
  inherited Create;
  FList := TObjectList.Create;
end;

destructor TFontCacheList.Destroy;
begin
  FList.Free;
  inherited Destroy;
end;

procedure TFontCacheList.BuildFontCache;
var
  lPath: TfpgString;
  lPathList: TStringList;
  i: integer;
begin
  try
    m_library := nil;
    FT_Init_FreeType(m_library);

    lPathList := TStringList.Create;
    lPathList.Add('/usr/share/cups/fonts/');
    lPathList.Add('/usr/share/fonts/truetype/');
    lPathList.Add('/usr/local/lib/X11/fonts/');
    lPathList.Add(GetUserDir + '.fonts/');
    for i := 0 to lPathList.Count-1 do
    begin
      lPath := lPathList[i];
      SearchForFont(lPath);
    end;
  finally
    FT_Done_FreeType(m_library);
    m_library := nil;
    lPathList.Free;
  end;
end;

function TFontCacheList.Add(const AObject: TFontCacheItem): integer;
begin
  Result := FList.Add(AObject);
end;

procedure TFontCacheList.Clear;
begin
  FList.Clear;
end;

function TFontCacheList.IndexOf(const AObject: TFontCacheItem): integer;
begin
  Result := FList.IndexOf(AObject);
end;

function TFontCacheList.Find(const AFontCacheItem: TFontCacheItem): integer;
var
  i: integer;
begin
  Result := -1; // nothing found
  for i := 0 to Count-1 do
  begin
    if (Items[i].FamilyName = AFontCacheItem.FamilyName) and
       (Items[i].StyleFlags = AFontCacheItem.StyleFlags) then
    begin
      Result := i;
      exit;
    end;
  end;
end;


initialization
  uFontCacheList := nil;

finalization
  uFontCacheList.Free;

end.