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

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: rebase-update 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(0)
kouhei (in TOK) 2015/01/21 04:25:04 I prefer this default to 1.0f.
kojii 2015/01/21 07:00:24 Done.
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 auto renderingWidth = m_combinedTextWidth;
kouhei (in TOK) 2015/01/21 04:25:04 float
kojii 2015/01/21 07:00:23 Done.
76 if (m_scaleX)
77 renderingWidth /= m_scaleX;
78 textOrigin.move(boxRect.height() / 2 - renderingWidth / 2, style()->font().f ontDescription().computedPixelSize());
79 }
80
81 void RenderCombineText::transform(GraphicsContext& context, const FloatRect& box Rect) const
82 {
83 ASSERT(!m_needsFontUpdate);
84 auto centerX = boxRect.x() + boxRect.width() / 2;
85 AffineTransform transform(m_scaleX, 0, 0, 1, centerX * (1 - m_scaleX), 0);
86 context.concatCTM(transform);
76 } 87 }
77 88
78 void RenderCombineText::getStringToRender(int start, StringView& string, int& le ngth) const 89 void RenderCombineText::getStringToRender(int start, StringView& string, int& le ngth) const
79 { 90 {
80 ASSERT(start >= 0); 91 ASSERT(start >= 0);
92 ASSERT(!m_needsFontUpdate);
81 if (m_isCombined) { 93 if (m_isCombined) {
82 string = StringView(m_renderingText.impl()); 94 string = StringView(m_renderingText.impl());
83 length = string.length(); 95 length = string.length();
84 return; 96 return;
85 } 97 }
86 98
87 string = text().createView(start, length); 99 string = text().createView(start, length);
88 } 100 }
89 101
90 void RenderCombineText::combineText() 102 void RenderCombineText::updateIsCombinedAndText()
103 {
104 // CSS3 spec says text-combine works only in vertical writing mode.
105 bool shouldCombine = !style()->isHorizontalWritingMode()
106 // Nothing to combine.
107 && !(m_isCombined ? m_renderingText.isEmpty() : hasEmptyText());
108 if (shouldCombine == m_isCombined)
109 return;
110
111 m_isCombined = shouldCombine;
112
113 if (!shouldCombine) {
114 RenderText::setTextInternal(m_renderingText.impl());
115 m_renderingText.releaseImpl();
116 return;
117 }
118 DEFINE_STATIC_LOCAL(String, objectReplacementCharacterString, (&objectReplac ementCharacter, 1));
119 m_renderingText = text();
120 RenderText::setTextInternal(objectReplacementCharacterString.impl());
121 m_needsFontUpdate = true;
122 }
123
124 void RenderCombineText::updateFont()
91 { 125 {
92 if (!m_needsFontUpdate) 126 if (!m_needsFontUpdate)
93 return; 127 return;
94 128
95 m_isCombined = false;
96 m_needsFontUpdate = false; 129 m_needsFontUpdate = false;
97 130
98 // CSS3 spec says text-combine works only in vertical writing mode. 131 if (!m_isCombined)
99 if (style()->isHorizontalWritingMode())
100 return; 132 return;
101 133
102 // Nothing to combine. 134 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(); 135 FontDescription description = originalFont().fontDescription();
108 float emWidth = description.computedSize() * textCombineMargin; 136 float emWidth = description.computedSize();
109 bool shouldUpdateFont = false; 137 if (!(style()->textDecorationsInEffect() & (TextDecorationUnderline | TextDe corationOverline)))
138 emWidth *= textCombineMargin;
110 139
111 description.setOrientation(Horizontal); // We are going to draw combined tex t horizontally. 140 description.setOrientation(Horizontal); // We are going to draw combined tex t horizontally.
112 m_combinedTextWidth = originalFont().width(run); 141 m_combinedTextWidth = originalFont().width(run);
113 m_isCombined = m_combinedTextWidth <= emWidth;
114 142
115 FontSelector* fontSelector = style()->font().fontSelector(); 143 FontSelector* fontSelector = style()->font().fontSelector();
116 144
117 if (m_isCombined) 145 bool shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal.
118 shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal. 146
119 else { 147 if (m_combinedTextWidth <= emWidth) {
148 m_scaleX = 0;
149 } else {
120 // Need to try compressed glyphs. 150 // Need to try compressed glyphs.
121 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth }; 151 static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth };
122 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) { 152 for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) {
123 description.setWidthVariant(widthVariants[i]); 153 description.setWidthVariant(widthVariants[i]);
124 Font compressedFont = Font(description); 154 Font compressedFont = Font(description);
125 compressedFont.update(fontSelector); 155 compressedFont.update(fontSelector);
126 float runWidth = compressedFont.width(run); 156 float runWidth = compressedFont.width(run);
127 if (runWidth <= emWidth) { 157 if (runWidth <= emWidth) {
128 m_combinedTextWidth = runWidth; 158 m_combinedTextWidth = runWidth;
129 m_isCombined = true;
130 159
131 // Replace my font with the new one. 160 // Replace my font with the new one.
132 shouldUpdateFont = style()->setFontDescription(description); 161 shouldUpdateFont = style()->setFontDescription(description);
133 break; 162 break;
134 } 163 }
135 } 164 }
165
166 if (m_combinedTextWidth <= emWidth) {
167 m_scaleX = 0;
168 } else {
169 m_scaleX = emWidth / m_combinedTextWidth;
170 m_combinedTextWidth = emWidth;
171 }
136 } 172 }
137 173
138 if (!m_isCombined)
139 shouldUpdateFont = style()->setFontDescription(originalFont().fontDescri ption());
140
141 if (shouldUpdateFont) 174 if (shouldUpdateFont)
142 style()->font().update(fontSelector); 175 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 } 176 }
150 177
151 } // namespace blink 178 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/rendering/RenderCombineText.h ('k') | Source/core/rendering/line/BreakingContextInlineHeaders.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698