1 47 package com.lowagie.text.pdf; 48 49 import java.util.ArrayList ; 50 51 import com.lowagie.text.Chunk; 52 import com.lowagie.text.Font; 53 import com.lowagie.text.Phrase; 54 55 63 public class FontSelector { 64 65 protected ArrayList fonts = new ArrayList (); 66 67 71 public void addFont(Font font) { 72 if (font.getBaseFont() != null) { 73 fonts.add(font); 74 return; 75 } 76 BaseFont bf = font.getCalculatedBaseFont(true); 77 Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor()); 78 fonts.add(f2); 79 } 80 81 87 public Phrase process(String text) { 88 int fsize = fonts.size(); 89 if (fsize == 0) 90 throw new IndexOutOfBoundsException ("No font is defined."); 91 char cc[] = text.toCharArray(); 92 int len = cc.length; 93 StringBuffer sb = new StringBuffer (); 94 Font font = null; 95 int lastidx = -1; 96 Phrase ret = new Phrase(); 97 for (int k = 0; k < len; ++k) { 98 char c = cc[k]; 99 if (c == '\n' || c == '\r') { 100 sb.append(c); 101 continue; 102 } 103 for (int f = 0; f < fsize; ++f) { 104 font = (Font)fonts.get(f); 105 if (font.getBaseFont().charExists(c)) { 106 if (lastidx == f) 107 sb.append(c); 108 else { 109 if (sb.length() > 0 && lastidx != -1) { 110 Chunk ck = new Chunk(sb.toString(), (Font)fonts.get(lastidx)); 111 ret.add(ck); 112 sb.setLength(0); 113 } 114 sb.append(c); 115 lastidx = f; 116 } 117 break; 118 } 119 } 120 } 121 if (sb.length() > 0) { 122 Chunk ck = new Chunk(sb.toString(), (Font)fonts.get(lastidx == -1 ? 0 : lastidx)); 123 ret.add(ck); 124 } 125 return ret; 126 } 127 } 128 | Popular Tags |