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_node.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 kFontPropertyData[] = {
17 {XFA_Element::Fill, 1, 0},
18 {XFA_Element::Extras, 1, 0},
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(
57 doc,
58 packet,
59 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Config | XFA_XDPPACKET_Form),
60 XFA_ObjectType::Node,
61 XFA_Element::Font,
62 kFontPropertyData,
63 kFontAttributeData,
64 pdfium::MakeUnique<CJX_Node>(this)) {}
65
66 CXFA_Font::~CXFA_Font() = default;
67
GetBaselineShift() const68 float CXFA_Font::GetBaselineShift() const {
69 return JSObject()->GetMeasureInUnit(XFA_Attribute::BaselineShift,
70 XFA_Unit::Pt);
71 }
72
GetHorizontalScale()73 float CXFA_Font::GetHorizontalScale() {
74 WideString wsValue = JSObject()->GetCData(XFA_Attribute::FontHorizontalScale);
75 int32_t iScale = FXSYS_wtoi(wsValue.c_str());
76 return iScale > 0 ? (float)iScale : 100.0f;
77 }
78
GetVerticalScale()79 float CXFA_Font::GetVerticalScale() {
80 WideString wsValue = JSObject()->GetCData(XFA_Attribute::FontVerticalScale);
81 int32_t iScale = FXSYS_wtoi(wsValue.c_str());
82 return iScale > 0 ? (float)iScale : 100.0f;
83 }
84
GetLetterSpacing()85 float CXFA_Font::GetLetterSpacing() {
86 WideString wsValue = JSObject()->GetCData(XFA_Attribute::LetterSpacing);
87 CXFA_Measurement ms(wsValue.AsStringView());
88 if (ms.GetUnit() == XFA_Unit::Em)
89 return ms.GetValue() * GetFontSize();
90 return ms.ToUnit(XFA_Unit::Pt);
91 }
92
GetLineThrough()93 int32_t CXFA_Font::GetLineThrough() {
94 return JSObject()->GetInteger(XFA_Attribute::LineThrough);
95 }
96
GetUnderline()97 int32_t CXFA_Font::GetUnderline() {
98 return JSObject()->GetInteger(XFA_Attribute::Underline);
99 }
100
GetUnderlinePeriod()101 XFA_AttributeValue CXFA_Font::GetUnderlinePeriod() {
102 return JSObject()
103 ->TryEnum(XFA_Attribute::UnderlinePeriod, true)
104 .value_or(XFA_AttributeValue::All);
105 }
106
GetFontSize() const107 float CXFA_Font::GetFontSize() const {
108 return JSObject()->GetMeasureInUnit(XFA_Attribute::Size, XFA_Unit::Pt);
109 }
110
GetTypeface()111 WideString CXFA_Font::GetTypeface() {
112 return JSObject()->GetCData(XFA_Attribute::Typeface);
113 }
114
IsBold()115 bool CXFA_Font::IsBold() {
116 return JSObject()->GetEnum(XFA_Attribute::Weight) == XFA_AttributeValue::Bold;
117 }
118
IsItalic()119 bool CXFA_Font::IsItalic() {
120 return JSObject()->GetEnum(XFA_Attribute::Posture) ==
121 XFA_AttributeValue::Italic;
122 }
123
SetColor(FX_ARGB color)124 void CXFA_Font::SetColor(FX_ARGB color) {
125 CXFA_Fill* node =
126 JSObject()->GetOrCreateProperty<CXFA_Fill>(0, XFA_Element::Fill);
127 if (!node)
128 return;
129
130 node->SetColor(color);
131 }
132
GetColor()133 FX_ARGB CXFA_Font::GetColor() {
134 CXFA_Fill* fill = GetChild<CXFA_Fill>(0, XFA_Element::Fill, false);
135 return fill ? fill->GetColor(true) : 0xFF000000;
136 }
137