Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporating fixes #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions wmf2svg/src/net/arnx/wmf2svg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.lang.reflect.Proxy;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;

import com.google.typography.font.sfntly.FontFactory;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish this library don't have dependencies for other libraries. If it must have, you need to realize by using a dynamic class loading and a reflection.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it does not have any dependency

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A FontFactory class is not a starndard jdk class. So it will add a depedency.

import net.arnx.wmf2svg.gdi.Gdi;
import net.arnx.wmf2svg.gdi.svg.*;
import net.arnx.wmf2svg.gdi.wmf.*;
Expand All @@ -34,29 +34,50 @@ public class Main {
private static Logger log = Logger.getLogger(Main.class.getName());

public static void main(String[] args) {
FontFactory factory = FontFactory.getInstance();
factory.fingerprintFont();
String src = null;
String dest = null;

String fontsRegEntryPath = null;
boolean debug = false;
boolean compatible = false;
boolean replaceSymbolFont = false;

for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if (args[i].equals("-debug")) {
if (args[i].startsWith("-"))
{
if (args[i].equals("-debug"))
{
debug = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This break line style is inappropriate. A line break shouldn't be placed before '{' in a common java coding style.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I am correcting it and will update in 2nd code pull request

} else if (args[i].equals("-compatible")) {
}
else if (args[i].equals("-compatible"))
{
compatible = true;
} else if (args[i].equals("-replace-symbol-font")) {
}
else if (args[i].equals("-replace-symbol-font"))
{
replaceSymbolFont = true;
} else {
}
else
{
usage();
return;
}
} else if (i == args.length-2) {
src = args[i];
} else if (i == args.length-1) {
dest = args[i];
}
else
{
if(i == args.length-3)
{
fontsRegEntryPath = args[i];
}
else if (i == args.length-2)
{
src = args[i];
}
else if (i == args.length-1)
{
dest = args[i];
}
}
}

Expand All @@ -68,7 +89,11 @@ public static void main(String[] args) {
try {
InputStream in = new FileInputStream(src);
WmfParser parser = new WmfParser();
final SvgGdi gdi = new SvgGdi(compatible);
final SvgGdi gdi;
if(null == fontsRegEntryPath)
gdi = new SvgGdi(compatible);
else
gdi = new SvgGdi(compatible, fontsRegEntryPath);
gdi.setReplaceSymbolFont(replaceSymbolFont);
if (debug) {
ClassLoader cl = gdi.getClass().getClassLoader();
Expand Down
2 changes: 1 addition & 1 deletion wmf2svg/src/net/arnx/wmf2svg/gdi/svg/SvgFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public int[] validateDx(byte[] chars, int[] dx) {
}

public int getFontSize() {
return Math.abs((int)getGDI().getDC().toRelativeY(height * heightMultiply));
return Math.abs((int)getGDI().getDC().toRelativeY(height /* * heightMultiply*/));
}

public int hashCode() {
Expand Down
195 changes: 183 additions & 12 deletions wmf2svg/src/net/arnx/wmf2svg/gdi/svg/SvgGdi.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
package net.arnx.wmf2svg.gdi.svg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
Expand All @@ -37,6 +43,11 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.google.typography.font.sfntly.Font;
import com.google.typography.font.sfntly.FontFactory;
import com.google.typography.font.sfntly.table.core.CMap;
import com.google.typography.font.sfntly.table.core.CMapTable;
import com.google.typography.font.sfntly.table.truetype.GlyphTable;

import net.arnx.wmf2svg.gdi.Gdi;
import net.arnx.wmf2svg.gdi.GdiBrush;
Expand Down Expand Up @@ -101,6 +112,10 @@ public class SvgGdi implements Gdi {

private SvgFont defaultFont;

private int maxWidth;
private int maxHeight;
private String fontsRegEntryPath;
private HashMap<String, String> fontMap;
public SvgGdi() throws SvgGdiException {
this(false);
}
Expand Down Expand Up @@ -134,6 +149,63 @@ public SvgGdi(boolean compatible) throws SvgGdiException {
}
}

public SvgGdi(boolean compatible, String fontsRegEntry) throws SvgGdiException {
this.compatible = compatible;
this.fontsRegEntryPath = fontsRegEntry;
this.fontMap = new HashMap<String, String>();
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.fontsRegEntryPath), "UTF-16"));
String line;
while((line = reader.readLine()) != null)
{
String[] parts = line.split("=");
if(parts.length == 2)
{
String val = parts[1].substring(1, parts[1].length() -1);
if(val.endsWith(".ttf"))
{
String key = parts[0].substring(1, parts[0].length() -1);
int idxOfOpenBracket = key.indexOf("(");
key = key.substring(0, idxOfOpenBracket - 1);
//System.out.println(key + val + " " + idxOfOpenBracket);
this.fontMap.put(key, val);
}

}
} //end while
reader.close();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Exception caught while reading fonts file: " + this.fontsRegEntryPath + " " + e);
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new SvgGdiException(e);
}

DOMImplementation dom = builder.getDOMImplementation();
doc = dom.createDocument("http://www.w3.org/2000/svg", "svg", null);

InputStream in = null;
try {
in = getClass().getResourceAsStream("SvgGdi.properties");
props.load(in);
} catch (Exception e) {
throw new SvgGdiException("properties format error: SvgGDI.properties");
} finally {
try {
if (in != null) in.close();
} catch (IOException e) {
// no handle
}
}
}

public void write(OutputStream out) throws IOException {
try {
TransformerFactory factory = TransformerFactory.newInstance();
Expand Down Expand Up @@ -704,13 +776,61 @@ public void extTextOut(int x, int y, int options, int[] rect, byte[] text, int[]
elem.setAttribute("clip-path", "url(#" + name + ")");
}

String str = null;
if (dc.getFont() != null) {
str = GdiUtils.convertString(text, dc.getFont().getCharset());
} else {
str = GdiUtils.convertString(text, GdiFont.DEFAULT_CHARSET);
String str = "";
String regfilepath = fontsRegEntryPath;
if(regfilepath != null && regfilepath != "")
{
String windowsDir = System.getenv("WINDIR");
File winFontDir = new File(windowsDir, "Fonts");
String winFontsPath = winFontDir.getAbsolutePath();
//System.out.println(winFontsPath);
String fontFileName = "";
try {
fontFileName = fontMap.get(dc.getFont().getFaceName());
//System.out.println(fontFileName);
if(fontFileName != "")
{
java.nio.file.Path path = Paths.get(winFontsPath+"\\"+ fontFileName);
byte[] fontFileData = Files.readAllBytes(path);
FontFactory fontFactory = FontFactory.getInstance();
Font[] fontArray = null;
fontArray = fontFactory.loadFonts(fontFileData);
Font font = fontArray[0];
CMapTable cMapTable = font.getTable(com.google.typography.font.sfntly.Tag.cmap);
GlyphTable glyfTable = font.getTable(com.google.typography.font.sfntly.Tag.glyf);
CMap cmap = cMapTable.cmap(3, 0);
if(null != cmap)
{
for (int mt=0; mt < text.length; mt++)
{
int code = text[mt];
if(code < 0)
code = 127 + (code - (-129));
code = 0xf000 | code;
str += Character.toString((char)code);

}
}
else
{
//System.out.println("cmap does not exist");
}
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("exception caught : " + e);
}
}

if(str.isEmpty())
{
if (dc.getFont() != null) {
str = GdiUtils.convertString(text, dc.getFont().getCharset());
} else {
str = GdiUtils.convertString(text, GdiFont.DEFAULT_CHARSET);
}
}
if (dc.getFont() != null && dc.getFont().getLang() != null) {
elem.setAttribute("xml:lang", dc.getFont().getLang());
}
Expand Down Expand Up @@ -1187,6 +1307,12 @@ public void setViewportOrgEx(int x, int y, Point old) {
}

public void setWindowExtEx(int width, int height, Size old) {
if (width > maxWidth) {
maxWidth = width;
}
if (height > maxHeight) {
maxHeight = height;
}
dc.setWindowExtEx(width, height, old);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxWidth, maxHeight value need to be taken from dc because it restore or save

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could please explain in little detail, that shall be very helpful.

}

Expand Down Expand Up @@ -1265,7 +1391,52 @@ public void textOut(int x, int y, byte[] text) {
elem.setAttribute("transform", "rotate(" + (-escapement/10.0) + ", " + ax + ", " + ay + ")");
}

String str = null;
String str = "";
String regfilepath = fontsRegEntryPath;
if(regfilepath != null && regfilepath != "")
{
String windowsDir = System.getenv("WINDIR");
File winFontDir = new File(windowsDir, "Fonts");
String winFontsPath = winFontDir.getAbsolutePath();
//System.out.println(winFontsPath);
String fontFileName = "";
try {
fontFileName = fontMap.get(dc.getFont().getFaceName());
//System.out.println(fontFileName);
if(fontFileName != "")
{
java.nio.file.Path path = Paths.get(winFontsPath+"\\"+ fontFileName);
byte[] fontFileData = Files.readAllBytes(path);
FontFactory fontFactory = FontFactory.getInstance();
Font[] fontArray = null;
fontArray = fontFactory.loadFonts(fontFileData);
Font font = fontArray[0];
CMapTable cMapTable = font.getTable(com.google.typography.font.sfntly.Tag.cmap);
GlyphTable glyfTable = font.getTable(com.google.typography.font.sfntly.Tag.glyf);
CMap cmap = cMapTable.cmap(3, 0);
if(null != cmap)
{
for (int mt=0; mt < text.length; mt++)
{
int code = text[mt];
if(code < 0)
code = 127 + (code - (-129));
code = 0xf000 | code;
str += Character.toString((char)code);

}
}
else
{
//System.out.println("cmap does not exist");
}
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("exception caught : " + e);
}
}
if (dc.getFont() != null) {
str = GdiUtils.convertString(text, dc.getFont().getCharset());
} else {
Expand Down Expand Up @@ -1295,14 +1466,14 @@ public void textOut(int x, int y, byte[] text) {

public void footer() {
Element root = doc.getDocumentElement();
if (!root.hasAttribute("width") && dc.getWindowWidth() != 0) {
root.setAttribute("width", "" + Math.abs(dc.getWindowWidth()));
if (!root.hasAttribute("width") && maxWidth != 0) {
root.setAttribute("width", "" + maxWidth);
}
if (!root.hasAttribute("height") && dc.getWindowHeight() != 0) {
root.setAttribute("height", "" + Math.abs(dc.getWindowHeight()));
if (!root.hasAttribute("height") && maxHeight != 0) {
root.setAttribute("height", "" + maxHeight);
}
if (dc.getWindowWidth() != 0 && dc.getWindowHeight() != 0) {
root.setAttribute("viewBox", "0 0 " + Math.abs(dc.getWindowWidth()) + " " + Math.abs(dc.getWindowHeight()));
if (maxWidth != 0 && maxHeight != 0) {
root.setAttribute("viewBox", "0 0 " + maxWidth + " " + maxHeight);
root.setAttribute("preserveAspectRatio", "none");
}
root.setAttribute("stroke-linecap", "round");
Expand Down