comparison org.eclipse.draw2d/src/org/eclipse/draw2d/PrinterGraphics.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.PrinterGraphics;
14
15 import java.lang.all;
16
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.FontData;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.printing.Printer;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.draw2d.ScaledGraphics;
23 import org.eclipse.draw2d.Graphics;
24
25 /**
26 * A scalable graphics object used to print to a printer.
27 * @author danlee
28 */
29 public class PrinterGraphics : ScaledGraphics {
30
31 Map imageCache;
32
33 Printer printer;
34
35 /**
36 * Creates a new PrinterGraphics with Graphics g, using Printer p
37 * @param g Graphics object to draw with
38 * @param p Printer to print to
39 */
40 public this(Graphics g, Printer p) {
41 imageCache = new HashMap();
42 super(g);
43 printer = p;
44 }
45
46 Font createFont(FontData data) {
47 return new Font(printer, data);
48 }
49
50 private Image printerImage(Image image) {
51 Image result = cast(Image)imageCache.get(image);
52 if (result !is null)
53 return result;
54
55 result = new Image(printer, image.getImageData());
56 imageCache.put(image, result);
57 return result;
58 }
59
60 /**
61 * @see org.eclipse.draw2d.ScaledGraphics#dispose()
62 */
63 public void dispose() {
64 super.dispose();
65
66 //Dispose printer images
67 Iterator iter = imageCache.values().iterator();
68 while (iter.hasNext()) {
69 Image printerImage = (cast(Image)iter.next());
70 printerImage.dispose();
71 }
72
73 imageCache.clear();
74 }
75
76 /**
77 * @see org.eclipse.draw2d.Graphics#drawImage(Image, int, int)
78 */
79 public void drawImage(Image srcImage, int x, int y) {
80 super.drawImage(printerImage(srcImage), x, y);
81 }
82
83 /**
84 * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int)
85 */
86 public void drawImage(Image srcImage, int sx, int sy, int sw, int sh,
87 int tx, int ty, int tw, int th) {
88 super.drawImage(printerImage(srcImage), sx, sy, sw, sh, tx, ty, tw, th);
89 }
90
91 int zoomFontHeight(int height) {
92 return cast(int)
93 (height * zoom * Display.getCurrent().getDPI().y / printer.getDPI().y
94 + 0.0000001);
95 }
96
97 int zoomLineWidth(int w) {
98 return cast(int)(w * zoom);
99 }
100
101 }