merge the formfield patch from ooo-build
[ooovba.git] / filter / source / svg / units.cxx
blob200bc1df5ed07541df792a4b35080fd2617cf288
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * Author:
6 * Jan Holesovsky <kendy@suse.cz>
7 * Fridrich Strba <fridrich.strba@bluewin.ch>
8 * Thorsten Behrens <tbehrens@novell.com>
10 * Copyright (C) 2008, Novell Inc.
12 * The Contents of this file are made available subject to
13 * the terms of GNU Lesser General Public License Version 2.1.
15 ************************************************************************/
17 // MARKER(update_precomp.py): autogen include statement, do not remove
18 #include "precompiled_filter.hxx"
20 #include "units.hxx"
21 #include "gfxtypes.hxx"
22 #include "spirit_supplements.hxx"
24 #include <string.h>
25 #include <rtl/ustring.hxx>
27 #include <boost/bind.hpp>
28 // workaround. spirit uses INT_MAX.
29 #include <limits.h>
30 #include <boost/spirit.hpp>
33 namespace svgi
36 double convLength( double value, SvgUnit unit, const State& rState, char dir )
38 // convert svg unit to internal coordinates ("pixel"). Since the
39 // OOo drawing layer is still largely integer-based, the initial
40 // viewport transformation includes a certain scale factor
41 double fRet(value);
42 switch ( unit )
44 case SVG_LENGTH_UNIT_CM: fRet *= 72.0/2.54; break;
45 case SVG_LENGTH_UNIT_IN: fRet *= 72.0; break;
46 case SVG_LENGTH_UNIT_MM: fRet *= 72.0/25.4; break;
47 case SVG_LENGTH_UNIT_PC: fRet *= 72.0/6.0; break;
48 case SVG_LENGTH_UNIT_USER:
49 case SVG_LENGTH_UNIT_PX: // no unit defaults to PX in svg,
50 // assume display to have 72DPI
51 case SVG_LENGTH_UNIT_PT: break;
52 case SVG_LENGTH_UNIT_EM: fRet *= rState.mnFontSize; break;
53 case SVG_LENGTH_UNIT_EX: fRet *= rState.mnFontSize / 2.0; break;
54 case SVG_LENGTH_UNIT_PERCENTAGE:
56 double fBoxLen;
57 if (rState.maViewBox.isEmpty())
59 basegfx::B2DRange aDefaultBox(0, 0,
60 convLength(210, SVG_LENGTH_UNIT_MM, rState, 'h'),
61 convLength(297, SVG_LENGTH_UNIT_MM, rState, 'v'));
62 fBoxLen = (dir=='h' ? aDefaultBox.getWidth() :
63 (dir=='v' ? aDefaultBox.getHeight() :
64 aDefaultBox.getRange().getLength()));
66 else
68 fBoxLen = (dir=='h' ? rState.maViewBox.getWidth() :
69 (dir=='v' ? rState.maViewBox.getHeight() :
70 rState.maViewBox.getRange().getLength()));
73 fRet *= fBoxLen/100.0;
75 break;
76 default: OSL_TRACE( "Unknown length type" ); break;
79 return fRet;
82 double convLength( const rtl::OUString& sValue, const State& rState, char dir )
84 using namespace ::boost::spirit;
86 rtl::OString aUTF8 = rtl::OUStringToOString( sValue,
87 RTL_TEXTENCODING_UTF8 );
89 double nVal=0.0;
90 SvgUnit eUnit=SVG_LENGTH_UNIT_PX;
91 const bool bRes = parse(aUTF8.getStr(),
92 // Begin grammar
94 real_p[assign_a(nVal)]
95 >> ( str_p("cm") [assign_a(eUnit,SVG_LENGTH_UNIT_CM)]
96 | str_p("em") [assign_a(eUnit,SVG_LENGTH_UNIT_EM)]
97 | str_p("ex") [assign_a(eUnit,SVG_LENGTH_UNIT_EX)]
98 | str_p("in") [assign_a(eUnit,SVG_LENGTH_UNIT_IN)]
99 | str_p("mm") [assign_a(eUnit,SVG_LENGTH_UNIT_MM)]
100 | str_p("pc") [assign_a(eUnit,SVG_LENGTH_UNIT_PC)]
101 | str_p("pt") [assign_a(eUnit,SVG_LENGTH_UNIT_PT)]
102 | str_p("px") [assign_a(eUnit,SVG_LENGTH_UNIT_PX)]
103 | str_p("%") [assign_a(eUnit,SVG_LENGTH_UNIT_PERCENTAGE)]
104 | str_p("") [assign_a(eUnit,SVG_LENGTH_UNIT_USER)]
105 | end_p)
107 // End grammar
108 space_p).full;
110 if( !bRes )
111 return 0.0;
113 return convLength(nVal,eUnit,rState,dir);
116 } // namespace svgi