Skip to content

Commit 0f3a6d4

Browse files
committed
chore: sonarcloud fixes
1 parent 9f70c94 commit 0f3a6d4

1 file changed

Lines changed: 37 additions & 23 deletions

File tree

openpdf-renderer/src/main/java/org/openpdf/renderer/core/OpenPdfCorePageRenderer.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@
4040
import org.openpdf.text.pdf.PdfString;
4141

4242
/**
43-
* Renders a single PDF page to a {@link Graphics2D} surface, parsing the page's
44-
* content stream with {@code openpdf-core}'s {@link PdfContentParser} and
45-
* dispatching the resulting operators to Java2D drawing calls.
43+
* Renders a single PDF page to a {@link Graphics2D} surface, parsing the page's content stream with
44+
* {@code openpdf-core}'s {@link PdfContentParser} and dispatching the resulting operators to Java2D drawing calls.
4645
*
4746
* <p>This is the {@code openpdf-core}-driven Java2D rasterizer that backs
4847
* {@link OpenPdfCoreRenderer#renderPage(int, float)}.</p>
@@ -79,7 +78,9 @@ final class OpenPdfCorePageRenderer {
7978

8079
private static final Logger LOG = Logger.getLogger(OpenPdfCorePageRenderer.class.getName());
8180

82-
/** Default user-space resolution of a PDF, in DPI. */
81+
/**
82+
* Default user-space resolution of a PDF, in DPI.
83+
*/
8384
private static final float PDF_USER_SPACE_DPI = 72f;
8485

8586
private final Graphics2D g2;
@@ -115,8 +116,7 @@ private OpenPdfCorePageRenderer(Graphics2D g2, PdfDictionary resources) {
115116
}
116117

117118
/**
118-
* Renders the given page to {@code g2}, sized to {@code targetWidth x targetHeight}
119-
* pixels at the requested DPI.
119+
* Renders the given page to {@code g2}, sized to {@code targetWidth x targetHeight} pixels at the requested DPI.
120120
*
121121
* @param reader the open PDF
122122
* @param pageNumber 1-based page number
@@ -198,8 +198,10 @@ private void processContent(byte[] contentBytes) throws IOException {
198198
}
199199
}
200200

201-
/** Dispatches one operator. Operands include the trailing operator literal at index size-1. */
202-
private void dispatch(String op, List<PdfObject> operands) throws IOException {
201+
/**
202+
* Dispatches one operator. Operands include the trailing operator literal at index size-1.
203+
*/
204+
private void dispatch(String op, List<PdfObject> operands) {
203205
switch (op) {
204206
// --- Graphics state ---
205207
case "q":
@@ -303,8 +305,7 @@ private void dispatch(String op, List<PdfObject> operands) throws IOException {
303305
strokePath();
304306
resetPath();
305307
break;
306-
case "f":
307-
case "F":
308+
case "f", "F":
308309
fillPath(Path2D.WIND_NON_ZERO);
309310
resetPath();
310311
break;
@@ -530,23 +531,33 @@ private Font mapFont(CMapAwareDocumentFont docFont, float size) {
530531
String name = docFont.getPostscriptFontName();
531532
if (name != null) {
532533
String lower = name.toLowerCase();
533-
if (lower.contains("mono") || lower.contains("courier")) {
534-
family = Font.MONOSPACED;
535-
} else if (lower.contains("sans") || lower.contains("helvetica")
536-
|| lower.contains("arial")) {
537-
family = Font.SANS_SERIF;
538-
}
539-
if (lower.contains("bold")) {
540-
style |= Font.BOLD;
541-
}
542-
if (lower.contains("italic") || lower.contains("oblique")) {
543-
style |= Font.ITALIC;
544-
}
534+
family = detectFontFamily(lower, family);
535+
style = detectStyles(lower, style);
545536
}
546537
}
547538
return new Font(family, style, 1).deriveFont(Math.max(size, 0.1f));
548539
}
549540

541+
private static int detectStyles(String lower, int style) {
542+
if (lower.contains("bold")) {
543+
style |= Font.BOLD;
544+
}
545+
if (lower.contains("italic") || lower.contains("oblique")) {
546+
style |= Font.ITALIC;
547+
}
548+
return style;
549+
}
550+
551+
private static String detectFontFamily(String lower, String family) {
552+
if (lower.contains("mono") || lower.contains("courier")) {
553+
family = Font.MONOSPACED;
554+
} else if (lower.contains("sans") || lower.contains("helvetica")
555+
|| lower.contains("arial")) {
556+
family = Font.SANS_SERIF;
557+
}
558+
return family;
559+
}
560+
550561
private CMapAwareDocumentFont lookupFont(String name) {
551562
if (resources == null) {
552563
return null;
@@ -597,8 +608,11 @@ private static float clamp01(float v) {
597608
return v;
598609
}
599610

600-
/** Mutable per-graphics-state snapshot. Not thread-safe. */
611+
/**
612+
* Mutable per-graphics-state snapshot. Not thread-safe.
613+
*/
601614
private static final class GState {
615+
602616
Color fillColor = Color.BLACK;
603617
Color strokeColor = Color.BLACK;
604618
float lineWidth = 1.0f;

0 commit comments

Comments
 (0)