#include "code39.h"

QMap<QString,QString> InitMap()
{
//    if(!map.isEmpty()) return;
    QMap<QString,QString> ret;
    ret.insert("A","110101001011");
    ret.insert("B","101101001011");
    ret.insert("C","110110100101");
    ret.insert("D","101011001011");
    ret.insert("E","110101100101");
    ret.insert("F","101101100101");
    ret.insert("G","101010011011");
    ret.insert("H","110101001101");
    ret.insert("I","101101001101");
    ret.insert("J","101011001101");
    ret.insert("K","110101010011");
    ret.insert("L","101101010011");
    ret.insert("M","110110101001");
    ret.insert("N","101011010011");
    ret.insert("O","110101101001");
    ret.insert("P","101101101001");
    ret.insert("Q","101010110011");
    ret.insert("R","110101011001");
    ret.insert("S","101101011001");
    ret.insert("T","101011011001");
    ret.insert("U","110010101011");
    ret.insert("V","100110101011");
    ret.insert("W","110011010101");
    ret.insert("X","100101101011");
    ret.insert("Y","110010110101");
    ret.insert("Z","100110110101");
    ret.insert("0","101001101101");
    ret.insert("1","110100101011");
    ret.insert("2","101100101011");
    ret.insert("3","110110010101");
    ret.insert("4","101001101011");
    ret.insert("5","110100110101");
    ret.insert("6","101100110101");
    ret.insert("7","101001011011");
    ret.insert("8","110100101101");
    ret.insert("9","101100101101");
    ret.insert("+","100101001001");
    ret.insert("-","100101011011");
    ret.insert("*","100101101101");
    ret.insert("/","100100101001");
    ret.insert("%","101001001001");
    ret.insert("$","100100100101");
    ret.insert(".","110010101101");
    ret.insert(" ","100110101101");
    return ret;
}

static QMap<QString,QString> map = InitMap();

Code39::Code39()
{

//    InitMap();
}



void Code39::draw(QPainter & painter, QRect & pos,QString & barcode)
{
    QString BinaryNum = CodeBinary(barcode);
    int MAXHEIGHT = pos.height();
    int MAXWIDTH = pos.width();
    int FONTHEIGHT=10; //预留10px的左右空白；
    int widght = MAXWIDTH - FONTHEIGHT;
    //设定条码线条宽度px
    int LineWidth = widght / BinaryNum.size();
    int move= (MAXWIDTH-LineWidth*BinaryNum.size())/2;
    //QBitmap bmp(MAXWIDTH,MAXHEIGHT+FONTHEIGHT);
//    QPainter painter(&image);
    painter.save();
//    painter.setWindow(pos);
    QPen white,black;
    //    painter.setRenderHint(QPainter::Antialiasing,true);//弧线圆润
    white.setColor(QColor(255,255,255));
    black.setColor(QColor(0,0,0));
    white.setWidth(LineWidth);
    black.setWidth(LineWidth);

    //背景充填白色
    painter.setPen(white);
    painter.drawRect(0,0,MAXWIDTH,MAXHEIGHT+FONTHEIGHT);

    //画线
    for(int i=0;i<BinaryNum.size();i++)
    {
        if(BinaryNum.at(i)=='1') painter.setPen(black);
        else painter.setPen(white);
        painter.drawLine(i*LineWidth+LineWidth/2+move,pos.top(),i*LineWidth+LineWidth/2+move,MAXHEIGHT);
    }

//    qDebug()<<barcode<<endl;
    //添加底部条形码字符
    painter.setPen(black);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setRenderHint(QPainter::TextAntialiasing,true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing,true);
    QFont font;
    font.setFamily("宋体");
    font.setPixelSize(5*5);
    font.setWeight(QFont::ExtraLight);
    painter.setFont(font);
    painter.drawText(0*5,MAXHEIGHT+1,MAXWIDTH,17,Qt::AlignCenter,/*"S/N:" +*/ barcode);

    painter.restore();
}
/*
QImage Code39::draw(QString BinaryNum,QString barcode)
{
    int MAXWIDTH;
    int MAXHEIGHT=30;
    int FONTHEIGHT=10;

    //设定条码线条宽度px
    int LineWidth=1;
    //预留10px的左右空白；
    MAXWIDTH=LineWidth*BinaryNum.size()+5;

    //计算条线宽度时，精度丢失造成条码右边空白，进行右移居中
    int move=(MAXWIDTH-LineWidth*BinaryNum.size())/2;

    QSize size(MAXWIDTH,MAXHEIGHT+FONTHEIGHT);
    QImage image(size,QImage::Format_ARGB32);
    image.fill(qRgba(255, 255, 255, 0));

    //QBitmap bmp(MAXWIDTH,MAXHEIGHT+FONTHEIGHT);
    QPainter painter(&image);
    QPen white,black;
    //    painter.setRenderHint(QPainter::Antialiasing,true);//弧线圆润
    white.setColor(QColor(255,255,255));
    black.setColor(QColor(0,0,0));
    white.setWidth(LineWidth);
    black.setWidth(LineWidth);

    //背景充填白色
    painter.setPen(white);
    painter.drawRect(0,0,MAXWIDTH,MAXHEIGHT+FONTHEIGHT);

    //画线
    for(int i=0;i<BinaryNum.size();i++)
    {
        if(BinaryNum.at(i)=='1') painter.setPen(black);
        else painter.setPen(white);
        painter.drawLine(i*LineWidth+LineWidth/2+move,0,i*LineWidth+LineWidth/2+move,MAXHEIGHT);
    }

//    qDebug()<<barcode<<endl;
    //添加底部条形码字符
    painter.setPen(black);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setRenderHint(QPainter::TextAntialiasing,true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing,true);
    QFont font;
    font.setFamily("宋体");
    font.setPixelSize(10);
    font.setWeight(QFont::ExtraLight);
    painter.setFont(font);
    painter.drawText(5,30,MAXWIDTH,10,Qt::AlignCenter,"S/N:"barcode);
//    bool isSaved = image.save(path + barcode + ".bmp");
//    qDebug()<<isSaved;
//    return image;
//}*/


QString Code39::CodeBinary(QString barcode)
{
    barcode="*"+barcode+"*";
    QString str="";
    for(int i=0;i<barcode.size();i++)
    {
        str+= map[barcode.at(i)]+"0";//每个字符的二进制逻辑码之间，用“0”隔开（加一条白线）
    }
    return str;
}



//void Code39::reset(QString BarCode, QString SavePath)
//{
////    barcode=BarCode;
////    path=SavePath;
//}
