Author: tilman Date: Tue Aug 19 09:42:53 2025 New Revision: 1927876 Log: PDFBOX-5660: optimize, as suggested by Valery Bokov; closes #217
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Tue Aug 19 09:37:31 2025 (r1927875) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Tue Aug 19 09:42:53 2025 (r1927876) @@ -474,12 +474,15 @@ class AppearanceGeneratorHelper { } float padding = Math.max(1f, borderWidth); PDRectangle clipRect = applyPadding(bbox, padding); + float clipRectLowerLeftY = clipRect.getLowerLeftY(); + float clipRectHeight = clipRect.getHeight(); + PDRectangle contentRect = applyPadding(clipRect, padding); contents.saveGraphicsState(); // Acrobat always adds a clipping path - contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), clipRect.getWidth(), clipRect.getHeight()); + contents.addRect(clipRect.getLowerLeftX(), clipRectLowerLeftY, clipRect.getWidth(), clipRectHeight); contents.clip(); // get the font @@ -538,22 +541,28 @@ class AppearanceGeneratorHelper { fontDescentAtSize = fontDescent * fontScaleY; } - if (field instanceof PDTextField && ((PDTextField) field).isMultiline()) { + if (field instanceof PDTextField && ((PDTextField) field).isMultiline()) + { y = contentRect.getUpperRightY() - fontBoundingBoxAtSize; - } else { - // Adobe shows the text 'shifted up' in case the caps don't fit into the - // clipping area - if (fontCapAtSize > clipRect.getHeight()) { - y = clipRect.getLowerLeftY() + -fontDescentAtSize; - } else { + } + else + { + // Adobe shows the text 'shifted up' in case the caps don't fit into the clipping area + if (fontCapAtSize > clipRectHeight) + { + y = clipRectLowerLeftY + -fontDescentAtSize; + } + else + { // calculate the position based on the content rectangle - y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize) / 2; + y = clipRectLowerLeftY + (clipRectHeight - 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; + if (y - clipRectLowerLeftY < -fontDescentAtSize) + { + float contentRectLowerLeftY = contentRect.getLowerLeftY(); + float fontDescentBased = -fontDescentAtSize + contentRectLowerLeftY; + float fontCapBased = contentRect.getHeight() - contentRectLowerLeftY - fontCapAtSize; y = Math.min(fontDescentBased, Math.max(y, fontCapBased)); }