Reader.cpp
上传用户:hell82222
上传日期:2013-12-19
资源大小:1872k
文件大小:38k
- // Reader.cpp: implementation of the CReader class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "FileSwitch.h"
- #include "Reader.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CStdioFile g_CensorialFile; //监察转换进度的文件
- int g_mode;
- CTypedPtrMap<CMapStringToPtr, CString, void*> g_afont;
- CReader::CReader()
- {
- g_mode=0;
- _CONV_OPEN;
- }
- CReader::~CReader()
- {
- _CONV_CLOSE;
- g_afont.RemoveAll();
- }
- BOOL CReader::calculate_findfontname(CString strfontfile2,CString strfontfile,CString &strfontname)
- {
- //为防止破坏系统的字体文件,将文件拷贝到临时目录.
- CString strPath;
- {
- TCHAR szFileName[MAX_PATH];
- ::GetModuleFileName(::GetModuleHandle(_T("FileSwitch.dll")),szFileName,MAX_PATH);
- (*_tcsrchr(szFileName,'\'))=' ';
- strPath=szFileName;
- strPath+="\";
- strPath+=strfontfile2;
- }
- try
- {
- if(! ::CopyFile(strfontfile,strPath,FALSE) )
- {
- int i=::GetLastError();
- return FALSE;
- }
- strfontfile=strPath;
- }
- catch(...)
- {
- return FALSE;
- }
- CFile file;
- if(!file.Open(strfontfile,CFile::modeRead))
- {
- try{::DeleteFile(strfontfile);}catch(...){}
- return FALSE;
- }
- unsigned int inumTables;
- unsigned int isearchRange;
- unsigned int ientrySelector;
- unsigned int irangeShift;
- {
- __int16 data;
- //1.Fixed sfnt version 0x00010000 for version 1.0.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- //2.USHORT numTables Number of tables. USHORT 16-bit unsigned integer.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- inumTables=calculate_findint16(data);
- //3.USHORT searchRange (Maximum power of 2 £ numTables) x 16.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- isearchRange=calculate_findint16(data);
- //4.USHORT entrySelector Log2(maximum power of 2 £ numTables).
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- ientrySelector=calculate_findint16(data);
- //5.USHORT rangeShift NumTables x 16-searchRange.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- irangeShift=calculate_findint16(data);
- }
- unsigned long lcheckSum;
- unsigned long loffset;
- unsigned long llength;
- for(unsigned int index=0;index<inumTables;index++)
- {
- BOOL bName=FALSE;
- __int32 data;
- //ULONG tag 4 -byte identifier. ULONG 32-bit unsigned integer.
- if(file.Read(&data,4)!=4){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(calculate_findname(data))
- {
- bName=TRUE;
- }
- //ULONG checkSum CheckSum for this table.
- if(file.Read(&data,4)!=4){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(bName)
- {
- lcheckSum=calculate_findint32(data);
- }
- //ULONG offset Offset from beginning of TrueType font file.
- if(file.Read(&data,4)!=4){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(bName)
- {
- loffset=calculate_findint32(data);
- }
- //ULONG length Length of this table.
- if(file.Read(&data,4)!=4){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(bName)
- {
- llength=calculate_findint32(data);
- break;
- }
- if((index+1)==inumTables)//最后一个数据.
- {
- {try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- }
- }
- try
- {
- file.Seek(loffset,CFile::begin);
- }
- catch(CFileException e)
- {
- {try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- }
- unsigned int istrStoOffset;
- CArray<unsigned int,unsigned int>istrLength;
- CArray<unsigned int,unsigned int>istrOffset;
- {
- __int16 data;
- unsigned int inumNameRecord;
- //USHORT Format selector (=0).
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- //USHORT Number of NameRecords that follow n.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- inumNameRecord=calculate_findint16(data);
- //USHORT Offset to start of string storage (from start of table).
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- istrStoOffset=calculate_findint16(data);
- //n NameRecords The NameRecords.
- for(unsigned index=0;index<inumNameRecord;index++)
- {
- BOOL bName=FALSE;
- //USHORT Platform ID.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- {
- unsigned int iPlatID=calculate_findint16(data);
- if(iPlatID!=3)//3.表示microsoft操作系统
- {
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- continue;
- }
- }
- //USHORT Platform-specific encoding ID.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- //USHORT Language ID.
- BOOL bEng=TRUE;
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- {
- unsigned int iLangID=calculate_findint16(data);
- //对于不是英语表示的信息,涉及到内码转换的问题,均不予处理.
- if((iLangID!=0x409)&&(iLangID!=0x809)&&(iLangID!=0xC09)&&(iLangID!=0x1009)&&(iLangID!=0x1409)&&(iLangID!=0x1809))//0x409表示English (6): American (0409; ENU) 1252 437
- {
- bEng=FALSE;
- }
- }
- //USHORT Name ID.
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(calculate_findint16(data)==((unsigned int)1))
- {
- //1 Font Family name
- bName=TRUE;
- }
- //USHORT String length (in bytes).
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(bName)
- {
- if(bEng)
- {
- istrLength.Add(calculate_findint16(data));
- }
- else
- {
- istrLength.InsertAt(0,calculate_findint16(data));
- }
- }
-
- //USHORT String offset from start of storage area (in bytes).
- if(file.Read(&data,2)!=2){try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- if(bName)
- {
- if(bEng)
- {
- istrOffset.Add(calculate_findint16(data));
- }
- else
- {
- istrOffset.InsertAt(0,calculate_findint16(data));
- }
- }
- }
- }
- PBYTE pSourceData = NULL;
- {
- for(int index=0;index<istrLength.GetSize();index++)
- {
- try
- {
- file.Seek(loffset+istrStoOffset+istrOffset[index],CFile::begin);
- }
- catch(CFileException e)
- {
- {try{file.Close();::DeleteFile(strfontfile);}catch(...){};return FALSE;}
- }
- if(istrLength[index]%2)//如果不是偶数就退出
- {
- continue;
- }
- {
- pSourceData=(PBYTE)(LPVOID)GlobalAlloc(GPTR,istrLength[index]+2);
- pSourceData[istrLength[index]]=0;
- pSourceData[istrLength[index]+1]=0;
- __int8 data1;
- __int8 data2;
- CString strFontName;
- for(unsigned int index2=0;index2<istrLength[index];index2+=2)//index2和istrLength[index]是偶数
- {
- if(file.Read(&data1,1)!=1){try{file.Close();::DeleteFile(strfontfile);}catch(...){};GlobalFree(GlobalHandle(pSourceData));return FALSE;}
- if(file.Read(&data2,1)!=1){try{file.Close();::DeleteFile(strfontfile);}catch(...){};GlobalFree(GlobalHandle(pSourceData));return FALSE;}
- pSourceData[index2]=data2;
- pSourceData[index2+1]=data1;
- }
- strfontname=(LPCWSTR)pSourceData;
- for(int index3=0;index3<strfontname.GetLength();index3++)
- {
- strfontname.SetAt(index3,' ');
- }
- strfontname=(LPCWSTR)pSourceData;
- if(strfontname.GetLength()>0)
- {
- GlobalFree(GlobalHandle(pSourceData));
- try{file.Close();::DeleteFile(strfontfile);}catch(...){}
- return TRUE;
- }
- GlobalFree(GlobalHandle(pSourceData));
- pSourceData=NULL;
- }
- }
- }
- try{file.Close();::DeleteFile(strfontfile);}catch(...){}
- return FALSE;
- }
- unsigned int CReader::calculate_findint16(__int16 data)
- {
- __int16 data2;
- __int8* byte=(__int8*)&data;
- __int8* byte2=(__int8*)&data2;
- (*byte2)=(*(byte+1));
- (*(byte2+1))=(*byte);
- return data2;
- }
- unsigned long CReader::calculate_findint32(__int32 data)
- {
- __int32 data2;
- __int8* byte=(__int8*)&data;
- __int8* byte2=(__int8*)&data2;
- (*byte2)=(*(byte+3));
- (*(byte2+1))=(*(byte+2));
- (*(byte2+2))=(*(byte+1));
- (*(byte2+3))=(*byte);
- return data2;
- }
- BOOL CReader::calculate_findname(__int32 data)
- {
- __int8* byte=(__int8*)&data;
- if((*byte)==0x6E)//'n'
- if((*(byte+1))==0x61)//'a'
- if((*(byte+2))==0x6D)//'m'
- if((*(byte+3))==0x65)//'e'
- return TRUE;
- return FALSE;
- }
- void CReader::CreateShape(AD_DB_HANDLE dwghandle,AD_OBJHANDLE shapefileobjhandle,char *str,int len, double* px, double* py, double dAng, int bound_high,
- double text_h, double text_w, KmBuffer* pBuffer)
- {
- BOOL bDown;
- int i,byte_num1,byte_num2,byte_num3,byte_num4,nPosStackDepth;
- int nVar1,nVar2,nVar3,nVar4,nVar5;
- double x0,y0,x1,y1,xStack[4],yStack[4];
- double endX,endY,cenX,cenY;
- double PointX1,PointY1,PointX2,PointY2,x,y,r,xCen,yCen;
- double dAmplify=1;
- BYTE uScale;
- x0=y0=0;
- x=*px;
- y=*py;
- nPosStackDepth=0;//位置栈的深度
- for(i=0; i<len; i++)
- {
- byte_num1 = (int)str[i];
- switch(byte_num1)
- {
- case 1://落笔
- {
- bDown=TRUE;
- while(bDown && (++i)<len)
- {
- byte_num2 = (int)str[i];
- byte_num3 = byte_num2&0xf;
- byte_num4 = byte_num2&0xf0;
- if(byte_num4==0)
- {//非矢量表达,该字符是控制符
- switch(byte_num2)
- {
- case 0:
- return;
- case 1:
- continue;
- case 2:
- {
- i--;
- bDown=FALSE;
- break;
- }
- case 3://控制文字矢量长度,除以下一字节
- {
- uScale=str[++i];
- //text_h/=x2;
- dAmplify/=uScale;
- break;
- }
- case 4://控制文字矢量长度,乘以下一字节
- {
- uScale=str[++i];
- //text_h*=x2;
- dAmplify*=uScale;
- break;
- }
- case 5://将当前位置压入位置栈
- {
- xStack[nPosStackDepth]=x0;
- yStack[nPosStackDepth]=y0;
- nPosStackDepth++;
- ASSERT(nPosStackDepth<=4);//位置栈的深度最多只能为4
- break;
- }
- case 6://从位置栈中弹出当前位置
- {
- nPosStackDepth--;
- ASSERT(nPosStackDepth>=0);
- x0=xStack[nPosStackDepth];
- y0=yStack[nPosStackDepth];
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX2,PointY2);
- *px=x+PointX2;
- *py=y+PointY2;
- break;
- }
- case 7://子形
- {
- uScale=str[++i];
- calculate_findshape(dwghandle,shapefileobjhandle,uScale,px,py,dAng,bound_high,text_h,text_w,pBuffer);
- TRACE("子形");
- break;
- }
- case 8://单个坐标点
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
-
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- //if(DRAWPRESENT!=DRAWTOCALC)
- DrawLine(x+PointX1,y+PointY1,x+PointX2,y+PointY2,pBuffer);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- break;
- }
- case 9://连续坐标点
- for(;i<len;)
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- if(x1==0 && y1==0)
- break;
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- //if(DRAWPRESENT!=DRAWTOCALC)
- DrawLine(x+PointX1,y+PointY1,x+PointX2,y+PointY2,pBuffer);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
-
- case 10://Octant弧
- {
- x1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- OctantArc(x0,y0,x1,nVar1,endX,endY,cenX,cenY);
- r=sqrt((endX-cenX)*(endX-cenX)+(endY-cenY)*(endY-cenY));
- //if(DRAWPRESENT!=DRAWTOCALC)
- {
- DrawEllipse(x,y,cenX*text_h*text_w/bound_high,
- cenY*text_h/bound_high,
- r*text_h*text_w/bound_high,
- r*text_h/bound_high,
- dAng,
- x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,
- endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,
- nVar1>0 ? 1 : 0,
- pBuffer);
- }
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate(endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 = endX;
- y0 = endY;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 11://Fractional弧
- {
- nVar1 = (int)str[++i];
- nVar2 = (int)str[++i];
- nVar3 = (int)str[++i];
- nVar4 = (int)str[++i];
- nVar5 = (int)str[++i];
- FractionalArc(x0,y0,nVar1,nVar2,nVar3,nVar4,nVar5,dAmplify,endX,endY,cenX,cenY);
- r=sqrt((endX-cenX)*(endX-cenX)+(endY-cenY)*(endY-cenY));
- //if(DRAWPRESENT!=DRAWTOCALC)
- {
- DrawEllipse(x,y,cenX*text_h*text_w/bound_high,
- cenY*text_h/bound_high,
- r*text_h*text_w/bound_high,
- r*text_h/bound_high,
- dAng,
- x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,
- endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,
- nVar5>0 ? 1 : 0,
- pBuffer);
- }
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate(endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,dAng,
- PointX2,PointY2);
- TransRotate(cenX*text_h*text_w/bound_high,
- cenY*text_h/bound_high,dAng,
- xCen,yCen);
- x0 = endX;
- y0 = endY;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 12://Bulge弧
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- BulgeArc(x0,y0,x0+x1,y0+y1,nVar1,r,xCen,yCen);
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- if(fabs(r)<PRECISION)//直接画线
- {
- DrawLine(x+PointX1,y+PointY1,x+PointX2,y+PointY2,pBuffer);
- }
- else//画椭圆
- {
- DrawEllipse(x,y,xCen*text_h*text_w/bound_high,
- yCen*text_h/bound_high,
- r*text_h*text_w/bound_high,
- r*text_h/bound_high,
- dAng,
- x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,
- (x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,
- nVar1>0 ? 1 : 0 ,
- pBuffer);
- }
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 13://Bulge弧
- for(;i<len;)
- {
- if((int)str[i+1]==0 && (int)str[i+2]==0)
- break;
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- BulgeArc(x0,y0,x0+x1,y0+y1,nVar1,r,xCen,yCen);
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- if(fabs(r)<PRECISION)//直接画线
- {
- DrawLine(x+PointX1,y+PointY1,x+PointX2,y+PointY2,pBuffer);
- }
- else//画椭圆
- {
- DrawEllipse(x,y,xCen*text_h*text_w/bound_high,
- yCen*text_h/bound_high,
- r*text_h*text_w/bound_high,
- r*text_h/bound_high,
- dAng,
- x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,
- (x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,
- nVar1>0 ? 1 : 0 ,
- pBuffer);
- }
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 14://暂不处理铅垂排列的字
- {
- //后移一位
- byte_num2 = (int)str[++i];
- if(byte_num2==8) i+=2;//如果是点坐标,再后移两位
- else if(byte_num2&0xf0) i++;//如果是及坐标仅后移一位
- break;
- }
- }//switch(byte_num2)
- }//if(byte_num4==0)
- else
- {
- //矢量表达的点
- TranXY(byte_num3,byte_num4/16*dAmplify,x1,y1);
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- //if(DRAWPRESENT!=DRAWTOCALC)
- DrawLine(x+PointX1,y+PointY1,x+PointX2,y+PointY2,pBuffer);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- }//while(bDown && (++i)<len)
- break;
- }//case 1
-
- case 2://抬笔
- {
- bDown=FALSE;
- while(!bDown && (++i)<len )
- {
- byte_num2 = (int)str[i];
- switch(byte_num2)
- {
- case 0:
- return;
- case 1:
- {
- i--;
- bDown=TRUE;
- break;
- }
- case 2:
- {
- continue;
- }
-
-
- case 3://控制文字矢量长度,除以下一字节
- {
- uScale=str[++i];
- //text_h/=x2;
- dAmplify/=uScale;
- break;
- }
- case 4://控制文字矢量长度,乘以下一字节
- {
- uScale=str[++i];
- //text_h*=x2;
- dAmplify*=uScale;
- break;
- }
- case 5://将当前位置压入位置栈
- {
- xStack[nPosStackDepth]=x0;
- yStack[nPosStackDepth]=y0;
- nPosStackDepth++;
- ASSERT(nPosStackDepth<=4);//位置栈的深度最多只能为4
- break;
- }
- case 6://从位置栈中弹出当前位置
- {
- nPosStackDepth--;
- ASSERT(nPosStackDepth>=0);
- x0=xStack[nPosStackDepth];
- y0=yStack[nPosStackDepth];
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX2,PointY2);
- *px=x+PointX2;
- *py=y+PointY2;
- break;
- }
- case 7://子形
- {
- uScale=str[++i];
- calculate_findshape(dwghandle,shapefileobjhandle,uScale,px,py,dAng,bound_high,text_h,text_w,pBuffer);
- TRACE("子形");
- break;
- }
- case 8://位置移动
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- break;
- }
- case 9://连续坐标点
- for(;i<len;)
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- if(x1==0 && y1==0)
- break;
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 10://Octant弧
- {
- x1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- OctantArc(x0,y0,x1,nVar1,endX,endY,cenX,cenY);
- r=sqrt((endX-cenX)*(endX-cenX)+(endY-cenY)*(endY-cenY));
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate(endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 = endX;
- y0 = endY;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 11://Fractional弧
- {
- nVar1 = (int)str[++i];
- nVar2 = (int)str[++i];
- nVar3 = (int)str[++i];
- nVar4 = (int)str[++i];
- nVar5 = (int)str[++i];
- FractionalArc(x0,y0,nVar1,nVar2,nVar3,nVar4,nVar5,dAmplify,endX,endY,cenX,cenY);
- r=sqrt((endX-cenX)*(endX-cenX)+(endY-cenY)*(endY-cenY));
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate(endX*text_h*text_w/bound_high,
- endY*text_h/bound_high,dAng,
- PointX2,PointY2);
- TransRotate(cenX*text_h*text_w/bound_high,
- cenY*text_h/bound_high,dAng,
- xCen,yCen);
- x0 = endX;
- y0 = endY;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 12://Bulge弧
- {
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- BulgeArc(x0,y0,x0+x1,y0+y1,nVar1,r,xCen,yCen);
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 13://Bulge弧
- for(;i<len;)
- {
- if((int)str[i+1]==0 && (int)str[i+2]==0)
- break;
- x1 = ((int)str[++i])*dAmplify;
- y1 = ((int)str[++i])*dAmplify;
- nVar1 = (int)str[++i];
- BulgeArc(x0,y0,x0+x1,y0+y1,nVar1,r,xCen,yCen);
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX1,PointY1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- break;
- case 14://暂不处理铅垂排列的字
- {
- //后移一位
- byte_num2 = (int)str[++i];
- if(byte_num2==8) i+=2;//如果是点坐标,再后移两位
- break;
- }
-
- default://极坐标的位置移动
- {
- byte_num2 = (int)str[i];
- byte_num3 = byte_num2&0xf;
- byte_num4 = byte_num2&0xf0;
- if(byte_num4)
- {
- //极坐标转换为坐标点
- TranXY(byte_num3,byte_num4/16*dAmplify,x1,y1);
- TransRotate((x0+x1)*text_h*text_w/bound_high,
- (y0+y1)*text_h/bound_high,dAng,
- PointX2,PointY2);
- x0 += x1;
- y0 += y1;
- *px=x+PointX2;
- *py=y+PointY2;
- }
- else
- {
- ASSERT(FALSE);
- }
- break;
-
- }
- }//switch(byte_num2)
- }//while(!bDown)
- break;
- }//case 2
- case 3://控制文字矢量长度,除以下一字节
- {
- uScale=str[++i];
- //text_h/=x2;
- dAmplify/=uScale;
- break;
- }
- case 4://控制文字矢量长度,乘以下一字节
- {
- uScale=str[++i];
- //text_h*=x2;
- dAmplify*=uScale;
- break;
- }
- case 5://将当前位置压入位置栈
- {
- xStack[nPosStackDepth]=x0;
- yStack[nPosStackDepth]=y0;
- nPosStackDepth++;
- ASSERT(nPosStackDepth<=4);//位置栈的深度最多只能为4
- break;
- }
- case 6://从位置栈中弹出当前位置
- {
- nPosStackDepth--;
- ASSERT(nPosStackDepth>=0);
- x0=xStack[nPosStackDepth];
- y0=yStack[nPosStackDepth];
- TransRotate(x0*text_h*text_w/bound_high,
- y0*text_h/bound_high,dAng,
- PointX2,PointY2);
- *px=x+PointX2;
- *py=y+PointY2;
- break;
- }
- case 7://子形
- {
- uScale=str[++i];
- calculate_findshape(dwghandle,shapefileobjhandle,uScale,px,py,dAng,bound_high,text_h,text_w,pBuffer);
- TRACE("子形");
- break;
- }
- }//switch(byte_num1)
- }//for
- }
- int CReader::calculate_findstringlength(CString& strEnter)
- {
- int imax=0;
- int icurmax=0;
- for(int i=0;i<strEnter.GetLength();i++)
- {
- if(strEnter[i]=='\n')
- {
- if(icurmax>imax)
- {
- imax=icurmax;
- }
- icurmax=0;
- }
- else
- {
- if(strEnter[i]=='\')
- {
- if(i+1<strEnter.GetLength())
- {
- if(strEnter[i+1]=='\')
- {
- icurmax++;
- i++;
- continue;
- }
- else if(strEnter[i+1]=='U')
- {
- icurmax+=2;
- i+=6;
- continue;
- }
- else if(strEnter[i+1]=='u')
- {
- icurmax+=2;
- i+=6;
- continue;
- }
- else if(strEnter[i+1]=='M')
- {
- icurmax+=2;
- i+=7;
- continue;
- }
- else if(strEnter[i+1]=='m')
- {
- icurmax+=2;
- i+=7;
- continue;
- }
- }
- {
- for(int j=i+1;j<strEnter.GetLength();j++)
- {
- if(strEnter[j]==';')
- {
- i=j;
- break;
- }
- }
- }
- }
- else
- {
- icurmax++;
- }
- }
- }
- if(icurmax>imax)
- {
- imax=icurmax;
- }
- return imax;
- }
- void CReader::BulgeArc(double x0, double y0, double x1, double y1,
- int hArc, double& r ,double& xCen, double& yCen)
- {
- double x,y,w;
- double dx=x1-x0;
- double dy=y1-y0;
- double dis=sqrt(dx*dx+dy*dy);
- if(fabs(dis)<PRECISION)
- {
- TRACE("在拱弧计算中两点距离为0n");
- r=0;
- return;
- }
- dx/=dis;
- dy/=dis;
- //计算两线中点
- x=x0+dx*dis/2;
- y=y0+dy*dis/2;
-
- //计算半径
- w=fabs(hArc)*dis/254;
- if(fabs(w)<PRECISION)
- {
- //是直线
- TRACE("拱弧拱高为0n");
- r=0;
- return;
- }
- //勾股弦定理计算
- dis/=2;
- r=(dis*dis)/(2*w)+w/2;
- if(fabs(r)<PRECISION)
- return;
- //求圆心
- if(hArc>0)
- {
- xCen=x-dy*(r-w);
- yCen=y+dx*(r-w);
- }
- else
- {
- xCen=x+dy*(r-w);
- yCen=y-dx*(r-w);
- }
- /* //求起始角和终止角
- if(w>0)//逆弧
- {
- ASSERT(fabs((x0-cenX)/r)<=1);
- dBAng=acos((x0-cenX)/r);
- if((y0-cenY)<0)
- dBAng=2*PI-dBAng;
- ASSERT(fabs((x1-cenX)/r)<=1);
- dEAng=acos((x1-cenX)/r);
- if((y1-cenY)<0)
- dEAng=2*PI-dEAng;
- }
- else//顺弧
- {
- ASSERT(fabs((x1-cenX)/r)<=1);
- dBAng=acos((x1-cenX)/r);
- if((y1-cenY)<0)
- dBAng=2*PI-dBAng;
- ASSERT(fabs((x0-cenX)/r)<=1);
- dEAng=acos((x0-cenX)/r);
- if((y0-cenY)<0)
- dEAng=360-dEAng;
- }*/
- }
- void CReader::OctantArc(double begX,double begY,double Rad,int OSC,
- double& endX,double& endY,double& cenX,double& cenY)
- {
- int osc;
- int i;
- int begin_ang,end_ang;
- double tempX,tempY;
- if(OSC<0)
- osc = 256 + OSC;
- else
- osc = OSC;
-
- i = osc>>4;
- i = i&15;
- if(i>7)
- i = i - 8;
- switch(i)
- {//根据第二位判断开始角度.角度知道后可求出坐标系中心(圆心)
- case 0:
- begin_ang = 0;
- cenX = begX - Rad;
- cenY = begY;
- break;
- case 1:
- begin_ang = 45;
- cenX = begX - Rad;
- cenY = begY - Rad;
- break;
- case 2:
- begin_ang = 90;
- cenX = begX;
- cenY = begY - Rad;
- break;
- case 3:
- begin_ang = 135;
- cenX = begX + Rad;
- cenY = begY - Rad;
- break;
- case 4:
- begin_ang = 180;
- cenX = begX + Rad;
- cenY = begY;
- break;
- case 5:
- begin_ang = 225;
- cenX = begX + Rad;
- cenY = begY + Rad;
- break;
- case 6:
- begin_ang = 270;
- cenX = begX;
- cenY = begY + Rad;
- break;
- case 7:
- begin_ang = 315;
- cenX = begX - Rad;
- cenY = begY + Rad;
- break;
- }
- i = osc&15;
- if(i==0)
- end_ang = 360;
- else
- end_ang = i*45;
- if(OSC<0)//顺时针
- TransRotate((double)(begX - cenX),(double)(begY - cenY),
- (float)(-end_ang*PI/180),tempX,tempY);
- else//逆时针
- TransRotate((double)(begX - cenX),(double)(begY - cenY),
- (float)(end_ang*PI/180),tempX,tempY);
- endX = cenX + tempX;//+0.499);
- endY = cenY + tempY;//+0.49999);
- }
- void CReader::FractionalArc(double begX,double begY, int s_off,int e_off,
- int h_rad,int l_rad,int OSC,double dAmplify,
- double& endX,double& endY,double& cenX,double& cenY)
- {
- double Rad;
- int osc;
- int i;
- int begin_ang,end_ang;
- double tempX,tempY;
- Rad = (h_rad*255 + l_rad)*dAmplify;
- if(OSC<0) osc = 256 + OSC;
- else osc = OSC;
- i = osc>>4;
- i = i&15;
- if(i>7) i = i - 8;
- switch(i)
- {//根据第二位判断开始角度.角度知道后可求出坐标系中心(圆心)
- case 0:
- begin_ang = (45*s_off/256)+0;
- cenX = begX - Rad;
- cenY = begY;
- break;
- case 1:
- begin_ang = (45*s_off/256)+45;
- cenX = begX - Rad;
- cenY = begY - Rad;
- break;
- case 2:
- begin_ang = (45*s_off/256)+90;
- cenX = begX;
- cenY = begY - Rad;
- break;
- case 3:
- begin_ang = (45*s_off/256)+135;
- cenX = begX + Rad;
- cenY = begY - Rad;
- break;
- case 4:
- begin_ang = (45*s_off/256)+180;
- cenX = begX + Rad;
- cenY = begY;
- break;
- case 5:
- begin_ang = (45*s_off/256)+225;
- cenX = begX + Rad;
- cenY = begY + Rad;
- break;
- case 6:
- begin_ang = (45*s_off/256)+270;
- cenX = begX;
- cenY = begY + Rad;
- break;
- case 7:
- begin_ang = (45*s_off/256)+315;
- cenX = begX - Rad;
- cenY = begY + Rad;
- break;
- }
- i = osc&15;
- switch(i)
- {//根据第二位判断开始角度.角度知道后可求出坐标系中心(圆心)
- case 0:
- end_ang = (45*e_off/256)+0;
- cenX = begX - Rad;
- cenY = begY;
- break;
- case 1:
- end_ang = (45*e_off/256)+45;
- cenX = begX - Rad;
- cenY = begY - Rad;
- break;
- case 2:
- end_ang = (45*e_off/256)+90;
- cenX = begX;
- cenY = begY - Rad;
- break;
- case 3:
- end_ang = (45*e_off/256)+135;
- cenX = begX + Rad;
- cenY = begY - Rad;
- break;
- case 4:
- end_ang = (45*e_off/256)+180;
- cenX = begX + Rad;
- cenY = begY;
- break;
- case 5:
- end_ang = (45*e_off/256)+225;
- cenX = begX + Rad;
- cenY = begY + Rad;
- break;
- case 6:
- end_ang = (45*e_off/256)+270;
- cenX = begX;
- cenY = begY + Rad;
- break;
- case 7:
- end_ang = (45*e_off/256)+315;
- cenX = begX - Rad;
- cenY = begY + Rad;
- break;
- }
-
- end_ang = end_ang - begin_ang;
- if(OSC<0)//顺时针
- TransRotate((double)(begX - cenX),(double)(begY - cenY),
- (double)(-end_ang*PI/180),tempX,tempY);
- else//逆时针
- TransRotate((double)(begX - cenX),(double)(begY - cenY),
- (double)(end_ang*PI/180),tempX,tempY);
- endX = cenX + tempX;//+0.499);
- endY = cenY + tempY;//+0.49999);
- }
- void CReader::TranXY(int direct,double len,double& x1,double& y1)
- {
- switch(direct)
- {
- case 0:
- x1 = len; y1 = 0;
- break;
- case 1:
- x1 = len; y1 = len/2;
- break;
- case 2:
- x1 = len; y1 = len;
- break;
- case 3:
- x1 = len/2; y1 = len;
- break;
- case 4:
- x1 = 0; y1 = len;
- break;
- case 5:
- x1 = -len/2; y1 = len;
- break;
- case 6:
- x1 = -len; y1 = len;
- break;
- case 7:
- x1 = -len; y1 = len/2;
- break;
- case 8:
- x1 = -len; y1 = 0;
- break;
- case 9:
- x1 = -len; y1 = -len/2;
- break;
- case 10:
- x1 = -len; y1 = -len;
- break;
- case 11:
- x1 = -len/2; y1 = -len;
- break;
- case 12:
- x1 = 0; y1 = -len;
- break;
- case 13:
- x1 = len/2; y1 = -len;
- break;
- case 14:
- x1 = len; y1 = -len;
- break;
- case 15:
- x1 = len; y1 = -len/2;
- break;
- }
- }
- void CReader::DrawLine(double x1,double y1,double x2, double y2, KmBuffer* pBuffer)
- {
- if(pBuffer!=NULL)
- {
- S_LINE_BUFFER* pLineBuf= new S_LINE_BUFFER;
- pLineBuf->Color=RGB(0,0,0);
- pLineBuf->LineScale=1;
- pLineBuf->LineType=SOLID_LINE;
- pLineBuf->Mode=DRAWPRESENT;
- pLineBuf->P1.x=x1;
- pLineBuf->P1.y=y1;
- pLineBuf->P2.x=x2;
- pLineBuf->P2.y=y2;
- pLineBuf->Width=1;
- pBuffer->AddLine(pLineBuf);
- }
- }
- void CReader::DrawArc(double x1, double y1, double x2, double y2,
- double xCen, double yCen, int nWise, KmBuffer* pBuffer)
- {
- double r,dBAng,dEAng,dDivAng;
-
- r=sqrt((x1-xCen)*(x1-xCen)+(y1-yCen)*(y1-yCen));
- if(r<PRECISION) r=PRECISION;
- if(nWise>0)//逆弧
- {
- ASSERT(fabs((x1-xCen)/r)<=1);
- dBAng=acos((x1-xCen)/r);
- if((y1-yCen)<0)
- dBAng=2*PI-dBAng;
- ASSERT(fabs((x2-xCen)/r)<=1);
- dEAng=acos((x2-xCen)/r);
- if((y2-yCen)<0)
- dEAng=2*PI-dEAng;
- }
- else
- {
- ASSERT(fabs((x1-xCen)/r)<=1);
- dEAng=acos((x1-xCen)/r);
- if((y1-yCen)<0)
- dEAng=2*PI-dEAng;
- ASSERT(fabs((x2-xCen)/r)<=1);
- dBAng=acos((x2-xCen)/r);
- if((y2-yCen)<0)
- dBAng=2*PI-dBAng;
- }
- if(r < PRECISION_SHOW)
- {
- r = PRECISION_SHOW;
- }
- dDivAng = sqrt(0.50 / r);
- if (dDivAng > 1)
- dDivAng = 1;
- dDivAng = fabs(2 * asin(dDivAng));
- if(fabs(dDivAng) < PRECISION_SHOW)
- {
- dDivAng = PRECISION_SHOW;
- }
- if(dDivAng>PI/16)
- dDivAng=PI/16;
-
- //将圆分段,用直线绘制
- if(dEAng<dBAng)
- dEAng+=2*PI;
-
- //if(m_pShow->show_scale>1)
- // a/=m_pShow->show_scale;
- //画线
- if(pBuffer)
- {
- x1=xCen+r*cos(dBAng);
- y1=yCen+r*sin(dBAng);
- while(dBAng<dEAng-PRECISION_SHOW_ANG)
- {
- dBAng+=dDivAng;
- if(dBAng>dEAng-PRECISION_SHOW_ANG)
- dBAng=dEAng;
- x2=xCen+r*cos(dBAng);
- y2=yCen+r*sin(dBAng);
- {
- S_LINE_BUFFER* pLineBuf= new S_LINE_BUFFER;
- pLineBuf->Color=RGB(0,0,0);
- pLineBuf->LineScale=1;
- pLineBuf->LineType=SOLID_LINE;
- pLineBuf->Mode=DRAWPRESENT;
- pLineBuf->P1.x=x1;
- pLineBuf->P1.y=y1;
- pLineBuf->P2.x=x2;
- pLineBuf->P2.y=y2;
- pLineBuf->Width=1;
- pBuffer->AddLine(pLineBuf);
- x1=x2;
- y1=y2;
- }
- };
- }
- }
- //画椭圆
- //dBaseX,dBaseY为基点坐标
- //xCen,yCen为圆心坐标
- //xr,yr为两方向轴长
- //dAng为椭圆x轴与坐标x轴的夹角
- //bx,by为椭圆弧的起点
- //ex,ey为椭圆弧的终点
- //bCounterClockWise为椭圆弧的旋转方向是否为逆时针方向
- //pBuffer为KmBuffer缓存
- void CReader::DrawEllipse(double dBaseX, double dBaseY, double xCen, double yCen,
- double xr, double yr, double dAng, double bx, double by,
- double ex, double ey, BOOL bCounterClockWise, KmBuffer* pBuffer)
- {
- //若近似为圆则画圆弧
- if(fabs(xr-yr)<PRECISION)
- {
- TransRotate(xCen,yCen,dAng,xCen,yCen);
- TransRotate(bx,by,dAng,bx,by);
- TransRotate(ex,ey,dAng,ex,ey);
- DrawArc(dBaseX+bx,dBaseY+by,dBaseX+ex,dBaseY+ey,
- dBaseX+xCen,dBaseY+yCen, bCounterClockWise ? 1: -1,pBuffer);
- return;
- }
- //画椭圆弧
- //1. 先计算起始角和终止角
- double dBAng,dEAng,dDivAng,x1,y1,x2,y2,xend,yend;
-
- dBAng=atan2((by-yCen)/yr,(bx-xCen)/xr);
- dEAng=atan2((ey-yCen)/yr,(ex-xCen)/xr);
- if(dBAng<0)
- dBAng+=2*PI;
- if(dEAng<0)
- dEAng+=2*PI;
- //2. 再计算弧度步长
- if(xr<0 || yr<0)
- return;
- dDivAng = sqrt(1 / (xr+yr));
- if (dDivAng > 1)
- dDivAng = 1;
- dDivAng = fabs(2 * asin(dDivAng));
- if(fabs(dDivAng) < PRECISION_SHOW)
- {
- dDivAng = PRECISION_SHOW;
- }
- if(dDivAng>PI/4)
- dDivAng=PI/4;
- //3.计算每步画线的两点坐标,旋转dAng并加基点坐标偏移后画线
- if(!bCounterClockWise)//统一为逆弧
- {
- double dAng=dBAng;
- dBAng=dEAng;
- dEAng=dAng;
- xend=bx;
- yend=by;
- bx=ex;
- by=ey;
- }
- else
- {
- xend=ex;
- yend=ey;
- }
- if(dEAng<dBAng)
- dEAng+=2*PI;
-
- TransRotate(bx,by,dAng,x1,y1);
- x1+=dBaseX;
- y1+=dBaseY;
- if(pBuffer)
- {
- while(dBAng<dEAng-PRECISION_SHOW_ANG)
- {
- dBAng+=dDivAng;
- if(dBAng>dEAng-PRECISION_SHOW_ANG)
- {
- //dBAng=dEAng;
- ex=xend;
- ey=yend;
- }
- else
- {
- ex=xCen+xr*cos(dBAng);
- ey=yCen+yr*sin(dBAng);
- }
- TransRotate(ex,ey,dAng,x2,y2);
- x2+=dBaseX;
- y2+=dBaseY;
-
- {
- S_LINE_BUFFER* pLineBuf= new S_LINE_BUFFER;
- pLineBuf->Color=RGB(0,0,0);
- pLineBuf->LineScale=1;
- pLineBuf->LineType=SOLID_LINE;
- pLineBuf->Mode=DRAWPRESENT;
- pLineBuf->P1.x=x1;
- pLineBuf->P1.y=y1;
- pLineBuf->P2.x=x2;
- pLineBuf->P2.y=y2;
- pLineBuf->Width=1;
- pBuffer->AddLine(pLineBuf);
- x1=x2;
- y1=y2;
- }
-
- };
- }
- }
- void CReader::TransRotate(double x, double y, double dAng, double& x1, double& y1)
- {
- if(fabs(dAng)<PRECISION)
- {
- x1=x;
- y1=y;
- return;
- }
- double dRotAng=atan2(y,x);
- if(dRotAng<0)
- dRotAng+=2*PI;
- x1 = sqrt(x*x+y*y)*cos(dRotAng+dAng);
- y1 = sqrt(x*x+y*y)*sin(dRotAng+dAng);
- }