75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.ui.internal.forms.widgets.FormFonts;
|
|
14
|
|
15 import tango.util.collection.HashMap;
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.graphics.Font;
|
|
19 import dwt.graphics.FontData;
|
|
20 import dwt.widgets.Display;
|
|
21
|
|
22 import dwt.dwthelper.utils;
|
|
23
|
|
24 public class FormFonts {
|
|
25
|
76
|
26 alias HashMap!(Object,Object) HashMapStrToObj;
|
75
|
27
|
|
28 private static FormFonts instance;
|
|
29
|
|
30 public static FormFonts getInstance() {
|
|
31 if (instance is null)
|
|
32 instance = new FormFonts();
|
|
33 return instance;
|
|
34 }
|
|
35
|
|
36 private HashMapStrToObj fonts;
|
|
37 private HashMapStrToObj ids;
|
|
38
|
|
39 private this() {
|
|
40 }
|
|
41
|
|
42 private class FontIdentifier {
|
|
43 private Display fDisplay;
|
|
44 private Font fFont;
|
|
45
|
|
46 this (Display display, Font font) {
|
|
47 fDisplay = display;
|
|
48 fFont = font;
|
|
49 }
|
|
50
|
|
51 public bool equals(Object obj) {
|
|
52 if (auto id = cast(FontIdentifier)obj ) {
|
|
53 return id.fDisplay.opEquals(fDisplay) && id.fFont.opEquals(fFont);
|
|
54 }
|
|
55 return false;
|
|
56 }
|
|
57
|
|
58 public override hash_t toHash() {
|
|
59 return fDisplay.toHash() * 7 + fFont.toHash();
|
|
60 }
|
|
61 }
|
|
62
|
|
63 private class FontReference {
|
|
64 private Font fFont;
|
|
65 private int fCount;
|
|
66
|
|
67 public this(Font font) {
|
|
68 fFont = font;
|
|
69 fCount = 1;
|
|
70 }
|
|
71
|
|
72 public Font getFont() {
|
|
73 return fFont;
|
|
74 }
|
|
75 // returns a bool indicating if all clients of this font are finished
|
|
76 // a true result indicates the underlying image should be disposed
|
|
77 public bool decCount() {
|
|
78 return --fCount is 0;
|
|
79 }
|
|
80 public void incCount() {
|
|
81 fCount++;
|
|
82 }
|
|
83 }
|
|
84
|
|
85 public Font getBoldFont(Display display, Font font) {
|
|
86 checkHashMaps();
|
|
87 FontIdentifier fid = new FontIdentifier(display, font);
|
85
|
88 FontReference result = fonts.containsKey(fid) ? cast(FontReference) fonts.get(fid) : null;
|
75
|
89 if (result !is null && !result.getFont().isDisposed()) {
|
|
90 result.incCount();
|
|
91 return result.getFont();
|
|
92 }
|
|
93 Font boldFont = createBoldFont(display, font);
|
76
|
94 fonts.add(fid, new FontReference(boldFont));
|
|
95 ids.add(boldFont, fid);
|
75
|
96 return boldFont;
|
|
97 }
|
|
98
|
|
99 public bool markFinished(Font boldFont) {
|
|
100 checkHashMaps();
|
|
101 FontIdentifier id = cast(FontIdentifier)ids.get(boldFont);
|
|
102 if (id !is null) {
|
|
103 FontReference ref_ = cast(FontReference) fonts.get(id);
|
|
104 if (ref_ !is null) {
|
|
105 if (ref_.decCount()) {
|
|
106 fonts.remove(id);
|
|
107 ids.remove(ref_.getFont());
|
|
108 ref_.getFont().dispose();
|
|
109 validateHashMaps();
|
|
110 }
|
|
111 return true;
|
|
112 }
|
|
113 }
|
|
114 // if the image was not found, dispose of it for the caller
|
|
115 boldFont.dispose();
|
|
116 return false;
|
|
117 }
|
|
118
|
|
119 private Font createBoldFont(Display display, Font regularFont) {
|
|
120 FontData[] fontDatas = regularFont.getFontData();
|
|
121 for (int i = 0; i < fontDatas.length; i++) {
|
|
122 fontDatas[i].setStyle(fontDatas[i].getStyle() | DWT.BOLD);
|
|
123 }
|
|
124 return new Font(display, fontDatas);
|
|
125 }
|
|
126
|
|
127 private void checkHashMaps() {
|
|
128 if (fonts is null)
|
|
129 fonts = new HashMapStrToObj();
|
|
130 if (ids is null)
|
|
131 ids = new HashMapStrToObj();
|
|
132 }
|
|
133
|
|
134 private void validateHashMaps() {
|
|
135 if (fonts.size() is 0)
|
|
136 fonts = null;
|
|
137 if (ids.size() is 0)
|
|
138 ids = null;
|
|
139 }
|
|
140 }
|