public static float ParseLength(String str, float actualFontSize) {
if (str == null)
return 0f;
int pos = 0;
int length = str.Length;
bool ok = true;
while (ok && pos < length) {
switch (str[pos]) {
case '+':
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
pos++;
break;
default:
ok = false;
break;
}
}
if (pos == 0) return 0f;
if (pos == length) return float.Parse(str, System.Globalization.NumberFormatInfo.InvariantInfo);
float f = float.Parse(str.Substring(0, pos), System.Globalization.NumberFormatInfo.InvariantInfo);
str = str.Substring(pos);
// inches
if (str.StartsWith("in")) {
return f * 72f;
}
// centimeters
if (str.StartsWith("cm")) {
return (f / 2.54f) * 72f;
}
// millimeters
if (str.StartsWith("mm")) {
return (f / 25.4f) * 72f;
}
// picas
if (str.StartsWith("pc")) {
return f * 12f;
}
// 1em is equal to the current font size
if (str.StartsWith("em")) {
return f * actualFontSize;
}
// one ex is the x-height of a font (x-height is usually about half the
// font-size)
if (str.StartsWith("ex")) {
return f * actualFontSize / 2;
}
// default: we assume the length was measured in points
return f;
}