Skip to content

Commit

Permalink
修复了 setlinestyle 的 bug
Browse files Browse the repository at this point in the history
设置 PS_USERSTYLE 得到正确间隔
改进了 upattern 转换算法
  • Loading branch information
chirsz-ever committed Apr 12, 2020
1 parent 52d662f commit 7b0326c
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,31 +617,35 @@ getcolor(PIMAGE pimg) {
}

// 将描述线形的位模式转换为 style 数组
// 用于 ExtCreatePen 中
// 返回用到的数组元素数
// 用于 ExtCreatePen 中
// 返回值为用到的数组元素数。
static int upattern2array(unsigned short upattern, DWORD style[]) {
int n, bn = 0, len = 1;
int st = upattern & 1;
int n, segments = 0, segmentLength = 1;
int state = !!(upattern & 1);
for (n = 1; n < 16; n++) {
if (upattern & (1<<n)) {
if (st == 0) {
st = 1;
style[bn++] = len;
len = 1;
} else {
len++;
}
int currentBit = !!(upattern & (1<<n));
if (state == currentBit) {
segmentLength += 1;
} else {
if (st == 0) {
len++;
} else {
st = 0;
style[bn++] = len;
len = 1;
}
state = currentBit;
style[segments] = segmentLength;
segments += 1;
segmentLength = 1;
}
}
return bn;
style[segments] = segmentLength;
segments += 1;

// 若 upattern 以 0 开头且为偶数段
if (!(upattern & 1) && segments % 2 == 0) {
DWORD p0 = style[0];
for (int i = 0; i < segments - 1; ++i) {
style[i] = style[i + 1];
}
style[segments - 1] = p0;
}

return segments;
}

void
Expand All @@ -668,7 +672,7 @@ setcolor(color_t color, PIMAGE pimg) {
lbr.lbStyle = BS_SOLID;
lbr.lbHatch = 0;
bn = upattern2array(upattern, style);
hpen = ExtCreatePen(PS_GEOMETRIC, img->m_linestyle.thickness, &lbr, bn, style);
hpen = ExtCreatePen(PS_GEOMETRIC|PS_USERSTYLE, img->m_linestyle.thickness, &lbr, bn, style);
} else {
hpen = CreatePenIndirect(&lPen);
}
Expand Down Expand Up @@ -1370,7 +1374,8 @@ void
getlinestyle(int *plinestyle, unsigned short *pupattern, int *pthickness, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE_CONST(pimg);
if (plinestyle) {
*plinestyle = img->m_linestyle.linestyle;
unsigned short linestyle = img->m_linestyle.linestyle;
*plinestyle = (linestyle & PS_USERSTYLE) ? PS_USERSTYLE : linestyle;
}
if (pupattern) {
*pupattern = img->m_linestyle.upattern;
Expand Down Expand Up @@ -1403,7 +1408,7 @@ setlinestyle(int linestyle, unsigned short upattern, int thickness, PIMAGE pimg)
lbr.lbStyle = BS_SOLID;
lbr.lbHatch = 0;
bn = upattern2array(upattern, style);
hpen = ExtCreatePen(PS_GEOMETRIC, thickness, &lbr, bn, style);
hpen = ExtCreatePen(PS_GEOMETRIC|PS_USERSTYLE, thickness, &lbr, bn, style);
} else {
hpen = CreatePenIndirect(&lpen);
}
Expand Down Expand Up @@ -1431,7 +1436,7 @@ setlinewidth(float width, PIMAGE pimg) {
lbr.lbStyle = BS_SOLID;
lbr.lbHatch = 0;
bn = upattern2array(img->m_linestyle.upattern, style);
hpen = ExtCreatePen(PS_GEOMETRIC, (int)width, &lbr, bn, style);
hpen = ExtCreatePen(PS_GEOMETRIC|PS_USERSTYLE, (int)width, &lbr, bn, style);
} else {
LOGPEN lpen = {0};
lpen.lopnColor = img->m_color;
Expand Down

0 comments on commit 7b0326c

Please sign in to comment.