Mercurial > projects > dwt-addons
comparison dwtx/draw2d/PrinterGraphics.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
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 dwtx.draw2d.PrinterGraphics; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Collection; | |
17 | |
18 import dwt.graphics.Font; | |
19 import dwt.graphics.FontData; | |
20 import dwt.graphics.Image; | |
21 import dwt.printing.Printer; | |
22 import dwt.widgets.Display; | |
23 import dwtx.draw2d.ScaledGraphics; | |
24 import dwtx.draw2d.Graphics; | |
25 | |
26 /** | |
27 * A scalable graphics object used to print to a printer. | |
28 * @author danlee | |
29 */ | |
30 public class PrinterGraphics : ScaledGraphics { | |
31 | |
32 Map imageCache; | |
33 | |
34 Printer printer; | |
35 | |
36 /** | |
37 * Creates a new PrinterGraphics with Graphics g, using Printer p | |
38 * @param g Graphics object to draw with | |
39 * @param p Printer to print to | |
40 */ | |
41 public this(Graphics g, Printer p) { | |
42 imageCache = new HashMap(); | |
43 super(g); | |
44 printer = p; | |
45 } | |
46 | |
47 Font createFont(FontData data) { | |
48 return new Font(printer, data); | |
49 } | |
50 | |
51 private Image printerImage(Image image) { | |
52 Image result = cast(Image)imageCache.get(image); | |
53 if (result !is null) | |
54 return result; | |
55 | |
56 result = new Image(printer, image.getImageData()); | |
57 imageCache.put(image, result); | |
58 return result; | |
59 } | |
60 | |
61 /** | |
62 * @see dwtx.draw2d.ScaledGraphics#dispose() | |
63 */ | |
64 public void dispose() { | |
65 super.dispose(); | |
66 | |
67 //Dispose printer images | |
68 Iterator iter = imageCache.values().iterator(); | |
69 while (iter.hasNext()) { | |
70 Image printerImage = (cast(Image)iter.next()); | |
71 printerImage.dispose(); | |
72 } | |
73 | |
74 imageCache.clear(); | |
75 } | |
76 | |
77 /** | |
78 * @see dwtx.draw2d.Graphics#drawImage(Image, int, int) | |
79 */ | |
80 public void drawImage(Image srcImage, int x, int y) { | |
81 super.drawImage(printerImage(srcImage), x, y); | |
82 } | |
83 | |
84 /** | |
85 * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int) | |
86 */ | |
87 public void drawImage(Image srcImage, int sx, int sy, int sw, int sh, | |
88 int tx, int ty, int tw, int th) { | |
89 super.drawImage(printerImage(srcImage), sx, sy, sw, sh, tx, ty, tw, th); | |
90 } | |
91 | |
92 int zoomFontHeight(int height) { | |
93 return cast(int) | |
94 (height * zoom * Display.getCurrent().getDPI().y / printer.getDPI().y | |
95 + 0.0000001); | |
96 } | |
97 | |
98 int zoomLineWidth(int w) { | |
99 return cast(int)(w * zoom); | |
100 } | |
101 | |
102 } |