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/fxfa/parser/cxfa_font.h"
8
9 #include "fxjs/xfa/cjx_font.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_fill.h"
12 #include "xfa/fxfa/parser/cxfa_measurement.h"
13
14 namespace {
15
16 const CXFA_Node::PropertyData kPropertyData[] = {{XFA_Element::Fill, 1, 0},
17 {XFA_Element::Extras, 1, 0},
18 {XFA_Element::Unknown, 0, 0}};
19 const CXFA_Node::AttributeData kAttributeData[] = {
20 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
21 {XFA_Attribute::LineThrough, XFA_AttributeType::Integer, (void*)0},
22 {XFA_Attribute::Typeface, XFA_AttributeType::CData, (void*)L"Courier"},
23 {XFA_Attribute::FontHorizontalScale, XFA_AttributeType::CData,
24 (void*)L"100%"},
25 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
26 {XFA_Attribute::KerningMode, XFA_AttributeType::Enum,
27 (void*)XFA_AttributeEnum::None},
28 {XFA_Attribute::Underline, XFA_AttributeType::Integer, (void*)0},
29 {XFA_Attribute::BaselineShift, XFA_AttributeType::Measure, (void*)L"0in"},
30 {XFA_Attribute::OverlinePeriod, XFA_AttributeType::Enum,
31 (void*)XFA_AttributeEnum::All},
32 {XFA_Attribute::LetterSpacing, XFA_AttributeType::CData, nullptr},
33 {XFA_Attribute::LineThroughPeriod, XFA_AttributeType::Enum,
34 (void*)XFA_AttributeEnum::All},
35 {XFA_Attribute::FontVerticalScale, XFA_AttributeType::CData,
36 (void*)L"100%"},
37 {XFA_Attribute::PsName, XFA_AttributeType::CData, nullptr},
38 {XFA_Attribute::Size, XFA_AttributeType::Measure, (void*)L"10pt"},
39 {XFA_Attribute::Posture, XFA_AttributeType::Enum,
40 (void*)XFA_AttributeEnum::Normal},
41 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
42 {XFA_Attribute::Weight, XFA_AttributeType::Enum,
43 (void*)XFA_AttributeEnum::Normal},
44 {XFA_Attribute::UnderlinePeriod, XFA_AttributeType::Enum,
45 (void*)XFA_AttributeEnum::All},
46 {XFA_Attribute::Overline, XFA_AttributeType::Integer, (void*)0},
47 {XFA_Attribute::GenericFamily, XFA_AttributeType::Enum,
48 (void*)XFA_AttributeEnum::Serif},
49 {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
50
51 constexpr wchar_t kName[] = L"font";
52
53 } // namespace
54
CXFA_Font(CXFA_Document * doc,XFA_PacketType packet)55 CXFA_Font::CXFA_Font(CXFA_Document* doc, XFA_PacketType packet)
56 : CXFA_Node(
57 doc,
58 packet,
59 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Config | XFA_XDPPACKET_Form),
60 XFA_ObjectType::Node,
61 XFA_Element::Font,
62 kPropertyData,
63 kAttributeData,
64 kName,
65 pdfium::MakeUnique<CJX_Font>(this)) {}
66
~CXFA_Font()67 CXFA_Font::~CXFA_Font() {}
68
GetBaselineShift() const69 float CXFA_Font::GetBaselineShift() const {
70 return JSObject()
71 ->GetMeasure(XFA_Attribute::BaselineShift)
72 .ToUnit(XFA_Unit::Pt);
73 }
74
GetHorizontalScale()75 float CXFA_Font::GetHorizontalScale() {
76 WideString wsValue = JSObject()->GetCData(XFA_Attribute::FontHorizontalScale);
77 int32_t iScale = FXSYS_wtoi(wsValue.c_str());
78 return iScale > 0 ? (float)iScale : 100.0f;
79 }
80
GetVerticalScale()81 float CXFA_Font::GetVerticalScale() {
82 WideString wsValue = JSObject()->GetCData(XFA_Attribute::FontVerticalScale);
83 int32_t iScale = FXSYS_wtoi(wsValue.c_str());
84 return iScale > 0 ? (float)iScale : 100.0f;
85 }
86
GetLetterSpacing()87 float CXFA_Font::GetLetterSpacing() {
88 WideString wsValue = JSObject()->GetCData(XFA_Attribute::LetterSpacing);
89 CXFA_Measurement ms(wsValue.AsStringView());
90 if (ms.GetUnit() == XFA_Unit::Em)
91 return ms.GetValue() * GetFontSize();
92 return ms.ToUnit(XFA_Unit::Pt);
93 }
94
GetLineThrough()95 int32_t CXFA_Font::GetLineThrough() {
96 return JSObject()->GetInteger(XFA_Attribute::LineThrough);
97 }
98
GetUnderline()99 int32_t CXFA_Font::GetUnderline() {
100 return JSObject()->GetInteger(XFA_Attribute::Underline);
101 }
102
GetUnderlinePeriod()103 XFA_AttributeEnum CXFA_Font::GetUnderlinePeriod() {
104 return JSObject()
105 ->TryEnum(XFA_Attribute::UnderlinePeriod, true)
106 .value_or(XFA_AttributeEnum::All);
107 }
108
GetFontSize() const109 float CXFA_Font::GetFontSize() const {
110 return JSObject()->GetMeasure(XFA_Attribute::Size).ToUnit(XFA_Unit::Pt);
111 }
112
GetTypeface()113 WideString CXFA_Font::GetTypeface() {
114 return JSObject()->GetCData(XFA_Attribute::Typeface);
115 }
116
IsBold()117 bool CXFA_Font::IsBold() {
118 return JSObject()->GetEnum(XFA_Attribute::Weight) == XFA_AttributeEnum::Bold;
119 }
120
IsItalic()121 bool CXFA_Font::IsItalic() {
122 return JSObject()->GetEnum(XFA_Attribute::Posture) ==
123 XFA_AttributeEnum::Italic;
124 }
125
SetColor(FX_ARGB color)126 void CXFA_Font::SetColor(FX_ARGB color) {
127 CXFA_Fill* node =
128 JSObject()->GetOrCreateProperty<CXFA_Fill>(0, XFA_Element::Fill);
129 if (!node)
130 return;
131
132 node->SetColor(color);
133 }
134
GetColor()135 FX_ARGB CXFA_Font::GetColor() {
136 CXFA_Fill* fill = GetChild<CXFA_Fill>(0, XFA_Element::Fill, false);
137 return fill ? fill->GetColor(true) : 0xFF000000;
138 }
139