xfa_ffbarcode.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
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 "xfa/fxfa/app/xfa_ffbarcode.h"
8
9#include "core/fxcrt/fx_ext.h"
10#include "xfa/fwl/cfwl_app.h"
11#include "xfa/fwl/cfwl_barcode.h"
12#include "xfa/fwl/cfwl_notedriver.h"
13#include "xfa/fxfa/app/xfa_fffield.h"
14#include "xfa/fxfa/app/xfa_fftextedit.h"
15#include "xfa/fxfa/app/xfa_fwladapter.h"
16#include "xfa/fxfa/xfa_ffpageview.h"
17#include "xfa/fxfa/xfa_ffwidget.h"
18
19namespace {
20
21const XFA_BARCODETYPEENUMINFO g_XFABarCodeTypeEnumData[] = {
22    {0x7fb4a18, L"ean13", XFA_BARCODETYPE_ean13, BC_EAN13},
23    {0x8d13a3d, L"code11", XFA_BARCODETYPE_code11, BC_UNKNOWN},
24    {0x8d149a8, L"code49", XFA_BARCODETYPE_code49, BC_UNKNOWN},
25    {0x8d16347, L"code93", XFA_BARCODETYPE_code93, BC_UNKNOWN},
26    {0x91a92e2, L"upsMaxicode", XFA_BARCODETYPE_upsMaxicode, BC_UNKNOWN},
27    {0xa7d48dc, L"fim", XFA_BARCODETYPE_fim, BC_UNKNOWN},
28    {0xb359fe9, L"msi", XFA_BARCODETYPE_msi, BC_UNKNOWN},
29    {0x121f738c, L"code2Of5Matrix", XFA_BARCODETYPE_code2Of5Matrix, BC_UNKNOWN},
30    {0x15358616, L"ucc128", XFA_BARCODETYPE_ucc128, BC_UNKNOWN},
31    {0x1f4bfa05, L"rfid", XFA_BARCODETYPE_rfid, BC_UNKNOWN},
32    {0x1fda71bc, L"rss14Stacked", XFA_BARCODETYPE_rss14Stacked, BC_UNKNOWN},
33    {0x22065087, L"ean8add2", XFA_BARCODETYPE_ean8add2, BC_UNKNOWN},
34    {0x2206508a, L"ean8add5", XFA_BARCODETYPE_ean8add5, BC_UNKNOWN},
35    {0x2278366c, L"codabar", XFA_BARCODETYPE_codabar, BC_CODABAR},
36    {0x2a039a8d, L"telepen", XFA_BARCODETYPE_telepen, BC_UNKNOWN},
37    {0x323ed337, L"upcApwcd", XFA_BARCODETYPE_upcApwcd, BC_UNKNOWN},
38    {0x347a1846, L"postUSIMB", XFA_BARCODETYPE_postUSIMB, BC_UNKNOWN},
39    {0x391bb836, L"code128", XFA_BARCODETYPE_code128, BC_CODE128},
40    {0x398eddaf, L"dataMatrix", XFA_BARCODETYPE_dataMatrix, BC_DATAMATRIX},
41    {0x3cff60a8, L"upcEadd2", XFA_BARCODETYPE_upcEadd2, BC_UNKNOWN},
42    {0x3cff60ab, L"upcEadd5", XFA_BARCODETYPE_upcEadd5, BC_UNKNOWN},
43    {0x402cb188, L"code2Of5Standard", XFA_BARCODETYPE_code2Of5Standard,
44     BC_UNKNOWN},
45    {0x411764f7, L"aztec", XFA_BARCODETYPE_aztec, BC_UNKNOWN},
46    {0x44d4e84c, L"ean8", XFA_BARCODETYPE_ean8, BC_EAN8},
47    {0x48468902, L"ucc128sscc", XFA_BARCODETYPE_ucc128sscc, BC_UNKNOWN},
48    {0x4880aea4, L"upcAadd2", XFA_BARCODETYPE_upcAadd2, BC_UNKNOWN},
49    {0x4880aea7, L"upcAadd5", XFA_BARCODETYPE_upcAadd5, BC_UNKNOWN},
50    {0x54f18256, L"code2Of5Industrial", XFA_BARCODETYPE_code2Of5Industrial,
51     BC_UNKNOWN},
52    {0x58e15f25, L"rss14Limited", XFA_BARCODETYPE_rss14Limited, BC_UNKNOWN},
53    {0x5c08d1b9, L"postAUSReplyPaid", XFA_BARCODETYPE_postAUSReplyPaid,
54     BC_UNKNOWN},
55    {0x5fa700bd, L"rss14", XFA_BARCODETYPE_rss14, BC_UNKNOWN},
56    {0x631a7e35, L"logmars", XFA_BARCODETYPE_logmars, BC_UNKNOWN},
57    {0x6a236236, L"pdf417", XFA_BARCODETYPE_pdf417, BC_PDF417},
58    {0x6d098ece, L"upcean2", XFA_BARCODETYPE_upcean2, BC_UNKNOWN},
59    {0x6d098ed1, L"upcean5", XFA_BARCODETYPE_upcean5, BC_UNKNOWN},
60    {0x76b04eed, L"code3Of9extended", XFA_BARCODETYPE_code3Of9extended,
61     BC_UNKNOWN},
62    {0x7c7db84a, L"maxicode", XFA_BARCODETYPE_maxicode, BC_UNKNOWN},
63    {0x8266f7f7, L"ucc128random", XFA_BARCODETYPE_ucc128random, BC_UNKNOWN},
64    {0x83eca147, L"postUSDPBC", XFA_BARCODETYPE_postUSDPBC, BC_UNKNOWN},
65    {0x8dd71de0, L"postAUSStandard", XFA_BARCODETYPE_postAUSStandard,
66     BC_UNKNOWN},
67    {0x98adad85, L"plessey", XFA_BARCODETYPE_plessey, BC_UNKNOWN},
68    {0x9f84cce6, L"ean13pwcd", XFA_BARCODETYPE_ean13pwcd, BC_UNKNOWN},
69    {0xb514fbe9, L"upcA", XFA_BARCODETYPE_upcA, BC_UPCA},
70    {0xb514fbed, L"upcE", XFA_BARCODETYPE_upcE, BC_UNKNOWN},
71    {0xb5c6a853, L"ean13add2", XFA_BARCODETYPE_ean13add2, BC_UNKNOWN},
72    {0xb5c6a856, L"ean13add5", XFA_BARCODETYPE_ean13add5, BC_UNKNOWN},
73    {0xb81fc512, L"postUKRM4SCC", XFA_BARCODETYPE_postUKRM4SCC, BC_UNKNOWN},
74    {0xbad34b22, L"code128SSCC", XFA_BARCODETYPE_code128SSCC, BC_UNKNOWN},
75    {0xbfbe0cf6, L"postUS5Zip", XFA_BARCODETYPE_postUS5Zip, BC_UNKNOWN},
76    {0xc56618e8, L"pdf417macro", XFA_BARCODETYPE_pdf417macro, BC_UNKNOWN},
77    {0xca730f8a, L"code2Of5Interleaved", XFA_BARCODETYPE_code2Of5Interleaved,
78     BC_UNKNOWN},
79    {0xd0097ac6, L"rss14Expanded", XFA_BARCODETYPE_rss14Expanded, BC_UNKNOWN},
80    {0xd25a0240, L"postAUSCust2", XFA_BARCODETYPE_postAUSCust2, BC_UNKNOWN},
81    {0xd25a0241, L"postAUSCust3", XFA_BARCODETYPE_postAUSCust3, BC_UNKNOWN},
82    {0xd53ed3e7, L"rss14Truncated", XFA_BARCODETYPE_rss14Truncated, BC_UNKNOWN},
83    {0xe72bcd57, L"code128A", XFA_BARCODETYPE_code128A, BC_UNKNOWN},
84    {0xe72bcd58, L"code128B", XFA_BARCODETYPE_code128B, BC_CODE128_B},
85    {0xe72bcd59, L"code128C", XFA_BARCODETYPE_code128C, BC_CODE128_C},
86    {0xee83c50f, L"rss14StackedOmni", XFA_BARCODETYPE_rss14StackedOmni,
87     BC_UNKNOWN},
88    {0xf2a18f7e, L"QRCode", XFA_BARCODETYPE_QRCode, BC_QR_CODE},
89    {0xfaeaf37f, L"postUSStandard", XFA_BARCODETYPE_postUSStandard, BC_UNKNOWN},
90    {0xfb48155c, L"code3Of9", XFA_BARCODETYPE_code3Of9, BC_CODE39},
91};
92const int32_t g_iXFABarcodeTypeCount =
93    sizeof(g_XFABarCodeTypeEnumData) / sizeof(XFA_BARCODETYPEENUMINFO);
94
95const XFA_BARCODETYPEENUMINFO* XFA_GetBarcodeTypeByName(
96    const CFX_WideStringC& wsName) {
97  if (wsName.IsEmpty())
98    return nullptr;
99
100  uint32_t uHash = FX_HashCode_GetW(wsName, true);
101  int32_t iStart = 0;
102  int32_t iEnd = g_iXFABarcodeTypeCount - 1;
103  do {
104    int32_t iMid = (iStart + iEnd) / 2;
105    const XFA_BARCODETYPEENUMINFO* pInfo = g_XFABarCodeTypeEnumData + iMid;
106    if (uHash == pInfo->uHash) {
107      return pInfo;
108    } else if (uHash < pInfo->uHash) {
109      iEnd = iMid - 1;
110    } else {
111      iStart = iMid + 1;
112    }
113  } while (iStart <= iEnd);
114  return nullptr;
115}
116
117}  // namespace.
118
119CXFA_FFBarcode::CXFA_FFBarcode(CXFA_FFPageView* pPageView,
120                               CXFA_WidgetAcc* pDataAcc)
121    : CXFA_FFTextEdit(pPageView, pDataAcc) {}
122
123CXFA_FFBarcode::~CXFA_FFBarcode() {}
124
125bool CXFA_FFBarcode::LoadWidget() {
126  CFWL_Barcode* pFWLBarcode = new CFWL_Barcode(GetFWLApp());
127
128  m_pNormalWidget = pFWLBarcode;
129  m_pNormalWidget->SetLayoutItem(this);
130  CFWL_NoteDriver* pNoteDriver =
131      m_pNormalWidget->GetOwnerApp()->GetNoteDriver();
132  pNoteDriver->RegisterEventTarget(m_pNormalWidget, m_pNormalWidget);
133
134  m_pOldDelegate = m_pNormalWidget->GetDelegate();
135  m_pNormalWidget->SetDelegate(this);
136  m_pNormalWidget->LockUpdate();
137
138  CFX_WideString wsText;
139  m_pDataAcc->GetValue(wsText, XFA_VALUEPICTURE_Display);
140  pFWLBarcode->SetText(wsText);
141  UpdateWidgetProperty();
142  m_pNormalWidget->UnlockUpdate();
143  return CXFA_FFField::LoadWidget();
144}
145void CXFA_FFBarcode::RenderWidget(CFX_Graphics* pGS,
146                                  CFX_Matrix* pMatrix,
147                                  uint32_t dwStatus) {
148  if (!IsMatchVisibleStatus(dwStatus)) {
149    return;
150  }
151  CFX_Matrix mtRotate;
152  GetRotateMatrix(mtRotate);
153  if (pMatrix) {
154    mtRotate.Concat(*pMatrix);
155  }
156  CXFA_FFWidget::RenderWidget(pGS, &mtRotate, dwStatus);
157  CXFA_Border borderUI = m_pDataAcc->GetUIBorder();
158  DrawBorder(pGS, borderUI, m_rtUI, &mtRotate);
159  RenderCaption(pGS, &mtRotate);
160  CFX_RectF rtWidget = m_pNormalWidget->GetWidgetRect();
161  CFX_Matrix mt;
162  mt.Set(1, 0, 0, 1, rtWidget.left, rtWidget.top);
163  mt.Concat(mtRotate);
164  m_pNormalWidget->DrawWidget(pGS, &mt);
165}
166
167void CXFA_FFBarcode::UpdateWidgetProperty() {
168  CXFA_FFTextEdit::UpdateWidgetProperty();
169  CFWL_Barcode* pBarCodeWidget = (CFWL_Barcode*)m_pNormalWidget;
170  CFX_WideString wsType = GetDataAcc()->GetBarcodeType();
171  const XFA_BARCODETYPEENUMINFO* pBarcodeTypeInfo =
172      XFA_GetBarcodeTypeByName(wsType.AsStringC());
173  if (!pBarcodeTypeInfo)
174    return;
175
176  pBarCodeWidget->SetType(pBarcodeTypeInfo->eBCType);
177  CXFA_WidgetAcc* pAcc = GetDataAcc();
178  int32_t intVal;
179  FX_CHAR charVal;
180  bool boolVal;
181  FX_FLOAT floatVal;
182  if (pAcc->GetBarcodeAttribute_CharEncoding(intVal)) {
183    pBarCodeWidget->SetCharEncoding((BC_CHAR_ENCODING)intVal);
184  }
185  if (pAcc->GetBarcodeAttribute_Checksum(boolVal)) {
186    pBarCodeWidget->SetCalChecksum(boolVal);
187  }
188  if (pAcc->GetBarcodeAttribute_DataLength(intVal)) {
189    pBarCodeWidget->SetDataLength(intVal);
190  }
191  if (pAcc->GetBarcodeAttribute_StartChar(charVal)) {
192    pBarCodeWidget->SetStartChar(charVal);
193  }
194  if (pAcc->GetBarcodeAttribute_EndChar(charVal)) {
195    pBarCodeWidget->SetEndChar(charVal);
196  }
197  if (pAcc->GetBarcodeAttribute_ECLevel(intVal)) {
198    pBarCodeWidget->SetErrorCorrectionLevel(intVal);
199  }
200  if (pAcc->GetBarcodeAttribute_ModuleWidth(intVal)) {
201    pBarCodeWidget->SetModuleWidth(intVal);
202  }
203  if (pAcc->GetBarcodeAttribute_ModuleHeight(intVal)) {
204    pBarCodeWidget->SetModuleHeight(intVal);
205  }
206  if (pAcc->GetBarcodeAttribute_PrintChecksum(boolVal)) {
207    pBarCodeWidget->SetPrintChecksum(boolVal);
208  }
209  if (pAcc->GetBarcodeAttribute_TextLocation(intVal)) {
210    pBarCodeWidget->SetTextLocation((BC_TEXT_LOC)intVal);
211  }
212  if (pAcc->GetBarcodeAttribute_Truncate(boolVal)) {
213    pBarCodeWidget->SetTruncated(boolVal);
214  }
215  if (pAcc->GetBarcodeAttribute_WideNarrowRatio(floatVal)) {
216    pBarCodeWidget->SetWideNarrowRatio((int32_t)floatVal);
217  }
218  if (pBarcodeTypeInfo->eName == XFA_BARCODETYPE_code3Of9 ||
219      pBarcodeTypeInfo->eName == XFA_BARCODETYPE_ean8 ||
220      pBarcodeTypeInfo->eName == XFA_BARCODETYPE_ean13 ||
221      pBarcodeTypeInfo->eName == XFA_BARCODETYPE_upcA) {
222    pBarCodeWidget->SetPrintChecksum(true);
223  }
224}
225
226bool CXFA_FFBarcode::OnLButtonDown(uint32_t dwFlags, FX_FLOAT fx, FX_FLOAT fy) {
227  CFWL_Barcode* pBarCodeWidget = (CFWL_Barcode*)m_pNormalWidget;
228  if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType()) {
229    return false;
230  }
231  if (m_pDataAcc->GetAccess() != XFA_ATTRIBUTEENUM_Open) {
232    return false;
233  }
234  return CXFA_FFTextEdit::OnLButtonDown(dwFlags, fx, fy);
235}
236bool CXFA_FFBarcode::OnRButtonDown(uint32_t dwFlags, FX_FLOAT fx, FX_FLOAT fy) {
237  CFWL_Barcode* pBarCodeWidget = (CFWL_Barcode*)m_pNormalWidget;
238  if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType()) {
239    return false;
240  }
241  return CXFA_FFTextEdit::OnRButtonDown(dwFlags, fx, fy);
242}
243