T168_Debug222\appl\Barcode\One文件:CodaBar.c

/******************************************************************************
 *                                                                            *
 *                         M O D U L E   D E F I N E                          *
 *                                                                            *
 ******************************************************************************/

#define CODABAR_C

/******************************************************************************
 *                                                                            *
 *        C O M P I L E R   D E F I N E D   I N C L U D E   F I L E S         *
 *                                                                            *
 ******************************************************************************/

#include <string.h>
#include <ctype.h>

/******************************************************************************
 *                                                                            *
 *            U S E R   D E F I N E D   I N C L U D E   F I L E S             *
 *                                                                            *
 ******************************************************************************/

#include "Common.h"
#include "XGraphic.h"
#include "Barcode.h"

/******************************************************************************
 *                                                                            *
 *                         L O C A L   D E F I N E S                          *
 *                                                                            *
 ******************************************************************************/

#define    CODABAR_TEXT_ERROR            0x02
#define    QUIET_MU                    0
#define    TEXT_DIS                    5

/******************************************************************************
 *                                                                            *
 *                        L O C A L   T Y P E D E F S                         *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *             L O C A L   F U N C T I O N   P R O T O T Y P E S              *
 *                                                                            *
 ******************************************************************************/

/* None */

/******************************************************************************
 *                                                                            *
 *    L O C A L   I N I T I A L I Z E D   D A T A   D E F I N I T I O N S     *
 *                                                                            *
 ******************************************************************************/

CONST BYTE CODABAR_TBL[20]=
{
    /*-----------------02-24-99 15:06-------------------
        0,1,2,3,4,5,6,7
    --------------------------------------------------*/
        0x03,0x06,0x09,0x60,0x12,0x42,0x21,0x24,
    /*-----------------02-24-99 15:06-------------------
        8,9,-,$,:,/,.,+
    --------------------------------------------------*/
        0x30,0x48,0x0c,0x18,0x45,0x51,0x54,0x15,
    /*-----------------02-24-99 15:16-------------------
        A,B,C,D
    --------------------------------------------------*/
        0x1a,0x29,0x0b,0x0e
};

CONST BYTE CODABAR_CHECK_TABLE[20]=
{
    '0','1','2','3','4','5','6','7','8','9',
    '-','$',':','/','.','+','A','B','C','D'
};

/******************************************************************************
 *                                                                            *
 *    L O C A L   U N I T I A L I Z E D   D A T A   D E F I N I T I O N S     *
 *                                                                            *
 ******************************************************************************/


/*-----------------08-16-99 17:15-------------------
 name:
    Codabar
 description:
    draw codabar to image buffer
 input:
    _BarCodeAttr structure

 output:
    TRUE:     OK
    FALSE:     ERROR
--------------------------------------------------*/
INT Codabar(_BarCodeAttr *psBarCodeAttr)
{
    INT iProcess, iTemp;
    INT iCheckC;
    BYTE bCodedata, bCodeMask, bTempdata;
    BYTE *pExpress;
    BYTE bsHuman[100];
    BYTE BarcodeData[100];
    BYTE *pHuman;
    BYTE EndChar;
    INT i, j;

    _BarAttr sBarAttr;
    _BarCodeAttr sBarCodeAttr;

    sBarCodeAttr = *psBarCodeAttr;
    pHuman = bsHuman;

    // if ZPL, filter out all byte not supported by Codabar
    if ( psBarCodeAttr->eLanguage == LANG_ZPL )
    {
        pExpress = sBarCodeAttr.pExp;
        i = 0;
        while ( *pExpress )
        {
            for ( j = 0 ; j < sizeof(CODABAR_CHECK_TABLE) ; j++ )
            {
                if ( CODABAR_CHECK_TABLE[j] == toupper(*pExpress) )
                {
                    BarcodeData[i] = toupper(*pExpress);
                    i += 1;
                    break;
                }
            }
            pExpress += 1;
        }
        BarcodeData[i] = '\0';
    }

    /*-----------------08-16-99 17:19-------------------
     ImagePoint initial
    --------------------------------------------------*/
    sBarAttr.psImageBuffer = sBarCodeAttr.psImageBuffer;
    pExpress = sBarCodeAttr.pExp;
    if ( psBarCodeAttr->eLanguage == LANG_ZPL )
        pExpress = BarcodeData;
    iProcess = 0;

    /*-----------------02-10-99 11:35-------------------
     decode
    --------------------------------------------------*/
    while(1)
    {
        /*-----------------02-10-99 11:29-------------------
         leading quiet zone
        --------------------------------------------------*/
        if ( iProcess == 0 )
        {
            switch ( sBarCodeAttr.iRotation )
            {
                case 90:
                    sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX - sBarCodeAttr.iHeight;
                    sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY + sBarCodeAttr.iNarrow * QUIET_MU;
                    break;
                case 180:
                    sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY - sBarCodeAttr.iHeight;
                    sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX - sBarCodeAttr.iNarrow * QUIET_MU;
                    break;
                case 270:
                    sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY - sBarCodeAttr.iNarrow * QUIET_MU;
                    break;
                case 0:
                default:
                    sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX + sBarCodeAttr.iNarrow * QUIET_MU;
                    break;
            }
            iProcess = 1;
            continue;
        }
        /*-----------------02-10-99 11:35-------------------
         start code
        --------------------------------------------------*/
        else if ( iProcess == 1 )
        {
            switch ( toupper(*pExpress) )
            {
                case 'A':
                    bCodedata = CODABAR_TBL[16];
                    break;
                case 'B':
                    bCodedata = CODABAR_TBL[17];
                    break;
                case 'C':
                case '*':
                    bCodedata = CODABAR_TBL[18];
                    break;
                case 'D':
                    bCodedata = CODABAR_TBL[19];
                    break;
                default:
                    return CODABAR_TEXT_ERROR;
            }
            *pHuman++ = *pExpress++;
            iProcess = 2;
        }
        /*-----------------02-10-99 12:06-------------------
         decodeer
        --------------------------------------------------*/
        else if ( iProcess == 2 )
        {
            if ( (*pExpress >= '0') && (*pExpress <= '9') )
            {
                bCodedata = CODABAR_TBL[(*pExpress - '0')];
                *pHuman++ = *pExpress++;
            }
            else
            {
                switch ( toupper(*pExpress) )
                {
                    case 'A':
                        bTempdata = 16;
                        EndChar = *pExpress;
                        iProcess = 3;
                        continue;
                    case 'B':
                        bTempdata = 17;
                        EndChar = *pExpress;
                        iProcess = 3;
                        continue;
                    case 'C':
                    case '*':
                        bTempdata = 18;
                        EndChar = *pExpress;
                        iProcess = 3;
                        continue;
                    case 'D':
                        bTempdata = 19;
                        EndChar = *pExpress;
                        iProcess = 3;
                        continue;
                    case '-':
                        bCodedata = CODABAR_TBL[10];
                        *pHuman++ = *pExpress++;
                        break;
                    case '$':
                        bCodedata = CODABAR_TBL[11];
                        *pHuman++ = *pExpress++;
                        break;
                    case ':':
                        bCodedata = CODABAR_TBL[12];
                        *pHuman++ = *pExpress++;
                        break;
                    case '/':
                        bCodedata = CODABAR_TBL[13];
                        *pHuman++ = *pExpress++;
                        break;
                    case '.':
                        bCodedata = CODABAR_TBL[14];
                        *pHuman++ = *pExpress++;
                        break;
                    case '+':
                        bCodedata = CODABAR_TBL[15];
                        *pHuman++ = *pExpress++;
                        break;
                    default:
                        return CODABAR_TEXT_ERROR;
                }
            }
        }
        /*-----------------02-10-99 14:52-------------------
         C check digital
        --------------------------------------------------*/
        else if( iProcess == 3 )
        {
            if ( sBarCodeAttr.iCheck != 1 )
            {
                *pHuman++ = EndChar;
                *pHuman = 0;
                iProcess = 4;
                continue;
            }
            pExpress = sBarCodeAttr.pExp;
            if ( psBarCodeAttr->eLanguage == LANG_ZPL )
                pExpress = BarcodeData;
            iCheckC = 0;
            while (*pExpress != 0)   
            {
                if ( (*pExpress>='0') && (*pExpress<='9') )
                {
                    iTemp = (*pExpress-'0');
                    pExpress++;
                }
                else
                {
                    switch ( toupper(*pExpress) )
                    {
                        case '-':
                            iTemp = 10;
                            ++pExpress;
                            break;
                        case '$':
                            iTemp = 11;
                            ++pExpress;
                            break;
                        case ':':
                            iTemp = 12;
                            ++pExpress;
                            break;
                        case '/':
                            iTemp = 13;
                            ++pExpress;
                            break;
                        case '.':
                            iTemp = 14;
                            ++pExpress;
                            break;
                        case '+':
                            iTemp = 15;
                            ++pExpress;
                            break;
                        case 'A':
                            iTemp = 16;
                            ++pExpress;
                            break;
                        case 'B':
                            iTemp = 17;
                            ++pExpress;
                            break;
                        case 'C':
                        case '*':
                            iTemp = 18;
                            ++pExpress;
                            break;
                        case 'D':
                            iTemp = 19;
                            ++pExpress;
                            break;
                        default:
                            return CODABAR_TEXT_ERROR;
                    }
                }
                iCheckC = iCheckC+iTemp;
            }
            bCodedata = CODABAR_TBL[iCheckC%16];
            *pHuman++ = CODABAR_CHECK_TABLE[iCheckC%16];
            *pHuman++ = EndChar;
            *pHuman = 0;
            iProcess=4;
        }
        /*-----------------02-10-99 12:07-------------------
         end code
        --------------------------------------------------*/
        else if ( iProcess == 4 )
        {
            bCodedata = CODABAR_TBL[bTempdata];
            iProcess = 5;
        }
        /*-----------------02-10-99 11:35-------------------
         draw bar
        --------------------------------------------------*/
        bCodeMask = 0x80;
        iTemp = 7;
        while (iTemp >= 0)
        {
            iTemp--;
            bCodeMask = bCodeMask>>1;
            sBarAttr.iType = DRAW_BAR;
            if ((iTemp % 2) == 0)
            {
                switch (sBarCodeAttr.iRotation)
                {
                    case 90:
                        sBarAttr.sCoord.iX = sBarCodeAttr.sCoord.iX;
                        sBarAttr.sCoord.iY = sBarCodeAttr.sCoord.iY;
                        sBarAttr.iWidth = sBarCodeAttr.iHeight;
                        if ((bCodedata & bCodeMask) != 0)
                        {
                            sBarAttr.iHeight = sBarCodeAttr.iWide;
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY+sBarCodeAttr.iWide;
                        }
                        else
                        {
                            sBarAttr.iHeight = sBarCodeAttr.iNarrow;
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY+sBarCodeAttr.iNarrow;
                        }
                        break;
                    case 180:
                        sBarAttr.sCoord.iY = sBarCodeAttr.sCoord.iY;
                        if ((bCodedata & bCodeMask) != 0)
                        {
                            sBarAttr.iWidth = sBarCodeAttr.iWide;
                            sBarAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iWide;
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iWide;
                        }
                        else
                        {
                            sBarAttr.iWidth = sBarCodeAttr.iNarrow;
                            sBarAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iNarrow;
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iNarrow;
                        }
                        sBarAttr.iHeight = sBarCodeAttr.iHeight;
                        break;
                    case 270:
                        sBarAttr.sCoord.iX = sBarCodeAttr.sCoord.iX;
                        sBarAttr.iWidth = sBarCodeAttr.iHeight;
                        if ((bCodedata & bCodeMask) != 0)
                        {
                            sBarAttr.iHeight = sBarCodeAttr.iWide;
                            sBarAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iWide;
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iWide;
                        }
                        else
                        {
                            sBarAttr.iHeight = sBarCodeAttr.iNarrow;
                            sBarAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iNarrow;
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iNarrow;
                        }
                        break;
                    case 0:
                    default:
                        sBarAttr.sCoord.iX = sBarCodeAttr.sCoord.iX;
                        sBarAttr.sCoord.iY = sBarCodeAttr.sCoord.iY;
                        if ((bCodedata & bCodeMask) != 0)
                        {
                            sBarAttr.iWidth = sBarCodeAttr.iWide;
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX+sBarCodeAttr.iWide;
                        }
                        else
                        {
                            sBarAttr.iWidth = sBarCodeAttr.iNarrow;
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX+sBarCodeAttr.iNarrow;
                        }
                        sBarAttr.iHeight = sBarCodeAttr.iHeight;
                        break;
                }
                DrawBar(&sBarAttr);
            }
            else
            {
                switch ( sBarCodeAttr.iRotation )
                {
                    case 90:
                        if ((bCodedata & bCodeMask) != 0)
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY+sBarCodeAttr.iWide;
                        else
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY+sBarCodeAttr.iNarrow;
                        break;
                    case 180:
                        if ((bCodedata & bCodeMask)!= 0)
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iWide;
                        else
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX-sBarCodeAttr.iNarrow;
                        break;
                    case 270:
                        if ((bCodedata & bCodeMask) != 0)
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iWide;
                        else
                            sBarCodeAttr.sCoord.iY = sBarCodeAttr.sCoord.iY-sBarCodeAttr.iNarrow;
                        break;
                    case 0:
                    default:
                        if ((bCodedata & bCodeMask) != 0)
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX+sBarCodeAttr.iWide;
                        else
                            sBarCodeAttr.sCoord.iX = sBarCodeAttr.sCoord.iX+sBarCodeAttr.iNarrow;
                        break;
                }
            }
        }
        /*-----------------02-10-99 11:40-------------------
         if draw ok
        --------------------------------------------------*/
        if (iProcess == 5)
        {
            switch ( sBarCodeAttr.iRotation )
            {
                case 90:
                    psBarCodeAttr->TotalWidth  = sBarCodeAttr.sCoord.iY - psBarCodeAttr->sCoord.iY;
                    psBarCodeAttr->TotalHeight = psBarCodeAttr->iHeight;
                    break;
                case 180:
                    psBarCodeAttr->TotalWidth  = psBarCodeAttr->sCoord.iX - sBarCodeAttr.sCoord.iX;
                    psBarCodeAttr->TotalHeight = psBarCodeAttr->iHeight;
                    break;
                case 270:
                    psBarCodeAttr->TotalWidth  = psBarCodeAttr->sCoord.iY - sBarCodeAttr.sCoord.iY;
                    psBarCodeAttr->TotalHeight = psBarCodeAttr->iHeight;
                    break;
                case 0:
                default:
                    psBarCodeAttr->TotalWidth  = sBarCodeAttr.sCoord.iX - psBarCodeAttr->sCoord.iX;
                    psBarCodeAttr->TotalHeight = psBarCodeAttr->iHeight;
                    break;
            }
            break;
        }
    }
    /*-----------------02-24-99 11:26-------------------
     human readable is true draw text
    --------------------------------------------------*/
    if ( psBarCodeAttr->iHuman != 0 )
    {
        _TextAttr sTextAttr;

        sTextAttr.sCoord.iX  = psBarCodeAttr->sCoord.iX;
        sTextAttr.sCoord.iY  = psBarCodeAttr->sCoord.iY;
        sTextAttr.iDirection = psBarCodeAttr->iRotation;
        sTextAttr.iRotation  = psBarCodeAttr->iRotation;
        sTextAttr.iDistance  = 0;
        sTextAttr.iHoriMulti = psBarCodeAttr->FontHoriMulti;
        sTextAttr.iVertMulti = psBarCodeAttr->FontVertMulti;
        sTextAttr.ExpLength  = strlen((CHAR *)bsHuman);
        sTextAttr.pExp       = bsHuman;
        sTextAttr.pFont      = psBarCodeAttr->pFont;
        sTextAttr.eLanguage  = psBarCodeAttr->eLanguage;
        sTextAttr.ePutWay    = psBarCodeAttr->ePutWay;

        sTextAttr.psImageBuffer = psBarCodeAttr->psImageBuffer;

        PreviewText(&sTextAttr);

        switch ( psBarCodeAttr->iRotation )
        {
            case 90:
                if ( psBarCodeAttr->AboveCode )
                    sTextAttr.sCoord.iX += sTextAttr.iRealHeight + TEXT_DIS;
                else
                    sTextAttr.sCoord.iX -= psBarCodeAttr->iHeight + TEXT_DIS;

                sTextAttr.sCoord.iY += psBarCodeAttr->iNarrow * QUIET_MU;
                if ( psBarCodeAttr->iHuman == 2 )
                    sTextAttr.sCoord.iY += (psBarCodeAttr->TotalWidth - sTextAttr.iRealWidth) / 2;
                else if ( psBarCodeAttr->iHuman == 3 )
                    sTextAttr.sCoord.iY = sBarCodeAttr.sCoord.iY - sTextAttr.iRealWidth;

                break;

            case 180:
                if ( psBarCodeAttr->AboveCode )
                    sTextAttr.sCoord.iY += sTextAttr.iRealHeight + TEXT_DIS;
                else
                    sTextAttr.sCoord.iY -= psBarCodeAttr->iHeight + TEXT_DIS;

                sTextAttr.sCoord.iX -= psBarCodeAttr->iNarrow * QUIET_MU;
                if ( psBarCodeAttr->iHuman == 2 )
                    sTextAttr.sCoord.iX -= (psBarCodeAttr->TotalWidth - sTextAttr.iRealWidth) / 2;
                else if ( psBarCodeAttr->iHuman == 3 )
                    sTextAttr.sCoord.iX = sBarCodeAttr.sCoord.iX + sTextAttr.iRealWidth;

                break;

            case 270:
                if ( psBarCodeAttr->AboveCode )
                    sTextAttr.sCoord.iX -= sTextAttr.iRealHeight + TEXT_DIS;
                else
                    sTextAttr.sCoord.iX += psBarCodeAttr->iHeight + TEXT_DIS;

                sTextAttr.sCoord.iY -= psBarCodeAttr->iNarrow * QUIET_MU;
                if ( psBarCodeAttr->iHuman == 2 )
                    sTextAttr.sCoord.iY -= (psBarCodeAttr->TotalWidth - sTextAttr.iRealWidth) / 2;
                else if ( psBarCodeAttr->iHuman == 3 )
                    sTextAttr.sCoord.iY = sBarCodeAttr.sCoord.iY + sTextAttr.iRealWidth;

                break;

            case 0:
            default:
                if ( psBarCodeAttr->AboveCode )
                    sTextAttr.sCoord.iY -= sTextAttr.iRealHeight + TEXT_DIS;
                else
                    sTextAttr.sCoord.iY += psBarCodeAttr->iHeight + TEXT_DIS;

                sTextAttr.sCoord.iX += psBarCodeAttr->iNarrow * QUIET_MU;
                if ( psBarCodeAttr->iHuman == 2 )
                    sTextAttr.sCoord.iX += (psBarCodeAttr->TotalWidth - sTextAttr.iRealWidth) / 2;
                else if ( psBarCodeAttr->iHuman == 3 )
                    sTextAttr.sCoord.iX = sBarCodeAttr.sCoord.iX - sTextAttr.iRealWidth;

                break;
        }
        OutText(&sTextAttr);
        psBarCodeAttr->TotalHeight += sTextAttr.iRealHeight + TEXT_DIS;
        psBarCodeAttr->FontWidth = sTextAttr.iRealWidth;
        psBarCodeAttr->FontHeight = sTextAttr.iRealHeight + TEXT_DIS;
    }

    return TRUE;
}

INT PreviewCodabar(_BarCodeAttr *psBarCodeAttr)
{
    CHAR *pExp;
    INT TotalLen;

    pExp = (CHAR *)psBarCodeAttr->pExp;

    TotalLen = 0;
    while (*pExp)
    {
        if (((*pExp >= '0') && (*pExp <= '9')) || (*pExp <= '-') || (*pExp <= '$'))
            TotalLen += 6 * psBarCodeAttr->iNarrow + 2 * psBarCodeAttr->iWide;
        else
            TotalLen += 5 * psBarCodeAttr->iNarrow + 3 * psBarCodeAttr->iWide;
        pExp += 1;
    }

    psBarCodeAttr->TotalWidth =    TotalLen - psBarCodeAttr->iNarrow + QUIET_MU;
    psBarCodeAttr->TotalHeight = psBarCodeAttr->iHeight;

    if (psBarCodeAttr->eLanguage == LANG_DPL && psBarCodeAttr->iHuman)
    {
        _TextAttr sTextAttr;

        sTextAttr.sCoord.iX  = psBarCodeAttr->sCoord.iX;
        sTextAttr.sCoord.iY  = psBarCodeAttr->sCoord.iY;
        sTextAttr.iDirection = psBarCodeAttr->iRotation;
        sTextAttr.iRotation  = psBarCodeAttr->iRotation;
        sTextAttr.iDistance  = 0;
        sTextAttr.iHoriMulti = psBarCodeAttr->FontHoriMulti;
        sTextAttr.iVertMulti = psBarCodeAttr->FontVertMulti;
        sTextAttr.ExpLength  = strlen((CHAR *)psBarCodeAttr->pExp);
        sTextAttr.pExp       = psBarCodeAttr->pExp;
        sTextAttr.pFont      = psBarCodeAttr->pFont;
        sTextAttr.eLanguage  = psBarCodeAttr->eLanguage;
        sTextAttr.ePutWay    = psBarCodeAttr->ePutWay;

        sTextAttr.psImageBuffer = psBarCodeAttr->psImageBuffer;

        PreviewText(&sTextAttr);

        psBarCodeAttr->TotalHeight += sTextAttr.iRealHeight + TEXT_DIS;
        psBarCodeAttr->FontWidth = sTextAttr.iRealWidth;
        psBarCodeAttr->FontHeight = sTextAttr.iRealHeight + TEXT_DIS;
    }
    return TRUE;
}

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值