OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/canvas.h" | 5 #include "ui/gfx/canvas.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #import <Cocoa/Cocoa.h> | 9 #import <Cocoa/Cocoa.h> |
10 | 10 |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
15 #include "ui/gfx/font_list.h" | 15 #include "ui/gfx/font_list.h" |
16 | 16 |
17 namespace gfx { | 17 namespace gfx { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Mac-specific code for string size computations. This is a verbatim copy | 21 // Returns the pixel width of the string via calling the native method |
22 // of the old implementation that used to be in canvas_mac.mm. | 22 // sizeWithAttributes. |
Alexei Svitkine (slow)
2013/10/07 23:33:28
Nit: "sizeWithAttributes" -> "-sizeWithAttributes"
jianli
2013/10/08 00:01:18
Done.
| |
23 void CanvasMac_SizeStringInt(const base::string16& text, | 23 float GetStringNativeWidth(const base::string16& text, |
24 const FontList& font_list, | 24 const FontList& font_list) { |
25 int* width, | |
26 int* height, | |
27 int line_height, | |
28 int flags) { | |
29 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; | |
30 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; | |
31 | |
32 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont(); | 25 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont(); |
33 NSString* ns_string = base::SysUTF16ToNSString(text); | 26 NSString* ns_string = base::SysUTF16ToNSString(text); |
34 NSDictionary* attributes = | 27 NSDictionary* attributes = |
35 [NSDictionary dictionaryWithObject:native_font | 28 [NSDictionary dictionaryWithObject:native_font |
36 forKey:NSFontAttributeName]; | 29 forKey:NSFontAttributeName]; |
37 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | 30 return [ns_string sizeWithAttributes:attributes].width; |
38 *width = std::ceil(string_size.width); | |
39 *height = font_list.GetHeight(); | |
40 } | 31 } |
41 | 32 |
42 } // namespace | 33 } // namespace |
43 | 34 |
44 class CanvasTestMac : public testing::Test { | 35 class CanvasTestMac : public testing::Test { |
45 protected: | 36 protected: |
46 // Compare the size returned by Canvas::SizeStringInt to the size generated | 37 // Compare the size returned by Canvas::SizeStringInt to the size generated |
47 // by the platform-specific version in CanvasMac_SizeStringInt. Will generate | 38 // by the platform-specific version in CanvasMac_SizeStringInt. Will generate |
48 // expectation failure on any mismatch. Only works for single-line text | 39 // expectation failure on any mismatch. Only works for single-line text |
49 // without specified line height, since that is all the platform | 40 // without specified line height, since that is all the platform |
50 // implementation supports. | 41 // implementation supports. |
51 void CompareSizes(const char* text) { | 42 void CompareSizes(const char* text) { |
52 const int kReallyLargeNumber = 12345678; | 43 const float kReallyLargeNumber = 12345678; |
53 FontList font_list(font_); | 44 FontList font_list(font_); |
54 base::string16 text16 = base::UTF8ToUTF16(text); | 45 base::string16 text16 = base::UTF8ToUTF16(text); |
55 | 46 |
56 int mac_width = kReallyLargeNumber; | 47 float mac_width = GetStringNativeWidth(text16, font_list); |
57 int mac_height = kReallyLargeNumber; | 48 int mac_height = font_list.GetHeight(); |
58 CanvasMac_SizeStringInt(text16, font_list, &mac_width, &mac_height, 0, 0); | |
59 | 49 |
60 int canvas_width = kReallyLargeNumber; | 50 float canvas_width = kReallyLargeNumber; |
61 int canvas_height = kReallyLargeNumber; | 51 float canvas_height = kReallyLargeNumber; |
62 Canvas::SizeStringInt( | 52 Canvas::SizeStringFloat( |
63 text16, font_list, &canvas_width, &canvas_height, 0, 0); | 53 text16, font_list, &canvas_width, &canvas_height, 0, 0); |
64 | 54 |
65 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text; | 55 EXPECT_NE(kReallyLargeNumber, mac_width) << "no width for " << text; |
66 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text; | 56 EXPECT_NE(kReallyLargeNumber, mac_height) << "no height for " << text; |
67 EXPECT_EQ(mac_width, canvas_width) << " width for " << text; | 57 EXPECT_EQ(mac_width, canvas_width) << " width for " << text; |
68 EXPECT_EQ(mac_height, canvas_height) << " height for " << text; | 58 // FontList::GetHeight returns a truncated height. |
59 EXPECT_EQ(mac_height, | |
60 static_cast<int>(canvas_height)) << " height for " << text; | |
69 } | 61 } |
70 | 62 |
71 private: | 63 private: |
72 Font font_; | 64 Font font_; |
73 }; | 65 }; |
74 | 66 |
75 // Tests that Canvas' SizeStringInt yields result consistent with a native | 67 // Tests that Canvas' SizeStringFloat yields result consistent with a native |
76 // implementation. | 68 // implementation. |
77 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) { | 69 TEST_F(CanvasTestMac, StringSizeIdenticalForSkia) { |
78 CompareSizes(""); | 70 CompareSizes(""); |
79 CompareSizes("Foo"); | 71 CompareSizes("Foo"); |
80 CompareSizes("Longword"); | 72 CompareSizes("Longword"); |
81 CompareSizes("This is a complete sentence."); | 73 CompareSizes("This is a complete sentence."); |
82 } | 74 } |
83 | 75 |
76 TEST_F(CanvasTestMac, FractionalWidth) { | |
77 const float kReallyLargeNumber = 12345678; | |
78 float width = kReallyLargeNumber; | |
79 float height = kReallyLargeNumber; | |
80 | |
81 FontList font_list; | |
82 Canvas::SizeStringFloat( | |
83 base::UTF8ToUTF16("Test"), font_list, &width, &height, 0, 0); | |
84 | |
85 EXPECT_GT(width - static_cast<int>(width), 0); | |
86 } | |
87 | |
84 } // namespace gfx | 88 } // namespace gfx |
OLD | NEW |