OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Apple Inc. All rights reserved. | |
3 * | |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public License | |
15 * along with this library; see the file COPYING.LIB. If not, write to | |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
17 * Boston, MA 02110-1301, USA. | |
18 * | |
19 */ | |
20 | |
21 #include "config.h" | |
22 #include "core/rendering/RenderCombineText.h" | |
23 | |
24 #include "core/layout/TextRunConstructor.h" | |
25 #include "platform/graphics/GraphicsContext.h" | |
26 | |
27 namespace blink { | |
28 | |
29 const float textCombineMargin = 1.1f; // Allow em + 10% margin | |
30 | |
31 RenderCombineText::RenderCombineText(Node* node, PassRefPtr<StringImpl> string) | |
32 : RenderText(node, string) | |
33 , m_combinedTextWidth(0) | |
34 , m_scaleX(1.0f) | |
35 , m_isCombined(false) | |
36 , m_needsFontUpdate(false) | |
37 { | |
38 } | |
39 | |
40 void RenderCombineText::styleDidChange(StyleDifference diff, const LayoutStyle*
oldStyle) | |
41 { | |
42 setStyleInternal(LayoutStyle::clone(styleRef())); | |
43 RenderText::styleDidChange(diff, oldStyle); | |
44 | |
45 updateIsCombined(); | |
46 } | |
47 | |
48 void RenderCombineText::setTextInternal(PassRefPtr<StringImpl> text) | |
49 { | |
50 RenderText::setTextInternal(text); | |
51 | |
52 updateIsCombined(); | |
53 } | |
54 | |
55 float RenderCombineText::width(unsigned from, unsigned length, const Font& font,
float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallb
ackFonts, GlyphOverflow* glyphOverflow) const | |
56 { | |
57 if (!length) | |
58 return 0; | |
59 | |
60 if (hasEmptyText()) | |
61 return 0; | |
62 | |
63 if (m_isCombined) | |
64 return font.fontDescription().computedSize(); | |
65 | |
66 return RenderText::width(from, length, font, xPosition, direction, fallbackF
onts, glyphOverflow); | |
67 } | |
68 | |
69 void scaleHorizontallyAndTranslate(GraphicsContext& context, float scaleX, float
centerX, float offsetX, float offsetY) | |
70 { | |
71 context.concatCTM(AffineTransform(scaleX, 0, 0, 1, centerX * (1.0f - scaleX)
+ offsetX * scaleX, offsetY)); | |
72 } | |
73 | |
74 void RenderCombineText::transformToInlineCoordinates(GraphicsContext& context, c
onst FloatRect& boxRect) const | |
75 { | |
76 ASSERT(!m_needsFontUpdate); | |
77 ASSERT(m_isCombined); | |
78 if (m_scaleX >= 1.0f) { | |
79 // Fast path, more than 90% of cases | |
80 ASSERT(m_scaleX == 1.0f); | |
81 context.concatCTM(AffineTransform::translation(offsetXNoScale(boxRect),
offsetY())); | |
82 return; | |
83 } | |
84 ASSERT(m_scaleX > 0.0f); | |
85 const float centerX = boxRect.x() + boxRect.width() / 2; | |
86 scaleHorizontallyAndTranslate(context, m_scaleX, centerX, offsetX(boxRect),
offsetY()); | |
87 } | |
88 | |
89 void RenderCombineText::transformLayoutRect(FloatRect& boxRect) const | |
90 { | |
91 ASSERT(!m_needsFontUpdate); | |
92 ASSERT(m_isCombined); | |
93 boxRect.move(offsetXNoScale(boxRect), offsetY()); | |
94 } | |
95 | |
96 void RenderCombineText::updateIsCombined() | |
97 { | |
98 // CSS3 spec says text-combine works only in vertical writing mode. | |
99 m_isCombined = !style()->isHorizontalWritingMode() | |
100 // Nothing to combine. | |
101 && !hasEmptyText(); | |
102 | |
103 if (m_isCombined) | |
104 m_needsFontUpdate = true; | |
105 } | |
106 | |
107 void RenderCombineText::updateFont() | |
108 { | |
109 if (!m_needsFontUpdate) | |
110 return; | |
111 | |
112 m_needsFontUpdate = false; | |
113 | |
114 if (!m_isCombined) | |
115 return; | |
116 | |
117 TextRun run = constructTextRun(this, originalFont(), this, styleRef(), style
()->direction()); | |
118 FontDescription description = originalFont().fontDescription(); | |
119 float emWidth = description.computedSize(); | |
120 if (!(style()->textDecorationsInEffect() & (TextDecorationUnderline | TextDe
corationOverline))) | |
121 emWidth *= textCombineMargin; | |
122 | |
123 description.setOrientation(Horizontal); // We are going to draw combined tex
t horizontally. | |
124 m_combinedTextWidth = originalFont().width(run); | |
125 | |
126 FontSelector* fontSelector = style()->font().fontSelector(); | |
127 | |
128 bool shouldUpdateFont = style()->setFontDescription(description); // Need to
change font orientation to horizontal. | |
129 | |
130 if (m_combinedTextWidth <= emWidth) { | |
131 m_scaleX = 1.0f; | |
132 } else { | |
133 // Need to try compressed glyphs. | |
134 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth,
QuarterWidth }; | |
135 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) { | |
136 description.setWidthVariant(widthVariants[i]); | |
137 Font compressedFont = Font(description); | |
138 compressedFont.update(fontSelector); | |
139 float runWidth = compressedFont.width(run); | |
140 if (runWidth <= emWidth) { | |
141 m_combinedTextWidth = runWidth; | |
142 | |
143 // Replace my font with the new one. | |
144 shouldUpdateFont = style()->setFontDescription(description); | |
145 break; | |
146 } | |
147 } | |
148 | |
149 // If width > ~1em, shrink to fit within ~1em, otherwise render without
scaling (no expansion) | |
150 // http://dev.w3.org/csswg/css-writing-modes-3/#text-combine-compression | |
151 if (m_combinedTextWidth > emWidth) { | |
152 m_scaleX = emWidth / m_combinedTextWidth; | |
153 m_combinedTextWidth = emWidth; | |
154 } else { | |
155 m_scaleX = 1.0f; | |
156 } | |
157 } | |
158 | |
159 if (shouldUpdateFont) | |
160 style()->font().update(fontSelector); | |
161 } | |
162 | |
163 } // namespace blink | |
OLD | NEW |