fpttf_test.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ;
  13. type
  14. TFPFontCacheItemTest = class(TTestCase)
  15. private
  16. FCacheItem: TFPFontCacheItem;
  17. protected
  18. procedure SetUp; override;
  19. procedure TearDown; override;
  20. public
  21. property CI: TFPFontCacheItem read FCacheItem;
  22. published
  23. procedure TestIsRegular;
  24. procedure TestIsBold;
  25. procedure TestIsItalic;
  26. procedure TestIsFixedWidth;
  27. procedure TestRegularVsFixedWidth;
  28. procedure TestFileName;
  29. procedure TestTextWidth_FontUnits;
  30. procedure TestTextWidth_Pixels;
  31. end;
  32. TFPFontCacheListTest = class(TTestCase)
  33. private
  34. FFontCacheList: TFPFontCacheList;
  35. protected
  36. procedure SetUp; override;
  37. procedure TearDown; override;
  38. public
  39. property FC: TFPFontCacheList read FFontCacheList;
  40. published
  41. procedure TestCount;
  42. procedure TestBuildFontCache;
  43. procedure TestBuildFontCache_tests_for_bug;
  44. procedure TestClear;
  45. procedure TestFind_FamilyName;
  46. procedure TestFind_PostscriptName;
  47. procedure TestAssignFontList;
  48. end;
  49. implementation
  50. uses
  51. fpparsettf;
  52. const
  53. cFontCount = 5;
  54. resourcestring
  55. cErrFontCountWrong = ' - make sure you only have the 5 test fonts in the "fonts" directory.';
  56. { TFPFontCacheItemTest }
  57. procedure TFPFontCacheItemTest.SetUp;
  58. begin
  59. inherited SetUp;
  60. FCacheItem := TFPFontCacheItem.Create('mytest.ttf');
  61. end;
  62. procedure TFPFontCacheItemTest.TearDown;
  63. begin
  64. FCacheItem.Free;
  65. inherited TearDown;
  66. end;
  67. procedure TFPFontCacheItemTest.TestIsRegular;
  68. begin
  69. { regular should be the default flag set }
  70. AssertEquals('Failed on 1', True, CI.IsRegular);
  71. end;
  72. procedure TFPFontCacheItemTest.TestIsBold;
  73. begin
  74. AssertEquals('Failed on 1', False, CI.IsBold);
  75. end;
  76. procedure TFPFontCacheItemTest.TestIsItalic;
  77. begin
  78. AssertEquals('Failed on 1', False, CI.IsItalic);
  79. end;
  80. procedure TFPFontCacheItemTest.TestIsFixedWidth;
  81. begin
  82. AssertEquals('Failed on 1', False, CI.IsFixedWidth);
  83. end;
  84. procedure TFPFontCacheItemTest.TestRegularVsFixedWidth;
  85. begin
  86. AssertEquals('Failed on 1', True, CI.IsRegular);
  87. AssertEquals('Failed on 2', False, CI.IsFixedWidth);
  88. end;
  89. procedure TFPFontCacheItemTest.TestFileName;
  90. begin
  91. AssertTrue('Failed on 1', CI.FileName <> '');
  92. { FileName is a non-existing file though, so FontData should be nil }
  93. AssertTrue('Failed on 2', CI.FontData = nil);
  94. end;
  95. procedure TFPFontCacheItemTest.TestTextWidth_FontUnits;
  96. var
  97. lFC: TFPFontCacheList;
  98. lCI: TFPFontCacheItem;
  99. begin
  100. lFC := TFPFontCacheList.Create;
  101. try
  102. lFC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  103. lFC.BuildFontCache;
  104. lCI := lFC.Find('LiberationSans');
  105. AssertEquals('Failed on 1', 14684, round(lCI.TextWidth('Country Ppml01', 0.0)));
  106. lCI := lFC.Find('DejaVuSans');
  107. AssertEquals('Failed on 2', 16492, round(lCI.TextWidth('Country Ppml01', 0.0)));
  108. lCI := lFC.Find('Ubuntu'); // 7333 is the raw glyph width, but with kerning it is 7339
  109. AssertEquals('Failed on 3', 7333, round(lCI.TextWidth('Country Ppml01', 0.0)));
  110. finally
  111. lFC.Free;
  112. end;
  113. end;
  114. procedure TFPFontCacheItemTest.TestTextWidth_Pixels;
  115. var
  116. lFC: TFPFontCacheList;
  117. lCI: TFPFontCacheItem;
  118. px: single;
  119. begin
  120. lFC := TFPFontCacheList.Create;
  121. try
  122. lFC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  123. lFC.BuildFontCache;
  124. lCI := lFC.Find('LiberationSans');
  125. px := 14684 * 10 * 96 / (72 * 2048); // 95.599px
  126. AssertEquals('Failed on 1', px, lCI.TextWidth('Country Ppml01', 10.0));
  127. px := 14684 * 12 * 96 / (72 * 2048); // 114.7188px
  128. AssertEquals('Failed on 2', px, lCI.TextWidth('Country Ppml01', 12.0));
  129. px := 14684 * 24 * 96 / (72 * 2048); // 229.4375px
  130. AssertEquals('Failed on 3', px, lCI.TextWidth('Country Ppml01', 24.0));
  131. lCI := lFC.Find('DejaVuSans');
  132. px := 16492 * 10 * 96 / (72 * 2048); // 107.369px
  133. AssertEquals('Failed on 4', px, lCI.TextWidth('Country Ppml01', 10.0));
  134. px := 16492 * 12 * 96 / (72 * 2048); // 128.8438px
  135. AssertEquals('Failed on 5', px, lCI.TextWidth('Country Ppml01', 12.0));
  136. px := 16492 * 24 * 96 / (72 * 2048); // 205.6875px
  137. AssertEquals('Failed on 6', px, lCI.TextWidth('Country Ppml01', 24.0));
  138. lCI := lFC.Find('Ubuntu');
  139. px := 7333 * 10 * 96 / (72 * 1000); // 97.7733px
  140. AssertEquals('Failed on 7', px, lCI.TextWidth('Country Ppml01', 10.0));
  141. px := 7333 * 12 * 96 / (72 * 1000); // 117.328px
  142. AssertEquals('Failed on 8', px, lCI.TextWidth('Country Ppml01', 12.0));
  143. px := 7333 * 24 * 96 / (72 * 1000); // 234.656px
  144. AssertEquals('Failed on 9', px, lCI.TextWidth('Country Ppml01', 24.0));
  145. finally
  146. lFC.Free;
  147. end;
  148. end;
  149. { TFPFontCacheListTest }
  150. procedure TFPFontCacheListTest.SetUp;
  151. begin
  152. inherited SetUp;
  153. FFontCacheList := TFPFontCacheList.Create;
  154. end;
  155. procedure TFPFontCacheListTest.TearDown;
  156. begin
  157. FFontCacheList.Free;
  158. inherited TearDown;
  159. end;
  160. procedure TFPFontCacheListTest.TestCount;
  161. begin
  162. AssertEquals('Failed on 1', 0, FC.Count);
  163. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  164. AssertEquals('Failed on 2', 0, FC.Count);
  165. FC.BuildFontCache;
  166. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  167. end;
  168. procedure TFPFontCacheListTest.TestBuildFontCache;
  169. begin
  170. AssertEquals('Failed on 1', 0, FC.Count);
  171. try
  172. FC.BuildFontCache;
  173. Fail('Failed on 2. We don''t have font paths, so BuildFontCache shouldn''t run.');
  174. except
  175. on e: Exception do
  176. begin
  177. AssertEquals('Failed on 3', E.ClassName, 'ETTF');
  178. end;
  179. end;
  180. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  181. AssertEquals('Failed on 4', 0, FC.Count);
  182. FC.BuildFontCache;
  183. AssertEquals('Failed on 5' + cErrFontCountWrong, cFontCount, FC.Count);
  184. end;
  185. procedure TFPFontCacheListTest.TestBuildFontCache_tests_for_bug;
  186. begin
  187. AssertEquals('Failed on 1', 0, FC.Count);
  188. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'path_doesnt_exist');
  189. FC.BuildFontCache;
  190. AssertEquals('Failed on 2', 0, FC.Count);
  191. end;
  192. procedure TFPFontCacheListTest.TestClear;
  193. begin
  194. AssertEquals('Failed on 1', 0, FC.Count);
  195. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  196. FC.BuildFontCache;
  197. AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
  198. FC.Clear;
  199. AssertEquals('Failed on 3', 0, FC.Count);
  200. end;
  201. procedure TFPFontCacheListTest.TestFind_FamilyName;
  202. var
  203. lCI: TFPFontCacheItem;
  204. begin
  205. lCI := nil;
  206. AssertEquals('Failed on 1', 0, FC.Count);
  207. lCI := FC.Find('Ubuntu');
  208. AssertTrue('Failed on 2', lCI = nil);
  209. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  210. FC.BuildFontCache;
  211. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  212. lCI := FC.Find('Ubuntu');
  213. AssertTrue('Failed on 4', Assigned(lCI));
  214. { TODO: We should try and extend this to make font paths user configure
  215. thus the tests could be more flexible. }
  216. lCI := FC.Find('Ubuntu', True, False); // bold font
  217. AssertTrue('Failed on 5', lCI = nil);
  218. lCI := FC.Find('Ubuntu', False, True); // italic font
  219. AssertTrue('Failed on 6', lCI = nil);
  220. lCI := FC.Find('Ubuntu', True, True); // bold+italic font
  221. AssertTrue('Failed on 7', lCI = nil);
  222. lCI := FC.Find('DejaVu Sans', False, False);
  223. AssertTrue('Failed on 8', Assigned(lCI));
  224. lCI := FC.Find('DejaVu Sans', True, False);
  225. AssertTrue('Failed on 9', lCI = nil);
  226. end;
  227. procedure TFPFontCacheListTest.TestFind_PostscriptName;
  228. var
  229. lCI: TFPFontCacheItem;
  230. begin
  231. lCI := nil;
  232. AssertEquals('Failed on 1', 0, FC.Count);
  233. lCI := FC.Find('Ubuntu');
  234. AssertTrue('Failed on 2', lCI = nil);
  235. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  236. FC.BuildFontCache;
  237. AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
  238. lCI := FC.Find('Ubuntu');
  239. AssertTrue('Failed on 4', Assigned(lCI));
  240. { TODO: We should try and extend this to make font paths user configure
  241. thus the tests could be more flexible. }
  242. lCI := FC.Find('Ubuntu-Bold');
  243. AssertTrue('Failed on 5', lCI = nil);
  244. lCI := FC.Find('Ubuntu-Italic');
  245. AssertTrue('Failed on 6', lCI = nil);
  246. lCI := FC.Find('Ubuntu-BoldItalic');
  247. AssertTrue('Failed on 7', lCI = nil);
  248. lCI := FC.Find('DejaVuSans');
  249. AssertTrue('Failed on 8', Assigned(lCI));
  250. lCI := FC.Find('DejaVuSans-Bold');
  251. AssertTrue('Failed on 9', lCI = nil);
  252. end;
  253. procedure TFPFontCacheListTest.TestAssignFontList;
  254. var
  255. sl: TStringList;
  256. begin
  257. sl := TStringList.Create;
  258. try
  259. AssertEquals('Failed on 1', 0, FC.Count);
  260. FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
  261. FC.BuildFontCache;
  262. AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
  263. FC.AssignFontList(sl);
  264. AssertEquals('Failed on 3', cFontCount, sl.Count);
  265. finally
  266. sl.Free;
  267. end;
  268. end;
  269. initialization
  270. RegisterTest({$ifdef fptest}'fpTTF', {$endif}TFPFontCacheItemTest{$ifdef fptest}.Suite{$endif});
  271. RegisterTest({$ifdef fptest}'fpTTF', {$endif}TFPFontCacheListTest{$ifdef fptest}.Suite{$endif});
  272. end.