1 26 27 package it.stefanochizzolini.clown.documents.contents.composition; 28 29 import it.stefanochizzolini.clown.documents.contents.fonts.Font; 30 31 38 final class TextFitter 39 { 40 private Font font; 44 private double fontSize; 45 private boolean hyphenation; 46 private String text; 47 private double width; 48 49 private int beginIndex = 0; 50 private int endIndex = -1; 51 private String fittedText; 52 private double fittedWidth; 53 55 TextFitter( 57 String text, 58 double width, 59 Font font, 60 double fontSize, 61 boolean hyphenation 62 ) 63 { 64 this.text = text; 65 this.width = width; 66 this.font = font; 67 this.fontSize = fontSize; 68 this.hyphenation = hyphenation; 69 } 70 72 78 public boolean fit( 79 ) 80 { 81 return fit( 82 endIndex + 1, 83 width 84 ); 85 } 86 87 93 public boolean fit( 94 int index, 95 double width 96 ) 97 { 98 beginIndex = index; 99 this.width = width; 100 fittedText = null; 101 fittedWidth = 0; 102 103 while(true) 104 { 105 int foundIndex = text.indexOf(' ',index + 1); 107 if(foundIndex == -1) 108 {foundIndex = text.length();} 109 110 double wordWidth = font.getKernedWidth( 112 text.substring(index,foundIndex), 113 fontSize 114 ); fittedWidth += wordWidth; 116 if(fittedWidth > width) 118 { 119 fittedWidth -= wordWidth; 121 foundIndex = index; 122 if(foundIndex == 0) 123 break; 124 if(!hyphenation) 125 { 126 fittedText = text.substring(beginIndex,index); 127 break; 128 } 129 130 133 136 while(true) 137 { 138 char textChar = text.charAt(foundIndex); wordWidth = (font.getKerning(text.charAt(foundIndex - 1),textChar) + font.getWidth(textChar)) * font.getScalingFactor(fontSize); foundIndex++; 142 fittedWidth += wordWidth; 143 if(fittedWidth > width) 145 { 146 fittedWidth -= wordWidth; 148 foundIndex--; 149 if(foundIndex > index + 4) { 152 foundIndex--; 154 index = foundIndex; 155 textChar = text.charAt(foundIndex); 156 fittedWidth -= (font.getKerning(text.charAt(foundIndex - 1),textChar) + font.getWidth(textChar)) * font.getScalingFactor(fontSize); 157 158 textChar = '-'; fittedWidth += (font.getKerning(text.charAt(foundIndex - 1),textChar) + font.getWidth(textChar)) * font.getScalingFactor(fontSize); 161 162 fittedText = text.substring(beginIndex,index) + textChar; 163 } 164 else { 166 while(foundIndex > index) 168 { 169 foundIndex--; 170 textChar = text.charAt(foundIndex); 171 fittedWidth -= (font.getKerning(text.charAt(foundIndex - 1),textChar) + font.getWidth(textChar)) * font.getScalingFactor(fontSize); 172 } 173 174 fittedText = text.substring(beginIndex,index); 175 } 176 break; 177 } 178 } 179 break; 180 } 181 182 index = foundIndex; 183 if(index == text.length()) 184 { 185 fittedText = text.substring(beginIndex,index); 186 break; 187 } 188 } 189 190 endIndex = index; 191 192 return (fittedWidth > 0); 193 } 194 195 198 public int getBeginIndex( 199 ) 200 {return beginIndex;} 201 202 205 public int getEndIndex( 206 ) 207 {return endIndex;} 208 209 212 public String getFittedText( 213 ) 214 {return fittedText;} 215 216 219 public double getFittedWidth( 220 ) 221 {return fittedWidth;} 222 223 226 public Font getFont( 227 ) 228 {return font;} 229 230 233 public double getFontSize( 234 ) 235 {return fontSize;} 236 237 240 public boolean getHyphenation( 241 ) 242 {return hyphenation;} 243 244 247 public String getText( 248 ) 249 {return text;} 250 251 254 public double getWidth( 255 ) 256 {return width;} 257 } | Popular Tags |