TrueType文字显示,该如何解决
TrueType文字显示 - C++ Builder / Windows SDK/API
下面的代码我想大家都见过 是读取TrueType字体结构的代码,我现在用他读取的轮廓感觉比较粗糙,锯齿多,不够圆滑,请问有什么方法可以修正 或者有更好的算法,请赐教,谢谢
pCanvas->Font->Size =80;
pCanvas->Font->Name="宋体";
pCanvas->Pen->Color = clBlack;
pCanvas->Pen->Mode = pmCopy;
pCanvas->Pen->Style = psSolid;
pCanvas->Pen->Width = 1;
int XSize = ClipRect.Width() / Text.Length();
int YSize = ClipRect.Height();
MAT2 mat2; // 转置矩阵,不用变换
mat2.eM11.value = 1;mat2.eM11.fract = 0;
mat2.eM12.value = 0;mat2.eM12.fract = 0;
mat2.eM21.value = 0;mat2.eM21.fract = 0;
mat2.eM22.value = 1;mat2.eM22.fract = 0;
GLYPHMETRICS gm,gmm;
// 首先获得字符的位置矩阵,存入gm
GetGlyphOutlineA(pCanvas->Handle,0x0b0a1,
GGO_METRICS,&gm,0,NULL,&mat2);
char *ptr = Text.c_str();
TRect TheRect;
for(int i = 0;i < Text.Length();) {
int c1 = (unsigned char)*ptr;
int c2 = (unsigned char)*(ptr + 1);
UINT nChar;
TheRect.Left = i * XSize + ClipRect.Left;
TheRect.Top = ClipRect.Top;
TheRect.Right = (i + 2) * XSize + ClipRect.Left;
TheRect.Bottom = ClipRect.Top + YSize;
if(c1 > 127) { // 当前字符是汉字
nChar = c1 * 256 + c2;
ptr+=2;i+=2;
}
else { // 字母或数字
nChar = c1;
ptr++;i++;
}
// 获得当前字符数据的数组的大小
DWORD cbBuffer = GetGlyphOutlineA(pCanvas->
Handle,nChar,GGO_NATIVE,&gmm,0,NULL,&mat2);
if(cbBuffer == GDI_ERROR) break;
void *lpBuffer = malloc(cbBuffer);
if(lpBuffer != NULL) {
// 读入数据置缓冲区
if(GetGlyphOutlineA(pCanvas->
Handle,nChar,GGO_NATIVE,
&gmm,cbBuffer,lpBuffer,&mat2) != GDI_ERROR) {
// 分析数据并绘制字符
TMemoryStream *MBuffer = new TMemoryStream();
MBuffer->Write(lpBuffer,cbBuffer);
MBuffer->Position = 0;
for(;MBuffer->Position < MBuffer->Size;) {
int CurPos = MBuffer->Position;
TTPOLYGONHEADER polyheader;
int ptn = 0;
MBuffer->Read(&polyheader,sizeof(polyheader));
ptn++;
for(int j = 0;j < (int)(polyheader.cb
- sizeof(polyheader));) {
WORD wtype,cpfx;
MBuffer->Read(&wtype,sizeof(WORD));
MBuffer->Read(&cpfx,sizeof(WORD));
MBuffer->Position += cpfx * sizeof(POINTFX);
j += sizeof(WORD) * 2 + cpfx * sizeof(POINTFX);
if(wtype == TT_PRIM_LINE) ptn += cpfx;
else ptn += (cpfx - 1) * 3 + 1;
}
TPoint *pts = new TPoint[ptn+1]; // 存储多边形顶点
MBuffer->Position = CurPos;
ptn = 0;
MBuffer->Read(&polyheader,sizeof(polyheader));
TPoint pt0 = POINTFX2TPoint(polyheader.pfxStart,TheRect,&gm);
pts[ptn++] = pt0;
for(int j = 0;j < (int)(polyheader.cb - sizeof(polyheader));) {
TPoint pt1;
WORD wtype,cpfx;
MBuffer->Read(&wtype,sizeof(WORD));
MBuffer->Read(&cpfx,sizeof(WORD));
POINTFX *pPfx = new POINTFX[cpfx];
MBuffer->Read((void *)pPfx,cpfx * sizeof(POINTFX));
j += sizeof(WORD) * 2 + cpfx * sizeof(POINTFX);
if(wtype == TT_PRIM_LINE) { // 直线段
for(int i = 0;i < cpfx;i++) {
pt1 = POINTFX2TPoint(pPfx[i],TheRect,&gm);
pts[ptn++] = pt1;
}
}
else { // Bezier曲线
下面的代码我想大家都见过 是读取TrueType字体结构的代码,我现在用他读取的轮廓感觉比较粗糙,锯齿多,不够圆滑,请问有什么方法可以修正 或者有更好的算法,请赐教,谢谢
pCanvas->Font->Size =80;
pCanvas->Font->Name="宋体";
pCanvas->Pen->Color = clBlack;
pCanvas->Pen->Mode = pmCopy;
pCanvas->Pen->Style = psSolid;
pCanvas->Pen->Width = 1;
int XSize = ClipRect.Width() / Text.Length();
int YSize = ClipRect.Height();
MAT2 mat2; // 转置矩阵,不用变换
mat2.eM11.value = 1;mat2.eM11.fract = 0;
mat2.eM12.value = 0;mat2.eM12.fract = 0;
mat2.eM21.value = 0;mat2.eM21.fract = 0;
mat2.eM22.value = 1;mat2.eM22.fract = 0;
GLYPHMETRICS gm,gmm;
// 首先获得字符的位置矩阵,存入gm
GetGlyphOutlineA(pCanvas->Handle,0x0b0a1,
GGO_METRICS,&gm,0,NULL,&mat2);
char *ptr = Text.c_str();
TRect TheRect;
for(int i = 0;i < Text.Length();) {
int c1 = (unsigned char)*ptr;
int c2 = (unsigned char)*(ptr + 1);
UINT nChar;
TheRect.Left = i * XSize + ClipRect.Left;
TheRect.Top = ClipRect.Top;
TheRect.Right = (i + 2) * XSize + ClipRect.Left;
TheRect.Bottom = ClipRect.Top + YSize;
if(c1 > 127) { // 当前字符是汉字
nChar = c1 * 256 + c2;
ptr+=2;i+=2;
}
else { // 字母或数字
nChar = c1;
ptr++;i++;
}
// 获得当前字符数据的数组的大小
DWORD cbBuffer = GetGlyphOutlineA(pCanvas->
Handle,nChar,GGO_NATIVE,&gmm,0,NULL,&mat2);
if(cbBuffer == GDI_ERROR) break;
void *lpBuffer = malloc(cbBuffer);
if(lpBuffer != NULL) {
// 读入数据置缓冲区
if(GetGlyphOutlineA(pCanvas->
Handle,nChar,GGO_NATIVE,
&gmm,cbBuffer,lpBuffer,&mat2) != GDI_ERROR) {
// 分析数据并绘制字符
TMemoryStream *MBuffer = new TMemoryStream();
MBuffer->Write(lpBuffer,cbBuffer);
MBuffer->Position = 0;
for(;MBuffer->Position < MBuffer->Size;) {
int CurPos = MBuffer->Position;
TTPOLYGONHEADER polyheader;
int ptn = 0;
MBuffer->Read(&polyheader,sizeof(polyheader));
ptn++;
for(int j = 0;j < (int)(polyheader.cb
- sizeof(polyheader));) {
WORD wtype,cpfx;
MBuffer->Read(&wtype,sizeof(WORD));
MBuffer->Read(&cpfx,sizeof(WORD));
MBuffer->Position += cpfx * sizeof(POINTFX);
j += sizeof(WORD) * 2 + cpfx * sizeof(POINTFX);
if(wtype == TT_PRIM_LINE) ptn += cpfx;
else ptn += (cpfx - 1) * 3 + 1;
}
TPoint *pts = new TPoint[ptn+1]; // 存储多边形顶点
MBuffer->Position = CurPos;
ptn = 0;
MBuffer->Read(&polyheader,sizeof(polyheader));
TPoint pt0 = POINTFX2TPoint(polyheader.pfxStart,TheRect,&gm);
pts[ptn++] = pt0;
for(int j = 0;j < (int)(polyheader.cb - sizeof(polyheader));) {
TPoint pt1;
WORD wtype,cpfx;
MBuffer->Read(&wtype,sizeof(WORD));
MBuffer->Read(&cpfx,sizeof(WORD));
POINTFX *pPfx = new POINTFX[cpfx];
MBuffer->Read((void *)pPfx,cpfx * sizeof(POINTFX));
j += sizeof(WORD) * 2 + cpfx * sizeof(POINTFX);
if(wtype == TT_PRIM_LINE) { // 直线段
for(int i = 0;i < cpfx;i++) {
pt1 = POINTFX2TPoint(pPfx[i],TheRect,&gm);
pts[ptn++] = pt1;
}
}
else { // Bezier曲线