Skip to content

Commit

Permalink
(#135) Fix Margins
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Feb 1, 2024
1 parent e39f4fe commit 62303e2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.fabriciofx.cactoos.pdf.pages;
package com.github.fabriciofx.cactoos.pdf.content;

import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.Indirect;
import com.github.fabriciofx.cactoos.pdf.Pages;
import com.github.fabriciofx.cactoos.pdf.Resource;
import com.github.fabriciofx.cactoos.pdf.indirect.DefaultIndirect;
import com.github.fabriciofx.cactoos.pdf.page.Format;
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.nio.charset.StandardCharsets;
import java.util.List;
Expand All @@ -45,10 +46,11 @@
{
"PMD.UnusedPrivateField",
"PMD.SingularField",
"PMD.UseUnderscoresInNumericLiterals"
"PMD.UseUnderscoresInNumericLiterals",
"PMD.StringInstantiation"
}
)
public final class Margins implements Pages {
public final class Margins implements Content {
/**
* One centimeter in points.
*
Expand Down Expand Up @@ -77,17 +79,22 @@ public final class Margins implements Pages {
private final double left;

/**
* Pages.
* Page Format.
*/
private final Pages origin;
private final Format format;

/**
* Text.
*/
private final Text origin;

/**
* Ctor.
*
* @param pages Pages
* @param text Text to be decorated
*/
public Margins(final Pages pages) {
this(1.0, 1.0, 1.0, 1.0, pages);
public Margins(final Text text) {
this(1.0, 1.0, 1.0, 1.0, Format.A4, text);
}

/**
Expand All @@ -97,29 +104,31 @@ public Margins(final Pages pages) {
* @param right Right margin
* @param bottom Bottom margin
* @param left Left margin
* @param pages Pages to be decorated
* @param format Page format
* @param text Text to be decorated
* @checkstyle ParameterNumberCheck (10 lines)
*/
public Margins(
final double top,
final double right,
final double bottom,
final double left,
final Pages pages
final Format format,
final Text text
) {
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
this.origin = pages;
this.format = format;
this.origin = text;
}

@Override
public Indirect indirect(final int... parent) throws Exception {
final Indirect indirect = this.origin.indirect();
public byte[] asStream() throws Exception {
final Pattern pattern = Pattern.compile("BT.*TL");
final Matcher matcher = pattern.matcher(
new String(indirect.asBytes())
new String(this.origin.asStream())
);
final StringBuffer stream = new StringBuffer();
while (matcher.find()) {
Expand All @@ -135,35 +144,38 @@ public Indirect indirect(final int... parent) throws Exception {
fontname,
fontsize,
this.left * Margins.ONE_CM,
this.format().height() - this.top * Margins.ONE_CM,
this.format.height() - this.top * Margins.ONE_CM,
leading
).asString()
);
}
matcher.appendTail(stream);
final Dictionary dictionary = indirect.dictionary().with(
new Stream(
stream.toString().getBytes(StandardCharsets.UTF_8)
)
);
return stream.toString().getBytes(StandardCharsets.UTF_8);
}

@Override
public Indirect indirect(final int... parent) throws Exception {
final Indirect indirect = this.origin.indirect();
final byte[] stream = this.asStream();
return new DefaultIndirect(
indirect.reference().number(),
indirect.reference().generation(),
dictionary
indirect.dictionary()
.add("Length", new Int(stream.length))
.with(new Stream(stream))
);
}

@Override
public List<Resource> resource() {
return this.origin.resource();
}

@Override
public void print(
final List<Indirect> indirects,
final int... parent
) throws Exception {
indirects.add(this.indirect());
this.origin.print(indirects);
}

@Override
public Format format() {
return this.origin.format();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.fabriciofx.cactoos.pdf.pages;
package com.github.fabriciofx.cactoos.pdf.content;

import com.github.fabriciofx.cactoos.pdf.Document;
import com.github.fabriciofx.cactoos.pdf.Font;
import com.github.fabriciofx.cactoos.pdf.Id;
import com.github.fabriciofx.cactoos.pdf.content.Contents;
import com.github.fabriciofx.cactoos.pdf.content.Text;
import com.github.fabriciofx.cactoos.pdf.id.Serial;
import com.github.fabriciofx.cactoos.pdf.object.Information;
import com.github.fabriciofx.cactoos.pdf.page.DefaultPage;
import com.github.fabriciofx.cactoos.pdf.page.Format;
import com.github.fabriciofx.cactoos.pdf.pages.DefaultPages;
import com.github.fabriciofx.cactoos.pdf.resource.font.TimesRoman;
import org.cactoos.bytes.BytesOf;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.Joined;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;

Expand All @@ -46,7 +44,6 @@
* @since 0.0.1
*/
final class MarginsTest {
@Disabled
@Test
void margins() throws Exception {
final Id id = new Serial();
Expand All @@ -64,20 +61,17 @@ void margins() throws Exception {
final Font font = new TimesRoman(id, 12);
final byte[] actual = new Document(
id,
new Information(
new DefaultPages(
id,
"Title", "Hello World"
),
new Margins(
2.5,
2.5,
2.5,
2.5,
new DefaultPages(
new DefaultPage(
id,
new DefaultPage(
id,
new Contents(
new Contents(
new Margins(
2.5,
2.5,
2.5,
2.5,
Format.A4,
new Text(id, font, 0, 500, 60, 14, content)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
import com.github.fabriciofx.cactoos.pdf.Font;
import com.github.fabriciofx.cactoos.pdf.Id;
import com.github.fabriciofx.cactoos.pdf.content.Contents;
import com.github.fabriciofx.cactoos.pdf.content.Margins;
import com.github.fabriciofx.cactoos.pdf.content.Text;
import com.github.fabriciofx.cactoos.pdf.id.Serial;
import com.github.fabriciofx.cactoos.pdf.page.DefaultPage;
import com.github.fabriciofx.cactoos.pdf.page.Format;
import com.github.fabriciofx.cactoos.pdf.pages.DefaultPages;
import com.github.fabriciofx.cactoos.pdf.pages.Margins;
import com.github.fabriciofx.cactoos.pdf.resource.font.TimesRoman;
import java.io.File;
import java.nio.file.Files;
Expand All @@ -44,7 +45,7 @@
* @checkstyle HideUtilityClassConstructorCheck (200 lines)
*/
@SuppressWarnings({"PMD.UseUtilityClass", "PMD.ProhibitPublicStaticMethods"})
public final class MarginsPage {
public final class MarginsText {
/**
* Main method.
*
Expand All @@ -70,16 +71,17 @@ public static void main(final String[] args) throws Exception {
file.toPath(),
new Document(
id,
new Margins(
2.5,
2.5,
2.5,
2.5,
new DefaultPages(
new DefaultPages(
id,
new DefaultPage(
id,
new DefaultPage(
id,
new Contents(
new Contents(
new Margins(
2.5,
2.5,
2.5,
2.5,
Format.A4,
new Text(id, font, 0, 500, 60, 14, content)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsNumber;
import org.llorllale.cactoos.matchers.IsText;

/**
* Test case for {@link Dictionary}.
*
* @since 0.0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class DictionaryTest {
@Test
void dictionary() {
Expand Down Expand Up @@ -225,4 +227,22 @@ void merge() throws Exception {
)
).affirm();
}

@Test
void dictionaryUpdateStream() throws Exception {
final String msga = "Hello World!";
final String msgb = "New message, with a new Hello World!";
final Dictionary dictionary = new Dictionary()
.add("Length", new Int(msga.length()))
.with(new Stream(msga.getBytes()));
final Dictionary changed = dictionary
.add("Length", new Int(msgb.length()))
.with(new Stream(msgb.getBytes()));
final Int length = changed.get("Length");
new Assertion<>(
"Must update a stream in a dictionary",
length.value(),
new IsNumber(36)
).affirm();
}
}
Binary file modified src/test/resources/document/margins.pdf
Binary file not shown.

0 comments on commit 62303e2

Please sign in to comment.