1 51 package org.apache.fop.fo; 52 53 import org.apache.fop.layout.Area; 55 import org.apache.fop.layout.BlockArea; 56 import org.apache.fop.layout.FontState; 57 import org.apache.fop.layout.*; 58 import org.apache.fop.datatypes.*; 59 import org.apache.fop.fo.properties.*; 60 import org.apache.fop.apps.FOPException; 61 62 66 public class FOText extends FONode { 67 68 private char[] ca; 69 70 private FontState fs; 71 private float red; 72 private float green; 73 private float blue; 74 private int wrapOption; 75 private int whiteSpaceCollapse; 76 private int verticalAlign; 77 78 private TextState ts; 80 81 public FOText(StringBuffer b, FObj parent) { 82 super(parent); 83 this.ca = new char[b.length()]; 84 b.getChars(0,b.length(),ca,0); 85 } 86 87 public void setTextState(TextState ts) { 88 this.ts = ts; 89 } 90 91 public boolean willCreateArea() { 92 this.whiteSpaceCollapse = 93 this.parent.properties.get("white-space-collapse").getEnum(); 94 if (this.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE 95 && ca.length > 0) { 96 return true; 97 } 98 99 for (int i = 0; i < ca.length; i++) { 100 char ch = ca[i]; 101 if (!((ch == ' ') || (ch == '\n') || (ch == '\r') 102 || (ch == '\t'))) { return true; 104 } 105 } 106 return false; 107 } 108 109 public boolean mayPrecedeMarker() { 110 for (int i = 0; i < ca.length; i++) { 111 char ch = ca[i]; 112 if ((ch != ' ') || (ch != '\n') || (ch != '\r') 113 || (ch != '\t')) { return true; 115 } 116 } 117 return false; 118 } 119 120 public int layout(Area area) throws FOPException { 121 if (!(area instanceof BlockArea)) { 122 log.error("text outside block area" 123 + new String (ca, 0, ca.length)); 124 return Status.OK; 125 } 126 if (this.marker == START) { 127 String fontFamily = 128 this.parent.properties.get("font-family").getString(); 129 String fontStyle = 130 this.parent.properties.get("font-style").getString(); 131 String fontWeight = 132 this.parent.properties.get("font-weight").getString(); 133 int fontSize = 134 this.parent.properties.get("font-size").getLength().mvalue(); 135 int fontVariant = 138 this.parent.properties.get("font-variant").getEnum(); 139 140 int letterSpacing = 141 this.parent.properties.get("letter-spacing").getLength().mvalue(); 142 this.fs = new FontState(area.getFontInfo(), fontFamily, 143 fontStyle, fontWeight, fontSize, 144 fontVariant, letterSpacing); 145 146 ColorType c = this.parent.properties.get("color").getColorType(); 147 this.red = c.red(); 148 this.green = c.green(); 149 this.blue = c.blue(); 150 151 this.verticalAlign = 152 this.parent.properties.get("vertical-align").getEnum(); 153 154 this.wrapOption = 155 this.parent.properties.get("wrap-option").getEnum(); 156 this.whiteSpaceCollapse = 157 this.parent.properties.get("white-space-collapse").getEnum(); 158 this.marker = 0; 159 } 160 int orig_start = this.marker; 161 this.marker = addText((BlockArea)area, fs, red, green, blue, 162 wrapOption, this.getLinkSet(), 163 whiteSpaceCollapse, ca, this.marker, ca.length, 164 ts, verticalAlign); 165 if (this.marker == -1) { 166 167 168 177 return Status.OK; 179 } else if (this.marker != orig_start) { 180 return Status.AREA_FULL_SOME; 181 } else { 182 return Status.AREA_FULL_NONE; 183 } 184 } 185 186 public static int addText(BlockArea ba, FontState fontState, float red, 189 float green, float blue, int wrapOption, 190 LinkSet ls, int whiteSpaceCollapse, 191 char data[], int start, int end, 192 TextState textState, int vAlign) { 193 if (fontState.getFontVariant() == FontVariant.SMALL_CAPS) { 194 FontState smallCapsFontState; 195 try { 196 int smallCapsFontHeight = 197 (int)(((double)fontState.getFontSize()) * 0.8d); 198 smallCapsFontState = new FontState(fontState.getFontInfo(), 199 fontState.getFontFamily(), 200 fontState.getFontStyle(), 201 fontState.getFontWeight(), 202 smallCapsFontHeight, 203 FontVariant.NORMAL); 204 } catch (FOPException ex) { 205 smallCapsFontState = fontState; 206 } 209 210 char c; 212 char newdata[] = new char[end]; 213 boolean isLowerCase; 214 int caseStart; 215 FontState fontStateToUse; 216 for (int i = start; i < end; ) { 217 caseStart = i; 218 c = data[i]; 219 isLowerCase = (java.lang.Character.isLetter(c) 220 && java.lang.Character.isLowerCase(c)); 221 while (isLowerCase 222 == (java.lang.Character.isLetter(c) 223 && java.lang.Character.isLowerCase(c))) { 224 if (isLowerCase) { 225 newdata[i] = java.lang.Character.toUpperCase(c); 226 } else { 227 newdata[i] = c; 228 } 229 i++; 230 if (i == end) 231 break; 232 c = data[i]; 233 } 234 if (isLowerCase) { 235 fontStateToUse = smallCapsFontState; 236 } else { 237 fontStateToUse = fontState; 238 } 239 int index = addRealText(ba, fontStateToUse, red, green, blue, 240 wrapOption, ls, whiteSpaceCollapse, 241 newdata, caseStart, i, textState, 242 vAlign); 243 if (index != -1) { 244 return index; 245 } 246 } 247 248 return -1; 249 } 250 251 return addRealText(ba, fontState, red, green, blue, wrapOption, ls, 253 whiteSpaceCollapse, data, start, end, textState, 254 vAlign); 255 } 256 257 protected static int addRealText(BlockArea ba, FontState fontState, 258 float red, float green, float blue, 259 int wrapOption, LinkSet ls, 260 int whiteSpaceCollapse, char data[], 261 int start, int end, TextState textState, 262 int vAlign) { 263 LineArea la = ba.getCurrentLineArea(); 264 if (la == null) { 265 return start; 266 } 267 268 la.changeFont(fontState); 269 la.changeColor(red, green, blue); 270 la.changeWrapOption(wrapOption); 271 la.changeWhiteSpaceCollapse(whiteSpaceCollapse); 272 la.changeVerticalAlign(vAlign); 273 ba.setupLinkSet(ls); 277 278 start = la.addText(data, start, end, ls, textState); 279 280 while ( start != -1) { 281 la = ba.createNextLineArea(); 282 if (la == null) { 283 return start; 284 } 285 la.changeFont(fontState); 286 la.changeColor(red, green, blue); 287 la.changeWrapOption(wrapOption); 288 la.changeWhiteSpaceCollapse(whiteSpaceCollapse); 289 ba.setupLinkSet(ls); 293 294 start = la.addText(data, start, end, ls, textState); 295 } 296 return -1; 297 } 298 299 300 } 301 | Popular Tags |