1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <ctype.h> // don't ask. msdev breaks otherwise...
28 #include <canvas/debug.hxx>
29 #include <canvas/verbosetrace.hxx>
30 
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/numeric/ftools.hxx>
33 #include "dx_bitmap.hxx"
34 #include "dx_textlayout.hxx"
35 #include "dx_spritecanvas.hxx"
36 #include "dx_textlayout_drawhelper.hxx"
37 
38 
39 using namespace ::com::sun::star;
40 
41 namespace dxcanvas
42 {
43     TextLayout::TextLayout( const rendering::StringContext& 	aText,
44                             sal_Int8                        	nDirection,
45                             sal_Int64                       	/*nRandomSeed*/,
46                             const CanvasFont::ImplRef&      	rFont ) :
47         TextLayout_Base( m_aMutex ),
48         maText( aText ),
49         maLogicalAdvancements(),
50         mpFont( rFont ),
51         mnTextDirection( nDirection )
52     {
53     }
54 
55     TextLayout::~TextLayout()
56     {
57     }
58 
59     void SAL_CALL TextLayout::disposing()
60     {
61         mpFont.reset();
62     }
63 
64     // XTextLayout
65     uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > > SAL_CALL TextLayout::queryTextShapes(  ) throw (uno::RuntimeException)
66     {
67         ::osl::MutexGuard aGuard( m_aMutex );
68 
69         // TODO
70         return uno::Sequence< uno::Reference< rendering::XPolyPolygon2D > >();
71     }
72 
73     uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryInkMeasures(  ) throw (uno::RuntimeException)
74     {
75         ::osl::MutexGuard aGuard( m_aMutex );
76 
77         // TODO
78         return uno::Sequence< geometry::RealRectangle2D >();
79     }
80 
81     uno::Sequence< geometry::RealRectangle2D > SAL_CALL TextLayout::queryMeasures(  ) throw (uno::RuntimeException)
82     {
83         ::osl::MutexGuard aGuard( m_aMutex );
84 
85         // TODO
86         return uno::Sequence< geometry::RealRectangle2D >();
87     }
88 
89     uno::Sequence< double > SAL_CALL TextLayout::queryLogicalAdvancements(  ) throw (uno::RuntimeException)
90     {
91         ::osl::MutexGuard aGuard( m_aMutex );
92 
93         return maLogicalAdvancements;
94     }
95 
96     void SAL_CALL TextLayout::applyLogicalAdvancements( const uno::Sequence< double >& aAdvancements ) throw (lang::IllegalArgumentException, uno::RuntimeException)
97     {
98         ::osl::MutexGuard aGuard( m_aMutex );
99 
100         if( aAdvancements.getLength() != maText.Length )
101         {
102             OSL_TRACE( "TextLayout::applyLogicalAdvancements(): mismatching number of advancements" );
103             throw lang::IllegalArgumentException();
104         }
105 
106         maLogicalAdvancements = aAdvancements;
107     }
108 
109     geometry::RealRectangle2D SAL_CALL TextLayout::queryTextBounds( ) throw (uno::RuntimeException)
110     {
111         ::osl::MutexGuard aGuard( m_aMutex );
112 
113 		uno::Reference< rendering::XGraphicDevice > xGraphicDevice;
114 		::dxcanvas::TextLayoutDrawHelper aDrawHelper(xGraphicDevice);
115 
116 		// render text
117 		const geometry::RealRectangle2D aBounds(
118             aDrawHelper.queryTextBounds(
119                 maText,
120                 maLogicalAdvancements,
121                 mpFont.getRef(),
122                 mpFont->getFontMatrix()));
123 
124         return aBounds;
125     }
126 
127     double SAL_CALL TextLayout::justify( double /*nSize*/ ) throw (lang::IllegalArgumentException, uno::RuntimeException)
128     {
129         ::osl::MutexGuard aGuard( m_aMutex );
130 
131         // TODO
132         return 0.0;
133     }
134 
135     double SAL_CALL TextLayout::combinedJustify( const uno::Sequence< uno::Reference< rendering::XTextLayout > >& /*aNextLayouts*/,
136                                                  double /*nSize*/ ) throw (lang::IllegalArgumentException, uno::RuntimeException)
137     {
138         ::osl::MutexGuard aGuard( m_aMutex );
139 
140         // TODO
141         return 0.0;
142     }
143 
144     rendering::TextHit SAL_CALL TextLayout::getTextHit( const geometry::RealPoint2D& /*aHitPoint*/ ) throw (uno::RuntimeException)
145     {
146         ::osl::MutexGuard aGuard( m_aMutex );
147 
148         // TODO
149         return rendering::TextHit();
150     }
151 
152     rendering::Caret SAL_CALL TextLayout::getCaret( sal_Int32 /*nInsertionIndex*/,
153                                                     sal_Bool /*bExcludeLigatures*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
154     {
155         ::osl::MutexGuard aGuard( m_aMutex );
156 
157         // TODO
158         return rendering::Caret();
159     }
160 
161     sal_Int32 SAL_CALL TextLayout::getNextInsertionIndex( sal_Int32 /*nStartIndex*/,
162                                                           sal_Int32 /*nCaretAdvancement*/,
163                                                           sal_Bool /*bExcludeLigatures*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
164     {
165         ::osl::MutexGuard aGuard( m_aMutex );
166 
167         // TODO
168         return 0;
169     }
170 
171     uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryVisualHighlighting( sal_Int32 /*nStartIndex*/,
172                                                                                               sal_Int32 /*nEndIndex*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
173     {
174         ::osl::MutexGuard aGuard( m_aMutex );
175 
176         // TODO
177         return uno::Reference< rendering::XPolyPolygon2D >();
178     }
179 
180     uno::Reference< rendering::XPolyPolygon2D > SAL_CALL TextLayout::queryLogicalHighlighting( sal_Int32 /*nStartIndex*/,
181                                                                                                sal_Int32 /*nEndIndex*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
182     {
183         ::osl::MutexGuard aGuard( m_aMutex );
184 
185         // TODO
186         return uno::Reference< rendering::XPolyPolygon2D >();
187     }
188 
189     double SAL_CALL TextLayout::getBaselineOffset(  ) throw (uno::RuntimeException)
190     {
191         ::osl::MutexGuard aGuard( m_aMutex );
192 
193         // TODO
194         return 0.0;
195     }
196 
197     sal_Int8 SAL_CALL TextLayout::getMainTextDirection(  ) throw (uno::RuntimeException)
198     {
199         ::osl::MutexGuard aGuard( m_aMutex );
200 
201         return mnTextDirection;
202     }
203 
204     uno::Reference< rendering::XCanvasFont > SAL_CALL TextLayout::getFont(  ) throw (uno::RuntimeException)
205     {
206         ::osl::MutexGuard aGuard( m_aMutex );
207 
208         return mpFont.getRef();
209     }
210 
211     rendering::StringContext SAL_CALL TextLayout::getText(  ) throw (uno::RuntimeException)
212     {
213         ::osl::MutexGuard aGuard( m_aMutex );
214 
215         return maText;
216     }
217 
218     namespace
219     {
220         // TODO(P2): Check whether this gets inlined. If not, make functor
221         // out of it
222         inline Gdiplus::PointF gdiPlusPointFromDx( const double& dx )
223         {
224             return Gdiplus::PointF( static_cast<Gdiplus::REAL>(dx),
225                                     0.0f );
226         }
227     }
228 
229     bool TextLayout::draw( const GraphicsSharedPtr&                           rGraphics,
230 						   const rendering::ViewState& 						  rViewState,
231 						   const rendering::RenderState& 					  rRenderState,
232 						   const ::basegfx::B2ISize& 						  rOutputOffset,
233 						   const uno::Reference< rendering::XGraphicDevice >& xGraphicDevice,
234                            bool                                               bAlphaSurface ) const
235     {
236         ::osl::MutexGuard aGuard( m_aMutex );
237 
238 		::dxcanvas::TextLayoutDrawHelper aDrawHelper(xGraphicDevice);
239 
240 		// render text
241 		aDrawHelper.drawText(
242             rGraphics,
243 			rViewState,
244 			rRenderState,
245 			rOutputOffset,
246 			maText,
247 			maLogicalAdvancements,
248 			mpFont.getRef(),
249 			mpFont->getFontMatrix(),
250             bAlphaSurface);
251 
252 		return true;
253     }
254 
255 
256 #define SERVICE_NAME "com.sun.star.rendering.TextLayout"
257 #define IMPLEMENTATION_NAME "DXCanvas::TextLayout"
258 
259     ::rtl::OUString SAL_CALL TextLayout::getImplementationName() throw( uno::RuntimeException )
260     {
261         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
262     }
263 
264     sal_Bool SAL_CALL TextLayout::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
265     {
266         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
267     }
268 
269     uno::Sequence< ::rtl::OUString > SAL_CALL TextLayout::getSupportedServiceNames()  throw( uno::RuntimeException )
270     {
271         uno::Sequence< ::rtl::OUString > aRet(1);
272         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
273 
274         return aRet;
275     }
276 }
277