Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5853d4e14a7f9bed01f327441270d4c3c528fea6 (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
/*******************************************************************************
 * Copyright (c) 2015, 2016 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
 *******************************************************************************/
package org.eclipse.jface.text.contentassist;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.TextStyle;

import org.eclipse.jface.viewers.StyledString.Styler;


/**
 * Provides {@link Styler} that applies bold style on the given font.
 *
 * @since 3.11
 */
public final class BoldStylerProvider {

	private Font fFont;

	private Font fBoldFont;

	private Styler fBoldStyler;

	/**
	 * Creates a {@link BoldStylerProvider} instance which uses the given <code>font</code>.
	 *
	 * @param font the font to use for creating the bold font
	 */
	public BoldStylerProvider(Font font) {
		fFont= font;
	}

	/**
	 * Disposes the bold font created for the styler.
	 */
	public void dispose() {
		if (fBoldFont != null) {
			fBoldFont.dispose();
			fBoldFont= null;
		}
	}

	/**
	 * Returns a {@link Styler} that applies bold style to the given {@link TextStyle}'s font.
	 *
	 * @return a bold styler
	 */
	public Styler getBoldStyler() {
		if (fBoldStyler == null) {
			fBoldStyler= new Styler() {
				@Override
				public void applyStyles(TextStyle textStyle) {
					textStyle.font= getBoldFont();
				}
			};
		}
		return fBoldStyler;
	}

	/**
	 * Creates (if not already done) and returns the bold font used by the styler to apply the bold
	 * style.
	 *
	 * <p>
	 * <strong>Note:</strong> The callers must not cache and re-use the returned font outside the
	 * current call.
	 * </p>
	 *
	 * @return the bold font used by the styler
	 */
	public Font getBoldFont() {
		if (fBoldFont == null) {
			FontData[] data= fFont.getFontData();
			for (int i= 0; i < data.length; i++) {
				data[i].setStyle(SWT.BOLD);
			}
			fBoldFont= new Font(fFont.getDevice(), data);
		}
		return fBoldFont;
	}

	/**
	 * Returns the font used by the styler to create the bold font.
	 *
	 * @return the font used for creating the bold font
	 */
	public Font getFont() {
		return fFont;
	}

}

Back to the top