diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Back.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Back.java deleted file mode 100644 index 48b7ed86..00000000 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Back.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.kumoricon.registration.print.formatter.badgeimage; - - - -import org.kumoricon.registration.model.staff.StaffBadgeDTO; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -public class BadgeCreatorStaff2018Back implements BadgeCreatorStaff { - private static final int DPI = 300; - private static final int BADGE_WIDTH = (int) 4.25*DPI; // 4x6 inch badge with overprint - private static final int BADGE_HEIGHT = (int) 6.25*DPI; - private Font font; - - @Override - public byte[] createBadge(StaffBadgeDTO attendee) { - if (font == null) font = new Font("Dialog", Font.BOLD, 36); - BadgeImage b = new BadgeImage(BADGE_WIDTH, BADGE_HEIGHT, DPI); - - drawBadgeTypeStripe(b, attendee); - drawAgeColorStripe(b, attendee); - drawName(b, attendee); - drawBadgeImage(b, attendee); - - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - ImageIO.write(b.getImage(), "png", baos); - return baos.toByteArray(); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - - public byte[] createBadge(StaffBadgeDTO staff, Font font) { - this.font = font; - return createBadge(staff); - } - - private void drawBadgeImage(BadgeImage b, StaffBadgeDTO attendee) { - Rectangle badgeImageLocation = new Rectangle(280, 310, 660, 660); -// b.fillRect(badgeImageLocation, Color.LIGHT_GRAY); - b.drawStretchedImage(attendee.getBadgeImage(), badgeImageLocation); - } - - - @SuppressWarnings("Duplicates") - private void drawAgeColorStripe(BadgeImage b, StaffBadgeDTO attendee) { - Rectangle ageBackground = new Rectangle(0, 1560, BADGE_WIDTH, 225); - Color bgColor = Color.decode(attendee.getAgeBackgroundColor()); - Color fgColor = BadgeImage.getInverseColor(bgColor); - b.fillRect(ageBackground, bgColor); - b.drawStretchedCenteredString("STAFF", ageBackground, font, fgColor); - } - - private void drawName(BadgeImage b, StaffBadgeDTO attendee) { - Rectangle nameBg = new Rectangle(300, 1250, 650, 300); - - String[] names = {attendee.getFirstName(), attendee.getLastName()}; - b.drawCenteredStrings(names, nameBg, font, Color.BLACK); - } - - private void drawBadgeTypeStripe(BadgeImage b, StaffBadgeDTO attendee) { - if (attendee != null) { - Color bgColor = Color.decode(attendee.getDepartmentBackgroundColor()); - Color fgColor = BadgeImage.getInverseColor(bgColor); - Rectangle positionsBackground = new Rectangle(976, 0, 225, 1575); - b.fillRect(positionsBackground, bgColor); - - Rectangle textBounds = new Rectangle(990, 50, 150, 1475); - b.drawRotatedCenteredStrings(attendee.getPositions(), textBounds, font, fgColor, false); - } - } -} diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Front.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Front.java deleted file mode 100644 index 9a709612..00000000 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaff2018Front.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.kumoricon.registration.print.formatter.badgeimage; - - -import org.kumoricon.registration.model.staff.StaffBadgeDTO; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -public class BadgeCreatorStaff2018Front implements BadgeCreatorStaff { - private static final int DPI = 300; - private static final int BADGE_WIDTH = (int) 4.25*DPI; // 4x6 inch badge with overprint - private static final int BADGE_HEIGHT = (int) 6.25*DPI; - private Font font; - - @Override - public byte[] createBadge(StaffBadgeDTO attendee) { - if (font == null) { - font = new Font("Dialog", Font.BOLD, 36); - } - BadgeImage b = new BadgeImage(BADGE_WIDTH, BADGE_HEIGHT, DPI); - - drawBadgeTypeStripe(b, attendee); - drawAgeColorStripe(b, attendee); - drawName(b, attendee); - - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - ImageIO.write(b.getImage(), "png", baos); - return baos.toByteArray(); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - - } - - public byte[] createBadge(StaffBadgeDTO attendee, Font font) { - this.font = font; - return createBadge(attendee); - } - - @SuppressWarnings("Duplicates") - private void drawAgeColorStripe(BadgeImage b, StaffBadgeDTO attendee) { - Rectangle ageBackground = new Rectangle(0, 1560, BADGE_WIDTH, 225); - Color bgColor = Color.decode(attendee.getAgeBackgroundColor()); - Color fgColor = BadgeImage.getInverseColor(bgColor); - b.fillRect(ageBackground, bgColor); - - b.drawStretchedCenteredString("STAFF", ageBackground, font, fgColor); - } - - private void drawName(BadgeImage b, StaffBadgeDTO attendee) { - Rectangle nameBg = new Rectangle(235, 1250, 650, 300); - String[] names = {attendee.getFirstName(), attendee.getLastName()}; - b.drawCenteredStrings(names, nameBg, font, Color.BLACK); - } - - private void drawBadgeTypeStripe(BadgeImage b, StaffBadgeDTO attendee) { - if (attendee != null) { - Color bgColor = Color.decode(attendee.getDepartmentBackgroundColor()); - Color fgColor = BadgeImage.getInverseColor(bgColor); - Rectangle positionsBackground = new Rectangle(0, 0, 218, 1575); - b.fillRect(positionsBackground, bgColor); - - Rectangle textBounds = new Rectangle(45, 50, 150, 1475); - b.drawRotatedCenteredStrings(attendee.getPositions(), textBounds, font, fgColor, true); - } - } -} diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBack.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBack.java index e3d9ac2c..e6178202 100644 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBack.java +++ b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBack.java @@ -15,9 +15,8 @@ public byte[] createBadge(StaffBadgeDTO staff) { drawPositionsStripe(b, staff); drawLargeName(b, staff); drawPronouns(b, staff); - //drawBadgeAgeImage(b, staff); drawBadgeNumber(b, staff); - + return b.writePNGToByteArray(); } } diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBase.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBase.java index 0b447491..ef9f1027 100644 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBase.java +++ b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffBase.java @@ -1,7 +1,5 @@ package org.kumoricon.registration.print.formatter.badgeimage; - import org.kumoricon.registration.model.staff.StaffBadgeDTO; - import java.awt.*; import java.util.ArrayList; @@ -21,22 +19,16 @@ public BadgeCreatorStaffBase(Font boldFont, Font plainFont) { } // For 2023, an age image is not used (background color instead) - void drawBadgeAgeImage(BadgeImage b, StaffBadgeDTO staff) { -// Rectangle badgeImageLocation = new Rectangle(250, 290, 165, 413); - Rectangle badgeImageLocation = new Rectangle(160, 292, 165, 113); -// b.fillRect(badgeImageLocation, Color.RED); - b.drawImage(staff.getAgeImage(), badgeImageLocation); - } + // void drawBadgeAgeImage(BadgeImage b, StaffBadgeDTO staff) { + // Rectangle badgeImageLocation = new Rectangle(160, 292, 165, 113); + // b.drawImage(staff.getAgeImage(), badgeImageLocation); + // } void drawPositionsStripe(BadgeImage b, StaffBadgeDTO staff) { // Guests don't have a department color stripe, but we have to fill in the background color this // year. (In past years, it was part of the badge background) Color fgColor = positionForeground(staff.getDepartmentBackgroundColor()); Color bgColor = positionBackground(staff.getDepartmentBackgroundColor()); - // if (staff.getDepartmentBackgroundColor() != null && !staff.getDepartmentBackgroundColor().isBlank()) { - // bgColor = Color.decode(staff.getDepartmentBackgroundColor()); - // fgColor = BadgeImage.getInverseColor(bgColor); - // } Rectangle positionsBackground = new Rectangle(0, 625, 1200, 207); b.fillRect(positionsBackground, bgColor); @@ -46,15 +38,12 @@ void drawPositionsStripe(BadgeImage b, StaffBadgeDTO staff) { if (staffPositions != null) { Rectangle textBounds = null; - if (staffPositions.length == 1) { - textBounds = new Rectangle(80, 625, 1040, 207); - } - if (staffPositions.length == 2) { - textBounds = new Rectangle(80, 625, 1040, 207); + textBounds = new Rectangle(80, 605, 1040, 207); + } else { + textBounds = new Rectangle(80, 605, 1040, 207); } -// b.fillRect(textBounds, Color.CYAN); float maxFontSize = 72f; boolean yAxisCentering = true; b.drawCenteredStrings(staffPositions, textBounds, boldFont, fgColor, 1, maxFontSize, yAxisCentering); @@ -66,149 +55,57 @@ static Color positionForeground(String departmentBackgroundColor) { return Color.WHITE; } else { return Color.WHITE; - //return BadgeImage.getInverseColor(Color.decode(departmentBackgroundColor)); } } static Color positionBackground(String departmentBackgroundColor) { if (departmentBackgroundColor == null || departmentBackgroundColor.isEmpty()) { - return Color.decode("#E79B2"); + return Color.decode("#E69826"); } else { return Color.decode(departmentBackgroundColor); } } - // This is version "B", see below for the original void drawLargeName(BadgeImage b, StaffBadgeDTO staff) { // Staff don't have fan names, but guests do, so add that to the list if it's not null. // Also, some guests apparently don't have a last name entered. // Draw the background with the age color Color bgColor = Color.decode(staff.getAgeBackgroundColor()); - // if (staff.getDepartmentBackgroundColor() != null && !staff.getDepartmentBackgroundColor().isBlank()) { - // bgColor = positionBackground(staff.getDepartmentBackgroundColor()); - // } - Rectangle positionsBackground = new Rectangle(0, 1512, 1200, 150); - if (staff.getDepartment() == null) { - //guest badge - b.fillRect(positionsBackground, Color.decode("#A63439")); - } else { - b.fillRect(positionsBackground, Color.decode("#463161")); - } - - - Rectangle positionsBackground2 = new Rectangle(0, 1662, 1200, 150); - b.fillRect(positionsBackground2, bgColor); - - Color fgColor = foregroundColorForName(staff); - String[] names = buildNameList(staff); - Rectangle line1 = null; - //Rectangle line2 = null; - - //if (staff.getPreferredPronoun() == null || staff.getPreferredPronoun().isBlank()) { - // No pronoun - - if (names.length == 1) { - line1 = new Rectangle(80, 1515, 1020, 200); - float maxFontSize = 0.0f; - b.drawStretchedCenteredString(names[0], line1, boldFont, fgColor, 1, maxFontSize); - } else if (names.length == 2) { - line1 = new Rectangle(80, 1515, 1020, 200); - //line2 = new Rectangle(80, 1630, 1000, 140); - float maxFontSize = 0.0f; - b.drawStretchedCenteredString(names[0] + ' ' + names[1], line1, boldFont, fgColor, 1, maxFontSize); - //b.drawStretchedCenteredString(names[1], line2, boldFont, fgColor, 1); - } - - //} - // else { - // // Pronouns - // if (names.length == 1) { - // line1 = new Rectangle(80, 1530, 1020, 200); - // float maxFontSize = 0.0f; - // b.drawStretchedCenteredString(names[0], line1, boldFont, fgColor, 1, maxFontSize); - // } else if (names.length == 2) { - // line1 = new Rectangle(80, 1530, 1020, 200); - // //line2 = new Rectangle(80, 1630, 1000, 140); - // float maxFontSize = 0.0f; - // b.drawStretchedCenteredString(names[0] + ' ' + names[1], line1, boldFont, fgColor, 0, maxFontSize); - // //b.drawStretchedCenteredString(names[1], line2, boldFont, fgColor, 1); - // } - // } -// if (line1 != null) b.fillRect(line1, Color.RED); -// if (line2 != null) b.fillRect(line2, Color.ORANGE); - } - - void drawLargeNameA(BadgeImage b, StaffBadgeDTO staff) { - // Staff don't have fan names, but guests do, so add that to the list if it's not null. - // Also, some guests apparently don't have a last name entered. - - // Draw the background with the age color - Color bgColor = Color.decode(staff.getAgeBackgroundColor()); - // if (staff.getDepartmentBackgroundColor() != null && !staff.getDepartmentBackgroundColor().isBlank()) { - // bgColor = positionBackground(staff.getDepartmentBackgroundColor()); - // } Rectangle positionsBackground = new Rectangle(0, 1513, 1200, 390); b.fillRect(positionsBackground, bgColor); Color fgColor = foregroundColorForName(staff); String[] names = buildNameList(staff); Rectangle line1 = null; - //Rectangle line2 = null; - - //if (staff.getPreferredPronoun() == null || staff.getPreferredPronoun().isBlank()) { - // No pronoun if (names.length == 1) { - line1 = new Rectangle(80, 1530, 1020, 200); + line1 = new Rectangle(80, 1495, 1020, 200); float maxFontSize = 0.0f; b.drawStretchedCenteredString(names[0], line1, boldFont, fgColor, 1, maxFontSize); } else if (names.length == 2) { - line1 = new Rectangle(80, 1530, 1020, 200); - //line2 = new Rectangle(80, 1630, 1000, 140); + line1 = new Rectangle(80, 1495, 1020, 200); float maxFontSize = 0.0f; b.drawStretchedCenteredString(names[0] + ' ' + names[1], line1, boldFont, fgColor, 1, maxFontSize); - //b.drawStretchedCenteredString(names[1], line2, boldFont, fgColor, 1); } - - //} - // else { - // // Pronouns - // if (names.length == 1) { - // line1 = new Rectangle(80, 1530, 1020, 200); - // float maxFontSize = 0.0f; - // b.drawStretchedCenteredString(names[0], line1, boldFont, fgColor, 1, maxFontSize); - // } else if (names.length == 2) { - // line1 = new Rectangle(80, 1530, 1020, 200); - // //line2 = new Rectangle(80, 1630, 1000, 140); - // float maxFontSize = 0.0f; - // b.drawStretchedCenteredString(names[0] + ' ' + names[1], line1, boldFont, fgColor, 0, maxFontSize); - // //b.drawStretchedCenteredString(names[1], line2, boldFont, fgColor, 1); - // } - // } -// if (line1 != null) b.fillRect(line1, Color.RED); -// if (line2 != null) b.fillRect(line2, Color.ORANGE); } private static Color foregroundColorForName(StaffBadgeDTO staff) { Color fgColor; if (staff.getDepartment() == null) { // No department == Guest of Honor - fgColor = Color.WHITE; //Color.decode("#39296c"); + fgColor = Color.WHITE; } else { // Regular staff - fgColor = Color.WHITE; //Color.decode("#c3c2fe"); + fgColor = Color.WHITE; } return fgColor; } void drawPronouns(BadgeImage b, StaffBadgeDTO staff) { if (staff.getPreferredPronoun() != null && !staff.getPreferredPronoun().isBlank()) { - //Color fgColor = foregroundColorForName(staff); - Color bgColor = Color.decode(staff.getAgeBackgroundColor()); - Color fgColor = BadgeImage.getInverseColor(bgColor); + Color fgColor = Color.WHITE; Rectangle background = new Rectangle(810, 1675, 300, 70); -// b.fillRect(background,f Color.RED); Font pronounFont = plainFont.deriveFont(56f); b.drawRightAlignedString(staff.getPreferredPronoun(), background, pronounFont, fgColor, 0); } @@ -224,7 +121,7 @@ void drawBadgeNumber(BadgeImage b, StaffBadgeDTO staff) { bgColor = Color.decode(staff.getAgeBackgroundColor()); } - Color fgColor = BadgeImage.getInverseColor(bgColor); + Color fgColor = Color.WHITE; //BadgeImage.getInverseColor(bgColor); if (staff.getBadgeNumber() != null && !staff.getBadgeNumber().isBlank()) { Rectangle background = new Rectangle(70, 1685, 200, 60); b.drawStretchedCenteredString(staff.getBadgeNumber(), background, plainFont, fgColor, 0); diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffFront.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffFront.java index 07c114b2..c58e60eb 100644 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffFront.java +++ b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeCreatorStaffFront.java @@ -14,16 +14,14 @@ public byte[] createBadge(StaffBadgeDTO staff) { drawLargeName(b, staff); drawPositionsStripe(b, staff); drawPronouns(b, staff); - //drawBadgeAgeImage(b, staff); // Not used for 2023 drawBadgeNumber(b, staff); drawBadgeImage(b, staff); return b.writePNGToByteArray(); } - private void drawBadgeImage(BadgeImage b, StaffBadgeDTO staff) { - Rectangle badgeImageLocation = new Rectangle(380, 850, 450, 510); -// b.fillRect(badgeImageLocation, Color.CYAN); + private void drawBadgeImage(BadgeImage b, StaffBadgeDTO staff) { + Rectangle badgeImageLocation = new Rectangle(80, 850, 1040, 510); b.drawStretchedImage(staff.getBadgeImage(), badgeImageLocation); } } diff --git a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeImage.java b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeImage.java index 2752edd3..e0a2a305 100644 --- a/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeImage.java +++ b/src/main/java/org/kumoricon/registration/print/formatter/badgeimage/BadgeImage.java @@ -13,6 +13,7 @@ * and text placement, plus the image itself. */ public class BadgeImage { + //private static final Logger log = LoggerFactory.getLogger(BadgeImage.class); private final BufferedImage image; private final Graphics2D g2; private final int dpi; @@ -39,27 +40,66 @@ public class BadgeImage { void drawImage(Image image, Rectangle area) { if (image == null) return; + g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2.drawImage(image, area.x, area.y, area.width, area.height, null); } - void drawStretchedImage(Image image, Rectangle area) { + void drawStretchedImage(Image image, Rectangle boundingArea) { if (image == null) return; - double imageWidth = image.getWidth(null); - double imageHeight = image.getHeight(null); - - double widthRatio = area.getWidth() / imageWidth; - double heightRatio = area.getHeight() / imageHeight; - double ratio = Math.min(widthRatio, heightRatio); - - int newWidth = (int) (imageWidth * ratio); - int newHeight = (int) (imageHeight * ratio); - - Rectangle scaledArea = new Rectangle( - area.x + (area.width - newWidth)/2, - area.y + (area.height - newHeight)/2, - newWidth, - newHeight); - drawImage(image, scaledArea); + int imageWidth = image.getWidth(null); + int imageHeight = image.getHeight(null); + + Dimension imageDimension = new Dimension(imageWidth, imageHeight); + Dimension boundaryDimension = new Dimension(boundingArea.width, boundingArea.height); + Dimension scaledDimension = getScaledDimension(imageDimension, boundaryDimension); + + Rectangle imageArea = new Rectangle( + boundingArea.x, + boundingArea.y, + scaledDimension.width, + scaledDimension.height + ); + + int heightDifference = boundingArea.height - imageArea.height; + int widthDifference = boundingArea.width - imageArea.width; + + Rectangle centeredImage = new Rectangle( + boundingArea.x + (widthDifference/2), + boundingArea.y + (heightDifference/2), + imageArea.width, + imageArea.height + ); + + drawImage(image, centeredImage); + } + + public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) { + int original_width = imgSize.width; + int original_height = imgSize.height; + int bound_width = boundary.width; + int bound_height = boundary.height; + int new_width = original_width; + int new_height = original_height; + + // first check if we need to scale width + if (original_width > bound_width) { + //scale width to fit + new_width = bound_width; + //scale height to maintain aspect ratio + new_height = (new_width * original_height) / original_width; + } + + // then check if we need to scale even with the new height + if (new_height > bound_height) { + //scale height to fit instead + new_height = bound_height; + //scale width to maintain aspect ratio + // The '* 125 / 100' part is an embarrassing hack to make scaled images 25% wider + // since I couldn't figure out what was shrinking them by 25% with the amount of time I had + new_width = (new_height * original_width * 125 / 100) / original_height; + } + + return new Dimension(new_width, new_height); } /** @@ -80,7 +120,6 @@ void drawStretchedCenteredString(String text, Rectangle rect, Font font, Color c drawCenteredString(text, rect, sizedFont, color, outlineWidth); } - @SuppressWarnings("SuspiciousNameCombination") void drawRotatedStretchedCenteredString(String text, Rectangle rect, Font font, Color color, float maxFontSize) { Rectangle rotatedBounds = new Rectangle(rect.x, rect.y, rect.height, rect.width); @@ -159,7 +198,6 @@ void drawTextOutline(String text, Color color, int x, int y, int width) { g2.drawString(text, x+width, y+width); } - void drawLeftAlignedString(String text, Rectangle rect, Font font, Color color) { drawLeftAlignedString(text, rect, font, color, 0); } @@ -229,7 +267,6 @@ void fillRect(final Rectangle rectangle, final Color color) { g2.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); } - private Rectangle getPaddedRect(Rectangle rect) { if (rect.getHeight() > 40 && rect.getWidth() > 40) { return new Rectangle(rect.x+10, rect.y+10, rect.width-20, rect.height-20);