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
BC_OnedEAN8Writer.cpp
Go to the documentation of this file.
1// Copyright 2014 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// Original code is licensed as follows:
7/*
8 * Copyright 2009 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "fxbarcode/oned/BC_OnedEAN8Writer.h"
24
25#include <math.h>
26
27#include <algorithm>
28#include <memory>
29#include <vector>
30
31#include "core/fxcrt/fx_extension.h"
32#include "core/fxcrt/fx_memory_wrappers.h"
33#include "core/fxge/cfx_defaultrenderdevice.h"
34#include "core/fxge/text_char_pos.h"
35#include "fxbarcode/BC_Writer.h"
36#include "fxbarcode/common/BC_CommonBitMatrix.h"
37#include "fxbarcode/oned/BC_OneDimWriter.h"
38#include "fxbarcode/oned/BC_OnedEANChecksum.h"
39
40namespace {
41
42const uint8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
43const uint8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
44const uint8_t kOnedEAN8LPattern[10][4] = {
45 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
46 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
47
48} // namespace
49
50CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() {
51 m_iDataLenth = 8;
52}
53
54CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() = default;
55
56void CBC_OnedEAN8Writer::SetDataLength(int32_t length) {
57 m_iDataLenth = 8;
58}
59
60void CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
61 if (location == BC_TEXT_LOC::kBelowEmbed)
62 m_locTextLoc = location;
63}
64
65bool CBC_OnedEAN8Writer::CheckContentValidity(WideStringView contents) {
66 return HasValidContentSize(contents) &&
67 std::all_of(contents.begin(), contents.end(),
68 [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
69}
70
71WideString CBC_OnedEAN8Writer::FilterContents(WideStringView contents) {
72 WideString filtercontents;
73 filtercontents.Reserve(contents.GetLength());
74 wchar_t ch;
75 for (size_t i = 0; i < contents.GetLength(); i++) {
76 ch = contents[i];
77 if (ch > 175) {
78 i++;
79 continue;
80 }
82 filtercontents += ch;
83 }
84 return filtercontents;
85}
86
87int32_t CBC_OnedEAN8Writer::CalcChecksum(const ByteString& contents) {
88 return EANCalcChecksum(contents);
89}
90
91DataVector<uint8_t> CBC_OnedEAN8Writer::Encode(const ByteString& contents) {
92 if (contents.GetLength() != 8)
93 return {};
94
95 DataVector<uint8_t> result(m_codeWidth);
96 auto result_span = pdfium::make_span(result);
97 result_span = AppendPattern(result_span, kOnedEAN8StartPattern, true);
98
99 for (int i = 0; i <= 3; i++) {
100 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
101 result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], false);
102 }
103 result_span = AppendPattern(result_span, kOnedEAN8MiddlePattern, false);
104
105 for (int i = 4; i <= 7; i++) {
106 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
107 result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], true);
108 }
109 AppendPattern(result_span, kOnedEAN8StartPattern, true);
110 return result;
111}
112
113bool CBC_OnedEAN8Writer::ShowChars(WideStringView contents,
114 CFX_RenderDevice* device,
115 const CFX_Matrix& matrix,
116 int32_t barWidth) {
117 if (!device)
118 return false;
119
120 constexpr float kLeftPosition = 3.0f;
121 ByteString str = FX_UTF8Encode(contents);
122 size_t iLength = str.GetLength();
123 std::vector<TextCharPos> charpos(iLength);
124 ByteString tempStr = str.First(4);
125 size_t iLen = tempStr.GetLength();
126 constexpr int32_t kWidth = 28;
127 float blank = 0.0f;
128
129 int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
130 int32_t iTextHeight = iFontSize + 1;
131
132 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
133 CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
134 kLeftPosition + kWidth - 0.5, (float)m_Height);
135 matr.Concat(matrix);
138 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
139 CFX_FloatRect rect1(kLeftPosition + 33, (float)(m_Height - iTextHeight),
140 kLeftPosition + 33 + kWidth - 0.5, (float)m_Height);
141 matr1.Concat(matrix);
144 int32_t strWidth = static_cast<int32_t>(kWidth * m_outputHScale);
145
146 CalcTextInfo(tempStr, charpos.data(), m_pFont, (float)strWidth, iFontSize,
147 blank);
148 {
149 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
150 kLeftPosition * m_outputHScale,
151 (float)(m_Height - iTextHeight + iFontSize));
152 affine_matrix1.Concat(matrix);
153 device->DrawNormalText(pdfium::make_span(charpos).first(iLen), m_pFont,
154 static_cast<float>(iFontSize), affine_matrix1,
155 m_fontColor, GetTextRenderOptions());
156 }
157 tempStr = str.Substr(4, 4);
158 iLen = tempStr.GetLength();
159 CalcTextInfo(tempStr, &charpos[4], m_pFont, (float)strWidth, iFontSize,
160 blank);
161 {
162 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
163 (kLeftPosition + 33) * m_outputHScale,
164 (float)(m_Height - iTextHeight + iFontSize));
165 affine_matrix1.Concat(matrix);
166 device->DrawNormalText(pdfium::make_span(charpos).subspan(4, iLen), m_pFont,
167 static_cast<float>(iFontSize), affine_matrix1,
168 m_fontColor, GetTextRenderOptions());
169 }
170 return true;
171}
BC_TEXT_LOC
Definition BC_Library.h:12
int32_t EANCalcChecksum(const ByteString &contents)
BC_TEXT_LOC m_locTextLoc
static bool HasValidContentSize(WideStringView contents)
pdfium::span< uint8_t > AppendPattern(pdfium::span< uint8_t > target, pdfium::span< const uint8_t > pattern, bool startColor)
int32_t CalcChecksum(const ByteString &contents) override
bool ShowChars(WideStringView contents, CFX_RenderDevice *device, const CFX_Matrix &matrix, int32_t barWidth) override
~CBC_OnedEAN8Writer() override
bool CheckContentValidity(WideStringView contents) override
void SetTextLocation(BC_TEXT_LOC location) override
void SetDataLength(int32_t length) override
WideString FilterContents(WideStringView contents) override
DataVector< uint8_t > Encode(const ByteString &contents) override
int32_t m_Height
Definition BC_Writer.h:36
static const FX_ARGB kBackgroundColor
Definition BC_Writer.h:32
constexpr CFX_FloatRect(float l, float b, float r, float t)
FX_RECT GetOuterRect() const
CFX_FloatRect TransformRect(const CFX_FloatRect &rect) const
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Concat(const CFX_Matrix &right)
bool FillRect(const FX_RECT &rect, uint32_t color)
CharType operator[](const size_t index) const
Definition bytestring.h:150
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
int FXSYS_DecimalCharToInt(char c)
FX_RECT & operator=(const FX_RECT &that)=default