001: /*******************************************************************************
002: * Copyright (c) 2007-2008 Kirill Grouchnikov and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *******************************************************************************/package test;
008:
009: import java.awt.*;
010: import java.awt.Color;
011: import java.awt.Font;
012: import java.awt.image.*;
013: import java.io.IOException;
014: import java.util.Map;
015:
016: import javax.swing.*;
017:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.graphics.*;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.widgets.Display;
022:
023: public class Main extends JFrame {
024: private BufferedImage bi;
025:
026: protected class SwtPanel extends JPanel {
027: @Override
028: protected void paintComponent(Graphics g) {
029: g.drawImage(bi, 0, 0, null);
030: }
031: }
032:
033: public Main() throws IOException {
034: super ("SWT vs. Java2D text rendering");
035: this .setLayout(new BorderLayout());
036: SwtPanel panel = new SwtPanel();
037: panel.setOpaque(true);
038: this .add(panel, BorderLayout.CENTER);
039:
040: // Load AWT image
041: BufferedImage awtImage = new BufferedImage(400, 150,
042: BufferedImage.TYPE_INT_ARGB);
043: awtImage.getGraphics().setColor(Color.lightGray);
044: awtImage.getGraphics().fillRect(0, 0, 400, 150);
045: int width = awtImage.getWidth();
046: int height = awtImage.getHeight();
047:
048: this .setSize(width, height);
049: this .setLocationRelativeTo(null);
050: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
051:
052: Display display = Display.getDefault();
053:
054: long time = System.nanoTime();
055: PaletteData PALETTE_DATA = new PaletteData(0xFF0000, 0xFF00,
056: 0xFF);
057: ImageData swtImageData = new ImageData(width, height, 24,
058: PALETTE_DATA);
059: // int step = swtImageData.depth / 8;
060: byte[] data = swtImageData.data;
061: int[] awtPixels = null;
062: awtPixels = awtImage.getRGB(0, 0, width, height, awtPixels, 0,
063: width);
064: for (int i = 0; i < height; i++) {
065: int idx = i * swtImageData.bytesPerLine;
066: for (int j = 0; j < width; j++) {
067: int rgb = awtPixels[j + i * width];
068: for (int k = swtImageData.depth - 8; k >= 0; k -= 8) {
069: data[idx++] = (byte) ((rgb >> k) & 0xFF);
070: }
071: }
072: }
073: System.out.println("AWT to SWT " + (System.nanoTime() - time));
074: time = System.nanoTime();
075:
076: // Create SWT image and render some text in it
077: Image swtImage = new Image(display, swtImageData);
078: GC swtGC = new GC(swtImage);
079: swtGC.setTextAntialias(SWT.DEFAULT);
080: swtGC.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
081:
082: TextLayout swtTextLayout = new TextLayout(display);
083: org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(
084: display, "Segoe UI", 9, SWT.NORMAL);
085: swtTextLayout.setFont(swtFont);
086: swtTextLayout
087: .setText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
088: swtTextLayout.draw(swtGC, 10, 10);
089: swtTextLayout
090: .setText("1234567890!@#$%^&*()-=_+~`[]\\;',./{}|:\"<>?");
091: swtTextLayout.draw(swtGC, 10, 45);
092:
093: System.out.println("SWT text rendering "
094: + (System.nanoTime() - time));
095: time = System.nanoTime();
096:
097: // System.out.println(gc.getFont().getFontData()[0].getName());
098:
099: ImageData imageData = swtImage.getImageData();
100: this .bi = new BufferedImage(width, height,
101: BufferedImage.TYPE_INT_ARGB);
102:
103: int redMask = imageData.palette.redMask;
104: int redShift = imageData.palette.redShift;
105: int greenMask = imageData.palette.greenMask;
106: int greenShift = imageData.palette.greenShift;
107: int blueShift = imageData.palette.blueShift;
108: int blueMask = imageData.palette.blueMask;
109:
110: int[] lineData = new int[width];
111:
112: // Get the raw data buffer and write directly to it.
113: // Compared to BufferedImage.setRGB this speeds up the
114: // image creation by the factor of three.
115: WritableRaster srcRaster = this .bi.getRaster();
116: DataBufferInt dataBuffer = (DataBufferInt) srcRaster
117: .getDataBuffer();
118: int[] rawData = dataBuffer.getData();
119:
120: for (int y = 0; y < height; y++) {
121: imageData.getPixels(0, y, width, lineData, 0);
122: // Analyze each pixel value in the line
123: for (int x = 0; x < lineData.length; x++) {
124: // Extract the red, green and blue component -
125: // see org/eclipse/swt/internal/image/PngEncoder.java
126: int pixelValue = lineData[x];
127: int alpha = imageData.getAlpha(x, y);
128:
129: int r = pixelValue & redMask;
130: r = (redShift < 0) ? r >>> -redShift : r << redShift;
131: int g = pixelValue & greenMask;
132: g = (greenShift < 0) ? g >>> -greenShift
133: : g << greenShift;
134: int b = pixelValue & blueMask;
135: b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
136:
137: rawData[x + y * width] = alpha << 24 | r << 16 | g << 8
138: | b;
139: }
140: }
141: System.out.println("SWT to AWT " + (System.nanoTime() - time));
142: time = System.nanoTime();
143:
144: swtImage.dispose();
145: swtTextLayout.dispose();
146: swtGC.dispose();
147: swtFont.dispose();
148:
149: Graphics2D g2d = (Graphics2D) bi.getGraphics();
150: Toolkit tk = Toolkit.getDefaultToolkit();
151: Map desktopHints = (Map) (tk
152: .getDesktopProperty("awt.font.desktophints"));
153: if (desktopHints != null) {
154: g2d.addRenderingHints(desktopHints);
155: }
156: Font font = new Font("Segoe UI", Font.PLAIN, 12);
157: g2d.setFont(font);
158: g2d.setColor(Color.black);
159: g2d.drawString(
160: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
161: 10, 40);
162: g2d.drawString("1234567890!@#$%^&*()-=_+~`[]\\;',./{}|:\"<>?",
163: 10, 75);
164:
165: }
166:
167: public static void main(String[] args) {
168: SwingUtilities.invokeLater(new Runnable() {
169: public void run() {
170: try {
171: new Main().setVisible(true);
172: } catch (IOException ioe) {
173: ioe.printStackTrace();
174: }
175: }
176: });
177: }
178: }
|