Skip to content

Commit

Permalink
Add maxHeight for images
Browse files Browse the repository at this point in the history
To be able to limit images by height, an
additional `maxHeight` parameter is added
to the `@Image` annotation.
During processing, both `maxHeight` and
`maxWidth` are used to find the maximum scale
factor, which is then applied.
  • Loading branch information
AntonOellerer committed Jun 26, 2024
1 parent efcb5fd commit 51d13c7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '4.0.0'
version = '4.1.0'

java {
toolchain {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/docutools/jocument/annotations/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@

/**
* The maximum pixel width the image should be inserted with. If the image is smaller won't affect it.
* A value of 0 is not permitted and will be ignored.
*
* @return the maximum pixel width of the image to be inserted
*/
int maxWidth() default -1;

/**
* The maximum pixel height the image should be inserted with. If the image is smaller won't affect it.
* A value of 0 is not permitted and will be ignored.
*
* @return the maximum pixel height of the image to be inserted
*/
int maxHeight() default -1;

/**
* Whether the resolved image should be deleted from the file system after insertion.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,10 @@ private Optional<PlaceholderData> resolveSimplePlaceholder(Object property, Stri
return formatTemporal(placeholderName, temporal, locale);
} else if (property instanceof Path path && isFieldAnnotatedWith(bean.getClass(), placeholderName, Image.class)) {
return ReflectionUtils.findFieldAnnotation(bean.getClass(), placeholderName, Image.class)
.map(image -> new ImagePlaceholderData(path).withMaxWidth(image.maxWidth()).withFileDeletionAfterInsertion(image.deleteAfterInsertion()));
.map(image -> new ImagePlaceholderData(path)
.withMaxWidth(image.maxWidth())
.withMaxHeight(image.maxHeight())
.withFileDeletionAfterInsertion(image.deleteAfterInsertion()));
} else if (property instanceof UUID uuid) {
return Optional.of(new ScalarPlaceholderData<>(uuid.toString()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,44 @@ public class ImagePlaceholderData extends CustomWordPlaceholderData {
private final Path imagePath;

// Options
private int maxWidth;
private int maxWidth = -1;
private int maxHeight = -1;
private boolean deleteAfterInsertion;

public ImagePlaceholderData(Path imagePath) {
this.imagePath = imagePath;
}

/**
* Set maximum width of the image. A width of 0 is not permitted and will be ignored.
*
* @param maxWidth The maximum width the image should have.
* @return the image placeholder data with the maxWidth applied (if != 0)
*/
public ImagePlaceholderData withMaxWidth(int maxWidth) {
if (maxWidth == 0) {
logger.warn("A max width of 0 is not permitted");
return this;
}
this.maxWidth = maxWidth;
return this;
}

/**
* Set max height of the image. A height of 0 is not permitted and will be ignored.
*
* @param maxHeight The maximum height the image should have.
* @return the image placeholder data with the maxHeight applied (if != 0)
*/
public ImagePlaceholderData withMaxHeight(int maxHeight) {
if (maxHeight == 0) {
logger.warn("A max height of 0 is not permitted");
return this;
}
this.maxHeight = maxHeight;
return this;
}

public ImagePlaceholderData withFileDeletionAfterInsertion(boolean deleteAfterInsertion) {
this.deleteAfterInsertion = deleteAfterInsertion;
return this;
Expand Down Expand Up @@ -69,9 +95,9 @@ protected void transform(IBodyElement placeholder, IBody part, Locale locale, Ge
private Path applyOptions(GenerationOptions options) {
try {
var image = options.imageStrategy().load(imagePath);
if (maxWidth > 0 && image.getWidth() > maxWidth) {
double scale = (double) maxWidth / image.getWidth();
var resized = options.imageStrategy().scale(image, scale);
double scale = Math.max(image.getWidth() / (double) maxWidth, image.getHeight() / (double) maxHeight);
if (scale > 1.0) {
var resized = options.imageStrategy().scale(image, 1 / scale);
image.close();
image = resized;
}
Expand Down

0 comments on commit 51d13c7

Please sign in to comment.