optimization: replace all occurrence is dramatically faster
[fedora-idea.git] / platform-impl / src / com / intellij / openapi / editor / impl / FontInfo.java
blob51e3e61fd6c5b0ca0fb79e85cb3325b8b67955d7
1 package com.intellij.openapi.editor.impl;
3 import gnu.trove.TIntHashSet;
5 import javax.swing.*;
6 import java.awt.*;
8 /**
9 * @author max
11 public class FontInfo {
12 private final String myFamilyName;
13 private final Font myFont;
14 private final int mySize;
15 private final int myStyle;
16 private final TIntHashSet mySafeCharacters = new TIntHashSet();
17 private FontMetrics myFontMetrics = null;
18 private final int[] charWidth = new int[128];
20 public FontInfo(final String familyName, final int size, final int style) {
21 myFamilyName = familyName;
22 mySize = size;
23 myStyle = style;
24 myFont = new Font(familyName, style, size);
27 public boolean canDisplay(char c) {
28 try {
29 if (c < 128) return true;
30 if (mySafeCharacters.contains(c)) return true;
31 if (myFont.canDisplay(c)) {
32 mySafeCharacters.add(c);
33 return true;
35 return false;
37 catch (Exception e) {
38 // JRE has problems working with the font. Just skip.
39 return false;
43 public Font getFont() {
44 return myFont;
47 public int charWidth(char c, JComponent anyComponent) {
48 final FontMetrics metrics = fontMetrics(anyComponent);
49 if (c < 128) return charWidth[c];
50 return metrics.charWidth(c);
53 private FontMetrics fontMetrics(JComponent anyComponent) {
54 if (myFontMetrics == null) {
55 myFontMetrics = anyComponent.getFontMetrics(myFont);
56 for (int i = 0; i < 128; i++) {
57 charWidth[i] = myFontMetrics.charWidth(i);
60 return myFontMetrics;
63 public int getSize() {
64 return mySize;
67 public int getStyle() {
68 return myStyle;