Move string calculations from writer2latex.util.Misc to the separate class writer2latex.util.Calc + add JavaDoc

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@256 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2015-06-19 16:06:09 +00:00
parent 67ceaae08a
commit 6f46ed3177
19 changed files with 319 additions and 213 deletions

View file

@ -470,7 +470,7 @@ public class CharStyleConverter extends StyleConverter {
private static final String nfssSize(String sFontSize){
if (sFontSize==null) return null;
return "\\fontsize{"+sFontSize+"}{"+Misc.multiply("120%",sFontSize)+"}\\selectfont";
return "\\fontsize{"+sFontSize+"}{"+Calc.multiply("120%",sFontSize)+"}\\selectfont";
}
// other character formatting

View file

@ -41,6 +41,7 @@ import writer2latex.office.MIMETypes;
import writer2latex.office.OfficeReader;
import writer2latex.office.XMLString;
import writer2latex.util.CSVList;
import writer2latex.util.Calc;
import writer2latex.util.Misc;
/**
@ -355,8 +356,8 @@ public class DrawConverter extends ConverterHelper {
CSVList options = new CSVList(',');
if (!config.originalImageSize()) {
Element frame = getFrame(node);
String sWidth = Misc.truncateLength(frame.getAttribute(XMLString.SVG_WIDTH));
String sHeight = Misc.truncateLength(frame.getAttribute(XMLString.SVG_HEIGHT));
String sWidth = Calc.truncateLength(frame.getAttribute(XMLString.SVG_WIDTH));
String sHeight = Calc.truncateLength(frame.getAttribute(XMLString.SVG_HEIGHT));
if (sWidth!=null) { options.addValue("width="+sWidth); }
if (sHeight!=null) { options.addValue("height="+sHeight); }
}
@ -415,7 +416,7 @@ public class DrawConverter extends ConverterHelper {
if (ofr.isFigureSequenceName(sSeqName)) { bIsCaption = true; }
}
String sWidth = Misc.truncateLength(getFrame(node).getAttribute(XMLString.SVG_WIDTH));
String sWidth = Calc.truncateLength(getFrame(node).getAttribute(XMLString.SVG_WIDTH));
if (!bIsCaption) {
ldp.append("\\begin{minipage}{").append(sWidth).append("}").nl();
}

View file

@ -37,6 +37,7 @@ import writer2latex.office.ListStyle;
import writer2latex.office.OfficeReader;
import writer2latex.office.StyleWithProperties;
import writer2latex.office.XMLString;
import writer2latex.util.Calc;
import writer2latex.util.Misc;
/* This class converts OpenDocument headings (<code>text:h</code>) and
@ -246,12 +247,12 @@ public class HeadingConverter extends ConverterHelper {
// Note: Use first line as left indent (cannot have separate first line indent)
ldp.append("\\renewcommand\\").append(sSecName)
.append("{\\@startsection{").append(sSecName).append("}{"+hm.getLevel(i))
.append("}{"+Misc.add(sMarginLeft,sTextIndent)+"}{");
.append("}{"+Calc.add(sMarginLeft,sTextIndent)+"}{");
// Suppress indentation after heading? currently not..
// ldp.append("-");
ldp.append(sMarginTop)
.append("}{")
.append(Misc.isZero(sMarginBottom) ? "0.1mm" : sMarginBottom)
.append(Calc.isZero(sMarginBottom) ? "0.1mm" : sMarginBottom)
.append("}{");
// Note: decl.getAfter() may include a page break after, which we ignore
ldp.append(decl.getBefore());
@ -319,7 +320,7 @@ public class HeadingConverter extends ConverterHelper {
if (sTextIndent==null) { sTextIndent = "0cm"; }*/
String sTabPos = outline.getLevelStyleProperty(i, XMLString.TEXT_LIST_TAB_STOP_POSITION);
if (sTabPos==null) { sTabPos = "0cm"; }
sDistance = Misc.sub(sTabPos, Misc.add(sMarginLeft, sTextIndent));
sDistance = Calc.sub(sTabPos, Calc.add(sMarginLeft, sTextIndent));
}
else if ("space".equals(sFormat)) {
sSpaceChar="\\ ";

View file

@ -36,6 +36,7 @@ import writer2latex.latex.util.Context;
import writer2latex.office.ListStyle;
import writer2latex.office.OfficeReader;
import writer2latex.office.XMLString;
import writer2latex.util.Calc;
import writer2latex.util.Misc;
public class ListConverter extends StyleConverter {
@ -405,7 +406,7 @@ public class ListConverter extends StyleConverter {
// This defines the position of an additional tab stop, which really means the start position of the text *after* the label
String sTabPos = style.getLevelStyleProperty(i, XMLString.TEXT_LIST_TAB_STOP_POSITION);
if (sTabPos==null) { sTabPos = "0cm"; }
sTheLabel += "\\hspace{"+Misc.sub(sTabPos, Misc.add(sMarginLeft, sTextIndent))+"}";
sTheLabel += "\\hspace{"+Calc.sub(sTabPos, Calc.add(sMarginLeft, sTextIndent))+"}";
}
}
@ -433,7 +434,7 @@ public class ListConverter extends StyleConverter {
.append("\\newenvironment{")
.append(sLevelName[i]).append("}{")
.append("\\def\\writerlistleftskip{\\addtolength\\leftskip{")
.append(Misc.add(sSpaceBefore,sLabelWidth)).append("}}")
.append(Calc.add(sSpaceBefore,sLabelWidth)).append("}}")
.append("\\def\\writerlistparindent{}")
.append("\\def\\writerlistlabel{}");
// Redefine \item

View file

@ -32,6 +32,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.Node;
import writer2latex.util.CSVList;
import writer2latex.util.Calc;
import writer2latex.util.Misc;
import writer2latex.office.*;
import writer2latex.latex.util.BeforeAfter;
@ -393,22 +394,22 @@ public class PageStyleConverter extends StyleConverter {
if (layout!=null) {
if (layout.hasHeaderStyle()) {
String sThisHeadHeight = layout.getHeaderProperty(XMLString.FO_MIN_HEIGHT);
if (sThisHeadHeight!=null && Misc.isLessThan(sHeadHeight,sThisHeadHeight)) {
if (sThisHeadHeight!=null && Calc.isLessThan(sHeadHeight,sThisHeadHeight)) {
sHeadHeight = sThisHeadHeight;
}
String sThisHeadSep = layout.getHeaderProperty(XMLString.FO_MARGIN_BOTTOM);
if (sThisHeadSep!=null && Misc.isLessThan(sHeadSep,sThisHeadSep)) {
if (sThisHeadSep!=null && Calc.isLessThan(sHeadSep,sThisHeadSep)) {
sHeadSep = sThisHeadSep;
}
bIncludeHead = true;
}
if (layout.hasFooterStyle()) {
String sThisFootHeight = layout.getFooterProperty(XMLString.FO_MIN_HEIGHT);
if (sThisFootHeight!=null && Misc.isLessThan(sFootHeight,sThisFootHeight)) {
if (sThisFootHeight!=null && Calc.isLessThan(sFootHeight,sThisFootHeight)) {
sFootHeight = sThisFootHeight;
}
String sThisFootSep = layout.getFooterProperty(XMLString.FO_MARGIN_TOP);
if (sThisFootSep!=null && Misc.isLessThan(sFootSep,sThisFootSep)) {
if (sThisFootSep!=null && Calc.isLessThan(sFootSep,sThisFootSep)) {
sFootSep = sThisFootSep;
}
bIncludeFoot = true;
@ -417,14 +418,14 @@ public class PageStyleConverter extends StyleConverter {
}
}
// Define 12pt as minimum height (the source may specify 0pt..)
if (bIncludeHead && Misc.isLessThan(sHeadHeight,"12pt")) {
if (bIncludeHead && Calc.isLessThan(sHeadHeight,"12pt")) {
sHeadHeight = "12pt";
}
if (bIncludeFoot && Misc.isLessThan(sFootHeight,"12pt")) {
if (bIncludeFoot && Calc.isLessThan(sFootHeight,"12pt")) {
sFootHeight = "12pt";
}
String sFootSkip = Misc.add(sFootHeight,sFootSep);
String sFootSkip = Calc.add(sFootHeight,sFootSep);
if (config.useGeometry()) {
// Set up options for geometry.sty
@ -459,13 +460,13 @@ public class PageStyleConverter extends StyleConverter {
}
else {
// Calculate text height and text width
String sTextHeight = Misc.sub(sPaperHeight,sMarginTop);
sTextHeight = Misc.sub(sTextHeight,sHeadHeight);
sTextHeight = Misc.sub(sTextHeight,sHeadSep);
sTextHeight = Misc.sub(sTextHeight,sFootSkip);
sTextHeight = Misc.sub(sTextHeight,sMarginBottom);
String sTextWidth = Misc.sub(sPaperWidth,sMarginLeft);
sTextWidth = Misc.sub(sTextWidth,sMarginRight);
String sTextHeight = Calc.sub(sPaperHeight,sMarginTop);
sTextHeight = Calc.sub(sTextHeight,sHeadHeight);
sTextHeight = Calc.sub(sTextHeight,sHeadSep);
sTextHeight = Calc.sub(sTextHeight,sFootSkip);
sTextHeight = Calc.sub(sTextHeight,sMarginBottom);
String sTextWidth = Calc.sub(sPaperWidth,sMarginLeft);
sTextWidth = Calc.sub(sTextWidth,sMarginRight);
ldp.append("% Page layout (geometry)").nl();
@ -514,12 +515,12 @@ public class PageStyleConverter extends StyleConverter {
if (sHeight==null) { sHeight = "0.2mm"; }
String sWidth = mainPageLayout.getFootnoteProperty(XMLString.STYLE_REL_WIDTH);
if (sWidth==null) { sWidth = "25%"; }
sWidth=Float.toString(Misc.getFloat(sWidth.substring(0,sWidth.length()-1),1)/100);
sWidth=Float.toString(Calc.getFloat(sWidth.substring(0,sWidth.length()-1),1)/100);
BeforeAfter baColor = new BeforeAfter();
String sColor = mainPageLayout.getFootnoteProperty(XMLString.STYLE_COLOR);
palette.getColorCv().applyColor(sColor,false,baColor,new Context());
String sSkipFootins = Misc.add(sBefore,sHeight);
String sSkipFootins = Calc.add(sBefore,sHeight);
ldp.append("% Footnote rule").nl()
.append("\\setlength{\\skip\\footins}{").append(sSkipFootins).append("}").nl()
@ -582,7 +583,7 @@ public class PageStyleConverter extends StyleConverter {
}
private boolean compare(String sLength1, String sLength2, String sTolerance) {
return Misc.isLessThan(Misc.abs(Misc.sub(sLength1,sLength2)),sTolerance);
return Calc.isLessThan(Calc.abs(Calc.sub(sLength1,sLength2)),sTolerance);
}
/* Helper: Get display name, or original name if it doesn't exist */

View file

@ -35,7 +35,7 @@ import writer2latex.latex.util.StyleMap;
import writer2latex.office.OfficeReader;
import writer2latex.office.StyleWithProperties;
import writer2latex.office.XMLString;
import writer2latex.util.Misc;
import writer2latex.util.Calc;
/* <p>This class converts OpenDocument paragraphs (<code>text:p</code>) and
* paragraph styles/formatting into LaTeX</p>
@ -493,7 +493,7 @@ public class ParConverter extends StyleConverter {
if (style==null) { return; }
String sLineHeight = style.getProperty(XMLString.FO_LINE_HEIGHT);
if (sLineHeight==null || !sLineHeight.endsWith("%")) { return; }
float fPercent=Misc.getFloat(sLineHeight.substring(0,sLineHeight.length()-1),100);
float fPercent=Calc.getFloat(sLineHeight.substring(0,sLineHeight.length()-1),100);
// Do not allow less that 120% (LaTeX default)
if (fPercent<120) { fPercent = 120; }
ba.add("\\renewcommand\\baselinestretch{"+fPercent/120+"}","");
@ -552,10 +552,10 @@ public class ParConverter extends StyleConverter {
bParFill = true; // justified paragraph with ragged last line
}
// Create formatting:
String sRubberMarginTop = Misc.multiply("10%",sMarginTop);
if (Misc.length2px(sRubberMarginTop).equals("0")) { sRubberMarginTop="1pt"; }
String sRubberMarginBottom = Misc.multiply("10%",sMarginBottom);
if (Misc.length2px(sRubberMarginBottom).equals("0")) { sRubberMarginBottom="1pt"; }
String sRubberMarginTop = Calc.multiply("10%",sMarginTop);
if (Calc.length2px(sRubberMarginTop).equals("0")) { sRubberMarginTop="1pt"; }
String sRubberMarginBottom = Calc.multiply("10%",sMarginBottom);
if (Calc.length2px(sRubberMarginBottom).equals("0")) { sRubberMarginBottom="1pt"; }
ba.add("\\setlength\\leftskip{"+sMarginLeft+(bRaggedLeft?" plus 1fil":"")+"}","");
ba.add("\\setlength\\rightskip{"+sMarginRight+(bRaggedRight?" plus 1fil":"")+"}","");
ba.add("\\setlength\\parindent{"+sTextIndent+"}","");

View file

@ -1495,34 +1495,34 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
if (curToken.eType==Token.PLUS){
nextToken();
if (curToken.eType==Token.NUMBER){
fSize+=Misc.getFloat(curToken.sLaTeX,0);
fSize+=Calc.getFloat(curToken.sLaTeX,0);
nextToken();
} // else error in formula: ignore
}
else if(curToken.eType==Token.MINUS){
nextToken();
if (curToken.eType==Token.NUMBER){
fSize-=Misc.getFloat(curToken.sLaTeX,0);
fSize-=Calc.getFloat(curToken.sLaTeX,0);
nextToken();
} // else error in formula: ignore
}
else if(curToken.eType==Token.MULTIPLY){
nextToken();
if (curToken.eType==Token.NUMBER){
fSize*=Misc.getFloat(curToken.sLaTeX,1);
fSize*=Calc.getFloat(curToken.sLaTeX,1);
nextToken();
} // else error in formula: ignore
}
else if(curToken.eType==Token.DIVIDEBY){
nextToken();
if (curToken.eType==Token.NUMBER){
float f=Misc.getFloat(curToken.sLaTeX,1);
float f=Calc.getFloat(curToken.sLaTeX,1);
if (f!=0) {fSize/=f;}
nextToken();
} // else error in formula: ignore
}
else if (curToken.eType==Token.NUMBER){
fSize=Misc.getFloat(curToken.sLaTeX,fSize);
fSize=Calc.getFloat(curToken.sLaTeX,fSize);
nextToken();
} // else error in formula: ignore
return term(fSize,eAlign);

View file

@ -381,7 +381,7 @@ public class TableFormatter extends ConverterHelper {
}
else if (!bIsTabulary) {
// note: The column width in OOo includes padding, which we subtract
ba.add("m{"+Misc.add(sColumnWidth[nCol],"-0.2cm")+"}","");
ba.add("m{"+Calc.add(sColumnWidth[nCol],"-0.2cm")+"}","");
}
else {
ba.add("J","");
@ -450,9 +450,9 @@ public class TableFormatter extends ConverterHelper {
// calculate column width
String sTotalColumnWidth = sColumnWidth[nCol];
for (int i=nCol+1; i<nCol+nColSpan; i++) {
sTotalColumnWidth = Misc.add(sTotalColumnWidth,sColumnWidth[i]);
sTotalColumnWidth = Calc.add(sTotalColumnWidth,sColumnWidth[i]);
}
sTotalColumnWidth = Misc.add(sTotalColumnWidth,"-0.2cm");
sTotalColumnWidth = Calc.add(sTotalColumnWidth,"-0.2cm");
if (bNeedAlign || bNeedLeft || bNeedRight || nColSpan>1) {
ba.add("\\multicolumn{"+nColSpan+"}{","");