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

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: Created 6 years, 4 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 "platform/fonts/FontDescription.h" 32 #include "platform/fonts/FontDescription.h"
34 #include "platform/text/LocaleToScriptMapping.h" 33 #include "platform/text/LocaleToScriptMapping.h"
35 34
36 namespace blink { 35 namespace blink {
37 36
38 // FIXME: This scoping class is a short-term fix to minimize the changes in 37 // FIXME: This scoping class is a short-term fix to minimize the changes in
(...skipping 16 matching lines...) Expand all
55 m_fontBuilder->didChangeFontParameters(m_fontBuilder->m_style->setFontDe scription(m_fontDescription)); 54 m_fontBuilder->didChangeFontParameters(m_fontBuilder->m_style->setFontDe scription(m_fontDescription));
56 } 55 }
57 56
58 private: 57 private:
59 RawPtrWillBeMember<FontBuilder> m_fontBuilder; 58 RawPtrWillBeMember<FontBuilder> m_fontBuilder;
60 FontDescription m_fontDescription; 59 FontDescription m_fontDescription;
61 }; 60 };
62 61
63 FontBuilder::FontBuilder() 62 FontBuilder::FontBuilder()
64 : m_document(nullptr) 63 : m_document(nullptr)
65 , m_fontSizehasViewportUnits(false)
66 , m_style(0) 64 , m_style(0)
67 , m_fontDirty(false) 65 , m_fontDirty(false)
68 { 66 {
69 } 67 }
70 68
71 void FontBuilder::initForStyleResolve(const Document& document, RenderStyle* sty le) 69 void FontBuilder::initForStyleResolve(const Document& document, RenderStyle* sty le)
72 { 70 {
73 ASSERT(document.frame()); 71 ASSERT(document.frame());
74 m_document = &document; 72 m_document = &document;
75 m_style = style; 73 m_style = style;
(...skipping 18 matching lines...) Expand all
94 void FontBuilder::setInitial(float effectiveZoom) 92 void FontBuilder::setInitial(float effectiveZoom)
95 { 93 {
96 ASSERT(m_document && m_document->settings()); 94 ASSERT(m_document && m_document->settings());
97 if (!m_document || !m_document->settings()) 95 if (!m_document || !m_document->settings())
98 return; 96 return;
99 97
100 FontDescriptionChangeScope scope(this); 98 FontDescriptionChangeScope scope(this);
101 99
102 scope.reset(); 100 scope.reset();
103 setFontFamilyToStandard(scope.fontDescription(), m_document); 101 setFontFamilyToStandard(scope.fontDescription(), m_document);
104 scope.fontDescription().setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1) ; 102 setSize(scope.fontDescription(), FontBuilder::initialSize());
105 setSize(scope.fontDescription(), effectiveZoom, FontSize::fontSizeForKeyword (m_document, CSSValueMedium, NonFixedPitchFont));
106 } 103 }
107 104
108 void FontBuilder::inheritFrom(const FontDescription& fontDescription) 105 void FontBuilder::inheritFrom(const FontDescription& fontDescription)
109 { 106 {
110 FontDescriptionChangeScope scope(this); 107 FontDescriptionChangeScope scope(this);
111 108
112 scope.set(fontDescription); 109 scope.set(fontDescription);
113 } 110 }
114 111
115 void FontBuilder::didChangeFontParameters(bool changed) 112 void FontBuilder::didChangeFontParameters(bool changed)
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 currFamily = newFamily.get(); 222 currFamily = newFamily.get();
226 } 223 }
227 } 224 }
228 } 225 }
229 226
230 // We can't call useFixedDefaultSize() until all new font families have been added 227 // We can't call useFixedDefaultSize() until all new font families have been added
231 // If currFamily is non-zero then we set at least one family on this descrip tion. 228 // If currFamily is non-zero then we set at least one family on this descrip tion.
232 if (!currFamily) 229 if (!currFamily)
233 return; 230 return;
234 231
235 if (scope.fontDescription().keywordSize() && scope.fontDescription().fixedPi tchFontType() != oldFixedPitchFontType) { 232 if (scope.fontDescription().keywordSize() && scope.fontDescription().fixedPi tchFontType() != oldFixedPitchFontType)
236 scope.fontDescription().setSpecifiedSize(FontSize::fontSizeForKeyword(m_ document, 233 setSize(scope.fontDescription(), FontDescription::Size(scope.fontDescrip tion().keywordSize(), 0.0f, false));
237 static_cast<CSSValueID>(CSSValueXxSmall + scope.fontDescription().keywor dSize() - 1), scope.fontDescription().fixedPitchFontType()));
238 }
239 }
240
241 void FontBuilder::setFontSizeInitial()
242 {
243 FontDescriptionChangeScope scope(this);
244
245 float size = FontSize::fontSizeForKeyword(m_document, CSSValueMedium, scope. fontDescription().fixedPitchFontType());
246
247 if (size < 0)
248 return;
249
250 scope.fontDescription().setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1) ;
251 scope.fontDescription().setSpecifiedSize(size);
252 }
253
254 void FontBuilder::setFontSizeInherit(const FontDescription& parentFontDescriptio n)
255 {
256 FontDescriptionChangeScope scope(this);
257
258 float size = parentFontDescription.specifiedSize();
259
260 if (size < 0)
261 return;
262
263 scope.fontDescription().setKeywordSize(parentFontDescription.keywordSize());
264 scope.fontDescription().setSpecifiedSize(size);
265 }
266
267 // FIXME: Figure out where we fall in the size ranges (xx-small to xxx-large)
268 // and scale down/up to the next size level.
269 static float largerFontSize(float size)
270 {
271 return size * 1.2f;
272 }
273
274 static float smallerFontSize(float size)
275 {
276 return size / 1.2f;
277 }
278
279 // FIXME: Have to pass RenderStyles here for calc/computed values. This shouldn' t be neecessary.
280 void FontBuilder::setFontSizeValue(CSSValue* value, RenderStyle* parentStyle, co nst RenderStyle* rootElementStyle)
281 {
282 if (!value->isPrimitiveValue())
283 return;
284
285 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
286
287 FontDescriptionChangeScope scope(this);
288
289 scope.fontDescription().setKeywordSize(0);
290 float parentSize = 0;
291 bool parentIsAbsoluteSize = false;
292 float size = 0;
293
294 // FIXME: Find out when parentStyle could be 0?
295 if (parentStyle) {
296 parentSize = parentStyle->fontDescription().specifiedSize();
297 parentIsAbsoluteSize = parentStyle->fontDescription().isAbsoluteSize();
298 }
299
300 if (CSSValueID valueID = primitiveValue->getValueID()) {
301 switch (valueID) {
302 case CSSValueXxSmall:
303 case CSSValueXSmall:
304 case CSSValueSmall:
305 case CSSValueMedium:
306 case CSSValueLarge:
307 case CSSValueXLarge:
308 case CSSValueXxLarge:
309 case CSSValueWebkitXxxLarge:
310 size = FontSize::fontSizeForKeyword(m_document, valueID, scope.fontD escription().fixedPitchFontType());
311 scope.fontDescription().setKeywordSize(valueID - CSSValueXxSmall + 1 );
312 break;
313 case CSSValueLarger:
314 size = largerFontSize(parentSize);
315 break;
316 case CSSValueSmaller:
317 size = smallerFontSize(parentSize);
318 break;
319 default:
320 return;
321 }
322
323 scope.fontDescription().setIsAbsoluteSize(parentIsAbsoluteSize && (value ID == CSSValueLarger || valueID == CSSValueSmaller));
324 } else {
325 scope.fontDescription().setIsAbsoluteSize(parentIsAbsoluteSize || !(prim itiveValue->isPercentage() || primitiveValue->isFontRelativeLength()));
326 if (primitiveValue->isPercentage()) {
327 size = (primitiveValue->getFloatValue() * parentSize) / 100.0f;
328 } else {
329 // If we have viewport units the conversion will mark the parent sty le as having viewport units.
330 bool parentHasViewportUnits = parentStyle->hasViewportUnits();
331 parentStyle->setHasViewportUnits(false);
332 CSSToLengthConversionData conversionData(parentStyle, rootElementSty le, m_document->renderView(), 1.0f, true);
333 if (primitiveValue->isLength())
334 size = primitiveValue->computeLength<float>(conversionData);
335 else if (primitiveValue->isCalculatedPercentageWithLength())
336 size = primitiveValue->cssCalcValue()->toCalcValue(conversionDat a)->evaluate(parentSize);
337 else
338 ASSERT_NOT_REACHED();
339 m_fontSizehasViewportUnits = parentStyle->hasViewportUnits();
340 parentStyle->setHasViewportUnits(parentHasViewportUnits);
341 }
342 }
343
344 if (size < 0)
345 return;
346
347 // Overly large font sizes will cause crashes on some platforms (such as Win dows).
348 // Cap font size here to make sure that doesn't happen.
349 size = std::min(maximumAllowedFontSize, size);
350
351
352 scope.fontDescription().setSpecifiedSize(size);
353 } 234 }
354 235
355 void FontBuilder::setWeight(FontWeight fontWeight) 236 void FontBuilder::setWeight(FontWeight fontWeight)
356 { 237 {
357 FontDescriptionChangeScope scope(this); 238 FontDescriptionChangeScope scope(this);
358 239
359 scope.fontDescription().setWeight(fontWeight); 240 scope.fontDescription().setWeight(fontWeight);
360 } 241 }
361 242
243 void FontBuilder::setSize(const FontDescription::Size& size)
244 {
245 FontDescriptionChangeScope scope(this);
246
247 setSize(scope.fontDescription(), size);
248 }
249
362 void FontBuilder::setStretch(FontStretch fontStretch) 250 void FontBuilder::setStretch(FontStretch fontStretch)
363 { 251 {
364 FontDescriptionChangeScope scope(this); 252 FontDescriptionChangeScope scope(this);
365 253
366 scope.fontDescription().setStretch(fontStretch); 254 scope.fontDescription().setStretch(fontStretch);
367 } 255 }
368 256
369 void FontBuilder::setScript(const String& locale) 257 void FontBuilder::setScript(const String& locale)
370 { 258 {
371 FontDescriptionChangeScope scope(this); 259 FontDescriptionChangeScope scope(this);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 scope.fontDescription().setFontSmoothing(foontSmoothingMode); 304 scope.fontDescription().setFontSmoothing(foontSmoothingMode);
417 } 305 }
418 306
419 void FontBuilder::setFeatureSettings(PassRefPtr<FontFeatureSettings> settings) 307 void FontBuilder::setFeatureSettings(PassRefPtr<FontFeatureSettings> settings)
420 { 308 {
421 FontDescriptionChangeScope scope(this); 309 FontDescriptionChangeScope scope(this);
422 310
423 scope.fontDescription().setFeatureSettings(settings); 311 scope.fontDescription().setFeatureSettings(settings);
424 } 312 }
425 313
426 void FontBuilder::setSize(FontDescription& fontDescription, float effectiveZoom, float size) 314 void FontBuilder::setSize(FontDescription& fontDescription, const FontDescriptio n::Size& size)
427 { 315 {
428 fontDescription.setSpecifiedSize(size); 316 float specifiedSize = size.value;
429 fontDescription.setComputedSize(getComputedSizeFromSpecifiedSize(fontDescrip tion, effectiveZoom, size)); 317
318 if (!specifiedSize && size.keyword)
319 specifiedSize = FontSize::fontSizeForKeyword(m_document, size.keyword, f ontDescription.fixedPitchFontType());
320
321 if (specifiedSize < 0)
322 return;
323
324 // Overly large font sizes will cause crashes on some platforms (such as Win dows).
325 // Cap font size here to make sure that doesn't happen.
326 specifiedSize = std::min(maximumAllowedFontSize, specifiedSize);
327
328 fontDescription.setKeywordSize(size.keyword);
329 fontDescription.setSpecifiedSize(specifiedSize);
330 fontDescription.setIsAbsoluteSize(size.isAbsolute);
430 } 331 }
431 332
432 float FontBuilder::getComputedSizeFromSpecifiedSize(FontDescription& fontDescrip tion, float effectiveZoom, float specifiedSize) 333 float FontBuilder::getComputedSizeFromSpecifiedSize(FontDescription& fontDescrip tion, float effectiveZoom, float specifiedSize)
433 { 334 {
434 float zoomFactor = effectiveZoom; 335 float zoomFactor = effectiveZoom;
435 // FIXME: Why is this here!!!!?! 336 // FIXME: Why is this here!!!!?!
436 if (LocalFrame* frame = m_document->frame()) 337 if (LocalFrame* frame = m_document->frame())
437 zoomFactor *= frame->textZoomFactor(); 338 zoomFactor *= frame->textZoomFactor();
438 339
439 return FontSize::getComputedSizeFromSpecifiedSize(m_document, zoomFactor, fo ntDescription.isAbsoluteSize(), specifiedSize); 340 return FontSize::getComputedSizeFromSpecifiedSize(m_document, zoomFactor, fo ntDescription.isAbsoluteSize(), specifiedSize);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 if (scope.fontDescription().genericFamily() != FontDescription::MonospaceFam ily 409 if (scope.fontDescription().genericFamily() != FontDescription::MonospaceFam ily
509 && parentFontDescription.genericFamily() != FontDescription::MonospaceFa mily) 410 && parentFontDescription.genericFamily() != FontDescription::MonospaceFa mily)
510 return; 411 return;
511 412
512 // We know the parent is monospace or the child is monospace, and that font 413 // We know the parent is monospace or the child is monospace, and that font
513 // size was unspecified. We want to scale our font size as appropriate. 414 // size was unspecified. We want to scale our font size as appropriate.
514 // If the font uses a keyword size, then we refetch from the table rather th an 415 // If the font uses a keyword size, then we refetch from the table rather th an
515 // multiplying by our scale factor. 416 // multiplying by our scale factor.
516 float size; 417 float size;
517 if (scope.fontDescription().keywordSize()) { 418 if (scope.fontDescription().keywordSize()) {
518 size = FontSize::fontSizeForKeyword(m_document, static_cast<CSSValueID>( CSSValueXxSmall + scope.fontDescription().keywordSize() - 1), scope.fontDescript ion().fixedPitchFontType()); 419 size = FontSize::fontSizeForKeyword(m_document, scope.fontDescription(). keywordSize(), scope.fontDescription().fixedPitchFontType());
519 } else { 420 } else {
520 Settings* settings = m_document->settings(); 421 Settings* settings = m_document->settings();
521 float fixedScaleFactor = (settings && settings->defaultFixedFontSize() & & settings->defaultFontSize()) 422 float fixedScaleFactor = (settings && settings->defaultFixedFontSize() & & settings->defaultFontSize())
522 ? static_cast<float>(settings->defaultFixedFontSize()) / settings->d efaultFontSize() 423 ? static_cast<float>(settings->defaultFixedFontSize()) / settings->d efaultFontSize()
523 : 1; 424 : 1;
524 size = parentFontDescription.fixedPitchFontType() == FixedPitchFont ? 425 size = parentFontDescription.fixedPitchFontType() == FixedPitchFont ?
525 scope.fontDescription().specifiedSize() / fixedScaleFactor : 426 scope.fontDescription().specifiedSize() / fixedScaleFactor :
526 scope.fontDescription().specifiedSize() * fixedScaleFactor; 427 scope.fontDescription().specifiedSize() * fixedScaleFactor;
527 } 428 }
528 429
529 setSize(scope.fontDescription(), style->effectiveZoom(), size); 430 scope.fontDescription().setSpecifiedSize(size);
431 updateComputedSize(scope.fontDescription(), style->effectiveZoom());
530 } 432 }
531 433
532 void FontBuilder::updateComputedSize(RenderStyle* style, const RenderStyle* pare ntStyle) 434 void FontBuilder::updateComputedSize(RenderStyle* style)
533 { 435 {
534 FontDescriptionChangeScope scope(this); 436 FontDescriptionChangeScope scope(this);
437 updateComputedSize(scope.fontDescription(), style->effectiveZoom());
438 }
535 439
536 scope.fontDescription().setComputedSize(getComputedSizeFromSpecifiedSize(sco pe.fontDescription(), style->effectiveZoom(), scope.fontDescription().specifiedS ize())); 440 void FontBuilder::updateComputedSize(FontDescription& fontDescription, float eff ectiveZoom)
441 {
442 fontDescription.setComputedSize(getComputedSizeFromSpecifiedSize(fontDescrip tion, effectiveZoom, fontDescription.specifiedSize()));
537 } 443 }
538 444
539 // FIXME: style param should come first 445 // FIXME: style param should come first
540 void FontBuilder::createFont(PassRefPtrWillBeRawPtr<FontSelector> fontSelector, const RenderStyle* parentStyle, RenderStyle* style) 446 void FontBuilder::createFont(PassRefPtrWillBeRawPtr<FontSelector> fontSelector, const RenderStyle* parentStyle, RenderStyle* style)
541 { 447 {
542 if (!m_fontDirty) 448 if (!m_fontDirty)
543 return; 449 return;
544 450
545 updateComputedSize(style, parentStyle); 451 updateComputedSize(style, parentStyle);
546 checkForGenericFamilyChange(style, parentStyle); 452 checkForGenericFamilyChange(style, parentStyle);
547 checkForOrientationChange(style); 453 checkForOrientationChange(style);
548 style->font().update(fontSelector); 454 style->font().update(fontSelector);
549 m_fontDirty = false; 455 m_fontDirty = false;
550 } 456 }
551 457
552 void FontBuilder::createFontForDocument(PassRefPtrWillBeRawPtr<FontSelector> fon tSelector, RenderStyle* documentStyle) 458 void FontBuilder::createFontForDocument(PassRefPtrWillBeRawPtr<FontSelector> fon tSelector, RenderStyle* documentStyle)
553 { 459 {
554 FontDescription fontDescription = FontDescription(); 460 FontDescription fontDescription = FontDescription();
555 fontDescription.setScript(localeToScriptCodeForFontSelection(documentStyle-> locale())); 461 fontDescription.setScript(localeToScriptCodeForFontSelection(documentStyle-> locale()));
556 462
557 setFontFamilyToStandard(fontDescription, m_document); 463 setFontFamilyToStandard(fontDescription, m_document);
558 fontDescription.setKeywordSize(CSSValueMedium - CSSValueXxSmall + 1); 464
559 int size = FontSize::fontSizeForKeyword(m_document, CSSValueMedium, NonFixed PitchFont); 465 setSize(fontDescription, FontDescription::Size(FontSize::initialKeywordSize( ), 0.0f, false));
560 fontDescription.setSpecifiedSize(size); 466 updateComputedSize(fontDescription, documentStyle->effectiveZoom());
561 fontDescription.setComputedSize(getComputedSizeFromSpecifiedSize(fontDescrip tion, documentStyle->effectiveZoom(), size));
562 467
563 FontOrientation fontOrientation; 468 FontOrientation fontOrientation;
564 NonCJKGlyphOrientation glyphOrientation; 469 NonCJKGlyphOrientation glyphOrientation;
565 getFontAndGlyphOrientation(documentStyle, fontOrientation, glyphOrientation) ; 470 getFontAndGlyphOrientation(documentStyle, fontOrientation, glyphOrientation) ;
566 fontDescription.setOrientation(fontOrientation); 471 fontDescription.setOrientation(fontOrientation);
567 fontDescription.setNonCJKGlyphOrientation(glyphOrientation); 472 fontDescription.setNonCJKGlyphOrientation(glyphOrientation);
568 documentStyle->setFontDescription(fontDescription); 473 documentStyle->setFontDescription(fontDescription);
569 documentStyle->font().update(fontSelector); 474 documentStyle->font().update(fontSelector);
570 } 475 }
571 476
572 } 477 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698