widget.setAppearance(appearance);
}
PDAppearanceEntry normalAppearance = appearance.getNormalAppearance();
// TODO support more than one appearance stream
PDAppearanceStream appearanceStream =
normalAppearance.isStream() ? normalAppearance.getAppearanceStream() : null;
if (appearanceStream == null)
{
COSStream cosStream = acroForm.getDocument().getDocument().createCOSStream();
appearanceStream = new PDAppearanceStream(cosStream);
appearanceStream.setBBox(widget.getRectangle()
.createRetranslatedRectangle());
appearance.setNormalAppearance(appearanceStream);
}
List<Object> tokens = getStreamTokens(appearanceStream);
List<Object> daTokens = getStreamTokens(getDefaultAppearance());
PDFont pdFont = getFontAndUpdateResources(tokens, appearanceStream);
if (!containsMarkedContent(tokens))
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
// BJL 9/25/2004 Must prepend existing stream
// because it might have operators to draw things like
// rectangles and such
ContentStreamWriter writer = new ContentStreamWriter(output);
writer.writeTokens(tokens);
output.write(" /Tx BMC\n".getBytes("ISO-8859-1"));
insertGeneratedAppearance(widget, output, pdFont, tokens, appearanceStream);
output.write(" EMC".getBytes("ISO-8859-1"));
writeToStream(output.toByteArray(), appearanceStream);
}
else
{
if (tokens != null)
{
if (daTokens != null)
{
int bmcIndex = tokens.indexOf(Operator.getOperator("BMC"));
int emcIndex = tokens.indexOf(Operator.getOperator("EMC"));
if (bmcIndex != -1 && emcIndex != -1 && emcIndex == bmcIndex + 1)
{
// if the EMC immediately follows the BMC index then should
// insert the daTokens inbetween the two markers.
tokens.addAll(emcIndex, daTokens);
}
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
ContentStreamWriter writer = new ContentStreamWriter(output);
float fontSize = calculateFontSize(pdFont,
appearanceStream.getBBox(), tokens, daTokens);
int setFontIndex = tokens.indexOf(Operator.getOperator("Tf"));
tokens.set(setFontIndex - 1, new COSFloat(fontSize));
int bmcIndex = tokens.indexOf(Operator.getOperator("BMC"));
int emcIndex = tokens.indexOf(Operator.getOperator("EMC"));