xref: /aosp_15_r20/external/pdfium/core/fxcrt/css/cfx_cssdata.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2018 The PDFium Authors
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_cssdata.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "core/fxcrt/css/cfx_cssstyleselector.h"
13 #include "core/fxcrt/css/cfx_cssvaluelistparser.h"
14 #include "core/fxcrt/fx_codepage.h"
15 #include "core/fxcrt/fx_extension.h"
16 
17 namespace {
18 
19 #undef CSS_PROP____
20 #define CSS_PROP____(a, b, c, d) {CFX_CSSProperty::a, c, d},
21 const CFX_CSSData::Property kPropertyTable[] = {
22 #include "core/fxcrt/css/properties.inc"
23 };
24 #undef CSS_PROP____
25 
26 #undef CSS_PROP_VALUE____
27 #define CSS_PROP_VALUE____(a, b, c) {CFX_CSSPropertyValue::a, c},
28 const CFX_CSSData::PropertyValue kPropertyValueTable[] = {
29 #include "core/fxcrt/css/property_values.inc"
30 };
31 #undef CSS_PROP_VALUE____
32 
33 const CFX_CSSData::LengthUnit kLengthUnitTable[] = {
34     {L"cm", CFX_CSSNumberValue::Unit::kCentiMeters},
35     {L"em", CFX_CSSNumberValue::Unit::kEMS},
36     {L"ex", CFX_CSSNumberValue::Unit::kEXS},
37     {L"in", CFX_CSSNumberValue::Unit::kInches},
38     {L"mm", CFX_CSSNumberValue::Unit::kMilliMeters},
39     {L"pc", CFX_CSSNumberValue::Unit::kPicas},
40     {L"pt", CFX_CSSNumberValue::Unit::kPoints},
41     {L"px", CFX_CSSNumberValue::Unit::kPixels},
42 };
43 
44 // 16 colours from CSS 2.0 + alternate spelling of grey/gray.
45 const CFX_CSSData::Color kColorTable[] = {
46     {L"aqua", 0xff00ffff},    {L"black", 0xff000000}, {L"blue", 0xff0000ff},
47     {L"fuchsia", 0xffff00ff}, {L"gray", 0xff808080},  {L"green", 0xff008000},
48     {L"grey", 0xff808080},    {L"lime", 0xff00ff00},  {L"maroon", 0xff800000},
49     {L"navy", 0xff000080},    {L"olive", 0xff808000}, {L"orange", 0xffffa500},
50     {L"purple", 0xff800080},  {L"red", 0xffff0000},   {L"silver", 0xffc0c0c0},
51     {L"teal", 0xff008080},    {L"white", 0xffffffff}, {L"yellow", 0xffffff00},
52 };
53 
54 }  // namespace
55 
GetPropertyByName(WideStringView name)56 const CFX_CSSData::Property* CFX_CSSData::GetPropertyByName(
57     WideStringView name) {
58   if (name.IsEmpty())
59     return nullptr;
60 
61   uint32_t hash = FX_HashCode_GetLoweredW(name);
62   auto* result = std::lower_bound(
63       std::begin(kPropertyTable), std::end(kPropertyTable), hash,
64       [](const CFX_CSSData::Property& iter, const uint32_t& hash) {
65         return iter.dwHash < hash;
66       });
67 
68   if (result != std::end(kPropertyTable) && result->dwHash == hash) {
69     return result;
70   }
71   return nullptr;
72 }
73 
GetPropertyByEnum(CFX_CSSProperty property)74 const CFX_CSSData::Property* CFX_CSSData::GetPropertyByEnum(
75     CFX_CSSProperty property) {
76   return &kPropertyTable[static_cast<uint8_t>(property)];
77 }
78 
GetPropertyValueByName(WideStringView wsName)79 const CFX_CSSData::PropertyValue* CFX_CSSData::GetPropertyValueByName(
80     WideStringView wsName) {
81   if (wsName.IsEmpty())
82     return nullptr;
83 
84   uint32_t hash = FX_HashCode_GetLoweredW(wsName);
85   auto* result = std::lower_bound(
86       std::begin(kPropertyValueTable), std::end(kPropertyValueTable), hash,
87       [](const PropertyValue& iter, const uint32_t& hash) {
88         return iter.dwHash < hash;
89       });
90 
91   if (result != std::end(kPropertyValueTable) && result->dwHash == hash) {
92     return result;
93   }
94   return nullptr;
95 }
96 
GetLengthUnitByName(WideStringView wsName)97 const CFX_CSSData::LengthUnit* CFX_CSSData::GetLengthUnitByName(
98     WideStringView wsName) {
99   if (wsName.IsEmpty() || wsName.GetLength() != 2)
100     return nullptr;
101 
102   WideString lowerName = WideString(wsName);
103   lowerName.MakeLower();
104 
105   for (auto* iter = std::begin(kLengthUnitTable);
106        iter != std::end(kLengthUnitTable); ++iter) {
107     if (lowerName == iter->value)
108       return iter;
109   }
110 
111   return nullptr;
112 }
113 
GetColorByName(WideStringView wsName)114 const CFX_CSSData::Color* CFX_CSSData::GetColorByName(WideStringView wsName) {
115   if (wsName.IsEmpty())
116     return nullptr;
117 
118   WideString lowerName = WideString(wsName);
119   lowerName.MakeLower();
120 
121   for (auto* iter = std::begin(kColorTable); iter != std::end(kColorTable);
122        ++iter) {
123     if (lowerName == iter->name)
124       return iter;
125   }
126   return nullptr;
127 }
128