Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / css / CSSToLengthConversionData.cpp
blobe4311a3ff38df7c3b6c0c4a2abec6ee0ef848a5c
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "core/css/CSSToLengthConversionData.h"
34 #include "core/layout/LayoutView.h"
35 #include "core/style/ComputedStyle.h"
37 namespace blink {
39 CSSToLengthConversionData::FontSizes::FontSizes(float em, float rem, const Font* font)
40 : m_em(em)
41 , m_rem(rem)
42 , m_font(font)
44 // FIXME: Improve RAII of StyleResolverState to use const Font&.
45 ASSERT(m_font);
48 CSSToLengthConversionData::FontSizes::FontSizes(const ComputedStyle* style, const ComputedStyle* rootStyle)
49 : FontSizes(style->computedFontSize(), rootStyle ? rootStyle->computedFontSize() : 1.0f, &style->font())
53 float CSSToLengthConversionData::FontSizes::ex() const
55 ASSERT(m_font);
56 // FIXME: We have a bug right now where the zoom will be applied twice to EX units.
57 // We really need to compute EX using fontMetrics for the original specifiedSize and not use
58 // our actual constructed layoutObject font.
59 if (!m_font->fontMetrics().hasXHeight())
60 return m_em / 2.0f;
61 return m_font->fontMetrics().xHeight();
64 float CSSToLengthConversionData::FontSizes::ch() const
66 ASSERT(m_font);
67 return m_font->fontMetrics().zeroWidth();
70 CSSToLengthConversionData::ViewportSize::ViewportSize(const LayoutView* layoutView)
71 : m_width(layoutView ? layoutView->layoutViewportWidth() : 0)
72 , m_height(layoutView ? layoutView->layoutViewportHeight() : 0)
76 CSSToLengthConversionData::CSSToLengthConversionData(const ComputedStyle* style, const FontSizes& fontSizes, const ViewportSize& viewportSize, float zoom)
77 : m_style(style)
78 , m_fontSizes(fontSizes)
79 , m_viewportSize(viewportSize)
80 , m_zoom(clampTo<float>(zoom, std::numeric_limits<float>::denorm_min()))
82 ASSERT(m_style);
85 CSSToLengthConversionData::CSSToLengthConversionData(const ComputedStyle* style, const ComputedStyle* rootStyle, const LayoutView* layoutView, float zoom)
86 : CSSToLengthConversionData(style, FontSizes(style, rootStyle), ViewportSize(layoutView), zoom)
90 double CSSToLengthConversionData::viewportWidthPercent() const
92 m_style->setHasViewportUnits();
93 return m_viewportSize.width() / 100;
95 double CSSToLengthConversionData::viewportHeightPercent() const
97 m_style->setHasViewportUnits();
98 return m_viewportSize.height() / 100;
100 double CSSToLengthConversionData::viewportMinPercent() const
102 m_style->setHasViewportUnits();
103 return std::min(m_viewportSize.width(), m_viewportSize.height()) / 100;
105 double CSSToLengthConversionData::viewportMaxPercent() const
107 m_style->setHasViewportUnits();
108 return std::max(m_viewportSize.width(), m_viewportSize.height()) / 100;
111 float CSSToLengthConversionData::remFontSize() const
113 m_style->setHasRemUnits();
114 return m_fontSizes.rem();
117 } // namespace blink