Skip to content

Commit

Permalink
(#73) Add coordinates and size parameters to Image
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 30, 2023
1 parent 745b734 commit cf36952
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
91 changes: 89 additions & 2 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.github.fabriciofx.cactoos.pdf.type.Int;
import com.github.fabriciofx.cactoos.pdf.type.Stream;
import java.io.ByteArrayOutputStream;
import org.cactoos.Scalar;
import org.cactoos.text.FormattedText;

/**
Expand All @@ -49,16 +50,98 @@ public final class Image implements Content {
*/
private final Png png;

/**
* Position X.
*/
private final double posx;

/**
* Position Y.
*/
private final double posy;

/**
* Width.
*/
private final Scalar<Double> width;

/**
* Height.
*/
private final Scalar<Double> height;

/**
* Ctor.
*
* @param name Image name
* @param png Raw PNG
* @param posx Position X
* @param posy Position Y
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Image(
final String name,
final Png png,
final double posx,
final double posy
) {
this(
name,
png,
posx,
posy,
() -> (double) png.width(),
() -> (double) png.height()
);
}

/**
* Ctor.
*
* @param name Image name
* @param png Raw PNG
* @param posx Position X
* @param posy Position Y
* @param width Image width
* @param height Image height
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Image(
final String name,
final Png png,
final double posx,
final double posy,
final double width,
final double height
) {
this(name, png, posx, posy, () -> width, () -> height);
}

/**
* Ctor.
*
* @param name Image name
* @param png Raw PNG
* @param posx Position X
* @param posy Position Y
* @param width Image width
* @param height Image height
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Image(final String name, final Png png) {
public Image(
final String name,
final Png png,
final double posx,
final double posy,
final Scalar<Double> width,
final Scalar<Double> height
) {
this.label = name;
this.png = png;
this.posx = posx;
this.posy = posy;
this.width = width;
this.height = height;
}

/**
Expand All @@ -73,7 +156,11 @@ public String name() {
@Override
public byte[] asStream() throws Exception {
return new FormattedText(
"q 85.04 0 0 58.06 28.35 766.83 cm /%s Do Q",
"q %.2f 0 0 %.2f %.2f %.2f cm /%s Do Q",
this.width.value(),
this.height.value(),
this.posx,
this.posy,
this.label
).asString().getBytes();
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/content/Png.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ public Png(final Bytes bytes) {
this.raw = new SafePngRaw(new PngRaw(bytes));
}

/**
* Image width.
*
* @return Image width in pixels
* @throws Exception if fails
*/
public int width() throws Exception {
return this.raw.header().width();
}

/**
* Image height.
*
* @return Image height in pixels
* @throws Exception if fails
*/
public int height() throws Exception {
return this.raw.header().height();
}

@Override
public Definition definition(final Id id) throws Exception {
final int num = id.increment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ void buildDocumentWithPngImage() throws Exception {
"I1",
new Png(
"src/test/resources/image/logo.png"
)
),
28,
766
);
final byte[] actual = new Document(
new Information(
Expand Down Expand Up @@ -175,7 +177,9 @@ void buildFileWithPngImage() throws Exception {
"I1",
new Png(
"src/test/resources/image/logo.png"
)
),
28,
766
);
final File file = new File("image-png.pdf");
Files.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void definition() throws Exception {
);
final Image image = new Image(
"I1",
png
png,
28,
766
);
new Assertion<>(
"Must represent a resources definition",
Expand Down
Binary file modified src/test/resources/document/image-png.pdf
Binary file not shown.

0 comments on commit cf36952

Please sign in to comment.