fpttf_test.pas 8.7 KB

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