Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cxfa_font.cpp
Go to the documentation of this file.
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
14namespace {
15
16const CXFA_Node::PropertyData kFontPropertyData[] = {
17 {XFA_Element::Fill, 1, {}},
18 {XFA_Element::Extras, 1, {}},
19};
20
21const CXFA_Node::AttributeData kFontAttributeData[] = {
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%"},
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"},
42 (void*)XFA_AttributeValue::Normal},
43 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
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},
50 (void*)XFA_AttributeValue::Serif},
51};
52
53} // namespace
54
55CXFA_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
68CXFA_Font::~CXFA_Font() = default;
69
70float CXFA_Font::GetBaselineShift() const {
71 return JSObject()->GetMeasureInUnit(XFA_Attribute::BaselineShift,
73}
74
75float 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
81float 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
87float 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
95int32_t CXFA_Font::GetLineThrough() {
96 return JSObject()->GetInteger(XFA_Attribute::LineThrough);
97}
98
99int32_t CXFA_Font::GetUnderline() {
100 return JSObject()->GetInteger(XFA_Attribute::Underline);
101}
102
104 return JSObject()
105 ->TryEnum(XFA_Attribute::UnderlinePeriod, true)
106 .value_or(XFA_AttributeValue::All);
107}
108
109float CXFA_Font::GetFontSize() const {
110 return JSObject()->GetMeasureInUnit(XFA_Attribute::Size, XFA_Unit::Pt);
111}
112
113WideString CXFA_Font::GetTypeface() {
114 return JSObject()->GetCData(XFA_Attribute::Typeface);
115}
116
117bool CXFA_Font::IsBold() {
118 return JSObject()->GetEnum(XFA_Attribute::Weight) == XFA_AttributeValue::Bold;
119}
120
121bool CXFA_Font::IsItalic() {
122 return JSObject()->GetEnum(XFA_Attribute::Posture) ==
123 XFA_AttributeValue::Italic;
124}
125
126void 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
135FX_ARGB CXFA_Font::GetColor() const {
136 const auto* fill = GetChild<CXFA_Fill>(0, XFA_Element::Fill, false);
137 return fill ? fill->GetTextColor() : 0xFF000000;
138}
void SetColor(FX_ARGB color)
Definition cxfa_fill.cpp:64
float GetVerticalScale()
Definition cxfa_font.cpp:81
float GetBaselineShift() const
Definition cxfa_font.cpp:70
WideString GetTypeface()
bool IsItalic()
FX_ARGB GetColor() const
bool IsBold()
int32_t GetUnderline()
Definition cxfa_font.cpp:99
int32_t GetLineThrough()
Definition cxfa_font.cpp:95
~CXFA_Font() override
float GetLetterSpacing()
Definition cxfa_font.cpp:87
float GetHorizontalScale()
Definition cxfa_font.cpp:75
float GetFontSize() const
void SetColor(FX_ARGB color)
XFA_AttributeValue GetUnderlinePeriod()
float GetValue() const
float ToUnit(XFA_Unit eUnit) const
XFA_Unit GetUnit() const
const wchar_t * c_str() const
Definition widestring.h:81
int32_t FXSYS_wtoi(const wchar_t *str)
XFA_Unit
Definition fxfa_basic.h:91
XFA_AttributeType
Definition fxfa_basic.h:83
XFA_Attribute
Definition fxfa_basic.h:67
XFA_Element
Definition fxfa_basic.h:75
XFA_AttributeValue
Definition fxfa_basic.h:60
XFA_PacketType
Definition fxfa_basic.h:44