blob: 80672cfb3eaa31ed830d8bcc6b9c56cf973c0749 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
Lei Zhang9091dba2019-06-13 18:44:08 +00002// Use of this source code is governed by a BSD-style license that can be
Dan Sinclair1770c022016-03-14 14:14:16 -04003// 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 2006-2007 Jeremias Maerki.
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
Lei Zhang6714ff82017-04-27 13:21:00 -070023#include "fxbarcode/datamatrix/BC_TextEncoder.h"
24
Lei Zhangf2635302018-11-16 23:47:51 +000025#include "core/fxcrt/fx_extension.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040026#include "fxbarcode/datamatrix/BC_C40Encoder.h"
27#include "fxbarcode/datamatrix/BC_Encoder.h"
28#include "fxbarcode/datamatrix/BC_EncoderContext.h"
29#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
30#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040031
Lei Zhangd429f542018-11-27 20:02:04 +000032CBC_TextEncoder::CBC_TextEncoder() = default;
33
34CBC_TextEncoder::~CBC_TextEncoder() = default;
35
Lei Zhang2d1531a2018-12-07 20:10:59 +000036CBC_HighLevelEncoder::Encoding CBC_TextEncoder::GetEncodingMode() {
37 return CBC_HighLevelEncoder::Encoding::TEXT;
Dan Sinclair1770c022016-03-14 14:14:16 -040038}
Lei Zhangd429f542018-11-27 20:02:04 +000039
40int32_t CBC_TextEncoder::EncodeChar(wchar_t c, WideString* sb) {
Dan Sinclair1770c022016-03-14 14:14:16 -040041 if (c == ' ') {
Lei Zhangd429f542018-11-27 20:02:04 +000042 *sb += (wchar_t)'\3';
Dan Sinclair1770c022016-03-14 14:14:16 -040043 return 1;
44 }
Lei Zhangf2635302018-11-16 23:47:51 +000045 if (FXSYS_IsDecimalDigit(c)) {
Lei Zhangd429f542018-11-27 20:02:04 +000046 *sb += (wchar_t)(c - 48 + 4);
Dan Sinclair1770c022016-03-14 14:14:16 -040047 return 1;
48 }
Hui Yingstbfc82af2020-03-05 19:11:06 +000049 if (FXSYS_IsLowerASCII(c)) {
Lei Zhangd429f542018-11-27 20:02:04 +000050 *sb += (wchar_t)(c - 97 + 14);
Dan Sinclair1770c022016-03-14 14:14:16 -040051 return 1;
52 }
53 if (c <= 0x1f) {
Lei Zhangd429f542018-11-27 20:02:04 +000054 *sb += (wchar_t)'\0';
55 *sb += c;
Dan Sinclair1770c022016-03-14 14:14:16 -040056 return 2;
57 }
58 if (c >= '!' && c <= '/') {
Lei Zhangd429f542018-11-27 20:02:04 +000059 *sb += (wchar_t)'\1';
60 *sb += (wchar_t)(c - 33);
Dan Sinclair1770c022016-03-14 14:14:16 -040061 return 2;
62 }
63 if (c >= ':' && c <= '@') {
Lei Zhangd429f542018-11-27 20:02:04 +000064 *sb += (wchar_t)'\1';
65 *sb += (wchar_t)(c - 58 + 15);
Dan Sinclair1770c022016-03-14 14:14:16 -040066 return 2;
67 }
68 if (c >= '[' && c <= '_') {
Lei Zhangd429f542018-11-27 20:02:04 +000069 *sb += (wchar_t)'\1';
70 *sb += (wchar_t)(c - 91 + 22);
Dan Sinclair1770c022016-03-14 14:14:16 -040071 return 2;
72 }
73 if (c == 0x0060) {
Lei Zhangd429f542018-11-27 20:02:04 +000074 *sb += (wchar_t)'\2';
75 *sb += (wchar_t)(c - 96);
Dan Sinclair1770c022016-03-14 14:14:16 -040076 return 2;
77 }
Hui Yingst946be342020-03-04 14:47:35 +000078 if (FXSYS_IsUpperASCII(c)) {
Lei Zhangd429f542018-11-27 20:02:04 +000079 *sb += (wchar_t)'\2';
80 *sb += (wchar_t)(c - 65 + 1);
Dan Sinclair1770c022016-03-14 14:14:16 -040081 return 2;
82 }
83 if (c >= '{' && c <= 0x007f) {
Lei Zhangd429f542018-11-27 20:02:04 +000084 *sb += (wchar_t)'\2';
85 *sb += (wchar_t)(c - 123 + 27);
Dan Sinclair1770c022016-03-14 14:14:16 -040086 return 2;
87 }
Lei Zhangd429f542018-11-27 20:02:04 +000088 if (c < 0x0080)
89 return 0;
90
91 *sb += (wchar_t)'\1';
92 *sb += (wchar_t)0x001e;
93 int32_t encode_result = EncodeChar(c - 128, sb);
94 return encode_result > 0 ? encode_result + 2 : 0;
Dan Sinclair1770c022016-03-14 14:14:16 -040095}