1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fde/css/cfde_cssstyleselector.h"
#include <algorithm>
#include "third_party/base/ptr_util.h"
#include "xfa/fde/css/cfde_cssaccelerator.h"
#include "xfa/fde/css/cfde_csscolorvalue.h"
#include "xfa/fde/css/cfde_csscomputedstyle.h"
#include "xfa/fde/css/cfde_csscustomproperty.h"
#include "xfa/fde/css/cfde_cssdeclaration.h"
#include "xfa/fde/css/cfde_cssenumvalue.h"
#include "xfa/fde/css/cfde_csspropertyholder.h"
#include "xfa/fde/css/cfde_cssselector.h"
#include "xfa/fde/css/cfde_csssyntaxparser.h"
#include "xfa/fde/css/cfde_csstagcache.h"
#include "xfa/fde/css/cfde_cssvaluelist.h"
#include "xfa/fxfa/app/cxfa_csstagprovider.h"
namespace {
template <class T>
T* ToValue(CFDE_CSSValue* val) {
return static_cast<T*>(val);
}
template <class T>
const T* ToValue(const CFDE_CSSValue* val) {
return static_cast<T*>(val);
}
} // namespace
#define FDE_CSSUNIVERSALHASH ('*')
CFDE_CSSStyleSelector::CFDE_CSSStyleSelector(CFGAS_FontMgr* pFontMgr)
: m_pFontMgr(pFontMgr), m_fDefFontSize(12.0f) {
m_ePriorities[static_cast<int32_t>(FDE_CSSStyleSheetPriority::High)] =
FDE_CSSStyleSheetGroup::Author;
m_ePriorities[static_cast<int32_t>(FDE_CSSStyleSheetPriority::Mid)] =
FDE_CSSStyleSheetGroup::User;
m_ePriorities[static_cast<int32_t>(FDE_CSSStyleSheetPriority::Low)] =
FDE_CSSStyleSheetGroup::UserAgent;
}
CFDE_CSSStyleSelector::~CFDE_CSSStyleSelector() {
Reset();
}
void CFDE_CSSStyleSelector::SetDefFontSize(FX_FLOAT fFontSize) {
ASSERT(fFontSize > 0);
m_fDefFontSize = fFontSize;
}
CFDE_CSSAccelerator* CFDE_CSSStyleSelector::InitAccelerator() {
if (!m_pAccelerator)
m_pAccelerator = pdfium::MakeUnique<CFDE_CSSAccelerator>();
m_pAccelerator->Clear();
return m_pAccelerator.get();
}
CFDE_CSSComputedStyle* CFDE_CSSStyleSelector::CreateComputedStyle(
CFDE_CSSComputedStyle* pParentStyle) {
CFDE_CSSComputedStyle* pStyle = new CFDE_CSSComputedStyle();
if (pParentStyle)
pStyle->m_InheritedData = pParentStyle->m_InheritedData;
return pStyle;
}
bool CFDE_CSSStyleSelector::SetStyleSheet(FDE_CSSStyleSheetGroup eType,
CFDE_CSSStyleSheet* pSheet) {
CFX_ArrayTemplate<CFDE_CSSStyleSheet*>& dest =
m_SheetGroups[static_cast<int32_t>(eType)];
dest.RemoveAt(0, dest.GetSize());
if (pSheet)
dest.Add(pSheet);
return true;
}
bool CFDE_CSSStyleSelector::SetStyleSheets(
FDE_CSSStyleSheetGroup eType,
const CFX_ArrayTemplate<CFDE_CSSStyleSheet*>* pArray) {
CFX_ArrayTemplate<CFDE_CSSStyleSheet*>& dest =
m_SheetGroups[static_cast<int32_t>(eType)];
if (pArray)
dest.Copy(*pArray);
else
dest.RemoveAt(0, dest.GetSize());
return true;
}
void CFDE_CSSStyleSelector::SetStylePriority(
FDE_CSSStyleSheetGroup eType,
FDE_CSSStyleSheetPriority ePriority) {
m_ePriorities[static_cast<int32_t>(ePriority)] = eType;
}
void CFDE_CSSStyleSelector::UpdateStyleIndex(uint32_t dwMediaList) {
Reset();
// TODO(dsinclair): Hard coded size bad. This should probably just be a map.
for (int32_t iGroup = 0; iGroup < 3; ++iGroup) {
CFDE_CSSRuleCollection& rules = m_RuleCollection[iGroup];
rules.AddRulesFrom(m_SheetGroups[iGroup], dwMediaList, m_pFontMgr);
}
}
void CFDE_CSSStyleSelector::Reset() {
// TODO(dsinclair): Hard coded size bad. This should probably just be a map.
for (int32_t iGroup = 0; iGroup < 3; ++iGroup) {
m_RuleCollection[iGroup].Clear();
}
}
int32_t CFDE_CSSStyleSelector::MatchDeclarations(
CXFA_CSSTagProvider* pTag,
CFX_ArrayTemplate<CFDE_CSSDeclaration*>& matchedDecls,
FDE_CSSPseudo ePseudoType) {
ASSERT(pTag);
CFDE_CSSTagCache* pCache = m_pAccelerator->top();
ASSERT(pCache && pCache->GetTag() == pTag);
matchedDecls.RemoveAt(0, matchedDecls.GetSize());
// TODO(dsinclair): Hard coded size bad ...
for (int32_t ePriority = 2; ePriority >= 0; --ePriority) {
FDE_CSSStyleSheetGroup eGroup = m_ePriorities[ePriority];
CFDE_CSSRuleCollection& rules =
m_RuleCollection[static_cast<int32_t>(eGroup)];
if (rules.CountSelectors() == 0)
continue;
if (ePseudoType == FDE_CSSPseudo::NONE) {
MatchRules(pCache, rules.GetUniversalRuleData(), ePseudoType);
if (pCache->HashTag()) {
MatchRules(pCache, rules.GetTagRuleData(pCache->HashTag()),
ePseudoType);
}
int32_t iCount = pCache->CountHashClass();
for (int32_t i = 0; i < iCount; i++) {
pCache->SetClassIndex(i);
MatchRules(pCache, rules.GetClassRuleData(pCache->HashClass()),
ePseudoType);
}
} else {
MatchRules(pCache, rules.GetPseudoRuleData(), ePseudoType);
}
std::sort(m_MatchedRules.begin(), m_MatchedRules.end(),
[](const CFDE_CSSRuleCollection::Data* p1,
const CFDE_CSSRuleCollection::Data* p2) {
return p1->dwPriority < p2->dwPriority;
});
for (const auto& rule : m_MatchedRules)
matchedDecls.Add(rule->pDeclaration);
m_MatchedRules.clear();
}
return matchedDecls.GetSize();
}
void CFDE_CSSStyleSelector::MatchRules(CFDE_CSSTagCache* pCache,
CFDE_CSSRuleCollection::Data* pList,
FDE_CSSPseudo ePseudoType) {
while (pList) {
if (MatchSelector(pCache, pList->pSelector, ePseudoType))
m_MatchedRules.push_back(pList);
pList = pList->pNext;
}
}
bool CFDE_CSSStyleSelector::MatchSelector(CFDE_CSSTagCache* pCache,
CFDE_CSSSelector* pSel,
FDE_CSSPseudo ePseudoType) {
uint32_t dwHash;
while (pSel && pCache) {
switch (pSel->GetType()) {
case FDE_CSSSelectorType::Descendant:
dwHash = pSel->GetNameHash();
while ((pCache = pCache->GetParent()) != nullptr) {
if (dwHash != FDE_CSSUNIVERSALHASH && dwHash != pCache->HashTag()) {
continue;
}
if (MatchSelector(pCache, pSel->GetNextSelector(), ePseudoType)) {
return true;
}
}
return false;
case FDE_CSSSelectorType::ID:
dwHash = pCache->HashID();
if (dwHash != pSel->GetNameHash()) {
return false;
}
break;
case FDE_CSSSelectorType::Class:
dwHash = pCache->HashClass();
if (dwHash != pSel->GetNameHash()) {
return false;
}
break;
case FDE_CSSSelectorType::Element:
dwHash = pSel->GetNameHash();
if (dwHash != FDE_CSSUNIVERSALHASH && dwHash != pCache->HashTag()) {
return false;
}
break;
case FDE_CSSSelectorType::Pseudo:
dwHash = FDE_GetCSSPseudoByEnum(ePseudoType)->dwHash;
if (dwHash != pSel->GetNameHash()) {
return false;
}
break;
default:
ASSERT(false);
break;
}
pSel = pSel->GetNextSelector();
}
return !pSel && pCache;
}
void CFDE_CSSStyleSelector::ComputeStyle(
CXFA_CSSTagProvider* pTag,
const CFDE_CSSDeclaration** ppDeclArray,
int32_t iDeclCount,
CFDE_CSSComputedStyle* pDestStyle) {
ASSERT(iDeclCount >= 0);
ASSERT(pDestStyle);
static const uint32_t s_dwStyleHash = FX_HashCode_GetW(L"style", true);
static const uint32_t s_dwAlignHash = FX_HashCode_GetW(L"align", true);
if (!pTag->empty()) {
CFDE_CSSDeclaration* pDecl = nullptr;
for (auto it : *pTag) {
CFX_WideString wsAttri = it.first;
CFX_WideString wsValue = it.second;
uint32_t dwAttriHash = FX_HashCode_GetW(wsAttri.AsStringC(), true);
if (dwAttriHash == s_dwStyleHash) {
if (!pDecl)
pDecl = new CFDE_CSSDeclaration;
AppendInlineStyle(pDecl, wsValue.c_str(), wsValue.GetLength());
} else if (dwAttriHash == s_dwAlignHash) {
if (!pDecl)
pDecl = new CFDE_CSSDeclaration;
FDE_CSSPropertyArgs args;
args.pStringCache = nullptr;
args.pProperty = FDE_GetCSSPropertyByEnum(FDE_CSSProperty::TextAlign);
pDecl->AddProperty(&args, wsValue.c_str(), wsValue.GetLength());
}
}
if (pDecl) {
CFX_ArrayTemplate<CFDE_CSSDeclaration*> decls;
decls.SetSize(iDeclCount + 1);
CFDE_CSSDeclaration** ppInline = decls.GetData();
FXSYS_memcpy(ppInline, ppDeclArray,
iDeclCount * sizeof(CFDE_CSSDeclaration*));
ppInline[iDeclCount++] = pDecl;
ApplyDeclarations(true, const_cast<const CFDE_CSSDeclaration**>(ppInline),
iDeclCount, pDestStyle);
ApplyDeclarations(false,
const_cast<const CFDE_CSSDeclaration**>(ppInline),
iDeclCount, pDestStyle);
return;
}
}
if (iDeclCount > 0) {
ASSERT(ppDeclArray);
ApplyDeclarations(true, ppDeclArray, iDeclCount, pDestStyle);
ApplyDeclarations(false, ppDeclArray, iDeclCount, pDestStyle);
}
}
void CFDE_CSSStyleSelector::ApplyDeclarations(
bool bPriority,
const CFDE_CSSDeclaration** ppDeclArray,
int32_t iDeclCount,
CFDE_CSSComputedStyle* pDestStyle) {
CFDE_CSSComputedStyle* pComputedStyle = pDestStyle;
int32_t i;
if (bPriority) {
CFDE_CSSValue* pLastest = nullptr;
CFDE_CSSValue* pImportant = nullptr;
for (i = 0; i < iDeclCount; ++i) {
bool bImportant;
CFDE_CSSValue* pVal =
ppDeclArray[i]->GetProperty(FDE_CSSProperty::FontSize, bImportant);
if (!pVal)
continue;
if (bImportant)
pImportant = pVal;
else
pLastest = pVal;
}
if (pImportant) {
ApplyProperty(FDE_CSSProperty::FontSize, pImportant, pComputedStyle);
} else if (pLastest) {
ApplyProperty(FDE_CSSProperty::FontSize, pLastest, pComputedStyle);
}
} else {
CFX_ArrayTemplate<CFDE_CSSDeclaration*> importants;
const CFDE_CSSDeclaration* pDecl = nullptr;
for (i = 0; i < iDeclCount; ++i) {
pDecl = ppDeclArray[i];
for (auto it = pDecl->begin(); it != pDecl->end(); it++) {
if ((*it)->eProperty == FDE_CSSProperty::FontSize)
continue;
if (!(*it)->bImportant) {
ApplyProperty((*it)->eProperty, (*it)->pValue.Get(), pComputedStyle);
} else if (importants.GetSize() == 0 ||
importants[importants.GetUpperBound()] != pDecl) {
importants.Add(const_cast<CFDE_CSSDeclaration*>(pDecl));
}
}
}
iDeclCount = importants.GetSize();
for (i = 0; i < iDeclCount; ++i) {
pDecl = importants[i];
for (auto it = pDecl->begin(); it != pDecl->end(); it++) {
if ((*it)->bImportant && (*it)->eProperty != FDE_CSSProperty::FontSize)
ApplyProperty((*it)->eProperty, (*it)->pValue.Get(), pComputedStyle);
}
}
for (auto it = pDecl->custom_begin(); it != pDecl->custom_end(); it++) {
pComputedStyle->AddCustomStyle((*it)->pwsName, (*it)->pwsValue);
}
}
}
void CFDE_CSSStyleSelector::AppendInlineStyle(CFDE_CSSDeclaration* pDecl,
const FX_WCHAR* psz,
int32_t iLen) {
ASSERT(pDecl && psz && iLen > 0);
auto pSyntax = pdfium::MakeUnique<CFDE_CSSSyntaxParser>();
if (!pSyntax->Init(psz, iLen, 32, true))
return;
int32_t iLen2 = 0;
const FX_WCHAR* psz2;
FDE_CSSPropertyArgs args;
args.pStringCache = nullptr;
args.pProperty = nullptr;
CFX_WideString wsName;
while (1) {
FDE_CSSSyntaxStatus eStatus = pSyntax->DoSyntaxParse();
if (eStatus == FDE_CSSSyntaxStatus::PropertyName) {
psz2 = pSyntax->GetCurrentString(iLen2);
args.pProperty = FDE_GetCSSPropertyByName(CFX_WideStringC(psz2, iLen2));
if (!args.pProperty)
wsName = CFX_WideStringC(psz2, iLen2);
} else if (eStatus == FDE_CSSSyntaxStatus::PropertyValue) {
if (args.pProperty) {
psz2 = pSyntax->GetCurrentString(iLen2);
if (iLen2 > 0)
pDecl->AddProperty(&args, psz2, iLen2);
} else if (iLen2 > 0) {
psz2 = pSyntax->GetCurrentString(iLen2);
if (iLen2 > 0) {
pDecl->AddProperty(&args, wsName.c_str(), wsName.GetLength(), psz2,
iLen2);
}
}
} else {
break;
}
}
}
void CFDE_CSSStyleSelector::ApplyProperty(
FDE_CSSProperty eProperty,
CFDE_CSSValue* pValue,
CFDE_CSSComputedStyle* pComputedStyle) {
if (pValue->GetType() != FDE_CSSPrimitiveType::List) {
FDE_CSSPrimitiveType eType = pValue->GetType();
switch (eProperty) {
case FDE_CSSProperty::Display:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_NonInheritedData.m_eDisplay =
ToDisplay(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
}
break;
case FDE_CSSProperty::FontSize: {
FX_FLOAT& fFontSize = pComputedStyle->m_InheritedData.m_fFontSize;
if (eType == FDE_CSSPrimitiveType::Number) {
fFontSize = ToValue<CFDE_CSSNumberValue>(pValue)->Apply(fFontSize);
} else if (eType == FDE_CSSPrimitiveType::Enum) {
fFontSize = ToFontSize(ToValue<CFDE_CSSEnumValue>(pValue)->Value(),
fFontSize);
}
} break;
case FDE_CSSProperty::LineHeight:
if (eType == FDE_CSSPrimitiveType::Number) {
const CFDE_CSSNumberValue* v = ToValue<CFDE_CSSNumberValue>(pValue);
if (v->Kind() == FDE_CSSNumberType::Number) {
pComputedStyle->m_InheritedData.m_fLineHeight =
v->Value() * pComputedStyle->m_InheritedData.m_fFontSize;
} else {
pComputedStyle->m_InheritedData.m_fLineHeight =
v->Apply(pComputedStyle->m_InheritedData.m_fFontSize);
}
}
break;
case FDE_CSSProperty::TextAlign:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_eTextAlign =
ToTextAlign(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
}
break;
case FDE_CSSProperty::TextIndent:
SetLengthWithPercent(pComputedStyle->m_InheritedData.m_TextIndent,
eType, pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
break;
case FDE_CSSProperty::FontWeight:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_wFontWeight =
ToFontWeight(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
} else if (eType == FDE_CSSPrimitiveType::Number) {
int32_t iValue =
(int32_t)ToValue<CFDE_CSSNumberValue>(pValue)->Value() / 100;
if (iValue >= 1 && iValue <= 9) {
pComputedStyle->m_InheritedData.m_wFontWeight = iValue * 100;
}
}
break;
case FDE_CSSProperty::FontStyle:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_eFontStyle =
ToFontStyle(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
}
break;
case FDE_CSSProperty::Color:
if (eType == FDE_CSSPrimitiveType::RGB) {
pComputedStyle->m_InheritedData.m_dwFontColor =
ToValue<CFDE_CSSColorValue>(pValue)->Value();
}
break;
case FDE_CSSProperty::MarginLeft:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_MarginWidth.left, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasMargin = true;
}
break;
case FDE_CSSProperty::MarginTop:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_MarginWidth.top, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasMargin = true;
}
break;
case FDE_CSSProperty::MarginRight:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_MarginWidth.right, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasMargin = true;
}
break;
case FDE_CSSProperty::MarginBottom:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_MarginWidth.bottom, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasMargin = true;
}
break;
case FDE_CSSProperty::PaddingLeft:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_PaddingWidth.left, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasPadding = true;
}
break;
case FDE_CSSProperty::PaddingTop:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_PaddingWidth.top, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasPadding = true;
}
break;
case FDE_CSSProperty::PaddingRight:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_PaddingWidth.right, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasPadding = true;
}
break;
case FDE_CSSProperty::PaddingBottom:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_PaddingWidth.bottom, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasPadding = true;
}
break;
case FDE_CSSProperty::BorderLeftWidth:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_BorderWidth.left, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasBorder = true;
}
break;
case FDE_CSSProperty::BorderTopWidth:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_BorderWidth.top, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasBorder = true;
}
break;
case FDE_CSSProperty::BorderRightWidth:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_BorderWidth.right, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasBorder = true;
}
break;
case FDE_CSSProperty::BorderBottomWidth:
if (SetLengthWithPercent(
pComputedStyle->m_NonInheritedData.m_BorderWidth.bottom, eType,
pValue, pComputedStyle->m_InheritedData.m_fFontSize)) {
pComputedStyle->m_NonInheritedData.m_bHasBorder = true;
}
break;
case FDE_CSSProperty::VerticalAlign:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_NonInheritedData.m_eVerticalAlign =
ToVerticalAlign(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
} else if (eType == FDE_CSSPrimitiveType::Number) {
pComputedStyle->m_NonInheritedData.m_eVerticalAlign =
FDE_CSSVerticalAlign::Number;
pComputedStyle->m_NonInheritedData.m_fVerticalAlign =
ToValue<CFDE_CSSNumberValue>(pValue)->Apply(
pComputedStyle->m_InheritedData.m_fFontSize);
}
break;
case FDE_CSSProperty::FontVariant:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_eFontVariant =
ToFontVariant(ToValue<CFDE_CSSEnumValue>(pValue)->Value());
}
break;
case FDE_CSSProperty::LetterSpacing:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_LetterSpacing.Set(
FDE_CSSLengthUnit::Normal);
} else if (eType == FDE_CSSPrimitiveType::Number) {
if (ToValue<CFDE_CSSNumberValue>(pValue)->Kind() ==
FDE_CSSNumberType::Percent) {
break;
}
SetLengthWithPercent(pComputedStyle->m_InheritedData.m_LetterSpacing,
eType, pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
}
break;
case FDE_CSSProperty::WordSpacing:
if (eType == FDE_CSSPrimitiveType::Enum) {
pComputedStyle->m_InheritedData.m_WordSpacing.Set(
FDE_CSSLengthUnit::Normal);
} else if (eType == FDE_CSSPrimitiveType::Number) {
if (ToValue<CFDE_CSSNumberValue>(pValue)->Kind() ==
FDE_CSSNumberType::Percent) {
break;
}
SetLengthWithPercent(pComputedStyle->m_InheritedData.m_WordSpacing,
eType, pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
}
break;
case FDE_CSSProperty::Top:
SetLengthWithPercent(pComputedStyle->m_NonInheritedData.m_Top, eType,
pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
break;
case FDE_CSSProperty::Bottom:
SetLengthWithPercent(pComputedStyle->m_NonInheritedData.m_Bottom, eType,
pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
break;
case FDE_CSSProperty::Left:
SetLengthWithPercent(pComputedStyle->m_NonInheritedData.m_Left, eType,
pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
break;
case FDE_CSSProperty::Right:
SetLengthWithPercent(pComputedStyle->m_NonInheritedData.m_Right, eType,
pValue,
pComputedStyle->m_InheritedData.m_fFontSize);
break;
default:
break;
}
} else if (pValue->GetType() == FDE_CSSPrimitiveType::List) {
CFDE_CSSValueList* pList = ToValue<CFDE_CSSValueList>(pValue);
int32_t iCount = pList->CountValues();
if (iCount > 0) {
switch (eProperty) {
case FDE_CSSProperty::FontFamily:
pComputedStyle->m_InheritedData.m_pFontFamily = pList;
break;
case FDE_CSSProperty::TextDecoration:
pComputedStyle->m_NonInheritedData.m_dwTextDecoration =
ToTextDecoration(pList);
break;
default:
break;
}
}
} else {
ASSERT(false);
}
}
FDE_CSSDisplay CFDE_CSSStyleSelector::ToDisplay(FDE_CSSPropertyValue eValue) {
switch (eValue) {
case FDE_CSSPropertyValue::Block:
return FDE_CSSDisplay::Block;
case FDE_CSSPropertyValue::None:
return FDE_CSSDisplay::None;
case FDE_CSSPropertyValue::ListItem:
return FDE_CSSDisplay::ListItem;
case FDE_CSSPropertyValue::InlineTable:
return FDE_CSSDisplay::InlineTable;
case FDE_CSSPropertyValue::InlineBlock:
return FDE_CSSDisplay::InlineBlock;
case FDE_CSSPropertyValue::Inline:
default:
return FDE_CSSDisplay::Inline;
}
}
FDE_CSSTextAlign CFDE_CSSStyleSelector::ToTextAlign(
FDE_CSSPropertyValue eValue) {
switch (eValue) {
case FDE_CSSPropertyValue::Center:
return FDE_CSSTextAlign::Center;
case FDE_CSSPropertyValue::Right:
return FDE_CSSTextAlign::Right;
case FDE_CSSPropertyValue::Justify:
return FDE_CSSTextAlign::Justify;
case FDE_CSSPropertyValue::Left:
default:
return FDE_CSSTextAlign::Left;
}
}
uint16_t CFDE_CSSStyleSelector::ToFontWeight(FDE_CSSPropertyValue eValue) {
switch (eValue) {
case FDE_CSSPropertyValue::Bold:
return 700;
case FDE_CSSPropertyValue::Bolder:
return 900;
case FDE_CSSPropertyValue::Lighter:
return 200;
case FDE_CSSPropertyValue::Normal:
default:
return 400;
}
}
FDE_CSSFontStyle CFDE_CSSStyleSelector::ToFontStyle(
FDE_CSSPropertyValue eValue) {
switch (eValue) {
case FDE_CSSPropertyValue::Italic:
case FDE_CSSPropertyValue::Oblique:
return FDE_CSSFontStyle::Italic;
default:
return FDE_CSSFontStyle::Normal;
}
}
bool CFDE_CSSStyleSelector::SetLengthWithPercent(FDE_CSSLength& width,
FDE_CSSPrimitiveType eType,
CFDE_CSSValue* pValue,
FX_FLOAT fFontSize) {
if (eType == FDE_CSSPrimitiveType::Number) {
const CFDE_CSSNumberValue* v = ToValue<CFDE_CSSNumberValue>(pValue);
if (v->Kind() == FDE_CSSNumberType::Percent) {
width.Set(FDE_CSSLengthUnit::Percent,
ToValue<CFDE_CSSNumberValue>(pValue)->Value() / 100.0f);
return width.NonZero();
}
FX_FLOAT fValue = v->Apply(fFontSize);
width.Set(FDE_CSSLengthUnit::Point, fValue);
return width.NonZero();
} else if (eType == FDE_CSSPrimitiveType::Enum) {
switch (ToValue<CFDE_CSSEnumValue>(pValue)->Value()) {
case FDE_CSSPropertyValue::Auto:
width.Set(FDE_CSSLengthUnit::Auto);
return true;
case FDE_CSSPropertyValue::None:
width.Set(FDE_CSSLengthUnit::None);
return true;
case FDE_CSSPropertyValue::Thin:
width.Set(FDE_CSSLengthUnit::Point, 2);
return true;
case FDE_CSSPropertyValue::Medium:
width.Set(FDE_CSSLengthUnit::Point, 3);
return true;
case FDE_CSSPropertyValue::Thick:
width.Set(FDE_CSSLengthUnit::Point, 4);
return true;
default:
return false;
}
}
return false;
}
FX_FLOAT CFDE_CSSStyleSelector::ToFontSize(FDE_CSSPropertyValue eValue,
FX_FLOAT fCurFontSize) {
switch (eValue) {
case FDE_CSSPropertyValue::XxSmall:
return m_fDefFontSize / 1.2f / 1.2f / 1.2f;
case FDE_CSSPropertyValue::XSmall:
return m_fDefFontSize / 1.2f / 1.2f;
case FDE_CSSPropertyValue::Small:
return m_fDefFontSize / 1.2f;
case FDE_CSSPropertyValue::Medium:
return m_fDefFontSize;
case FDE_CSSPropertyValue::Large:
return m_fDefFontSize * 1.2f;
case FDE_CSSPropertyValue::XLarge:
return m_fDefFontSize * 1.2f * 1.2f;
case FDE_CSSPropertyValue::XxLarge:
return m_fDefFontSize * 1.2f * 1.2f * 1.2f;
case FDE_CSSPropertyValue::Larger:
return fCurFontSize * 1.2f;
case FDE_CSSPropertyValue::Smaller:
return fCurFontSize / 1.2f;
default:
return fCurFontSize;
}
}
FDE_CSSVerticalAlign CFDE_CSSStyleSelector::ToVerticalAlign(
FDE_CSSPropertyValue eValue) {
switch (eValue) {
case FDE_CSSPropertyValue::Middle:
return FDE_CSSVerticalAlign::Middle;
case FDE_CSSPropertyValue::Bottom:
return FDE_CSSVerticalAlign::Bottom;
case FDE_CSSPropertyValue::Super:
return FDE_CSSVerticalAlign::Super;
case FDE_CSSPropertyValue::Sub:
return FDE_CSSVerticalAlign::Sub;
case FDE_CSSPropertyValue::Top:
return FDE_CSSVerticalAlign::Top;
case FDE_CSSPropertyValue::TextTop:
return FDE_CSSVerticalAlign::TextTop;
case FDE_CSSPropertyValue::TextBottom:
return FDE_CSSVerticalAlign::TextBottom;
case FDE_CSSPropertyValue::Baseline:
default:
return FDE_CSSVerticalAlign::Baseline;
}
}
uint32_t CFDE_CSSStyleSelector::ToTextDecoration(CFDE_CSSValueList* pValue) {
uint32_t dwDecoration = 0;
for (int32_t i = pValue->CountValues() - 1; i >= 0; --i) {
CFDE_CSSValue* pVal = pValue->GetValue(i);
if (pVal->GetType() != FDE_CSSPrimitiveType::Enum)
continue;
switch (ToValue<CFDE_CSSEnumValue>(pVal)->Value()) {
case FDE_CSSPropertyValue::Underline:
dwDecoration |= FDE_CSSTEXTDECORATION_Underline;
break;
case FDE_CSSPropertyValue::LineThrough:
dwDecoration |= FDE_CSSTEXTDECORATION_LineThrough;
break;
case FDE_CSSPropertyValue::Overline:
dwDecoration |= FDE_CSSTEXTDECORATION_Overline;
break;
case FDE_CSSPropertyValue::Blink:
dwDecoration |= FDE_CSSTEXTDECORATION_Blink;
break;
case FDE_CSSPropertyValue::Double:
dwDecoration |= FDE_CSSTEXTDECORATION_Double;
break;
default:
break;
}
}
return dwDecoration;
}
FDE_CSSFontVariant CFDE_CSSStyleSelector::ToFontVariant(
FDE_CSSPropertyValue eValue) {
return eValue == FDE_CSSPropertyValue::SmallCaps
? FDE_CSSFontVariant::SmallCaps
: FDE_CSSFontVariant::Normal;
}
|