private void FillMetrics(byte[] touni, IntHashtable widths, int dw)
{
PdfContentParser ps = new PdfContentParser(new PRTokeniser(touni));
PdfObject ob = null;
bool notFound = true;
int nestLevel = 0;
int maxExc = 50;
while ((notFound || nestLevel > 0)) {
try {
ob = ps.ReadPRObject();
}
catch {
if (--maxExc < 0)
break;
continue;
}
if (ob == null)
break;
if (ob.Type == PdfContentParser.COMMAND_TYPE) {
if (ob.ToString().Equals("begin")) {
notFound = false;
nestLevel++;
}
else if (ob.ToString().Equals("end")) {
nestLevel--;
}
else if (ob.ToString().Equals("beginbfchar")) {
while (true) {
PdfObject nx = ps.ReadPRObject();
if (nx.ToString().Equals("endbfchar"))
break;
String cid = DecodeString((PdfString)nx);
String uni = DecodeString((PdfString)ps.ReadPRObject());
if (uni.Length == 1) {
int cidc = (int)cid[0];
int unic = (int)uni[uni.Length - 1];
int w = dw;
if (widths.ContainsKey(cidc))
w = widths[cidc];
metrics[unic] = new int[]{cidc, w};
}
}
}
else if (ob.ToString().Equals("beginbfrange")) {
while (true) {
PdfObject nx = ps.ReadPRObject();
if (nx.ToString().Equals("endbfrange"))
break;
String cid1 = DecodeString((PdfString)nx);
String cid2 = DecodeString((PdfString)ps.ReadPRObject());
int cid1c = (int)cid1[0];
int cid2c = (int)cid2[0];
PdfObject ob2 = ps.ReadPRObject();
if (ob2.IsString()) {
String uni = DecodeString((PdfString)ob2);
if (uni.Length == 1) {
int unic = (int)uni[uni.Length - 1];
for (; cid1c <= cid2c; cid1c++, unic++) {
int w = dw;
if (widths.ContainsKey(cid1c))
w = widths[cid1c];
metrics[unic] = new int[]{cid1c, w};
}
}
}
else {
PdfArray a = (PdfArray)ob2;
for (int j = 0; j < a.Size; ++j, ++cid1c) {
String uni = DecodeString(a.GetAsString(j));
if (uni.Length == 1) {
int unic = (int)uni[uni.Length - 1];
int w = dw;
if (widths.ContainsKey(cid1c))
w = widths[cid1c];
metrics[unic] = new int[]{cid1c, w};
}
}
}
}
}
}
}
}