1 /*******************************************************************************
2  * Copyright (c) 2006, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.swt.examples.graphics;
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Device;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Combo;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Menu;
29 
30 /**
31  * This tab demonstrates antialiasing for text. Antialiasing is used for
32  * smoothing jagged edges in graphics. This tab allows the user to see the
33  * effects of different antialiasing values.
34  */
35 public class TextAntialiasTab extends GraphicsTab {
36 
37 	Combo aliasCombo;
38 	static int[] aliasValues = { SWT.OFF, SWT.DEFAULT, SWT.ON };
39 
40 	Button colorButton;
41 	Menu menu;
42 	GraphicsBackground textColor;
43 	String text = GraphicsExample.getResourceString("SWT");
44 
45 
TextAntialiasTab(GraphicsExample example)46 public TextAntialiasTab(GraphicsExample example) {
47 	super(example);
48 }
49 
50 @Override
getCategory()51 public String getCategory() {
52 	return GraphicsExample.getResourceString("Antialiasing"); //$NON-NLS-1$
53 }
54 
55 @Override
getText()56 public String getText() {
57 	return GraphicsExample.getResourceString("Text"); //$NON-NLS-1$
58 }
59 
60 @Override
getDescription()61 public String getDescription() {
62 	return GraphicsExample.getResourceString("AntialiasingTextDesc"); //$NON-NLS-1$
63 }
64 
65 @Override
dispose()66 public void dispose() {
67 	if (menu != null) {
68 		menu.dispose();
69 		menu = null;
70 	}
71 }
72 
73 @Override
createControlPanel(Composite parent)74 public void createControlPanel(Composite parent) {
75 
76 	Composite comp;
77 
78 	// create drop down combo for antialiasing
79 	comp = new Composite(parent, SWT.NONE);
80 	comp.setLayout(new GridLayout(2, false));
81 	new Label(comp, SWT.CENTER).setText(GraphicsExample
82 			.getResourceString("Antialiasing")); //$NON-NLS-1$
83 	aliasCombo = new Combo(comp, SWT.DROP_DOWN);
84 	aliasCombo.add("OFF");
85 	aliasCombo.add("DEFAULT");
86 	aliasCombo.add("ON");
87 	aliasCombo.select(0);
88 	aliasCombo.addListener(SWT.Selection, event -> example.redraw());
89 
90 	ColorMenu cm = new ColorMenu();
91 	cm.setColorItems(true);
92 	menu = cm.createMenu(parent.getParent(), gb -> {
93 		textColor = gb;
94 		colorButton.setImage(gb.getThumbNail());
95 		example.redraw();
96 	});
97 
98 	// create color button
99 	comp = new Composite(parent, SWT.NONE);
100 	comp.setLayout(new GridLayout());
101 
102 	// initialize the color to black
103 	textColor = (GraphicsBackground)menu.getItem(1).getData();
104 
105 	colorButton = new Button(comp, SWT.PUSH);
106 	colorButton.setText(GraphicsExample.getResourceString("Color")); //$NON-NLS-1$
107 	colorButton.setImage(textColor.getThumbNail());
108 
109 	colorButton.addListener(SWT.Selection, event -> {
110 		final Button button = (Button) event.widget;
111 		final Composite parent1 = button.getParent();
112 		Rectangle bounds = button.getBounds();
113 		Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
114 		menu.setLocation(point.x, point.y + bounds.height);
115 		menu.setVisible(true);
116 	});
117 }
118 
119 @Override
paint(GC gc, int width, int height)120 public void paint(GC gc, int width, int height) {
121 	if (!example.checkAdvancedGraphics()) return;
122 	Device device = gc.getDevice();
123 
124 	if (textColor != null && textColor.getBgColor1() != null)
125 		gc.setForeground(textColor.getBgColor1());
126 
127 	gc.setTextAntialias(aliasValues[aliasCombo.getSelectionIndex()]);
128 
129 	// column 1, row 1
130 	Font font = new Font(device, getPlatformFontFace(0), 100, SWT.NORMAL);
131 	gc.setFont(font);
132 	Point size = gc.stringExtent(text);
133 	gc.drawString(text, width/4 - size.x/2, height/4 - size.y/2, true);
134 	font.dispose();
135 
136 	// column 1, row 2
137 	font = new Font(device, getPlatformFontFace(1), 100, SWT.NORMAL);
138 	gc.setFont(font);
139 	size = gc.stringExtent(text);
140 	gc.drawString(text, width/4 - size.x/2, 3*height/4 - size.y/2, true);
141 	font.dispose();
142 
143 	// column 2, row 1
144 	font = new Font(device, getPlatformFontFace(2), 50, SWT.NORMAL);
145 	gc.setFont(font);
146 	size = gc.stringExtent(text);
147 	gc.drawString(text, (width-size.x)/2, 0, true);
148 	font.dispose();
149 
150 	// column 2, row 2
151 	font = new Font(device, getPlatformFontFace(3), 100, SWT.ITALIC);
152 	gc.setFont(font);
153 	size = gc.stringExtent(text);
154 	gc.drawString(text, (width-size.x)/2, (height-size.y)/2, true);
155 	font.dispose();
156 
157 	// column 2, row 3
158 	font = new Font(device, getPlatformFontFace(4), 50, SWT.NORMAL);
159 	gc.setFont(font);
160 	size = gc.stringExtent(text);
161 	gc.drawString(text, (width-size.x)/2, height-size.y, true);
162 	font.dispose();
163 
164 	// column 3, row 1
165 	font = new Font(device, getPlatformFontFace(5), 100, SWT.NORMAL);
166 	gc.setFont(font);
167 	size = gc.stringExtent(text);
168 	gc.drawString(text, 3*width/4 - size.x/2, height/4 - size.y/2, true);
169 	font.dispose();
170 
171 	// column 3, row 2
172 	font = new Font(device, getPlatformFontFace(6), 100, SWT.NORMAL);
173 	gc.setFont(font);
174 	size = gc.stringExtent(text);
175 	gc.drawString(text, 3*width/4 - size.x/2, 3*height/4 - size.y/2, true);
176 	font.dispose();
177 }
178 
179 /**
180  * Returns the name of a valid font for the host platform.
181  *
182  * @param index
183  *            index is used to determine the appropriate font face
184  */
getPlatformFontFace(int index)185 static String getPlatformFontFace(int index) {
186 	if(SWT.getPlatform() == "win32") {
187 		return new String [] {"Bookman Old Style", "Century Gothic", "Comic Sans MS", "Impact", "Garamond", "Lucida Console", "Monotype Corsiva"} [index];
188 	} else if (SWT.getPlatform() == "gtk") {
189 		return new String [] {"Luxi Mono", "KacstTitleL", "Baekmuk Batang", "Baekmuk Headline", "KacstFarsi", "Baekmuk Gulim", "URW Chancery L"} [index];
190 	} else {
191 		return new String [] {"Courier", "Verdana", "Verdana", "Verdana", "Verdana", "Verdana", "Verdana"} [index];
192 	}
193 }
194 }
195