diff --git a/source/java/writer2latex/latex/CharStyleConverter.java b/source/java/writer2latex/latex/CharStyleConverter.java index f5454e7..cae32e2 100644 --- a/source/java/writer2latex/latex/CharStyleConverter.java +++ b/source/java/writer2latex/latex/CharStyleConverter.java @@ -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 diff --git a/source/java/writer2latex/latex/DrawConverter.java b/source/java/writer2latex/latex/DrawConverter.java index 99b529d..80ada90 100644 --- a/source/java/writer2latex/latex/DrawConverter.java +++ b/source/java/writer2latex/latex/DrawConverter.java @@ -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(); } diff --git a/source/java/writer2latex/latex/HeadingConverter.java b/source/java/writer2latex/latex/HeadingConverter.java index 7b14a15..d4a0d12 100644 --- a/source/java/writer2latex/latex/HeadingConverter.java +++ b/source/java/writer2latex/latex/HeadingConverter.java @@ -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 (text:h) 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="\\ "; diff --git a/source/java/writer2latex/latex/ListConverter.java b/source/java/writer2latex/latex/ListConverter.java index c266334..a754d9c 100644 --- a/source/java/writer2latex/latex/ListConverter.java +++ b/source/java/writer2latex/latex/ListConverter.java @@ -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 diff --git a/source/java/writer2latex/latex/PageStyleConverter.java b/source/java/writer2latex/latex/PageStyleConverter.java index 2889408..7a7a62c 100644 --- a/source/java/writer2latex/latex/PageStyleConverter.java +++ b/source/java/writer2latex/latex/PageStyleConverter.java @@ -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 */ diff --git a/source/java/writer2latex/latex/ParConverter.java b/source/java/writer2latex/latex/ParConverter.java index 5baadac..fbe1b31 100644 --- a/source/java/writer2latex/latex/ParConverter.java +++ b/source/java/writer2latex/latex/ParConverter.java @@ -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; /*

This class converts OpenDocument paragraphs (text:p) and * paragraph styles/formatting into LaTeX

@@ -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+"}",""); diff --git a/source/java/writer2latex/latex/StarMathConverter.java b/source/java/writer2latex/latex/StarMathConverter.java index 1f6de3c..dfdcc36 100644 --- a/source/java/writer2latex/latex/StarMathConverter.java +++ b/source/java/writer2latex/latex/StarMathConverter.java @@ -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); diff --git a/source/java/writer2latex/latex/TableFormatter.java b/source/java/writer2latex/latex/TableFormatter.java index 967578e..ffcd608 100644 --- a/source/java/writer2latex/latex/TableFormatter.java +++ b/source/java/writer2latex/latex/TableFormatter.java @@ -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; i1) { ba.add("\\multicolumn{"+nColSpan+"}{",""); diff --git a/source/java/writer2latex/office/OfficeReader.java b/source/java/writer2latex/office/OfficeReader.java index 2f791cc..9e85c59 100644 --- a/source/java/writer2latex/office/OfficeReader.java +++ b/source/java/writer2latex/office/OfficeReader.java @@ -39,6 +39,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Document; + import writer2latex.util.Misc; /**

This class reads and collects global information about an OOo document. diff --git a/source/java/writer2latex/office/StyleWithProperties.java b/source/java/writer2latex/office/StyleWithProperties.java index a86679c..08b04eb 100644 --- a/source/java/writer2latex/office/StyleWithProperties.java +++ b/source/java/writer2latex/office/StyleWithProperties.java @@ -27,6 +27,7 @@ package writer2latex.office; //import org.w3c.dom.Element; import org.w3c.dom.Node; +import writer2latex.util.Calc; //import org.w3c.dom.NamedNodeMap; //import java.util.Hashtable; import writer2latex.util.Misc; @@ -139,7 +140,7 @@ public class StyleWithProperties extends OfficeStyle { int nRealIndex = bIsOldProps ? OLDPROPS : nIndex; if (properties[nRealIndex].containsProperty(sName)) { String sValue = properties[nRealIndex].getProperty(sName); - return Misc.truncateLength(sValue); + return Calc.truncateLength(sValue); } else if (bInherit && getParentName()!=null) { StyleWithProperties parentStyle = (StyleWithProperties) family.getStyle(getParentName()); @@ -206,16 +207,16 @@ public class StyleWithProperties extends OfficeStyle { = (StyleWithProperties) family.getStyle(getParentName()); if (parentStyle!=null) { String sParentValue = parentStyle.getAbsoluteProperty(nIndex,sProperty); - if (sParentValue!=null) { return Misc.multiply(sValue,sParentValue); } + if (sParentValue!=null) { return Calc.multiply(sValue,sParentValue); } } else if (getFamily()!=null && getFamily().getDefaultStyle()!=null) { StyleWithProperties style = (StyleWithProperties) getFamily().getDefaultStyle(); String sDefaultValue=(String) style.getProperty(nIndex,sProperty,false); - if (sValue !=null) { return Misc.multiply(sValue,sDefaultValue); } + if (sValue !=null) { return Calc.multiply(sValue,sDefaultValue); } } } else { - return Misc.truncateLength(sValue); + return Calc.truncateLength(sValue); } } else if (getParentName()!=null){ diff --git a/source/java/writer2latex/office/TableReader.java b/source/java/writer2latex/office/TableReader.java index eaff414..7abafdc 100644 --- a/source/java/writer2latex/office/TableReader.java +++ b/source/java/writer2latex/office/TableReader.java @@ -32,6 +32,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import writer2latex.util.Calc; import writer2latex.util.Misc; /** @@ -137,7 +138,7 @@ public class TableReader { else if (sTableWidth!=null){ // Calculate the relative column width from the absolute column widths // This may not add up to exactly 100%, but we will live with that - sRelColWidth[nCol] = Misc.divide(sColWidth[nCol], sTableWidth, true); + sRelColWidth[nCol] = Calc.divide(sColWidth[nCol], sTableWidth, true); } else { // The table has not width, distribute the columns evenly @@ -471,7 +472,7 @@ public class TableReader { int nCols = Misc.getPosInteger(cell.getAttribute(XMLString.TABLE_NUMBER_COLUMNS_SPANNED),1); String sWidth = sColWidth[nCol]; for (int i=nCol+1; i0) { + // Never return less that 1px + return Float.toString(fPixels<1 ? 1 : fPixels)+"px"; + } + else { + // Or above -1px + return Float.toString(fPixels>-1 ? -1 : fPixels)+"px"; + } + } + + /** Divide dividend by divisor and return the quotient as an integer percentage + * (e.g. "0.5cm" divided by "2cm" returns "25%"). + * Exception: Never returns below 1% except if the dividend is zero. + * + * @param sDividend the length to use as dividend + * @param sDivisor the length to use as divisor + * @return the quotient percentage + */ + public static final String divide(String sDividend, String sDivisor) { + return divide(sDividend,sDivisor,false); + } + + /** Divide dividend by divisor and return the quotient as an integer percentage + * (e.g. "0.5cm" divided by "2cm" returns "25%"). + * Exception: Never returns below 1% except if the dividend is zero, and never returns above 100% + * if last parameter is true. + * + * @param sDividend the length to use as dividend + * @param sDivisor the length to use as divisor + * @param bMax100 true if a maximum of 100% should be returned + * @return the quotient percentage + */ + public static final String divide(String sDividend, String sDivisor, boolean bMax100) { + if (sDividend.equals("0")) { return "0%"; } + if (sDivisor.equals("0")) { return "100%"; } + + float fDividend=getFloat(sDividend.substring(0,sDividend.length()-2),1); + String sDividendUnit=sDividend.substring(sDividend.length()-2); + float fDivisor=getFloat(sDivisor.substring(0,sDivisor.length()-2),1); + String sDivisorUnit=sDivisor.substring(sDivisor.length()-2); + int nPercent = Math.round(100*fDividend*getUpi(sDivisorUnit)/fDivisor/getUpi(sDividendUnit)); + if (bMax100 && nPercent>100) { + return "100%"; + } + else if (nPercent>0) { + return Integer.toString(nPercent)+"%"; + } + else { + return "1%"; + } + } + + /** Multiply a length by a percentage (e.g. "150%" multiplied with "2.5mm" returns "3.75cm") + * + * @param sPercent the percentage + * @param sLength the length + * @return the product length + */ + public static final String multiply(String sPercent, String sLength){ + if (sLength.equals("0")) { return "0"; } + float fPercent=getFloat(sPercent.substring(0,sPercent.length()-1),1); + float fLength=getFloat(sLength.substring(0,sLength.length()-2),1); + String sUnit=sLength.substring(sLength.length()-2); + return Float.toString(fPercent*fLength/100)+sUnit; + } + + /** Add two lengths (e.g. "2.5cm" added to "1.08cm" returns "3.58cm") + * + * @param sLength1 the first length term + * @param sLength2 the second length term + * @return the sum (as a length with the same unit as the first term) + */ + public static final String add(String sLength1, String sLength2){ + if (sLength1.equals("0")) { return sLength2; } + if (sLength2.equals("0")) { return sLength1; } + float fLength1=getFloat(sLength1.substring(0,sLength1.length()-2),1); + String sUnit1=sLength1.substring(sLength1.length()-2); + float fLength2=getFloat(sLength2.substring(0,sLength2.length()-2),1); + String sUnit2=sLength2.substring(sLength2.length()-2); + // Use unit from sLength1: + return Float.toString(fLength1+getUpi(sUnit1)/getUpi(sUnit2)*fLength2)+sUnit1; + } + + /** Subtract two lengths (e.g. "2.5cm" subtracted by "1.08cm" returns "1.42cm") + * + * @param sLength1 the first length term + * @param sLength2 the second length term + * @return the difference (as a length with the same unit as the first term) + */ + public static final String sub(String sLength1, String sLength2){ + return add(sLength1,multiply("-100%",sLength2)); + } + + /** Test whether a given length is smaller than another length + * (e.g. "2.5cm" compared to "2.6cm" returns true; + * + * @param sThis is this length the smaller? + * @param sThat is this length the larger? + * @return true is the first length is smaller than the second length + */ + public static boolean isLessThan(String sThis, String sThat) { + return sub(sThis,sThat).startsWith("-"); + } + + /** Get the absolute value of a length (e.g. "-2.5cm" returns "2.5cm") + * + * @param sLength the length + * @return the absolute value + */ + public static String abs(String sLength) { + return sLength.startsWith("-") ? sLength.substring(1) : sLength; + } + +} diff --git a/source/java/writer2latex/util/Misc.java b/source/java/writer2latex/util/Misc.java index d424418..748ee92 100644 --- a/source/java/writer2latex/util/Misc.java +++ b/source/java/writer2latex/util/Misc.java @@ -31,7 +31,6 @@ import java.io.File; import java.io.InputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.lang.Math; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; @@ -142,28 +141,6 @@ public class Misc{ return int2alph(number,bLetterSync).toUpperCase(); } - public static final int getPosInteger(String sInteger, int nDefault){ - int n; - try { - n=Integer.parseInt(sInteger); - } - catch (NumberFormatException e) { - return nDefault; - } - return n>0 ? n : nDefault; - } - - public static final float getFloat(String sFloat, float fDefault){ - float f; - try { - f=Float.parseFloat(sFloat); - } - catch (NumberFormatException e) { - return fDefault; - } - return f; - } - public static final int getIntegerFromHex(String sHex, int nDefault){ int n; try { @@ -175,109 +152,6 @@ public class Misc{ return n; } - public static String truncateLength(String sValue) { - if (sValue.endsWith("inch")) { - // Cut of inch to in - return sValue.substring(0,sValue.length()-2); - } - else { - return sValue; - } - } - - public static boolean isZero(String sValue) { - return Math.abs(getFloat(sValue.substring(0, sValue.length()-2),0))<0.001; - } - - // Return units per inch for some unit - private static final float getUpi(String sUnit) { - if ("in".equals(sUnit)) { return 1.0F; } - else if ("mm".equals(sUnit)) { return 25.4F; } - else if ("cm".equals(sUnit)) { return 2.54F; } - else if ("pc".equals(sUnit)) { return 6F; } - else { return 72; } // pt or unknown - } - - // Convert a length to px assuming 96ppi (cf. css spec) - // Exception: Never return less than 1px - public static final String length2px(String sLength) { - if (sLength.equals("0")) { return "0"; } - float fLength=getFloat(sLength.substring(0,sLength.length()-2),1); - String sUnit=sLength.substring(sLength.length()-2); - float fPixels = 96.0F/getUpi(sUnit)*fLength; - if (Math.abs(fPixels)<0.01) { - // Very small, treat as zero - return "0"; - } - else if (fPixels>0) { - // Never return less that 1px - return Float.toString(fPixels<1 ? 1 : fPixels)+"px"; - } - else { - // Or above -1px - return Float.toString(fPixels>-1 ? -1 : fPixels)+"px"; - } - } - - // Divide dividend by divisor and return the quotient as an integer percentage - // (never below 1% except if the dividend is zero) - public static final String divide(String sDividend, String sDivisor) { - return divide(sDividend,sDivisor,false); - } - - // Divide dividend by divisor and return the quotient as an integer percentage - // (never below 1% except if the dividend is zero, and never above 100% if last parameter is true) - public static final String divide(String sDividend, String sDivisor, boolean bMax100) { - if (sDividend.equals("0")) { return "0%"; } - if (sDivisor.equals("0")) { return "100%"; } - - float fDividend=getFloat(sDividend.substring(0,sDividend.length()-2),1); - String sDividendUnit=sDividend.substring(sDividend.length()-2); - float fDivisor=getFloat(sDivisor.substring(0,sDivisor.length()-2),1); - String sDivisorUnit=sDivisor.substring(sDivisor.length()-2); - int nPercent = Math.round(100*fDividend*getUpi(sDivisorUnit)/fDivisor/getUpi(sDividendUnit)); - if (bMax100 && nPercent>100) { - return "100%"; - } - else if (nPercent>0) { - return Integer.toString(nPercent)+"%"; - } - else { - return "1%"; - } - } - - public static final String multiply(String sPercent, String sLength){ - if (sLength.equals("0")) { return "0"; } - float fPercent=getFloat(sPercent.substring(0,sPercent.length()-1),1); - float fLength=getFloat(sLength.substring(0,sLength.length()-2),1); - String sUnit=sLength.substring(sLength.length()-2); - return Float.toString(fPercent*fLength/100)+sUnit; - } - - public static final String add(String sLength1, String sLength2){ - if (sLength1.equals("0")) { return sLength2; } - if (sLength2.equals("0")) { return sLength1; } - float fLength1=getFloat(sLength1.substring(0,sLength1.length()-2),1); - String sUnit1=sLength1.substring(sLength1.length()-2); - float fLength2=getFloat(sLength2.substring(0,sLength2.length()-2),1); - String sUnit2=sLength2.substring(sLength2.length()-2); - // Use unit from sLength1: - return Float.toString(fLength1+getUpi(sUnit1)/getUpi(sUnit2)*fLength2)+sUnit1; - } - - public static final String sub(String sLength1, String sLength2){ - return add(sLength1,multiply("-100%",sLength2)); - } - - public static boolean isLessThan(String sThis, String sThat) { - return sub(sThis,sThat).startsWith("-"); - } - - public static String abs(String sLength) { - return sLength.startsWith("-") ? sLength.substring(1) : sLength; - } - /** Make a file name TeX friendly, replacing offending characters * * @param sFileName the file name @@ -587,6 +461,17 @@ public class Misc{ } return baos.toByteArray(); } + + public static final int getPosInteger(String sInteger, int nDefault){ + int n; + try { + n=Integer.parseInt(sInteger); + } + catch (NumberFormatException e) { + return nDefault; + } + return n>0 ? n : nDefault; + } diff --git a/source/java/writer2latex/xhtml/DrawConverter.java b/source/java/writer2latex/xhtml/DrawConverter.java index ce19cdc..1ee7a58 100644 --- a/source/java/writer2latex/xhtml/DrawConverter.java +++ b/source/java/writer2latex/xhtml/DrawConverter.java @@ -56,6 +56,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Element; +import writer2latex.util.Calc; import writer2latex.util.Misc; import writer2latex.util.CSVList; import writer2latex.util.SimpleXMLParser; @@ -459,9 +460,9 @@ public class DrawConverter extends ConverterHelper { // It is if the image width exceeds a certain percentage of the current text width and the height is // greater than 1.33*the width (recommended by Michel "Coolmicro") if (sWidth!=null && sHeight!=null && - Misc.sub(Misc.multiply("133%",sWidth), sHeight).startsWith("-") && - Misc.sub(Misc.multiply(sImageSplit,converter.getContentWidth()), - Misc.multiply(sScale,Misc.truncateLength(sWidth))).startsWith("-")) { + Calc.sub(Calc.multiply("133%",sWidth), sHeight).startsWith("-") && + Calc.sub(Calc.multiply(sImageSplit,converter.getContentWidth()), + Calc.multiply(sScale,Calc.truncateLength(sWidth))).startsWith("-")) { fullscreenFrames.add(onode); return; } @@ -942,18 +943,18 @@ public class DrawConverter extends ConverterHelper { if (style!=null) { // Subtract padding String s = style.getProperty(XMLString.FO_PADDING_LEFT); - if (s!=null) sWidth = Misc.sub(sWidth, s); + if (s!=null) sWidth = Calc.sub(sWidth, s); s = style.getProperty(XMLString.FO_PADDING_RIGHT); - if (s!=null) sWidth = Misc.sub(sWidth, s); + if (s!=null) sWidth = Calc.sub(sWidth, s); s = style.getProperty(XMLString.FO_PADDING); - if (s!=null) sWidth = Misc.sub(sWidth, Misc.multiply("200%", s)); + if (s!=null) sWidth = Calc.sub(sWidth, Calc.multiply("200%", s)); // Subtract border s = style.getProperty(XMLString.FO_BORDER_LEFT); - if (s!=null) sWidth = Misc.sub(sWidth, getTableCv().borderWidth(s)); + if (s!=null) sWidth = Calc.sub(sWidth, getTableCv().borderWidth(s)); s = style.getProperty(XMLString.FO_BORDER_RIGHT); - if (s!=null) sWidth = Misc.sub(sWidth, getTableCv().borderWidth(s)); + if (s!=null) sWidth = Calc.sub(sWidth, getTableCv().borderWidth(s)); s = style.getProperty(XMLString.FO_BORDER); - if (s!=null) sWidth = Misc.sub(sWidth, Misc.multiply("200%", getTableCv().borderWidth(s))); + if (s!=null) sWidth = Calc.sub(sWidth, Calc.multiply("200%", getTableCv().borderWidth(s))); } return sWidth; } @@ -967,18 +968,18 @@ public class DrawConverter extends ConverterHelper { if (style!=null) { // Subtract padding String s = style.getProperty(XMLString.FO_PADDING_TOP); - if (s!=null) sHeight = Misc.sub(sHeight, s); + if (s!=null) sHeight = Calc.sub(sHeight, s); s = style.getProperty(XMLString.FO_PADDING_BOTTOM); - if (s!=null) sHeight = Misc.sub(sHeight, s); + if (s!=null) sHeight = Calc.sub(sHeight, s); s = style.getProperty(XMLString.FO_PADDING); - if (s!=null) sHeight = Misc.sub(sHeight, Misc.multiply("200%", s)); + if (s!=null) sHeight = Calc.sub(sHeight, Calc.multiply("200%", s)); // Subtract border s = style.getProperty(XMLString.FO_BORDER_TOP); - if (s!=null) sHeight = Misc.sub(sHeight, getTableCv().borderWidth(s)); + if (s!=null) sHeight = Calc.sub(sHeight, getTableCv().borderWidth(s)); s = style.getProperty(XMLString.FO_BORDER_BOTTOM); - if (s!=null) sHeight = Misc.sub(sHeight, getTableCv().borderWidth(s)); + if (s!=null) sHeight = Calc.sub(sHeight, getTableCv().borderWidth(s)); s = style.getProperty(XMLString.FO_BORDER); - if (s!=null) sHeight = Misc.sub(sHeight, Misc.multiply("200%", getTableCv().borderWidth(s))); + if (s!=null) sHeight = Calc.sub(sHeight, Calc.multiply("200%", getTableCv().borderWidth(s))); } return sHeight; } @@ -1018,7 +1019,7 @@ public class DrawConverter extends ConverterHelper { else { String sWidth = getFrameWidth(node, ofr.getFrameStyle(node.getAttribute(XMLString.DRAW_STYLE_NAME))); if (sWidth!=null) { - props.addValue("width", Misc.divide(Misc.multiply(sScale,Misc.truncateLength(sWidth)),converter.getContentWidth())); + props.addValue("width", Calc.divide(Calc.multiply(sScale,Calc.truncateLength(sWidth)),converter.getContentWidth())); } return sWidth; } @@ -1039,9 +1040,9 @@ public class DrawConverter extends ConverterHelper { StyleWithProperties style = ofr.getFrameStyle(node.getAttribute(XMLString.DRAW_STYLE_NAME)); if (style!=null) { String s = style.getProperty(XMLString.FO_MARGIN_TOP); - if (s!=null) sX=Misc.sub(sX,s); + if (s!=null) sX=Calc.sub(sX,s); s = style.getProperty(XMLString.FO_MARGIN_LEFT); - if (s!=null) sY=Misc.sub(sY,s); + if (s!=null) sY=Calc.sub(sY,s); } props.addValue("position","absolute"); @@ -1159,10 +1160,10 @@ public class DrawConverter extends ConverterHelper { // TODO: Move to ConverterHelper.java private String scale(String s) { if (bConvertToPx) { - return Misc.length2px(Misc.multiply(sScale,Misc.truncateLength(s))); + return Calc.length2px(Calc.multiply(sScale,Calc.truncateLength(s))); } else { - return Misc.multiply(sScale,Misc.truncateLength(s)); + return Calc.multiply(sScale,Calc.truncateLength(s)); } } } diff --git a/source/java/writer2latex/xhtml/FrameStyleConverter.java b/source/java/writer2latex/xhtml/FrameStyleConverter.java index 4826b05..5742d4b 100644 --- a/source/java/writer2latex/xhtml/FrameStyleConverter.java +++ b/source/java/writer2latex/xhtml/FrameStyleConverter.java @@ -33,7 +33,7 @@ import writer2latex.office.OfficeStyleFamily; import writer2latex.office.StyleWithProperties; import writer2latex.office.XMLString; import writer2latex.util.CSVList; -import writer2latex.util.Misc; +import writer2latex.util.Calc; //import writer2latex.util.Misc; import writer2latex.util.SimpleInputBuffer; @@ -275,7 +275,7 @@ public class FrameStyleConverter extends StyleWithPropertiesConverterHelper { if ('0'<=in.peekChar() && in.peekChar()<='9') { String sDim = scale(in.getNumber()+in.getIdentifier()); // Do not output a border less than 1px wide - some browsers will render it invisible - out.append(Misc.isLessThan(sDim, "1px") ? "1px" : sDim); + out.append(Calc.isLessThan(sDim, "1px") ? "1px" : sDim); } // skip other characters while (in.peekChar()!=' ' && in.peekChar()!='\0') { diff --git a/source/java/writer2latex/xhtml/PageStyleConverter.java b/source/java/writer2latex/xhtml/PageStyleConverter.java index fa472ca..e532672 100644 --- a/source/java/writer2latex/xhtml/PageStyleConverter.java +++ b/source/java/writer2latex/xhtml/PageStyleConverter.java @@ -35,7 +35,7 @@ import writer2latex.office.PageLayout; import writer2latex.office.StyleWithProperties; import writer2latex.office.XMLString; import writer2latex.util.CSVList; -import writer2latex.util.Misc; +import writer2latex.util.Calc; /** * This class converts OpenDocument page styles to CSS2 styles. @@ -74,11 +74,11 @@ public class PageStyleConverter extends StyleConverterHelper { if (sWidth!=null) { String sMarginLeft = pageLayout.getProperty(XMLString.FO_MARGIN_LEFT); if (sMarginLeft!=null) { - sWidth = Misc.sub(sWidth, sMarginLeft); + sWidth = Calc.sub(sWidth, sMarginLeft); } String sMarginRight = pageLayout.getProperty(XMLString.FO_MARGIN_RIGHT); if (sMarginRight!=null) { - sWidth = Misc.sub(sWidth, sMarginRight); + sWidth = Calc.sub(sWidth, sMarginRight); } return sWidth; } diff --git a/source/java/writer2latex/xhtml/StyleConverterHelper.java b/source/java/writer2latex/xhtml/StyleConverterHelper.java index d958188..7785421 100644 --- a/source/java/writer2latex/xhtml/StyleConverterHelper.java +++ b/source/java/writer2latex/xhtml/StyleConverterHelper.java @@ -30,8 +30,8 @@ import writer2latex.office.OfficeReader; import writer2latex.office.OfficeStyleFamily; import writer2latex.office.StyleWithProperties; import writer2latex.office.XMLString; +import writer2latex.util.Calc; import writer2latex.util.ExportNameCollection; -import writer2latex.util.Misc; /** *

This is an abstract base class to convert an OpenDocument style family to @@ -73,15 +73,15 @@ public abstract class StyleConverterHelper extends ConverterHelper { protected String scale(String s) { if (bConvertToPx) { - return Misc.length2px(Misc.multiply(sScale,s)); + return Calc.length2px(Calc.multiply(sScale,s)); } else { - return Misc.multiply(sScale,s); + return Calc.multiply(sScale,s); } } protected String colScale(String s) { - return scale(Misc.multiply(sColScale,s)); + return scale(Calc.multiply(sColScale,s)); } /** Apply the writing direction (ltr or rtl) attribute from a style diff --git a/source/java/writer2latex/xhtml/TableConverter.java b/source/java/writer2latex/xhtml/TableConverter.java index 2777c42..b09309b 100644 --- a/source/java/writer2latex/xhtml/TableConverter.java +++ b/source/java/writer2latex/xhtml/TableConverter.java @@ -32,6 +32,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Element; +import writer2latex.util.Calc; import writer2latex.util.Misc; import writer2latex.util.SimpleInputBuffer; import writer2latex.office.XMLString; @@ -348,7 +349,7 @@ public class TableConverter extends ConverterHelper { if (sWidth!=null) { if (config.tableSize()==XhtmlConfig.RELATIVE){ // Force relative width - sWidth=Misc.divide(sWidth, converter.getContentWidth(), true); + sWidth=Calc.divide(sWidth, converter.getContentWidth(), true); info.props.addValue("width",sWidth); } else { @@ -413,31 +414,31 @@ public class TableConverter extends ConverterHelper { // "total cell width" - "border" - "padding" String s = style.getProperty(XMLString.FO_PADDING_LEFT); if (s!=null) { - sEdge=Misc.add(sEdge,getTableSc().colScale(s)); + sEdge=Calc.add(sEdge,getTableSc().colScale(s)); } s = style.getProperty(XMLString.FO_PADDING_RIGHT); if (s!=null) { - sEdge=Misc.add(sEdge,getTableSc().colScale(s)); + sEdge=Calc.add(sEdge,getTableSc().colScale(s)); } s = style.getProperty(XMLString.FO_PADDING); if (s!=null) { - sEdge=Misc.add(sEdge,Misc.multiply("200%",getTableSc().colScale(s))); + sEdge=Calc.add(sEdge,Calc.multiply("200%",getTableSc().colScale(s))); } s = style.getProperty(XMLString.FO_BORDER_LEFT); if (s!=null) { - sEdge=Misc.add(sEdge,getTableSc().colScale(borderWidth(s))); + sEdge=Calc.add(sEdge,getTableSc().colScale(borderWidth(s))); } s = style.getProperty(XMLString.FO_BORDER_RIGHT); if (s!=null) { - sEdge=Misc.add(sEdge,getTableSc().colScale(borderWidth(s))); + sEdge=Calc.add(sEdge,getTableSc().colScale(borderWidth(s))); } s = style.getProperty(XMLString.FO_BORDER); if (s!=null) { - sEdge=Misc.add(sEdge,Misc.multiply("200%",getTableSc().colScale(borderWidth(s)))); + sEdge=Calc.add(sEdge,Calc.multiply("200%",getTableSc().colScale(borderWidth(s)))); } if (sTotalWidth!=null) { - info.props.addValue("width",Misc.sub(getTableSc().colScale(sTotalWidth),sEdge)); + info.props.addValue("width",Calc.sub(getTableSc().colScale(sTotalWidth),sEdge)); } } diff --git a/source/java/writer2latex/xhtml/TextStyleConverter.java b/source/java/writer2latex/xhtml/TextStyleConverter.java index 0513de7..1379d35 100644 --- a/source/java/writer2latex/xhtml/TextStyleConverter.java +++ b/source/java/writer2latex/xhtml/TextStyleConverter.java @@ -35,8 +35,8 @@ import writer2latex.office.OfficeStyleFamily; import writer2latex.office.StyleWithProperties; import writer2latex.office.XMLString; import writer2latex.util.CSVList; +import writer2latex.util.Calc; import writer2latex.util.ExportNameCollection; -import writer2latex.util.Misc; /** * This class converts OpenDocument text styles to CSS2 styles. @@ -319,11 +319,11 @@ public class TextStyleConverter extends StyleWithPropertiesConverterHelper { } if (s!=null) { if (bRelativeFontSize) { - String sFontSize = Misc.divide(Misc.multiply(sFontScaling, Misc.multiply(s4,s)), sBaseFontSize); + String sFontSize = Calc.divide(Calc.multiply(sFontScaling, Calc.multiply(s4,s)), sBaseFontSize); if (!"100%".equals(sFontSize)) props.addValue("font-size", sFontSize); } else { - props.addValue("font-size",Misc.multiply(s4,scale(s))); + props.addValue("font-size",Calc.multiply(s4,scale(s))); } } else { @@ -335,7 +335,7 @@ public class TextStyleConverter extends StyleWithPropertiesConverterHelper { } else if (s!=null) { if (bRelativeFontSize) { - String sFontSize = Misc.divide(Misc.multiply(sFontScaling, s),sBaseFontSize); + String sFontSize = Calc.divide(Calc.multiply(sFontScaling, s),sBaseFontSize); if (!"100%".equals(sFontSize)) props.addValue("font-size", sFontSize); } else {