IDEA-51739
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / editor / impl / ComplementaryFontsRegistry.java
blob44f4e7b33211a9fef70e9beefab9dc147ee29457
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.editor.impl;
18 import gnu.trove.TIntHashSet;
19 import org.jetbrains.annotations.NonNls;
21 import java.awt.*;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
26 /**
27 * @author max
29 public class ComplementaryFontsRegistry {
30 private static final Object lock = new String("common lock");
31 private static final ArrayList<String> ourFontNames;
32 private static final LinkedHashMap<FontKey, FontInfo> ourUsedFonts;
33 private static FontKey ourSharedKeyInstance = new FontKey(null, 0, 0);
34 private static FontInfo ourSharedDefaultFont;
35 private static final TIntHashSet ourUndisplayableChars = new TIntHashSet();
37 private ComplementaryFontsRegistry() {
40 private static class FontKey {
41 public String myFamilyName;
42 public int mySize;
43 public int myStyle;
45 public FontKey(final String familyName, final int size, final int style) {
46 myFamilyName = familyName;
47 mySize = size;
48 myStyle = style;
51 public boolean equals(final Object o) {
52 if (this == o) return true;
53 final FontKey fontKey = (FontKey)o;
55 if (mySize != fontKey.mySize) return false;
56 if (myStyle != fontKey.myStyle) return false;
57 return myFamilyName.equals(fontKey.myFamilyName);
60 public int hashCode() {
61 int result = myFamilyName.hashCode();
62 result = 29 * result + mySize;
63 result = 29 * result + myStyle;
64 return result;
68 @NonNls private static final String BOLD_SUFFIX = ".bold";
70 @NonNls private static final String ITALIC_SUFFIX = ".italic";
72 static {
73 ourFontNames = new ArrayList<String>();
74 GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
75 String[] fontNames = graphicsEnvironment.getAvailableFontFamilyNames();
76 for (final String fontName : fontNames) {
77 if (!fontName.endsWith(BOLD_SUFFIX) && !fontName.endsWith(ITALIC_SUFFIX)) {
78 ourFontNames.add(fontName);
81 ourUsedFonts = new LinkedHashMap<FontKey, FontInfo>();
84 public static FontInfo getFontAbleToDisplay(char c, int size, int style, String defaultFontFamily) {
85 synchronized (lock) {
86 if (ourSharedKeyInstance.mySize == size &&
87 ourSharedKeyInstance.myStyle == style &&
88 ourSharedKeyInstance.myFamilyName != null &&
89 ourSharedKeyInstance.myFamilyName.equals(defaultFontFamily) &&
90 ourSharedDefaultFont != null &&
91 ( c < 128 ||
92 ourSharedDefaultFont.canDisplay(c)
94 ) {
95 return ourSharedDefaultFont;
98 ourSharedKeyInstance.myFamilyName = defaultFontFamily;
99 ourSharedKeyInstance.mySize = size;
100 ourSharedKeyInstance.myStyle = style;
102 FontInfo defaultFont = ourUsedFonts.get(ourSharedKeyInstance);
103 if (defaultFont == null) {
104 defaultFont = new FontInfo(defaultFontFamily, size, style);
105 ourUsedFonts.put(ourSharedKeyInstance, defaultFont);
106 ourSharedKeyInstance = new FontKey(null, 0, 0);
109 ourSharedDefaultFont = defaultFont;
110 if (c < 128 || defaultFont.canDisplay(c)) {
111 return defaultFont;
114 if (ourUndisplayableChars.contains(c)) return defaultFont;
116 final Collection<FontInfo> descriptors = ourUsedFonts.values();
117 for (FontInfo font : descriptors) {
118 if (font.getSize() == size && font.getStyle() == style && font.canDisplay(c)) {
119 return font;
123 for (int i = 0; i < ourFontNames.size(); i++) {
124 String name = ourFontNames.get(i);
125 FontInfo font = new FontInfo(name, size, style);
126 if (font.canDisplay(c)) {
127 ourUsedFonts.put(new FontKey(name, size, style), font);
128 ourFontNames.remove(i);
129 return font;
133 ourUndisplayableChars.add(c);
135 return defaultFont;