Issue #2420 - fixed font memory leak in the setFont method
Change-Id: Ibf773c6df580cb65dd62b46f8fd127fde6585a57 Former-commit-id: 16effa6f2cda60d566e5f820ab517ed105a0894e
This commit is contained in:
parent
7da1197e7a
commit
9c159e7c40
1 changed files with 72 additions and 55 deletions
|
@ -42,8 +42,9 @@ import com.raytheon.uf.common.status.UFStatus.Priority;
|
|||
* ------------ ---------- ----------- --------------------------
|
||||
* Sep 15, 2011 10557 rferrel Initial creation
|
||||
* Jul 17, 2012 14274 rferrel Now use eclipse Printer instead of awt.
|
||||
* Text is printed using same font as the GUI
|
||||
* Text is printed using same font as the GUI
|
||||
* Dec 31, 2012 15651 M Gamazaychikov Added setFont method to scale font for printing
|
||||
* Sep 30, 2013 #2420 lvenable Fixed memory leak in the setFont method.
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
|
@ -54,6 +55,12 @@ import com.raytheon.uf.common.status.UFStatus.Priority;
|
|||
public class PrintDisplay {
|
||||
public static void print(final String printedText, final FontData fontData,
|
||||
IUFStatusHandler statusHandler) {
|
||||
|
||||
if (printedText == null || printedText.trim().length() == 0) {
|
||||
// Do not waste paper when nothing to print.
|
||||
return;
|
||||
}
|
||||
|
||||
PrinterData data = Printer.getDefaultPrinterData();
|
||||
if (data == null) {
|
||||
statusHandler.handle(Priority.PROBLEM,
|
||||
|
@ -61,11 +68,6 @@ public class PrintDisplay {
|
|||
return;
|
||||
}
|
||||
|
||||
if (printedText == null || printedText.trim().length() == 0) {
|
||||
// Do not waste paper when nothing to print.
|
||||
return;
|
||||
}
|
||||
|
||||
final Printer printer = new Printer(data);
|
||||
PrintDisplay pd = new PrintDisplay(printer, printedText, fontData);
|
||||
pd.printJob();
|
||||
|
@ -93,7 +95,10 @@ public class PrintDisplay {
|
|||
|
||||
private int lineHeight;
|
||||
|
||||
StringBuilder wordBuffer;
|
||||
private StringBuilder wordBuffer;
|
||||
|
||||
/** Temp font. */
|
||||
private Font tmpFont = null;
|
||||
|
||||
int x;
|
||||
|
||||
|
@ -113,78 +118,90 @@ public class PrintDisplay {
|
|||
Thread thread = new Thread("Printing") {
|
||||
public void run() {
|
||||
printIt();
|
||||
|
||||
/** Dispose of the resources. */
|
||||
printer.dispose();
|
||||
|
||||
if (tmpFont != null && tmpFont.isDisposed() == false) {
|
||||
tmpFont.dispose();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}
|
||||
|
||||
protected void setFont() {
|
||||
/*
|
||||
* get the max number of characters in a line of text
|
||||
* and add a length of tab
|
||||
*/
|
||||
String [] textLines = textToPrint.split("[\n]");
|
||||
int maxChar = textLines[0].length();
|
||||
for ( int counter=1; counter<textLines.length; counter++){
|
||||
if (textLines[counter].length() > maxChar){
|
||||
maxChar = textLines[counter].length();
|
||||
}
|
||||
}
|
||||
maxChar = maxChar + 4;
|
||||
|
||||
/*
|
||||
* get the original font size and set the gc font.
|
||||
*/
|
||||
float origFontSize = printerFontData.getHeight();
|
||||
/*
|
||||
* get the max number of characters in a line of text and add a length
|
||||
* of tab
|
||||
*/
|
||||
String[] textLines = textToPrint.split("[\n]");
|
||||
int maxChar = textLines[0].length();
|
||||
for (int counter = 1; counter < textLines.length; counter++) {
|
||||
if (textLines[counter].length() > maxChar) {
|
||||
maxChar = textLines[counter].length();
|
||||
}
|
||||
}
|
||||
maxChar = maxChar + 4;
|
||||
|
||||
/*
|
||||
* get the original font size and set the gc font.
|
||||
*/
|
||||
float origFontSize = printerFontData.getHeight();
|
||||
Font printerFont = new Font(printer, printerFontData);
|
||||
gc.setFont(printerFont);
|
||||
|
||||
|
||||
/*
|
||||
* Create a buffer for computing line width in pixels.
|
||||
*/
|
||||
StringBuilder aBuffer = new StringBuilder(maxChar);
|
||||
for (int i = 0; i < maxChar; i++) {
|
||||
aBuffer.append(' ');
|
||||
aBuffer.append(' ');
|
||||
}
|
||||
/*
|
||||
* Get the line width in pixels and the device's width in pixels
|
||||
*/
|
||||
int lineWidthPixels = gc.stringExtent(aBuffer.toString()).x;
|
||||
int deviceWidthPixels = rightMargin - leftMargin;
|
||||
int deviceWidthPixels = rightMargin - leftMargin;
|
||||
|
||||
/*
|
||||
* Scale the original font size;
|
||||
*/
|
||||
float fontSize = (float)deviceWidthPixels / (float)lineWidthPixels * (float)origFontSize;
|
||||
|
||||
/*
|
||||
* Validate that the line width in scaled font does not exceed the deviceWidthPixelx
|
||||
* Scale the original font size;
|
||||
*/
|
||||
boolean isValidated = false;
|
||||
while (!isValidated) {
|
||||
printerFontData.setHeight((int) (fontSize));
|
||||
gc.setFont(new Font(printer, printerFontData));
|
||||
lineWidthPixels = gc.stringExtent(aBuffer.toString()).x;
|
||||
if (lineWidthPixels < deviceWidthPixels) {
|
||||
isValidated = true;
|
||||
} else {
|
||||
fontSize--;
|
||||
}
|
||||
}
|
||||
printerFont.dispose();
|
||||
/*
|
||||
* Set the printerFont Data font to the scaled font
|
||||
*/
|
||||
printerFontData.setHeight((int)(fontSize));
|
||||
gc.setFont(new Font(printer, printerFontData));
|
||||
}
|
||||
float fontSize = (float) deviceWidthPixels / (float) lineWidthPixels
|
||||
* (float) origFontSize;
|
||||
|
||||
private void printIt() {
|
||||
/*
|
||||
* Validate that the line width in scaled font does not exceed the
|
||||
* deviceWidthPixelx
|
||||
*/
|
||||
boolean isValidated = false;
|
||||
|
||||
if (tmpFont != null && tmpFont.isDisposed() == false) {
|
||||
tmpFont.dispose();
|
||||
}
|
||||
|
||||
while (!isValidated) {
|
||||
printerFontData.setHeight((int) (fontSize));
|
||||
tmpFont = new Font(printer, printerFontData);
|
||||
gc.setFont(tmpFont);
|
||||
lineWidthPixels = gc.stringExtent(aBuffer.toString()).x;
|
||||
if (lineWidthPixels < deviceWidthPixels) {
|
||||
isValidated = true;
|
||||
} else {
|
||||
fontSize--;
|
||||
tmpFont.dispose();
|
||||
}
|
||||
}
|
||||
printerFont.dispose();
|
||||
|
||||
gc.setFont(tmpFont);
|
||||
}
|
||||
|
||||
private void printIt() {
|
||||
if (printer.startJob("Text")) { // the string is the job name - shows up
|
||||
// in the printer's job list
|
||||
Rectangle clientArea = printer.getClientArea();
|
||||
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
|
||||
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
|
||||
Point dpi = printer.getDPI();
|
||||
|
||||
// one inch from left side of paper
|
||||
|
@ -207,7 +224,7 @@ public class PrintDisplay {
|
|||
* foreground color.
|
||||
*/
|
||||
gc = new GC(printer);
|
||||
setFont();
|
||||
setFont();
|
||||
Color printerForegroundColor = new Color(printer, new RGB(0, 0, 0));
|
||||
Color printerBackgroundColor = new Color(printer, new RGB(255, 255,
|
||||
255));
|
||||
|
@ -221,7 +238,7 @@ public class PrintDisplay {
|
|||
printer.endJob();
|
||||
|
||||
// Cleanup graphics resources used in printing
|
||||
|
||||
|
||||
printerForegroundColor.dispose();
|
||||
printerBackgroundColor.dispose();
|
||||
gc.dispose();
|
||||
|
|
Loading…
Add table
Reference in a new issue