#include <vcl.h>
#include <iostream.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include "libcean8.h"
#include "bcean8.h"
#include "kbc.h"
char *Table_EANA[]={"2605", //{ 0 }
"1615", //{ 1 }
"1516", //{ 2 }
"0805", //{ 3 }
"0526", //{ 4 }
"0625", //{ 5 }
"0508", //{ 6 }
"0706", //{ 7 }
"0607", //{ 8 }
"2506", //{ 9 }
NULL};
char *Table_EANB[]={"0517", //{ 0 }
"0616", //{ 1 }
"1606", //{ 2 }
"0535", //{ 3 }
"1705", //{ 4 }
"0715", //{ 5 }
"3505", //{ 6 }
"1525", //{ 7 }
"2515", //{ 8 }
"1507", //{ 9 }
NULL};
char *Table_EANC[]={"7150", //{ 0 }
"6160", //{ 1 }
"6061", //{ 2 }
"5350", //{ 3 }
"5071", //{ 4 }
"5170", //{ 5 }
"5053", //{ 6 }
"5251", //{ 7 }
"5152", //{ 8 }
"7051", //{ 9 }
NULL};
static ABC_PLUGIN bcplg ;
static void GetBarWidthEAN8(int BoldLevel, int *oWidth, int *iWidth)
{
*iWidth=((11+56)+BAR_BLANK_RESERVE*2)*BoldLevel/10;
*oWidth=((11+56)+BAR_BLANK_RESERVE*2)*(int)(BoldLevel/10);
}
static bool IsSymbolCharEAN8(IN PACBC bc,IN char c)
{
return isdigit(c);
}
static bool setFont(IN PACBC bc,IN const char *name,int size,int space,int group)
{
bool nrlt=false;
Graphics::TCanvas* oCanvas = bc->bmp->Canvas;
if (name && oCanvas->Font->Name.AnsiCompareIC(name)) {
oCanvas->Font->Name = name;
nrlt = true;
}
if (size && (oCanvas->Font->Size != size)) {
oCanvas->Font->Size = size;
nrlt = true;
}
if (nrlt)
bc->changed = true;
return true;
}
static bool doCheckContent(IN PACBC bc,IN char *values)
{
int nCnt = strlen(values);
if (nCnt>BAR_EAN8_LEN) return false;
for (int i=0;i<nCnt;i++)
{
if (!isdigit(values[i]))
return false;
}
return true;
}
static bool CheckCodeLength(IN PACBC bc,IN int dlen)
{
if (BAR_EAN8_LEN!=dlen)
return false;
return true;
}
static unsigned char Code_EAN8(char* strData, char* strReturn, unsigned char iIndex)
{
if (iIndex>=1 && iIndex<=4 && strlen(strData)==1)
sprintf(strReturn,"%s",&Table_EANA[atoi(strData)][0]);
else if (iIndex>=5 && iIndex<=8 && strlen(strData)==1)
sprintf(strReturn,"%s",&Table_EANC[atoi(strData)][0]);
else return 0;
return 1;
}
static void DrawTextEAN8(IN PACBC bc)
{
int x,y,i,w;
char* strContent =bc->strContent;
float FBoldLevel = bc->widthlevel/10.0;
Graphics::TCanvas* oCanvas = bc->bmp->Canvas;
w = oCanvas->TextWidth("0");
y = bc->barheight - bc->fontheight;
//oCanvas->Pen->Color=clBlack;
x=(3+BAR_BLANK_RESERVE)*FBoldLevel;
for(i=0;i<4;i++)
{
DrawBCWord(bc, x+(7 * FBoldLevel - w)/2, y, strContent[i]);
x += 7 * FBoldLevel;
}
x=(36+BAR_BLANK_RESERVE)*FBoldLevel;
for(i=4;i<8;i++)
{
DrawBCWord(bc, x+(7 * FBoldLevel -w)/2, y, strContent[i]);
x += 7 * FBoldLevel;
}
}
static void DrawLineEAN8(IN PACBC bc)
{
int iLen,iDrawClr,itempHeight;
int i,j,iBedeck;
char strData[32],strByte[5];
char* strContent =bc->strContent;
int FHeight = bc->barheight;
int y = 0, x = bc->widthlevel / 10 * BAR_BLANK_RESERVE;
Graphics::TCanvas *oCanvas = bc->obmp->Canvas;
oCanvas->Brush->Color = clBlack;
iBedeck = bc->bedeck * bc->widthlevel / 1000;
if (bc->textmode)
itempHeight= FHeight - bc->fontheight;
else
itempHeight = FHeight;
sprintf(strData,"%s","505");
iLen=strlen(strData);
for(i=0;i<iLen;i++)
x=DrawLine(oCanvas, strData[i], FHeight, x, y, iBedeck);
for(j=1;j<=4;j++)
{
sprintf(strByte,"%c",strContent[j-1]);
Code_EAN8(strByte, strData, j);
iLen=strlen(strData);
for(i=0;i<iLen;i++)
x=DrawLine(oCanvas, strData[i], itempHeight, x, y, iBedeck);
}
y=0;
sprintf(strData,"%s","05050"); //Center Guard Pattern
iLen=strlen(strData);
for(i=0;i<iLen;i++)
x=DrawLine(oCanvas, strData[i], FHeight, x, y, iBedeck);
for(j=5;j<=8;j++)
{
memset(strByte,0,sizeof(strByte));
memset(strData,0,sizeof(strData));
sprintf(strByte,"%c",strContent[j-1]);
Code_EAN8(strByte, strData, j);
iLen=strlen(strData);
for(i=0;i<iLen;i++)
x=DrawLine(oCanvas, strData[i], itempHeight, x, y, iBedeck);
}
y=0;
sprintf(strData,"%s","505"); //StopCode
iLen=strlen(strData);
for(i=0;i<iLen;i++)
x=DrawLine(oCanvas, strData[i], FHeight, x, y, iBedeck);
oCanvas->Brush->Color = clWhite;
}
static void CheckDrawContent(PACBC bc)
{
unsigned char* FContent = bc->strContent ;
int iContentLen = bc->contentLen;
memset(FContent,0,sizeof(bc->strContent));
if (BAR_EAN8_LEN>iContentLen) {
memset(FContent, '0', BAR_EAN8_LEN);
memcpy(FContent+BAR_EAN8_LEN-iContentLen, bc->content, iContentLen);
}
else memcpy(FContent, bc->content, BAR_EAN8_LEN);
DeleteCharacter(FContent);
//check sum if need
int iChkSum=CheckSumModulo10(FContent, BAR_EAN8_LEN);
FContent[BAR_EAN8_LEN]=0x30+iChkSum;
FContent[BAR_EAN8_LEN+1]=0x00;
int ow;
GetBarWidthEAN8(bc->widthlevel, &ow, &bc->barwidth);
ResizeOImage(bc, ow, false);
}
static bool Draw(IN PACBC bc,OUT void* data,IN OUT int* dlen)
{
if(bc->changed)
{
CalculateWidth(bc->widthlevel/10);
CheckDrawContent(bc);
DrawLineEAN8(bc);
if (bc->textmode) {
ResizeImage(bc);
bc->bmp->Canvas->CopyRect(bc->bmp->Canvas->ClipRect, bc->obmp->Canvas, bc->obmp->Canvas->ClipRect);
DrawTextEAN8(bc);
bc->Tms->Clear();
bc->bmp->SaveToStream(bc->Tms);
}
else {
if (bc->widthlevel % 10) {
ResizeImage(bc);
bc->bmp->Canvas->CopyRect(bc->bmp->Canvas->ClipRect, bc->obmp->Canvas, bc->obmp->Canvas->ClipRect);
bc->Tms->Clear();
bc->bmp->SaveToStream(bc->Tms);
}
else {
bc->Tms->Clear();
bc->obmp->SaveToStream(bc->Tms);
}
}
bc->changed = false;
}
if(data != NULL)
{
bc->Tms->Position = 0 ;
bc->Tms->ReadBuffer(data,bc->Tms->Size);
}
if(dlen !=NULL)
{
*dlen = bc->Tms->Size;
}
return true;
}
static PACBC Create(void)
{
PACBC bc;
bc = (PACBC)calloc(sizeof(ACBC),sizeof(char));
bc->privatedata = calloc(sizeof(CBC_EAN8),sizeof(char));
bc->uid = bcplg.uid;
bc->bartype = bcplg.bartype;
bc->plugindex = bcplg.plugindex;
bc->datasize = sizeof(CBC_EAN8_IN);
//default content
memset(bc->content,0,sizeof(bc->content));
bc->contentLen = 0;
bc->obmp = new Graphics::TBitmap();
bc->obmp->PixelFormat = pf1bit;
bc->bmp = new Graphics::TBitmap();
bc->bmp->PixelFormat = pf1bit;
//bc->canvas = bc->bmp->Canvas;
bc->Tms = new TMemoryStream();
return bc;
}
static void Destroy(PACBC bc)
{
if (bc->bmp)
bc->bmp->Free();
if (bc->obmp)
bc->obmp->Free();
if (bc->Tms)
bc->Tms->Free();
free(bc->privatedata);
free(bc);
}
static bool LoadFrom( PACBC bc, void* data , int dlen)
{
if(dlen != bc->datasize)