Refactored PageStyleParser css declarations

This commit is contained in:
Georgy Litvinov 2020-01-30 12:49:55 +01:00
parent 66360146d7
commit b97be9c4c6

View file

@ -50,7 +50,7 @@ import writer2latex.xhtml.XhtmlConfig;
*/
public class PageStyleParser extends StyleParser {
private boolean bHasFootnoteRules = false;
private boolean hasFootnoteRules = false;
/** Create a new <code>PageStyleConverter</code>
* @param ofr an <code>OfficeReader</code> to read style information from
@ -94,7 +94,7 @@ public class PageStyleParser extends StyleParser {
* @param info then StyleInfo to which style information should be attached
*/
public void applyFootnoteRuleStyle(StyleInfo info) {
bHasFootnoteRules = true;
hasFootnoteRules = true;
info.sClass="footnoterule";
}
@ -139,69 +139,74 @@ public class PageStyleParser extends StyleParser {
Enumeration<String> names = styleNames.keys();
while (names.hasMoreElements()) {
String sDisplayName = names.nextElement();
MasterPage style = (MasterPage)
getStyles().getStyleByDisplayName(sDisplayName);
StyleInfo info = new StyleInfo();
// First apply page layout (size)
PageLayout pageLayout = ofr.getPageLayout(style.getPageLayoutName());
if (pageLayout!=null) {
applyDirection(pageLayout,info);
cssPageSize(pageLayout,info.props);
getFrameSc().cssBackground(pageLayout,info.props,true);
}
// Next apply drawing-page style (draw background)
StyleWithProperties drawingPage = ofr.getDrawingPageStyle(style.getProperty(XMLString.DRAW_STYLE_NAME));
if (drawingPage!=null) {
cssDrawBackground(drawingPage,info.props,true);
}
// Then export the results
buf.append(indent);
buf.append(".masterpage");
styleNames.addName(sDisplayName);
buf.append(styleNames.getName(sDisplayName));
buf.append(" {");
buf.append(info.props.toString());
buf.append("}");
buf.append(config.prettyPrint() ? "\n" : " ");
buf.append(composeDeclaration(sDisplayName));
}
if (ofr.isText()) {
if (ofr.isText() && bConvertStyles) {
// 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(indent);
buf.append("body {");
buf.append(pageInfo.props.toString());
buf.append("}");
buf.append(config.prettyPrint() ? "\n" : " ");
}
// Footnote rule
if (bHasFootnoteRules) {
StyleInfo ruleInfo = new StyleInfo();
cssFootnoteRule(pageLayout,ruleInfo.props);
buf.append(indent);
buf.append("hr.footnoterule {");
buf.append(ruleInfo.props.toString());
buf.append("}");
buf.append(config.prettyPrint() ? "\n" : " ");
}
}
}
}
buf.append(composeFirstPageDeclaration());
}
return buf.toString();
}
private String composeFirstPageDeclaration() {
StringBuilder buf = new StringBuilder();
MasterPage masterPage = ofr.getFirstMasterPage();
if (masterPage == null) { return ""; }
PageLayout pageLayout = ofr.getPageLayout(masterPage.getPageLayoutName());
if (pageLayout == null) { return ""; }
// Background color
StyleInfo pageInfo = new StyleInfo();
getFrameSc().cssBackground(pageLayout, pageInfo.props, true);
if (pageInfo.hasAttributes()) {
buf.append(indent + "body {" + pageInfo.props.toString() + "}" + (prettyPrint ? "\n" : " "));
}
// Footnote rule
if (hasFootnoteRules) {
StyleInfo ruleInfo = new StyleInfo();
cssFootnoteRule(pageLayout, ruleInfo.props);
buf.append(indent + "hr.footnoterule {" + ruleInfo.props.toString() + "}" + (prettyPrint ? "\n" : " "));
}
return buf.toString();
}
private String composeDeclaration(String displayName) {
String declaration = "";
String selector = composeSelector(displayName);
String declarationBlock = composeDeclarationBlock(displayName);
// Then export the results
if (!selector.isEmpty() && !declarationBlock.isEmpty()) {
declaration = indent + selector + " {" + declarationBlock + "}" + (prettyPrint ? "\n" : " ");
}
return declaration;
}
private String composeDeclarationBlock( String displayName) {
MasterPage style = (MasterPage) getStyles().getStyleByDisplayName(displayName);
StyleInfo info = new StyleInfo();
// First apply page layout (size)
PageLayout pageLayout = ofr.getPageLayout(style.getPageLayoutName());
if (pageLayout!=null) {
applyDirection(pageLayout,info);
cssPageSize(pageLayout,info.props);
getFrameSc().cssBackground(pageLayout,info.props,true);
}
// Next apply drawing-page style (draw background)
StyleWithProperties drawingPage = ofr.getDrawingPageStyle(style.getProperty(XMLString.DRAW_STYLE_NAME));
if (drawingPage!=null) {
cssDrawBackground(drawingPage,info.props,true);
}
return info.props.toString();
}
private String composeSelector( String displayName) {
styleNames.addName(displayName);
return ".masterpage" + styleNames.getName(displayName);
}
/** Get the family of page styles (master pages)
* @return the style family