| Code with Finding: |
class FontSelector {
/**
* Process the text so that it will render with a combination of fonts
* if needed.
* @param text the text
* @return a <CODE>Phrase</CODE> with one or more chunks
*/
public Phrase process(String text) {
int fsize = fonts.size();
if (fsize == 0)
throw new IndexOutOfBoundsException(MessageLocalization.getComposedMessage("no.font.is.defined"));
char cc[] = text.toCharArray();
int len = cc.length;
StringBuffer sb = new StringBuffer();
Font font = null;
int lastidx = -1;
Phrase ret = new Phrase();
for (int k = 0; k < len; ++k) {
char c = cc[k];
if (c == '\n' || c == '\r') {
sb.append(c);
continue;
}
if (Utilities.isSurrogatePair(cc, k)) {
int u = Utilities.convertToUtf32(cc, k);
for (int f = 0; f < fsize; ++f) {
font = fonts.get(f);
if (font.getBaseFont().charExists(u)) {
if (lastidx != f) {
if (sb.length() > 0 && lastidx != -1) {
Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx));
ret.add(ck);
sb.setLength(0);
}
lastidx = f;
}
sb.append(c);
sb.append(cc[++k]);
break;
}
}
}
else {
for (int f = 0; f < fsize; ++f) {
font = fonts.get(f);
if (font.getBaseFont().charExists(c)) {
if (lastidx != f) {
if (sb.length() > 0 && lastidx != -1) {
Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx));
ret.add(ck);
sb.setLength(0);
}
lastidx = f;
}
sb.append(c);
break;
}
}
}
}
if (sb.length() > 0) {
Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx == -1 ? 0 : lastidx));
ret.add(ck);
}
return ret;
}
}
|