Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: 5118f8ba1b20b47ad822bd1b7656afc26b3936ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.resource;

import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

/**
 * Describes a Font using an array of FontData
 *
 * @since 3.1
 */
final class ArrayFontDescriptor extends FontDescriptor {

    private FontData[] data;
    private Font originalFont = null;

    /**
     * Creates a font descriptor for a font with the given name, height,
     * and style. These arguments are passed directly to the constructor
     * of Font.
     *
     * @param data FontData describing the font to create
     *
     * @see org.eclipse.swt.graphics.Font#Font(org.eclipse.swt.graphics.Device, org.eclipse.swt.graphics.FontData)
     * @since 3.1
     */
    public ArrayFontDescriptor(FontData[] data) {
        this.data = data;
    }

    /**
     * Creates a font descriptor that describes the given font.
     *
     * @param originalFont font to be described
     *
     * @see FontDescriptor#createFrom(org.eclipse.swt.graphics.Font)
     * @since 3.1
     */
    public ArrayFontDescriptor(Font originalFont) {
        this(originalFont.getFontData());
        this.originalFont = originalFont;
    }

    @Override
	public FontData[] getFontData() {
    	// Copy the original array to ensure that callers will not modify it
    	return copy(data);
    }


    @Override
	public Font createFont(Device device) {

        // If this descriptor is an existing font, then we can return the original font
        // if this is the same device.
        if (originalFont != null) {
            // If we're allocating on the same device as the original font, return the original.
            if (originalFont.getDevice() == device) {
                return originalFont;
            }
        }

        return new Font(device, data);
    }

    @Override
	public boolean equals(Object obj) {
		if (obj instanceof ArrayFontDescriptor) {
            ArrayFontDescriptor descr = (ArrayFontDescriptor)obj;

            if (descr.originalFont != originalFont) {
                return false;
            }

            if (originalFont != null) {
                return true;
            }

            if (data.length != descr.data.length) {
                return false;
            }

            for (int i = 0; i < data.length; i++) {
                FontData fd = data[i];
                FontData fd2 = descr.data[i];

                if (!fd.equals(fd2)) {
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    @Override
	public int hashCode() {
        if (originalFont != null) {
            return originalFont.hashCode();
        }

        int code = 0;

        for (FontData fd : data) {
            code += fd.hashCode();
        }
        return code;
    }

    @Override
	public void destroyFont(Font previouslyCreatedFont) {
        if (previouslyCreatedFont == originalFont) {
            return;
        }
        previouslyCreatedFont.dispose();
    }

}

Back to the top