W2X page style and footnote improvements

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@96 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2011-03-09 08:48:57 +00:00
parent 029d82e2eb
commit bf235469bb
10 changed files with 236 additions and 124 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-02-21)
* Version 1.2 (2011-03-08)
*
*/
@ -237,10 +237,6 @@ public class Converter extends ConverterBase {
else if (ofr.isPresentation()) { drawCv.convertDrawContent(body); }
else { textCv.convertTextContent(body); }
// Add footnotes and endnotes
textCv.insertFootnotes(htmlDoc.getContentNode());
textCv.insertEndnotes(htmlDoc.getContentNode());
// Set the title page and text page entries
if (converterResult.getContent().isEmpty()) {
// No headings in the document: There is no title page and the text page is the first page
@ -555,7 +551,6 @@ public class Converter extends ConverterBase {
// Prepare next output file
public Element nextOutFile() {
if (nOutFileIndex>=0) { textCv.insertFootnotes(htmlDoc.getContentNode()); }
htmlDoc = new XhtmlDocument(getOutFileName(++nOutFileIndex,false),nType);
htmlDoc.setConfig(config);
if (template!=null) { htmlDoc.readFromTemplate(template); }
@ -574,10 +569,10 @@ public class Converter extends ConverterBase {
". See http://writer2latex.sourceforge.net for more info."),
rootElement.getFirstChild());
// Apply page formatting (using first master page)
if (ofr.getFirstMasterPage()!=null && !ofr.isPresentation()) {
// Apply default writing direction
if (!ofr.isPresentation()) {
StyleInfo pageInfo = new StyleInfo();
styleCv.getPageSc().applyStyle(ofr.getFirstMasterPage().getName(),pageInfo);
styleCv.getPageSc().applyDefaultWritingDirection(pageInfo);
styleCv.getPageSc().applyStyle(pageInfo,htmlDoc.getContentNode());
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2010-03-01)
* Version 1.2 (2010-03-08)
*
*/
@ -41,12 +41,15 @@ import writer2latex.util.CSVList;
* A page style in a presentation is represented through the master page,
* which links to a page layout defining the geometry and optionally a drawing
* page defining the drawing background.
* In a presentation document we export the full page style, in a text
* document we only export the background.
*
* In a presentation document we export the full page style.
* In a text document we export the writing direction, background color and footnote rule for the first master page only
*/
public class PageStyleConverter extends StyleConverterHelper {
private boolean bHasFootnoteRules = false;
/** Create a new <code>PageStyleConverter</code>
/** Create a new <code>PageStyleConverter</code>
* @param ofr an <code>OfficeReader</code> to read style information from
* @param config the configuration to use
* @param converter the main <code>Converter</code> class
@ -54,27 +57,45 @@ public class PageStyleConverter extends StyleConverterHelper {
*/
public PageStyleConverter(OfficeReader ofr, XhtmlConfig config, Converter converter, int nType) {
super(ofr,config,converter,nType);
this.bConvertStyles = config.xhtmlFormatting()==XhtmlConfig.CONVERT_ALL || config.xhtmlFormatting()==XhtmlConfig.IGNORE_HARD;
}
/** Apply footnote rule formatting (based on first master page)
*
* @param info then StyleInfo to which style information should be attached
*/
public void applyFootnoteRuleStyle(StyleInfo info) {
bHasFootnoteRules = true;
info.sClass="footnoterule";
}
/** Apply default writing direction (based on first master page)
*
* @param info then StyleInfo to which style information should be attached
*/
public void applyDefaultWritingDirection(StyleInfo info) {
MasterPage masterPage = ofr.getFirstMasterPage();
if (masterPage!=null) {
PageLayout pageLayout = ofr.getPageLayout(masterPage.getPageLayoutName());
if (pageLayout!=null) {
applyDirection(pageLayout,info);
}
}
}
/** Apply a master page style - currently only for presentations
*
* @param sStyleName The name of the master page
* @param info the StyleInfo to which style information should be attached
*/
public void applyStyle(String sStyleName, StyleInfo info) {
MasterPage masterPage = ofr.getMasterPage(sStyleName);
String sDisplayName = masterPage.getDisplayName();
if (masterPage!=null) {
String sDisplayName = masterPage.getDisplayName();
if (ofr.isPresentation()) {
// Always generates class name
info.sClass="masterpage"+styleNames.getExportName(sDisplayName);
}
else {
// For text documents only writing direction and background
String sPageLayout = masterPage.getPageLayoutName();
PageLayout pageLayout = ofr.getPageLayout(sPageLayout);
if (pageLayout!=null) {
applyDirection(pageLayout,info);
if (bConvertStyles) {
getFrameSc().cssBackground(pageLayout,info.props,true);
}
}
}
}
}
@ -83,9 +104,10 @@ public class PageStyleConverter extends StyleConverterHelper {
*/
public String getStyleDeclarations(String sIndent) {
StringBuffer buf = new StringBuffer();
// This will be master pages for presentations only
Enumeration<String> names = styleNames.keys();
while (names.hasMoreElements()) {
// This will be master pages for presentations only
String sDisplayName = names.nextElement();
MasterPage style = (MasterPage)
getStyles().getStyleByDisplayName(sDisplayName);
@ -102,12 +124,42 @@ public class PageStyleConverter extends StyleConverterHelper {
if (drawingPage!=null) {
cssDrawBackground(drawingPage,info.props,true);
}
// The export the results
// Then export the results
buf.append(sIndent)
.append(".masterpage").append(styleNames.getExportName(sDisplayName))
.append(" {").append(info.props.toString()).append("}")
.append(config.prettyPrint() ? "\n" : " ");
}
if (ofr.isText()) {
// Export page formatting for first master page in text documents
MasterPage masterPage = ofr.getFirstMasterPage();
if (masterPage!=null) {
PageLayout pageLayout = ofr.getPageLayout(masterPage.getPageLayoutName());
if (pageLayout!=null) {
if (bConvertStyles) {
// Background color
StyleInfo pageInfo = new StyleInfo();
getFrameSc().cssBackground(pageLayout,pageInfo.props,true);
if (converter.isOPS()) { // Use zero margin for EPUB and default margins for XHTML
pageInfo.props.addValue("margin", "0");
}
if (pageInfo.hasAttributes()) {
buf.append(sIndent).append("body {").append(pageInfo.props.toString()).append("}")
.append(config.prettyPrint() ? "\n" : " ");
}
// Footnote rule
if (bHasFootnoteRules) {
StyleInfo ruleInfo = new StyleInfo();
cssFootnoteRule(pageLayout,ruleInfo.props);
buf.append(sIndent).append("hr.footnoterule {").append(ruleInfo.props.toString()).append("}")
.append(config.prettyPrint() ? "\n" : " ");
}
}
}
}
}
return buf.toString();
}
@ -132,6 +184,39 @@ public class PageStyleConverter extends StyleConverterHelper {
String sHeight = style.getProperty(XMLString.FO_PAGE_HEIGHT);
if (sHeight!=null) { props.addValue("height",scale(sHeight)); }
}
// Footnote rule
private void cssFootnoteRule(PageLayout style, CSVList props) {
String sBefore = style.getFootnoteProperty(XMLString.STYLE_DISTANCE_BEFORE_SEP);
if (sBefore!=null) { props.addValue("margin-top",scale(sBefore)); }
String sAfter = style.getFootnoteProperty(XMLString.STYLE_DISTANCE_AFTER_SEP);
if (sAfter!=null) { props.addValue("margin-bottom", scale(sAfter)); }
String sHeight = style.getFootnoteProperty(XMLString.STYLE_WIDTH);
if (sHeight!=null) { props.addValue("height", scale(sHeight)); }
String sWidth = style.getFootnoteProperty(XMLString.STYLE_REL_WIDTH);
if (sWidth!=null) { props.addValue("width", sWidth); }
String sColor = style.getFootnoteProperty(XMLString.STYLE_COLOR);
if (sColor!=null) { // To get the expected result in all browsers we must set both
props.addValue("color", sColor);
props.addValue("background-color", sColor);
}
String sAdjustment = style.getFootnoteProperty(XMLString.STYLE_ADJUSTMENT);
if ("right".equals(sAdjustment)) {
props.addValue("margin-left", "auto");
props.addValue("margin-right", "0");
}
else if ("center".equals(sAdjustment)) {
props.addValue("margin-left", "auto");
props.addValue("margin-right", "auto");
}
else { // default left
props.addValue("margin-left", "0");
props.addValue("margin-right", "auto");
}
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2010 by Henrik Just
* Copyright: 2002-2011 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2010-12-29)
* Version 1.2 (2011-03-08)
*
*/
@ -127,6 +127,9 @@ public class TextConverter extends ConverterHelper {
private String sFntCitStyle = null;
private String sEntCitBodyStyle = null;
private String sEntCitStyle = null;
// Footnote position (can be page or document)
private boolean bFootnotesAtPage = true;
// Gather the footnotes and endnotes
private LinkedList<Node> footnotes = new LinkedList<Node>();
@ -161,6 +164,7 @@ public class TextConverter extends ConverterHelper {
if (notes!=null) {
sFntCitBodyStyle = notes.getProperty(XMLString.TEXT_CITATION_BODY_STYLE_NAME);
sFntCitStyle = notes.getProperty(XMLString.TEXT_CITATION_STYLE_NAME);
bFootnotesAtPage = !"document".equals(notes.getProperty(XMLString.TEXT_FOOTNOTES_POSITION));
}
notes = ofr.getEndnotesConfiguration();
if (notes!=null) {
@ -187,7 +191,11 @@ public class TextConverter extends ConverterHelper {
}
// Convert content
traverseBlockText(onode,hnode);
hnode = (Element)traverseBlockText(onode,hnode);
// Add footnotes and endnotes
insertFootnotes(hnode,true);
insertEndnotes(hnode);
// Generate all indexes
int nIndexCount = indexes.size();
@ -563,6 +571,7 @@ public class TextConverter extends ConverterHelper {
// No objections, this is a level that causes splitting
nCharacterCount = 0;
bPendingPageBreak = false;
if (converter.getOutFileIndex()>=0) { insertFootnotes(node,false); }
return converter.nextOutFile();
}
return node;
@ -1685,22 +1694,37 @@ public class TextConverter extends ConverterHelper {
footnotes.add(onode);
}
public void insertFootnotes(Node hnode) {
private void insertFootnotes(Node hnode, boolean bFinal) {
int n = footnotes.size();
for (int i=0; i<n; i++) {
Node footnote = footnotes.get(i);
String sId = Misc.getAttribute(footnote,XMLString.TEXT_ID);
Node citation = Misc.getChildByTagName(footnote,XMLString.TEXT_FOOTNOTE_CITATION);
if (citation==null) { // try oasis
citation = Misc.getChildByTagName(footnote,XMLString.TEXT_NOTE_CITATION);
}
Node body = Misc.getChildByTagName(footnote,XMLString.TEXT_FOOTNOTE_BODY);
if (body==null) { // try oasis
body = Misc.getChildByTagName(footnote,XMLString.TEXT_NOTE_BODY);
}
traverseNoteBody(sId,sFntCitStyle,citation,body,hnode);
if (n>0 && bFootnotesAtPage) { // Add footnote rule
Element rule = converter.createElement("hr");
StyleInfo info = new StyleInfo();
getPageSc().applyFootnoteRuleStyle(info);
getPageSc().applyStyle(info, rule);
hnode.appendChild(rule);
}
else if (bFinal && !bFootnotesAtPage) { // New page if required for footnotes as endnotes
if (nSplit>0) { hnode = converter.nextOutFile(); }
insertNoteHeading(hnode, config.getFootnotesHeading(), "footnotes");
}
if (bFinal || bFootnotesAtPage) { // Insert the footnotes
for (int i=0; i<n; i++) {
Node footnote = footnotes.get(i);
String sId = Misc.getAttribute(footnote,XMLString.TEXT_ID);
Node citation = Misc.getChildByTagName(footnote,XMLString.TEXT_FOOTNOTE_CITATION);
if (citation==null) { // try oasis
citation = Misc.getChildByTagName(footnote,XMLString.TEXT_NOTE_CITATION);
}
Node body = Misc.getChildByTagName(footnote,XMLString.TEXT_FOOTNOTE_BODY);
if (body==null) { // try oasis
body = Misc.getChildByTagName(footnote,XMLString.TEXT_NOTE_BODY);
}
traverseNoteBody(sId,sFntCitStyle,citation,body,hnode,ofr.getFootnotesConfiguration());
}
footnotes.clear();
}
footnotes.clear();
}
/* Process an endnote */
@ -1719,27 +1743,11 @@ public class TextConverter extends ConverterHelper {
endnotes.add(onode);
}
public void insertEndnotes(Node hnode) {
private void insertEndnotes(Node hnode) {
int n = endnotes.size();
if (n>0) {
if (nSplit>0) { hnode = converter.nextOutFile(); }
String sHeading = config.getEndnotesHeading();
if (sHeading.length()>0) {
Element heading = converter.createElement("h1");
hnode.appendChild(heading);
heading.appendChild(converter.createTextNode(sHeading));
// Add to external content.
if (nSplit>0) {
converter.addContentEntry(sHeading, 1, null);
}
else {
//For single output file we need a target
converter.addTarget(heading,"endnotes");
converter.addContentEntry(sHeading, 1, "endnotes");
}
}
insertNoteHeading(hnode, config.getEndnotesHeading(), "endnotes");
for (int i=0; i<n; i++) {
Node endnote = endnotes.get(i);
String sId = Misc.getAttribute(endnote,XMLString.TEXT_ID);
@ -1751,15 +1759,33 @@ public class TextConverter extends ConverterHelper {
if (body==null) { // try oasis
body = Misc.getChildByTagName(endnote,XMLString.TEXT_NOTE_BODY);
}
traverseNoteBody(sId,sEntCitStyle,citation,body,hnode);
traverseNoteBody(sId,sEntCitStyle,citation,body,hnode,ofr.getEndnotesConfiguration());
}
}
}
private void insertNoteHeading(Node hnode, String sHeading, String sTarget) {
if (sHeading.length()>0) {
Element heading = converter.createElement("h1");
hnode.appendChild(heading);
heading.appendChild(converter.createTextNode(sHeading));
// Add to external content.
if (nSplit>0) {
converter.addContentEntry(sHeading, 1, null);
}
else {
//For single output file we need a target
converter.addTarget(heading,sTarget);
converter.addContentEntry(sHeading, 1, sTarget);
}
}
}
/*
* Process the contents of a footnote or endnote
*/
private void traverseNoteBody (String sId, String sCitStyle, Node citation,Node onode, Node hnode) {
private void traverseNoteBody (String sId, String sCitStyle, Node citation, Node onode, Node hnode, PropertySet noteConfig) {
// Create the anchor/footnote symbol:
// Create target and link
Element link = converter.createLink("body"+sId);
@ -1767,43 +1793,22 @@ public class TextConverter extends ConverterHelper {
StyleInfo linkInfo = new StyleInfo();
getTextSc().applyStyle(sCitStyle,linkInfo);
applyStyle(linkInfo,link);
String sPrefix = noteConfig.getProperty(XMLString.STYLE_NUM_PREFIX);
if (sPrefix!=null) {
link.appendChild(converter.createTextNode(sPrefix));
}
traversePCDATA(citation,link);
// Add a space and save it for later insertion:
String sSuffix = noteConfig.getProperty(XMLString.STYLE_NUM_SUFFIX);
if (sSuffix!=null) {
link.appendChild(converter.createTextNode(sSuffix));
}
// Add a space and save it for later insertion
Element span = converter.createElement("span");
span.appendChild(link);
span.appendChild(converter.createTextNode(" "));
asapNode = span;
traverseBlockText(onode,hnode);
/*if (onode.hasChildNodes()) {
NodeList nList = onode.getChildNodes();
int len = nList.getLength();
for (int i = 0; i < len; i++) {
Node child = nList.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
String nodeName = child.getNodeName();
if (nodeName.equals(XMLString.TEXT_H)) {
handleHeading(child,hnode);
}
if (nodeName.equals(XMLString.TEXT_P)) {
handleParagraph(child,hnode);
}
if (nodeName.equals(XMLString.TEXT_ORDERED_LIST)) {
handleOL(child,0,null,hnode);
}
if (nodeName.equals(XMLString.TEXT_UNORDERED_LIST)) {
handleUL(child,0,null,hnode);
}
}
}
}*/
}
private void handlePageNumber(Node onode, Node hnode) {

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-02-23)
* Version 1.2 (2011-03-08)
*
*/
@ -41,7 +41,7 @@ import writer2latex.util.Misc;
public class XhtmlConfig extends writer2latex.base.ConfigBase {
// Implement configuration methods
protected int getOptionCount() { return 53; }
protected int getOptionCount() { return 54; }
protected String getDefaultConfigPath() { return "/writer2latex/xhtml/config/"; }
// Override setOption: To be backwards compatible, we must accept options
@ -117,25 +117,26 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
private static final int TABSTOP_STYLE = 31;
private static final int FORMULAS = 32;
private static final int ENDNOTES_HEADING = 33;
private static final int EXTERNAL_TOC_DEPTH = 34;
private static final int INCLUDE_TOC = 35;
private static final int SPLIT_LEVEL = 36;
private static final int REPEAT_LEVELS = 37;
private static final int PAGE_BREAK_SPLIT = 38;
private static final int SPLIT_AFTER = 39;
private static final int CALC_SPLIT = 40;
private static final int DISPLAY_HIDDEN_SHEETS = 41;
private static final int DISPLAY_HIDDEN_ROWS_COLS = 42;
private static final int DISPLAY_FILTERED_ROWS_COLS = 43;
private static final int APPLY_PRINT_RANGES = 44;
private static final int USE_TITLE_AS_HEADING = 45;
private static final int USE_SHEET_NAMES_AS_HEADINGS = 46;
private static final int XSLT_PATH = 47;
private static final int SAVE_IMAGES_IN_SUBDIR = 48;
private static final int UPLINK = 49;
private static final int DIRECTORY_ICON = 50;
private static final int DOCUMENT_ICON = 51;
private static final int ZEN_HACK = 52; // temporary hack for ePub Zen Garden styles
private static final int FOOTNOTES_HEADING = 34;
private static final int EXTERNAL_TOC_DEPTH = 35;
private static final int INCLUDE_TOC = 36;
private static final int SPLIT_LEVEL = 37;
private static final int REPEAT_LEVELS = 38;
private static final int PAGE_BREAK_SPLIT = 39;
private static final int SPLIT_AFTER = 40;
private static final int CALC_SPLIT = 41;
private static final int DISPLAY_HIDDEN_SHEETS = 42;
private static final int DISPLAY_HIDDEN_ROWS_COLS = 43;
private static final int DISPLAY_FILTERED_ROWS_COLS = 44;
private static final int APPLY_PRINT_RANGES = 45;
private static final int USE_TITLE_AS_HEADING = 46;
private static final int USE_SHEET_NAMES_AS_HEADINGS = 47;
private static final int XSLT_PATH = 48;
private static final int SAVE_IMAGES_IN_SUBDIR = 49;
private static final int UPLINK = 50;
private static final int DIRECTORY_ICON = 51;
private static final int DOCUMENT_ICON = 52;
private static final int ZEN_HACK = 53; // temporary hack for ePub Zen Garden styles
protected ComplexOption xheading = addComplexOption("heading-map");
protected ComplexOption xpar = addComplexOption("paragraph-map");
@ -187,6 +188,7 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
options[FLOAT_OBJECTS] = new BooleanOption("float_objects","true");
options[TABSTOP_STYLE] = new Option("tabstop_style","");
options[ENDNOTES_HEADING] = new Option("endnotes_heading","");
options[FOOTNOTES_HEADING] = new Option("footnotes_heading","");
options[FORMULAS] = new IntegerOption("formulas","image+starmath") {
@Override public void setString(String sValue) {
super.setString(sValue);
@ -351,6 +353,7 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
public boolean xhtmlFloatObjects() { return ((BooleanOption) options[FLOAT_OBJECTS]).getValue(); }
public String getXhtmlTabstopStyle() { return options[TABSTOP_STYLE].getString(); }
public String getEndnotesHeading() { return options[ENDNOTES_HEADING].getString(); }
public String getFootnotesHeading() { return options[FOOTNOTES_HEADING].getString(); }
public int formulas() { return ((IntegerOption) options[FORMULAS]).getValue(); }
public int externalTocDepth() { return ((IntegerOption) options[EXTERNAL_TOC_DEPTH]).getValue(); }
public boolean includeToc() { return ((BooleanOption) options[INCLUDE_TOC]).getValue(); }