Skip to content

Package: ExtendedFontRegistry$1

ExtendedFontRegistry$1

nameinstructionbranchcomplexitylinemethod
getFont(Font, Object)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2008-2010 IBM Corporation and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * IBM - Initial API and implementation
13: */
14: // REUSED CLASS
15: package org.eclipse.emf.edit.ui.provider;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.HashMap;
20:
21: import org.eclipse.emf.common.util.URI;
22: import org.eclipse.emf.edit.provider.IItemFontProvider;
23: import org.eclipse.jface.resource.DeviceResourceException;
24: import org.eclipse.jface.resource.FontDescriptor;
25: import org.eclipse.swt.SWT;
26: import org.eclipse.swt.graphics.Font;
27: import org.eclipse.swt.graphics.FontData;
28: import org.eclipse.swt.widgets.Display;
29:
30: /**
31: * A font registry for turning a font description into an actual font object.
32: *
33: * @see IItemFontProvider
34: */
35: public class ExtendedFontRegistry {
36:
37:         public static final ExtendedFontRegistry INSTANCE = new ExtendedFontRegistry() {
38:                 @Override
39:                 public Font getFont(Font baseFont, Object object) {
40:                         return ExtendedImageRegistry.getInstance(ExtendedFontRegistry.class).getFont(baseFont, object);
41:                 }
42:         };
43:
44:         protected Display display;
45:         protected HashMap<Collection<?>, Font> table = new HashMap<Collection<?>, Font>(10);
46:
47:         public ExtendedFontRegistry() {
48:                 display = Display.getCurrent();
49:                 hookDisplayDispose(display);
50:         }
51:
52:         public ExtendedFontRegistry(Display display) {
53:                 this.display = display;
54:                 hookDisplayDispose(display);
55:         }
56:
57:         public Font getFont(Font baseFont, Object object) {
58:                 if (object instanceof Font) {
59:                         return (Font) object;
60:                 } else {
61:                         final Collection<Object> key = new ArrayList<Object>(2);
62:                         key.add(baseFont);
63:                         key.add(object);
64:
65:                         Font result = table.get(key);
66:                         if (result == null) {
67:                                 if (object instanceof FontDescriptor) {
68:                                         final FontDescriptor fontDescriptor = (FontDescriptor) object;
69:                                         try {
70:                                                 result = fontDescriptor.createFont(display);
71:                                         } catch (final DeviceResourceException exception) {
72:                                                 Activator.log(exception);
73:                                         }
74:                                 } else if (object instanceof URI) {
75:                                         final URI fontURI = (URI) object;
76:                                         if (!"font".equals(fontURI.scheme())) {
77:                                                 throw new IllegalArgumentException("Only 'font' scheme is recognized" + fontURI);
78:                                         }
79:                                         String fontNameSpecification = fontURI.authority();
80:                                         if ("".equals(fontNameSpecification)) {
81:                                                 fontNameSpecification = null;
82:                                         }
83:                                         final String heightSpecification = fontURI.segment(0);
84:                                         boolean delta;
85:                                         int height;
86:                                         if (heightSpecification.startsWith("+")) {
87:                                                 delta = true;
88:                                                 height = Integer.parseInt(heightSpecification.substring(1));
89:                                         } else if ("".equals(heightSpecification)) {
90:                                                 delta = true;
91:                                                 height = 0;
92:                                         } else {
93:                                                 height = Integer.parseInt(heightSpecification);
94:                                                 delta = height < 0;
95:                                         }
96:
97:                                         final String styleSpecification = fontURI.segment(1);
98:                                         final int style = "bold".equals(styleSpecification) ? SWT.BOLD
99:                                                 : "italic".equals(styleSpecification) ? SWT.ITALIC
100:                                                         : "italic+bold".equals(styleSpecification) || "bold+italic".equals(styleSpecification)
101:                                                                 ? SWT.ITALIC | SWT.BOLD
102:                                                                 : "normal".equals(styleSpecification) ? SWT.NORMAL : -1;
103:
104:                                         final FontData[] baseFontData = baseFont.getFontData();
105:                                         final FontData[] fontData = new FontData[baseFontData.length];
106:
107:                                         for (int i = 0; i < baseFontData.length; ++i) {
108:                                                 fontData[i] = new FontData(
109:                                                         fontNameSpecification == null ? baseFontData[i].getName() : fontNameSpecification,
110:                                                         delta ? baseFontData[i].getHeight() + height : height,
111:                                                         style == -1 ? baseFontData[i].getStyle() : style);
112:                                         }
113:
114:                                         try {
115:                                                 result = FontDescriptor.createFrom(fontData).createFont(display);
116:                                         } catch (final DeviceResourceException exception) {
117:                                                 Activator.log(exception);
118:                                         }
119:                                 }
120:
121:                                 if (result != null) {
122:                                         table.put(key, result);
123:                                 }
124:                         }
125:                         return result;
126:                 }
127:         }
128:
129:         protected void handleDisplayDispose() {
130:                 for (final Font image : table.values()) {
131:                         image.dispose();
132:                 }
133:                 table = null;
134:         }
135:
136:         protected void hookDisplayDispose(Display display) {
137:                 display.disposeExec(new Runnable() {
138:                         @Override
139:                         public void run() {
140:                                 handleDisplayDispose();
141:                         }
142:                 });
143:         }
144: }