Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0c9f47825fe76f39659d4db5a87036d0a6f7ff46 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2006, 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.swt.examples.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;

/**
 * This tab demonstrates antialiasing for text. Antialiasing is used for
 * smoothing jagged edges in graphics. This tab allows the user to see the
 * effects of different antialiasing values.
 */
public class TextAntialiasTab extends GraphicsTab {

	Combo aliasCombo;
	static int[] aliasValues = { SWT.OFF, SWT.DEFAULT, SWT.ON };

	Button colorButton;
	Menu menu;
	GraphicsBackground textColor;
	String text = GraphicsExample.getResourceString("SWT");


public TextAntialiasTab(GraphicsExample example) {
	super(example);
}

@Override
public String getCategory() {
	return GraphicsExample.getResourceString("Antialiasing"); //$NON-NLS-1$
}

@Override
public String getText() {
	return GraphicsExample.getResourceString("Text"); //$NON-NLS-1$
}

@Override
public String getDescription() {
	return GraphicsExample.getResourceString("AntialiasingTextDesc"); //$NON-NLS-1$
}

@Override
public void dispose() {
	if (menu != null) {
		menu.dispose();
		menu = null;
	}
}

@Override
public void createControlPanel(Composite parent) {

	Composite comp;

	// create drop down combo for antialiasing
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout(2, false));
	new Label(comp, SWT.CENTER).setText(GraphicsExample
			.getResourceString("Antialiasing")); //$NON-NLS-1$
	aliasCombo = new Combo(comp, SWT.DROP_DOWN);
	aliasCombo.add("OFF");
	aliasCombo.add("DEFAULT");
	aliasCombo.add("ON");
	aliasCombo.select(0);
	aliasCombo.addListener(SWT.Selection, event -> example.redraw());

	ColorMenu cm = new ColorMenu();
	cm.setColorItems(true);
	menu = cm.createMenu(parent.getParent(), gb -> {
		textColor = gb;
		colorButton.setImage(gb.getThumbNail());
		example.redraw();
	});

	// create color button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());

	// initialize the color to black
	textColor = (GraphicsBackground)menu.getItem(1).getData();

	colorButton = new Button(comp, SWT.PUSH);
	colorButton.setText(GraphicsExample.getResourceString("Color")); //$NON-NLS-1$
	colorButton.setImage(textColor.getThumbNail());

	colorButton.addListener(SWT.Selection, event -> {
		final Button button = (Button) event.widget;
		final Composite parent1 = button.getParent();
		Rectangle bounds = button.getBounds();
		Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
		menu.setLocation(point.x, point.y + bounds.height);
		menu.setVisible(true);
	});
}

@Override
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

	if (textColor != null && textColor.getBgColor1() != null)
		gc.setForeground(textColor.getBgColor1());

	gc.setTextAntialias(aliasValues[aliasCombo.getSelectionIndex()]);

	// column 1, row 1
	Font font = new Font(device, getPlatformFontFace(0), 100, SWT.NORMAL);
	gc.setFont(font);
	Point size = gc.stringExtent(text);
	gc.drawString(text, width/4 - size.x/2, height/4 - size.y/2, true);
	font.dispose();

	// column 1, row 2
	font = new Font(device, getPlatformFontFace(1), 100, SWT.NORMAL);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, width/4 - size.x/2, 3*height/4 - size.y/2, true);
	font.dispose();

	// column 2, row 1
	font = new Font(device, getPlatformFontFace(2), 50, SWT.NORMAL);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, (width-size.x)/2, 0, true);
	font.dispose();

	// column 2, row 2
	font = new Font(device, getPlatformFontFace(3), 100, SWT.ITALIC);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, (width-size.x)/2, (height-size.y)/2, true);
	font.dispose();

	// column 2, row 3
	font = new Font(device, getPlatformFontFace(4), 50, SWT.NORMAL);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, (width-size.x)/2, height-size.y, true);
	font.dispose();

	// column 3, row 1
	font = new Font(device, getPlatformFontFace(5), 100, SWT.NORMAL);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, 3*width/4 - size.x/2, height/4 - size.y/2, true);
	font.dispose();

	// column 3, row 2
	font = new Font(device, getPlatformFontFace(6), 100, SWT.NORMAL);
	gc.setFont(font);
	size = gc.stringExtent(text);
	gc.drawString(text, 3*width/4 - size.x/2, 3*height/4 - size.y/2, true);
	font.dispose();
}

/**
 * Returns the name of a valid font for the host platform.
 *
 * @param index
 *            index is used to determine the appropriate font face
 */
static String getPlatformFontFace(int index) {
	if(SWT.getPlatform() == "win32") {
		return new String [] {"Bookman Old Style", "Century Gothic", "Comic Sans MS", "Impact", "Garamond", "Lucida Console", "Monotype Corsiva"} [index];
	} else if (SWT.getPlatform() == "gtk") {
		return new String [] {"Luxi Mono", "KacstTitleL", "Baekmuk Batang", "Baekmuk Headline", "KacstFarsi", "Baekmuk Gulim", "URW Chancery L"} [index];
	} else {
		return new String [] {"Courier", "Verdana", "Verdana", "Verdana", "Verdana", "Verdana", "Verdana"} [index];
	}
}
}

Back to the top