diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java index c0b87e1..2c65c00 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java @@ -23,10 +23,12 @@ */ package com.github.fabriciofx.cactoos.pdf; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Text; import java.io.ByteArrayOutputStream; import org.cactoos.Bytes; import org.cactoos.text.FormattedText; -import org.cactoos.text.UncheckedText; /** * Catalog. @@ -73,14 +75,14 @@ public Catalog(final int number, final int generation, final Pages pages) { } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); + } + + public Dictionary dictionary() throws Exception { + return new Dictionary() + .add("Type", new Name("Catalog")) + .add("Pages", new Text(this.pages.reference().asString())); } @Override @@ -88,12 +90,13 @@ public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - "%d %d obj\n<< /Type /Catalog /Pages %s >>\nendobj\n", + "%d %d obj\n", this.number, - this.generation, - this.pages.reference() + this.generation ).asString().getBytes() ); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); baos.write(this.pages.asBytes()); return baos.toByteArray(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Content.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Content.java index 7d7c685..8800b95 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Content.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Content.java @@ -23,6 +23,7 @@ */ package com.github.fabriciofx.cactoos.pdf; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; import org.cactoos.Bytes; /** @@ -39,4 +40,6 @@ public interface Content extends Object, Bytes { * @throws Exception if fails */ byte[] stream() throws Exception; + + Dictionary dictionary() throws Exception; } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java index 71915e2..3562220 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java @@ -99,9 +99,9 @@ public byte[] asBytes() throws Exception { baos.write( new FormattedText( "trailer << /Root %s /Size %d /Info %s >>\n", - this.catalog.reference(), + this.catalog.reference().asString(), this.count.value(), - this.information.reference() + this.information.reference().asString() ).asString().getBytes() ); baos.write(Document.EOF.getBytes()); diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java index 797a91a..66ae83c 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java @@ -79,14 +79,8 @@ public Information( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java index 4e2ae5b..8a26efa 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java @@ -33,5 +33,5 @@ public interface Object { * Object reference. * @return The object reference. */ - String reference(); + Reference reference(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java index 6379827..d059864 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java @@ -25,6 +25,7 @@ import com.github.fabriciofx.cactoos.pdf.content.Contents; import com.github.fabriciofx.cactoos.pdf.resource.Resources; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; /** * PageDefault. @@ -39,7 +40,7 @@ public interface Page extends Object { * @param parent Pages parent * @return The page dictionary */ - String dictionary(Pages parent); + Dictionary dictionary(Pages parent) throws Exception; /** * Page Resources. diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java index 6109093..4e76c78 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java @@ -23,6 +23,7 @@ */ package com.github.fabriciofx.cactoos.pdf; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; import org.cactoos.Bytes; /** @@ -38,4 +39,6 @@ public interface Pages extends Object, Bytes { * @param page An PDF page */ void add(Page page); + + Dictionary dictionary() throws Exception; } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Reference.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Reference.java new file mode 100644 index 0000000..cab5a2e --- /dev/null +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Reference.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (C) 2023 Fabrício Barros Cabral + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.fabriciofx.cactoos.pdf; + +import org.cactoos.Text; +import org.cactoos.text.FormattedText; + +public final class Reference implements Text { + private final int number; + private final int generation; + + public Reference(final int number, final int generation) { + this.number = number; + this.generation = generation; + } + + public int number() { + return this.number; + } + + public int generation() { + return this.generation; + } + + @Override + public String asString() throws Exception { + return new FormattedText( + "%d %d R", + this.number, + this.generation + ).asString(); + } +} diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Resource.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Resource.java index a5a62f0..6f9a3ad 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Resource.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Resource.java @@ -23,6 +23,7 @@ */ package com.github.fabriciofx.cactoos.pdf; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; import org.cactoos.Bytes; /** @@ -32,4 +33,5 @@ */ @SuppressWarnings("PMD.ExtendsObject") public interface Resource extends Bytes { + Dictionary dictionary() throws Exception; } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Contents.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Contents.java index 75aa6c2..a651c14 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Contents.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Contents.java @@ -25,9 +25,9 @@ import com.github.fabriciofx.cactoos.pdf.Content; import com.github.fabriciofx.cactoos.pdf.Object; +import com.github.fabriciofx.cactoos.pdf.Reference; import java.io.ByteArrayOutputStream; import java.util.List; -import java.util.stream.Collectors; import org.cactoos.Bytes; import org.cactoos.list.ListEnvelope; import org.cactoos.list.ListOf; @@ -58,10 +58,13 @@ public Contents(final List list) { } @Override - public String reference() { - return this.stream() - .map(Content::reference) - .collect(Collectors.joining(" ")); + public Reference reference() { + if (this.isEmpty()) { + throw new IllegalStateException( + "Contents is empty; there is not any reference" + ); + } + return this.get(0).reference(); } @Override diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/FlateEncode.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/FlateEncode.java index 3f3f047..2f4a928 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/FlateEncode.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/FlateEncode.java @@ -24,6 +24,11 @@ package com.github.fabriciofx.cactoos.pdf.content; import com.github.fabriciofx.cactoos.pdf.Content; +import com.github.fabriciofx.cactoos.pdf.Reference; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Stream; import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; @@ -74,23 +79,31 @@ public byte[] stream() throws Exception { } @Override - public String reference() { + public Dictionary dictionary() throws Exception { + final byte[] stream = this.stream(); + return new Dictionary() + .add("Length", new Int(stream.length)) + .add("Filter", new Name("FlateDecode")) + .with(new Stream(stream)); + } + + @Override + public Reference reference() { return this.origin.reference(); } @Override public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final byte[] stream = this.stream(); baos.write( new FormattedText( - "%s obj\n<< /Length %d /Filter /FlateDecode >>\nstream\n", - this.reference().replaceAll(" R", ""), - stream.length + "%d %d obj\n", + this.reference().number(), + this.reference().generation() ).asString().getBytes() ); - baos.write(stream); - baos.write("\nendstream\nendobj\n".getBytes()); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java index 081c7b2..580e806 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java @@ -2,6 +2,11 @@ import com.github.fabriciofx.cactoos.pdf.Content; import com.github.fabriciofx.cactoos.pdf.Count; +import com.github.fabriciofx.cactoos.pdf.Reference; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Stream; import java.io.ByteArrayOutputStream; import org.cactoos.text.FormattedText; import org.cactoos.text.Joined; @@ -47,14 +52,16 @@ public byte[] stream() throws Exception { } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Dictionary dictionary() throws Exception { + final byte[] stream = this.stream(); + return new Dictionary() + .add("Length", new Int(stream.length)) + .with(new Stream(stream)); + } + + @Override + public Reference reference() { + return new Reference(this.number, this.generation); } @Override @@ -62,14 +69,13 @@ public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - "%d %d obj\n<< /Length %d >>\nstream\n", + "%d %d obj\n", this.number, - this.generation, - this.stream().length + this.generation ).asString().getBytes() ); - baos.write(this.stream()); - baos.write("\nendstream\nendobj\n".getBytes()); + baos.write(this.dictionary().asBytes()); + baos.write("endobj\n".getBytes()); baos.write(this.png.asBytes()); return baos.toByteArray(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/PngImage.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/PngImage.java index 4633481..f986ed1 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/PngImage.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/PngImage.java @@ -23,32 +23,32 @@ */ package com.github.fabriciofx.cactoos.pdf.content; +import com.github.fabriciofx.cactoos.pdf.Content; import com.github.fabriciofx.cactoos.pdf.Count; -import com.github.fabriciofx.cactoos.pdf.Object; -import com.github.fabriciofx.cactoos.pdf.png.Body; +import com.github.fabriciofx.cactoos.pdf.Reference; import com.github.fabriciofx.cactoos.pdf.png.Header; import com.github.fabriciofx.cactoos.pdf.png.Img; import com.github.fabriciofx.cactoos.pdf.png.Palette; import com.github.fabriciofx.cactoos.pdf.png.PngImg; import com.github.fabriciofx.cactoos.pdf.png.SafePngImg; +import com.github.fabriciofx.cactoos.pdf.type.Array; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Stream; +import com.github.fabriciofx.cactoos.pdf.type.Text; import java.io.ByteArrayOutputStream; import java.io.File; import java.nio.file.Files; import org.cactoos.Bytes; import org.cactoos.text.FormattedText; -import org.cactoos.text.Joined; -import org.cactoos.text.UncheckedText; -public final class PngImage implements Object, Bytes { +public final class PngImage implements Content { private final int number; private final int generation; - private final Count count; - private final Bytes raw; + private final Img img; - public PngImage( - final Count count, - final String filename - ) { + public PngImage(final Count count, final String filename) { this( count.increment(), 0, @@ -61,67 +61,76 @@ public PngImage( final int number, final int generation, final Count count, - final Bytes raw + final Bytes bytes ) { this.number = number; this.generation = generation; - this.count = count; - this.raw = raw; + this.img = new SafePngImg(new PngImg(count, bytes)); } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override public byte[] asBytes() throws Exception { - final Img img = new SafePngImg(new PngImg(this.count, this.raw)); - final Header header = img.header(); - final Body body = img.body(); - final Palette palette = img.palette(); + final Palette palette = this.img.palette(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - new Joined( - "\n", - "%d %d obj", - "<< /Type /XObject", - "/Subtype /Image", - "/Width %d", - "/Height %d", - "/ColorSpace [/%s /DeviceRGB %d %s]", - "/BitsPerComponent %d", - "/Filter /FlateDecode", - "/DecodeParms << /Predictor 15 /Colors %d /BitsPerComponent %d /Columns %d >>", - "/Mask [0 0]", - "/Length %d >>", - "stream\n" - ), + "%d %d obj\n", this.number, - this.generation, - header.width(), - header.height(), - header.color().space(), - palette.stream().length / 3 - 1, - palette.reference(), - header.depth(), - header.color().space().equals("DeviceRGB") ? 3 : 1, - header.depth(), - header.width(), - body.stream().length + this.generation ).asString().getBytes() ); - baos.write(body.stream()); - baos.write("\nendstream\nendobj\n".getBytes()); - // Write the palette object + baos.write(this.dictionary().asBytes()); + baos.write("endobj\n".getBytes()); baos.write(palette.asBytes()); return baos.toByteArray(); } + + @Override + public byte[] stream() throws Exception { + return this.img.body().stream(); + } + + @Override + public Dictionary dictionary() throws Exception { + final Header header = this.img.header(); + final Palette palette = this.img.palette(); + final byte[] stream = this.stream(); + return new Dictionary() + .add("Type", new Name("XObject")) + .add("Subtype", new Name("Image")) + .add("Width", new Int(header.width())) + .add("Height", new Int(header.height())) + .add( + "ColorSpace", + new Array( + new Name(header.color().space()), + new Name("DeviceRGB"), + new Int(palette.stream().length / 3 - 1), + new Text(palette.reference().asString()) + ) + ) + .add("BitsPerComponent", new Int(header.depth())) + .add("Filter", new Name("FlateDecode")) + .add( + "DecodeParms", + new Dictionary() + .add("Predictor", new Int(15)) + .add( + "Colors", + new Int( + header.color().space().equals("DeviceRGB") ? 3 : 1 + ) + ) + .add("BitsPerComponent", new Int(header.depth())) + .add("Columns", new Int(header.width())) + ) + .add("Mask", new Array(new Int(0), new Int(0))) + .add("Length", new Int(stream.length)) + .with(new Stream(stream)); + } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Text.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Text.java index 2130998..a42ecf0 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Text.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/content/Text.java @@ -26,6 +26,11 @@ import com.github.fabriciofx.cactoos.pdf.Content; import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Escaped; +import com.github.fabriciofx.cactoos.pdf.Reference; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Stream; +import java.io.ByteArrayOutputStream; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -169,14 +174,8 @@ public Text( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override @@ -220,15 +219,26 @@ public byte[] stream() throws Exception { } @Override - public byte[] asBytes() throws Exception { + public Dictionary dictionary() throws Exception { final byte[] stream = this.stream(); - return new FormattedText( - "%d %d obj\n<< /Length %d >>\nstream\n%s\nendstream\nendobj\n", - this.number, - this.generation, - stream.length, - new String(stream) - ).asString().getBytes(); + return new Dictionary() + .add("Length", new Int(stream.length)) + .with(new Stream(stream)); + } + + @Override + public byte[] asBytes() throws Exception { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write( + new FormattedText( + "%d %d obj\n", + this.number, + this.generation + ).asString().getBytes() + ); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); + return baos.toByteArray(); } /** diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/page/DefaultPage.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/page/DefaultPage.java index ecd597a..7a416fd 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/page/DefaultPage.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/page/DefaultPage.java @@ -26,8 +26,12 @@ import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Page; import com.github.fabriciofx.cactoos.pdf.Pages; +import com.github.fabriciofx.cactoos.pdf.Reference; import com.github.fabriciofx.cactoos.pdf.content.Contents; import com.github.fabriciofx.cactoos.pdf.resource.Resources; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Text; import java.io.ByteArrayOutputStream; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -96,26 +100,17 @@ public DefaultPage( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override - public String dictionary(final Pages parent) { - return new UncheckedText( - new FormattedText( - "/Type /Page /Resources %s /Contents %s /Parent %s", - this.resources.reference(), - this.contents.reference(), - parent.reference() - ) - ).asString(); + public Dictionary dictionary(final Pages parent) throws Exception { + return new Dictionary() + .add("Type", new Name("Page")) + .add("Resources", new Text(this.resources.reference().asString())) + .add("Contents", new Text(this.contents.reference().asString())) + .add("Parent", new Text(parent.reference().asString())); } @Override @@ -133,12 +128,13 @@ public byte[] with(final Pages parent) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - "%d %d obj\n<< %s >>\nendobj\n", + "%d %d obj\n", this.number, - this.generation, - this.dictionary(parent) + this.generation ).asString().getBytes() ); + baos.write(this.dictionary(parent).asBytes()); + baos.write("\nendobj\n".getBytes()); baos.write(this.resources.asBytes()); baos.write(this.contents.asBytes()); return baos.toByteArray(); diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/page/Rotate.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/page/Rotate.java index 634d439..bae8d25 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/page/Rotate.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/page/Rotate.java @@ -25,8 +25,11 @@ import com.github.fabriciofx.cactoos.pdf.Page; import com.github.fabriciofx.cactoos.pdf.Pages; +import com.github.fabriciofx.cactoos.pdf.Reference; import com.github.fabriciofx.cactoos.pdf.content.Contents; import com.github.fabriciofx.cactoos.pdf.resource.Resources; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; import java.io.ByteArrayOutputStream; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -59,19 +62,14 @@ public Rotate(final Page page, final int angle) { } @Override - public String reference() { + public Reference reference() { return this.origin.reference(); } @Override - public String dictionary(final Pages parent) { - return new UncheckedText( - new FormattedText( - "%s /Rotate %d", - this.origin.dictionary(parent), - this.angle - ) - ).asString(); + public Dictionary dictionary(final Pages parent) throws Exception { + return this.origin.dictionary(parent) + .add("Rotate", new Int(this.angle)); } @Override @@ -89,11 +87,13 @@ public byte[] with(final Pages parent) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - "%s obj\n<< %s >>\nendobj\n", - this.origin.reference().replaceAll(" R", ""), - this.dictionary(parent) + "%d %d obj\n", + this.origin.reference().number(), + this.origin.reference().generation() ).asString().getBytes() ); + baos.write(this.dictionary(parent).asBytes()); + baos.write("\nendobj\n".getBytes()); baos.write(this.resources().asBytes()); baos.write(this.contents().asBytes()); return baos.toByteArray(); diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/pages/DefaultPages.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/pages/DefaultPages.java index 083bfe6..34bfb02 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/pages/DefaultPages.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/pages/DefaultPages.java @@ -26,7 +26,13 @@ import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Page; import com.github.fabriciofx.cactoos.pdf.Pages; +import com.github.fabriciofx.cactoos.pdf.Reference; import com.github.fabriciofx.cactoos.pdf.page.PageFormat; +import com.github.fabriciofx.cactoos.pdf.type.Array; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Name; +import com.github.fabriciofx.cactoos.pdf.type.Text; import java.io.ByteArrayOutputStream; import java.util.Arrays; import java.util.List; @@ -98,36 +104,22 @@ public DefaultPages( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final String kds = this.kids.stream() - .map(Page::reference) - .collect(Collectors.joining(" ")); baos.write( new FormattedText( - new Joined( - " ", - "%d %d obj\n<< /Type /Pages /Kids [%s]", - "/Count %d /MediaBox [0 0 %s] >>\nendobj\n" - ), + "%d %d obj\n", this.number, - this.generation, - kds, - this.kids.size(), - this.size.asString() + this.generation ).asString().getBytes() ); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); for (final Page page : this.kids) { baos.write(page.with(this)); } @@ -138,4 +130,22 @@ public byte[] asBytes() throws Exception { public void add(final Page page) { this.kids.add(page); } + + @Override + public Dictionary dictionary() throws Exception { + final String kds = this.kids.stream() + .map(page -> new UncheckedText(page.reference()).asString()) + .collect(Collectors.joining(" ")); + return new Dictionary() + .add("Type", new Name("Pages")) + .add("Kids", new Array(new Text(kds))) + .add("Count", new Int(this.kids.size())) + .add( + "MediaBox", + new Array( + new Int(0), + new Int(0), + new Text(this.size.asString())) + ); + } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/Header.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/Header.java index 0540428..1f483fc 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/Header.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/Header.java @@ -53,12 +53,12 @@ * - Other optional chunks for various purposes */ public interface Header extends Text, Bytes { - int length(); - int width(); - int height(); - int depth(); - Color color(); - int compression(); - int filter(); - int interlacing(); + int length() throws Exception; + int width() throws Exception; + int height() throws Exception; + int depth() throws Exception; + Color color() throws Exception; + int compression() throws Exception; + int filter() throws Exception; + int interlacing() throws Exception; } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngBody.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngBody.java index 391d587..bc20258 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngBody.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngBody.java @@ -25,7 +25,12 @@ import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Flow; +import com.github.fabriciofx.cactoos.pdf.Reference; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Stream; import java.io.ByteArrayOutputStream; +import org.cactoos.Bytes; import org.cactoos.Scalar; import org.cactoos.scalar.Sticky; import org.cactoos.text.FormattedText; @@ -36,13 +41,13 @@ public final class PngBody implements Body { private final int generation; private final Scalar bytes; - public PngBody(final Count count, final byte[] bytes) { + public PngBody(final Count count, final Bytes bytes) { this( count.increment(), 0, new Sticky<>( () -> { - final Flow flow = new Flow(bytes); + final Flow flow = new Flow(bytes.asBytes()); final ByteArrayOutputStream body = new ByteArrayOutputStream(); int len; flow.skip(33); @@ -75,14 +80,8 @@ public PngBody( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override @@ -90,20 +89,26 @@ public byte[] stream() throws Exception { return this.bytes.value(); } + @Override + public Dictionary dictionary() throws Exception { + final byte[] stream = this.stream(); + return new Dictionary() + .add("Length", new Int(stream.length)) + .with(new Stream(stream)); + } + @Override public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final byte[] stream = this.stream(); baos.write( new FormattedText( - "%d %d obj\n<< /Length %d >>\nstream\n", + "%d %d obj\n", this.number, - this.generation, - stream.length + this.generation ).asString().getBytes() ); - baos.write(stream); - baos.write("\nendstream\nendobj\n".getBytes()); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngHeader.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngHeader.java index e607e76..c338578 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngHeader.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngHeader.java @@ -24,6 +24,7 @@ package com.github.fabriciofx.cactoos.pdf.png; import java.util.Arrays; +import org.cactoos.Bytes; import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; import org.cactoos.text.Joined; @@ -55,62 +56,56 @@ * - Other optional chunks for various purposes */ public final class PngHeader implements Header { - private final byte[] bytes; + private final Bytes bytes; - public PngHeader(final byte[] bytes) { - this.bytes = Arrays.copyOfRange(bytes, 0, 33); + public PngHeader(final Bytes bytes) { + this.bytes = bytes; } @Override - public int length() { - return new Unchecked<>( - new BytesAsInteger( - Arrays.copyOfRange(this.bytes, 8, 12) - ) + public int length() throws Exception { + return new BytesAsInteger( + Arrays.copyOfRange(this.bytes.asBytes(), 8, 12) ).value(); } @Override - public int width() { - return new Unchecked<>( - new BytesAsInteger( - Arrays.copyOfRange(this.bytes, 16, 20) - ) + public int width() throws Exception { + return new BytesAsInteger( + Arrays.copyOfRange(this.bytes.asBytes(), 16, 20) ).value(); } @Override - public int height() { - return new Unchecked<>( - new BytesAsInteger( - Arrays.copyOfRange(this.bytes, 20, 24) - ) + public int height() throws Exception { + return new BytesAsInteger( + Arrays.copyOfRange(this.bytes.asBytes(), 20, 24) ).value(); } @Override - public int depth() { - return this.bytes[24]; + public int depth() throws Exception { + return this.bytes.asBytes()[24]; } @Override - public Color color() { - return new Color(this.bytes[25]); + public Color color() throws Exception { + return new Color(this.bytes.asBytes()[25]); } @Override - public int compression() { - return this.bytes[26]; + public int compression() throws Exception { + return this.bytes.asBytes()[26]; } @Override - public int filter() { - return this.bytes[27]; + public int filter() throws Exception { + return this.bytes.asBytes()[27]; } @Override - public int interlacing() { - return this.bytes[28]; + public int interlacing() throws Exception { + return this.bytes.asBytes()[28]; } @Override @@ -142,6 +137,6 @@ public String asString() throws Exception { @Override public byte[] asBytes() throws Exception { - return this.bytes; + return this.bytes.asBytes(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngImg.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngImg.java index b4793a5..3c3a1a9 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngImg.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngImg.java @@ -29,30 +29,32 @@ import org.cactoos.Bytes; public final class PngImg implements Img { - private final Count count; - private final Bytes bytes; + private final Header header; + private final Body body; + private final Palette palette; public PngImg(final Count count, final String filename) { this(count, () -> Files.readAllBytes(new File(filename).toPath())); } public PngImg(final Count count, final Bytes bytes) { - this.count = count; - this.bytes = bytes; + this.header = new PngHeader(bytes); + this.body = new PngBody(count, bytes); + this.palette = new PngPalette(count, bytes); } @Override public Header header() throws Exception { - return new PngHeader(this.bytes.asBytes()); + return this.header; } @Override public Body body() throws Exception { - return new PngBody(this.count, this.bytes.asBytes()); + return this.body; } @Override public Palette palette() throws Exception { - return new PngPalette(this.count, this.bytes.asBytes()); + return this.palette; } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngPalette.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngPalette.java index d596dad..aa2ca77 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngPalette.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/png/PngPalette.java @@ -25,7 +25,12 @@ import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Flow; +import com.github.fabriciofx.cactoos.pdf.Reference; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Int; +import com.github.fabriciofx.cactoos.pdf.type.Stream; import java.io.ByteArrayOutputStream; +import org.cactoos.Bytes; import org.cactoos.Scalar; import org.cactoos.scalar.Sticky; import org.cactoos.text.FormattedText; @@ -36,13 +41,13 @@ public final class PngPalette implements Palette { private final int generation; private final Scalar bytes; - public PngPalette(final Count count, final byte[] bytes) { + public PngPalette(final Count count, final Bytes bytes) { this( count.increment(), 0, new Sticky<>( () -> { - final Flow flow = new Flow(bytes); + final Flow flow = new Flow(bytes.asBytes()); int len; final ByteArrayOutputStream palette = new ByteArrayOutputStream(); flow.skip(33); @@ -75,14 +80,8 @@ public PngPalette( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); } @Override @@ -90,20 +89,26 @@ public byte[] stream() throws Exception { return this.bytes.value(); } + @Override + public Dictionary dictionary() throws Exception { + final byte[] stream = this.stream(); + return new Dictionary() + .add("Length", new Int(stream.length)) + .with(new Stream(stream)); + } + @Override public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final byte[] stream = this.stream(); baos.write( new FormattedText( - "%d %d obj\n<< /Length %d >>\nstream\n", + "%d %d obj\n", this.number, - this.generation, - stream.length + this.generation ).asString().getBytes() ); - baos.write(stream); - baos.write("\nendstream\nendobj\n".getBytes()); + baos.write(this.dictionary().asBytes()); + baos.write("endobj\n".getBytes()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Font.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Font.java index 0332df5..16b5f61 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Font.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Font.java @@ -24,7 +24,7 @@ package com.github.fabriciofx.cactoos.pdf.resource; import com.github.fabriciofx.cactoos.pdf.Resource; -import org.cactoos.text.FormattedText; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; /** * Font. @@ -58,10 +58,15 @@ public Font( @Override public byte[] asBytes() throws Exception { - return new FormattedText( - "/Font << /%s %s >>", - this.name, - new String(this.family.asBytes()) - ).asString().getBytes(); + return this.dictionary().asBytes(); + } + + @Override + public Dictionary dictionary() throws Exception { + return new Dictionary() + .add( + "Font", + new Dictionary().add(this.name, this.family.dictionary()) + ); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/FontFamily.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/FontFamily.java index 1cca415..595f0fe 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/FontFamily.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/FontFamily.java @@ -23,15 +23,16 @@ */ package com.github.fabriciofx.cactoos.pdf.resource; -import org.cactoos.Bytes; -import org.cactoos.text.FormattedText; +import com.github.fabriciofx.cactoos.pdf.Resource; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Name; /** * Font Family. * * @since 0.0.1 */ -public final class FontFamily implements Bytes { +public final class FontFamily implements Resource { /** * Font base. */ @@ -55,10 +56,14 @@ public FontFamily(final String base, final String subtype) { @Override public byte[] asBytes() throws Exception { - return new FormattedText( - "<< /Type /Font /BaseFont /%s /Subtype /%s >>", - this.base, - this.subtype - ).asString().getBytes(); + return this.dictionary().asBytes(); + } + + @Override + public Dictionary dictionary() throws Exception { + return new Dictionary() + .add("Type", new Name("Font")) + .add("BaseFont", new Name(this.base)) + .add("Subtype", new Name(this.subtype)); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/ProcSet.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/ProcSet.java index 58fa8ec..462952d 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/ProcSet.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/ProcSet.java @@ -1,10 +1,44 @@ +/* + * The MIT License (MIT) + * + * Copyright (C) 2023 Fabrício Barros Cabral + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.github.fabriciofx.cactoos.pdf.resource; import com.github.fabriciofx.cactoos.pdf.Resource; +import com.github.fabriciofx.cactoos.pdf.type.Array; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; public final class ProcSet implements Resource { @Override public byte[] asBytes() throws Exception { - return "/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]".getBytes(); + return this.dictionary().asBytes(); + } + + @Override + public Dictionary dictionary() throws Exception { + return new Dictionary() + .add( + "ProcSet", + new Array("PDF", "Text", "ImageB", "ImageC", "ImageI") + ); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Resources.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Resources.java index cd0c773..71721a0 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Resources.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Resources.java @@ -25,15 +25,14 @@ import com.github.fabriciofx.cactoos.pdf.Count; import com.github.fabriciofx.cactoos.pdf.Object; +import com.github.fabriciofx.cactoos.pdf.Reference; import com.github.fabriciofx.cactoos.pdf.Resource; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; import java.io.ByteArrayOutputStream; import java.util.List; -import java.util.stream.Collectors; -import org.cactoos.Bytes; import org.cactoos.list.ListEnvelope; import org.cactoos.list.ListOf; import org.cactoos.text.FormattedText; -import org.cactoos.text.UncheckedText; /** * Resources. @@ -41,7 +40,7 @@ * @since 0.0.1 */ public final class Resources extends ListEnvelope - implements Object, Bytes { + implements Object, Resource { /** * Object number. */ @@ -77,14 +76,17 @@ public Resources( } @Override - public String reference() { - return new UncheckedText( - new FormattedText( - "%d %d R", - this.number, - this.generation - ) - ).asString(); + public Reference reference() { + return new Reference(this.number, this.generation); + } + + @Override + public Dictionary dictionary() throws Exception { + Dictionary main = this.get(0).dictionary(); + for (int idx = 1; idx < this.size(); ++idx) { + main = main.merge(this.get(idx).dictionary()); + } + return main; } @Override @@ -92,15 +94,13 @@ public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( - "%d %d obj\n<<", + "%d %d obj\n", this.number, this.generation ).asString().getBytes() ); - for (final Bytes obj : this) { - baos.write(obj.asBytes()); - } - baos.write(" >>\nendobj\n".getBytes()); + baos.write(this.dictionary().asBytes()); + baos.write("\nendobj\n".getBytes()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/XObject.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/XObject.java index 96d129a..79ba924 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/XObject.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/resource/XObject.java @@ -1,9 +1,9 @@ package com.github.fabriciofx.cactoos.pdf.resource; import com.github.fabriciofx.cactoos.pdf.Resource; -import com.github.fabriciofx.cactoos.pdf.content.Image; import com.github.fabriciofx.cactoos.pdf.content.PngImage; -import org.cactoos.text.FormattedText; +import com.github.fabriciofx.cactoos.pdf.type.Dictionary; +import com.github.fabriciofx.cactoos.pdf.type.Text; public class XObject implements Resource { private final String name; @@ -16,10 +16,18 @@ public XObject(final String name, final PngImage png) { @Override public byte[] asBytes() throws Exception { - return new FormattedText( - "/XObject << /%s %s >>", - this.name, - this.png.reference() - ).asString().getBytes(); + return this.dictionary().asBytes(); + } + + @Override + public Dictionary dictionary() throws Exception { + return new Dictionary() + .add( + "XObject", + new Dictionary().add( + this.name, + new Text(this.png.reference().asString()) + ) + ); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/type/Stream.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/type/Stream.java index adfc9ae..b676698 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/type/Stream.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/type/Stream.java @@ -50,14 +50,14 @@ public byte[] asBytes() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write("stream\n".getBytes()); baos.write(this.value.asBytes()); - baos.write("\nendstream\n".getBytes()); + baos.write("\nendstream".getBytes()); return baos.toByteArray(); } @Override public String asString() throws Exception { return new FormattedText( - "stream\n%s\nendstream\n", + "\nstream\n%s\nendstream\n", new String(this.value.asBytes()) ).asString(); } diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java index f5ead94..1e4e563 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java @@ -308,7 +308,9 @@ void buildFileWithImage() throws Exception { ), new XObject(image.name(), png) ), - new Contents(image) + new Contents( + image + ) ) ) ) diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/page/PageTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/page/PageTest.java index 681c827..4d217fb 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/page/PageTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/page/PageTest.java @@ -23,26 +23,6 @@ */ package com.github.fabriciofx.cactoos.pdf.page; -import com.github.fabriciofx.cactoos.pdf.Catalog; -import com.github.fabriciofx.cactoos.pdf.Count; -import com.github.fabriciofx.cactoos.pdf.Date; -import com.github.fabriciofx.cactoos.pdf.Document; -import com.github.fabriciofx.cactoos.pdf.Information; -import com.github.fabriciofx.cactoos.pdf.content.Contents; -import com.github.fabriciofx.cactoos.pdf.content.Text; -import com.github.fabriciofx.cactoos.pdf.count.ObjectCount; -import com.github.fabriciofx.cactoos.pdf.pages.DefaultPages; -import com.github.fabriciofx.cactoos.pdf.resource.Font; -import com.github.fabriciofx.cactoos.pdf.resource.FontFamily; -import com.github.fabriciofx.cactoos.pdf.resource.Resources; -import org.cactoos.map.MapEntry; -import org.cactoos.map.MapOf; -import org.cactoos.text.Joined; -import org.cactoos.text.TextOf; -import org.junit.jupiter.api.Test; -import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.IsText; - /** * Test case for {@link com.github.fabriciofx.cactoos.pdf.Page}. * diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/FontTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/FontTest.java index 1b3ada5..960e9b5 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/FontTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/FontTest.java @@ -35,25 +35,23 @@ * @since 0.0.1 */ final class FontTest { -// @Test -// void build() throws Exception { -// new Assertion<>( -// "Must build a PDF Times-Roman font", -// new TextOf( -// new Font( -// 1, -// 0, -// new FontFamily("Times-Roman", "Type1"), -// "F0" -// ) -// ), -// new IsText( -// new Joined( -// " ", -// "1 0 obj\n<< /Font << /F0 << /Type /Font /BaseFont", -// "/Times-Roman /Subtype /Type1 >> >> >>\nendobj\n" -// ) -// ) -// ).affirm(); -// } + @Test + void build() throws Exception { + new Assertion<>( + "Must build a PDF Times-Roman font", + new TextOf( + new Font( + new FontFamily("Times-Roman", "Type1"), + "F0" + ) + ), + new IsText( + new Joined( + " ", + "<< /Font << /F0 << /Type /Font /BaseFont", + "/Times-Roman /Subtype /Type1 >> >> >>" + ) + ) + ).affirm(); + } } diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/ResourcesTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/ResourcesTest.java new file mode 100644 index 0000000..1c64fce --- /dev/null +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/resource/ResourcesTest.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (C) 2023 Fabrício Barros Cabral + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.fabriciofx.cactoos.pdf.resource; + +import com.github.fabriciofx.cactoos.pdf.Count; +import com.github.fabriciofx.cactoos.pdf.content.Image; +import com.github.fabriciofx.cactoos.pdf.content.PngImage; +import com.github.fabriciofx.cactoos.pdf.count.ObjectCount; +import org.cactoos.text.Joined; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.IsText; + +final class ResourcesTest { + @Test + void dictionary() throws Exception { + final Count count = new ObjectCount(); + final PngImage png = new PngImage( + count, + "src/test/resources/image/logo.png" + ); + final Image image = new Image( + count, + "I1", + png + ); + new Assertion<>( + "Must represent a resources dictionary", + new Resources( + count, + new ProcSet(), + new Font( + new FontFamily("Times-Roman", "Type1"), + "F1" + ), + new XObject(image.name(), png) + ).dictionary(), + new IsText( + new Joined( + " ", + "<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]", + "/Font << /F1 << /Type /Font /BaseFont /Times-Roman /Subtype", + "/Type1 >> >> /XObject << /I1 1 0 R >> >>" + ) + ) + ).affirm(); + } +}