test_browser_font.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ppapi/tests/test_browser_font.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include "ppapi/cpp/image_data.h"
  8. #include "ppapi/cpp/trusted/browser_font_trusted.h"
  9. #include "ppapi/tests/test_utils.h"
  10. #include "ppapi/tests/testing_instance.h"
  11. REGISTER_TEST_CASE(BrowserFont);
  12. bool TestBrowserFont::Init() {
  13. return true;
  14. }
  15. void TestBrowserFont::RunTests(const std::string& filter) {
  16. RUN_TEST(FontFamilies, filter);
  17. RUN_TEST(Measure, filter);
  18. RUN_TEST(MeasureRTL, filter);
  19. RUN_TEST(CharPos, filter);
  20. // This test is disabled. It doesn't currently pass. See the
  21. // CharacterOffsetForPixel API.
  22. //RUN_TEST(CharPosRTL, filter);
  23. RUN_TEST(Draw, filter);
  24. }
  25. // Just tests that GetFontFamilies is hooked up & returns something.
  26. std::string TestBrowserFont::TestFontFamilies() {
  27. // This function is only supported out-of-process.
  28. const PPB_Testing_Private* testing_interface = GetTestingInterface();
  29. if (testing_interface && !testing_interface->IsOutOfProcess())
  30. PASS();
  31. pp::Var families = pp::BrowserFont_Trusted::GetFontFamilies(instance_);
  32. ASSERT_TRUE(families.is_string());
  33. ASSERT_TRUE(!families.AsString().empty());
  34. PASS();
  35. }
  36. // Tests that measuring text behaves reasonably. We aren't sure if the browser
  37. // will be doing kerning or something for the particular default font, so we
  38. // just make a string that we're pretty sure should be more than twice as long
  39. // as another one, and verify that condition.
  40. std::string TestBrowserFont::TestMeasure() {
  41. pp::BrowserFontDescription desc;
  42. pp::BrowserFont_Trusted font(instance_, desc);
  43. int32_t length1 = font.MeasureText(pp::BrowserFontTextRun("WWW"));
  44. ASSERT_TRUE(length1 > 0);
  45. int32_t length2 = font.MeasureText(pp::BrowserFontTextRun("WWWWWWWW"));
  46. ASSERT_TRUE(length2 >= length1 * 2);
  47. // Fallback font test.
  48. int32_t length3 = font.MeasureText(pp::BrowserFontTextRun("こんにちは"));
  49. ASSERT_TRUE(length3 > 0);
  50. PASS();
  51. }
  52. std::string TestBrowserFont::TestMeasureRTL() {
  53. pp::BrowserFontDescription desc;
  54. pp::BrowserFont_Trusted font(instance_, desc);
  55. // Mixed string, two chars of LTR, two of RTL, then two of LTR.
  56. // Note this is in UTF-8 so has more than 6 bytes.
  57. std::string mixed("AB\xd7\x94\xd7\x97ZZ");
  58. const int kNumChars = 6;
  59. pp::BrowserFontTextRun run(mixed);
  60. // Note that since this is UTF-8, the two RTL chars are two bytes each.
  61. int32_t len[kNumChars];
  62. len[0] = font.PixelOffsetForCharacter(run, 0);
  63. len[1] = font.PixelOffsetForCharacter(run, 1);
  64. len[2] = font.PixelOffsetForCharacter(run, 2);
  65. len[3] = font.PixelOffsetForCharacter(run, 3);
  66. len[4] = font.PixelOffsetForCharacter(run, 4);
  67. len[5] = font.PixelOffsetForCharacter(run, 5);
  68. // First three chars should be increasing.
  69. ASSERT_TRUE(len[0] >= 0);
  70. ASSERT_TRUE(len[1] > len[0]);
  71. ASSERT_TRUE(len[3] > len[1]);
  72. ASSERT_TRUE(len[2] > len[3]);
  73. ASSERT_TRUE(len[4] > len[2]);
  74. ASSERT_TRUE(len[5] > len[4]);
  75. // Test the same sequence with force LTR. The offsets should appear in
  76. // sequence.
  77. pp::BrowserFontTextRun forced_run(mixed, false, true);
  78. len[0] = font.PixelOffsetForCharacter(forced_run, 0);
  79. len[1] = font.PixelOffsetForCharacter(forced_run, 1);
  80. len[2] = font.PixelOffsetForCharacter(forced_run, 2);
  81. len[3] = font.PixelOffsetForCharacter(forced_run, 3);
  82. len[4] = font.PixelOffsetForCharacter(forced_run, 4);
  83. len[5] = font.PixelOffsetForCharacter(forced_run, 5);
  84. for (int i = 1; i < kNumChars; i++)
  85. ASSERT_TRUE(len[i] > len[i - 1]);
  86. PASS();
  87. }
  88. // Tests that the character/pixel offset functions correctly round-trip.
  89. std::string TestBrowserFont::TestCharPos() {
  90. pp::BrowserFontDescription desc;
  91. pp::BrowserFont_Trusted font(instance_, desc);
  92. pp::BrowserFontTextRun run("Hello, world");
  93. uint32_t original_char = 3;
  94. uint32_t pixel_offset = font.PixelOffsetForCharacter(run, original_char);
  95. ASSERT_TRUE(pixel_offset > 0);
  96. uint32_t computed_char = font.CharacterOffsetForPixel(
  97. run, static_cast<int32_t>(pixel_offset));
  98. ASSERT_TRUE(computed_char == original_char);
  99. PASS();
  100. }
  101. // Tests that we can get character positions in a mixed LTR/RTL run.
  102. std::string TestBrowserFont::TestCharPosRTL() {
  103. pp::BrowserFontDescription desc;
  104. pp::BrowserFont_Trusted font(instance_, desc);
  105. // Mixed string, two chars of LTR, two of RTL, than two of LTR.
  106. // Note this is in UTF-8 so has more than 6 bytes.
  107. std::string mixed("AB\xd7\x94\xd7\x97ZZ");
  108. pp::BrowserFontTextRun run(mixed);
  109. static const int kNumChars = 6;
  110. int expected_char_sequence[kNumChars] = { 0, 1, 3, 2, 4, 5 };
  111. // Check that the characters appear in the order we expect.
  112. int pixel_width = font.MeasureText(pp::BrowserFontTextRun(mixed));
  113. int last_sequence = 0; // Index into expected_char_sequence.
  114. for (int x = 0; x < pixel_width; x++) {
  115. int cur_char = font.CharacterOffsetForPixel(run, x);
  116. if (cur_char != expected_char_sequence[last_sequence]) {
  117. // This pixel has a different character. It should be the next one in
  118. // the sequence for it to be correct.
  119. last_sequence++;
  120. ASSERT_TRUE(last_sequence < kNumChars);
  121. ASSERT_TRUE(cur_char == expected_char_sequence[last_sequence]);
  122. }
  123. }
  124. // Try the same string with force LTR. The characters should all appear in
  125. // sequence.
  126. pp::BrowserFontTextRun forced_run(mixed, false, true);
  127. int last_forced_char = 0; // Char index into the forced sequence.
  128. for (int x = 0; x < pixel_width; x++) {
  129. int cur_char = font.CharacterOffsetForPixel(forced_run, x);
  130. if (cur_char != last_forced_char) {
  131. last_forced_char++;
  132. ASSERT_TRUE(cur_char == last_forced_char);
  133. }
  134. }
  135. PASS();
  136. }
  137. // Tests that drawing some text produces "some" output.
  138. std::string TestBrowserFont::TestDraw() {
  139. pp::BrowserFontDescription desc;
  140. desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
  141. desc.set_size(10);
  142. pp::BrowserFont_Trusted font(instance_, desc);
  143. const pp::Size kSize(30, 10);
  144. pp::ImageData image(instance_,
  145. PP_IMAGEDATAFORMAT_BGRA_PREMUL, // 0xAARRGGBB
  146. kSize,
  147. false); // init_to_zero
  148. ASSERT_FALSE(image.is_null());
  149. // Draw black text on white canvas.
  150. memset(image.data(), 0xFF, 4 * kSize.GetArea());
  151. font.DrawSimpleText(&image,
  152. "Hello",
  153. pp::Point(0, 10), // Baseline position.
  154. 0xFF000000, // Black text.
  155. true); // image_data_is_opaque.
  156. // Expect that at least a few pixels are non-white (text).
  157. // Due to blending, there may be rounding errors and
  158. // checking for exact black may not be correct.
  159. // Also expect that all pixels are opaque.
  160. const uint32_t kRGBMask = 0x00FFFFFF;
  161. const uint32_t kAlphaMask = 0xFF000000;
  162. int text_pixels = 0, opaque_pixels = 0;
  163. const uint32_t* pixels = static_cast<const uint32_t*>(image.data());
  164. for (int i = 0; i < kSize.GetArea(); ++i) {
  165. if ((pixels[i] & kRGBMask) != kRGBMask)
  166. ++text_pixels;
  167. if ((pixels[i] & kAlphaMask) == kAlphaMask)
  168. ++opaque_pixels;
  169. }
  170. ASSERT_GT(text_pixels, 0);
  171. ASSERT_EQ(opaque_pixels, kSize.GetArea());
  172. PASS();
  173. }