1 4 5 9 10 package org.openlaszlo.compiler; 11 import java.io.*; 12 import java.util.*; 13 import org.openlaszlo.xml.internal.XMLUtils; 14 import org.apache.log4j.*; 15 16 17 21 22 23 24 class LineMetrics { 25 double maxw = 0.0; 26 double w = 0.0; 27 boolean verbatim = false; 28 int nlines = 1; 29 30 int last_space_pos; 31 int last_newline_pos; 32 double last_spacewidth = 0; 33 34 int trailing_newlines = 0; 35 36 48 51 boolean trim = true; 52 53 StringBuffer buf = new StringBuffer (); 54 55 public String toString() { 56 return "LineMetrics: maxw="+maxw+" nlines="+nlines+" verbatim="+verbatim+ " str=|"+buf.toString()+"|"; 57 } 58 59 75 76 void addHTML (String rawtext, String normalized_text, FontInfo fontInfo, SWFWriter generator) { 77 if (rawtext.length() == 0) { 78 return; 79 } 80 boolean leading_whitespace = Character.isWhitespace(rawtext.charAt(0)); 81 boolean trailing_whitespace = Character.isWhitespace(rawtext.charAt(rawtext.length()-1)); 82 boolean all_whitespace = (normalized_text.length() == 0); 83 84 if (all_whitespace) { 85 if (!this.trim && (leading_whitespace || trailing_whitespace)) { 86 normalized_text = " "; 87 } else { 88 normalized_text = ""; 89 } 90 this.trim = true; 91 } else { 92 if (!this.trim && leading_whitespace) { 93 normalized_text = " " + normalized_text; 94 } 95 if (trailing_whitespace) { 96 normalized_text = normalized_text + " "; 97 } 98 this.trim = trailing_whitespace; 99 } 100 101 addSpan(normalized_text, fontInfo, generator); 102 } 103 104 void setVerbatim (boolean val) { 105 this.verbatim = val; 106 resetLineWidth(); 107 } 108 109 111 void addSpan (String str, FontInfo fontInfo, SWFWriter generator) { 112 if (str.length() > 0) { 113 if (generator != null) { 114 double sw = TextCompiler.getTextWidth(str, fontInfo, generator, this); 115 w += sw; 116 } 117 str = XMLUtils.escapeXml(str); 118 119 if (!verbatim) { 122 int buflen = buf.length(); 123 char lastchar = str.charAt(str.length()-1); 124 if (lastchar == ' ') { 125 last_space_pos = buflen + str.length() -1; 126 } else { 127 last_space_pos = -1; 128 } 129 130 int newline_pos = str.lastIndexOf('\n'); 131 if (newline_pos >= 0) { 132 last_newline_pos = newline_pos + buflen; 133 } 134 } 135 buf.append(str); 136 } 137 } 138 139 140 void addFormat (String str) { 141 buf.append(str); 142 } 143 144 void addStartTag (String tagName, FontInfo fontInfo, SWFWriter generator) { 145 addFormat("<" + tagName); 146 } 147 148 void endStartTag () { 149 addFormat(">"); 150 } 151 152 void addEndTag (String tagName) { 153 addFormat("</" + tagName + ">"); 154 } 155 156 157 String getText () { 158 return buf.toString(); 159 } 160 161 162 void endOfLine() { 163 if (!verbatim) { 165 if (last_space_pos > 0 && last_space_pos > last_newline_pos) { 166 buf.deleteCharAt(last_space_pos); 167 w -= last_spacewidth; 168 } 169 } 170 maxw = Math.max(maxw, w); 171 w = 0.0; 172 last_space_pos = -1; 173 } 174 175 177 void resetLineWidth() { 178 maxw = Math.max(maxw, w); 179 w = 0.0; 180 last_space_pos = -1; 181 } 182 183 184 void newline() { 185 nlines++; 186 endOfLine(); 187 trim = true; 188 buf.append("<br/>"); 189 } 190 191 193 void paragraphBreak () { 194 if (buf.length() == 0) return; 196 197 trailing_newlines = 0; 199 for (int i = buf.length()-1; i >= 0; i--) { 200 char c = buf.charAt(i); 201 if (c == '\t' || c == ' ') continue; 202 if (c == '\n') { 203 trailing_newlines++; 204 } else { 205 break; 206 } 207 } 208 if (trailing_newlines == 0) { 209 buf.append("<br/><br/>"); 210 } else if (trailing_newlines == 1) { 211 buf.append("<br/>"); 212 } else { 213 } 215 } 216 217 } 218 | Popular Tags |