1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.*; 17 18 42 43 public final class FontData { 44 45 55 public LOGFONT data; 56 57 67 public float height; 68 69 72 String lang, country, variant; 73 74 77 public FontData() { 78 data = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 79 data.lfCharSet = (byte)OS.DEFAULT_CHARSET; 83 height = 12; 84 } 85 86 92 FontData(LOGFONT data, float height) { 93 this.data = data; 94 this.height = height; 95 } 96 97 116 public FontData(String string) { 117 if (string == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 118 int start = 0; 119 int end = string.indexOf('|'); 120 if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 121 String version1 = string.substring(start, end); 122 try { 123 if (Integer.parseInt(version1) != 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 124 } catch (NumberFormatException e) { 125 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 126 } 127 128 start = end + 1; 129 end = string.indexOf('|', start); 130 if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 131 String name = string.substring(start, end); 132 133 start = end + 1; 134 end = string.indexOf('|', start); 135 if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 136 float height = 0; 137 try { 138 height = Float.parseFloat(string.substring(start, end)); 139 } catch (NumberFormatException e) { 140 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 141 } 142 143 start = end + 1; 144 end = string.indexOf('|', start); 145 if (end == -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 146 int style = 0; 147 try { 148 style = Integer.parseInt(string.substring(start, end)); 149 } catch (NumberFormatException e) { 150 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 151 } 152 153 start = end + 1; 154 end = string.indexOf('|', start); 155 data = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 156 data.lfCharSet = (byte)OS.DEFAULT_CHARSET; 157 setName(name); 158 setHeight(height); 159 setStyle(style); 160 if (end == -1) return; 161 String platform = string.substring(start, end); 162 163 start = end + 1; 164 end = string.indexOf('|', start); 165 if (end == -1) return; 166 String version2 = string.substring(start, end); 167 168 if (platform.equals("WINDOWS") && version2.equals("1")) { LOGFONT newData = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 170 try { 171 start = end + 1; 172 end = string.indexOf('|', start); 173 if (end == -1) return; 174 newData.lfHeight = Integer.parseInt(string.substring(start, end)); 175 start = end + 1; 176 end = string.indexOf('|', start); 177 if (end == -1) return; 178 newData.lfWidth = Integer.parseInt(string.substring(start, end)); 179 start = end + 1; 180 end = string.indexOf('|', start); 181 if (end == -1) return; 182 newData.lfEscapement = Integer.parseInt(string.substring(start, end)); 183 start = end + 1; 184 end = string.indexOf('|', start); 185 if (end == -1) return; 186 newData.lfOrientation = Integer.parseInt(string.substring(start, end)); 187 start = end + 1; 188 end = string.indexOf('|', start); 189 if (end == -1) return; 190 newData.lfWeight = Integer.parseInt(string.substring(start, end)); 191 start = end + 1; 192 end = string.indexOf('|', start); 193 if (end == -1) return; 194 newData.lfItalic = Byte.parseByte(string.substring(start, end)); 195 start = end + 1; 196 end = string.indexOf('|', start); 197 if (end == -1) return; 198 newData.lfUnderline = Byte.parseByte(string.substring(start, end)); 199 start = end + 1; 200 end = string.indexOf('|', start); 201 if (end == -1) return; 202 newData.lfStrikeOut = Byte.parseByte(string.substring(start, end)); 203 start = end + 1; 204 end = string.indexOf('|', start); 205 if (end == -1) return; 206 newData.lfCharSet = Byte.parseByte(string.substring(start, end)); 207 start = end + 1; 208 end = string.indexOf('|', start); 209 if (end == -1) return; 210 newData.lfOutPrecision = Byte.parseByte(string.substring(start, end)); 211 start = end + 1; 212 end = string.indexOf('|', start); 213 if (end == -1) return; 214 newData.lfClipPrecision = Byte.parseByte(string.substring(start, end)); 215 start = end + 1; 216 end = string.indexOf('|', start); 217 if (end == -1) return; 218 newData.lfQuality = Byte.parseByte(string.substring(start, end)); 219 start = end + 1; 220 end = string.indexOf('|', start); 221 if (end == -1) return; 222 newData.lfPitchAndFamily = Byte.parseByte(string.substring(start, end)); 223 start = end + 1; 224 } catch (NumberFormatException e) { 225 setName(name); 226 setHeight(height); 227 setStyle(style); 228 return; 229 } 230 TCHAR buffer = new TCHAR(0, string.substring(start), false); 231 int length = Math.min(OS.LF_FACESIZE - 1, buffer.length()); 232 if (OS.IsUnicode) { 233 char[] lfFaceName = ((LOGFONTW)newData).lfFaceName; 234 System.arraycopy(buffer.chars, 0, lfFaceName, 0, length); 235 } else { 236 byte[] lfFaceName = ((LOGFONTA)newData).lfFaceName; 237 System.arraycopy(buffer.bytes, 0, lfFaceName, 0, length); 238 } 239 data = newData; 240 } 241 } 242 243 257 public FontData(String name, int height, int style) { 258 if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 259 data = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 260 setName(name); 261 setHeight(height); 262 setStyle(style); 263 data.lfCharSet = (byte)OS.DEFAULT_CHARSET; 267 } 268 269 FontData(String name, float height, int style) { 270 if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 271 data = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 272 setName(name); 273 setHeight(height); 274 setStyle(style); 275 data.lfCharSet = (byte)OS.DEFAULT_CHARSET; 279 } 280 281 291 public boolean equals (Object object) { 292 if (object == this) return true; 293 if (!(object instanceof FontData)) return false; 294 FontData fd = (FontData)object; 295 LOGFONT lf = fd.data; 296 return data.lfCharSet == lf.lfCharSet && 297 302 height == fd.height && 304 data.lfWidth == lf.lfWidth && 305 data.lfEscapement == lf.lfEscapement && 306 data.lfOrientation == lf.lfOrientation && 307 data.lfWeight == lf.lfWeight && 308 data.lfItalic == lf.lfItalic && 309 data.lfUnderline == lf.lfUnderline && 310 data.lfStrikeOut == lf.lfStrikeOut && 311 data.lfCharSet == lf.lfCharSet && 312 data.lfOutPrecision == lf.lfOutPrecision && 313 data.lfClipPrecision == lf.lfClipPrecision && 314 data.lfQuality == lf.lfQuality && 315 data.lfPitchAndFamily == lf.lfPitchAndFamily && 316 getName().equals(fd.getName()); 317 } 318 319 int EnumLocalesProc(int lpLocaleString) { 320 321 322 int length = 8; 323 TCHAR buffer = new TCHAR(0, length); 324 int byteCount = length * TCHAR.sizeof; 325 OS.MoveMemory(buffer, lpLocaleString, byteCount); 326 int lcid = Integer.parseInt(buffer.toString(0, buffer.strlen ()), 16); 327 328 329 int size = OS.GetLocaleInfo(lcid, OS.LOCALE_SISO639LANGNAME, buffer, length); 330 if (size <= 0 || !lang.equals(buffer.toString(0, size - 1))) return 1; 331 332 333 if (country != null) { 334 size = OS.GetLocaleInfo(lcid, OS.LOCALE_SISO3166CTRYNAME, buffer, length); 335 if (size <= 0 || !country.equals(buffer.toString(0, size - 1))) return 1; 336 } 337 338 339 size = OS.GetLocaleInfo(lcid, OS.LOCALE_IDEFAULTANSICODEPAGE, buffer, length); 340 if (size <= 0) return 1; 341 int cp = Integer.parseInt(buffer.toString(0, size - 1)); 342 int [] lpCs = new int[8]; 343 OS.TranslateCharsetInfo(cp, lpCs, OS.TCI_SRCCODEPAGE); 344 data.lfCharSet = (byte)lpCs[0]; 345 346 return 0; 347 } 348 349 356 public int getHeight() { 357 return (int)(0.5f + height); 358 } 359 360 float getHeightF() { 361 return height; 362 } 363 364 381 public String getLocale () { 382 StringBuffer buffer = new StringBuffer (); 383 char sep = '_'; 384 if (lang != null) { 385 buffer.append (lang); 386 buffer.append (sep); 387 } 388 if (country != null) { 389 buffer.append (country); 390 buffer.append (sep); 391 } 392 if (variant != null) { 393 buffer.append (variant); 394 } 395 396 String result = buffer.toString (); 397 int length = result.length (); 398 if (length > 0) { 399 if (result.charAt (length - 1) == sep) { 400 result = result.substring (0, length - 1); 401 } 402 } 403 return result; 404 } 405 406 415 public String getName() { 416 char[] chars; 417 if (OS.IsUnicode) { 418 chars = ((LOGFONTW)data).lfFaceName; 419 } else { 420 chars = new char[OS.LF_FACESIZE]; 421 byte[] bytes = ((LOGFONTA)data).lfFaceName; 422 OS.MultiByteToWideChar (OS.CP_ACP, OS.MB_PRECOMPOSED, bytes, bytes.length, chars, chars.length); 423 } 424 int index = 0; 425 while (index < chars.length) { 426 if (chars [index] == 0) break; 427 index++; 428 } 429 return new String (chars, 0, index); 430 } 431 432 441 public int getStyle() { 442 int style = SWT.NORMAL; 443 if (data.lfWeight == 700) style |= SWT.BOLD; 444 if (data.lfItalic != 0) style |= SWT.ITALIC; 445 return style; 446 } 447 448 458 public int hashCode () { 459 return data.lfCharSet ^ getHeight() ^ data.lfWidth ^ data.lfEscapement ^ 460 data.lfOrientation ^ data.lfWeight ^ data.lfItalic ^data.lfUnderline ^ 461 data.lfStrikeOut ^ data.lfCharSet ^ data.lfOutPrecision ^ 462 data.lfClipPrecision ^ data.lfQuality ^ data.lfPitchAndFamily ^ 463 getName().hashCode(); 464 } 465 466 479 public void setHeight(int height) { 480 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 481 this.height = height; 482 } 483 484 void setHeight(float height) { 485 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 486 this.height = height; 487 } 488 489 506 public void setLocale(String locale) { 507 lang = country = variant = null; 508 if (locale != null) { 509 char sep = '_'; 510 int length = locale.length(); 511 int firstSep, secondSep; 512 513 firstSep = locale.indexOf(sep); 514 if (firstSep == -1) { 515 firstSep = secondSep = length; 516 } else { 517 secondSep = locale.indexOf(sep, firstSep + 1); 518 if (secondSep == -1) secondSep = length; 519 } 520 if (firstSep > 0) lang = locale.substring(0, firstSep); 521 if (secondSep > firstSep + 1) country = locale.substring(firstSep + 1, secondSep); 522 if (length > secondSep + 1) variant = locale.substring(secondSep + 1); 523 } 524 if (lang == null) { 525 data.lfCharSet = (byte)OS.DEFAULT_CHARSET; 526 } else { 527 Callback callback = new Callback (this, "EnumLocalesProc", 1); int lpEnumLocalesProc = callback.getAddress (); 529 if (lpEnumLocalesProc == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS); 530 OS.EnumSystemLocales(lpEnumLocalesProc, OS.LCID_SUPPORTED); 531 callback.dispose (); 532 } 533 } 534 535 560 public void setName(String name) { 561 if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 562 563 564 TCHAR buffer = new TCHAR(0, name, true); 565 int length = Math.min(OS.LF_FACESIZE - 1, buffer.length()); 566 if (OS.IsUnicode) { 567 char[] lfFaceName = ((LOGFONTW)data).lfFaceName; 568 for (int i = 0; i < lfFaceName.length; i++) lfFaceName[i] = 0; 569 System.arraycopy(buffer.chars, 0, lfFaceName, 0, length); 570 } else { 571 byte[] lfFaceName = ((LOGFONTA)data).lfFaceName; 572 for (int i = 0; i < lfFaceName.length; i++) lfFaceName[i] = 0; 573 System.arraycopy(buffer.bytes, 0, lfFaceName, 0, length); 574 } 575 } 576 577 587 public void setStyle(int style) { 588 if ((style & SWT.BOLD) == SWT.BOLD) { 589 data.lfWeight = 700; 590 } else { 591 data.lfWeight = 0; 592 } 593 if ((style & SWT.ITALIC) == SWT.ITALIC) { 594 data.lfItalic = 1; 595 } else { 596 data.lfItalic = 0; 597 } 598 } 599 600 609 public String toString() { 610 StringBuffer buffer = new StringBuffer (); 611 buffer.append("1|"); buffer.append(getName()); 613 buffer.append("|"); buffer.append(getHeightF()); 615 buffer.append("|"); buffer.append(getStyle()); 617 buffer.append("|"); buffer.append("WINDOWS|1|"); buffer.append(data.lfHeight); 620 buffer.append("|"); buffer.append(data.lfWidth); 622 buffer.append("|"); buffer.append(data.lfEscapement); 624 buffer.append("|"); buffer.append(data.lfOrientation); 626 buffer.append("|"); buffer.append(data.lfWeight); 628 buffer.append("|"); buffer.append(data.lfItalic); 630 buffer.append("|"); buffer.append(data.lfUnderline); 632 buffer.append("|"); buffer.append(data.lfStrikeOut); 634 buffer.append("|"); buffer.append(data.lfCharSet); 636 buffer.append("|"); buffer.append(data.lfOutPrecision); 638 buffer.append("|"); buffer.append(data.lfClipPrecision); 640 buffer.append("|"); buffer.append(data.lfQuality); 642 buffer.append("|"); buffer.append(data.lfPitchAndFamily); 644 buffer.append("|"); buffer.append(getName()); 646 return buffer.toString(); 647 } 648 649 663 public static FontData win32_new(LOGFONT data, float height) { 664 return new FontData(data, height); 665 } 666 667 } 668 | Popular Tags |