K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
Lei Zhang | 9091dba | 2019-06-13 18:44:08 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 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 Zhang | 6714ff8 | 2017-04-27 13:21:00 -0700 | [diff] [blame] | 23 | #include "fxbarcode/datamatrix/BC_TextEncoder.h" |
| 24 | |
Lei Zhang | f263530 | 2018-11-16 23:47:51 +0000 | [diff] [blame] | 25 | #include "core/fxcrt/fx_extension.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 26 | #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 Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 31 | |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 32 | CBC_TextEncoder::CBC_TextEncoder() = default; |
| 33 | |
| 34 | CBC_TextEncoder::~CBC_TextEncoder() = default; |
| 35 | |
Lei Zhang | 2d1531a | 2018-12-07 20:10:59 +0000 | [diff] [blame] | 36 | CBC_HighLevelEncoder::Encoding CBC_TextEncoder::GetEncodingMode() { |
| 37 | return CBC_HighLevelEncoder::Encoding::TEXT; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | } |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 39 | |
| 40 | int32_t CBC_TextEncoder::EncodeChar(wchar_t c, WideString* sb) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 41 | if (c == ' ') { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 42 | *sb += (wchar_t)'\3'; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | return 1; |
| 44 | } |
Lei Zhang | f263530 | 2018-11-16 23:47:51 +0000 | [diff] [blame] | 45 | if (FXSYS_IsDecimalDigit(c)) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 46 | *sb += (wchar_t)(c - 48 + 4); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | return 1; |
| 48 | } |
Hui Yingst | bfc82af | 2020-03-05 19:11:06 +0000 | [diff] [blame] | 49 | if (FXSYS_IsLowerASCII(c)) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 50 | *sb += (wchar_t)(c - 97 + 14); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | if (c <= 0x1f) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 54 | *sb += (wchar_t)'\0'; |
| 55 | *sb += c; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 56 | return 2; |
| 57 | } |
| 58 | if (c >= '!' && c <= '/') { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 59 | *sb += (wchar_t)'\1'; |
| 60 | *sb += (wchar_t)(c - 33); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | return 2; |
| 62 | } |
| 63 | if (c >= ':' && c <= '@') { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 64 | *sb += (wchar_t)'\1'; |
| 65 | *sb += (wchar_t)(c - 58 + 15); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 66 | return 2; |
| 67 | } |
| 68 | if (c >= '[' && c <= '_') { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 69 | *sb += (wchar_t)'\1'; |
| 70 | *sb += (wchar_t)(c - 91 + 22); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 71 | return 2; |
| 72 | } |
| 73 | if (c == 0x0060) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 74 | *sb += (wchar_t)'\2'; |
| 75 | *sb += (wchar_t)(c - 96); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 76 | return 2; |
| 77 | } |
Hui Yingst | 946be34 | 2020-03-04 14:47:35 +0000 | [diff] [blame] | 78 | if (FXSYS_IsUpperASCII(c)) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 79 | *sb += (wchar_t)'\2'; |
| 80 | *sb += (wchar_t)(c - 65 + 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 81 | return 2; |
| 82 | } |
| 83 | if (c >= '{' && c <= 0x007f) { |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 84 | *sb += (wchar_t)'\2'; |
| 85 | *sb += (wchar_t)(c - 123 + 27); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | return 2; |
| 87 | } |
Lei Zhang | d429f54 | 2018-11-27 20:02:04 +0000 | [diff] [blame] | 88 | 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 Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 95 | } |