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 #include "third_party/base/stl_util.h"
12
CFX_CSSComputedStyle()13 CFX_CSSComputedStyle::CFX_CSSComputedStyle() {}
14
~CFX_CSSComputedStyle()15 CFX_CSSComputedStyle::~CFX_CSSComputedStyle() {}
16
GetCustomStyle(const WideString & wsName,WideString & wsValue) const17 bool CFX_CSSComputedStyle::GetCustomStyle(const WideString& wsName,
18 WideString& wsValue) const {
19 for (auto iter = m_CustomProperties.rbegin();
20 iter != m_CustomProperties.rend(); iter++) {
21 if (wsName == iter->name()) {
22 wsValue = iter->value();
23 return true;
24 }
25 }
26 return false;
27 }
28
CountFontFamilies() const29 int32_t CFX_CSSComputedStyle::CountFontFamilies() const {
30 return m_InheritedData.m_pFontFamily
31 ? m_InheritedData.m_pFontFamily->CountValues()
32 : 0;
33 }
34
GetFontFamily(int32_t index) const35 const WideString CFX_CSSComputedStyle::GetFontFamily(int32_t index) const {
36 return m_InheritedData.m_pFontFamily->GetValue(index)
37 .As<CFX_CSSStringValue>()
38 ->Value();
39 }
40
GetFontWeight() const41 uint16_t CFX_CSSComputedStyle::GetFontWeight() const {
42 return m_InheritedData.m_wFontWeight;
43 }
44
GetFontVariant() const45 CFX_CSSFontVariant CFX_CSSComputedStyle::GetFontVariant() const {
46 return m_InheritedData.m_eFontVariant;
47 }
48
GetFontStyle() const49 CFX_CSSFontStyle CFX_CSSComputedStyle::GetFontStyle() const {
50 return m_InheritedData.m_eFontStyle;
51 }
52
GetFontSize() const53 float CFX_CSSComputedStyle::GetFontSize() const {
54 return m_InheritedData.m_fFontSize;
55 }
56
GetColor() const57 FX_ARGB CFX_CSSComputedStyle::GetColor() const {
58 return m_InheritedData.m_dwFontColor;
59 }
60
SetFontWeight(uint16_t wFontWeight)61 void CFX_CSSComputedStyle::SetFontWeight(uint16_t wFontWeight) {
62 m_InheritedData.m_wFontWeight = wFontWeight;
63 }
64
SetFontVariant(CFX_CSSFontVariant eFontVariant)65 void CFX_CSSComputedStyle::SetFontVariant(CFX_CSSFontVariant eFontVariant) {
66 m_InheritedData.m_eFontVariant = eFontVariant;
67 }
68
SetFontStyle(CFX_CSSFontStyle eFontStyle)69 void CFX_CSSComputedStyle::SetFontStyle(CFX_CSSFontStyle eFontStyle) {
70 m_InheritedData.m_eFontStyle = eFontStyle;
71 }
72
SetFontSize(float fFontSize)73 void CFX_CSSComputedStyle::SetFontSize(float fFontSize) {
74 m_InheritedData.m_fFontSize = fFontSize;
75 }
76
SetColor(FX_ARGB dwFontColor)77 void CFX_CSSComputedStyle::SetColor(FX_ARGB dwFontColor) {
78 m_InheritedData.m_dwFontColor = dwFontColor;
79 }
80
GetBorderWidth() const81 const CFX_CSSRect* CFX_CSSComputedStyle::GetBorderWidth() const {
82 return m_NonInheritedData.m_bHasBorder ? &(m_NonInheritedData.m_BorderWidth)
83 : nullptr;
84 }
85
GetMarginWidth() const86 const CFX_CSSRect* CFX_CSSComputedStyle::GetMarginWidth() const {
87 return m_NonInheritedData.m_bHasMargin ? &(m_NonInheritedData.m_MarginWidth)
88 : nullptr;
89 }
90
GetPaddingWidth() const91 const CFX_CSSRect* CFX_CSSComputedStyle::GetPaddingWidth() const {
92 return m_NonInheritedData.m_bHasPadding ? &(m_NonInheritedData.m_PaddingWidth)
93 : nullptr;
94 }
95
SetMarginWidth(const CFX_CSSRect & rect)96 void CFX_CSSComputedStyle::SetMarginWidth(const CFX_CSSRect& rect) {
97 m_NonInheritedData.m_MarginWidth = rect;
98 m_NonInheritedData.m_bHasMargin = true;
99 }
100
SetPaddingWidth(const CFX_CSSRect & rect)101 void CFX_CSSComputedStyle::SetPaddingWidth(const CFX_CSSRect& rect) {
102 m_NonInheritedData.m_PaddingWidth = rect;
103 m_NonInheritedData.m_bHasPadding = true;
104 }
105
GetDisplay() const106 CFX_CSSDisplay CFX_CSSComputedStyle::GetDisplay() const {
107 return m_NonInheritedData.m_eDisplay;
108 }
109
GetLineHeight() const110 float CFX_CSSComputedStyle::GetLineHeight() const {
111 return m_InheritedData.m_fLineHeight;
112 }
113
GetTextIndent() const114 const CFX_CSSLength& CFX_CSSComputedStyle::GetTextIndent() const {
115 return m_InheritedData.m_TextIndent;
116 }
117
GetTextAlign() const118 CFX_CSSTextAlign CFX_CSSComputedStyle::GetTextAlign() const {
119 return m_InheritedData.m_eTextAlign;
120 }
121
GetVerticalAlign() const122 CFX_CSSVerticalAlign CFX_CSSComputedStyle::GetVerticalAlign() const {
123 return m_NonInheritedData.m_eVerticalAlign;
124 }
125
GetNumberVerticalAlign() const126 float CFX_CSSComputedStyle::GetNumberVerticalAlign() const {
127 return m_NonInheritedData.m_fVerticalAlign;
128 }
129
GetTextDecoration() const130 uint32_t CFX_CSSComputedStyle::GetTextDecoration() const {
131 return m_NonInheritedData.m_dwTextDecoration;
132 }
133
GetLetterSpacing() const134 const CFX_CSSLength& CFX_CSSComputedStyle::GetLetterSpacing() const {
135 return m_InheritedData.m_LetterSpacing;
136 }
137
SetLineHeight(float fLineHeight)138 void CFX_CSSComputedStyle::SetLineHeight(float fLineHeight) {
139 m_InheritedData.m_fLineHeight = fLineHeight;
140 }
141
SetTextIndent(const CFX_CSSLength & textIndent)142 void CFX_CSSComputedStyle::SetTextIndent(const CFX_CSSLength& textIndent) {
143 m_InheritedData.m_TextIndent = textIndent;
144 }
145
SetTextAlign(CFX_CSSTextAlign eTextAlign)146 void CFX_CSSComputedStyle::SetTextAlign(CFX_CSSTextAlign eTextAlign) {
147 m_InheritedData.m_eTextAlign = eTextAlign;
148 }
149
SetNumberVerticalAlign(float fAlign)150 void CFX_CSSComputedStyle::SetNumberVerticalAlign(float fAlign) {
151 m_NonInheritedData.m_eVerticalAlign = CFX_CSSVerticalAlign::Number,
152 m_NonInheritedData.m_fVerticalAlign = fAlign;
153 }
154
SetTextDecoration(uint32_t dwTextDecoration)155 void CFX_CSSComputedStyle::SetTextDecoration(uint32_t dwTextDecoration) {
156 m_NonInheritedData.m_dwTextDecoration = dwTextDecoration;
157 }
158
SetLetterSpacing(const CFX_CSSLength & letterSpacing)159 void CFX_CSSComputedStyle::SetLetterSpacing(
160 const CFX_CSSLength& letterSpacing) {
161 m_InheritedData.m_LetterSpacing = letterSpacing;
162 }
163
AddCustomStyle(const CFX_CSSCustomProperty & prop)164 void CFX_CSSComputedStyle::AddCustomStyle(const CFX_CSSCustomProperty& prop) {
165 // Force the property to be copied so we aren't dependent on the lifetime
166 // of whatever currently owns it.
167 m_CustomProperties.push_back(prop);
168 }
169
InheritedData()170 CFX_CSSComputedStyle::InheritedData::InheritedData()
171 : m_LetterSpacing(CFX_CSSLengthUnit::Normal),
172 m_WordSpacing(CFX_CSSLengthUnit::Normal),
173 m_TextIndent(CFX_CSSLengthUnit::Point, 0),
174 m_pFontFamily(nullptr),
175 m_fFontSize(12.0f),
176 m_fLineHeight(14.0f),
177 m_dwFontColor(0xFF000000),
178 m_wFontWeight(400),
179 m_eFontVariant(CFX_CSSFontVariant::Normal),
180 m_eFontStyle(CFX_CSSFontStyle::Normal),
181 m_eTextAlign(CFX_CSSTextAlign::Left) {}
182
~InheritedData()183 CFX_CSSComputedStyle::InheritedData::~InheritedData() {}
184
NonInheritedData()185 CFX_CSSComputedStyle::NonInheritedData::NonInheritedData()
186 : m_MarginWidth(CFX_CSSLengthUnit::Point, 0),
187 m_BorderWidth(CFX_CSSLengthUnit::Point, 0),
188 m_PaddingWidth(CFX_CSSLengthUnit::Point, 0),
189 m_fVerticalAlign(0.0f),
190 m_eDisplay(CFX_CSSDisplay::Inline),
191 m_eVerticalAlign(CFX_CSSVerticalAlign::Baseline),
192 m_dwTextDecoration(0),
193 m_bHasMargin(false),
194 m_bHasBorder(false),
195 m_bHasPadding(false) {}
196