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
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.LinkedHashMap
;
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
;
45 public FontKey(final String familyName
, final int size
, final int style
) {
46 myFamilyName
= familyName
;
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
;
68 @NonNls private static final String BOLD_SUFFIX
= ".bold";
70 @NonNls private static final String ITALIC_SUFFIX
= ".italic";
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
) {
86 if (ourSharedKeyInstance
.mySize
== size
&&
87 ourSharedKeyInstance
.myStyle
== style
&&
88 ourSharedKeyInstance
.myFamilyName
!= null &&
89 ourSharedKeyInstance
.myFamilyName
.equals(defaultFontFamily
) &&
90 ourSharedDefaultFont
!= null &&
92 ourSharedDefaultFont
.canDisplay(c
)
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
)) {
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
)) {
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
);
133 ourUndisplayableChars
.add(c
);