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