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

Side by Side Diff: Source/core/css/resolver/FontBuilder.cpp

Issue 468793003: Make style building for 'font-size' less custom. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix regression related to FontDescriptions with isAbsolute=true. Created 6 years, 3 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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2013 Google Inc. All rights reserved. 4 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 * 20 *
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/css/resolver/FontBuilder.h" 24 #include "core/css/resolver/FontBuilder.h"
25 25
26 #include "core/css/CSSCalculationValue.h" 26 #include "core/css/CSSCalculationValue.h"
27 #include "core/css/CSSToLengthConversionData.h" 27 #include "core/css/CSSToLengthConversionData.h"
28 #include "core/css/FontSize.h"
29 #include "core/frame/LocalFrame.h" 28 #include "core/frame/LocalFrame.h"
30 #include "core/frame/Settings.h" 29 #include "core/frame/Settings.h"
31 #include "core/rendering/RenderTheme.h" 30 #include "core/rendering/RenderTheme.h"
32 #include "core/rendering/RenderView.h" 31 #include "core/rendering/RenderView.h"
33 #include "core/rendering/TextAutosizer.h" 32 #include "core/rendering/TextAutosizer.h"
34 #include "platform/fonts/FontDescription.h" 33 #include "platform/fonts/FontDescription.h"
35 #include "platform/text/LocaleToScriptMapping.h" 34 #include "platform/text/LocaleToScriptMapping.h"
36 35
37 namespace blink { 36 namespace blink {
38 37
(...skipping 17 matching lines...) Expand all
56 m_fontBuilder->didChangeFontParameters(m_fontBuilder->m_style->setFontDe scription(m_fontDescription)); 55 m_fontBuilder->didChangeFontParameters(m_fontBuilder->m_style->setFontDe scription(m_fontDescription));
57 } 56 }
58 57
59 private: 58 private:
60 RawPtrWillBeMember<FontBuilder> m_fontBuilder; 59 RawPtrWillBeMember<FontBuilder> m_fontBuilder;
61 FontDescription m_fontDescription; 60 FontDescription m_fontDescription;
62 }; 61 };
63 62
64 FontBuilder::FontBuilder() 63 FontBuilder::FontBuilder()
65 : m_document(nullptr) 64 : m_document(nullptr)
66 , m_fontSizehasViewportUnits(false)
67 , m_style(0) 65 , m_style(0)
68 , m_fontDirty(false) 66 , m_fontDirty(false)
69 { 67 {
70 } 68 }
71 69
72 void FontBuilder::initForStyleResolve(const Document& document, RenderStyle* sty le) 70 void FontBuilder::initForStyleResolve(const Document& document, RenderStyle* sty le)
73 { 71 {
74 ASSERT(document.frame()); 72 ASSERT(document.frame());
75 m_document = &document; 73 m_document = &document;
76 m_style = style; 74 m_style = style;
(...skipping 18 matching lines...) Expand all
95 void FontBuilder::setInitial(float effectiveZoom) 93 void FontBuilder::setInitial(float effectiveZoom)
96 { 94 {
97 ASSERT(m_document && m_document->settings()); 95 ASSERT(m_document && m_document->settings());
98 if (!m_document || !m_document->settings()) 96 if (!m_document || !m_document->settings())
99 return; 97 return;
100 98
101 FontDescriptionChangeScope scope(this); 99 FontDescriptionChangeScope scope(this);
102 100
103 scope.reset(); 101 scope.reset();
104 setFontFamilyToStandard(scope.fontDescription(), m_document); 102 setFontFamilyToStandard(scope.fontDescription(), m_document);
105 scope.fontDescription().setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1) ; 103 setSize(scope.fontDescription(), FontBuilder::initialSize());
106 setSize(scope.fontDescription(), effectiveZoom, FontSize::fontSizeForKeyword (m_document, CSSValueMedium, NonFixedPitchFont));
107 } 104 }
108 105
109 void FontBuilder::inheritFrom(const FontDescription& fontDescription) 106 void FontBuilder::inheritFrom(const FontDescription& fontDescription)
110 { 107 {
111 FontDescriptionChangeScope scope(this); 108 FontDescriptionChangeScope scope(this);
112 109
113 scope.set(fontDescription); 110 scope.set(fontDescription);
114 } 111 }
115 112
116 void FontBuilder::didChangeFontParameters(bool changed) 113 void FontBuilder::didChangeFontParameters(bool changed)
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 currFamily = newFamily.get(); 223 currFamily = newFamily.get();
227 } 224 }
228 } 225 }
229 } 226 }
230 227
231 // We can't call useFixedDefaultSize() until all new font families have been added 228 // We can't call useFixedDefaultSize() until all new font families have been added
232 // If currFamily is non-zero then we set at least one family on this descrip tion. 229 // If currFamily is non-zero then we set at least one family on this descrip tion.
233 if (!currFamily) 230 if (!currFamily)
234 return; 231 return;
235 232
236 if (scope.fontDescription().keywordSize() && scope.fontDescription().fixedPi tchFontType() != oldFixedPitchFontType) { 233 if (scope.fontDescription().keywordSize() && scope.fontDescription().fixedPi tchFontType() != oldFixedPitchFontType)
237 scope.fontDescription().setSpecifiedSize(FontSize::fontSizeForKeyword(m_ document, 234 setSize(scope.fontDescription(), FontDescription::Size(scope.fontDescrip tion().keywordSize(), 0.0f, false));
238 static_cast<CSSValueID>(CSSValueXxSmall + scope.fontDescription().keywor dSize() - 1), scope.fontDescription().fixedPitchFontType()));
239 }
240 }
241
242 void FontBuilder::setFontSizeInitial()
243 {
244 FontDescriptionChangeScope scope(this);
245
246 float size = FontSize::fontSizeForKeyword(m_document, CSSValueMedium, scope. fontDescription().fixedPitchFontType());
247
248 if (size < 0)
249 return;
250
251 scope.fontDescription().setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1) ;
252 scope.fontDescription().setSpecifiedSize(size);
253 }
254
255 void FontBuilder::setFontSizeInherit(const FontDescription& parentFontDescriptio n)
256 {
257 FontDescriptionChangeScope scope(this);
258
259 float size = parentFontDescription.specifiedSize();
260
261 if (size < 0)
262 return;
263
264 scope.fontDescription().setKeywordSize(parentFontDescription.keywordSize());
265 scope.fontDescription().setSpecifiedSize(size);
266 }
267
268 // FIXME: Figure out where we fall in the size ranges (xx-small to xxx-large)
269 // and scale down/up to the next size level.
270 static float largerFontSize(float size)
271 {
272 return size * 1.2f;
273 }
274
275 static float smallerFontSize(float size)
276 {
277 return size / 1.2f;
278 }
279
280 // FIXME: Have to pass RenderStyles here for calc/computed values. This shouldn' t be neecessary.
281 void FontBuilder::setFontSizeValue(CSSValue* value, RenderStyle* parentStyle, co nst RenderStyle* rootElementStyle)
282 {
283 if (!value->isPrimitiveValue())
284 return;
285
286 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
287
288 FontDescriptionChangeScope scope(this);
289
290 scope.fontDescription().setKeywordSize(0);
291 float parentSize = 0;
292 bool parentIsAbsoluteSize = false;
293 float size = 0;
294
295 // FIXME: Find out when parentStyle could be 0?
296 if (parentStyle) {
297 parentSize = parentStyle->fontDescription().specifiedSize();
298 parentIsAbsoluteSize = parentStyle->fontDescription().isAbsoluteSize();
299 }
300
301 if (CSSValueID valueID = primitiveValue->getValueID()) {
302 switch (valueID) {
303 case CSSValueXxSmall:
304 case CSSValueXSmall:
305 case CSSValueSmall:
306 case CSSValueMedium:
307 case CSSValueLarge:
308 case CSSValueXLarge:
309 case CSSValueXxLarge:
310 case CSSValueWebkitXxxLarge:
311 size = FontSize::fontSizeForKeyword(m_document, valueID, scope.fontD escription().fixedPitchFontType());
312 scope.fontDescription().setKeywordSize(valueID - CSSValueXxSmall + 1 );
313 break;
314 case CSSValueLarger:
315 size = largerFontSize(parentSize);
316 break;
317 case CSSValueSmaller:
318 size = smallerFontSize(parentSize);
319 break;
320 default:
321 return;
322 }
323
324 scope.fontDescription().setIsAbsoluteSize(parentIsAbsoluteSize && (value ID == CSSValueLarger || valueID == CSSValueSmaller));
325 } else {
326 scope.fontDescription().setIsAbsoluteSize(parentIsAbsoluteSize || !(prim itiveValue->isPercentage() || primitiveValue->isFontRelativeLength()));
327 if (primitiveValue->isPercentage()) {
328 size = (primitiveValue->getFloatValue() * parentSize) / 100.0f;
329 } else {
330 // If we have viewport units the conversion will mark the parent sty le as having viewport units.
331 bool parentHasViewportUnits = parentStyle->hasViewportUnits();
332 parentStyle->setHasViewportUnits(false);
333 CSSToLengthConversionData conversionData(parentStyle, rootElementSty le, m_document->renderView(), 1.0f, true);
334 if (primitiveValue->isLength())
335 size = primitiveValue->computeLength<float>(conversionData);
336 else if (primitiveValue->isCalculatedPercentageWithLength())
337 size = primitiveValue->cssCalcValue()->toCalcValue(conversionDat a)->evaluate(parentSize);
338 else
339 ASSERT_NOT_REACHED();
340 m_fontSizehasViewportUnits = parentStyle->hasViewportUnits();
341 parentStyle->setHasViewportUnits(parentHasViewportUnits);
342 }
343 }
344
345 if (size < 0)
346 return;
347
348 // Overly large font sizes will cause crashes on some platforms (such as Win dows).
349 // Cap font size here to make sure that doesn't happen.
350 size = std::min(maximumAllowedFontSize, size);
351
352
353 scope.fontDescription().setSpecifiedSize(size);
354 } 235 }
355 236
356 void FontBuilder::setWeight(FontWeight fontWeight) 237 void FontBuilder::setWeight(FontWeight fontWeight)
357 { 238 {
358 FontDescriptionChangeScope scope(this); 239 FontDescriptionChangeScope scope(this);
359 240
360 scope.fontDescription().setWeight(fontWeight); 241 scope.fontDescription().setWeight(fontWeight);
361 } 242 }
362 243
244 void FontBuilder::setSize(const FontDescription::Size& size)
245 {
246 FontDescriptionChangeScope scope(this);
247
248 setSize(scope.fontDescription(), size);
249 }
250
363 void FontBuilder::setStretch(FontStretch fontStretch) 251 void FontBuilder::setStretch(FontStretch fontStretch)
364 { 252 {
365 FontDescriptionChangeScope scope(this); 253 FontDescriptionChangeScope scope(this);
366 254
367 scope.fontDescription().setStretch(fontStretch); 255 scope.fontDescription().setStretch(fontStretch);
368 } 256 }
369 257
370 void FontBuilder::setScript(const String& locale) 258 void FontBuilder::setScript(const String& locale)
371 { 259 {
372 FontDescriptionChangeScope scope(this); 260 FontDescriptionChangeScope scope(this);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 scope.fontDescription().setFontSmoothing(foontSmoothingMode); 305 scope.fontDescription().setFontSmoothing(foontSmoothingMode);
418 } 306 }
419 307
420 void FontBuilder::setFeatureSettings(PassRefPtr<FontFeatureSettings> settings) 308 void FontBuilder::setFeatureSettings(PassRefPtr<FontFeatureSettings> settings)
421 { 309 {
422 FontDescriptionChangeScope scope(this); 310 FontDescriptionChangeScope scope(this);
423 311
424 scope.fontDescription().setFeatureSettings(settings); 312 scope.fontDescription().setFeatureSettings(settings);
425 } 313 }
426 314
427 void FontBuilder::setSize(FontDescription& fontDescription, float effectiveZoom, float size) 315 void FontBuilder::setSize(FontDescription& fontDescription, const FontDescriptio n::Size& size)
428 { 316 {
429 fontDescription.setSpecifiedSize(size); 317 float specifiedSize = size.value;
430 fontDescription.setComputedSize(getComputedSizeFromSpecifiedSize(fontDescrip tion, effectiveZoom, size)); 318
319 if (!specifiedSize && size.keyword)
320 specifiedSize = FontSize::fontSizeForKeyword(m_document, size.keyword, f ontDescription.fixedPitchFontType());
321
322 if (specifiedSize < 0)
323 return;
324
325 // Overly large font sizes will cause crashes on some platforms (such as Win dows).
326 // Cap font size here to make sure that doesn't happen.
327 specifiedSize = std::min(maximumAllowedFontSize, specifiedSize);
328
329 fontDescription.setKeywordSize(size.keyword);
330 fontDescription.setSpecifiedSize(specifiedSize);
331 fontDescription.setIsAbsoluteSize(size.isAbsolute);
431 } 332 }
432 333
433 float FontBuilder::getComputedSizeFromSpecifiedSize(FontDescription& fontDescrip tion, float effectiveZoom, float specifiedSize) 334 float FontBuilder::getComputedSizeFromSpecifiedSize(FontDescription& fontDescrip tion, float effectiveZoom, float specifiedSize)
434 { 335 {
435 float zoomFactor = effectiveZoom; 336 float zoomFactor = effectiveZoom;
436 // FIXME: Why is this here!!!!?! 337 // FIXME: Why is this here!!!!?!
437 if (LocalFrame* frame = m_document->frame()) 338 if (LocalFrame* frame = m_document->frame())
438 zoomFactor *= frame->textZoomFactor(); 339 zoomFactor *= frame->textZoomFactor();
439 340
440 return FontSize::getComputedSizeFromSpecifiedSize(m_document, zoomFactor, fo ntDescription.isAbsoluteSize(), specifiedSize); 341 return FontSize::getComputedSizeFromSpecifiedSize(m_document, zoomFactor, fo ntDescription.isAbsoluteSize(), specifiedSize);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 if (scope.fontDescription().genericFamily() != FontDescription::MonospaceFam ily 410 if (scope.fontDescription().genericFamily() != FontDescription::MonospaceFam ily
510 && parentFontDescription.genericFamily() != FontDescription::MonospaceFa mily) 411 && parentFontDescription.genericFamily() != FontDescription::MonospaceFa mily)
511 return; 412 return;
512 413
513 // We know the parent is monospace or the child is monospace, and that font 414 // We know the parent is monospace or the child is monospace, and that font
514 // size was unspecified. We want to scale our font size as appropriate. 415 // size was unspecified. We want to scale our font size as appropriate.
515 // If the font uses a keyword size, then we refetch from the table rather th an 416 // If the font uses a keyword size, then we refetch from the table rather th an
516 // multiplying by our scale factor. 417 // multiplying by our scale factor.
517 float size; 418 float size;
518 if (scope.fontDescription().keywordSize()) { 419 if (scope.fontDescription().keywordSize()) {
519 size = FontSize::fontSizeForKeyword(m_document, static_cast<CSSValueID>( CSSValueXxSmall + scope.fontDescription().keywordSize() - 1), scope.fontDescript ion().fixedPitchFontType()); 420 size = FontSize::fontSizeForKeyword(m_document, scope.fontDescription(). keywordSize(), scope.fontDescription().fixedPitchFontType());
520 } else { 421 } else {
521 Settings* settings = m_document->settings(); 422 Settings* settings = m_document->settings();
522 float fixedScaleFactor = (settings && settings->defaultFixedFontSize() & & settings->defaultFontSize()) 423 float fixedScaleFactor = (settings && settings->defaultFixedFontSize() & & settings->defaultFontSize())
523 ? static_cast<float>(settings->defaultFixedFontSize()) / settings->d efaultFontSize() 424 ? static_cast<float>(settings->defaultFixedFontSize()) / settings->d efaultFontSize()
524 : 1; 425 : 1;
525 size = parentFontDescription.fixedPitchFontType() == FixedPitchFont ? 426 size = parentFontDescription.fixedPitchFontType() == FixedPitchFont ?
526 scope.fontDescription().specifiedSize() / fixedScaleFactor : 427 scope.fontDescription().specifiedSize() / fixedScaleFactor :
527 scope.fontDescription().specifiedSize() * fixedScaleFactor; 428 scope.fontDescription().specifiedSize() * fixedScaleFactor;
528 } 429 }
529 430
530 setSize(scope.fontDescription(), style->effectiveZoom(), size); 431 scope.fontDescription().setSpecifiedSize(size);
432 updateComputedSize(scope.fontDescription(), style);
531 } 433 }
532 434
533 void FontBuilder::updateComputedSize(RenderStyle* style, const RenderStyle* pare ntStyle) 435 void FontBuilder::updateComputedSize(RenderStyle* style, const RenderStyle* pare ntStyle)
534 { 436 {
535 FontDescriptionChangeScope scope(this); 437 FontDescriptionChangeScope scope(this);
438 updateComputedSize(scope.fontDescription(), style);
439 }
536 440
537 float computedSize = getComputedSizeFromSpecifiedSize(scope.fontDescription( ), style->effectiveZoom(), scope.fontDescription().specifiedSize()); 441 void FontBuilder::updateComputedSize(FontDescription& fontDescription, RenderSty le* style)
442 {
443 float computedSize = getComputedSizeFromSpecifiedSize(fontDescription, style ->effectiveZoom(), fontDescription.specifiedSize());
538 float multiplier = style->textAutosizingMultiplier(); 444 float multiplier = style->textAutosizingMultiplier();
539 if (multiplier > 1) 445 if (multiplier > 1)
540 computedSize = TextAutosizer::computeAutosizedFontSize(computedSize, mul tiplier); 446 computedSize = TextAutosizer::computeAutosizedFontSize(computedSize, mul tiplier);
541 447 fontDescription.setComputedSize(computedSize);
542 scope.fontDescription().setComputedSize(computedSize);
543 } 448 }
544 449
545 // FIXME: style param should come first 450 // FIXME: style param should come first
546 void FontBuilder::createFont(PassRefPtrWillBeRawPtr<FontSelector> fontSelector, const RenderStyle* parentStyle, RenderStyle* style) 451 void FontBuilder::createFont(PassRefPtrWillBeRawPtr<FontSelector> fontSelector, const RenderStyle* parentStyle, RenderStyle* style)
547 { 452 {
548 if (!m_fontDirty) 453 if (!m_fontDirty)
549 return; 454 return;
550 455
551 updateComputedSize(style, parentStyle); 456 updateComputedSize(style, parentStyle);
552 checkForGenericFamilyChange(style, parentStyle); 457 checkForGenericFamilyChange(style, parentStyle);
553 checkForOrientationChange(style); 458 checkForOrientationChange(style);
554 style->font().update(fontSelector); 459 style->font().update(fontSelector);
555 m_fontDirty = false; 460 m_fontDirty = false;
556 } 461 }
557 462
558 void FontBuilder::createFontForDocument(PassRefPtrWillBeRawPtr<FontSelector> fon tSelector, RenderStyle* documentStyle) 463 void FontBuilder::createFontForDocument(PassRefPtrWillBeRawPtr<FontSelector> fon tSelector, RenderStyle* documentStyle)
559 { 464 {
560 FontDescription fontDescription = FontDescription(); 465 FontDescription fontDescription = FontDescription();
561 fontDescription.setScript(localeToScriptCodeForFontSelection(documentStyle-> locale())); 466 fontDescription.setScript(localeToScriptCodeForFontSelection(documentStyle-> locale()));
562 467
563 setFontFamilyToStandard(fontDescription, m_document); 468 setFontFamilyToStandard(fontDescription, m_document);
564 fontDescription.setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1); 469
565 int size = FontSize::fontSizeForKeyword(m_document, CSSValueMedium, NonFixed PitchFont); 470 setSize(fontDescription, FontDescription::Size(FontSize::initialKeywordSize( ), 0.0f, false));
566 fontDescription.setSpecifiedSize(size); 471 updateComputedSize(fontDescription, documentStyle);
567 fontDescription.setComputedSize(getComputedSizeFromSpecifiedSize(fontDescrip tion, documentStyle->effectiveZoom(), size));
568 472
569 FontOrientation fontOrientation; 473 FontOrientation fontOrientation;
570 NonCJKGlyphOrientation glyphOrientation; 474 NonCJKGlyphOrientation glyphOrientation;
571 getFontAndGlyphOrientation(documentStyle, fontOrientation, glyphOrientation) ; 475 getFontAndGlyphOrientation(documentStyle, fontOrientation, glyphOrientation) ;
572 fontDescription.setOrientation(fontOrientation); 476 fontDescription.setOrientation(fontOrientation);
573 fontDescription.setNonCJKGlyphOrientation(glyphOrientation); 477 fontDescription.setNonCJKGlyphOrientation(glyphOrientation);
574 documentStyle->setFontDescription(fontDescription); 478 documentStyle->setFontDescription(fontDescription);
575 documentStyle->font().update(fontSelector); 479 documentStyle->font().update(fontSelector);
576 } 480 }
577 481
578 } 482 }
OLDNEW
« no previous file with comments | « Source/core/css/resolver/FontBuilder.h ('k') | Source/core/css/resolver/StyleBuilderConverter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698