• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/css/cfx_csscomputedstyle.h"
8 
9 #include "core/fxcrt/css/cfx_cssstringvalue.h"
10 #include "core/fxcrt/css/cfx_cssvaluelist.h"
11 
12 CFX_CSSComputedStyle::CFX_CSSComputedStyle() = default;
13 
14 CFX_CSSComputedStyle::~CFX_CSSComputedStyle() = default;
15 
GetCustomStyle(const WideString & wsName,WideString * pValue) const16 bool CFX_CSSComputedStyle::GetCustomStyle(const WideString& wsName,
17                                           WideString* pValue) const {
18   for (auto iter = m_CustomProperties.rbegin();
19        iter != m_CustomProperties.rend(); ++iter) {
20     if (wsName == iter->name()) {
21       *pValue = iter->value();
22       return true;
23     }
24   }
25   return false;
26 }
27 
CountFontFamilies() const28 int32_t CFX_CSSComputedStyle::CountFontFamilies() const {
29   return m_InheritedData.m_pFontFamily
30              ? m_InheritedData.m_pFontFamily->CountValues()
31              : 0;
32 }
33 
GetFontFamily(int32_t index) const34 const WideString CFX_CSSComputedStyle::GetFontFamily(int32_t index) const {
35   return m_InheritedData.m_pFontFamily->GetValue(index)
36       .As<CFX_CSSStringValue>()
37       ->Value();
38 }
39 
GetFontWeight() const40 uint16_t CFX_CSSComputedStyle::GetFontWeight() const {
41   return m_InheritedData.m_wFontWeight;
42 }
43 
GetFontVariant() const44 CFX_CSSFontVariant CFX_CSSComputedStyle::GetFontVariant() const {
45   return m_InheritedData.m_eFontVariant;
46 }
47 
GetFontStyle() const48 CFX_CSSFontStyle CFX_CSSComputedStyle::GetFontStyle() const {
49   return m_InheritedData.m_eFontStyle;
50 }
51 
GetFontSize() const52 float CFX_CSSComputedStyle::GetFontSize() const {
53   return m_InheritedData.m_fFontSize;
54 }
55 
GetColor() const56 FX_ARGB CFX_CSSComputedStyle::GetColor() const {
57   return m_InheritedData.m_dwFontColor;
58 }
59 
SetFontWeight(uint16_t wFontWeight)60 void CFX_CSSComputedStyle::SetFontWeight(uint16_t wFontWeight) {
61   m_InheritedData.m_wFontWeight = wFontWeight;
62 }
63 
SetFontVariant(CFX_CSSFontVariant eFontVariant)64 void CFX_CSSComputedStyle::SetFontVariant(CFX_CSSFontVariant eFontVariant) {
65   m_InheritedData.m_eFontVariant = eFontVariant;
66 }
67 
SetFontStyle(CFX_CSSFontStyle eFontStyle)68 void CFX_CSSComputedStyle::SetFontStyle(CFX_CSSFontStyle eFontStyle) {
69   m_InheritedData.m_eFontStyle = eFontStyle;
70 }
71 
SetFontSize(float fFontSize)72 void CFX_CSSComputedStyle::SetFontSize(float fFontSize) {
73   m_InheritedData.m_fFontSize = fFontSize;
74 }
75 
SetColor(FX_ARGB dwFontColor)76 void CFX_CSSComputedStyle::SetColor(FX_ARGB dwFontColor) {
77   m_InheritedData.m_dwFontColor = dwFontColor;
78 }
79 
GetBorderWidth() const80 const CFX_CSSRect* CFX_CSSComputedStyle::GetBorderWidth() const {
81   return m_NonInheritedData.m_bHasBorder ? &(m_NonInheritedData.m_BorderWidth)
82                                          : nullptr;
83 }
84 
GetMarginWidth() const85 const CFX_CSSRect* CFX_CSSComputedStyle::GetMarginWidth() const {
86   return m_NonInheritedData.m_bHasMargin ? &(m_NonInheritedData.m_MarginWidth)
87                                          : nullptr;
88 }
89 
GetPaddingWidth() const90 const CFX_CSSRect* CFX_CSSComputedStyle::GetPaddingWidth() const {
91   return m_NonInheritedData.m_bHasPadding ? &(m_NonInheritedData.m_PaddingWidth)
92                                           : nullptr;
93 }
94 
SetMarginWidth(const CFX_CSSRect & rect)95 void CFX_CSSComputedStyle::SetMarginWidth(const CFX_CSSRect& rect) {
96   m_NonInheritedData.m_MarginWidth = rect;
97   m_NonInheritedData.m_bHasMargin = true;
98 }
99 
SetPaddingWidth(const CFX_CSSRect & rect)100 void CFX_CSSComputedStyle::SetPaddingWidth(const CFX_CSSRect& rect) {
101   m_NonInheritedData.m_PaddingWidth = rect;
102   m_NonInheritedData.m_bHasPadding = true;
103 }
104 
GetDisplay() const105 CFX_CSSDisplay CFX_CSSComputedStyle::GetDisplay() const {
106   return m_NonInheritedData.m_eDisplay;
107 }
108 
GetLineHeight() const109 float CFX_CSSComputedStyle::GetLineHeight() const {
110   return m_InheritedData.m_fLineHeight;
111 }
112 
GetTextIndent() const113 const CFX_CSSLength& CFX_CSSComputedStyle::GetTextIndent() const {
114   return m_InheritedData.m_TextIndent;
115 }
116 
GetTextAlign() const117 CFX_CSSTextAlign CFX_CSSComputedStyle::GetTextAlign() const {
118   return m_InheritedData.m_eTextAlign;
119 }
120 
GetVerticalAlign() const121 CFX_CSSVerticalAlign CFX_CSSComputedStyle::GetVerticalAlign() const {
122   return m_NonInheritedData.m_eVerticalAlign;
123 }
124 
GetNumberVerticalAlign() const125 float CFX_CSSComputedStyle::GetNumberVerticalAlign() const {
126   return m_NonInheritedData.m_fVerticalAlign;
127 }
128 
GetTextDecoration() const129 uint32_t CFX_CSSComputedStyle::GetTextDecoration() const {
130   return m_NonInheritedData.m_dwTextDecoration;
131 }
132 
GetLetterSpacing() const133 const CFX_CSSLength& CFX_CSSComputedStyle::GetLetterSpacing() const {
134   return m_InheritedData.m_LetterSpacing;
135 }
136 
SetLineHeight(float fLineHeight)137 void CFX_CSSComputedStyle::SetLineHeight(float fLineHeight) {
138   m_InheritedData.m_fLineHeight = fLineHeight;
139 }
140 
SetTextIndent(const CFX_CSSLength & textIndent)141 void CFX_CSSComputedStyle::SetTextIndent(const CFX_CSSLength& textIndent) {
142   m_InheritedData.m_TextIndent = textIndent;
143 }
144 
SetTextAlign(CFX_CSSTextAlign eTextAlign)145 void CFX_CSSComputedStyle::SetTextAlign(CFX_CSSTextAlign eTextAlign) {
146   m_InheritedData.m_eTextAlign = eTextAlign;
147 }
148 
SetNumberVerticalAlign(float fAlign)149 void CFX_CSSComputedStyle::SetNumberVerticalAlign(float fAlign) {
150   m_NonInheritedData.m_eVerticalAlign = CFX_CSSVerticalAlign::Number,
151   m_NonInheritedData.m_fVerticalAlign = fAlign;
152 }
153 
SetTextDecoration(uint32_t dwTextDecoration)154 void CFX_CSSComputedStyle::SetTextDecoration(uint32_t dwTextDecoration) {
155   m_NonInheritedData.m_dwTextDecoration = dwTextDecoration;
156 }
157 
SetLetterSpacing(const CFX_CSSLength & letterSpacing)158 void CFX_CSSComputedStyle::SetLetterSpacing(
159     const CFX_CSSLength& letterSpacing) {
160   m_InheritedData.m_LetterSpacing = letterSpacing;
161 }
162 
AddCustomStyle(const CFX_CSSCustomProperty & prop)163 void CFX_CSSComputedStyle::AddCustomStyle(const CFX_CSSCustomProperty& prop) {
164   // Force the property to be copied so we aren't dependent on the lifetime
165   // of whatever currently owns it.
166   m_CustomProperties.push_back(prop);
167 }
168 
InheritedData()169 CFX_CSSComputedStyle::InheritedData::InheritedData()
170     : m_LetterSpacing(CFX_CSSLengthUnit::Normal, 0),
171       m_WordSpacing(CFX_CSSLengthUnit::Normal, 0),
172       m_TextIndent(CFX_CSSLengthUnit::Point, 0),
173       m_pFontFamily(nullptr),
174       m_fFontSize(12.0f),
175       m_fLineHeight(14.0f),
176       m_dwFontColor(0xFF000000),
177       m_wFontWeight(400),
178       m_eFontVariant(CFX_CSSFontVariant::Normal),
179       m_eFontStyle(CFX_CSSFontStyle::Normal),
180       m_eTextAlign(CFX_CSSTextAlign::Left) {}
181 
~InheritedData()182 CFX_CSSComputedStyle::InheritedData::~InheritedData() {}
183 
NonInheritedData()184 CFX_CSSComputedStyle::NonInheritedData::NonInheritedData()
185     : m_MarginWidth(CFX_CSSLengthUnit::Point, 0),
186       m_BorderWidth(CFX_CSSLengthUnit::Point, 0),
187       m_PaddingWidth(CFX_CSSLengthUnit::Point, 0),
188       m_fVerticalAlign(0.0f),
189       m_eDisplay(CFX_CSSDisplay::Inline),
190       m_eVerticalAlign(CFX_CSSVerticalAlign::Baseline),
191       m_dwTextDecoration(0),
192       m_bHasMargin(false),
193       m_bHasBorder(false),
194       m_bHasPadding(false) {}
195