update dev300-m58
[ooovba.git] / canvas / source / java / CanvasBase.java
blob6d3f6a96414c7b63c4af3080f95545a586fa5483
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CanvasBase.java,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // Canvas
32 import com.sun.star.rendering.*;
33 import com.sun.star.geometry.*;
35 // Java AWT
36 import java.awt.*;
37 import java.awt.geom.*;
39 public abstract class CanvasBase
40 extends com.sun.star.lib.uno.helper.ComponentBase
41 implements com.sun.star.rendering.XCanvas
43 // to be overridden
44 public abstract Graphics2D getGraphics();
46 //----------------------------------------------------------------------------------
49 // XCanvas interface
50 // =================
52 public synchronized void clear()
54 Graphics2D graphics = getGraphics();
55 // TODO(F3): retrieve true dimensions of the Graphics
56 graphics.clearRect(0,0,1000,1000);
59 public synchronized void drawPoint( RealPoint2D aPoint,
60 ViewState viewState,
61 RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
63 // cache
64 Graphics2D graphics = getGraphics();
66 // initialize the Graphics2D
67 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
69 // calculate the domain value for a single device pixel. we're
70 // using delta mapping here, to avoid later subtraction of two
71 // mapped values (as we really only need a transformed size,
72 // not a transformed point).
73 AffineTransform transform = graphics.getTransform();
74 AffineTransform inverse;
75 try
77 inverse = transform.createInverse();
79 catch( NoninvertibleTransformException e )
81 // transformation not invertible. Nothing to render then.
82 return;
85 java.awt.geom.Point2D.Double pointSize = new java.awt.geom.Point2D.Double(1.0,1.0);
86 java.awt.geom.Point2D.Double domainPointSize = new java.awt.geom.Point2D.Double();
87 inverse.deltaTransform( pointSize, domainPointSize );
89 // render a circle one device pixel wide
90 Ellipse2D.Double ellipse = new Ellipse2D.Double(aPoint.X, aPoint.Y, domainPointSize.x, domainPointSize.y);
92 // render, at last
93 graphics.fill( ellipse );
95 CanvasUtils.printLog( "XCanvas: drawPoint called" );
98 public synchronized void drawLine( RealPoint2D aStartPoint,
99 RealPoint2D aEndPoint,
100 ViewState viewState,
101 RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
103 // cache
104 Graphics2D graphics = getGraphics();
106 // initialize the Graphics2D
107 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
108 graphics.setStroke( new BasicStroke() );
110 // setup line object
111 Line2D.Double line = new Line2D.Double(aStartPoint.X, aStartPoint.Y, aEndPoint.X, aEndPoint.Y);
113 // render, at last
114 graphics.draw( line );
116 CanvasUtils.printLog( "XCanvas: drawLine called" );
119 public synchronized void drawBezier( RealBezierSegment2D aBezierSegment,
120 RealPoint2D aEndPoint,
121 ViewState viewState,
122 RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
124 // cache
125 Graphics2D graphics = getGraphics();
127 // initialize the Graphics2D
128 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
129 graphics.setStroke( new BasicStroke() );
131 // setup bezier object
132 CubicCurve2D.Double curve = new CubicCurve2D.Double(aBezierSegment.Px, aBezierSegment.Py,
133 aBezierSegment.C1x, aBezierSegment.C1y,
134 aBezierSegment.C2x, aBezierSegment.C2y,
135 aEndPoint.X, aEndPoint.Y);
137 // render, at last
138 graphics.draw( curve );
140 CanvasUtils.printLog( "XCanvas: drawbezier called" );
143 public synchronized XCachedPrimitive drawPolyPolygon( XPolyPolygon2D xPolyPolygon,
144 ViewState viewState,
145 RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
147 CanvasUtils.printLog( "CanvasBase.drawPolyPolygon() called" );
149 // cache
150 Graphics2D graphics = getGraphics();
152 // initialize the Graphics2D
153 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
154 graphics.setStroke( new BasicStroke() );
156 // render the polygon
157 // TODO: maybe use Graphics.drawPolyline here!
158 graphics.draw( CanvasUtils.makeGeneralPath(xPolyPolygon) );
160 CanvasUtils.printLog( "XCanvas: drawPolyPolygon called" );
162 return null;
165 public synchronized XCachedPrimitive strokePolyPolygon( XPolyPolygon2D xPolyPolygon,
166 ViewState viewState,
167 RenderState renderState,
168 StrokeAttributes strokeAttributes ) throws com.sun.star.lang.IllegalArgumentException
170 // cache
171 Graphics2D graphics = getGraphics();
173 // initialize the Graphics2D
174 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
175 CanvasUtils.applyStrokeAttributes( graphics, strokeAttributes );
177 // stroke the polygon
178 graphics.draw( CanvasUtils.makeGeneralPath(xPolyPolygon) );
180 CanvasUtils.printLog( "XCanvas: strokePolyPolygon called" );
182 return null;
185 public synchronized XCachedPrimitive strokeTexturedPolyPolygon( XPolyPolygon2D xPolyPolygon,
186 ViewState viewState,
187 RenderState renderState,
188 Texture[] textures,
189 StrokeAttributes strokeAttributes ) throws com.sun.star.lang.IllegalArgumentException, VolatileContentDestroyedException
191 return null;
194 public synchronized XCachedPrimitive strokeTextureMappedPolyPolygon( XPolyPolygon2D xPolyPolygon,
195 ViewState viewState,
196 RenderState renderState,
197 Texture[] textures,
198 com.sun.star.geometry.XMapping2D xMapping,
199 StrokeAttributes strokeAttributes ) throws com.sun.star.lang.IllegalArgumentException, VolatileContentDestroyedException
201 return null;
204 public synchronized XPolyPolygon2D queryStrokeShapes( com.sun.star.rendering.XPolyPolygon2D xPolyPolygon,
205 com.sun.star.rendering.ViewState viewState,
206 com.sun.star.rendering.RenderState renderState,
207 com.sun.star.rendering.StrokeAttributes strokeAttributes ) throws com.sun.star.lang.IllegalArgumentException
209 return null;
212 public synchronized XCachedPrimitive fillPolyPolygon( com.sun.star.rendering.XPolyPolygon2D xPolyPolygon,
213 com.sun.star.rendering.ViewState viewState,
214 com.sun.star.rendering.RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
216 CanvasUtils.printLog( "CanvasBase.fillPolyPolygon() called" );
218 // cache
219 Graphics2D graphics = getGraphics();
221 // initialize the Graphics2D
222 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
224 // fill the polygon
225 graphics.fill( CanvasUtils.makeGeneralPath(xPolyPolygon) );
227 CanvasUtils.printLog( "XCanvas: fillPolyPolygon called" );
229 return null;
232 public synchronized XCachedPrimitive fillTexturedPolyPolygon( com.sun.star.rendering.XPolyPolygon2D xPolyPolygon,
233 com.sun.star.rendering.ViewState viewState,
234 com.sun.star.rendering.RenderState renderState,
235 com.sun.star.rendering.Texture [] textures ) throws com.sun.star.lang.IllegalArgumentException
237 return null;
240 public synchronized XCachedPrimitive fillTextureMappedPolyPolygon( XPolyPolygon2D xPolyPolygon,
241 ViewState viewState,
242 RenderState renderState,
243 Texture[] textures,
244 com.sun.star.geometry.XMapping2D xMapping ) throws com.sun.star.lang.IllegalArgumentException, VolatileContentDestroyedException
246 return null;
249 public synchronized XCanvasFont createFont( FontRequest fontRequest, com.sun.star.beans.PropertyValue[] extraFontProperties, com.sun.star.geometry.Matrix2D fontMatrix ) throws com.sun.star.lang.IllegalArgumentException
251 // TODO: support extra arguments
252 return new CanvasFont( fontRequest, this );
255 public FontInfo[] queryAvailableFonts( FontInfo aFilter, com.sun.star.beans.PropertyValue[] aFontProperties ) throws com.sun.star.lang.IllegalArgumentException
257 // TODO
258 return null;
261 public XCachedPrimitive drawText( StringContext text, XCanvasFont xFont, ViewState viewState, RenderState renderState, byte textDirection ) throws com.sun.star.lang.IllegalArgumentException
263 CanvasUtils.printLog( "CanvasBase.drawText() called" );
265 // cache
266 Graphics2D graphics = getGraphics();
268 CanvasUtils.printLog( "XCanvas: drawText called" );
270 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
271 CanvasUtils.setupGraphicsFont( graphics, viewState, renderState, xFont );
273 CanvasUtils.printLog( "XCanvas: drawText rendering \""+ text.Text.substring(text.StartPosition, text.StartPosition+text.Length) + "\"" );
275 graphics.drawString( text.Text.substring(text.StartPosition, text.StartPosition+text.Length), (float)0.0, (float)0.0 );
276 return null;
279 public XCachedPrimitive drawTextLayout( XTextLayout layoutetText, ViewState viewState, RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
281 CanvasUtils.printLog( "CanvasBase.drawOffsettedText() called" );
283 // cache
284 Graphics2D graphics = getGraphics();
286 CanvasUtils.printLog( "XCanvas: drawOffsettedText called" );
288 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
289 CanvasUtils.setupGraphicsFont( graphics, viewState, renderState, layoutetText.getFont() );
291 CanvasUtils.printLog( "XCanvas: drawOffsettedText canvas setup done" );
293 if( layoutetText instanceof TextLayout )
295 ((TextLayout)layoutetText).draw( graphics );
297 else
299 CanvasUtils.printLog( "drawTextLayout: mismatching TextLayout object." );
300 throw new com.sun.star.lang.IllegalArgumentException();
303 return null;
306 public synchronized XCachedPrimitive drawBitmap( com.sun.star.rendering.XBitmap xBitmap,
307 com.sun.star.rendering.ViewState viewState,
308 com.sun.star.rendering.RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
310 CanvasUtils.printLog( "CanvasBase.drawBitmap() called" );
312 // cache
313 Graphics2D graphics = getGraphics();
315 CanvasUtils.setupGraphicsState( graphics, viewState, renderState, CanvasUtils.alsoSetupPaint );
317 java.awt.image.BufferedImage bitmap = CanvasUtils.getBufferedImage( xBitmap );
319 if( !graphics.drawImage(bitmap, 0, 0, null) )
320 CanvasUtils.printLog( "CanvasBase.drawBitmap: image paint incomplete" );
322 CanvasUtils.postRenderImageTreatment( bitmap );
324 return null;
327 public synchronized XCachedPrimitive drawBitmapModulated( com.sun.star.rendering.XBitmap xBitmap,
328 com.sun.star.rendering.ViewState viewState,
329 com.sun.star.rendering.RenderState renderState ) throws com.sun.star.lang.IllegalArgumentException
331 CanvasUtils.printLog( "CanvasBase.drawBitmapModulated() called" );
333 // TODO(F3): Implement channel modulation
334 return drawBitmap(xBitmap,
335 viewState,
336 renderState);
339 public synchronized XGraphicDevice getDevice()
341 CanvasUtils.printLog( "CanvasBase.getDevice() called" );
342 return new CanvasGraphicDevice( getGraphics() );