Code with Finding: |
class TextField {
/**
* Get the <code>PdfAppearance</code> of a text or combo field
* @throws IOException on error
* @throws DocumentException on error
* @return A <code>PdfAppearance</code>
*/
public PdfAppearance getAppearance() throws IOException, DocumentException {
PdfAppearance app = getBorderAppearance();
app.beginVariableText();
if (text == null || text.length() == 0) {
app.endVariableText();
return app;
}
boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
float h = box.getHeight() - borderWidth * 2 - extraMarginTop;
float bw2 = borderWidth;
if (borderExtra) {
h -= borderWidth * 2;
bw2 *= 2;
}
float offsetX = Math.max(bw2, 1);
float offX = Math.min(bw2, offsetX);
app.saveState();
app.rectangle(offX, offX, box.getWidth() - 2 * offX, box.getHeight() - 2 * offX);
app.clip();
app.newPath();
String ptext;
if ((options & PASSWORD) != 0)
ptext = obfuscatePassword(text);
else if ((options & MULTILINE) == 0)
ptext = removeCRLF(text);
else
ptext = text; //fixed by Kazuya Ujihara (ujihara.jp)
BaseFont ufont = getRealFont();
BaseColor fcolor = textColor == null ? GrayColor.GRAYBLACK : textColor;
int rtl = checkRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;
float usize = fontSize;
Phrase phrase = composePhrase(ptext, ufont, fcolor, usize);
if ((options & MULTILINE) != 0) {
float width = box.getWidth() - 4 * offsetX - extraMarginLeft;
float factor = ufont.getFontDescriptor(BaseFont.BBOXURY, 1) - ufont.getFontDescriptor(BaseFont.BBOXLLY, 1);
ColumnText ct = new ColumnText(null);
if (usize == 0) {
usize = h / factor;
if (usize > 4) {
if (usize > 12)
usize = 12;
float step = Math.max((usize - 4) / 10, 0.2f);
ct.setSimpleColumn(0, -h, width, 0);
ct.setAlignment(alignment);
ct.setRunDirection(rtl);
for (; usize > 4; usize -= step) {
ct.setYLine(0);
changeFontSize(phrase, usize);
ct.setText(phrase);
ct.setLeading(factor * usize);
int status = ct.go(true);
if ((status & ColumnText.NO_MORE_COLUMN) == 0)
break;
}
}
if (usize < 4)
usize = 4;
}
changeFontSize(phrase, usize);
ct.setCanvas(app);
float leading = usize * factor;
float offsetY = offsetX + h - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);
ct.setSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.getWidth() - 2 * offsetX, offsetY + leading);
ct.setLeading(leading);
ct.setAlignment(alignment);
ct.setRunDirection(rtl);
ct.setText(phrase);
ct.go();
}
else {
if (usize == 0) {
float maxCalculatedSize = h / (ufont.getFontDescriptor(BaseFont.BBOXURX, 1) - ufont.getFontDescriptor(BaseFont.BBOXLLY, 1));
changeFontSize(phrase, 1);
float wd = ColumnText.getWidth(phrase, rtl, 0);
if (wd == 0)
usize = maxCalculatedSize;
else
usize = Math.min(maxCalculatedSize, (box.getWidth() - extraMarginLeft - 4 * offsetX) / wd);
if (usize < 4)
usize = 4;
}
changeFontSize(phrase, usize);
float offsetY = offX + (box.getHeight() - 2*offX - ufont.getFontDescriptor(BaseFont.ASCENT, usize)) / 2;
if (offsetY < offX)
offsetY = offX;
if (offsetY - offX < -ufont.getFontDescriptor(BaseFont.DESCENT, usize)) {
float ny = -ufont.getFontDescriptor(BaseFont.DESCENT, usize) + offX;
float dy = box.getHeight() - offX - ufont.getFontDescriptor(BaseFont.ASCENT, usize);
offsetY = Math.min(ny, Math.max(offsetY, dy));
}
if ((options & COMB) != 0 && maxCharacterLength > 0) {
int textLen = Math.min(maxCharacterLength, ptext.length());
int position = 0;
if (alignment == Element.ALIGN_RIGHT)
position = maxCharacterLength - textLen;
else if (alignment == Element.ALIGN_CENTER)
position = (maxCharacterLength - textLen) / 2;
float step = (box.getWidth() - extraMarginLeft) / maxCharacterLength;
float start = step / 2 + position * step;
if (textColor == null)
app.setGrayFill(0);
else
app.setColorFill(textColor);
app.beginText();
for (int k = 0; k < phrase.size(); ++k) {
Chunk ck = (Chunk)phrase.get(k);
BaseFont bf = ck.getFont().getBaseFont();
app.setFontAndSize(bf, usize);
StringBuffer sb = ck.append("");
for (int j = 0; j < sb.length(); ++j) {
String c = sb.substring(j, j + 1);
float wd = bf.getWidthPoint(c, usize);
app.setTextMatrix(extraMarginLeft + start - wd / 2, offsetY - extraMarginTop);
app.showText(c);
start += step;
}
}
app.endText();
}
else {
float x;
switch (alignment) {
case Element.ALIGN_RIGHT:
x = extraMarginLeft + box.getWidth() - 2 * offsetX;
break;
case Element.ALIGN_CENTER:
x = extraMarginLeft + box.getWidth() / 2;
break;
default:
x = extraMarginLeft + 2 * offsetX;
}
ColumnText.showTextAligned(app, alignment, phrase, x, offsetY - extraMarginTop, 0, rtl, 0);
}
}
app.restoreState();
app.endVariableText();
return app;
}
}
|