Skip to content

Commit

Permalink
Implement #71 in PDF export.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarwinNE committed May 18, 2020
1 parent 80057f9 commit d12e8d6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 51 deletions.
12 changes: 5 additions & 7 deletions src/net/sourceforge/fidocadj/export/ExportEPS.java
Expand Up @@ -46,6 +46,10 @@ public class ExportEPS implements ExportInterface, TextInterface
private float currentPhase=-1;
private float currentFontSize=0;
private DecoratedText dt;
private String fontname; // Some info about the font is stored
private String bold="";
private float textx; // This is used in sub-sup scripts position
private float texty;

// Number of digits to be used when representing coordinates
static final int PREC = 3;
Expand All @@ -62,7 +66,6 @@ public class ExportEPS implements ExportInterface, TextInterface
public void setDashUnit(double u)
{
sDash = new String[Globals.dashNumber];
dt=new DecoratedText(this);
// If the line width has been changed, we need to update the
// stroke table

Expand Down Expand Up @@ -103,6 +106,7 @@ public void setDashPhase(float p)
public ExportEPS (File f) throws IOException
{
fstream = new FileWriter(f);
dt=new DecoratedText(this);
}

/** Called at the beginning of the export phase. Ideally, in this routine
Expand Down Expand Up @@ -195,12 +199,6 @@ public void exportEnd()
out.close();
}

private String fontname;
private String bold="";
private float textx;
private float texty;


/** Called when exporting an Advanced Text primitive.
@param x the x position of the beginning of the string to be written.
@param y the y position of the beginning of the string to be written.
Expand Down
148 changes: 104 additions & 44 deletions src/net/sourceforge/fidocadj/export/ExportPDF.java
Expand Up @@ -38,7 +38,7 @@
@author Davide Bucci
*/
public class ExportPDF implements ExportInterface
public class ExportPDF implements ExportInterface, TextInterface
{
private final File temp;
private final OutputStreamWriter fstream;
Expand All @@ -49,6 +49,11 @@ public class ExportPDF implements ExportInterface
private String userfont;
private float dashPhase;
private float currentPhase=-1;
private float currentFontSize=0;
private DecoratedText dt;
private String currentFont; // Some info about the font is stored
private float textx; // This is used in sub-sup scripts position
private float texty;


private final GraphicsInterface gi;
Expand Down Expand Up @@ -147,8 +152,8 @@ public ExportPDF (File f, GraphicsInterface gg) throws IOException

fstreamt = new OutputStreamWriter(new FileOutputStream(temp),
encoding);

obj_PDF = new String[numOfObjects];
dt=new DecoratedText(this);
}

/** Called at the beginning of the export phase. Ideally, in this routine
Expand Down Expand Up @@ -753,6 +758,8 @@ private void writeCrossReferenceTable() throws java.io.IOException

}



/** Called when exporting an Advanced Text primitive.
@param x the x position of the beginning of the string to be written.
Expand All @@ -769,7 +776,6 @@ private void writeCrossReferenceTable() throws java.io.IOException
@throws IOException if a disaster happens, i.e. a file can not be
accessed.
*/

public void exportAdvText (int x, int y, int sizex, int sizey,
String fontname, boolean isBold, boolean isMirrored, boolean isItalic,
int orientation, int layer, String text_t)
Expand All @@ -791,41 +797,43 @@ public void exportAdvText (int x, int y, int sizex, int sizey,

if("Courier".equals(fontname) || "Courier New".equals(fontname)) {
if(isBold)
outt.write("/F2"+" "+ys+" Tf\n");
currentFont="/F2";
else
outt.write("/F1"+" "+ys+" Tf\n");
currentFont="/F1";
} else if("Times".equals(fontname) ||
"Times New Roman".equals(fontname) ||
"Times Roman".equals(fontname))
{
if(isBold)
outt.write("/F4"+" "+ys+" Tf\n");
currentFont="/F4";
else
outt.write("/F3"+" "+ys+" Tf\n");
currentFont="/F3";

} else if("Helvetica".equals(fontname) ||
"Arial".equals(fontname))
{
if(isBold)
outt.write("/F6"+" "+ys+" Tf\n");
currentFont="/F6";
else
outt.write("/F5"+" "+ys+" Tf\n");
currentFont="/F5";

} else if("Symbol".equals(fontname)) {
if(isBold)
outt.write("/F8"+" "+ys+" Tf\n");
currentFont="/F8";
else
outt.write("/F7"+" "+ys+" Tf\n");
currentFont="/F7";
} else {
fontWarning = true;
userfont=fontname;
outt.write("/F9"+" "+ys+" Tf\n");
currentFont="/F9";
}

outt.write(currentFont+" "+ys+" Tf\n");
currentFontSize=(float)ys;
outt.write("q\n");
outt.write(" 1 0 0 1 "+ Globals.roundTo(x)+" "+ Globals.roundTo(y)+
" cm\n");

textx=x;
texty=y;
if(orientation !=0) {
double alpha=(isMirrored?orientation:-orientation)/180.0*Math.PI;
outt.write(" "+Globals.roundTo(Math.cos(alpha))+" "
Expand All @@ -845,36 +853,9 @@ public void exportAdvText (int x, int y, int sizex, int sizey,
} else {
ratio=(double)sizey/(double)sizex*22.0/40.0;
}
outt.write(" 1 0 0 "+Globals.roundTo(ratio)+ " 0 "+(-ys*ratio*0.8)+
" cm\n");

outt.write(" <");
int ch;
int codechar;
for(int i=0; i<text.length();++i) {
ch=(int)text.charAt(i);

// Proceed to encode UTF-8 characters as much as possible.
if(ch>127) {
if(uncodeCharsNeeded.containsKey(ch)) {
ch=uncodeCharsNeeded.get(ch);
} else {
++unicodeCharIndex;
if(unicodeCharIndex<256) {
uncodeCharsNeeded.put(unicodeCharIndex,ch);
ch=unicodeCharIndex;
} else {
System.err.println("Too many Unicode chars! "+
"The present version of the PDF export filter "+
"handles up to 128 different Unicode chars in one "+
"file.");
}
}
}
outt.write(Integer.toHexString(ch));
outt.write(" ");
}
outt.write("> Tj\n");
outt.write(" 1 0 0 "+Globals.roundTo(ratio)+ " 0 "+
(-ys*ratio*0.8)+" cm\n");
dt.drawString(text,x,y);
outt.write("Q\nET\n");
}

Expand Down Expand Up @@ -1482,4 +1463,83 @@ public PointPr exportArrow(double x, double y, double xc, double yc,
}
return new PointPr(x0,y0);
}


// Functions required for the TextInterface.

/** Get the font size.
@return the font size.
*/
public double getFontSize()
{
return currentFontSize;
}

/** Set the font size.
@param size the font size.
*/
public void setFontSize(double size)
{
currentFontSize=(float)size;
try {
outt.write(currentFont+" "+currentFontSize+" Tf\n");
} catch(IOException E) {
System.err.println("Can not write to file in PDF export.");
}
}

/** Get the width of the given string with the current font.
@param s the string to be used.
@return the width of the string, in pixels.
*/
public int getStringWidth(String s)
{
return 0;
}

/** Draw a string on the current graphic context.
@param str the string to be drawn.
@param x the x coordinate of the starting point.
@param y the y coordinate of the starting point.
*/
public void drawString(String str,
int x,
int y)
{
try {
outt.write(" 1 0 0 1 "+ Globals.roundTo(textx-x)+
" "+ Globals.roundTo(texty-y)+
" cm\n");
texty=y;

outt.write(" <");
int ch;
int codechar;
for(int i=0; i<str.length();++i) {
ch=(int)str.charAt(i);
// Proceed to encode UTF-8 characters as much as possible.
if(ch>127) {
if(uncodeCharsNeeded.containsKey(ch)) {
ch=uncodeCharsNeeded.get(ch);
} else {
++unicodeCharIndex;
if(unicodeCharIndex<256) {
uncodeCharsNeeded.put(unicodeCharIndex,ch);
ch=unicodeCharIndex;
} else {
System.err.println("Too many Unicode chars! "+
"The present version of the PDF export filter "+
"handles up to 128 different Unicode chars in "+
"one file.");
}
}
}
outt.write(Integer.toHexString(ch));
outt.write(" ");
}
outt.write("> Tj\n");
} catch(IOException E) {
System.err.println("Can not write to file in EPS export.");
}
}
}

0 comments on commit d12e8d6

Please sign in to comment.