fpttf_test.pas 9.0 KB

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