📄 gcstringprinter.java
字号:
if (posWordStart < 0 || posLastWordStart >= lineInfo.originalLine.length())
break;
String word = lineInfo.originalLine.substring(posLastWordStart, posWordStart);
if (word.length() == 0)
{
excessPos = -1;
outputLine.append(' ');
}
for (int i = 0; i < word.length(); i += 4000)
{
int endPos = i + 4000;
String subWord;
if (endPos > word.length())
subWord = word.substring(i);
else
subWord = word.substring(i, endPos);
excessPos = processWord(gc, lineInfo.originalLine, subWord, printArea, wrap, lineInfo, outputLine, space);
if (excessPos >= 0)
{
excessPos += curPos;
break;
}
if (endPos <= word.length())
space.setLength(0);
curPos += subWord.length() + 1;
}
if (excessPos >= 0)
break;
posLastWordStart = posWordStart + 1;
posWordStart = lineInfo.originalLine.indexOf(' ', posLastWordStart);
if (posWordStart < 0)
posWordStart = lineInfo.originalLine.length();
} while (true);
}
} else
{
outputLine.append(lineInfo.originalLine);
}
if (!wrap && hasMoreElements && excessPos >= 0)
{
int len = outputLine.length();
if (len > 2)
len -= 2;
outputLine.setLength(len);
outputLine.append("…");
cutoff = true;
}
lineInfo.excessPos = excessPos;
lineInfo.lineOutputed = outputLine.toString();
return lineInfo;
}
private int processWord(GC gc, String sLine, String word, Rectangle printArea, boolean wrap, LineInfo lineInfo, StringBuffer outputLine,
StringBuffer space)
{
if (word.length() == 0)
{
space.append(' ');
return -1;
}
if (images != null && word.length() >= 2 && word.charAt(0) == '%')
{
int imgIdx = word.charAt(1) - 48;
if (images.length > imgIdx && imgIdx >= 0 && images[imgIdx] != null)
{
Image img = images[imgIdx];
Rectangle bounds = img.getBounds();
if (imageScales != null && imageScales.length > imgIdx)
{
bounds.width = (int)((float)bounds.width * imageScales[imgIdx]);
bounds.height = (int)((float)bounds.height * imageScales[imgIdx]);
}
Point spaceExtent = gc.stringExtent(space.toString());
int newWidth = lineInfo.width + bounds.width + spaceExtent.x;
if (newWidth > printArea.width && (bounds.width + spaceExtent.x < printArea.width || lineInfo.width > 0))
return 0;
if (lineInfo.imageIndexes == null)
lineInfo.imageIndexes = (new int[] {
imgIdx
});
int targetWidth = lineInfo.width + newWidth;
lineInfo.width = newWidth;
lineInfo.height = Math.max(bounds.height, lineInfo.height);
Point ptWordSize = gc.stringExtent((new StringBuilder()).append(word.substring(2)).append(" ").toString());
if (lineInfo.width + ptWordSize.x > printArea.width)
{
outputLine.append(space);
outputLine.append(word.substring(0, 2));
return 2;
}
outputLine.append(space);
space.setLength(0);
outputLine.append(word.substring(0, 2));
word = word.substring(2);
}
}
Point ptWordSize = gc.stringExtent((new StringBuilder()).append(word).append(" ").toString());
boolean bWordLargerThanWidth = ptWordSize.x > printArea.width;
if (bWordLargerThanWidth && lineInfo.width > 0)
return 0;
int targetWidth = lineInfo.width + ptWordSize.x;
if (targetWidth > printArea.width)
{
int endIndex = word.length();
long diff = endIndex;
do
{
if (targetWidth == printArea.width)
break;
diff = (diff >> 1) + diff % 2L;
if (diff <= 0L)
diff = 1L;
if (targetWidth > printArea.width)
{
endIndex = (int)((long)endIndex - diff);
if (endIndex < 1)
endIndex = 1;
} else
{
endIndex = (int)((long)endIndex + diff);
if (endIndex > word.length())
endIndex = word.length();
}
ptWordSize = gc.stringExtent((new StringBuilder()).append(word.substring(0, endIndex)).append(" ").toString());
targetWidth = lineInfo.width + ptWordSize.x;
} while (diff > 1L);
if (endIndex == 0)
endIndex = 1;
if (targetWidth > printArea.width && endIndex > 1)
{
endIndex--;
ptWordSize = gc.stringExtent((new StringBuilder()).append(word.substring(0, endIndex)).append(" ").toString());
}
if (endIndex > 0 && outputLine.length() > 0)
outputLine.append(space);
if (endIndex == 0 && outputLine.length() == 0)
endIndex = 1;
if (wrap && ptWordSize.x < printArea.width && !bWordLargerThanWidth)
return 0;
outputLine.append(word.substring(0, endIndex));
if (!wrap)
{
int len = outputLine.length();
if (len == 0)
{
if (word.length() > 0)
outputLine.append(word.charAt(0));
else
if (sLine.length() > 0)
outputLine.append(sLine.charAt(0));
} else
{
if (len > 2)
len -= 2;
outputLine.setLength(len);
outputLine.append("…");
cutoff = true;
}
}
return endIndex;
}
lineInfo.width += ptWordSize.x;
if (lineInfo.width > printArea.width)
{
if (space.length() > 0)
space.delete(0, space.length());
if (!wrap)
{
int len = outputLine.length();
if (len == 0)
{
if (word.length() > 0)
outputLine.append(word.charAt(0));
else
if (sLine.length() > 0)
outputLine.append(sLine.charAt(0));
} else
{
if (len > 2)
len -= 2;
outputLine.setLength(len);
outputLine.append("…");
cutoff = true;
}
return -1;
} else
{
return 0;
}
}
if (outputLine.length() > 0)
outputLine.append(space);
outputLine.append(word);
if (space.length() > 0)
space.delete(0, space.length());
space.append(' ');
return -1;
}
private void drawLine(GC gc, LineInfo lineInfo, int swtFlags, Rectangle printArea, boolean noDraw)
{
String text = lineInfo.lineOutputed;
if (lineInfo.width == 0 || lineInfo.height == 0)
{
Point gcExtent = gc.stringExtent(text);
if (lineInfo.width == 0)
lineInfo.width = gcExtent.x;
if (lineInfo.height == 0)
lineInfo.height = gcExtent.y;
}
Point drawSize = new Point(lineInfo.width, lineInfo.height);
int x0;
if ((swtFlags & 0x20000) > 0)
x0 = (printArea.x + printArea.width) - drawSize.x;
else
if ((swtFlags & 0x1000000) > 0)
x0 = printArea.x + (printArea.width - drawSize.x) / 2;
else
x0 = printArea.x;
int y0 = printArea.y;
int lineInfoRelEndPos = lineInfo.relStartPos + lineInfo.lineOutputed.length();
int relStartPos = lineInfo.relStartPos;
int lineStartPos = 0;
URLInfo urlInfo = null;
boolean drawURL = hasHitUrl();
if (drawURL)
{
URLInfo hitUrlInfo[] = getHitUrlInfo();
int nextHitUrlInfoPos = 0;
while (drawURL)
{
drawURL = false;
int i = nextHitUrlInfoPos;
do
{
if (i >= hitUrlInfo.length)
break;
urlInfo = hitUrlInfo[i];
drawURL = urlInfo.relStartPos < lineInfoRelEndPos && urlInfo.relStartPos + urlInfo.titleLength > relStartPos && relStartPos >= lineInfo.relStartPos && relStartPos < lineInfoRelEndPos;
if (drawURL)
{
nextHitUrlInfoPos = i + 1;
break;
}
i++;
} while (true);
if (!drawURL)
break;
i = (lineStartPos + urlInfo.relStartPos) - relStartPos;
if (i > 0 && i > lineStartPos && i <= text.length())
{
String s = text.substring(lineStartPos, i);
x0 += drawText(gc, s, x0, y0, lineInfo.height, null, noDraw).x;
relStartPos += i - lineStartPos;
lineStartPos += i - lineStartPos;
}
int end = i + urlInfo.titleLength;
if (i < 0)
i = 0;
if (end > text.length())
end = text.length();
String s = text.substring(i, end);
relStartPos += end - i;
lineStartPos += end - i;
Point pt = null;
Color fgColor = null;
if (!noDraw)
{
fgColor = gc.getForeground();
if (urlInfo.dropShadowColor != null)
{
gc.setForeground(urlInfo.dropShadowColor);
drawText(gc, s, x0 + 1, y0 + 1, lineInfo.height, null, noDraw);
}
if (urlInfo.urlColor != null)
gc.setForeground(urlInfo.urlColor);
else
if (urlColor != null)
gc.setForeground(urlColor);
}
if (urlInfo.hitAreas == null)
urlInfo.hitAreas = new ArrayList(1);
pt = drawText(gc, s, x0, y0, lineInfo.height, urlInfo.hitAreas, noDraw);
if (!noDraw)
{
if (urlInfo.urlUnderline)
gc.drawLine(x0, (y0 + pt.y) - 1, (x0 + pt.x) - 1, (y0 + pt.y) - 1);
gc.setForeground(fgColor);
}
if (urlInfo.hitAreas == null)
urlInfo.hitAreas = new ArrayList(1);
x0 += pt.x;
}
}
if (lineStartPos < text.length())
{
String s = text.substring(lineStartPos);
if (!noDraw)
drawText(gc, s, x0, y0, lineInfo.height, null, noDraw);
}
printArea.y += drawSize.y;
}
private Point drawText(GC gc, String s, int x, int y, int height, List hitAreas, boolean nodraw)
{
Point textExtent;
if (images != null)
{
int pctPos = s.indexOf('%');
int lastPos = 0;
int w = 0;
int h = 0;
do
{
if (pctPos < 0)
break;
if (pctPos >= 0 && s.length() > pctPos + 1)
{
int imgIdx = s.charAt(pctPos + 1) - 48;
String sStart;
int centerY;
if (imgIdx >= images.length || imgIdx < 0 || images[imgIdx] == null)
{
sStart = s.substring(lastPos, pctPos + 1);
textExtent = gc.textExtent(sStart);
centerY = y + (height / 2 - textExtent.y / 2);
if (hitAreas != null)
hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y));
if (!nodraw)
gc.drawText(sStart, x, centerY, true);
x += textExtent.x;
w += textExtent.x;
h = Math.max(h, textExtent.y);
lastPos = pctPos + 1;
pctPos = s.indexOf('%', pctPos + 1);
continue;
}
sStart = s.substring(lastPos, pctPos);
textExtent = gc.textExtent(sStart);
centerY = y + (height / 2 - textExtent.y / 2);
if (!nodraw)
gc.drawText(sStart, x, centerY, true);
x += textExtent.x;
w += textExtent.x;
h = Math.max(h, textExtent.y);
if (hitAreas != null)
hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y));
Rectangle imgBounds = images[imgIdx].getBounds();
float scale = 1.0F;
if (imageScales != null && imageScales.length > imgIdx)
scale = imageScales[imgIdx];
int scaleImageWidth = (int)((float)imgBounds.width * scale);
int scaleImageHeight = (int)((float)imgBounds.height * scale);
centerY = y + (height / 2 - scaleImageHeight / 2);
if (hitAreas != null)
hitAreas.add(new Rectangle(x, centerY, scaleImageWidth, scaleImageHeight));
if (!nodraw)
gc.drawImage(images[imgIdx], 0, 0, imgBounds.width, imgBounds.height, x, centerY, scaleImageWidth, scaleImageHeight);
x += scaleImageWidth;
w += scaleImageWidth;
h = Math.max(h, scaleImageHeight);
}
lastPos = pctPos + 2;
pctPos = s.indexOf('%', lastPos);
} while (true);
if (s.length() >= lastPos)
{
String sEnd = s.substring(lastPos);
textExtent = gc.textExtent(sEnd);
int centerY = y + (height / 2 - textExtent.y / 2);
if (hitAreas != null)
hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y));
if (!nodraw)
gc.drawText(sEnd, x, centerY, true);
x += textExtent.x;
w += textExtent.x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -