Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Page numbers #38

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/net/sf/mcf2pdf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import net.sf.mcf2pdf.mcfelements.util.ImageUtil;
import net.sf.mcf2pdf.mcfelements.util.PdfUtil;


Expand All @@ -50,6 +49,7 @@ public static void main(String[] args) {
options.addOption("r", true, "Sets the resolution to use for page rendering, in DPI. Default is 150.");
options.addOption("n", true, "Sets the page number to render up to. Default renders all pages.");
options.addOption("b", false, "Prevents rendering of binding between double pages.");
options.addOption("p", false, "Enable page numbering");
options.addOption("x", false, "Generates only XSL-FO content instead of PDF content.");
options.addOption("q", false, "Quiet mode - only errors are logged.");
options.addOption("d", false, "Enables debugging logging output.");
Expand Down Expand Up @@ -148,6 +148,11 @@ public static void main(String[] args) {
if (cl.hasOption("b")) {
binding = false;
}

boolean pageNum = false;
if (cl.hasOption("p")) {
pageNum = true;
}

OutputStream finalOut;
if (cl.getArgs()[1].equals("-"))
Expand Down Expand Up @@ -186,7 +191,7 @@ public static void main(String[] args) {

try {
new Mcf2FoConverter(installDir, tempDir, tempImages).convert(
mcfFile, xslFoOut, dpi, binding, maxPageNo);
mcfFile, xslFoOut, dpi, binding, pageNum, maxPageNo);
xslFoOut.flush();

if (!cl.hasOption("x")) {
Expand Down
63 changes: 40 additions & 23 deletions src/main/java/net/sf/mcf2pdf/Mcf2FoConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.sf.mcf2pdf.pagebuild.PageClipart;
import net.sf.mcf2pdf.pagebuild.PageImage;
import net.sf.mcf2pdf.pagebuild.PageImageBackground;
import net.sf.mcf2pdf.pagebuild.PageNum;
import net.sf.mcf2pdf.pagebuild.PageRenderContext;
import net.sf.mcf2pdf.pagebuild.PageText;

Expand Down Expand Up @@ -148,7 +149,8 @@ public Mcf2FoConverter(File mcfInstallDir, File mcfTempDir, File tempImageDir) t
* @throws SAXException If any XML related problem occurs, e.g. the input file
* has an invalid format.
*/
public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean binding, int maxPageNo) throws IOException, SAXException {
public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean binding,
boolean pageNum, int maxPageNo) throws IOException, SAXException {
// build MCF DOM
log.debug("Reading MCF file");
McfFotobook book = new FotobookBuilder().readFotobook(mcfFile);
Expand All @@ -175,17 +177,28 @@ public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean bindin
// create XSL-FO document
XslFoDocumentBuilder docBuilder = new XslFoDocumentBuilder();

float pageWidth = albumType.getUsableWidth() / 10.0f * 2;
float pageHeight = albumType.getUsableHeight() / 10.0f;

// set page master
docBuilder.addPageMaster("default", pageWidth, pageHeight);

// create master for cover
float coverPageWidth = pageWidth + (albumType.getCoverExtraHorizontal() * 2 + albumType.getSpineWidth(book.getNormalPages())) / 10.0f;
float coverPageHeight = pageHeight + albumType.getCoverExtraVertical() / 10.0f;

docBuilder.addPageMaster("cover", coverPageWidth, coverPageHeight);
// setting page widths for master pages
int coverPageWidthPX = -1;
int coverPageHeightPX = -1;
int pageWidthPX = -1;
int pageHeightPX = -1;

for (McfPage p : book.getPages()) {
if (McfPage.FULLCOVER.matcher(p.getType()).matches() && coverPageWidthPX == -1 && coverPageHeightPX == -1) {
coverPageWidthPX = context.toPixel(p.getBundlesize().getWidth() / 10.0f);
coverPageHeightPX = context.toPixel(p.getBundlesize().getHeight() / 10.0f);
} else if (McfPage.CONTENT.matcher(p.getType()).matches()&& pageWidthPX == -1 && pageHeightPX == -1 ) {
pageWidthPX = context.toPixel(p.getBundlesize().getWidth() / 10.0f);
pageHeightPX = context.toPixel(p.getBundlesize().getHeight() / 10.0f);
}
if (coverPageWidthPX != -1 && coverPageHeightPX != -1 && pageWidthPX != -1 && pageHeightPX != -1) {
break;
}
}

// setting master pages
docBuilder.addPageMaster("cover", coverPageWidthPX, coverPageHeightPX);
docBuilder.addPageMaster("default", pageWidthPX, pageHeightPX);

// prepare temporary folder
log.debug("Preparing temporary working directory");
Expand All @@ -205,8 +218,9 @@ public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean bindin

if (leftCover != null) {
log.info("Rendering cover...");
currentPage = new BitmapPageBuilder(coverPageWidth, coverPageHeight, context, tempImageDir);
processDoublePage(leftCover, rightCover, imageDir, false);
currentPage = new BitmapPageBuilder(coverPageWidthPX, coverPageHeightPX, context, tempImageDir);
processDoublePage(leftCover, rightCover, imageDir, false, false,
book, coverPageWidthPX, coverPageHeightPX);
docBuilder.startFlow("cover");
currentPage.addToDocumentBuilder(docBuilder);
docBuilder.endFlow();
Expand All @@ -230,17 +244,16 @@ public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean bindin
// now, process pages as a pair
docBuilder.startFlow("default");
log.debug("Starting rendering of " + normalPages.size() + " pages");
currentPage = new BitmapPageBuilder(pageWidth, pageHeight, context, tempImageDir);
currentPage = new BitmapPageBuilder(pageWidthPX, pageHeightPX, context, tempImageDir);

for (int i = 0; i < normalPages.size(); i += 2) {
log.info("Rendering pages " + i + "+" + (i+1) + "...");
processDoublePage(normalPages.get(i),
i + 1 < normalPages.size() ? normalPages.get(i + 1) : null,
imageDir, binding);
imageDir, binding, pageNum, book, pageWidthPX, pageHeightPX);
currentPage.addToDocumentBuilder(docBuilder);
if (i < normalPages.size() - 2) {
docBuilder.newPage();
currentPage = new BitmapPageBuilder(pageWidth, pageHeight, context, tempImageDir);
currentPage = new BitmapPageBuilder(pageWidthPX, pageHeightPX, context, tempImageDir);
}
}

Expand All @@ -252,8 +265,9 @@ public void convert(File mcfFile, OutputStream xslFoOut, int dpi, boolean bindin
}

private void processDoublePage(McfPage leftPage, McfPage rightPage, File imageDir,
boolean addBinding) throws IOException {
// handle packgrounds
boolean addBinding, boolean addPageNum, McfFotobook book,
int pageWidth, int pageHeight) throws IOException {
// handle backgrounds
PageBackground bg = new PageBackground(
leftPage == null ? Collections.<McfBackground>emptyList() : leftPage.getBackgrounds(),
rightPage == null ? Collections.<McfBackground>emptyList() : rightPage.getBackgrounds());
Expand All @@ -280,11 +294,14 @@ else if (McfArea.TEXTAREA.matcher(a.getAreaType()).matches()) {
currentPage.addDrawable(new PageText((McfText)a.getContent()));
}
}

if (addPageNum) {
currentPage.addDrawable(new PageNum(book, leftPage, "left", pageWidth, pageHeight));
currentPage.addDrawable(new PageNum(book, rightPage, "right", pageWidth, pageHeight));
}

if (addBinding) {
currentPage.addDrawable(new PageBinding(resources.getBinding(),
(albumType.getUsableWidth() + albumType.getBleedMargin()) / 10.0f * 2,
(albumType.getUsableHeight() + albumType.getBleedMargin() * 2) / 10.0f));
currentPage.addDrawable(new PageBinding(resources.getBinding()));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/sf/mcf2pdf/mcfelements/McfBundlesize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.sf.mcf2pdf.mcfelements;

public interface McfBundlesize {

public McfPage getPage();

public int getHeight();

public int getWidth();

}
2 changes: 2 additions & 0 deletions src/main/java/net/sf/mcf2pdf/mcfelements/McfFotobook.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface McfFotobook {
public File getFile();

public List<? extends McfPage> getPages();

public McfPageNum getPageNum();

public int getProductType();

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/sf/mcf2pdf/mcfelements/McfPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface McfPage {
public final static Pattern SPINE = Pattern.compile("SPINE|spine");

public McfFotobook getFotobook();

public McfBundlesize getBundlesize();

public List<? extends McfArea> getAreas();

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/net/sf/mcf2pdf/mcfelements/McfPageNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.sf.mcf2pdf.mcfelements;

import java.awt.Color;

public interface McfPageNum {

public McfFotobook getFotobook();

public String getTextString();

public int getPosition();

public int getVerticalMargin();

public int getHorizontalMargin();

public Color getTextColor();

public Color getBgColor();

public float getFontSize();

public int getFontBold();

public boolean getFBold();

public int getFontItalics();

public boolean getFItalics();

public String getFontFamily();

public int getFormat();

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
import net.sf.mcf2pdf.mcfelements.McfArea;
import net.sf.mcf2pdf.mcfelements.McfBackground;
import net.sf.mcf2pdf.mcfelements.McfBorder;
import net.sf.mcf2pdf.mcfelements.McfBundlesize;
import net.sf.mcf2pdf.mcfelements.McfClipart;
import net.sf.mcf2pdf.mcfelements.McfFotobook;
import net.sf.mcf2pdf.mcfelements.McfImage;
import net.sf.mcf2pdf.mcfelements.McfImageBackground;
import net.sf.mcf2pdf.mcfelements.McfPage;
import net.sf.mcf2pdf.mcfelements.McfPageNum;
import net.sf.mcf2pdf.mcfelements.McfText;
import net.sf.mcf2pdf.mcfelements.util.DigesterUtil;

Expand Down Expand Up @@ -94,12 +96,25 @@ public void configureDigester(Digester digester, File mcfFile)
// fotobook element
digester.addObjectCreate("fotobook", getFotobookClass());
DigesterUtil.addSetProperties(digester, "fotobook", getSpecialFotobookAttributes());

// pagenumbering element
digester.addObjectCreate("fotobook/pagenumbering", getPageNumClass());
digester.addSetTop("fotobook/pagenumbering", "setFotobook");
// digester.addSetProperties("fotobook/pagenumbering");
DigesterUtil.addSetProperties(digester, "fotobook/pagenumbering", getSpecialPageNumAttributes());
digester.addSetNext("fotobook/pagenumbering", "addPageNum", McfPageNum.class.getName());

// page element
digester.addObjectCreate("fotobook/page", getPageClass());
digester.addSetTop("fotobook/page", "setFotobook");
DigesterUtil.addSetProperties(digester, "fotobook/page", getSpecialPageAttributes());
digester.addSetNext("fotobook/page", "addPage", McfPage.class.getName());

// bundlesize element
digester.addObjectCreate("fotobook/page/bundlesize", getBundlesizeClass());
digester.addSetTop("fotobook/page/bundlesize", "setPage");
digester.addSetProperties("fotobook/page/bundlesize");
digester.addSetNext("fotobook/page/bundlesize", "addBundlesize", McfBundlesize.class.getName());

// background element
digester.addObjectCreate("fotobook/page/background", getBackgroundClass());
Expand Down Expand Up @@ -149,7 +164,7 @@ public void configureDigester(Digester digester, File mcfFile)
digester.addObjectCreate("templates/template", Template.class);
digester.addSetProperties("templates/template");
digester.addSetNext("templates/template", "add");

// Decorations (fotoframes)
digester.addObjectCreate("decorations", LinkedList.class);
digester.addObjectCreate("decorations/decoration", Decoration.class);
Expand Down Expand Up @@ -218,6 +233,23 @@ protected List<String[]> getSpecialFotobookAttributes() {
result.add(new String[] { "programversion", "programVersion" });
return result;
}

protected Class<? extends McfPageNum> getPageNumClass() {
return McfPageNumImpl.class;
}

protected List<String[]> getSpecialPageNumAttributes() {
List<String[]> result = new Vector<String[]>();
result.add(new String[] { "textstring", "textString" });
result.add(new String[] { "margin", "horizontalMargin" });
result.add(new String[] { "textcolor", "textColor" });
result.add(new String[] { "bgcolor", "bgColor" });
result.add(new String[] { "fontsize", "fontSize" });
result.add(new String[] { "fontbold", "fontBold" });
result.add(new String[] { "fontitalics", "fontItalics" });
result.add(new String[] { "fontfamily", "fontFamily"});
return result;
}

protected Class<? extends McfPage> getPageClass() {
return McfPageImpl.class;
Expand All @@ -228,6 +260,10 @@ protected List<String[]> getSpecialPageAttributes() {
result.add(new String[] { "pagenr", "pageNr" });
return result;
}

protected Class<? extends McfBundlesize> getBundlesizeClass() {
return McfBundlesizeImpl.class;
}

protected Class<? extends McfBackground> getBackgroundClass() {
return McfBackgroundImpl.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.sf.mcf2pdf.mcfelements.impl;

import net.sf.mcf2pdf.mcfelements.McfBundlesize;
import net.sf.mcf2pdf.mcfelements.McfPage;

public class McfBundlesizeImpl implements McfBundlesize {

private McfPage page;
private int height;
private int width;

@Override
public McfPage getPage() {
return page;
}

public void setPage(McfPage page) {
this.page = page;
}

@Override
public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

@Override
public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

}
11 changes: 11 additions & 0 deletions src/main/java/net/sf/mcf2pdf/mcfelements/impl/McfFotobookImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import net.sf.mcf2pdf.mcfelements.McfFotobook;
import net.sf.mcf2pdf.mcfelements.McfPage;
import net.sf.mcf2pdf.mcfelements.McfPageNum;


public class McfFotobookImpl implements McfFotobook {
Expand All @@ -32,6 +33,8 @@ public class McfFotobookImpl implements McfFotobook {

private int totalPages;

private McfPageNum pageNumbering;

private List<McfPage> pages = new Vector<McfPage>();

public void addPage(McfPage page) {
Expand All @@ -42,6 +45,14 @@ public List<? extends McfPage> getPages() {
return Collections.unmodifiableList(pages);
}

public void addPageNum(McfPageNum pageNumbering) {
this.pageNumbering = pageNumbering;
}

public McfPageNum getPageNum() {
return pageNumbering;
}

public int getProductType() {
return productType;
}
Expand Down
Loading