Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | // 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 | |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 25 | #include "fxbarcode/BC_Dimension.h" |
| 26 | #include "fxbarcode/common/BC_CommonBitMatrix.h" |
| 27 | #include "fxbarcode/datamatrix/BC_C40Encoder.h" |
| 28 | #include "fxbarcode/datamatrix/BC_Encoder.h" |
| 29 | #include "fxbarcode/datamatrix/BC_EncoderContext.h" |
| 30 | #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" |
| 31 | #include "fxbarcode/datamatrix/BC_SymbolInfo.h" |
| 32 | #include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 33 | |
| 34 | CBC_TextEncoder::CBC_TextEncoder() {} |
| 35 | CBC_TextEncoder::~CBC_TextEncoder() {} |
| 36 | int32_t CBC_TextEncoder::getEncodingMode() { |
| 37 | return TEXT_ENCODATION; |
| 38 | } |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 39 | int32_t CBC_TextEncoder::encodeChar(wchar_t c, CFX_WideString& sb, int32_t& e) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 40 | if (c == ' ') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 41 | sb += (wchar_t)'\3'; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 42 | return 1; |
| 43 | } |
| 44 | if (c >= '0' && c <= '9') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 45 | sb += (wchar_t)(c - 48 + 4); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | if (c >= 'a' && c <= 'z') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 49 | sb += (wchar_t)(c - 97 + 14); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 50 | return 1; |
| 51 | } |
| 52 | if (c <= 0x1f) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 53 | sb += (wchar_t)'\0'; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 54 | sb += c; |
| 55 | return 2; |
| 56 | } |
| 57 | if (c >= '!' && c <= '/') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 58 | sb += (wchar_t)'\1'; |
| 59 | sb += (wchar_t)(c - 33); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 60 | return 2; |
| 61 | } |
| 62 | if (c >= ':' && c <= '@') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 63 | sb += (wchar_t)'\1'; |
| 64 | sb += (wchar_t)(c - 58 + 15); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | return 2; |
| 66 | } |
| 67 | if (c >= '[' && c <= '_') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 68 | sb += (wchar_t)'\1'; |
| 69 | sb += (wchar_t)(c - 91 + 22); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | return 2; |
| 71 | } |
| 72 | if (c == 0x0060) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 73 | sb += (wchar_t)'\2'; |
| 74 | sb += (wchar_t)(c - 96); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 75 | return 2; |
| 76 | } |
| 77 | if (c >= 'A' && c <= 'Z') { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 78 | sb += (wchar_t)'\2'; |
| 79 | sb += (wchar_t)(c - 65 + 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | return 2; |
| 81 | } |
| 82 | if (c >= '{' && c <= 0x007f) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 83 | sb += (wchar_t)'\2'; |
| 84 | sb += (wchar_t)(c - 123 + 27); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 85 | return 2; |
| 86 | } |
| 87 | if (c >= 0x0080) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 88 | sb += (wchar_t)'\1'; |
| 89 | sb += (wchar_t)0x001e; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 90 | int32_t len = 2; |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 91 | len += encodeChar((wchar_t)(c - 128), sb, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 92 | if (e != BCExceptionNO) |
| 93 | return -1; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 94 | return len; |
| 95 | } |
Lei Zhang | 6714ff8 | 2017-04-27 13:21:00 -0700 | [diff] [blame^] | 96 | e = BCExceptionIllegalArgument; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 97 | return -1; |
| 98 | } |