summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/src/BC_DataMatrixWriter.cpp
blob: de122035fe8a20844c4d1cc30df832d48a429477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "barcode.h"
#include "include/BC_Writer.h"
#include "include/BC_TwoDimWriter.h"
#include "include/BC_Encoder.h"
#include "include/BC_DefaultPlacement.h"
#include "include/BC_BinaryBitmap.h"
#include "include/BC_CommonBitMatrix.h"
#include "include/BC_SymbolShapeHint.h"
#include "include/BC_SymbolInfo.h"
#include "include/BC_DataMatrixSymbolInfo144.h"
#include "include/BC_ErrorCorrection.h"
#include "include/BC_Dimension.h"
#include "include/BC_EncoderContext.h"
#include "include/BC_C40Encoder.h"
#include "include/BC_TextEncoder.h"
#include "include/BC_X12Encoder.h"
#include "include/BC_EdifactEncoder.h"
#include "include/BC_Base256Encoder.h"
#include "include/BC_ASCIIEncoder.h"
#include "include/BC_HighLevelEncoder.h"
#include "include/BC_CommonByteMatrix.h"
#include "include/BC_DataMatrixWriter.h"
#include "include/BC_UtilCodingConvert.h"
CBC_DataMatrixWriter::CBC_DataMatrixWriter()
{
}
CBC_DataMatrixWriter::~CBC_DataMatrixWriter()
{
}
FX_BOOL CBC_DataMatrixWriter::SetErrorCorrectionLevel (FX_INT32 level)
{
    m_iCorrectLevel = level;
    return TRUE;
}
FX_BYTE* CBC_DataMatrixWriter::Encode(const CFX_WideString &contents, FX_INT32 &outWidth, FX_INT32 &outHeight, FX_INT32 &e)
{
    if (outWidth < 0 || outHeight < 0) {
        e = BCExceptionHeightAndWidthMustBeAtLeast1;
        BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    }
    CBC_SymbolShapeHint::SymbolShapeHint shape = CBC_SymbolShapeHint::FORCE_SQUARE;
    CBC_Dimension* minSize = NULL;
    CBC_Dimension* maxSize = NULL;
    CFX_WideString ecLevel;
    CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel(contents, ecLevel, shape, minSize, maxSize, e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup(encoded.GetLength(), shape, minSize, maxSize, TRUE, e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    CFX_WideString codewords = CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    CBC_DefaultPlacement* placement = FX_NEW CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), symbolInfo->getSymbolDataHeight(e));
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    placement->place();
    CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    outWidth = bytematrix->GetWidth();
    outHeight = bytematrix->GetHeight();
    FX_BYTE* result = FX_Alloc(FX_BYTE, outWidth * outHeight);
    FXSYS_memcpy32(result, bytematrix->GetArray(), outWidth * outHeight);
    delete bytematrix;
    delete placement;
    return result;
}
FX_BYTE *CBC_DataMatrixWriter::Encode(const CFX_ByteString &contents, BCFORMAT format, FX_INT32 &outWidth, FX_INT32 &outHeight, FX_INT32 &e)
{
    return NULL;
}
FX_BYTE *CBC_DataMatrixWriter::Encode(const CFX_ByteString &contents, BCFORMAT format, FX_INT32 &outWidth, FX_INT32 &outHeight, FX_INT32 hints, FX_INT32 &e)
{
    return NULL;
}
CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel(CBC_DefaultPlacement* placement, CBC_SymbolInfo* symbolInfo, FX_INT32 &e)
{
    FX_INT32 symbolWidth = symbolInfo->getSymbolDataWidth(e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    FX_INT32 symbolHeight = symbolInfo->getSymbolDataHeight(e);
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    CBC_CommonByteMatrix* matrix = FX_NEW CBC_CommonByteMatrix(symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e));
    BC_EXCEPTION_CHECK_ReturnValue(e,  NULL);
    matrix->Init();
    FX_INT32 matrixY = 0;
    for (FX_INT32 y = 0; y < symbolHeight; y++) {
        FX_INT32 matrixX;
        if ((y % symbolInfo->m_matrixHeight) == 0) {
            matrixX = 0;
            for (FX_INT32 x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
                matrix->Set(matrixX, matrixY, (x % 2) == 0);
                matrixX++;
            }
            matrixY++;
        }
        matrixX = 0;
        for (FX_INT32 x = 0; x < symbolWidth; x++) {
            if ((x % symbolInfo->m_matrixWidth) == 0) {
                matrix->Set(matrixX, matrixY, TRUE);
                matrixX++;
            }
            matrix->Set(matrixX, matrixY, placement->getBit(x, y));
            matrixX++;
            if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) {
                matrix->Set(matrixX, matrixY, (y % 2) == 0);
                matrixX++;
            }
        }
        matrixY++;
        if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) {
            matrixX = 0;
            for (FX_INT32 x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
                matrix->Set(matrixX, matrixY, TRUE);
                matrixX++;
            }
            matrixY++;
        }
    }
    return matrix;
}