Rename rendering/RenderFileUploadControl to layout/LayoutFileUploadControl.
[chromium-blink-merge.git] / third_party / WebKit / Source / core / rendering / RenderCombineText.cpp
blobe911b1f95afd40ed7494d61e61487483501e1ce8
1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
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.
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.
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.
21 #include "config.h"
22 #include "core/rendering/RenderCombineText.h"
24 #include "core/layout/TextRunConstructor.h"
25 #include "platform/graphics/GraphicsContext.h"
27 namespace blink {
29 const float textCombineMargin = 1.1f; // Allow em + 10% margin
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)
40 void RenderCombineText::styleDidChange(StyleDifference diff, const LayoutStyle* oldStyle)
42 setStyleInternal(LayoutStyle::clone(styleRef()));
43 RenderText::styleDidChange(diff, oldStyle);
45 updateIsCombined();
48 void RenderCombineText::setTextInternal(PassRefPtr<StringImpl> text)
50 RenderText::setTextInternal(text);
52 updateIsCombined();
55 float RenderCombineText::width(unsigned from, unsigned length, const Font& font, float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
57 if (!length)
58 return 0;
60 if (hasEmptyText())
61 return 0;
63 if (m_isCombined)
64 return font.fontDescription().computedSize();
66 return RenderText::width(from, length, font, xPosition, direction, fallbackFonts, glyphOverflow);
69 void scaleHorizontallyAndTranslate(GraphicsContext& context, float scaleX, float centerX, float offsetX, float offsetY)
71 context.concatCTM(AffineTransform(scaleX, 0, 0, 1, centerX * (1.0f - scaleX) + offsetX * scaleX, offsetY));
74 void RenderCombineText::transformToInlineCoordinates(GraphicsContext& context, const FloatRect& boxRect) const
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;
84 ASSERT(m_scaleX > 0.0f);
85 const float centerX = boxRect.x() + boxRect.width() / 2;
86 scaleHorizontallyAndTranslate(context, m_scaleX, centerX, offsetX(boxRect), offsetY());
89 void RenderCombineText::transformLayoutRect(FloatRect& boxRect) const
91 ASSERT(!m_needsFontUpdate);
92 ASSERT(m_isCombined);
93 boxRect.move(offsetXNoScale(boxRect), offsetY());
96 void RenderCombineText::updateIsCombined()
98 // CSS3 spec says text-combine works only in vertical writing mode.
99 m_isCombined = !style()->isHorizontalWritingMode()
100 // Nothing to combine.
101 && !hasEmptyText();
103 if (m_isCombined)
104 m_needsFontUpdate = true;
107 void RenderCombineText::updateFont()
109 if (!m_needsFontUpdate)
110 return;
112 m_needsFontUpdate = false;
114 if (!m_isCombined)
115 return;
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 | TextDecorationOverline)))
121 emWidth *= textCombineMargin;
123 description.setOrientation(Horizontal); // We are going to draw combined text horizontally.
124 m_combinedTextWidth = originalFont().width(run);
126 FontSelector* fontSelector = style()->font().fontSelector();
128 bool shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal.
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;
143 // Replace my font with the new one.
144 shouldUpdateFont = style()->setFontDescription(description);
145 break;
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;
159 if (shouldUpdateFont)
160 style()->font().update(fontSelector);
163 } // namespace blink