| 1 |
package org.freehep.graphicsio.ps; |
| 2 |
|
| 3 |
import java.awt.font.TextAttribute; |
| 4 |
import java.text.AttributedCharacterIterator.Attribute; |
| 5 |
import java.util.Map; |
| 6 |
|
| 7 |
import org.freehep.graphics2d.GenericTagHandler; |
| 8 |
import org.freehep.graphicsbase.util.ScientificFormat; |
| 9 |
import org.freehep.graphicsbase.util.Value; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class to create a "cfont" tag in PS. Use {@link PSStringStyler#getStyledString(java.util.Map, String)} |
| 13 |
* to operate. |
| 14 |
* |
| 15 |
* @author Charles Loomis |
| 16 |
* @author Steffen Greiffenberg |
| 17 |
* @version $Id: freehep-graphicsio-ps/src/main/java/org/freehep/graphicsio/ps/PSStringStyler.java cba39eb5843a 2006/03/20 18:04:28 duns $ |
| 18 |
*/ |
| 19 |
public class PSStringStyler { |
| 20 |
|
| 21 |
/** |
| 22 |
* The value 5 / 6 was found by try and error, e.g. |
| 23 |
* a font size of 300 has a valid cfont size of 250. |
| 24 |
* Not clear why. |
| 25 |
*/ |
| 26 |
private static final float FONTSIZE_CORRECTION = 5.0f / 6.0f; |
| 27 |
|
| 28 |
// Constant strings for the styles. |
| 29 |
// private final String UNSTYLED_FLAG = "\\340\\000"; |
| 30 |
|
| 31 |
private static final String BOLD_CHAR = "\\340\\001"; |
| 32 |
|
| 33 |
private static final String ITALIC_CHAR = "\\340\\002"; |
| 34 |
|
| 35 |
private static final String BOLD_ITALIC_CHAR = "\\340\\003"; |
| 36 |
|
| 37 |
// Constant strings for the ornaments (underlining, etc.) |
| 38 |
// superscript is done by transformation |
| 39 |
// private final String SUPERSUBSCRIPT_FLAG = "\\360\\000"; |
| 40 |
|
| 41 |
private static final String STRIKEOUT_FLAG = "\\360\\001"; |
| 42 |
|
| 43 |
private static final String UNDERLINE_FLAG = "\\360\\002"; |
| 44 |
|
| 45 |
private static final String DASHED_UNDERLINE_FLAG = "\\360\\003"; |
| 46 |
|
| 47 |
private static final String DOTTED_UNDERLINE_FLAG = "\\360\\004"; |
| 48 |
|
| 49 |
private static final String GRAY_UNDERLINE_FLAG = "\\360\\005"; |
| 50 |
|
| 51 |
private static final String THICK_UNDERLINE_FLAG = "\\360\\006"; |
| 52 |
|
| 53 |
private static final String OVERLINE_FLAG = "\\360\\007"; |
| 54 |
|
| 55 |
// Constant strings giving the begin-group and end-group markers. |
| 56 |
private static final String BEGIN_GROUP = "\\360\\376"; |
| 57 |
|
| 58 |
private static final String END_GROUP = "\\360\\377"; |
| 59 |
|
| 60 |
/** |
| 61 |
* Must override the parse method because the PostScript string must be |
| 62 |
* initialized and terminated. |
| 63 |
*/ |
| 64 |
public static String getStyledString(Map /*<TextAttribute,?>*/<Attribute, Object> attributes, String string) { |
| 65 |
StringBuffer result = new StringBuffer(); |
| 66 |
|
| 67 |
Float size = (Float) attributes.get(TextAttribute.SIZE); |
| 68 |
|
| 69 |
// FIXME: The lines are taken from PSTagHandler, but |
| 70 |
// they produced a to large text output. |
| 71 |
result.append(new ScientificFormat(6, 9, false).format(size.floatValue() * FONTSIZE_CORRECTION)); |
| 72 |
result.append(" "); |
| 73 |
|
| 74 |
// cfont tag |
| 75 |
result.append("cfont"); |
| 76 |
|
| 77 |
// replace font name |
| 78 |
String fontName = ((String)attributes.get(TextAttribute.FAMILY)).toLowerCase(); |
| 79 |
if (fontName.indexOf("helvetica") != -1) { |
| 80 |
result.append("H"); |
| 81 |
} else if (fontName.indexOf("dialog") != -1) { |
| 82 |
result.append("H"); |
| 83 |
} else if (fontName.indexOf("dialoginput") != -1) { |
| 84 |
result.append("H"); |
| 85 |
} else if (fontName.indexOf("sansserif") != -1) { |
| 86 |
result.append("H"); |
| 87 |
} else if (fontName.indexOf("times") != -1) { |
| 88 |
result.append("T"); |
| 89 |
} else if (fontName.indexOf("serif") != -1) { |
| 90 |
result.append("T"); |
| 91 |
} else if (fontName.indexOf("courier") != -1) { |
| 92 |
result.append("C"); |
| 93 |
} else if (fontName.indexOf("monospaced") != -1) { |
| 94 |
result.append("C"); |
| 95 |
} else if (fontName.indexOf("typewriter") != -1) { |
| 96 |
result.append("C"); |
| 97 |
} else { |
| 98 |
result.append("H"); |
| 99 |
} |
| 100 |
|
| 101 |
// weight |
| 102 |
Object weight = attributes.get(TextAttribute.WEIGHT); |
| 103 |
if (TextAttribute.WEIGHT_BOLD.equals(weight)) { |
| 104 |
result.append("B"); |
| 105 |
} |
| 106 |
|
| 107 |
// posture |
| 108 |
Object posture = attributes.get(TextAttribute.POSTURE); |
| 109 |
if (TextAttribute.POSTURE_OBLIQUE.equals(posture)) { |
| 110 |
result.append("I"); |
| 111 |
} |
| 112 |
|
| 113 |
// open the brackets for text string |
| 114 |
result.append("\n("); |
| 115 |
|
| 116 |
// count BEGIN_GROUP for writing END_GROUP |
| 117 |
Value openGroups = new Value(); |
| 118 |
openGroups.set(0); |
| 119 |
|
| 120 |
result.append(getOpenTag(attributes, openGroups)); |
| 121 |
result.append(getUnicodes(string)); |
| 122 |
result.append(getCloseTag(openGroups)); |
| 123 |
|
| 124 |
result.append(")"); |
| 125 |
|
| 126 |
return result.toString(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Converts str into unicodes |
| 131 |
* |
| 132 |
* @return list of unicodes for str |
| 133 |
*/ |
| 134 |
public static String getUnicodes(String str) { |
| 135 |
|
| 136 |
// Copy string to temporary string buffer. |
| 137 |
StringBuffer codedString = new StringBuffer(); |
| 138 |
|
| 139 |
for (int i = 0; i < str.length(); i++) { |
| 140 |
int chr = (int) str.charAt(i); |
| 141 |
int cvalue = (chr & 0x000000ff); |
| 142 |
int fvalue = (chr & 0x0000ff00) >>> 8; |
| 143 |
String cbyte = Integer.toOctalString(cvalue); |
| 144 |
String fbyte = Integer.toOctalString(fvalue); |
| 145 |
|
| 146 |
// Put in the font byte first. |
| 147 |
codedString.append('\\'); |
| 148 |
for (int j = 0; j < (3 - fbyte.length()); j++) { |
| 149 |
codedString.append('0'); |
| 150 |
} |
| 151 |
codedString.append(fbyte); |
| 152 |
|
| 153 |
// Now the character itself. Write as octal codes |
| 154 |
// non-printable characters, the backslash, parentheses, |
| 155 |
// and the percent. |
| 156 |
if (cvalue < 32 || cvalue > 126 || cvalue == '\\' || cvalue == '%' |
| 157 |
|| cvalue == '(' || cvalue == ')') { |
| 158 |
codedString.append('\\'); |
| 159 |
for (int j = 0; j < (3 - cbyte.length()); j++) { |
| 160 |
codedString.append('0'); |
| 161 |
} |
| 162 |
codedString.append(cbyte); |
| 163 |
} else { |
| 164 |
codedString.append((char) cvalue); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return codedString.toString(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @param str string to convert |
| 173 |
* @return str with replaced (, ), \, % |
| 174 |
*/ |
| 175 |
public static String getEscaped(String str) { |
| 176 |
|
| 177 |
// Then protect against unbalanced parentheses in the string. |
| 178 |
// Copy string to temporary string buffer. |
| 179 |
StringBuffer result = new StringBuffer(); |
| 180 |
|
| 181 |
result.append("("); |
| 182 |
|
| 183 |
// Loop over all characters in the string and escape the |
| 184 |
// parentheses. |
| 185 |
for (int i = 0; i < str.length(); i++) { |
| 186 |
char c = str.charAt(i); |
| 187 |
if (c == '(' || c == ')' || c == '\\' || c == '%') { |
| 188 |
result.append('\\'); |
| 189 |
result.append(c); |
| 190 |
} else if (c == 0) { |
| 191 |
result.append('?'); |
| 192 |
} else { |
| 193 |
result.append(c); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
result.append(")"); |
| 198 |
|
| 199 |
return result.toString(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* handles various instances of {@link TextAttribute} |
| 204 |
* |
| 205 |
* @param attributes font to translate in PS tags |
| 206 |
*/ |
| 207 |
private static String getOpenTag(Map/* Map<TextAttribute,?>*/<Attribute, Object> attributes, Value open) { |
| 208 |
|
| 209 |
int openGroups = open.getInt(); |
| 210 |
|
| 211 |
StringBuffer result = new StringBuffer(); |
| 212 |
|
| 213 |
// weight |
| 214 |
Object weight = attributes.get(TextAttribute.WEIGHT); |
| 215 |
|
| 216 |
// italic |
| 217 |
Object posture = attributes.get(TextAttribute.POSTURE); |
| 218 |
|
| 219 |
// bold and italic? |
| 220 |
if (weight != null && !TextAttribute.WEIGHT_REGULAR.equals(weight)) { |
| 221 |
if (TextAttribute.POSTURE_OBLIQUE.equals(posture)) { |
| 222 |
result.append(BOLD_ITALIC_CHAR); |
| 223 |
} else { |
| 224 |
result.append(BOLD_CHAR); |
| 225 |
} |
| 226 |
openGroups ++; |
| 227 |
result.append(BEGIN_GROUP); |
| 228 |
|
| 229 |
} else |
| 230 |
|
| 231 |
// simple italic? |
| 232 |
if (TextAttribute.POSTURE_OBLIQUE.equals(posture)) { |
| 233 |
result.append(ITALIC_CHAR); |
| 234 |
openGroups ++; |
| 235 |
result.append(BEGIN_GROUP); |
| 236 |
} |
| 237 |
|
| 238 |
// superScript is done by font.getTransform() |
| 239 |
// Object superScript = attributes.get(TextAttribute.SUPERSCRIPT); |
| 240 |
|
| 241 |
// underline |
| 242 |
Object underline = attributes.get(TextAttribute.UNDERLINE); |
| 243 |
if (TextAttribute.UNDERLINE_LOW_DASHED.equals(underline)) { |
| 244 |
result.append(DASHED_UNDERLINE_FLAG); |
| 245 |
openGroups ++; |
| 246 |
result.append(BEGIN_GROUP); |
| 247 |
} else if (TextAttribute.UNDERLINE_ON.equals(underline)) { |
| 248 |
result.append(UNDERLINE_FLAG); |
| 249 |
openGroups ++; |
| 250 |
result.append(BEGIN_GROUP); |
| 251 |
} else if (TextAttribute.UNDERLINE_LOW_DOTTED.equals(underline)) { |
| 252 |
result.append(DOTTED_UNDERLINE_FLAG); |
| 253 |
openGroups ++; |
| 254 |
result.append(BEGIN_GROUP); |
| 255 |
} else if (TextAttribute.UNDERLINE_LOW_TWO_PIXEL.equals(underline)) { |
| 256 |
result.append(THICK_UNDERLINE_FLAG); |
| 257 |
openGroups ++; |
| 258 |
result.append(BEGIN_GROUP); |
| 259 |
} else if (TextAttribute.UNDERLINE_LOW_GRAY.equals(underline)) { |
| 260 |
result.append(GRAY_UNDERLINE_FLAG); |
| 261 |
openGroups ++; |
| 262 |
result.append(BEGIN_GROUP); |
| 263 |
} else if (TextAttribute.UNDERLINE_LOW_DASHED.equals(underline)) { |
| 264 |
result.append(DASHED_UNDERLINE_FLAG); |
| 265 |
openGroups ++; |
| 266 |
result.append(BEGIN_GROUP); |
| 267 |
} else if (GenericTagHandler.UNDERLINE_OVERLINE.equals(underline)) { |
| 268 |
result.append(OVERLINE_FLAG); |
| 269 |
result.append(BEGIN_GROUP); |
| 270 |
} |
| 271 |
|
| 272 |
// strike through |
| 273 |
Object strike = attributes.get(TextAttribute.STRIKETHROUGH); |
| 274 |
if (TextAttribute.STRIKETHROUGH_ON.equals(strike)) { |
| 275 |
result.append(STRIKEOUT_FLAG); |
| 276 |
openGroups ++; |
| 277 |
result.append(BEGIN_GROUP); |
| 278 |
} |
| 279 |
|
| 280 |
// set the counter |
| 281 |
open.set(openGroups); |
| 282 |
|
| 283 |
return result.toString(); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @return string for closing all BEGIN_GROUP's |
| 288 |
*/ |
| 289 |
private static String getCloseTag(Value open) { |
| 290 |
StringBuffer result = new StringBuffer(); |
| 291 |
|
| 292 |
for (int i = 0; i < open.getInt(); i++) { |
| 293 |
result.append(END_GROUP); |
| 294 |
} |
| 295 |
|
| 296 |
// not needed but to leave the counter in correct state |
| 297 |
open.set(0); |
| 298 |
|
| 299 |
return result.toString(); |
| 300 |
} |
| 301 |
} |