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.
22 #include "RenderCombineText.h"
24 #include "RenderBlock.h"
25 #include "StyleInheritedData.h"
29 const float textCombineMargin = 1.15f; // Allow em + 15% margin
31 RenderCombineText::RenderCombineText(Text& textNode, const String& string)
32 : RenderText(textNode, string)
34 , m_needsFontUpdate(false)
38 void RenderCombineText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
40 // FIXME: This is pretty hackish.
41 m_combineFontStyle = RenderStyle::clone(&style());
43 RenderText::styleDidChange(diff, oldStyle);
45 if (m_isCombined && selfNeedsLayout()) {
46 // Layouts cause the text to be recombined; therefore, only only un-combine when the style diff causes a layout.
47 RenderText::setRenderedText(originalText()); // This RenderCombineText has been combined once. Restore the original text for the next combineText().
51 m_needsFontUpdate = true;
54 void RenderCombineText::setRenderedText(const String& text)
56 RenderText::setRenderedText(text);
58 m_needsFontUpdate = true;
61 float RenderCombineText::width(unsigned from, unsigned length, const FontCascade& font, float xPosition, HashSet<const Font*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
64 return !length ? 0 : font.size();
66 return RenderText::width(from, length, font, xPosition, fallbackFonts, glyphOverflow);
69 void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect& boxRect) const
72 textOrigin.move(boxRect.height() / 2 - ceilf(m_combinedTextSize.width()) / 2, boxRect.width() + (boxRect.width() - m_combinedTextSize.height()) / 2);
75 void RenderCombineText::getStringToRender(int start, String& string, int& length) const
79 string = originalText();
80 length = string.length();
85 string = string.substringSharingImpl(static_cast<unsigned>(start), length);
88 void RenderCombineText::combineText()
90 if (!m_needsFontUpdate)
94 m_needsFontUpdate = false;
96 // CSS3 spec says text-combine works only in vertical writing mode.
97 if (style().isHorizontalWritingMode())
100 auto description = originalFont().fontDescription();
101 float emWidth = description.computedSize() * textCombineMargin;
102 bool shouldUpdateFont = false;
104 description.setOrientation(Horizontal); // We are going to draw combined text horizontally.
106 GlyphOverflow glyphOverflow;
107 glyphOverflow.computeBounds = true;
109 float combinedTextWidth = width(0, textLength(), originalFont(), 0, nullptr, &glyphOverflow);
110 m_isCombined = combinedTextWidth <= emWidth;
112 FontSelector* fontSelector = style().fontCascade().fontSelector();
115 shouldUpdateFont = m_combineFontStyle->setFontDescription(description); // Need to change font orientation to horizontal.
117 // Need to try compressed glyphs.
118 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth };
119 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) {
120 description.setWidthVariant(widthVariants[i]); // When modifying this, make sure to keep it in sync with FontPlatformData::isForTextCombine()!
122 FontCascade compressedFont(description, style().fontCascade().letterSpacing(), style().fontCascade().wordSpacing());
123 compressedFont.update(fontSelector);
125 float runWidth = RenderText::width(0, textLength(), compressedFont, 0, nullptr, &glyphOverflow);
126 if (runWidth <= emWidth) {
127 combinedTextWidth = runWidth;
130 // Replace my font with the new one.
131 shouldUpdateFont = m_combineFontStyle->setFontDescription(description);
138 shouldUpdateFont = m_combineFontStyle->setFontDescription(originalFont().fontDescription());
140 if (shouldUpdateFont)
141 m_combineFontStyle->fontCascade().update(fontSelector);
144 DEPRECATED_DEFINE_STATIC_LOCAL(String, objectReplacementCharacterString, (&objectReplacementCharacter, 1));
145 RenderText::setRenderedText(objectReplacementCharacterString.impl());
146 m_combinedTextSize = FloatSize(combinedTextWidth, glyphOverflow.bottom + glyphOverflow.top);
150 } // namespace WebCore