fde_cssdatatable.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
1// Copyright 2014 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/fde_cssdatatable.h"
8
9#include <utility>
10
11#include "core/fxcrt/fx_ext.h"
12#include "xfa/fde/css/cfde_cssstyleselector.h"
13#include "xfa/fde/css/cfde_cssvaluelistparser.h"
14#include "xfa/fgas/crt/fgas_codepage.h"
15
16static const FDE_CSSPropertyTable g_FDE_CSSProperties[] = {
17    {FDE_CSSProperty::BorderLeft, L"border-left", 0x04080036,
18     FDE_CSSVALUETYPE_Shorthand},
19    {FDE_CSSProperty::Top, L"top", 0x0BEDAF33,
20     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
21         FDE_CSSVALUETYPE_MaybeNumber},
22    {FDE_CSSProperty::Margin, L"margin", 0x0CB016BE,
23     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeEnum |
24         FDE_CSSVALUETYPE_MaybeNumber},
25    {FDE_CSSProperty::TextIndent, L"text-indent", 0x169ADB74,
26     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber},
27    {FDE_CSSProperty::Right, L"right", 0x193ADE3E,
28     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
29         FDE_CSSVALUETYPE_MaybeNumber},
30    {FDE_CSSProperty::PaddingLeft, L"padding-left", 0x228CF02F,
31     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber},
32    {FDE_CSSProperty::MarginLeft, L"margin-left", 0x297C5656,
33     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber |
34         FDE_CSSVALUETYPE_MaybeEnum},
35    {FDE_CSSProperty::Border, L"border", 0x2A23349E,
36     FDE_CSSVALUETYPE_Shorthand},
37    {FDE_CSSProperty::BorderTop, L"border-top", 0x2B866ADE,
38     FDE_CSSVALUETYPE_Shorthand},
39    {FDE_CSSProperty::Bottom, L"bottom", 0x399F02B5,
40     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
41         FDE_CSSVALUETYPE_MaybeNumber},
42    {FDE_CSSProperty::PaddingRight, L"padding-right", 0x3F616AC2,
43     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber},
44    {FDE_CSSProperty::BorderBottom, L"border-bottom", 0x452CE780,
45     FDE_CSSVALUETYPE_Shorthand},
46    {FDE_CSSProperty::FontFamily, L"font-family", 0x574686E6,
47     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeString},
48    {FDE_CSSProperty::FontWeight, L"font-weight", 0x6692F60C,
49     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
50         FDE_CSSVALUETYPE_MaybeNumber},
51    {FDE_CSSProperty::Color, L"color", 0x6E67921F,
52     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
53         FDE_CSSVALUETYPE_MaybeColor},
54    {FDE_CSSProperty::LetterSpacing, L"letter-spacing", 0x70536102,
55     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
56         FDE_CSSVALUETYPE_MaybeNumber},
57    {FDE_CSSProperty::TextAlign, L"text-align", 0x7553F1BD,
58     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum},
59    {FDE_CSSProperty::BorderRightWidth, L"border-right-width", 0x8F5A6036,
60     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
61         FDE_CSSVALUETYPE_MaybeNumber},
62    {FDE_CSSProperty::VerticalAlign, L"vertical-align", 0x934A87D2,
63     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
64         FDE_CSSVALUETYPE_MaybeNumber},
65    {FDE_CSSProperty::PaddingTop, L"padding-top", 0x959D22B7,
66     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber},
67    {FDE_CSSProperty::FontVariant, L"font-variant", 0x9C785779,
68     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum},
69    {FDE_CSSProperty::BorderWidth, L"border-width", 0xA8DE4FEB,
70     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeEnum |
71         FDE_CSSVALUETYPE_MaybeNumber},
72    {FDE_CSSProperty::BorderBottomWidth, L"border-bottom-width", 0xAE41204D,
73     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
74         FDE_CSSVALUETYPE_MaybeNumber},
75    {FDE_CSSProperty::BorderRight, L"border-right", 0xB78E9EA9,
76     FDE_CSSVALUETYPE_Shorthand},
77    {FDE_CSSProperty::FontSize, L"font-size", 0xB93956DF,
78     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
79         FDE_CSSVALUETYPE_MaybeNumber},
80    {FDE_CSSProperty::BorderSpacing, L"border-spacing", 0xC72030F0,
81     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeNumber},
82    {FDE_CSSProperty::FontStyle, L"font-style", 0xCB1950F5,
83     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum},
84    {FDE_CSSProperty::Font, L"font", 0xCD308B77, FDE_CSSVALUETYPE_Shorthand},
85    {FDE_CSSProperty::LineHeight, L"line-height", 0xCFCACE2E,
86     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
87         FDE_CSSVALUETYPE_MaybeNumber},
88    {FDE_CSSProperty::MarginRight, L"margin-right", 0xD13C58C9,
89     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber |
90         FDE_CSSVALUETYPE_MaybeEnum},
91    {FDE_CSSProperty::BorderLeftWidth, L"border-left-width", 0xD1E93D83,
92     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
93         FDE_CSSVALUETYPE_MaybeNumber},
94    {FDE_CSSProperty::Display, L"display", 0xD4224C36,
95     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum},
96    {FDE_CSSProperty::PaddingBottom, L"padding-bottom", 0xE555B3B9,
97     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber},
98    {FDE_CSSProperty::BorderTopWidth, L"border-top-width", 0xED2CB62B,
99     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
100         FDE_CSSVALUETYPE_MaybeNumber},
101    {FDE_CSSProperty::WordSpacing, L"word-spacing", 0xEDA63BAE,
102     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
103         FDE_CSSVALUETYPE_MaybeNumber},
104    {FDE_CSSProperty::Left, L"left", 0xF5AD782B,
105     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeEnum |
106         FDE_CSSVALUETYPE_MaybeNumber},
107    {FDE_CSSProperty::TextDecoration, L"text-decoration", 0xF7C634BA,
108     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeEnum},
109    {FDE_CSSProperty::Padding, L"padding", 0xF8C373F7,
110     FDE_CSSVALUETYPE_List | FDE_CSSVALUETYPE_MaybeNumber},
111    {FDE_CSSProperty::MarginBottom, L"margin-bottom", 0xF93485A0,
112     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber |
113         FDE_CSSVALUETYPE_MaybeEnum},
114    {FDE_CSSProperty::MarginTop, L"margin-top", 0xFE51DCFE,
115     FDE_CSSVALUETYPE_Primitive | FDE_CSSVALUETYPE_MaybeNumber |
116         FDE_CSSVALUETYPE_MaybeEnum},
117};
118const int32_t g_iCSSPropertyCount =
119    sizeof(g_FDE_CSSProperties) / sizeof(FDE_CSSPropertyTable);
120static_assert(g_iCSSPropertyCount ==
121                  static_cast<int32_t>(FDE_CSSProperty::LAST_MARKER),
122              "Property table differs in size from property enum");
123
124static const FDE_CSSPropertyValueTable g_FDE_CSSPropertyValues[] = {
125    {FDE_CSSPropertyValue::Bolder, L"bolder", 0x009F1058},
126    {FDE_CSSPropertyValue::None, L"none", 0x048B6670},
127    {FDE_CSSPropertyValue::Dot, L"dot", 0x0A48CB27},
128    {FDE_CSSPropertyValue::Sub, L"sub", 0x0BD37FAA},
129    {FDE_CSSPropertyValue::Top, L"top", 0x0BEDAF33},
130    {FDE_CSSPropertyValue::Right, L"right", 0x193ADE3E},
131    {FDE_CSSPropertyValue::Normal, L"normal", 0x247CF3E9},
132    {FDE_CSSPropertyValue::Auto, L"auto", 0x2B35B6D9},
133    {FDE_CSSPropertyValue::Text, L"text", 0x2D08AF85},
134    {FDE_CSSPropertyValue::XSmall, L"x-small", 0x2D2FCAFE},
135    {FDE_CSSPropertyValue::Thin, L"thin", 0x2D574D53},
136    {FDE_CSSPropertyValue::Small, L"small", 0x316A3739},
137    {FDE_CSSPropertyValue::Bottom, L"bottom", 0x399F02B5},
138    {FDE_CSSPropertyValue::Underline, L"underline", 0x3A0273A6},
139    {FDE_CSSPropertyValue::Double, L"double", 0x3D98515B},
140    {FDE_CSSPropertyValue::Lighter, L"lighter", 0x45BEB7AF},
141    {FDE_CSSPropertyValue::Oblique, L"oblique", 0x53EBDDB1},
142    {FDE_CSSPropertyValue::Super, L"super", 0x6A4F842F},
143    {FDE_CSSPropertyValue::Center, L"center", 0x6C51AFC1},
144    {FDE_CSSPropertyValue::XxLarge, L"xx-large", 0x70BB1508},
145    {FDE_CSSPropertyValue::Smaller, L"smaller", 0x849769F0},
146    {FDE_CSSPropertyValue::Baseline, L"baseline", 0x87436BA3},
147    {FDE_CSSPropertyValue::Thick, L"thick", 0x8CC35EB3},
148    {FDE_CSSPropertyValue::Justify, L"justify", 0x8D269CAE},
149    {FDE_CSSPropertyValue::Middle, L"middle", 0x947FA00F},
150    {FDE_CSSPropertyValue::Medium, L"medium", 0xA084A381},
151    {FDE_CSSPropertyValue::ListItem, L"list-item", 0xA32382B8},
152    {FDE_CSSPropertyValue::XxSmall, L"xx-small", 0xADE1FC76},
153    {FDE_CSSPropertyValue::Bold, L"bold", 0xB18313A1},
154    {FDE_CSSPropertyValue::SmallCaps, L"small-caps", 0xB299428D},
155    {FDE_CSSPropertyValue::Inline, L"inline", 0xC02D649F},
156    {FDE_CSSPropertyValue::Overline, L"overline", 0xC0EC9FA4},
157    {FDE_CSSPropertyValue::TextBottom, L"text-bottom", 0xC7D08D87},
158    {FDE_CSSPropertyValue::Larger, L"larger", 0xCD3C409D},
159    {FDE_CSSPropertyValue::InlineTable, L"inline-table", 0xD131F494},
160    {FDE_CSSPropertyValue::InlineBlock, L"inline-block", 0xD26A8BD7},
161    {FDE_CSSPropertyValue::Blink, L"blink", 0xDC36E390},
162    {FDE_CSSPropertyValue::Block, L"block", 0xDCD480AB},
163    {FDE_CSSPropertyValue::Italic, L"italic", 0xE31D5396},
164    {FDE_CSSPropertyValue::LineThrough, L"line-through", 0xE4C5A276},
165    {FDE_CSSPropertyValue::XLarge, L"x-large", 0xF008E390},
166    {FDE_CSSPropertyValue::Large, L"large", 0xF4434FCB},
167    {FDE_CSSPropertyValue::Left, L"left", 0xF5AD782B},
168    {FDE_CSSPropertyValue::TextTop, L"text-top", 0xFCB58D45},
169};
170const int32_t g_iCSSPropertyValueCount =
171    sizeof(g_FDE_CSSPropertyValues) / sizeof(FDE_CSSPropertyValueTable);
172static_assert(g_iCSSPropertyValueCount ==
173                  static_cast<int32_t>(FDE_CSSPropertyValue::LAST_MARKER),
174              "Property value table differs in size from property value enum");
175
176static const FDE_CSSMEDIATYPETABLE g_FDE_CSSMediaTypes[] = {
177    {0xF09, FDE_CSSMEDIATYPE_Emboss},    {FDE_CSSMEDIATYPE_Screen},
178    {0x536A, FDE_CSSMEDIATYPE_TV},       {0x741D, FDE_CSSMEDIATYPE_Projection},
179    {0x76ED, FDE_CSSMEDIATYPE_Print},    {0x7CFB, FDE_CSSMEDIATYPE_Braille},
180    {0x9578, FDE_CSSMEDIATYPE_Handheld}, {0xC8E1, FDE_CSSMEDIATYPE_TTY},
181    {0xD0F9, FDE_CSSMEDIATYPE_ALL},
182};
183
184static const FDE_CSSLengthUnitTable g_FDE_CSSLengthUnits[] = {
185    {0x0672, FDE_CSSNumberType::EMS},
186    {0x067D, FDE_CSSNumberType::EXS},
187    {0x1AF7, FDE_CSSNumberType::Inches},
188    {0x2F7A, FDE_CSSNumberType::MilliMeters},
189    {0x3ED3, FDE_CSSNumberType::Picas},
190    {0x3EE4, FDE_CSSNumberType::Points},
191    {0x3EE8, FDE_CSSNumberType::Pixels},
192    {0xFC30, FDE_CSSNumberType::CentiMeters},
193};
194
195static const FDE_CSSCOLORTABLE g_FDE_CSSColors[] = {
196    {0x031B47FE, 0xff000080}, {0x0BB8DF5B, 0xffff0000},
197    {0x0D82A78C, 0xff800000}, {0x2ACC82E8, 0xff00ffff},
198    {0x2D083986, 0xff008080}, {0x4A6A6195, 0xffc0c0c0},
199    {0x546A8EF3, 0xff808080}, {0x65C9169C, 0xffffa500},
200    {0x8422BB61, 0xffffffff}, {0x9271A558, 0xff800080},
201    {0xA65A3EE3, 0xffff00ff}, {0xB1345708, 0xff0000ff},
202    {0xB6D2CF1F, 0xff808000}, {0xD19B5E1C, 0xffffff00},
203    {0xDB64391D, 0xff000000}, {0xF616D507, 0xff00ff00},
204    {0xF6EFFF31, 0xff008000},
205};
206
207static const FDE_CSSPseudoTable g_FDE_CSSPseudoType[] = {
208    {FDE_CSSPseudo::After, L":after", 0x16EE1FEC},
209    {FDE_CSSPseudo::Before, L":before", 0x7DCDDE2D},
210};
211
212const FDE_CSSPseudoTable* FDE_GetCSSPseudoByEnum(FDE_CSSPseudo ePseudo) {
213  return g_FDE_CSSPseudoType + static_cast<int>(ePseudo);
214}
215
216const FDE_CSSPropertyTable* FDE_GetCSSPropertyByName(
217    const CFX_WideStringC& wsName) {
218  ASSERT(!wsName.IsEmpty());
219  uint32_t dwHash = FX_HashCode_GetW(wsName, true);
220  int32_t iEnd = g_iCSSPropertyCount;
221  int32_t iMid, iStart = 0;
222  uint32_t dwMid;
223  do {
224    iMid = (iStart + iEnd) / 2;
225    dwMid = g_FDE_CSSProperties[iMid].dwHash;
226    if (dwHash == dwMid) {
227      return g_FDE_CSSProperties + iMid;
228    } else if (dwHash > dwMid) {
229      iStart = iMid + 1;
230    } else {
231      iEnd = iMid - 1;
232    }
233  } while (iStart <= iEnd);
234  return nullptr;
235}
236
237const FDE_CSSPropertyTable* FDE_GetCSSPropertyByEnum(FDE_CSSProperty eName) {
238  return g_FDE_CSSProperties + static_cast<int>(eName);
239}
240
241const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByName(
242    const CFX_WideStringC& wsName) {
243  ASSERT(!wsName.IsEmpty());
244  uint32_t dwHash = FX_HashCode_GetW(wsName, true);
245  int32_t iEnd = g_iCSSPropertyValueCount;
246  int32_t iMid, iStart = 0;
247  uint32_t dwMid;
248  do {
249    iMid = (iStart + iEnd) / 2;
250    dwMid = g_FDE_CSSPropertyValues[iMid].dwHash;
251    if (dwHash == dwMid) {
252      return g_FDE_CSSPropertyValues + iMid;
253    } else if (dwHash > dwMid) {
254      iStart = iMid + 1;
255    } else {
256      iEnd = iMid - 1;
257    }
258  } while (iStart <= iEnd);
259  return nullptr;
260}
261
262const FDE_CSSMEDIATYPETABLE* FDE_GetCSSMediaTypeByName(
263    const CFX_WideStringC& wsName) {
264  ASSERT(!wsName.IsEmpty());
265  uint16_t wHash = FX_HashCode_GetW(wsName, true);
266  int32_t iEnd =
267      sizeof(g_FDE_CSSMediaTypes) / sizeof(FDE_CSSMEDIATYPETABLE) - 1;
268  int32_t iMid, iStart = 0;
269  uint16_t uMid;
270  do {
271    iMid = (iStart + iEnd) / 2;
272    uMid = g_FDE_CSSMediaTypes[iMid].wHash;
273    if (wHash == uMid) {
274      return g_FDE_CSSMediaTypes + iMid;
275    } else if (wHash > uMid) {
276      iStart = iMid + 1;
277    } else {
278      iEnd = iMid - 1;
279    }
280  } while (iStart <= iEnd);
281  return nullptr;
282}
283
284const FDE_CSSLengthUnitTable* FDE_GetCSSLengthUnitByName(
285    const CFX_WideStringC& wsName) {
286  ASSERT(!wsName.IsEmpty());
287  uint16_t wHash = FX_HashCode_GetW(wsName, true);
288  int32_t iEnd =
289      sizeof(g_FDE_CSSLengthUnits) / sizeof(FDE_CSSLengthUnitTable) - 1;
290  int32_t iMid, iStart = 0;
291  uint16_t wMid;
292  do {
293    iMid = (iStart + iEnd) / 2;
294    wMid = g_FDE_CSSLengthUnits[iMid].wHash;
295    if (wHash == wMid) {
296      return g_FDE_CSSLengthUnits + iMid;
297    } else if (wHash > wMid) {
298      iStart = iMid + 1;
299    } else {
300      iEnd = iMid - 1;
301    }
302  } while (iStart <= iEnd);
303  return nullptr;
304}
305
306const FDE_CSSCOLORTABLE* FDE_GetCSSColorByName(const CFX_WideStringC& wsName) {
307  ASSERT(!wsName.IsEmpty());
308  uint32_t dwHash = FX_HashCode_GetW(wsName, true);
309  int32_t iEnd = sizeof(g_FDE_CSSColors) / sizeof(FDE_CSSCOLORTABLE) - 1;
310  int32_t iMid, iStart = 0;
311  uint32_t dwMid;
312  do {
313    iMid = (iStart + iEnd) / 2;
314    dwMid = g_FDE_CSSColors[iMid].dwHash;
315    if (dwHash == dwMid) {
316      return g_FDE_CSSColors + iMid;
317    } else if (dwHash > dwMid) {
318      iStart = iMid + 1;
319    } else {
320      iEnd = iMid - 1;
321    }
322  } while (iStart <= iEnd);
323  return nullptr;
324}
325