Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: Source/core/rendering/RenderCombineText.cpp

Issue 799123003: text-combine should scale rather than fall back to none when wide (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: All comments fixed Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 updateIsCombinedAndText();
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 m_isCombined = false; // flag that text() holds the text to render
53 updateIsCombinedAndText();
56 } 54 }
57 55
58 float RenderCombineText::width(unsigned from, unsigned length, const Font& font, float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallb ackFonts, GlyphOverflow* glyphOverflow) const 56 float RenderCombineText::width(unsigned from, unsigned length, const Font& font, float xPosition, TextDirection direction, HashSet<const SimpleFontData*>* fallb ackFonts, GlyphOverflow* glyphOverflow) const
59 { 57 {
60 if (!length) 58 if (!length)
61 return 0; 59 return 0;
62 60
63 if (hasEmptyText()) 61 if (hasEmptyText())
64 return 0; 62 return 0;
65 63
66 if (m_isCombined) 64 if (m_isCombined)
67 return font.fontDescription().computedSize(); 65 return font.fontDescription().computedSize();
68 66
69 return RenderText::width(from, length, font, xPosition, direction, fallbackF onts, glyphOverflow); 67 return RenderText::width(from, length, font, xPosition, direction, fallbackF onts, glyphOverflow);
70 } 68 }
71 69
72 void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect & boxRect) const 70 void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect & boxRect) const
73 { 71 {
74 if (m_isCombined) 72 ASSERT(!m_needsFontUpdate);
75 textOrigin.move(boxRect.height() / 2 - ceilf(m_combinedTextWidth) / 2, s tyle()->font().fontDescription().computedPixelSize()); 73 if (!m_isCombined)
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);
76 } 86 }
77 87
78 void RenderCombineText::getStringToRender(int start, StringView& string, int& le ngth) const 88 void RenderCombineText::getStringToRender(int start, StringView& string, int& le ngth) const
79 { 89 {
80 ASSERT(start >= 0); 90 ASSERT(start >= 0);
91 ASSERT(!m_needsFontUpdate);
81 if (m_isCombined) { 92 if (m_isCombined) {
82 string = StringView(m_renderingText.impl()); 93 string = StringView(m_renderingText.impl());
83 length = string.length(); 94 length = string.length();
84 return; 95 return;
85 } 96 }
86 97
87 string = text().createView(start, length); 98 string = text().createView(start, length);
88 } 99 }
89 100
90 void RenderCombineText::combineText() 101 void RenderCombineText::updateIsCombinedAndText()
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()
91 { 124 {
92 if (!m_needsFontUpdate) 125 if (!m_needsFontUpdate)
93 return; 126 return;
94 127
95 m_isCombined = false;
96 m_needsFontUpdate = false; 128 m_needsFontUpdate = false;
97 129
98 // CSS3 spec says text-combine works only in vertical writing mode. 130 if (!m_isCombined)
99 if (style()->isHorizontalWritingMode())
100 return; 131 return;
101 132
102 // Nothing to combine. 133 TextRun run = constructTextRun(this, originalFont(), m_renderingText, style( ), style()->direction());
103 if (hasEmptyText())
104 return;
105
106 TextRun run = constructTextRun(this, originalFont(), this, style(), style()- >direction());
107 FontDescription description = originalFont().fontDescription(); 134 FontDescription description = originalFont().fontDescription();
108 float emWidth = description.computedSize() * textCombineMargin; 135 float emWidth = description.computedSize();
109 bool shouldUpdateFont = false; 136 if (!(style()->textDecorationsInEffect() & (TextDecorationUnderline | TextDe corationOverline)))
137 emWidth *= textCombineMargin;
110 138
111 description.setOrientation(Horizontal); // We are going to draw combined tex t horizontally. 139 description.setOrientation(Horizontal); // We are going to draw combined tex t horizontally.
112 m_combinedTextWidth = originalFont().width(run); 140 m_combinedTextWidth = originalFont().width(run);
113 m_isCombined = m_combinedTextWidth <= emWidth;
114 141
115 FontSelector* fontSelector = style()->font().fontSelector(); 142 FontSelector* fontSelector = style()->font().fontSelector();
116 143
117 if (m_isCombined) 144 bool shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal.
118 shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal. 145
119 else { 146 if (m_combinedTextWidth <= emWidth) {
147 m_scaleX = 1.0f;
148 } else {
120 // Need to try compressed glyphs. 149 // Need to try compressed glyphs.
121 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth }; 150 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth };
122 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) { 151 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) {
123 description.setWidthVariant(widthVariants[i]); 152 description.setWidthVariant(widthVariants[i]);
124 Font compressedFont = Font(description); 153 Font compressedFont = Font(description);
125 compressedFont.update(fontSelector); 154 compressedFont.update(fontSelector);
126 float runWidth = compressedFont.width(run); 155 float runWidth = compressedFont.width(run);
127 if (runWidth <= emWidth) { 156 if (runWidth <= emWidth) {
128 m_combinedTextWidth = runWidth; 157 m_combinedTextWidth = runWidth;
129 m_isCombined = true;
130 158
131 // Replace my font with the new one. 159 // Replace my font with the new one.
132 shouldUpdateFont = style()->setFontDescription(description); 160 shouldUpdateFont = style()->setFontDescription(description);
133 break; 161 break;
134 } 162 }
135 } 163 }
164
165 if (m_combinedTextWidth <= emWidth) {
166 m_scaleX = 1.0f;
167 } else {
168 m_scaleX = emWidth / m_combinedTextWidth;
169 m_combinedTextWidth = emWidth;
170 }
136 } 171 }
137 172
138 if (!m_isCombined)
139 shouldUpdateFont = style()->setFontDescription(originalFont().fontDescri ption());
140
141 if (shouldUpdateFont) 173 if (shouldUpdateFont)
142 style()->font().update(fontSelector); 174 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 } 175 }
150 176
151 } // namespace blink 177 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698