Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / workben / svptest.cxx
blob1ecc03835fd8237bf0f8ca153cbc789416df59ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <sal/main.h>
23 #include <sal/log.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <tools/extendapplicationenvironment.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <vcl/event.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/gradient.hxx>
37 #include <vcl/lineinfo.hxx>
38 #include <vcl/bitmap.hxx>
39 #include <vcl/metric.hxx>
40 #include <vcl/vclptr.hxx>
41 #include <vcl/BitmapWriteAccess.hxx>
43 #include <rtl/ustrbuf.hxx>
45 #include <math.h>
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace cppu;
51 // Forward declaration
52 static void Main();
54 SAL_IMPLEMENT_MAIN()
56 try
58 tools::extendApplicationEnvironment();
60 Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
61 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
63 if( !xServiceManager.is() )
64 Application::Abort( "Failed to bootstrap" );
66 comphelper::setProcessServiceFactory( xServiceManager );
68 InitVCL();
69 ::Main();
70 DeInitVCL();
72 catch (const Exception&)
74 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
75 return 1;
77 catch (const std::exception &e)
79 fprintf(stderr, "fatal error: %s\n", e.what());
80 return 1;
83 return 0;
86 namespace {
88 class MyWin : public WorkWindow
90 Bitmap m_aBitmap;
91 public:
92 MyWin( vcl::Window* pParent, WinBits nWinStyle );
94 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
99 void Main()
101 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_APP | WB_STDWORK );
102 aMainWin->SetText( "VCL - Workbench" );
103 aMainWin->Show();
105 Application::Execute();
108 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
109 WorkWindow( pParent, nWinStyle ),
110 m_aBitmap(Size(256, 256), vcl::PixelFormat::N32_BPP)
112 // prepare an alpha mask
113 BitmapScopedWriteAccess pAcc(m_aBitmap);
114 for( int nX = 0; nX < 256; nX++ )
116 for( int nY = 0; nY < 256; nY++ )
118 double fRed = 255.0-1.5*std::hypot(nX, nY);
119 if( fRed < 0.0 )
120 fRed = 0.0;
121 double fGreen = 255.0-1.5*std::hypot(255-nX, nY);
122 if( fGreen < 0.0 )
123 fGreen = 0.0;
124 double fBlue = 255.0-1.5*std::hypot(128-nX, 255-nY);
125 if( fBlue < 0.0 )
126 fBlue = 0.0;
127 pAcc->SetPixel( nY, nX, BitmapColor( sal_uInt8(fRed), sal_uInt8(fGreen), sal_uInt8(fBlue) ) );
132 static Point project( const Point& rPoint )
134 const double angle_x = M_PI / 6.0;
135 const double angle_z = M_PI / 6.0;
137 // transform planar coordinates to 3d
138 double x = rPoint.X();
139 double y = rPoint.Y();
141 // rotate around X axis
142 double x1 = x;
143 double y1 = y * cos( angle_x );
144 double z1 = y * sin( angle_x );
146 // rotate around Z axis
147 double x2 = x1 * cos( angle_z ) + y1 * sin( angle_z );
148 //double y2 = y1 * cos( angle_z ) - x1 * sin( angle_z );
149 double z2 = z1;
151 return Point( static_cast<sal_Int32>(x2), static_cast<sal_Int32>(z2) );
154 static Color approachColor( const Color& rFrom, const Color& rTo )
156 Color aColor;
157 sal_uInt8 nDiff;
158 // approach red
159 if( rFrom.GetRed() < rTo.GetRed() )
161 nDiff = rTo.GetRed() - rFrom.GetRed();
162 aColor.SetRed( rFrom.GetRed() + std::min<sal_uInt8>( nDiff, 10 ) );
164 else if( rFrom.GetRed() > rTo.GetRed() )
166 nDiff = rFrom.GetRed() - rTo.GetRed();
167 aColor.SetRed( rFrom.GetRed() - std::min<sal_uInt8>( nDiff, 10 ) );
169 else
170 aColor.SetRed( rFrom.GetRed() );
172 // approach Green
173 if( rFrom.GetGreen() < rTo.GetGreen() )
175 nDiff = rTo.GetGreen() - rFrom.GetGreen();
176 aColor.SetGreen( rFrom.GetGreen() + std::min<sal_uInt8>( nDiff, 10 ) );
178 else if( rFrom.GetGreen() > rTo.GetGreen() )
180 nDiff = rFrom.GetGreen() - rTo.GetGreen();
181 aColor.SetGreen( rFrom.GetGreen() - std::min<sal_uInt8>( nDiff, 10 ) );
183 else
184 aColor.SetGreen( rFrom.GetGreen() );
186 // approach blue
187 if( rFrom.GetBlue() < rTo.GetBlue() )
189 nDiff = rTo.GetBlue() - rFrom.GetBlue();
190 aColor.SetBlue( rFrom.GetBlue() + std::min<sal_uInt8>( nDiff, 10 ) );
192 else if( rFrom.GetBlue() > rTo.GetBlue() )
194 nDiff = rFrom.GetBlue() - rTo.GetBlue();
195 aColor.SetBlue( rFrom.GetBlue() - std::min<sal_uInt8>( nDiff, 10 ) );
197 else
198 aColor.SetBlue( rFrom.GetBlue() );
200 return aColor;
203 #define DELTA 5.0
204 void MyWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
206 WorkWindow::Paint(rRenderContext, rRect);
208 rRenderContext.Push();
209 MapMode aMapMode(MapUnit::Map100thMM);
211 rRenderContext.SetMapMode(aMapMode);
213 Size aPaperSize = rRenderContext.GetOutputSize();
214 Point aCenter(aPaperSize.Width() / 2 - 300,
215 (aPaperSize.Height() - 8400) / 2 + 8400);
216 Point aP1(aPaperSize.Width() / 48, 0), aP2(aPaperSize.Width() / 40, 0);
217 Point aPoint;
219 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aPaperSize));
220 rRenderContext.DrawRect(tools::Rectangle(Point(100, 100),
221 Size(aPaperSize.Width() - 200,
222 aPaperSize.Height() - 200)));
223 rRenderContext.DrawRect(tools::Rectangle(Point(200, 200),
224 Size(aPaperSize.Width() - 400,
225 aPaperSize.Height() - 400)));
226 rRenderContext.DrawRect(tools::Rectangle(Point(300, 300),
227 Size(aPaperSize.Width() - 600,
228 aPaperSize.Height() - 600)));
230 const int nFontCount = rRenderContext.GetFontFaceCollectionCount();
231 const int nFontSamples = (nFontCount < 15) ? nFontCount : 15;
232 for (int i = 0; i < nFontSamples; ++i)
235 FontMetric aFont(rRenderContext.GetFontMetricFromCollection((i * nFontCount) / nFontSamples));
236 aFont.SetFontHeight(400 + (i % 7) * 100);
237 aFont.SetOrientation(Degree10(i * (3600 / nFontSamples)));
238 rRenderContext.SetFont(aFont);
240 sal_uInt8 nRed = (i << 6) & 0xC0;
241 sal_uInt8 nGreen = (i << 4) & 0xC0;
242 sal_uInt8 nBlue = (i << 2) & 0xC0;
243 rRenderContext.SetTextColor(Color(nRed, nGreen, nBlue));
245 rRenderContext.DrawText(tools::Rectangle(Point((aPaperSize.Width() - 4000) / 2, 2000),
246 Size(aPaperSize.Width() - 2100, aPaperSize.Height() - 4000)),
247 "SVP test program",
248 DrawTextFlags::MultiLine);
251 rRenderContext.SetFillColor();
252 rRenderContext.DrawRect(tools::Rectangle(Point(aPaperSize.Width() - 4000, 1000),
253 Size(3000, 3000)));
254 rRenderContext.DrawBitmap(Point(aPaperSize.Width() - 4000, 1000),
255 Size( 3000,3000 ),
256 m_aBitmap);
258 Color const aWhite(0xff, 0xff, 0xff);
259 Color const aBlack(0, 0, 0);
260 Color const aLightRed(0xff, 0, 0);
261 Color const aDarkRed(0x40, 0, 0);
262 Color const aLightBlue(0, 0, 0xff);
263 Color const aDarkBlue(0,0,0x40);
264 Color const aLightGreen(0, 0xff, 0);
265 Color const aDarkGreen(0, 0x40, 0);
267 Gradient aGradient(css::awt::GradientStyle_LINEAR, aBlack, aWhite);
268 aGradient.SetAngle(900_deg10);
269 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 4500),
270 Size(aPaperSize.Width() - 2000, 500)),
271 aGradient);
272 aGradient.SetStartColor(aDarkRed);
273 aGradient.SetEndColor(aLightBlue);
274 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 5300),
275 Size(aPaperSize.Width() - 2000, 500)),
276 aGradient);
277 aGradient.SetStartColor(aDarkBlue);
278 aGradient.SetEndColor(aLightGreen);
279 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6100),
280 Size(aPaperSize.Width() - 2000, 500)),
281 aGradient);
282 aGradient.SetStartColor(aDarkGreen);
283 aGradient.SetEndColor(aLightRed);
284 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6900),
285 Size(aPaperSize.Width() - 2000, 500)),
286 aGradient);
288 LineInfo aLineInfo(LineStyle::Solid, 200);
289 const double sind = sin(basegfx::deg2rad(DELTA));
290 const double cosd = cos(basegfx::deg2rad(DELTA));
291 const double factor = 1 + (DELTA / 1000.0);
292 int n = 0;
293 Color aLineColor(0, 0, 0);
294 Color aApproachColor(0, 0, 200);
296 while (aP2.X() < aCenter.X() && n++ < 680)
298 aLineInfo.SetWidth(n / 3);
299 aLineColor = approachColor(aLineColor, aApproachColor);
300 rRenderContext.SetLineColor(aLineColor);
302 // switch approach color
303 if (aApproachColor.IsRGBEqual(aLineColor))
305 if (aApproachColor.GetRed())
306 aApproachColor = Color(0, 0, 200);
307 else if (aApproachColor.GetGreen())
308 aApproachColor = Color(200, 0, 0);
309 else
310 aApproachColor = Color(0, 200, 0);
313 rRenderContext.DrawLine(project(aP1) + aCenter,
314 project(aP2) + aCenter,
315 aLineInfo);
316 aPoint.setX( static_cast<int>((static_cast<double>(aP1.X())*cosd - static_cast<double>(aP1.Y())*sind)*factor) );
317 aPoint.setY( static_cast<int>((static_cast<double>(aP1.Y())*cosd + static_cast<double>(aP1.X())*sind)*factor) );
318 aP1 = aPoint;
319 aPoint.setX( static_cast<int>((static_cast<double>(aP2.X())*cosd - static_cast<double>(aP2.Y())*sind)*factor) );
320 aPoint.setY( static_cast<int>((static_cast<double>(aP2.Y())*cosd + static_cast<double>(aP2.X())*sind)*factor) );
321 aP2 = aPoint;
323 rRenderContext.Pop();
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */