| 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" |
| 25 | 26 |
| 26 namespace blink { | 27 namespace blink { |
| 27 | 28 |
| 28 const float textCombineMargin = 1.1f; // Allow em + 10% margin | 29 const float textCombineMargin = 1.1f; // Allow em + 10% margin |
| 29 | 30 |
| 30 RenderCombineText::RenderCombineText(Node* node, PassRefPtr<StringImpl> string) | 31 RenderCombineText::RenderCombineText(Node* node, PassRefPtr<StringImpl> string) |
| 31 : RenderText(node, string) | 32 : RenderText(node, string) |
| 32 , m_combinedTextWidth(0) | 33 , m_combinedTextWidth(0) |
| 33 , m_isCombined(false) | 34 , m_scaleX(1.0f) |
| 34 , m_needsFontUpdate(false) | 35 , m_isCombined(false) |
| 36 , m_needsFontUpdate(false) |
| 35 { | 37 { |
| 36 } | 38 } |
| 37 | 39 |
| 38 void RenderCombineText::styleDidChange(StyleDifference diff, const RenderStyle*
oldStyle) | 40 void RenderCombineText::styleDidChange(StyleDifference diff, const RenderStyle*
oldStyle) |
| 39 { | 41 { |
| 40 setStyleInternal(RenderStyle::clone(style())); | 42 setStyleInternal(RenderStyle::clone(style())); |
| 41 RenderText::styleDidChange(diff, oldStyle); | 43 RenderText::styleDidChange(diff, oldStyle); |
| 42 | 44 |
| 43 if (m_isCombined) { | 45 updateIsCombined(); |
| 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; | |
| 49 } | 46 } |
| 50 | 47 |
| 51 void RenderCombineText::setTextInternal(PassRefPtr<StringImpl> text) | 48 void RenderCombineText::setTextInternal(PassRefPtr<StringImpl> text) |
| 52 { | 49 { |
| 53 RenderText::setTextInternal(text); | 50 RenderText::setTextInternal(text); |
| 54 | 51 |
| 55 m_needsFontUpdate = true; | 52 updateIsCombined(); |
| 56 } | 53 } |
| 57 | 54 |
| 58 float RenderCombineText::width(unsigned from, unsigned length, const Font& font,
float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallb
ackFonts, GlyphOverflow* glyphOverflow) const | 55 float RenderCombineText::width(unsigned from, unsigned length, const Font& font,
float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallb
ackFonts, GlyphOverflow* glyphOverflow) const |
| 59 { | 56 { |
| 60 if (!length) | 57 if (!length) |
| 61 return 0; | 58 return 0; |
| 62 | 59 |
| 63 if (hasEmptyText()) | 60 if (hasEmptyText()) |
| 64 return 0; | 61 return 0; |
| 65 | 62 |
| 66 if (m_isCombined) | 63 if (m_isCombined) |
| 67 return font.fontDescription().computedSize(); | 64 return font.fontDescription().computedSize(); |
| 68 | 65 |
| 69 return RenderText::width(from, length, font, xPosition, direction, fallbackF
onts, glyphOverflow); | 66 return RenderText::width(from, length, font, xPosition, direction, fallbackF
onts, glyphOverflow); |
| 70 } | 67 } |
| 71 | 68 |
| 72 void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect
& boxRect) const | 69 void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect
& boxRect) const |
| 73 { | 70 { |
| 74 if (m_isCombined) | 71 ASSERT(!m_needsFontUpdate); |
| 75 textOrigin.move(boxRect.height() / 2 - ceilf(m_combinedTextWidth) / 2, s
tyle()->font().fontDescription().computedPixelSize()); | 72 if (!m_isCombined) |
| 73 return; |
| 74 float renderingWidth = m_combinedTextWidth / m_scaleX; |
| 75 textOrigin.move(boxRect.height() / 2 - renderingWidth / 2, style()->font().f
ontDescription().computedPixelSize()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void RenderCombineText::getStringToRender(int start, StringView& string, int& le
ngth) const | 78 void RenderCombineText::transform(GraphicsContext& context, const FloatRect& box
Rect) const |
| 79 { | 79 { |
| 80 ASSERT(start >= 0); | 80 ASSERT(!m_needsFontUpdate); |
| 81 if (m_isCombined) { | 81 ASSERT(isTransformNeeded()); |
| 82 string = StringView(m_renderingText.impl()); | 82 auto centerX = boxRect.x() + boxRect.width() / 2; |
| 83 length = string.length(); | 83 AffineTransform transform(m_scaleX, 0, 0, 1, centerX * (1 - m_scaleX), 0); |
| 84 return; | 84 context.concatCTM(transform); |
| 85 } | |
| 86 | |
| 87 string = text().createView(start, length); | |
| 88 } | 85 } |
| 89 | 86 |
| 90 void RenderCombineText::combineText() | 87 void RenderCombineText::updateIsCombined() |
| 88 { |
| 89 // CSS3 spec says text-combine works only in vertical writing mode. |
| 90 m_isCombined = !style()->isHorizontalWritingMode() |
| 91 // Nothing to combine. |
| 92 && !hasEmptyText(); |
| 93 |
| 94 if (m_isCombined) |
| 95 m_needsFontUpdate = true; |
| 96 } |
| 97 |
| 98 void RenderCombineText::updateFont() |
| 91 { | 99 { |
| 92 if (!m_needsFontUpdate) | 100 if (!m_needsFontUpdate) |
| 93 return; | 101 return; |
| 94 | 102 |
| 95 m_isCombined = false; | |
| 96 m_needsFontUpdate = false; | 103 m_needsFontUpdate = false; |
| 97 | 104 |
| 98 // CSS3 spec says text-combine works only in vertical writing mode. | 105 if (!m_isCombined) |
| 99 if (style()->isHorizontalWritingMode()) | |
| 100 return; | |
| 101 | |
| 102 // Nothing to combine. | |
| 103 if (hasEmptyText()) | |
| 104 return; | 106 return; |
| 105 | 107 |
| 106 TextRun run = constructTextRun(this, originalFont(), this, style(), style()-
>direction()); | 108 TextRun run = constructTextRun(this, originalFont(), this, style(), style()-
>direction()); |
| 107 FontDescription description = originalFont().fontDescription(); | 109 FontDescription description = originalFont().fontDescription(); |
| 108 float emWidth = description.computedSize() * textCombineMargin; | 110 float emWidth = description.computedSize(); |
| 109 bool shouldUpdateFont = false; | 111 if (!(style()->textDecorationsInEffect() & (TextDecorationUnderline | TextDe
corationOverline))) |
| 112 emWidth *= textCombineMargin; |
| 110 | 113 |
| 111 description.setOrientation(Horizontal); // We are going to draw combined tex
t horizontally. | 114 description.setOrientation(Horizontal); // We are going to draw combined tex
t horizontally. |
| 112 m_combinedTextWidth = originalFont().width(run); | 115 m_combinedTextWidth = originalFont().width(run); |
| 113 m_isCombined = m_combinedTextWidth <= emWidth; | |
| 114 | 116 |
| 115 FontSelector* fontSelector = style()->font().fontSelector(); | 117 FontSelector* fontSelector = style()->font().fontSelector(); |
| 116 | 118 |
| 117 if (m_isCombined) | 119 bool shouldUpdateFont = style()->setFontDescription(description); // Need to
change font orientation to horizontal. |
| 118 shouldUpdateFont = style()->setFontDescription(description); // Need to
change font orientation to horizontal. | 120 |
| 119 else { | 121 if (m_combinedTextWidth <= emWidth) { |
| 122 m_scaleX = 1.0f; |
| 123 } else { |
| 120 // Need to try compressed glyphs. | 124 // Need to try compressed glyphs. |
| 121 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth,
QuarterWidth }; | 125 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth,
QuarterWidth }; |
| 122 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) { | 126 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) { |
| 123 description.setWidthVariant(widthVariants[i]); | 127 description.setWidthVariant(widthVariants[i]); |
| 124 Font compressedFont = Font(description); | 128 Font compressedFont = Font(description); |
| 125 compressedFont.update(fontSelector); | 129 compressedFont.update(fontSelector); |
| 126 float runWidth = compressedFont.width(run); | 130 float runWidth = compressedFont.width(run); |
| 127 if (runWidth <= emWidth) { | 131 if (runWidth <= emWidth) { |
| 128 m_combinedTextWidth = runWidth; | 132 m_combinedTextWidth = runWidth; |
| 129 m_isCombined = true; | |
| 130 | 133 |
| 131 // Replace my font with the new one. | 134 // Replace my font with the new one. |
| 132 shouldUpdateFont = style()->setFontDescription(description); | 135 shouldUpdateFont = style()->setFontDescription(description); |
| 133 break; | 136 break; |
| 134 } | 137 } |
| 135 } | 138 } |
| 139 |
| 140 // If width > ~1em, shrink to fit within ~1em, otherwise render without
scaling (no expansion) |
| 141 // http://dev.w3.org/csswg/css-writing-modes-3/#text-combine-compression |
| 142 if (m_combinedTextWidth > emWidth) { |
| 143 m_scaleX = emWidth / m_combinedTextWidth; |
| 144 m_combinedTextWidth = emWidth; |
| 145 } else { |
| 146 m_scaleX = 1.0f; |
| 147 } |
| 136 } | 148 } |
| 137 | 149 |
| 138 if (!m_isCombined) | |
| 139 shouldUpdateFont = style()->setFontDescription(originalFont().fontDescri
ption()); | |
| 140 | |
| 141 if (shouldUpdateFont) | 150 if (shouldUpdateFont) |
| 142 style()->font().update(fontSelector); | 151 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 } | |
| 149 } | 152 } |
| 150 | 153 |
| 151 } // namespace blink | 154 } // namespace blink |
| OLD | NEW |