diff dwtx/ui/internal/forms/widgets/FormFonts.d @ 75:5d489b9f966c

Fix continue porting
author Frank Benoit <benoit@tionex.de>
date Sat, 24 May 2008 05:11:16 +0200
parents
children e193036d82c9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/ui/internal/forms/widgets/FormFonts.d	Sat May 24 05:11:16 2008 +0200
@@ -0,0 +1,140 @@
+/*******************************************************************************
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.ui.internal.forms.widgets.FormFonts;
+
+import tango.util.collection.HashMap;
+
+import dwt.DWT;
+import dwt.graphics.Font;
+import dwt.graphics.FontData;
+import dwt.widgets.Display;
+
+import dwt.dwthelper.utils;
+
+public class FormFonts {
+
+    alias HashMap!(String,Object) HashMapStrToObj;
+
+    private static FormFonts instance;
+
+    public static FormFonts getInstance() {
+        if (instance is null)
+            instance = new FormFonts();
+        return instance;
+    }
+
+    private HashMapStrToObj fonts;
+    private HashMapStrToObj ids;
+
+    private this() {
+    }
+
+    private class FontIdentifier {
+        private Display fDisplay;
+        private Font fFont;
+
+        this (Display display, Font font) {
+            fDisplay = display;
+            fFont = font;
+        }
+
+        public bool equals(Object obj) {
+            if (auto id = cast(FontIdentifier)obj ) {
+                return id.fDisplay.opEquals(fDisplay) && id.fFont.opEquals(fFont);
+            }
+            return false;
+        }
+
+        public override hash_t toHash() {
+            return fDisplay.toHash() * 7 + fFont.toHash();
+        }
+    }
+
+    private class FontReference {
+        private Font fFont;
+        private int fCount;
+
+        public this(Font font) {
+            fFont = font;
+            fCount = 1;
+        }
+
+        public Font getFont() {
+            return fFont;
+        }
+        // returns a bool indicating if all clients of this font are finished
+        // a true result indicates the underlying image should be disposed
+        public bool decCount() {
+            return --fCount is 0;
+        }
+        public void incCount() {
+            fCount++;
+        }
+    }
+
+    public Font getBoldFont(Display display, Font font) {
+        checkHashMaps();
+        FontIdentifier fid = new FontIdentifier(display, font);
+        FontReference result = cast(FontReference) fonts.get(fid);
+        if (result !is null && !result.getFont().isDisposed()) {
+            result.incCount();
+            return result.getFont();
+        }
+        Font boldFont = createBoldFont(display, font);
+        fonts.put(fid, new FontReference(boldFont));
+        ids.put(boldFont, fid);
+        return boldFont;
+    }
+
+    public bool markFinished(Font boldFont) {
+        checkHashMaps();
+        FontIdentifier id = cast(FontIdentifier)ids.get(boldFont);
+        if (id !is null) {
+            FontReference ref_ = cast(FontReference) fonts.get(id);
+            if (ref_ !is null) {
+                if (ref_.decCount()) {
+                    fonts.remove(id);
+                    ids.remove(ref_.getFont());
+                    ref_.getFont().dispose();
+                    validateHashMaps();
+                }
+                return true;
+            }
+        }
+        // if the image was not found, dispose of it for the caller
+        boldFont.dispose();
+        return false;
+    }
+
+    private Font createBoldFont(Display display, Font regularFont) {
+        FontData[] fontDatas = regularFont.getFontData();
+        for (int i = 0; i < fontDatas.length; i++) {
+            fontDatas[i].setStyle(fontDatas[i].getStyle() | DWT.BOLD);
+        }
+        return new Font(display, fontDatas);
+    }
+
+    private void checkHashMaps() {
+        if (fonts is null)
+            fonts = new HashMapStrToObj();
+        if (ids is null)
+            ids = new HashMapStrToObj();
+    }
+
+    private void validateHashMaps() {
+        if (fonts.size() is 0)
+            fonts = null;
+        if (ids.size() is 0)
+            ids = null;
+    }
+}