Skip to content

Commit

Permalink
(#43) Add Page Rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 13, 2023
1 parent cd9aad8 commit c87a68b
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 45 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,36 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import com.github.fabriciofx.cactoos.pdf.content.Contents;
import com.github.fabriciofx.cactoos.pdf.resource.Resources;

/**
* PageDefault.
*
* @since 0.0.1
*/
@SuppressWarnings("PMD.ExtendsObject")
public interface Page extends Object {
/**
* Build the Page dictionary.
*
* @param parent Pages parent
* @return The page dictionary
*/
String dictionary(Pages parent);

/**
* Page Resources.
* @return Resources
*/
Resources resources();

/**
* Page Contents.
* @return Contents
*/
Contents contents();

/**
* Build a PDF page.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import com.github.fabriciofx.cactoos.pdf.resource.Resources;
import java.io.ByteArrayOutputStream;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.UncheckedText;

/**
* PageDefault.
*
* @since 0.0.1
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
public final class DefaultPage implements Page {
/**
* Object number.
Expand Down Expand Up @@ -106,21 +106,37 @@ public String reference() {
).asString();
}

@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();
}

@Override
public Resources resources() {
return this.resources;
}

@Override
public Contents contents() {
return this.contents;
}

@Override
public byte[] with(final Pages parent) throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(
new FormattedText(
new Joined(
" ",
"%d %d obj\n<< /Type /Page /Resources %s",
"/Contents %s /Parent %s >>\nendobj\n"
),
"%d %d obj\n<< %s >>\nendobj\n",
this.number,
this.generation,
this.resources.reference(),
this.contents.reference(),
parent.reference()
this.dictionary(parent)
).asString().getBytes()
);
baos.write(this.resources.asBytes());
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/page/Rotate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.page;

import com.github.fabriciofx.cactoos.pdf.Page;
import com.github.fabriciofx.cactoos.pdf.Pages;
import com.github.fabriciofx.cactoos.pdf.content.Contents;
import com.github.fabriciofx.cactoos.pdf.resource.Resources;
import java.io.ByteArrayOutputStream;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Rotate a Page in a angle.
*
* @since 0.0.1
*/
public final class Rotate implements Page {
/**
* The Page.
*/
private final Page origin;

/**
* Angle.
*/
private final int angle;

/**
* Ctor.
*
* @param page The Page
* @param angle The angle
*/
public Rotate(final Page page, final int angle) {
this.origin = page;
this.angle = angle;
}

@Override
public String 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();
}

@Override
public Resources resources() {
return this.origin.resources();
}

@Override
public Contents contents() {
return this.origin.contents();
}

@Override
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)
).asString().getBytes()
);
baos.write(this.resources().asBytes());
baos.write(this.contents().asBytes());
return baos.toByteArray();
}
}
154 changes: 118 additions & 36 deletions src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github.fabriciofx.cactoos.pdf.content.Text;
import com.github.fabriciofx.cactoos.pdf.page.DefaultPage;
import com.github.fabriciofx.cactoos.pdf.page.PageFormat;
import com.github.fabriciofx.cactoos.pdf.page.Rotate;
import com.github.fabriciofx.cactoos.pdf.pages.DefaultPages;
import com.github.fabriciofx.cactoos.pdf.resource.Font;
import com.github.fabriciofx.cactoos.pdf.resource.FontFamily;
Expand Down Expand Up @@ -123,6 +124,84 @@ void buildDocument() throws Exception {
).affirm();
}

@Test
void buildDocumentWithRotatePage() throws Exception {
final Count count = new ObjectCount();
final Date date = new Date(2023, 12, 11, 20, 11, 32, "Etc/GMT-3");
new Assertion<>(
"Must represent a PDF document",
new TextOf(
new Document(
count,
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabricio Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>(
"Keywords",
"cactoos pdf elegant objects"
)
)
),
new Catalog(
count,
new DefaultPages(
count,
PageFormat.A4,
new Rotate(
new DefaultPage(
count,
new Resources(
new Font(
count,
new FontFamily(
"Times-Roman",
"Type1"
),
"F1"
)
),
new Contents(
new Text(
count,
18,
0,
0,
new TextOf(
"Hello World!"
)
)
)
),
90
)
)
)
)
),
new IsText(
new Joined(
"\n",
"%PDF-1.3\n%���������",
"1 0 obj\n<< /Title (Hello World) /Subject (PDF document) /Author (Fabricio Cabral) /Creator (cactoos-pdf) /Producer (cactoos-pdf) /CreationDate (D:20231211201132+03'00') /ModDate (D:20231211201132+03'00') /Keywords (cactoos pdf elegant objects) >>\nendobj",
"6 0 obj\n<< /Type /Catalog /Pages 5 0 R >>\nendobj",
"5 0 obj\n<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 595.28 841.89] >>\nendobj",
"4 0 obj\n<< /Type /Page /Resources 2 0 R /Contents 3 0 R /Parent 5 0 R /Rotate 90 >>\nendobj",
"2 0 obj\n<< /Font << /F1 << /Type /Font /BaseFont /Times-Roman /Subtype /Type1 >> >> >>\nendobj",
"3 0 obj\n<< /Length 40 >>\nstream\nBT /F1 18 Tf 0 0 Td\n(Hello World!) Tj\nET\nendstream\nendobj",
"trailer << /Root 6 0 R /Size 7 /Info 1 0 R >>",
"%%EOF"
)
)
).affirm();
}

@Test
void buildMultiTextDocument() throws Exception {
final Count count = new ObjectCount();
Expand Down Expand Up @@ -488,46 +567,49 @@ void buildEncodedFile() throws Exception {
new DefaultPages(
count,
PageFormat.A4,
new DefaultPage(
count,
new Resources(
new Font(
count,
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Contents(
new FlateEncode(
new Text(
new Rotate(
new DefaultPage(
count,
new Resources(
new Font(
count,
18,
0,
500,
80,
20,
new Joined(
" ",
"Lorem ea et aliquip culpa aute",
"amet elit nostrud culpa veniam",
"dolore eu irure incididunt.",
"Velit officia occaecat est",
"adipisicing mollit veniam.",
"Minim sunt est culpa labore.",
"Ut culpa et nulla sunt labore",
"aliqua ipsum laborum nostrud sit",
"deserunt officia labore. Sunt",
"laboris id labore sit ex. Eiusmod",
"nulla eu incididunt excepteur",
"minim officia dolore veniam",
"labore enim quis reprehenderit.",
"Magna in laboris irure enim non",
"deserunt laborum mollit labore",
"id amet."
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Contents(
new FlateEncode(
new Text(
count,
18,
0,
500,
80,
20,
new Joined(
" ",
"Lorem ea et aliquip culpa aute",
"amet elit nostrud culpa veniam",
"dolore eu irure incididunt.",
"Velit officia occaecat est",
"adipisicing mollit veniam.",
"Minim sunt est culpa labore.",
"Ut culpa et nulla sunt labore",
"aliqua ipsum laborum nostrud sit",
"deserunt officia labore. Sunt",
"laboris id labore sit ex. Eiusmod",
"nulla eu incididunt excepteur",
"minim officia dolore veniam",
"labore enim quis reprehenderit.",
"Magna in laboris irure enim non",
"deserunt laborum mollit labore",
"id amet."
)
)
)
)
)
),
90
)
)
)
Expand Down

0 comments on commit c87a68b

Please sign in to comment.