1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CanvasBase.java,v $
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 ************************************************************************/
32 import com
.sun
.star
.rendering
.*;
33 import com
.sun
.star
.geometry
.*;
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
44 public abstract Graphics2D
getGraphics();
46 //----------------------------------------------------------------------------------
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
,
61 RenderState renderState
) throws com
.sun
.star
.lang
.IllegalArgumentException
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
;
77 inverse
= transform
.createInverse();
79 catch( NoninvertibleTransformException e
)
81 // transformation not invertible. Nothing to render then.
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
);
93 graphics
.fill( ellipse
);
95 CanvasUtils
.printLog( "XCanvas: drawPoint called" );
98 public synchronized void drawLine( RealPoint2D aStartPoint
,
99 RealPoint2D aEndPoint
,
101 RenderState renderState
) throws com
.sun
.star
.lang
.IllegalArgumentException
104 Graphics2D graphics
= getGraphics();
106 // initialize the Graphics2D
107 CanvasUtils
.setupGraphicsState( graphics
, viewState
, renderState
, CanvasUtils
.alsoSetupPaint
);
108 graphics
.setStroke( new BasicStroke() );
111 Line2D
.Double line
= new Line2D
.Double(aStartPoint
.X
, aStartPoint
.Y
, aEndPoint
.X
, aEndPoint
.Y
);
114 graphics
.draw( line
);
116 CanvasUtils
.printLog( "XCanvas: drawLine called" );
119 public synchronized void drawBezier( RealBezierSegment2D aBezierSegment
,
120 RealPoint2D aEndPoint
,
122 RenderState renderState
) throws com
.sun
.star
.lang
.IllegalArgumentException
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
);
138 graphics
.draw( curve
);
140 CanvasUtils
.printLog( "XCanvas: drawbezier called" );
143 public synchronized XCachedPrimitive
drawPolyPolygon( XPolyPolygon2D xPolyPolygon
,
145 RenderState renderState
) throws com
.sun
.star
.lang
.IllegalArgumentException
147 CanvasUtils
.printLog( "CanvasBase.drawPolyPolygon() called" );
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" );
165 public synchronized XCachedPrimitive
strokePolyPolygon( XPolyPolygon2D xPolyPolygon
,
167 RenderState renderState
,
168 StrokeAttributes strokeAttributes
) throws com
.sun
.star
.lang
.IllegalArgumentException
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" );
185 public synchronized XCachedPrimitive
strokeTexturedPolyPolygon( XPolyPolygon2D xPolyPolygon
,
187 RenderState renderState
,
189 StrokeAttributes strokeAttributes
) throws com
.sun
.star
.lang
.IllegalArgumentException
, VolatileContentDestroyedException
194 public synchronized XCachedPrimitive
strokeTextureMappedPolyPolygon( XPolyPolygon2D xPolyPolygon
,
196 RenderState renderState
,
198 com
.sun
.star
.geometry
.XMapping2D xMapping
,
199 StrokeAttributes strokeAttributes
) throws com
.sun
.star
.lang
.IllegalArgumentException
, VolatileContentDestroyedException
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
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" );
219 Graphics2D graphics
= getGraphics();
221 // initialize the Graphics2D
222 CanvasUtils
.setupGraphicsState( graphics
, viewState
, renderState
, CanvasUtils
.alsoSetupPaint
);
225 graphics
.fill( CanvasUtils
.makeGeneralPath(xPolyPolygon
) );
227 CanvasUtils
.printLog( "XCanvas: fillPolyPolygon called" );
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
240 public synchronized XCachedPrimitive
fillTextureMappedPolyPolygon( XPolyPolygon2D xPolyPolygon
,
242 RenderState renderState
,
244 com
.sun
.star
.geometry
.XMapping2D xMapping
) throws com
.sun
.star
.lang
.IllegalArgumentException
, VolatileContentDestroyedException
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
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" );
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 );
279 public XCachedPrimitive
drawTextLayout( XTextLayout layoutetText
, ViewState viewState
, RenderState renderState
) throws com
.sun
.star
.lang
.IllegalArgumentException
281 CanvasUtils
.printLog( "CanvasBase.drawOffsettedText() called" );
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
);
299 CanvasUtils
.printLog( "drawTextLayout: mismatching TextLayout object." );
300 throw new com
.sun
.star
.lang
.IllegalArgumentException();
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" );
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
);
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
,
339 public synchronized XGraphicDevice
getDevice()
341 CanvasUtils
.printLog( "CanvasBase.getDevice() called" );
342 return new CanvasGraphicDevice( getGraphics() );