Skip to content

Commit

Permalink
(#70) Add Font as Text dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 30, 2023
1 parent 8340dac commit cf3cbfc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 105 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/content/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.Definition;
import com.github.fabriciofx.cactoos.pdf.Id;
import com.github.fabriciofx.cactoos.pdf.resource.Font;
import com.github.fabriciofx.cactoos.pdf.text.Escaped;
import com.github.fabriciofx.cactoos.pdf.text.Indirect;
import com.github.fabriciofx.cactoos.pdf.text.Multiline;
Expand All @@ -41,6 +42,11 @@
* @since 0.0.1
*/
public final class Text implements Content {
/**
* Font.
*/
private final Font font;

/**
* Font size.
*/
Expand Down Expand Up @@ -74,24 +80,27 @@ public final class Text implements Content {
/**
* Ctor.
*
* @param font Font
* @param size Font size
* @param posx Position X
* @param posy Position Y
* @param content Text content
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Text(
final Font font,
final int size,
final double posx,
final double posy,
final org.cactoos.Text content
) {
this(size, posx, posy, 80, size * 1.20, content);
this(font, size, posx, posy, 80, size * 1.20, content);
}

/**
* Ctor.
*
* @param font Font
* @param size Font size
* @param posx Position X
* @param posy Position Y
Expand All @@ -101,13 +110,15 @@ public Text(
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Text(
final Font font,
final int size,
final double posx,
final double posy,
final int max,
final double leading,
final org.cactoos.Text content
) {
this.font = font;
this.size = size;
this.posx = posx;
this.posy = posy;
Expand All @@ -123,7 +134,8 @@ public byte[] asStream() throws Exception {
out.append(new FormattedText("(%s) Tj T*\n", line).asString());
}
return new FormattedText(
"BT /F1 %d Tf %.2f %.2f Td %.2f TL\n%sET",
"BT /%s %d Tf %.2f %.2f Td %.2f TL\n%sET",
this.font.name(),
this.size,
this.posx,
this.posy,
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/resource/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Font implements Resource {
/**
* Font name.
*/
private final String name;
private final String label;

/**
* Ctor.
Expand All @@ -53,7 +53,16 @@ public final class Font implements Resource {
*/
public Font(final FontFamily family, final String name) {
this.family = family;
this.name = name;
this.label = name;
}

/**
* The font name.
*
* @return The font name
*/
public String name() {
return this.label;
}

@Override
Expand All @@ -62,7 +71,7 @@ public Definition definition(final Id id) throws Exception {
final Dictionary dictionary = new Dictionary()
.add(
"Font",
new Dictionary().add(this.name, definition.dictionary())
new Dictionary().add(this.label, definition.dictionary())
);
return new Definition(dictionary, dictionary.asBytes());
}
Expand Down
68 changes: 23 additions & 45 deletions src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ void buildDocument() throws Exception {
"deserunt laborum mollit labore id amet."
);
final Date date = new Date(2023, 12, 11, 20, 11, 32, "Etc/GMT-3");
final String filename = "src/test/resources/document/HelloWorld.pdf";
final byte[] expected = Files.readAllBytes(
new File(filename).toPath()
final Font font = new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
);
final byte[] actual = new Document(
new Information(
Expand All @@ -86,50 +86,39 @@ void buildDocument() throws Exception {
new DefaultPages(
PageFormat.A4,
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
),
new Rotate(
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
),
90
),
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
)
)
)
).asBytes();
final String filename = "src/test/resources/document/HelloWorld.pdf";
final byte[] expected = Files.readAllBytes(
new File(filename).toPath()
);
new Assertion<>(
"Must match with HelloWorld PDF document",
expected,
Expand Down Expand Up @@ -232,6 +221,10 @@ void buildFile() throws Exception {
"deserunt laborum mollit labore id amet."
);
final Date date = new Date(2023, 12, 11, 20, 11, 32, "Etc/GMT-3");
final Font font = new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
);
final File file = new File("HelloWorld.pdf");
Files.write(
file.toPath(),
Expand All @@ -250,44 +243,29 @@ void buildFile() throws Exception {
new DefaultPages(
PageFormat.A4,
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
),
new Rotate(
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
),
90
),
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new FlateEncode(
new Text(18, 0, 500, 80, 20, content)
new Text(font, 18, 0, 500, 80, 20, content)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.github.fabriciofx.cactoos.pdf.content;

import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.resource.Font;
import com.github.fabriciofx.cactoos.pdf.resource.FontFamily;
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
Expand All @@ -41,6 +43,10 @@ final class FlateEncodeTest {
@Test
void encode() throws Exception {
final Content content = new Text(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
),
18,
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.github.fabriciofx.cactoos.pdf.content;

import com.github.fabriciofx.cactoos.pdf.Serial;
import com.github.fabriciofx.cactoos.pdf.resource.Font;
import com.github.fabriciofx.cactoos.pdf.resource.FontFamily;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;
Expand All @@ -42,6 +44,10 @@ void oneline() throws Exception {
"Must don't break a small text",
new TextOf(
new Text(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
),
18,
0,
0,
Expand Down Expand Up @@ -72,6 +78,10 @@ void multiLines() throws Exception {
"Must break a big text into multiline",
new TextOf(
new Text(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
),
18,
0,
0,
Expand Down
39 changes: 15 additions & 24 deletions src/test/java/com/github/fabriciofx/cactoos/pdf/page/PageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ final class PageTest {
@Test
void buildDocumentWithRotatePage() throws Exception {
final Date date = new Date(2023, 12, 11, 20, 11, 32, "Etc/GMT-3");
final Font font = new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
);
new Assertion<>(
"Must represent a PDF document",
new TextOf(
Expand All @@ -68,23 +72,14 @@ void buildDocumentWithRotatePage() throws Exception {
PageFormat.A4,
new Rotate(
new DefaultPage(
new Resources(
new Font(
new FontFamily(
"Times-Roman",
"Type1"
),
"F1"
)
),
new Resources(font),
new Contents(
new Text(
font,
18,
0,
0,
new TextOf(
"Hello World!"
)
new TextOf("Hello World!")
)
)
),
Expand Down Expand Up @@ -114,6 +109,10 @@ void buildDocumentWithRotatePage() throws Exception {
@Test
void buildTwoPagesDocument() throws Exception {
final Date date = new Date(2023, 12, 11, 20, 11, 32, "Etc/GMT-3");
final Font font = new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
);
new Assertion<>(
"Must represent a two pages PDF document",
new TextOf(
Expand All @@ -132,14 +131,10 @@ void buildTwoPagesDocument() throws Exception {
new DefaultPages(
PageFormat.A4,
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new Text(
font,
18,
0,
0,
Expand All @@ -148,14 +143,10 @@ void buildTwoPagesDocument() throws Exception {
)
),
new DefaultPage(
new Resources(
new Font(
new FontFamily("Times-Roman", "Type1"),
"F1"
)
),
new Resources(font),
new Contents(
new Text(
font,
18,
0,
0,
Expand Down
Loading

0 comments on commit cf3cbfc

Please sign in to comment.