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 "fpdfsdk/include/pdfwindow/PWL_Label.h"
8 #include "fpdfsdk/include/pdfwindow/PWL_Utils.h"
9 #include "fpdfsdk/include/pdfwindow/PWL_Wnd.h"
10
CPWL_Label()11 CPWL_Label::CPWL_Label() : m_pEdit(NULL) {
12 m_pEdit = IFX_Edit::NewEdit();
13 ASSERT(m_pEdit);
14 }
15
~CPWL_Label()16 CPWL_Label::~CPWL_Label() {
17 IFX_Edit::DelEdit(m_pEdit);
18 }
19
GetClassName() const20 CFX_ByteString CPWL_Label::GetClassName() const {
21 return "CPWL_Label";
22 }
23
OnCreated()24 void CPWL_Label::OnCreated() {
25 SetParamByFlag();
26 SetFontSize(GetCreationParam().fFontSize);
27
28 m_pEdit->SetFontMap(GetFontMap());
29 m_pEdit->Initialize();
30
31 if (HasFlag(PES_TEXTOVERFLOW)) {
32 SetClipRect(CPDF_Rect(0.0f, 0.0f, 0.0f, 0.0f));
33 m_pEdit->SetTextOverflow(TRUE);
34 }
35 }
36
SetText(const FX_WCHAR * csText)37 void CPWL_Label::SetText(const FX_WCHAR* csText) {
38 m_pEdit->SetText(csText);
39 }
40
RePosChildWnd()41 void CPWL_Label::RePosChildWnd() {
42 m_pEdit->SetPlateRect(GetClientRect());
43 }
44
SetFontSize(FX_FLOAT fFontSize)45 void CPWL_Label::SetFontSize(FX_FLOAT fFontSize) {
46 m_pEdit->SetFontSize(fFontSize);
47 }
48
GetFontSize() const49 FX_FLOAT CPWL_Label::GetFontSize() const {
50 return m_pEdit->GetFontSize();
51 }
52
SetParamByFlag()53 void CPWL_Label::SetParamByFlag() {
54 if (HasFlag(PES_LEFT)) {
55 m_pEdit->SetAlignmentH(0);
56 } else if (HasFlag(PES_MIDDLE)) {
57 m_pEdit->SetAlignmentH(1);
58 } else if (HasFlag(PES_RIGHT)) {
59 m_pEdit->SetAlignmentH(2);
60 } else {
61 m_pEdit->SetAlignmentH(0);
62 }
63
64 if (HasFlag(PES_TOP)) {
65 m_pEdit->SetAlignmentV(0);
66 } else if (HasFlag(PES_CENTER)) {
67 m_pEdit->SetAlignmentV(1);
68 } else if (HasFlag(PES_BOTTOM)) {
69 m_pEdit->SetAlignmentV(2);
70 } else {
71 m_pEdit->SetAlignmentV(0);
72 }
73
74 if (HasFlag(PES_PASSWORD)) {
75 m_pEdit->SetPasswordChar('*');
76 }
77
78 m_pEdit->SetMultiLine(HasFlag(PES_MULTILINE));
79 m_pEdit->SetAutoReturn(HasFlag(PES_AUTORETURN));
80 m_pEdit->SetAutoFontSize(HasFlag(PWS_AUTOFONTSIZE));
81 m_pEdit->SetAutoScroll(HasFlag(PES_AUTOSCROLL));
82 }
83
DrawThisAppearance(CFX_RenderDevice * pDevice,CFX_Matrix * pUser2Device)84 void CPWL_Label::DrawThisAppearance(CFX_RenderDevice* pDevice,
85 CFX_Matrix* pUser2Device) {
86 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
87
88 GetClientRect();
89
90 CPDF_Rect rcClip;
91 CPVT_WordRange wrRange = m_pEdit->GetVisibleWordRange();
92 CPVT_WordRange* pRange = NULL;
93
94 if (!HasFlag(PES_TEXTOVERFLOW)) {
95 rcClip = GetClientRect();
96 pRange = &wrRange;
97 }
98 IFX_SystemHandler* pSysHandler = GetSystemHandler();
99 IFX_Edit::DrawEdit(
100 pDevice, pUser2Device, m_pEdit,
101 CPWL_Utils::PWLColorToFXColor(GetTextColor(), GetTransparency()),
102 CPWL_Utils::PWLColorToFXColor(GetTextStrokeColor(), GetTransparency()),
103 rcClip, CPDF_Point(0.0f, 0.0f), pRange, pSysHandler, NULL);
104 }
105
SetHorzScale(int32_t nHorzScale)106 void CPWL_Label::SetHorzScale(int32_t nHorzScale) {
107 m_pEdit->SetHorzScale(nHorzScale);
108 }
109
SetCharSpace(FX_FLOAT fCharSpace)110 void CPWL_Label::SetCharSpace(FX_FLOAT fCharSpace) {
111 m_pEdit->SetCharSpace(fCharSpace);
112 }
113
GetContentRect() const114 CPDF_Rect CPWL_Label::GetContentRect() const {
115 return m_pEdit->GetContentRect();
116 }
117
GetThisAppearanceStream(CFX_ByteTextBuf & sAppStream)118 void CPWL_Label::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
119 CPWL_Wnd::GetThisAppearanceStream(sAppStream);
120
121 sAppStream << GetTextAppearanceStream(CPDF_Point(0.0f, 0.0f));
122 }
123
GetTextAppearanceStream(const CPDF_Point & ptOffset) const124 CFX_ByteString CPWL_Label::GetTextAppearanceStream(
125 const CPDF_Point& ptOffset) const {
126 CFX_ByteTextBuf sRet;
127 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(m_pEdit, ptOffset);
128
129 if (sEdit.GetLength() > 0) {
130 sRet << "BT\n" << CPWL_Utils::GetColorAppStream(GetTextColor()) << sEdit
131 << "ET\n";
132 }
133
134 return sRet.GetByteString();
135 }
136
GetText() const137 CFX_WideString CPWL_Label::GetText() const {
138 return m_pEdit->GetText();
139 }
140
SetLimitChar(int32_t nLimitChar)141 void CPWL_Label::SetLimitChar(int32_t nLimitChar) {
142 m_pEdit->SetLimitChar(nLimitChar);
143 }
144
GetTotalWords()145 int32_t CPWL_Label::GetTotalWords() {
146 if (m_pEdit)
147 return m_pEdit->GetTotalWords();
148
149 return 0;
150 }
151