?? wmf2svg.java
字號:
package SWF2SVG;
// Copyright 1997, 1998,1999 Carmen Delessio (carmen@blackdirt.com)
// Black Dirt Software http://www.blackdirt.com/graphics
// Free for non-commercial use
// revisions:
// May 12,1999 corrected bad URL http://www.w3.org
// Corrected using <desc> as placeholders by embedding in their own <g>
import java.io.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.net.*;
import java.applet.Applet;
import java.lang.Math;
import java.lang.String;
import java.awt.image.*;
class wmf2svg{
boolean brushset=true;
boolean penset=true;
boolean fontset=true;
boolean textColorset=true;
private byte[] bytebuffer;
private int i;
private int j;
private int bytes_read;
private int rdSize;
private int windowLong;
private short windowInt;
public short inch;
private boolean oldset;
private static MetaRecord mRecord;
private java.util.Vector MetaRecordVector;
private java.util.Enumeration MetaRecordInfo;
private DataInputStream d;
private int recordIndex;
private int handleIndex;
public WMFHandleTable ht;
private int[] ncount;
byte[] byteData; // Unpacked data: one byte per pixel
byte[] rawData; // the raw unpacked data
int[] intData; // Unpacked data: one int per pixel
public WMFToolkit cvtTool = new WMFToolkit();
private boolean drawFilled;
private short oldx;
private short oldy;
private String svgGraphic = new String("");
private String svgInit = new String("");
private short numRectangles;
private short numPolygons;
private short numOvals;
private short numLines;
public short WMFWidth;
public short WMFHeight;
private short htmlWidth = 800;
private short htmlHeight = 600;
public short logExtX;
public short logExtY;
public short logOrgX = 0;
public short logOrgY = 0;
public short devExtX;
public short devExtY;
public int AppletWidth;
public int AppletHeight;
public float d_x;
public float d_y;
public Graphics wmfGraphics;
public Image wmfImageBuffer;
public int fontStyle;
public int fontWeight;
public boolean fontItalic = false;
public int penRed =0;
public int penGreen =0;
public int penBlue =0;
public int textRed =0;
public int textGreen =0;
public int textBlue =0;
String textColor="ffff" ;
String penColor="ffff" ;
String brushColor="ffff";
List hs = new ArrayList();
public boolean styleSet = false;
Stack dcstack=new Stack();
public wmf2svg(InputStream is, PrintStream os) throws IOException
{
oldset=false;
d = new DataInputStream(is);
parseit();
WMFPlay();
if (styleSet){
svgGraphic += "</g>";
}
svgGraphic += "</svg>\n";
// System.out.println(svgGraphic);
os.println(svgGraphic);
}
// gets all wmf records into a hash table
public synchronized void parseit()throws IOException {
byte[] f_long =new byte[4];
byte[] f_int = new byte[2];
byte[] parmBuffer;
short x;
short y;
short x2;
short y2;
short count = 0;
MetaRecord locMetaRecord;
//begin metafile header
windowLong = readLong(d); //key 4 bytes
if (windowLong == -1698247209){
windowInt = readInt(d); // unused
x2 = readInt(d);
y2 = readInt(d);
x = readInt(d);
y = readInt(d);
WMFWidth = (short) Math.abs(x2-x);
WMFHeight = (short) Math.abs(y2-y);
windowInt = readInt(d); // inch
inch = windowInt;
x = (short)(inch/cvtTool.getScreenResolution()); //pixels per inch
if (x < 1){
x =1;
}
WMFWidth = (short)(WMFWidth /x);
WMFHeight = (short)(WMFHeight/x);
// based on bounded bo x- nnox consideration of map modes
svgGraphic = "<?xml version = \"1.0\" standalone = \"yes\"?>\n";
svgGraphic = svgGraphic + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG April 1999//EN\"\n \"http://www.w3.org/Graphics/SVG/svg-19990412.dtd\">\n";
svgGraphic = svgGraphic + "<svg width = \"" + WMFWidth +"px\" height=\"" + WMFHeight+"px\">\n";
htmlWidth = WMFWidth;
htmlHeight = WMFHeight;
devExtX = htmlWidth;
devExtY = htmlHeight;
x = cvtTool.twip2pixel(x); // add inch stuff here
y = cvtTool.twip2pixel(y);
windowLong = readLong(d); // reserved
windowInt = readInt(d);// checksum
//metaheader
for ( i = 0; i < 3; i++){
windowInt = readInt(d);
}
windowLong = readLong(d);
windowInt = readInt(d);
windowLong = readLong(d);
windowInt = readInt(d);
}
else {
// not placeable- no metafile header
// already read long to check for key
windowInt = readInt(d);
windowLong = readLong(d);
windowInt = readInt(d);
windowLong = readLong(d);
windowInt = readInt(d);
}
MetaRecordVector = new java.util.Vector();
while(true){
count++;
try{
d.readFully(f_long);
}
catch(EOFException e){ System.out.println("**** eof");break;}
if (bytes_read == -1)break;
windowLong = flipLong(f_long);
windowInt = readInt(d);
if (windowInt == 0){
break;}
if (windowLong >= 3){
windowLong = (windowLong*2) -6;
}
parmBuffer = null;
parmBuffer = new byte[windowLong];
d.readFully(parmBuffer);
MetaRecordVector.addElement( new MetaRecord( windowLong, windowInt, parmBuffer));
}
}
// interprets each record
public void WMFPlay( ) {
// AppletWidth = w;
// AppletHeight = h;
// devExtX = (short) w;
// devExtY = (short) h;
ht = new WMFHandleTable();
drawFilled = false;
MetaRecordInfo = MetaRecordVector.elements();
handleIndex = 0;
recordIndex = 0;
while(MetaRecordInfo.hasMoreElements()){
mRecord = (MetaRecord) MetaRecordInfo.nextElement();
WMFlistRecord(mRecord, false);
recordIndex++;
}
}
// works on a single metarecord
// still need something to write out all info assigned in this
// public synchronized void WMFlistRecord(MetaRecord mRecord , boolean fromSelect, boolean play, Graphics g){
public synchronized void WMFlistRecord(MetaRecord mRecord , boolean fromSelect){
short x;
short y;
short x2;
short y2;
short numChars;
short wOptions;
int selColor;
short w;
short h;
short lbhatch;
short lbstyle;
short numPoints;
DataInputStream parmStream;
ByteArrayInputStream parmIn;
String polyName;
String shapeName;
String tempBuffer;
String colorBuffer;
String currentFont = "Dialog";
byte[] textBuffer;
float fontHeight = 10;
short fontHeightShort = 10;
Polygon drawPoly = new Polygon();
Polygon poly = new Polygon();
// Image BMPimage = null;
String this_one;
// if (g == null) System.out.println(" graphics is null");
parmIn = new ByteArrayInputStream(mRecord.getParm());
parmStream = new DataInputStream(parmIn);
switch(mRecord.getFunction()){
case 0x2fa: // create pen indirect
if (!fromSelect){
hs.add(recordIndex, mRecord);
} else {
lbstyle = readInt(parmStream); //if 5 outline is off
x = readInt(parmStream);
y = readInt(parmStream);
selColor = readLong(parmStream);
//cvtTool.setColors(selColor);
colorBuffer = cvtTool.getRGBColor( selColor);
penColor = colorBuffer;
/* if (styleSet){
svgGraphic = svgGraphic +"</g> \n";
} else{
styleSet = true;
}*/
svgGraphic = svgGraphic + "stroke: #" + penColor + "stroke-width:#"+2+"\" > \n";
}
break;
case 0x2fc: //createBrushIndirect
if (!fromSelect){ // if not seleceting it, just add it to table
hs.add(recordIndex, mRecord);
}
else{ // selected - use it
lbstyle = readInt(parmStream);
selColor = readLong(parmStream);
lbhatch = readInt(parmStream);
cvtTool.setColors(selColor);
colorBuffer = cvtTool.getRGBColor( selColor);
brushColor=colorBuffer;
if (styleSet){
svgGraphic = svgGraphic +"> \n";
} else{
styleSet = true;
}
if (lbstyle > 0 ){
drawFilled = false;
svgGraphic = svgGraphic + "stroke: #" + brushColor + "\" > \n";
}
else{
drawFilled = true; //filled
svgGraphic = svgGraphic + "fill: #" + brushColor + "\" > \n";
// System.out.println("brushColor"+brushColor);
}
}
break;
case 0x6ff: // create region
System.out.println("建區域");
if (!fromSelect){
hs.add(recordIndex, mRecord);
}
break;
case 0x2fb: //createFontIndirect
if (!fromSelect){ // if not selecting it, just add it to table
hs.add(recordIndex, mRecord);
} else{
fontHeightShort = readInt(parmStream);
fontHeight = fontHeightShort;
fontHeightShort = (short)fontHeight;
if (fontHeightShort < 0) {
fontHeightShort *= -1;
fontHeightShort = mapY(fontHeightShort);
}
else{
fontHeight = (fontHeight/inch);
fontHeight = (fontHeight*72);
fontHeightShort = (short)fontHeight;
if (fontHeightShort < 5){
fontHeightShort = 9;
}
}
x2 = readInt(parmStream); // width
y2 = readInt(parmStream); //esc
y2 = readInt(parmStream); // orientation
y2 = readInt(parmStream); //weight
fontWeight = y2;
textBuffer = new byte[1];
try{
parmStream.read(textBuffer);
}
catch(IOException e){ System.err.println(e);}
x = (short)textBuffer[0]; // italic
fontItalic = false;
if (x < 0){
fontItalic = true;
}
textBuffer = new byte[7];
try{
parmStream.read(textBuffer);
}
catch(IOException e){ System.err.println(e);}
tempBuffer = new String(textBuffer,0);
textBuffer = new byte[32]; // name of font
try{
parmStream.read(textBuffer);
}
catch(IOException e){ System.err.println(e);}
tempBuffer = new String(textBuffer,0);
currentFont = "Dialog";
if (tempBuffer.startsWith("Courier")){
currentFont = "Courier";
}
else if (tempBuffer.startsWith("MS Sans Serif")) {
currentFont = "Dialog";
}
else if (tempBuffer.startsWith("Arial")) {
currentFont = "Helvetica";
}
else if (tempBuffer.startsWith("Arial Narrow")) {
currentFont = "Helvetica";
}
else if (tempBuffer.startsWith("Arial Black")) {
currentFont = "Helvetica";
fontWeight = 700;
}
else if (tempBuffer.startsWith("Times New Roman")) {
currentFont = "TimesRoman";
}
else if (tempBuffer.startsWith("Wingdings")) {
currentFont = "ZapfDingbats";
}
if (fontItalic) {
fontStyle = Font.ITALIC;
if (fontWeight >= 700){ // bold + italic
fontStyle = 3;
}
}
else{
fontStyle = Font.PLAIN; // plain
if (fontWeight >= 700){ // bold
fontStyle = Font.BOLD;
}
// g.setFont(new Font (currentFont, fontStyle, fontHeightShort));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -