fpttf_test.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. unit fpttf_test;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils
  6. {$ifdef fptest}
  7. ,TestFramework
  8. {$else}
  9. ,fpcunit, testregistry
  10. {$endif}
  11. ,fpttf
  12. ,fpparsettf
  13. ;
  14. type
  15. TMyTestFPFontCacheItem = class(TFPFontCacheItem)
  16. protected
  17. FFileInfo: TTFFileInfo;
  18. end;
  19. TFPFontCacheItemTest = class(TTestCase)
  20. private
  21. FCacheItem: TMyTestFPFontCacheItem;
  22. procedure SetupRealFont;
  23. protected
  24. procedure SetUp; override;
  25. procedure TearDown; override;
  26. public
  27. property CI: TMyTestFPFontCacheItem read FCacheItem;
  28. published
  29. procedure TestIsRegularCantFind;
  30. procedure TestIsBoldCantFind;
  31. procedure TestIsItalicCantFind;
  32. procedure TestIsFixedWidthCantFind;
  33. procedure TestFileInfoCantFind;
  34. procedure TestIsRegular;
  35. procedure TestIsBold;
  36. procedure TestIsItalic;
  37. procedure TestIsFixedWidth;
  38. procedure TestRegularVsFixedWidth;
  39. procedure TestFileName;
  40. procedure TestFontInfoAfterCreate;
  41. procedure TestTextWidth_FontUnits;
  42. procedure TestTextWidth_Pixels;
  43. end;
  44. TFPFontCacheListTest = class(TTestCase)
  45. private
  46. FFontCacheList: TFPFontCacheList;
  47. protected
  48. procedure SetUp; override;
  49. procedure TearDown; override;
  50. public
  51. property FC: TFPFontCacheList read FFontCacheList;
  52. published
  53. procedure TestCount;
  54. procedure TestBuildFontCache;
  55. procedure TestBuildFontCache_tests_for_bug;
  56. procedure TestClear;
  57. procedure TestFind_FamilyName;
  58. procedure TestFind_PostscriptName;
  59. procedure TestAssignFontList;
  60. procedure TestLoadFromFile;
  61. procedure TestReadStandardFonts;
  62. end;
  63. implementation
  64. const
  65. cFontCount = 5;
  66. resourcestring
  67. cErrFontCountWrong = ' - make sure you only have the 5 test fonts in the "fonts" directory.';
  68. { TFPFontCacheItemTest }
  69. procedure TFPFontCacheItemTest.SetupRealFont;
  70. begin
  71. FCacheItem.Free;
  72. FCacheItem := TMyTestFPFontCacheItem.Create('fonts' + PathDelim + 'DejaVuSans.ttf');
  73. end;
  74. procedure TFPFontCacheItemTest.SetUp;
  75. begin
  76. inherited SetUp;
  77. FCacheItem := TMyTestFPFontCacheItem.Create('mytest.ttf');
  78. end;
  79. procedure TFPFontCacheItemTest.TearDown;
  80. begin
  81. FCacheItem.Free;
  82. inherited TearDown;
  83. end;
  84. procedure TFPFontCacheItemTest.TestIsRegularCantFind;
  85. begin
  86. try
  87. AssertFalse(CI.IsRegular); // this should raise an error
  88. Fail('Failed on 1');
  89. except
  90. on E: Exception do
  91. begin
  92. AssertEquals('Failed on 2', 'ETTF', E.ClassName);
  93. AssertEquals('Failed on 3', 'The font file <mytest.ttf> can''t be found.', E.Message);
  94. end;
  95. end;
  96. end;
  97. procedure TFPFontCacheItemTest.TestIsBoldCantFind;
  98. begin
  99. try
  100. AssertFalse(CI.IsBold); // this should raise an error
  101. Fail('Failed on 1');
  102. except
  103. on E: Exception do
  104. begin
  105. AssertEquals('Failed on 2', 'ETTF', E.ClassName);
  106. AssertEquals('Failed on 3', 'The font file <mytest.ttf> can''t be found.', E.Message);
  107. end;
  108. end;
  109. end;
  110. procedure TFPFontCacheItemTest.TestIsItalicCantFind;
  111. begin
  112. try
  113. AssertFalse(CI.IsItalic); // this should raise an error
  114. Fail('Failed on 1');
  115. except
  116. on E: Exception do
  117. begin
  118. AssertEquals('Failed on 2', 'ETTF', E.ClassName);
  119. AssertEquals('Failed on 3', 'The font file <mytest.ttf> can''t be found.', E.Message);
  120. end;
  121. end;
  122. end;
  123. procedure TFPFontCacheItemTest.TestIsFixedWidthCantFind;
  124. begin
  125. try
  126. AssertFalse(CI.IsFixedWidth); // this should raise an error
  127. Fail('Failed on 1');
  128. except
  129. on E: Exception do
  130. begin
  131. AssertEquals('Failed on 2', 'ETTF', E.ClassName);
  132. AssertEquals('Failed on 3', 'The font file <mytest.ttf> can''t be found.', E.Message);
  133. end;
  134. end;end;
  135. procedure TFPFontCacheItemTest.TestFileInfoCantFind;
  136. begin
  137. try
  138. AssertFalse(CI.FontData <> nil); // this should raise an error
  139. Fail('Failed on 1');
  140. except
  141. on E: Exception do
  142. begin
  143. AssertEquals('Failed on 2', 'ETTF', E.ClassName);
  144. AssertEquals('Failed on 3', 'The font file <mytest.ttf> can''t be found.', E.Message);
  145. end;
  146. end;
  147. end;
  148. procedure TFPFontCacheItemTest.TestIsRegular;
  149. begin
  150. SetupRealFont;
  151. { regular should be the default flag set }
  152. AssertEquals('Failed on 1', True, CI.IsRegular);
  153. end;
  154. procedure TFPFontCacheItemTest.TestIsBold;
  155. begin
  156. SetupRealFont;
  157. AssertEquals('Failed on 1', False, CI.IsBold);
  158. end;
  159. procedure TFPFontCacheItemTest.TestIsItalic;
  160. begin
  161. SetupRealFont;
  162. AssertEquals('Failed on 1', False, CI.IsItalic);
  163. end;
  164. procedure TFPFontCacheItemTest.TestIsFixedWidth;
  165. begin
  166. SetupRealFont;
  167. AssertEquals('Failed on 1', False, CI.IsFixedWidth);
  168. end;
  169. procedure TFPFontCacheItemTest.TestRegularVsFixedWidth;
  170. begin
  171. SetupRealFont;
  172. AssertEquals('Failed on 1', True, CI.IsRegular);
  173. AssertEquals('Failed on 2', False, CI.IsFixedWidth);
  174. end;
  175. procedure TFPFontCacheItemTest.TestFileName;
  176. begin
  177. AssertTrue('Failed on 1', CI.FileName <> '');
  178. { The Filename property doesn't trigger the loading of font info data }
  179. AssertTrue('Failed on 2', CI.FFileInfo = nil);
  180. end;
  181. procedure TFPFontCacheItemTest.TestFontInfoAfterCreate;
  182. begin
  183. { Font info isn't loaded in the constructor any more - it is now loaded on demand }
  184. AssertTrue('Failed on 1', CI.FFileInfo = nil);
  185. end;
  186. procedure TFPFontCacheItemTest.TestTextWidth_FontUnits;
  187. var
  188. lFC: TFPFontCacheList;
  189. lCI: TFPFontCacheItem;
  190. begin
  191. lFC := TFPFontCacheList.Create;
  192. try
  193. lFC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  194. lFC.BuildFontCache;
  195. lCI := lFC.Find('LiberationSans');
  196. AssertEquals('Failed on 1', 14684, round(lCI.TextWidth('Country Ppml01', 0.0)));
  197. lCI := lFC.Find('DejaVuSans');
  198. AssertEquals('Failed on 2', 16492, round(lCI.TextWidth('Country Ppml01', 0.0)));
  199. lCI := lFC.Find('Ubuntu'); // 7333 is the raw glyph width, but with kerning it is 7339
  200. AssertEquals('Failed on 3', 7333, round(lCI.TextWidth('Country Ppml01', 0.0)));
  201. finally
  202. lFC.Free;
  203. end;
  204. end;
  205. procedure TFPFontCacheItemTest.TestTextWidth_Pixels;
  206. var
  207. lFC: TFPFontCacheList;
  208. lCI: TFPFontCacheItem;
  209. px: single;
  210. begin
  211. lFC := TFPFontCacheList.Create;
  212. try
  213. lFC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  214. lFC.BuildFontCache;
  215. lCI := lFC.Find('LiberationSans');
  216. px := 14684 * 10 * 96 / (72 * 2048); // 95.599px
  217. AssertEquals('Failed on 1', px, lCI.TextWidth('Country Ppml01', 10.0));
  218. px := 14684 * 12 * 96 / (72 * 2048); // 114.7188px
  219. AssertEquals('Failed on 2', px, lCI.TextWidth('Country Ppml01', 12.0));
  220. px := 14684 * 24 * 96 / (72 * 2048); // 229.4375px
  221. AssertEquals('Failed on 3', px, lCI.TextWidth('Country Ppml01', 24.0));
  222. lCI := lFC.Find('DejaVuSans');
  223. px := 16492 * 10 * 96 / (72 * 2048); // 107.369px
  224. AssertEquals('Failed on 4', px, lCI.TextWidth('Country Ppml01', 10.0));
  225. px := 16492 * 12 * 96 / (72 * 2048); // 128.8438px
  226. AssertEquals('Failed on 5', px, lCI.TextWidth('Country Ppml01', 12.0));
  227. px := 16492 * 24 * 96 / (72 * 2048); // 205.6875px
  228. AssertEquals('Failed on 6', px, lCI.TextWidth('Country Ppml01', 24.0));
  229. lCI := lFC.Find('Ubuntu');
  230. px := 7333 * 10 * 96 / (72 * 1000); // 97.7733px
  231. AssertEquals('Failed on 7', px, lCI.TextWidth('Country Ppml01', 10.0));
  232. px := 7333 * 12 * 96 / (72 * 1000); // 117.328px
  233. AssertEquals('Failed on 8', px, lCI.TextWidth('Country Ppml01', 12.0));
  234. px := 7333 * 24 * 96 / (72 * 1000); // 234.656px
  235. AssertEquals('Failed on 9', px, lCI.TextWidth('Country Ppml01', 24.0));
  236. finally
  237. lFC.Free;
  238. end;
  239. end;
  240. { TFPFontCacheListTest }
  241. procedure TFPFontCacheListTest.SetUp;
  242. begin
  243. inherited SetUp;
  244. FFontCacheList := TFPFontCacheList.Create;
  245. end;
  246. procedure TFPFontCacheListTest.TearDown;
  247. begin
  248. FFontCacheList.Free;
  249. inherited TearDown;
  250. end;
  251. procedure TFPFontCacheListTest.TestCount;
  252. begin
  253. AssertEquals('Failed on 1', 0, FC.Count);
  254. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  255. AssertEquals('Failed on 2', 0, FC.Count);
  256. FC.BuildFontCache;
  257. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  258. end;
  259. procedure TFPFontCacheListTest.TestBuildFontCache;
  260. begin
  261. AssertEquals('Failed on 1', 0, FC.Count);
  262. try
  263. FC.BuildFontCache;
  264. Fail('Failed on 2. We don''t have font paths, so BuildFontCache shouldn''t run.');
  265. except
  266. on e: Exception do
  267. begin
  268. AssertEquals('Failed on 3', E.ClassName, 'ETTF');
  269. end;
  270. end;
  271. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  272. AssertEquals('Failed on 4', 0, FC.Count);
  273. FC.BuildFontCache;
  274. AssertEquals('Failed on 5' + cErrFontCountWrong, cFontCount, FC.Count);
  275. end;
  276. procedure TFPFontCacheListTest.TestBuildFontCache_tests_for_bug;
  277. begin
  278. AssertEquals('Failed on 1', 0, FC.Count);
  279. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'path_doesnt_exist');
  280. FC.BuildFontCache;
  281. AssertEquals('Failed on 2', 0, FC.Count);
  282. end;
  283. procedure TFPFontCacheListTest.TestClear;
  284. begin
  285. AssertEquals('Failed on 1', 0, FC.Count);
  286. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  287. FC.BuildFontCache;
  288. AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
  289. FC.Clear;
  290. AssertEquals('Failed on 3', 0, FC.Count);
  291. end;
  292. procedure TFPFontCacheListTest.TestFind_FamilyName;
  293. var
  294. lCI: TFPFontCacheItem;
  295. begin
  296. lCI := nil;
  297. AssertEquals('Failed on 1', 0, FC.Count);
  298. lCI := FC.Find('Ubuntu');
  299. AssertTrue('Failed on 2', lCI = nil);
  300. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  301. FC.BuildFontCache;
  302. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  303. lCI := FC.Find('Ubuntu');
  304. AssertTrue('Failed on 4', Assigned(lCI));
  305. { TODO: We should try and extend this to make font paths user configure
  306. thus the tests could be more flexible. }
  307. lCI := FC.Find('Ubuntu', True, False); // bold font
  308. AssertTrue('Failed on 5', lCI = nil);
  309. lCI := FC.Find('Ubuntu', False, True); // italic font
  310. AssertTrue('Failed on 6', lCI = nil);
  311. lCI := FC.Find('Ubuntu', True, True); // bold+italic font
  312. AssertTrue('Failed on 7', lCI = nil);
  313. lCI := FC.Find('DejaVu Sans', False, False);
  314. AssertTrue('Failed on 8', Assigned(lCI));
  315. lCI := FC.Find('DejaVu Sans', True, False);
  316. AssertTrue('Failed on 9', lCI = nil);
  317. end;
  318. procedure TFPFontCacheListTest.TestFind_PostscriptName;
  319. var
  320. lCI: TFPFontCacheItem;
  321. begin
  322. lCI := nil;
  323. AssertEquals('Failed on 1', 0, FC.Count);
  324. lCI := FC.Find('Ubuntu');
  325. AssertTrue('Failed on 2', lCI = nil);
  326. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  327. FC.BuildFontCache;
  328. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  329. lCI := FC.Find('Ubuntu');
  330. AssertTrue('Failed on 4', Assigned(lCI));
  331. { TODO: We should try and extend this to make font paths user configure
  332. thus the tests could be more flexible. }
  333. lCI := FC.Find('Ubuntu-Bold');
  334. AssertTrue('Failed on 5', lCI = nil);
  335. lCI := FC.Find('Ubuntu-Italic');
  336. AssertTrue('Failed on 6', lCI = nil);
  337. lCI := FC.Find('Ubuntu-BoldItalic');
  338. AssertTrue('Failed on 7', lCI = nil);
  339. lCI := FC.Find('DejaVuSans');
  340. AssertTrue('Failed on 8', Assigned(lCI));
  341. lCI := FC.Find('DejaVuSans-Bold');
  342. AssertTrue('Failed on 9', lCI = nil);
  343. end;
  344. procedure TFPFontCacheListTest.TestAssignFontList;
  345. var
  346. sl: TStringList;
  347. begin
  348. sl := TStringList.Create;
  349. try
  350. AssertEquals('Failed on 1', 0, FC.Count);
  351. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  352. FC.BuildFontCache;
  353. AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
  354. FC.AssignFontList(sl);
  355. AssertEquals('Failed on 3', cFontCount, sl.Count);
  356. finally
  357. sl.Free;
  358. end;
  359. end;
  360. { The fontlist file contains 3 font names, instead of the 5 that should
  361. be available. This tests that we only load info of fonts that we need. }
  362. procedure TFPFontCacheListTest.TestLoadFromFile;
  363. const
  364. cFontListFile = 'fontlist.txt';
  365. var
  366. s: string;
  367. lCI: TFPFontCacheItem;
  368. begin
  369. s := ExtractFilePath(ParamStr(0)) + cFontListFile;
  370. AssertEquals('Failed on 1', 0, FC.Count);
  371. FC.LoadFromFile(s);
  372. AssertEquals('Failed on 2', 3, FC.Count);
  373. lCI := FC.Find('DejaVuSans');
  374. AssertTrue('Failed on 3', Assigned(lCI));
  375. lCI := nil;
  376. lCI := FC.Find('FreeSans');
  377. AssertTrue('Failed on 4', Assigned(lCI));
  378. lCI := nil;
  379. lCI := FC.Find('LiberationSans-Italic');
  380. AssertTrue('Failed on 5', Assigned(lCI));
  381. lCI := nil;
  382. end;
  383. procedure TFPFontCacheListTest.TestReadStandardFonts;
  384. begin
  385. AssertEquals('Failed on 1', 0, FC.Count);
  386. FC.ReadStandardFonts;
  387. AssertTrue('Failed on 2', FC.Count > 1);
  388. end;
  389. initialization
  390. RegisterTest({$ifdef fptest}'fpTTF', {$endif}TFPFontCacheItemTest{$ifdef fptest}.Suite{$endif});
  391. RegisterTest({$ifdef fptest}'fpTTF', {$endif}TFPFontCacheListTest{$ifdef fptest}.Suite{$endif});
  392. end.