Change the textual representation of fractional units to use Unicode superscripts and subscripts. Unfortunately, the default fonts included with iText do not have these characters mapped so we needed to switch to a new true type font DejaVu. Added the DejaVuSerif font definition to the resources and modified the pdf writing process to use this font. Because this font is slightly wider, the column headings in the DesignReport had to be made smaller. Finally, had to modify the ExpressionParser to convert the unicode chars to real digits in order to have the expression parser be able to work with this representation.

This commit is contained in:
Kevin Ruland 2012-05-22 18:36:04 +00:00
parent f42e6fa85a
commit 32ad0c1df6
9 changed files with 281 additions and 56 deletions

Binary file not shown.

View File

@ -0,0 +1,99 @@
Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.
Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below)
Bitstream Vera Fonts Copyright
------------------------------
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is
a trademark of Bitstream, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of the fonts accompanying this license ("Fonts") and associated
documentation files (the "Font Software"), to reproduce and distribute the
Font Software, including without limitation the rights to use, copy, merge,
publish, distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to the
following conditions:
The above copyright and trademark notices and this permission notice shall
be included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular
the designs of glyphs or characters in the Fonts may be modified and
additional glyphs or characters may be added to the Fonts, only if the fonts
are renamed to names not containing either the words "Bitstream" or the word
"Vera".
This License becomes null and void to the extent applicable to Fonts or Font
Software that has been modified and is distributed under the "Bitstream
Vera" names.
The Font Software may be sold as part of a larger software package but no
copy of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
FONT SOFTWARE.
Except as contained in this notice, the names of Gnome, the Gnome
Foundation, and Bitstream Inc., shall not be used in advertising or
otherwise to promote the sale, use or other dealings in this Font Software
without prior written authorization from the Gnome Foundation or Bitstream
Inc., respectively. For further information, contact: fonts at gnome dot
org.
Arev Fonts Copyright
------------------------------
Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of the fonts accompanying this license ("Fonts") and
associated documentation files (the "Font Software"), to reproduce
and distribute the modifications to the Bitstream Vera Font Software,
including without limitation the rights to use, copy, merge, publish,
distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to
the following conditions:
The above copyright and trademark notices and this permission notice
shall be included in all copies of one or more of the Font Software
typefaces.
The Font Software may be modified, altered, or added to, and in
particular the designs of glyphs or characters in the Fonts may be
modified and additional glyphs or characters may be added to the
Fonts, only if the fonts are renamed to names not containing either
the words "Tavmjong Bah" or the word "Arev".
This License becomes null and void to the extent applicable to Fonts
or Font Software that has been modified and is distributed under the
"Tavmjong Bah Arev" names.
The Font Software may be sold as part of a larger software package but
no copy of one or more of the Font Software typefaces may be sold by
itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Except as contained in this notice, the name of Tavmjong Bah shall not
be used in advertising or otherwise to promote the sale, use or other
dealings in this Font Software without prior written authorization
from Tavmjong Bah. For further information, contact: tavmjong @ free
. fr.
$Id: LICENSE 2133 2007-11-28 02:46:28Z lechimp $

View File

@ -0,0 +1,59 @@
DejaVu fonts 2.33 (c)2004-2011 DejaVu fonts team
------------------------------------------------
The DejaVu fonts are a font family based on the Bitstream Vera Fonts
(http://gnome.org/fonts/). Its purpose is to provide a wider range of
characters (see status.txt for more information) while maintaining the
original look and feel.
DejaVu fonts are based on Bitstream Vera fonts version 1.10.
Available fonts (Sans = sans serif, Mono = monospaced):
DejaVu Sans Mono
DejaVu Sans Mono Bold
DejaVu Sans Mono Bold Oblique
DejaVu Sans Mono Oblique
DejaVu Sans
DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Oblique
DejaVu Sans ExtraLight (experimental)
DejaVu Serif
DejaVu Serif Bold
DejaVu Serif Bold Italic (experimental)
DejaVu Serif Italic (experimental)
DejaVu Sans Condensed (experimental)
DejaVu Sans Condensed Bold (experimental)
DejaVu Sans Condensed Bold Oblique (experimental)
DejaVu Sans Condensed Oblique (experimental)
DejaVu Serif Condensed (experimental)
DejaVu Serif Condensed Bold (experimental)
DejaVu Serif Condensed Bold Italic (experimental)
DejaVu Serif Condensed Italic (experimental)
All fonts are also available as derivative called DejaVu LGC with support
only for Latin, Greek and Cyrillic scripts.
For license information see LICENSE. What's new is described in NEWS. Known
bugs are in BUGS. All authors are mentioned in AUTHORS.
Fonts are published in source form as SFD files (Spline Font Database from
FontForge - http://fontforge.sf.net/) and in compiled form as TTF files
(TrueType fonts).
For more information go to http://dejavu.sourceforge.net/.
Characters from Arev fonts, Copyright (c) 2006 by Tavmjong Bah:
---------------------------
U+01BA, U+01BF, U+01F7, U+021C-U+021D, U+0220, U+0222-U+0223,
U+02B9, U+02BA, U+02BD, U+02C2-U+02C5, U+02d4-U+02D5,
U+02D7, U+02EC-U+02EE, U+0346-U+034E, U+0360, U+0362,
U+03E2-03EF, U+0460-0463, U+0466-U+0486, U+0488-U+0489, U+04A8-U+04A9,
U+0500-U+050F, U+2055-205E, U+20B0, U+20B2-U+20B3, U+2102, U+210D, U+210F,
U+2111, U+2113, U+2115, U+2118-U+211A, U+211C-U+211D, U+2124, U+2135,
U+213C-U+2140, U+2295-U+2298, U+2308-U+230B, U+26A2-U+26B1, U+2701-U+2704,
U+2706-U+2709, U+270C-U+274B, U+2758-U+275A, U+2761-U+2775, U+2780-U+2794,
U+2798-U+27AF, U+27B1-U+27BE, U+FB05-U+FB06
$Id: README 2471 2011-02-27 14:25:15Z ben_laenen $

View File

@ -172,14 +172,7 @@ public class DesignReport {
double scale = paintRocketDiagram(pageImageableWidth, pageImageableHeight, canvas, figure, cp, cg);
canvas.beginText();
try {
canvas.setFontAndSize(BaseFont.createFont(PrintUtilities.NORMAL.getFamilyname(), BaseFont.CP1252,
BaseFont.EMBEDDED), PrintUtilities.NORMAL_FONT_SIZE);
} catch (DocumentException e) {
log.error("Could not set font.", e);
} catch (IOException e) {
log.error("Could not create font.", e);
}
canvas.setFontAndSize(ITextHelper.getBaseFont(), PrintUtilities.NORMAL_FONT_SIZE);
int figHeightPts = (int) (PrintUnit.METERS.toPoints(figure.getFigureHeight()) * 0.4 * (scale / PrintUnit.METERS
.toPoints(1)));
final int diagramHeight = pageImageableHeight * 2 - 70 - (figHeightPts);
@ -312,17 +305,17 @@ public class DesignReport {
motorTable.setWidthPercentage(68);
motorTable.setHorizontalAlignment(Element.ALIGN_LEFT);
final PdfPCell motorCell = ITextHelper.createCell(MOTOR, PdfPCell.BOTTOM);
final PdfPCell motorCell = ITextHelper.createCell(MOTOR, PdfPCell.BOTTOM, PrintUtilities.SMALL);
final int mPad = 10;
motorCell.setPaddingLeft(mPad);
motorTable.addCell(motorCell);
motorTable.addCell(ITextHelper.createCell(AVG_THRUST, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(BURN_TIME, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(MAX_THRUST, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(TOTAL_IMPULSE, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(THRUST_TO_WT, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(PROPELLANT_WT, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(SIZE, PdfPCell.BOTTOM));
motorTable.addCell(ITextHelper.createCell(AVG_THRUST, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(BURN_TIME, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(MAX_THRUST, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(TOTAL_IMPULSE, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(THRUST_TO_WT, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(PROPELLANT_WT, PdfPCell.BOTTOM, PrintUtilities.SMALL));
motorTable.addCell(ITextHelper.createCell(SIZE, PdfPCell.BOTTOM, PrintUtilities.SMALL));
DecimalFormat ttwFormat = new DecimalFormat("0.00");

View File

@ -3,6 +3,9 @@
*/
package net.sf.openrocket.gui.print;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
@ -10,19 +13,24 @@ import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* A bunch of helper methods for creating iText components.
*/
public final class ITextHelper {
public static BaseFont getBaseFont(){
try {
return BaseFont.createFont("/dejavu-font/DejaVuSerif.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (Exception ex ) {
throw new RuntimeException(ex);
}
}
/**
* Create a cell for an iText table.
*

View File

@ -34,12 +34,12 @@ public class PrintUtilities implements Printable {
public static final int NORMAL_FONT_SIZE = Font.DEFAULTSIZE - 3;
public static final int SMALL_FONT_SIZE = NORMAL_FONT_SIZE - 3;
public static final Font BOLD = new Font(Font.FontFamily.HELVETICA, NORMAL_FONT_SIZE, Font.BOLD);
public static final Font BIG_BOLD = new Font(Font.FontFamily.HELVETICA, NORMAL_FONT_SIZE + 3, Font.BOLD);
public static final Font BOLD_UNDERLINED = new Font(Font.FontFamily.HELVETICA, NORMAL_FONT_SIZE,
public static final Font BOLD = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE, Font.BOLD);
public static final Font BIG_BOLD = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE + 3, Font.BOLD);
public static final Font BOLD_UNDERLINED = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE,
Font.BOLD | Font.UNDERLINE);
public static final Font NORMAL = new Font(Font.FontFamily.HELVETICA, NORMAL_FONT_SIZE);
public static final Font SMALL = new Font(Font.FontFamily.HELVETICA, SMALL_FONT_SIZE);
public static final Font NORMAL = new Font(ITextHelper.getBaseFont(), NORMAL_FONT_SIZE);
public static final Font SMALL = new Font(ITextHelper.getBaseFont(), SMALL_FONT_SIZE);
private Component componentToBePrinted;

View File

@ -6,6 +6,34 @@ import java.util.ArrayList;
public class FractionalUnit extends Unit {
private final static String fraction = "\u2044";
private final static String[] numerator = {
"\u2070", // 0
"\u00B9", // 1
"\u00B2", // 2
"\u00B3", // 3
"\u2074", // 4
"\u2075", // 5
"\u2076", // 6
"\u2077", // 7
"\u2078", // 8
"\u2079" // 9
};
private final static String[] denominator = {
"\u2080", // 0
"\u2081", // 1
"\u2082", // 2
"\u2083", // 3
"\u2084", // 4
"\u2085", // 5
"\u2086", // 6
"\u2087", // 7
"\u2088", // 8
"\u2089" // 9
};
// This is the base of the fractions. ie, 16d for 1/16ths.
private final int fractionBase;
// This is 1d/fractionBase;
@ -168,12 +196,39 @@ public class FractionalUnit extends Unit {
if ( frac == 0.0 ) {
return intFormat.format(posValue);
} else if (intPart == 0.0 ){
return intFormat.format(sign*frac) + "/" + intFormat.format(fracBase);
return (sign <0 ? "-" : "" ) + numeratorString(Double.valueOf(frac).intValue())
+ fraction + denominatorString(Double.valueOf(fracBase).intValue());
} else {
return intFormat.format(sign*intPart) + " + " + intFormat.format(frac) + "/" + intFormat.format(fracBase);
return intFormat.format(sign*intPart) + " " + numeratorString(Double.valueOf(frac).intValue())
+ fraction + denominatorString(Double.valueOf(fracBase).intValue());
}
}
private String numeratorString( int value ) {
String rep = "";
if ( value == 0 ) {
return "0";
}
while ( value > 0 ) {
rep = numerator[ value % 10 ] + rep;
value = value /10;
}
return rep;
}
private String denominatorString( int value ) {
String rep = "";
if ( value == 0 ) {
return "0";
}
while ( value > 0 ) {
rep = denominator[ value % 10 ] + rep;
value = value /10;
}
return rep;
}
@Override
public String toStringUnit(double value) {

View File

@ -25,6 +25,17 @@ public class ExpressionParser {
}
private String modify(String exp) throws InvalidExpressionException {
char[] chars = exp.toCharArray();
for( int i = 0; i< chars.length; i++ ) {
int value = Character.getNumericValue(chars[i]);
if ( value >= 0 && value < 10 ) {
chars[i] = Character.toChars(48 + value)[0];
}
if ( chars[i] == '\u2044') {
chars[i] = '/';
}
}
exp = String.copyValueOf(chars);
exp = exp.replaceAll("(\\d+)\\s+(\\d+)\\s*/\\s*(\\d+)", "($1+$2/$3)");
exp = exp.replace(',', '.');
// Disallow spaces between numbers - default is to remove spaces!

View File

@ -106,17 +106,17 @@ public class FractionalUnitTest {
// default epsilon is 0.025
assertEquals("-1.2", testUnit.toString(-1.2));
assertEquals("-1 1/4", testUnit.toString(-1.225));
assertEquals("-1 1/4", testUnit.toString(-1.227));
assertEquals("-1 1/4", testUnit.toString(-1.25));
assertEquals("-1 1/4", testUnit.toString(-1.25));
assertEquals("-1 1/4", testUnit.toString(-1.275));
assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.225));
assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.227));
assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.25));
assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.25));
assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.275));
assertEquals("-1.3", testUnit.toString(-1.3));
assertEquals("-0.2", testUnit.toString(-.2));
assertEquals("-1/4", testUnit.toString(-.225));
assertEquals("-1/4", testUnit.toString(-.25));
assertEquals("-1/4", testUnit.toString(-.274));
assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.225));
assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.25));
assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.274));
//assertEquals("-1/4", testUnit.toString(-.275)); // this has roundoff error which pushes it over epsilon
assertEquals("-0.3", testUnit.toString(-.3));
@ -127,15 +127,15 @@ public class FractionalUnitTest {
assertEquals("0.1", testUnit.toString(.1));
assertEquals("0.2", testUnit.toString(.2));
assertEquals("1/4", testUnit.toString(.225));
assertEquals("1/4", testUnit.toString(.25));
assertEquals("1/4", testUnit.toString(.274));
assertEquals("\u00B9\u2044\u2084", testUnit.toString(.225));
assertEquals("\u00B9\u2044\u2084", testUnit.toString(.25));
assertEquals("\u00B9\u2044\u2084", testUnit.toString(.274));
assertEquals("0.3", testUnit.toString(.3));
assertEquals("1.2", testUnit.toString(1.2));
assertEquals("1 1/4", testUnit.toString(1.225));
assertEquals("1 1/4", testUnit.toString(1.25));
assertEquals("1 1/4", testUnit.toString(1.275));
assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.225));
assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.25));
assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.275));
assertEquals("1.3", testUnit.toString(1.3));
}
@ -144,17 +144,17 @@ public class FractionalUnitTest {
public void testToStringWithPrecision() {
// epsilon is .02
assertEquals("-1 3/16", testUnitApprox.toString(-1.2));
assertEquals("-1 \u00B3\u2044\u2081\u2086", testUnitApprox.toString(-1.2));
assertEquals("-1.225", testUnitApprox.toString(-1.225));
assertEquals("-1 1/4", testUnitApprox.toString(-1.25));
assertEquals("-1 \u00B9\u2044\u2084", testUnitApprox.toString(-1.25));
assertEquals("-1.275", testUnitApprox.toString(-1.275));
assertEquals("-1 5/16", testUnitApprox.toString(-1.3));
assertEquals("-1 \u2075\u2044\u2081\u2086", testUnitApprox.toString(-1.3));
assertEquals("-3/16", testUnitApprox.toString(-.2));
assertEquals("-\u00B3\u2044\u2081\u2086", testUnitApprox.toString(-.2));
assertEquals("-0.225", testUnitApprox.toString(-.225));
assertEquals("-1/4", testUnitApprox.toString(-.25));
assertEquals("-\u00B9\u2044\u2084", testUnitApprox.toString(-.25));
assertEquals("-0.275", testUnitApprox.toString(-.275));
assertEquals("-5/16", testUnitApprox.toString(-.3));
assertEquals("-\u2075\u2044\u2081\u2086", testUnitApprox.toString(-.3));
assertEquals("-0.1", testUnitApprox.toString(-.1));
assertEquals("-0.024", testUnitApprox.toString(-0.024));
@ -162,17 +162,17 @@ public class FractionalUnitTest {
assertEquals("0.024", testUnitApprox.toString(.024));
assertEquals("0.1", testUnitApprox.toString(.1));
assertEquals("3/16", testUnitApprox.toString(.2));
assertEquals("\u00B3\u2044\u2081\u2086", testUnitApprox.toString(.2));
assertEquals("0.225", testUnitApprox.toString(.225));
assertEquals("1/4", testUnitApprox.toString(.25));
assertEquals("\u00B9\u2044\u2084", testUnitApprox.toString(.25));
assertEquals("0.275", testUnitApprox.toString(.275));
assertEquals("5/16", testUnitApprox.toString(.3));
assertEquals("\u2075\u2044\u2081\u2086", testUnitApprox.toString(.3));
assertEquals("1 3/16", testUnitApprox.toString(1.2));
assertEquals("1 \u00B3\u2044\u2081\u2086", testUnitApprox.toString(1.2));
assertEquals("1.225", testUnitApprox.toString(1.225));
assertEquals("1 1/4", testUnitApprox.toString(1.25));
assertEquals("1 \u00B9\u2044\u2084", testUnitApprox.toString(1.25));
assertEquals("1.275", testUnitApprox.toString(1.275));
assertEquals("1 5/16", testUnitApprox.toString(1.3));
assertEquals("1 \u2075\u2044\u2081\u2086", testUnitApprox.toString(1.3));
}
@ -180,18 +180,18 @@ public class FractionalUnitTest {
public void testInchToString() {
// Just some random test points.
assertEquals( "1/64", inchUnit.toString( 1d/64d*0.0254));
assertEquals( "\u00B9\u2044\u2086\u2084", inchUnit.toString( 1d/64d*0.0254));
assertEquals( "-5/64", inchUnit.toString( -5d/64d*0.0254));
assertEquals( "-\u2075\u2044\u2086\u2084", inchUnit.toString( -5d/64d*0.0254));
assertEquals( "4 1/2", inchUnit.toString( 9d/2d*0.0254));
assertEquals( "4 \u00B9\u2044\u2082", inchUnit.toString( 9d/2d*0.0254));
assertEquals( "0.002", inchUnit.toString( 0.002*0.0254));
// default body tube length:
double length = 8d * 0.025;
assertEquals( "7 7/8", inchUnit.toString( length) );
assertEquals( "7 \u2077\u2044\u2088", inchUnit.toString( length) );
// had problems with roundoff in decrement.