Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 58A90200C92 for ; Mon, 12 Jun 2017 20:02:06 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 57431160BCF; Mon, 12 Jun 2017 18:02:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4B7FB160BDE for ; Mon, 12 Jun 2017 20:02:05 +0200 (CEST) Received: (qmail 68881 invoked by uid 500); 12 Jun 2017 18:02:04 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 68872 invoked by uid 99); 12 Jun 2017 18:02:04 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Jun 2017 18:02:04 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 1C15A3A111C for ; Mon, 12 Jun 2017 18:02:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1798498 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Date: Mon, 12 Jun 2017 18:02:00 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20170612180203.1C15A3A111C@svn01-us-west.apache.org> archived-at: Mon, 12 Jun 2017 18:02:06 -0000 Author: tilman Date: Mon Jun 12 18:02:00 2017 New Revision: 1798498 URL: http://svn.apache.org/viewvc?rev=1798498&view=rev Log: PDFBOX-2852: use jdk7 syntax Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1798498&r1=1798497&r2=1798498&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Mon Jun 12 18:02:00 2017 @@ -374,125 +374,125 @@ class AppearanceGeneratorHelper PDAppearanceStream appearanceStream, OutputStream output) throws IOException { - PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream, output); - - PDRectangle bbox = resolveBoundingBox(widget, appearanceStream); - - // Acrobat calculates the left and right padding dependent on the offset of the border edge - // This calculation works for forms having been generated by Acrobat. - // The minimum distance is always 1f even if there is no rectangle being drawn around. - float borderWidth = 0; - if (widget.getBorderStyle() != null) - { - borderWidth = widget.getBorderStyle().getWidth(); - } - PDRectangle clipRect = applyPadding(bbox, Math.max(1f, borderWidth)); - PDRectangle contentRect = applyPadding(clipRect, Math.max(1f, borderWidth)); - - contents.saveGraphicsState(); - - // Acrobat always adds a clipping path - contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), - clipRect.getWidth(), clipRect.getHeight()); - contents.clip(); - - // get the font - PDFont font = defaultAppearance.getFont(); - - // calculate the fontSize (because 0 = autosize) - float fontSize = defaultAppearance.getFontSize(); - - if (fontSize == 0) - { - fontSize = calculateFontSize(font, contentRect); - } - - // for a listbox generate the highlight rectangle for the selected - // options - if (field instanceof PDListBox) - { - insertGeneratedSelectionHighlight(contents, appearanceStream, font, fontSize); - } - - // start the text output - contents.beginText(); - - // write the /DA string - defaultAppearance.writeTo(contents, fontSize); - - // calculate the y-position of the baseline - float y; - - // calculate font metrics at font size - float fontScaleY = fontSize / FONTSCALE; - float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY; - float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY; - float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY; - - if (field instanceof PDTextField && ((PDTextField) field).isMultiline()) - { - y = contentRect.getUpperRightY() - fontBoundingBoxAtSize; - } - else + try (PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream, output)) { - // Adobe shows the text 'shiftet up' in case the caps don't fit into the clipping area - if (fontCapAtSize > clipRect.getHeight()) + PDRectangle bbox = resolveBoundingBox(widget, appearanceStream); + + // Acrobat calculates the left and right padding dependent on the offset of the border edge + // This calculation works for forms having been generated by Acrobat. + // The minimum distance is always 1f even if there is no rectangle being drawn around. + float borderWidth = 0; + if (widget.getBorderStyle() != null) + { + borderWidth = widget.getBorderStyle().getWidth(); + } + PDRectangle clipRect = applyPadding(bbox, Math.max(1f, borderWidth)); + PDRectangle contentRect = applyPadding(clipRect, Math.max(1f, borderWidth)); + + contents.saveGraphicsState(); + + // Acrobat always adds a clipping path + contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), + clipRect.getWidth(), clipRect.getHeight()); + contents.clip(); + + // get the font + PDFont font = defaultAppearance.getFont(); + + // calculate the fontSize (because 0 = autosize) + float fontSize = defaultAppearance.getFontSize(); + + if (fontSize == 0) + { + fontSize = calculateFontSize(font, contentRect); + } + + // for a listbox generate the highlight rectangle for the selected + // options + if (field instanceof PDListBox) { - y = clipRect.getLowerLeftY() + -fontDescentAtSize; + insertGeneratedSelectionHighlight(contents, appearanceStream, font, fontSize); + } + + // start the text output + contents.beginText(); + + // write the /DA string + defaultAppearance.writeTo(contents, fontSize); + + // calculate the y-position of the baseline + float y; + + // calculate font metrics at font size + float fontScaleY = fontSize / FONTSCALE; + float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY; + float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY; + float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY; + + if (field instanceof PDTextField && ((PDTextField) field).isMultiline()) + { + y = contentRect.getUpperRightY() - fontBoundingBoxAtSize; } else { - // calculate the position based on the content rectangle - y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize) / 2; - - // check to ensure that ascents and descents fit - if (y - clipRect.getLowerLeftY() < -fontDescentAtSize) { - - float fontDescentBased = -fontDescentAtSize + contentRect.getLowerLeftY(); - float fontCapBased = contentRect.getHeight() - contentRect.getLowerLeftY() - fontCapAtSize; - - y = Math.min(fontDescentBased, Math.max(y, fontCapBased)); + // Adobe shows the text 'shiftet up' in case the caps don't fit into the clipping area + if (fontCapAtSize > clipRect.getHeight()) + { + y = clipRect.getLowerLeftY() + -fontDescentAtSize; + } + else + { + // calculate the position based on the content rectangle + y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize) / 2; + + // check to ensure that ascents and descents fit + if (y - clipRect.getLowerLeftY() < -fontDescentAtSize) { + + float fontDescentBased = -fontDescentAtSize + contentRect.getLowerLeftY(); + float fontCapBased = contentRect.getHeight() - contentRect.getLowerLeftY() - fontCapAtSize; + + y = Math.min(fontDescentBased, Math.max(y, fontCapBased)); + } } } + + // show the text + float x = contentRect.getLowerLeftX(); + + // special handling for comb boxes as these are like table cells with individual + // chars + if (shallComb()) { + insertGeneratedCombAppearance(contents, appearanceStream, font, fontSize); + } + else if (field instanceof PDListBox) + { + insertGeneratedListboxAppearance(contents, appearanceStream, contentRect, font, fontSize); + } + else + { + PlainText textContent = new PlainText(value); + AppearanceStyle appearanceStyle = new AppearanceStyle(); + appearanceStyle.setFont(font); + appearanceStyle.setFontSize(fontSize); + + // Adobe Acrobat uses the font's bounding box for the leading between the lines + appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY); + + PlainTextFormatter formatter = new PlainTextFormatter + .Builder(contents) + .style(appearanceStyle) + .text(textContent) + .width(contentRect.getWidth()) + .wrapLines(isMultiLine()) + .initialOffset(x, y) + .textAlign(field.getQ()) + .build(); + formatter.format(); + } + + contents.endText(); + contents.restoreGraphicsState(); } - - // show the text - float x = contentRect.getLowerLeftX(); - - // special handling for comb boxes as these are like table cells with individual - // chars - if (shallComb()) { - insertGeneratedCombAppearance(contents, appearanceStream, font, fontSize); - } - else if (field instanceof PDListBox) - { - insertGeneratedListboxAppearance(contents, appearanceStream, contentRect, font, fontSize); - } - else - { - PlainText textContent = new PlainText(value); - AppearanceStyle appearanceStyle = new AppearanceStyle(); - appearanceStyle.setFont(font); - appearanceStyle.setFontSize(fontSize); - - // Adobe Acrobat uses the font's bounding box for the leading between the lines - appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY); - - PlainTextFormatter formatter = new PlainTextFormatter - .Builder(contents) - .style(appearanceStyle) - .text(textContent) - .width(contentRect.getWidth()) - .wrapLines(isMultiLine()) - .initialOffset(x, y) - .textAlign(field.getQ()) - .build(); - formatter.format(); - } - - contents.endText(); - contents.restoreGraphicsState(); - contents.close(); } private AffineTransform calculateMatrix(PDRectangle bbox, int rotation) @@ -699,9 +699,10 @@ class AppearanceGeneratorHelper */ private void writeToStream(byte[] data, PDAppearanceStream appearanceStream) throws IOException { - OutputStream out = appearanceStream.getCOSObject().createOutputStream(); - out.write(data); - out.close(); + try (OutputStream out = appearanceStream.getCOSObject().createOutputStream()) + { + out.write(data); + } } /**