Files
envi-code/SourceCode/Code2026/tg_shs/shs_kernal/InitTextStyle.cpp
T
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

509 lines
15 KiB
C++

#include "stdafx.h"
// 文字样式管理数据结构,sg modify 111231
struct TTextStyleInfo {
TTextStyle ts; // 样式名称,TS_
const TCHAR *szKey; // 类型关键字,sg add 101229
const TCHAR *szName; // 样式名称
const TCHAR *szWindows; // 是否是Windows字体,sg add 110110
const TCHAR *szChFont; // 中文字库
const TCHAR *szEnFont; // 英文字库
double xScale; // 中文宽高比
double nEnChScaleX; // 宽度比例
double nEnChScaleY; // 高度比例
double nHeight; // 字高
int nColor; // 颜色
};
// sg modify 111231,根据TTextStyleInfo结构的调整,修改了数组结构
//TTextStyleInfo g_tchTextStyles[]= {
// {TS_STANDARD, _T("STANDARD"), _T("GBCBIG"), _T("GBENOR"), 0.0, 1.0, 0.6, 1.0, 0},
// {TS_AXIS, _T("_TCH_AXIS"), _T("GBCBIG"), _T("COMPLEX"), 0.0, 1.0, 0.6, 0.85, 0},
// {TS_WINDOW, _T("_TCH_WINDOW"), _T("GBCBIG"), _T("SIMPLEX"), 0.0, 1.0, 0.6, 0.85, 0},
// {TS_LABEL, _T("_TCH_LABEL"), _T("GBCBIG"), _T("COMPLEX"), 0.0, 1.0, 0.6, 0.85, 0},
// {TS_DIM, _T("_TCH_DIM"), _T("GBCBIG"), _T("SIMPLEX"), 0.0, 1.0, 0.6, 0.85, 7},
//{TS_DIMFONT, _T("DIM_FONT"), _T(""), _T("SIMPLEX"), 0.0, 0.8, 1.0, 1.0, 0},
//{TS_DIMFONT, _T("DIM_FONT"), _T("HZTXT"), _T("SIMPLEX"), 0.0, 0.8, 1.0, 1.0, 0},//refreshed by whl on 040910 解决另存旧版后汉字无法显示
// {TS_DIMFONT, _T("DIM_FONT"), _T("GBCBIG"), _T("SIMPLEX"), 0.0, 0.7, 0.6, 0.85, 7},//refreshed by whl on 051121 粗略解决另存旧版后文字样式变化问题
// {TS_WDTABLE, _T("_TCH_WDTABLE"), _T("GBCBIG"), _T("SIMPLEX"), 0.0, 1.0, 0.6, 0.85, 0},
// {TS_SPACE, _T("_TCH_SPACE"), _T("GBCBIG"), _T("SIMPLEX"), 0.0, 1.0, 0.6, 0.85, 0},
// {TS_SUNANA, _T("_TCH_SUNANA"), _T("HZTXT"), _T("SIMPLEX"), 0.0, 0.8, 0.6, 0.85, 0},
//};
TTextStyleInfo g_tchTextStyles[]= {
{TS_NULL, _T("文字样式标准"), _T("TArch"), _T(""), _T(""), _T(""), 0.0, 0.0, 0.0, 0.0, 0},
{TS_STANDARD, _T("公文"), _T("STANDARD"), _T("否"), _T("GBCBIG"), _T("GBENOR"), 1.0, 0.6, 1.0, 3.5, 7},
{TS_AXIS, _T("轴号"), _T("_TCH_AXIS"), _T("否"), _T("GBCBIG"), _T("COMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_WINDOW, _T("门窗编号"), _T("_TCH_WINDOW"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_WDTABLE, _T("门窗表"), _T("_TCH_WDTABLE"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_SPACE, _T("房间标注"), _T("_TCH_SPACE"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_LABEL, _T("索引圆圈文字"), _T("_TCH_LABEL"), _T("否"), _T("GBCBIG"), _T("COMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
// sg add 120106,根据建筑9.0增加的新文字样式
{TS_INDEXSCALE, _T("索引图名比例"), _T("_TCH_DIM"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_SECTION, _T("剖切符号"), _T("_TCH_LABEL"), _T("否"), _T("GBCBIG"), _T("COMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_DRAWINGNAME, _T("图名标注"), _T("STANDARD"), _T("否"), _T("GBCBIG"), _T("GBENOR"), 1.0, 0.6, 1.0, 3.5, 7},
{TS_NORTHTHUMB, _T("指北针文字"), _T("_TCH_DIM"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_SYMBOL, _T("其他符号"), _T("_TCH_DIM"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_TABLE, _T("表格文字"), _T("_TCH_TAB"), _T("否"), _T("GBCBIG"), _T("COMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
// END
{TS_DIM, _T("尺寸标注"), _T("_TCH_DIM"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 3.5, 7},
{TS_DIMFONT, _T("旧版尺寸标注"), _T("DIM_FONT"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 0.7, 0.6, 0.85, 3.5, 7},
{TS_SUNANA, _T("日照分析"), _T("_TCH_SUNANA"), _T("否"), _T("HZTXT"), _T("SIMPLEX"), 0.8, 0.6, 0.85, 3.5, 7},
{TS_PKNGTSRP, _T("停车位"), _T("_TCH_NUMBER"), _T("否"), _T("GBCBIG"), _T("SIMPLEX"), 1.0, 0.6, 0.85, 10, 7 },
};
//////////////////////////////////////////////////////////////////
// sg add 101229,下列函数用于支持文字管理器
class CTextStyleInfo
{
public:
XString szKey; // 类型关键字
XString szName; // 样式名称
XString szWindows; // 是否是Windows字体,sg add 110110
XString szChFont; // 中文字库
XString szEnFont; // 英文字库
double xScale; // 中文宽高比
double nEnChScaleX; // 宽度比例
double nEnChScaleY; // 高度比例
double nHeight; // 文字高度
int nColor; // 文字颜色
public:
CTextStyleInfo(const TCHAR *csKey, const TCHAR *csName, const TCHAR *csWin, const TCHAR *csChF, const TCHAR *csEnF,
double dScale, double dXScale, double dYScale, double dHeight, int nTextColor)
:szKey(csKey), szName(csName), szWindows(csWin), szChFont(csChF), szEnFont(csEnF),
xScale(dScale), nEnChScaleX(dXScale), nEnChScaleY(dYScale), nHeight(dHeight), nColor(nTextColor) {}
};
class TextStyleList: public DList<CTextStyleInfo>
{
XString m_sFile;
public:
TextStyleList(const TCHAR *pszFile = _T("textstyledef.dat"));
};
TextStyleList::TextStyleList(const TCHAR *pszFile)
{
m_sFile = pszFile;
XString fullFile;
if (FindFile(pszFile, fullFile))
{
FILE *fp = _tfopen(fullFile, _T("r"));
if (!fp)
return;
while (!feof(fp))
{
TCHAR sBuf[256] = _T(""),
cKey[256]=_T(""), cName[256]=_T(""), cWin[256]=_T(""), cChF[256]=_T(""), cEnF[256]=_T(""),
cScale[256]=_T(""), cXScale[256]=_T(""), cYScale[256]=_T(""), cHeight[256]=_T(""), cColor[256]=_T("");
_fgetts(sBuf, 255, fp);
if (sBuf[0])
{
CT_strnth(sBuf, 1, cKey);
CT_strnth(sBuf, 2, cName);
CT_strnth(sBuf, 3, cWin);
CT_strnth(sBuf, 4, cChF);
CT_strnth(sBuf, 5, cEnF);
CT_strnth(sBuf, 6, cScale);
CT_strnth(sBuf, 7, cXScale);
CT_strnth(sBuf, 8, cYScale);
CT_strnth(sBuf, 9, cHeight);
CT_strnth(sBuf, 10, cColor);
}
if (cKey[0] && cName[0] && cChF[0] && cEnF[0])
{
double nScale = _tstof(cScale);
double nXScale = _tstof(cXScale);
double nYScale = _tstof(cYScale);
double dHeight = _tstof(cHeight);
int nColor = _ttoi(cColor);
Append(new CTextStyleInfo(cKey, cName, cWin, cChF, cEnF, nScale, nXScale, nYScale, dHeight, nColor));
}
}
fclose(fp);
}
}
// 文字样式名是否一致
int CTextStyleCompare(const CTextStyleInfo *l1, const void *cKey)
{
CTextStyleInfo *ts1 = (CTextStyleInfo*)l1;
return _tcsicmp(ts1->szKey, (const TCHAR *)cKey);
}
// sg add 120106,需要通过下列函数和方法来获取Windows字体
XStringList lstWinfont;
DList<int> lstCharset, lstPitch;
int CALLBACK EnumFontFamExProc(ENUMLOGFONTEX *lplf, NEWTEXTMETRICEX *lpntme,
int FontType, LONG lParam)
{
if (!(FontType & TRUETYPE_FONTTYPE))
return 1 ;
if (*(lplf->elfLogFont.lfFaceName))
{
if (!_tcsicmp(lplf->elfLogFont.lfFaceName, _T("marlett")))
return 1;
if (lstWinfont.Index(lplf->elfLogFont.lfFaceName, 0) < 0)
{
lstWinfont.Append(tstrnew(lplf->elfLogFont.lfFaceName));
lstCharset.Append(new int(lplf->elfLogFont.lfCharSet));
lstPitch.Append(new int(lplf->elfLogFont.lfPitchAndFamily));
}
}
return 1;
}
// 功能: 初始化文字样式变量,sg add 101229
TARCH_IMPEXP void InitTextStyles(BOOL bTrue)
{
const TCHAR *pszLayFile = _T("textstyledef.dat");
TextStyleList textStyles(pszLayFile);
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (bTrue)
{
// 应用程序加载
CTextStyleInfo *tsPtr = textStyles.FindNode(g_tchTextStyles[i].szKey, CTextStyleCompare);
int nColor = 0;
if (tsPtr != NULL)
{
g_tchTextStyles[i].szName = _tcsdup(tsPtr->szName);
g_tchTextStyles[i].szWindows = _tcsdup(tsPtr->szWindows);
g_tchTextStyles[i].szChFont = _tcsdup(tsPtr->szChFont);
g_tchTextStyles[i].szEnFont = _tcsdup(tsPtr->szEnFont);
g_tchTextStyles[i].xScale = tsPtr->xScale;
g_tchTextStyles[i].nEnChScaleX = tsPtr->nEnChScaleX;
g_tchTextStyles[i].nEnChScaleY = tsPtr->nEnChScaleY;
g_tchTextStyles[i].nHeight = tsPtr->nHeight;
g_tchTextStyles[i].nColor = tsPtr->nColor;
}
}
}
if (bTrue)
{
lstWinfont.Reset();
lstCharset.Reset();
lstPitch.Reset();
CWnd*pWin = AfxGetMainWnd();
if (pWin == NULL)
return;
CDC* pDC = pWin->GetDC();
if (pDC == NULL)
return;
LOGFONT lf;
ZeroMemory(&lf,sizeof(lf));
lf.lfCharSet = DEFAULT_CHARSET;
EnumFontFamiliesEx(pDC->GetSafeHdc(), &lf,
(FONTENUMPROC)EnumFontFamExProc, NULL, 0);
pWin->ReleaseDC(pDC);
// 保持3个数组的长度和索引对应关系一致,其实换成结构体数组更安全
if (lstWinfont.Length() != lstCharset.Length() ||
lstWinfont.Length() != lstPitch.Length())
{
// 不相等的话,全部清空,更安全
lstWinfont.Reset();
lstCharset.Reset();
lstPitch.Reset();
}
}
}
BOOL FindWinFont(const TCHAR* sFace, int &iCharSet, int &iPitchFamily)
{
for (long i=0; i<lstWinfont.Length(); i++)
{
if (_tcsicmp(lstWinfont[i], sFace) == 0)
{
iCharSet = *lstCharset[i];
iPitchFamily = *lstPitch[i];
return TRUE;
}
}
return FALSE;
}
// 通过TTextStyle的方式,创建文字样式
AcDbObjectId InitTextStyle(TTextStyle ts, AcDbDatabase *pDb)
{
AcDbObjectId idRet;
TTextStyleInfo *pInfo = &g_tchTextStyles[0];
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (g_tchTextStyles[i].ts == ts)
{
pInfo = &(g_tchTextStyles[i]);
break;
}
}
idRet = GetTextStyleId(pInfo->szName, pDb);
if (!idRet)
{
AcDbTextStyleTableRecord *pStyle = new AcDbTextStyleTableRecord;
pStyle->setName(pInfo->szName);
if (_tcsicmp(pInfo->szWindows, _T("否")) == 0) // CAD字体
{
pStyle->setFileName(pInfo->szEnFont);
pStyle->setBigFontFileName(pInfo->szChFont);
#if ADS < 21
pStyle->setFont(NULL, Adesk::kFalse, Adesk::kFalse, 0, 0);
#else
Autodesk::AutoCAD::PAL::FontUtils::FontPitch pitch;
Autodesk::AutoCAD::PAL::FontUtils::FontFamily family;
Charset charset;
pStyle->setFont(NULL, Adesk::kFalse, Adesk::kFalse, charset, pitch, family);
#endif
}
else // Windows字体
{
int iCharSet(0), iPitchFamily(0);
if (FindWinFont(pInfo->szChFont, iCharSet, iPitchFamily))
{
pStyle->setBigFontFileName(NULL);
#if ADS < 21
pStyle->setFont(pInfo->szChFont, Adesk::kFalse, Adesk::kFalse, iCharSet, iPitchFamily);
#else
Autodesk::AutoCAD::PAL::FontUtils::FontPitch pitch;
Autodesk::AutoCAD::PAL::FontUtils::FontFamily family;
Charset charset;
pStyle->setFont(pInfo->szChFont, Adesk::kFalse, Adesk::kFalse, charset, pitch, family);
#endif
}
else // 获取参数失败,这个地方我有点吃不准怎么弄好,是直接返回空的ID,还是换成CAD字体的接口,过去就是这样的,还是直接按CAD字体设置吧
{
delete pStyle;
return idRet;
//pStyle->setFileName(pInfo->szEnFont);
//pStyle->setBigFontFileName(pInfo->szChFont);
}
}
pStyle->setTextSize(0);
pStyle->setXScale(pInfo->xScale);
idRet = AddTextStyle(pStyle, FALSE, pDb);
if (!pDb || pDb==acdbCurDwg()) // 当前DWG
{
resbuf *pXData = ads_buildlist(1001, TCHXD,
1040, pInfo->nEnChScaleY, 1040, pInfo->nEnChScaleX, 1070, pInfo->nColor,
NULL);
if(!XSymbolTable(_T("APPID"), TCHXD))
ads_regapp(TCHXD);
pStyle->setXData(pXData);
ads_relrb(pXData);
}
pStyle->close();
}
return idRet;
}
// 通过TTextStyle的方式,获得文字样式名称
CString GetTextStyleName(TTextStyle ts)
{
CString csRet(_T(""));
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (g_tchTextStyles[i].ts == ts)
{
csRet = g_tchTextStyles[i].szName;
break;
}
}
return csRet;
}
// 通过TTextStyle的方式,获得文字字高
double GetTextHeightByTs(TTextStyle ts)
{
double dRet;
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (g_tchTextStyles[i].ts == ts)
{
dRet = g_tchTextStyles[i].nHeight;
break;
}
}
return dRet;
}
// 通过TTextStyle的方式,获得文字颜色
int GetTextColorByTs(TTextStyle ts)
{
int nRet;
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (g_tchTextStyles[i].ts == ts)
{
nRet = g_tchTextStyles[i].nColor;
break;
}
}
return nRet;
}
// 初始化一文字样式
AcDbObjectId InitTextStyle(const TCHAR *pszStyle, AcDbDatabase *pDb)
{
AcDbObjectId id = GetTextStyleId(pszStyle, pDb);;
if (!id)
{
CString sStyle(pszStyle);
TTextStyleInfo *pInfo(NULL);
for (int i=0; i<ELEMENTS(g_tchTextStyles); i++)
{
if (g_tchTextStyles[i].szName == sStyle)
{
pInfo = &(g_tchTextStyles[i]);
break;
}
}
if (pInfo)
{
id = InitTextStyle(pInfo->ts, pDb);
}
else
{
AcDbTextStyleTableRecord *pStyle = new AcDbTextStyleTableRecord;
pStyle->setFileName(_T("gbenor"));
pStyle->setBigFontFileName(_T("gbcbig"));
pStyle->setName(pszStyle);
id = AddTextStyle(pStyle, TRUE, pDb);
if (!id)
delete pStyle;
}
}
return id;
}
AcDbObjectId GetValidTextStyle(AcDbDatabase *pDb, const TCHAR *pStyle)
{
if (!pDb)
pDb = acdbCurDwg();
BOOL bGetNext = FALSE;
if (!pStyle)
pStyle = _T("STANDARD");
AcDbObjectId id = GetTextStyleId(pStyle, pDb);
if (!id)
{
id = pDb->textstyle();
}
return id;
}
int GetTextStyleColor(AcDbObjectId idStyle)
{
int nColor=0;
OPENOBJ_BEGIN(idStyle, AcDb::kForRead, AcDbTextStyleTableRecord, pStyle);
ASSERT(pStyle);
double sh, sw;
resbuf *pxData = pStyle->xData(TCHXD);
if (pxData)
{
resbuf *pNext = pxData->rbnext;
if (pNext && pNext->restype==1040)
{
sh = pNext->resval.rreal;
pNext = pNext->rbnext;
if (pNext && pNext->restype==1040)
{
sw = pNext->resval.rreal;
pNext = pNext->rbnext;
if (pNext && pNext->restype==1070)
nColor = pNext->resval.rint;
}
}
ads_relrb(pxData);
}
OPENOBJ_END();
return nColor;
}
//BOOL FindWinFont(const TCHAR* sFace,int&iCharSet,int&iPitchFamily)
//{
// lstWinfont.Reset();
// lstCharset.Reset();
// lstPitch.Reset();
//
// CWnd*pWin=AfxGetMainWnd();
// if(pWin==NULL)
// return FALSE;
// CDC* pDC=pWin->GetDC();
// if(pDC==NULL)
// return FALSE;
// LOGFONT lf;
// EnumFontFamiliesEx(pDC->GetSafeHdc(), &lf,
// (FONTENUMPROC)EnumFontFamExProc, NULL, 0);
// pWin->ReleaseDC(pDC);
//
// if(lstWinfont.Length()!=lstCharset.Length()||
// lstWinfont.Length()!=lstPitch.Length())
// return FALSE;
//
// for(long i=0;i<lstWinfont.Length();i++)
// if(_tcsicmp(lstWinfont[i],sFace)==0)
// {
// iCharSet=*lstCharset[i];
// iPitchFamily=*lstPitch[i];
// return TRUE;
// }
//
// return FALSE;
//}
//modify by xlc on 100310 移入tchgelib工程
//AcDbObjectId InitDynTextStyle()
//{
// AcDbObjectId idRet=GetTextStyleId(_T("_TCH_DYN"));
// if(!idRet)
// {
// AcDbTextStyleTableRecord *pStyle=new AcDbTextStyleTableRecord;
// pStyle->setName(_T("_TCH_DYN"));
//
// int iCharSet=0,iPitchFamily=0;
// if(/*FindWinFont(_T("宋体"),iCharSet,iPitchFamily)&&*/
// pStyle->setFont(_T("宋体"),Adesk::kFalse ,Adesk::kFalse ,iCharSet,iPitchFamily)==Acad::eOk)
// idRet=AddTextStyle(pStyle, TRUE);
// else
// delete pStyle;
// }
// return idRet;
//}