• 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 "xfa/fde/css/cfde_csscomputedstyle.h"
8 
9 #include "third_party/base/stl_util.h"
10 #include "xfa/fde/css/cfde_cssstringvalue.h"
11 #include "xfa/fde/css/cfde_cssvaluelist.h"
12 
CFDE_CSSComputedStyle()13 CFDE_CSSComputedStyle::CFDE_CSSComputedStyle() {}
14 
~CFDE_CSSComputedStyle()15 CFDE_CSSComputedStyle::~CFDE_CSSComputedStyle() {}
16 
GetCustomStyle(const CFX_WideString & wsName,CFX_WideString & wsValue) const17 bool CFDE_CSSComputedStyle::GetCustomStyle(const CFX_WideString& wsName,
18                                            CFX_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 CFDE_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 CFX_WideString CFDE_CSSComputedStyle::GetFontFamily(int32_t index) const {
36   return m_InheritedData.m_pFontFamily->GetValue(index)
37       .As<CFDE_CSSStringValue>()
38       ->Value();
39 }
40 
GetFontWeight() const41 uint16_t CFDE_CSSComputedStyle::GetFontWeight() const {
42   return m_InheritedData.m_wFontWeight;
43 }
44 
GetFontVariant() const45 FDE_CSSFontVariant CFDE_CSSComputedStyle::GetFontVariant() const {
46   return m_InheritedData.m_eFontVariant;
47 }
48 
GetFontStyle() const49 FDE_CSSFontStyle CFDE_CSSComputedStyle::GetFontStyle() const {
50   return m_InheritedData.m_eFontStyle;
51 }
52 
GetFontSize() const53 FX_FLOAT CFDE_CSSComputedStyle::GetFontSize() const {
54   return m_InheritedData.m_fFontSize;
55 }
56 
GetColor() const57 FX_ARGB CFDE_CSSComputedStyle::GetColor() const {
58   return m_InheritedData.m_dwFontColor;
59 }
60 
SetFontWeight(uint16_t wFontWeight)61 void CFDE_CSSComputedStyle::SetFontWeight(uint16_t wFontWeight) {
62   m_InheritedData.m_wFontWeight = wFontWeight;
63 }
64 
SetFontVariant(FDE_CSSFontVariant eFontVariant)65 void CFDE_CSSComputedStyle::SetFontVariant(FDE_CSSFontVariant eFontVariant) {
66   m_InheritedData.m_eFontVariant = eFontVariant;
67 }
68 
SetFontStyle(FDE_CSSFontStyle eFontStyle)69 void CFDE_CSSComputedStyle::SetFontStyle(FDE_CSSFontStyle eFontStyle) {
70   m_InheritedData.m_eFontStyle = eFontStyle;
71 }
72 
SetFontSize(FX_FLOAT fFontSize)73 void CFDE_CSSComputedStyle::SetFontSize(FX_FLOAT fFontSize) {
74   m_InheritedData.m_fFontSize = fFontSize;
75 }
76 
SetColor(FX_ARGB dwFontColor)77 void CFDE_CSSComputedStyle::SetColor(FX_ARGB dwFontColor) {
78   m_InheritedData.m_dwFontColor = dwFontColor;
79 }
80 
GetBorderWidth() const81 const FDE_CSSRect* CFDE_CSSComputedStyle::GetBorderWidth() const {
82   return m_NonInheritedData.m_bHasBorder ? &(m_NonInheritedData.m_BorderWidth)
83                                          : nullptr;
84 }
85 
GetMarginWidth() const86 const FDE_CSSRect* CFDE_CSSComputedStyle::GetMarginWidth() const {
87   return m_NonInheritedData.m_bHasMargin ? &(m_NonInheritedData.m_MarginWidth)
88                                          : nullptr;
89 }
90 
GetPaddingWidth() const91 const FDE_CSSRect* CFDE_CSSComputedStyle::GetPaddingWidth() const {
92   return m_NonInheritedData.m_bHasPadding ? &(m_NonInheritedData.m_PaddingWidth)
93                                           : nullptr;
94 }
95 
SetMarginWidth(const FDE_CSSRect & rect)96 void CFDE_CSSComputedStyle::SetMarginWidth(const FDE_CSSRect& rect) {
97   m_NonInheritedData.m_MarginWidth = rect;
98   m_NonInheritedData.m_bHasMargin = true;
99 }
100 
SetPaddingWidth(const FDE_CSSRect & rect)101 void CFDE_CSSComputedStyle::SetPaddingWidth(const FDE_CSSRect& rect) {
102   m_NonInheritedData.m_PaddingWidth = rect;
103   m_NonInheritedData.m_bHasPadding = true;
104 }
105 
GetDisplay() const106 FDE_CSSDisplay CFDE_CSSComputedStyle::GetDisplay() const {
107   return m_NonInheritedData.m_eDisplay;
108 }
109 
GetLineHeight() const110 FX_FLOAT CFDE_CSSComputedStyle::GetLineHeight() const {
111   return m_InheritedData.m_fLineHeight;
112 }
113 
GetTextIndent() const114 const FDE_CSSLength& CFDE_CSSComputedStyle::GetTextIndent() const {
115   return m_InheritedData.m_TextIndent;
116 }
117 
GetTextAlign() const118 FDE_CSSTextAlign CFDE_CSSComputedStyle::GetTextAlign() const {
119   return m_InheritedData.m_eTextAlign;
120 }
121 
GetVerticalAlign() const122 FDE_CSSVerticalAlign CFDE_CSSComputedStyle::GetVerticalAlign() const {
123   return m_NonInheritedData.m_eVerticalAlign;
124 }
125 
GetNumberVerticalAlign() const126 FX_FLOAT CFDE_CSSComputedStyle::GetNumberVerticalAlign() const {
127   return m_NonInheritedData.m_fVerticalAlign;
128 }
129 
GetTextDecoration() const130 uint32_t CFDE_CSSComputedStyle::GetTextDecoration() const {
131   return m_NonInheritedData.m_dwTextDecoration;
132 }
133 
GetLetterSpacing() const134 const FDE_CSSLength& CFDE_CSSComputedStyle::GetLetterSpacing() const {
135   return m_InheritedData.m_LetterSpacing;
136 }
137 
SetLineHeight(FX_FLOAT fLineHeight)138 void CFDE_CSSComputedStyle::SetLineHeight(FX_FLOAT fLineHeight) {
139   m_InheritedData.m_fLineHeight = fLineHeight;
140 }
141 
SetTextIndent(const FDE_CSSLength & textIndent)142 void CFDE_CSSComputedStyle::SetTextIndent(const FDE_CSSLength& textIndent) {
143   m_InheritedData.m_TextIndent = textIndent;
144 }
145 
SetTextAlign(FDE_CSSTextAlign eTextAlign)146 void CFDE_CSSComputedStyle::SetTextAlign(FDE_CSSTextAlign eTextAlign) {
147   m_InheritedData.m_eTextAlign = eTextAlign;
148 }
149 
SetNumberVerticalAlign(FX_FLOAT fAlign)150 void CFDE_CSSComputedStyle::SetNumberVerticalAlign(FX_FLOAT fAlign) {
151   m_NonInheritedData.m_eVerticalAlign = FDE_CSSVerticalAlign::Number,
152   m_NonInheritedData.m_fVerticalAlign = fAlign;
153 }
154 
SetTextDecoration(uint32_t dwTextDecoration)155 void CFDE_CSSComputedStyle::SetTextDecoration(uint32_t dwTextDecoration) {
156   m_NonInheritedData.m_dwTextDecoration = dwTextDecoration;
157 }
158 
SetLetterSpacing(const FDE_CSSLength & letterSpacing)159 void CFDE_CSSComputedStyle::SetLetterSpacing(
160     const FDE_CSSLength& letterSpacing) {
161   m_InheritedData.m_LetterSpacing = letterSpacing;
162 }
163 
AddCustomStyle(const CFDE_CSSCustomProperty & prop)164 void CFDE_CSSComputedStyle::AddCustomStyle(const CFDE_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 CFDE_CSSComputedStyle::InheritedData::InheritedData()
171     : m_LetterSpacing(FDE_CSSLengthUnit::Normal),
172       m_WordSpacing(FDE_CSSLengthUnit::Normal),
173       m_TextIndent(FDE_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(FDE_CSSFontVariant::Normal),
180       m_eFontStyle(FDE_CSSFontStyle::Normal),
181       m_eTextAlign(FDE_CSSTextAlign::Left) {}
182 
~InheritedData()183 CFDE_CSSComputedStyle::InheritedData::~InheritedData() {}
184 
NonInheritedData()185 CFDE_CSSComputedStyle::NonInheritedData::NonInheritedData()
186     : m_MarginWidth(FDE_CSSLengthUnit::Point, 0),
187       m_BorderWidth(FDE_CSSLengthUnit::Point, 0),
188       m_PaddingWidth(FDE_CSSLengthUnit::Point, 0),
189       m_fVerticalAlign(0.0f),
190       m_eDisplay(FDE_CSSDisplay::Inline),
191       m_eVerticalAlign(FDE_CSSVerticalAlign::Baseline),
192       m_dwTextDecoration(0),
193       m_bHasMargin(false),
194       m_bHasBorder(false),
195       m_bHasPadding(false) {}
196