看起來「Gerhard Dinhof」功能不正確,我在這方面浪費了時間。 在這裏你可以找到一個用c#編寫的修改版本,將簡單的SVG文件轉換爲相關的html地圖代碼。 用法:MessageBox.Show(Svg2map( 「C:\ test.svg」))
// Sample file contents:
// <?xml version="1.0" encoding="UTF-8" ?>
// <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
// <svg width="739pt" height="692pt" viewBox="0 0 739 692" version="1.1" xmlns="http://www.w3.org/2000/svg">
// <path fill="#fefefe" d=" M 0.00 0.00 L 190.18 0.00 C 188.15 2.70 186.03 5.53 185.30 8.90 L 0.00 0.00 Z" />
// </svg>
private string Svg2map(string svg_file_path)
{
string[] svgLines = File.ReadAllLines(svg_file_path);
string svg = string.Join("", svgLines).ToLower();
int temp;
int w = int.Parse(ExtractData(svg,0,out temp, "<svg", "width").Replace("pt", "").Replace("px", ""));
int h = int.Parse(ExtractData(svg, 0, out temp, "<svg", "height").Replace("pt", "").Replace("px", ""));
StringBuilder stringToFile = new StringBuilder();
stringToFile.AppendLine(string.Format("<img id=\"image1\" src=\"image1.jpg\" border=\"0\" width=\"{0}\" height=\"{1}\" orgwidth=\"{0}\" orgheight=\"{1}\" usemap=\"#map1\" alt=\"\" />", w, h));
stringToFile.AppendLine("<map name=\"map1\" id=\"map1\">");
byte dataKey1 = (byte)'a';
byte dataKey2 = (byte)'a';
int startIndex = 0;
int endIndex = 0;
while (true)
{
string color = ExtractData(svg, startIndex, out endIndex, "<path", "fill");
string svg_input = ExtractData(svg, startIndex, out endIndex, "<path", "d=");
if (svg_input == null)
break;
startIndex = endIndex;
/// Start..
stringToFile.Append(string.Format("<area data-key=\"{0}{1}\" shape=\"poly\" href=\"targetpage.html\" alt=\"Description\" coords=\"", (char)dataKey1, (char)dataKey2));
dataKey1 += 1;
if (dataKey1 > (byte)'z')
{
dataKey2 += 1;
dataKey1 = (byte)'a';
}
bool bFinished = false;
while (!bFinished)
{
string[] points = new string[0];
string pattern = "";
svg_input = svg_input.ToUpper().Trim();
char code = svg_input[0];
switch (code)
{
case 'M':
case 'L':
pattern = svg_input.Substring(0, svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ") + 1) + 1));
svg_input = svg_input.Remove(0, pattern.Length);
points = pattern.Trim().Substring(1).Trim().Split(' ');
break;
case 'C':
pattern = svg_input.Substring(0, svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ", svg_input.IndexOf(" ") + 1) + 1) + 1) + 1) + 1) + 1));
svg_input = svg_input.Remove(0, pattern.Length);
points = pattern.Trim().Substring(1).Trim().Split(' ');
break;
case 'Z':
bFinished = true;
continue;
default:
throw new Exception("Invalid pattern");
}
int count = points.Length;
if (count > 4)
count = 4;
for (int i = 0; i < count; i++)
{
var valueParts = points[i].Split('.');
// remove part after comma - we don't need this accurancy in HTML
stringToFile.Append(valueParts[0]);
// comma for seperation - don't worry, we'll clean the last ones within an area out later
stringToFile.Append(",");
}
}
stringToFile.AppendLine("\" />");
}
// clean obsolete commas - not pretty nor efficient
stringToFile.AppendLine("</map>");
stringToFile = stringToFile.Replace(",\"", "\"");
return stringToFile.ToString();
}
private string ExtractData(string data, int startIndex, out int endIndex, string key, string param)
{
try
{
endIndex = 0;
int a = data.IndexOf(key, startIndex);
int a2 = data.IndexOf(key, a + key.Length);
if (a2 == -1)
a2 = data.IndexOf(">", a + key.Length);
int b = data.IndexOf(param, a + key.Length);
int start = data.IndexOf("\"", b + param.Length) + 1;
int end = data.IndexOf("\"", start + 1);
if (b > a2 || start > a2 || end > a2)
return null;
int len = end - start;
endIndex = end;
string t = data.Substring(start, len);
return t;
}
catch
{
endIndex = 0;
return null;
}
}
尷尬,我不能確定那是什麼語言只是看它。講述'var'表示JavaScript,但頭部看起來更像C++或Java。 T.T;是C#嗎?也許? – Ziggy 2012-11-29 19:28:55