Skip to content

Commit

Permalink
Merge pull request #215 from metanorma/table_fo_if_performance
Browse files Browse the repository at this point in the history
string width calculation added, #214
  • Loading branch information
Intelligent2013 authored Jun 20, 2023
2 parents 4127d30 + 5f78f0a commit f1e136d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/main/java/org/metanorma/fop/PDFGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,11 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio
boolean isPostprocessing = isAddMathAsText || isAddAnnotations || isAddLineNumbers || isAddCommentaryPageNumbers;

if (isPostprocessing) {
if (isAddMathAsText) {
logger.info("Adding Math as text...");
}
logger.info("Starting post-processing...");

// release memory resources
sourceXMLDocument.flushResources();

logger.info("Transforming to Intermediate Format...");
xmlIF = generateFOPIntermediateFormat(src, fontcfg.getConfig(), pdf, false, "");

Expand Down Expand Up @@ -795,10 +797,10 @@ private String addHiddenMath(String sourceXML) {
FOPIFHiddenMathHandler fopIFHiddenMathHandler = new FOPIFHiddenMathHandler();
InputSource inputSource = new InputSource( new StringReader(sourceXML));
saxParser.parse(inputSource, fopIFHiddenMathHandler);
String result = fopIFHiddenMathHandler.getResultedXML();
StringBuilder result = fopIFHiddenMathHandler.getResultedXML();
Profiler.printProcessingTime(methodName, startMethodTime);
Profiler.removeMethodCall();
return result;
return result.toString();
}
catch (Exception ex) {
logger.severe("Can't update IF for hidden math.");
Expand Down Expand Up @@ -835,6 +837,8 @@ private String flatIFforXFDF(String sourceXML) {

private String createTableIF(String intermediateXML) {

logger.info("[INFO] Processing of Intermediate Format with information about the table's widths (table_if.xsl) ...");

String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName());
Profiler.addMethodCall(methodName);

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/metanorma/fop/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ public class Profiler {

protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME);


static ArrayList<String> arraysMethodCalls = new ArrayList<>();

static Stack<String> stackMethods = new Stack<>();

public static void addMethodCall(String methodName) {
debugShowMemoryInfo(false);
stackMethods.push(methodName);
String msg = String.join("", Collections.nCopies(stackMethods.size() * 2, " ")) + methodName;
arraysMethodCalls.add(msg);
}

public static void removeMethodCall() {
debugShowMemoryInfo(true);
try {
stackMethods.pop();
} catch (Exception ex) {};
Expand Down Expand Up @@ -59,4 +60,16 @@ public static void printFullProcessingTime() {
}
}

public static void debugShowMemoryInfo(boolean after) {
if (DEBUG) {
long heapSize = Runtime.getRuntime().totalMemory();
long heapFreeSize = Runtime.getRuntime().freeMemory();
long maxSize = Runtime.getRuntime().maxMemory();
String strAfter = after ? " after" : " before";
long usedSize = heapSize - heapFreeSize;
logger.log(Level.INFO, "Java used memory size" + strAfter + ": " + usedSize);
logger.log(Level.INFO, "Java available memory size" + strAfter + ": " + (maxSize - usedSize));
}
}

}
6 changes: 6 additions & 0 deletions src/main/java/org/metanorma/fop/SourceXMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,10 @@ public boolean hasMath() {
String element_math = readValue("//*[local-name() = 'math'][1]");
return element_math.length() != 0;
}

public void flushResources() {
sourceXML = null;
sourceXMLstr = "";
xmlFO = null;
}
}
14 changes: 10 additions & 4 deletions src/main/java/org/metanorma/fop/Util.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.metanorma.fop;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
Expand Down Expand Up @@ -75,6 +73,7 @@
import com.steadystate.css.dom.CSSStyleRuleImpl;
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.apache.fop.svg.SVGUtilities;
import org.metanorma.fop.fonts.FOPFont;
import org.metanorma.utils.LoggerHelper;
import org.w3c.css.sac.SelectorList;
Expand Down Expand Up @@ -566,7 +565,14 @@ public static String getFontSize(String text, String fontName, int width, int he
return String.valueOf((int)(fontSize * 1000));

}


public static int getStringWidth(String text, String fontName) {
int stringWidth = 0;
Font font = new Font(fontName, Font.PLAIN, 10);
stringWidth = (int)SVGUtilities.getStringWidth(text, font) * 100;
return stringWidth;
}

public static String saveFileToDisk(String filename, String content) {
try {
Files.createDirectories(tmpfilepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ private void updateStackChar(StringBuilder sb) {
}
}

public String getResultedXML() {
return sbResult.toString();
public StringBuilder getResultedXML() {
//return sbResult.toString(); // https://github.com/metanorma/mn2pdf/issues/214#issuecomment-1599200350
return sbResult;
}

}

0 comments on commit f1e136d

Please sign in to comment.