OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "core/paint/TextPainter.h" | 5 #include "core/paint/TextPainter.h" |
6 | 6 |
7 #include "core/CSSPropertyNames.h" | 7 #include "core/CSSPropertyNames.h" |
8 #include "core/CSSValueKeywords.h" | 8 #include "core/CSSValueKeywords.h" |
9 #include "core/css/CSSPrimitiveValue.h" | 9 #include "core/css/CSSPrimitiveValue.h" |
10 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
11 #include "core/layout/LayoutTestHelper.h" | 11 #include "core/layout/LayoutTestHelper.h" |
12 #include "core/layout/LayoutText.h" | 12 #include "core/layout/api/LineLayoutText.h" |
13 #include "core/paint/PaintInfo.h" | 13 #include "core/paint/PaintInfo.h" |
14 #include "core/style/ShadowData.h" | 14 #include "core/style/ShadowData.h" |
15 #include "core/style/ShadowList.h" | 15 #include "core/style/ShadowList.h" |
16 #include "platform/graphics/paint/PaintController.h" | 16 #include "platform/graphics/paint/PaintController.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace blink { | 19 namespace blink { |
20 namespace { | 20 namespace { |
21 | 21 |
22 class TextPainterTest : public RenderingTest { | 22 class TextPainterTest : public RenderingTest { |
23 public: | 23 public: |
24 TextPainterTest() | 24 TextPainterTest() |
25 : m_layoutText(nullptr) | 25 : m_layoutText(nullptr) |
26 , m_paintController(PaintController::create()) | 26 , m_paintController(PaintController::create()) |
27 , m_context(*m_paintController) | 27 , m_context(*m_paintController) |
28 { } | 28 { } |
29 | 29 |
30 protected: | 30 protected: |
31 LayoutText* layoutText() { return m_layoutText; } | 31 LineLayoutText lineLayoutText() { return LineLayoutText(m_layoutText); } |
32 | 32 |
33 PaintInfo createPaintInfo(bool usesTextAsClip, bool isPrinting) | 33 PaintInfo createPaintInfo(bool usesTextAsClip, bool isPrinting) |
34 { | 34 { |
35 return PaintInfo(m_context, IntRect(), usesTextAsClip ? PaintPhaseTextCl
ip : PaintPhaseSelfBlockBackgroundOnly, isPrinting ? GlobalPaintPrinting : Globa
lPaintNormalPhase, 0); | 35 return PaintInfo(m_context, IntRect(), usesTextAsClip ? PaintPhaseTextCl
ip : PaintPhaseSelfBlockBackgroundOnly, isPrinting ? GlobalPaintPrinting : Globa
lPaintNormalPhase, 0); |
36 } | 36 } |
37 | 37 |
38 private: | 38 private: |
39 void SetUp() override | 39 void SetUp() override |
40 { | 40 { |
41 RenderingTest::SetUp(); | 41 RenderingTest::SetUp(); |
42 setBodyInnerHTML("Hello world"); | 42 setBodyInnerHTML("Hello world"); |
43 m_layoutText = toLayoutText(document().body()->firstChild()->layoutObjec
t()); | 43 m_layoutText = toLayoutText(document().body()->firstChild()->layoutObjec
t()); |
44 ASSERT_TRUE(m_layoutText); | 44 ASSERT_TRUE(m_layoutText); |
45 ASSERT_EQ("Hello world", m_layoutText->text()); | 45 ASSERT_EQ("Hello world", m_layoutText->text()); |
46 } | 46 } |
47 | 47 |
48 LayoutText* m_layoutText; | 48 LayoutText* m_layoutText; |
49 OwnPtr<PaintController> m_paintController; | 49 OwnPtr<PaintController> m_paintController; |
50 GraphicsContext m_context; | 50 GraphicsContext m_context; |
51 }; | 51 }; |
52 | 52 |
53 TEST_F(TextPainterTest, TextPaintingStyle_Simple) | 53 TEST_F(TextPainterTest, TextPaintingStyle_Simple) |
54 { | 54 { |
55 document().body()->setInlineStyleProperty(CSSPropertyColor, CSSValueBlue); | 55 document().body()->setInlineStyleProperty(CSSPropertyColor, CSSValueBlue); |
56 document().view()->updateAllLifecyclePhases(); | 56 document().view()->updateAllLifecyclePhases(); |
57 | 57 |
58 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 58 TextPainter::Style textStyle = TextPainter::textPaintingStyle( |
59 *layoutText(), layoutText()->styleRef(), createPaintInfo(false /* usesTe
xtAsClip */, false /* isPrinting */)); | 59 lineLayoutText(), lineLayoutText().styleRef(), createPaintInfo(false /*
usesTextAsClip */, false /* isPrinting */)); |
60 EXPECT_EQ(Color(0, 0, 255), textStyle.fillColor); | 60 EXPECT_EQ(Color(0, 0, 255), textStyle.fillColor); |
61 EXPECT_EQ(Color(0, 0, 255), textStyle.strokeColor); | 61 EXPECT_EQ(Color(0, 0, 255), textStyle.strokeColor); |
62 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | 62 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); |
63 EXPECT_EQ(0, textStyle.strokeWidth); | 63 EXPECT_EQ(0, textStyle.strokeWidth); |
64 EXPECT_EQ(nullptr, textStyle.shadow); | 64 EXPECT_EQ(nullptr, textStyle.shadow); |
65 } | 65 } |
66 | 66 |
67 TEST_F(TextPainterTest, TextPaintingStyle_AllProperties) | 67 TEST_F(TextPainterTest, TextPaintingStyle_AllProperties) |
68 { | 68 { |
69 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | 69 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); |
70 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | 70 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); |
71 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | 71 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); |
72 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); | 72 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); |
73 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); | 73 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); |
74 document().view()->updateAllLifecyclePhases(); | 74 document().view()->updateAllLifecyclePhases(); |
75 | 75 |
76 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 76 TextPainter::Style textStyle = TextPainter::textPaintingStyle( |
77 *layoutText(), layoutText()->styleRef(), createPaintInfo(false /* usesTe
xtAsClip */, false /* isPrinting */)); | 77 lineLayoutText(), lineLayoutText().styleRef(), createPaintInfo(false /*
usesTextAsClip */, false /* isPrinting */)); |
78 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); | 78 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); |
79 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); | 79 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); |
80 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | 80 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); |
81 EXPECT_EQ(4, textStyle.strokeWidth); | 81 EXPECT_EQ(4, textStyle.strokeWidth); |
82 ASSERT_NE(nullptr, textStyle.shadow); | 82 ASSERT_NE(nullptr, textStyle.shadow); |
83 EXPECT_EQ(1u, textStyle.shadow->shadows().size()); | 83 EXPECT_EQ(1u, textStyle.shadow->shadows().size()); |
84 EXPECT_EQ(1, textStyle.shadow->shadows()[0].x()); | 84 EXPECT_EQ(1, textStyle.shadow->shadows()[0].x()); |
85 EXPECT_EQ(2, textStyle.shadow->shadows()[0].y()); | 85 EXPECT_EQ(2, textStyle.shadow->shadows()[0].y()); |
86 EXPECT_EQ(3, textStyle.shadow->shadows()[0].blur()); | 86 EXPECT_EQ(3, textStyle.shadow->shadows()[0].blur()); |
87 EXPECT_EQ(Color(255, 255, 0), textStyle.shadow->shadows()[0].color().color()
); | 87 EXPECT_EQ(Color(255, 255, 0), textStyle.shadow->shadows()[0].color().color()
); |
88 } | 88 } |
89 | 89 |
90 TEST_F(TextPainterTest, TextPaintingStyle_UsesTextAsClip) | 90 TEST_F(TextPainterTest, TextPaintingStyle_UsesTextAsClip) |
91 { | 91 { |
92 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | 92 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); |
93 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | 93 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); |
94 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | 94 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); |
95 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); | 95 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); |
96 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); | 96 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); |
97 document().view()->updateAllLifecyclePhases(); | 97 document().view()->updateAllLifecyclePhases(); |
98 | 98 |
99 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 99 TextPainter::Style textStyle = TextPainter::textPaintingStyle( |
100 *layoutText(), layoutText()->styleRef(), createPaintInfo(true /* usesTex
tAsClip */, false /* isPrinting */)); | 100 lineLayoutText(), lineLayoutText().styleRef(), createPaintInfo(true /* u
sesTextAsClip */, false /* isPrinting */)); |
101 EXPECT_EQ(Color::black, textStyle.fillColor); | 101 EXPECT_EQ(Color::black, textStyle.fillColor); |
102 EXPECT_EQ(Color::black, textStyle.strokeColor); | 102 EXPECT_EQ(Color::black, textStyle.strokeColor); |
103 EXPECT_EQ(Color::black, textStyle.emphasisMarkColor); | 103 EXPECT_EQ(Color::black, textStyle.emphasisMarkColor); |
104 EXPECT_EQ(4, textStyle.strokeWidth); | 104 EXPECT_EQ(4, textStyle.strokeWidth); |
105 EXPECT_EQ(nullptr, textStyle.shadow); | 105 EXPECT_EQ(nullptr, textStyle.shadow); |
106 } | 106 } |
107 | 107 |
108 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_NoAdjustmentNee
ded) | 108 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_NoAdjustmentNee
ded) |
109 { | 109 { |
110 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | 110 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); |
111 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | 111 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); |
112 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | 112 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); |
113 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); | 113 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); |
114 document().settings()->setShouldPrintBackgrounds(false); | 114 document().settings()->setShouldPrintBackgrounds(false); |
115 document().setPrinting(true); | 115 document().setPrinting(true); |
116 document().view()->updateAllLifecyclePhases(); | 116 document().view()->updateAllLifecyclePhases(); |
117 | 117 |
118 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 118 TextPainter::Style textStyle = TextPainter::textPaintingStyle( |
119 *layoutText(), layoutText()->styleRef(), createPaintInfo(false /* usesTe
xtAsClip */, true /* isPrinting */)); | 119 lineLayoutText(), lineLayoutText().styleRef(), createPaintInfo(false /*
usesTextAsClip */, true /* isPrinting */)); |
120 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); | 120 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); |
121 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); | 121 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); |
122 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | 122 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); |
123 } | 123 } |
124 | 124 |
125 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_Darkened) | 125 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_Darkened) |
126 { | 126 { |
127 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, "r
gb(255, 220, 220)"); | 127 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, "r
gb(255, 220, 220)"); |
128 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
"rgb(220, 255, 220)"); | 128 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
"rgb(220, 255, 220)"); |
129 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, "rgb(220, 220, 255)"); | 129 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, "rgb(220, 220, 255)"); |
130 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); | 130 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); |
131 document().settings()->setShouldPrintBackgrounds(false); | 131 document().settings()->setShouldPrintBackgrounds(false); |
132 document().setPrinting(true); | 132 document().setPrinting(true); |
133 document().view()->updateAllLifecyclePhases(); | 133 document().view()->updateAllLifecyclePhases(); |
134 | 134 |
135 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 135 TextPainter::Style textStyle = TextPainter::textPaintingStyle( |
136 *layoutText(), layoutText()->styleRef(), createPaintInfo(false /* usesTe
xtAsClip */, true /* isPrinting */)); | 136 lineLayoutText(), lineLayoutText().styleRef(), createPaintInfo(false /*
usesTextAsClip */, true /* isPrinting */)); |
137 EXPECT_EQ(Color(255, 220, 220).dark(), textStyle.fillColor); | 137 EXPECT_EQ(Color(255, 220, 220).dark(), textStyle.fillColor); |
138 EXPECT_EQ(Color(220, 255, 220).dark(), textStyle.strokeColor); | 138 EXPECT_EQ(Color(220, 255, 220).dark(), textStyle.strokeColor); |
139 EXPECT_EQ(Color(220, 220, 255).dark(), textStyle.emphasisMarkColor); | 139 EXPECT_EQ(Color(220, 220, 255).dark(), textStyle.emphasisMarkColor); |
140 } | 140 } |
141 | 141 |
142 } // namespace | 142 } // namespace |
143 } // namespace blink | 143 } // namespace blink |
OLD | NEW |