1 // Copyright 2017 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 "xfa/fxfa/parser/cxfa_font.h"
8
9 #include "fxjs/xfa/cjx_node.h"
10 #include "xfa/fxfa/parser/cxfa_document.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 kFontPropertyData[] = {
17 {XFA_Element::Fill, 1, {}},
18 {XFA_Element::Extras, 1, {}},
19 };
20
21 const CXFA_Node::AttributeData kFontAttributeData[] = {
22 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
23 {XFA_Attribute::LineThrough, XFA_AttributeType::Integer, (void*)0},
24 {XFA_Attribute::Typeface, XFA_AttributeType::CData, (void*)L"Courier"},
25 {XFA_Attribute::FontHorizontalScale, XFA_AttributeType::CData,
26 (void*)L"100%"},
27 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
28 {XFA_Attribute::KerningMode, XFA_AttributeType::Enum,
29 (void*)XFA_AttributeValue::None},
30 {XFA_Attribute::Underline, XFA_AttributeType::Integer, (void*)0},
31 {XFA_Attribute::BaselineShift, XFA_AttributeType::Measure, (void*)L"0in"},
32 {XFA_Attribute::OverlinePeriod, XFA_AttributeType::Enum,
33 (void*)XFA_AttributeValue::All},
34 {XFA_Attribute::LetterSpacing, XFA_AttributeType::CData, nullptr},
35 {XFA_Attribute::LineThroughPeriod, XFA_AttributeType::Enum,
36 (void*)XFA_AttributeValue::All},
37 {XFA_Attribute::FontVerticalScale, XFA_AttributeType::CData,
38 (void*)L"100%"},
39 {XFA_Attribute::PsName, XFA_AttributeType::CData, nullptr},
40 {XFA_Attribute::Size, XFA_AttributeType::Measure, (void*)L"10pt"},
41 {XFA_Attribute::Posture, XFA_AttributeType::Enum,
42 (void*)XFA_AttributeValue::Normal},
43 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
44 {XFA_Attribute::Weight, XFA_AttributeType::Enum,
45 (void*)XFA_AttributeValue::Normal},
46 {XFA_Attribute::UnderlinePeriod, XFA_AttributeType::Enum,
47 (void*)XFA_AttributeValue::All},
48 {XFA_Attribute::Overline, XFA_AttributeType::Integer, (void*)0},
49 {XFA_Attribute::GenericFamily, XFA_AttributeType::Enum,
50 (void*)XFA_AttributeValue::Serif},
51 };
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(doc,
57 packet,
58 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kConfig,
59 XFA_XDPPACKET::kForm},
60 XFA_ObjectType::Node,
61 XFA_Element::Font,
62 kFontPropertyData,
63 kFontAttributeData,
64 cppgc::MakeGarbageCollected<CJX_Node>(
65 doc->GetHeap()->GetAllocationHandle(),
66 this)) {}
67
68 CXFA_Font::~CXFA_Font() = default;
69
GetBaselineShift() const70 float CXFA_Font::GetBaselineShift() const {
71 return JSObject()->GetMeasureInUnit(XFA_Attribute::BaselineShift,
72 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_AttributeValue CXFA_Font::GetUnderlinePeriod() {
104 return JSObject()
105 ->TryEnum(XFA_Attribute::UnderlinePeriod, true)
106 .value_or(XFA_AttributeValue::All);
107 }
108
GetFontSize() const109 float CXFA_Font::GetFontSize() const {
110 return JSObject()->GetMeasureInUnit(XFA_Attribute::Size, 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_AttributeValue::Bold;
119 }
120
IsItalic()121 bool CXFA_Font::IsItalic() {
122 return JSObject()->GetEnum(XFA_Attribute::Posture) ==
123 XFA_AttributeValue::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() const135 FX_ARGB CXFA_Font::GetColor() const {
136 const auto* fill = GetChild<CXFA_Fill>(0, XFA_Element::Fill, false);
137 return fill ? fill->GetTextColor() : 0xFF000000;
138 }
139