1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <basegfx/range/b2drange.hxx>
12 #include "gfxtypes.hxx"
13 #include <rtl/ustring.hxx>
14 #include <osl/diagnose.h>
15 #include <boost/spirit/include/classic.hpp>
20 double convLength( double value
, SvgUnit unit
, const State
& rState
, char dir
)
22 // convert svg unit to internal coordinates ("pixel"). Since the
23 // OOo drawing layer is still largely integer-based, the initial
24 // viewport transformation includes a certain scale factor
28 case SVG_LENGTH_UNIT_CM
: fRet
*= 72.0/2.54; break;
29 case SVG_LENGTH_UNIT_IN
: fRet
*= 72.0; break;
30 case SVG_LENGTH_UNIT_MM
: fRet
*= 72.0/25.4; break;
31 case SVG_LENGTH_UNIT_PC
: fRet
*= 72.0/6.0; break;
32 case SVG_LENGTH_UNIT_USER
:
33 case SVG_LENGTH_UNIT_PX
: // no unit defaults to PX in svg,
34 // assume display to have 72DPI
35 case SVG_LENGTH_UNIT_PT
: break;
36 case SVG_LENGTH_UNIT_EM
: fRet
*= rState
.mnFontSize
; break;
37 case SVG_LENGTH_UNIT_EX
: fRet
*= rState
.mnFontSize
/ 2.0; break;
38 case SVG_LENGTH_UNIT_PERCENTAGE
:
41 if (rState
.maViewBox
.isEmpty())
43 basegfx::B2DRange
aDefaultBox(0, 0,
44 convLength(210, SVG_LENGTH_UNIT_MM
, rState
, 'h'),
45 convLength(297, SVG_LENGTH_UNIT_MM
, rState
, 'v'));
46 fBoxLen
= (dir
=='h' ? aDefaultBox
.getWidth() :
47 (dir
=='v' ? aDefaultBox
.getHeight() :
48 aDefaultBox
.getRange().getLength()));
52 fBoxLen
= (dir
=='h' ? rState
.maViewBox
.getWidth() :
53 (dir
=='v' ? rState
.maViewBox
.getHeight() :
54 rState
.maViewBox
.getRange().getLength()));
57 fRet
*= fBoxLen
/100.0;
60 default: OSL_TRACE( "Unknown length type" ); break;
66 double convLength( const OUString
& sValue
, const State
& rState
, char dir
)
68 //FIXME: convert deprecated spirit::classic to use spirit::qi
69 using namespace ::boost::spirit::classic
;
71 OString aUTF8
= OUStringToOString( sValue
,
72 RTL_TEXTENCODING_UTF8
);
75 SvgUnit eUnit
=SVG_LENGTH_UNIT_PX
;
76 const bool bRes
= parse(aUTF8
.getStr(),
79 real_p
[assign_a(nVal
)]
80 >> ( str_p("cm") [assign_a(eUnit
,SVG_LENGTH_UNIT_CM
)]
81 | str_p("em") [assign_a(eUnit
,SVG_LENGTH_UNIT_EM
)]
82 | str_p("ex") [assign_a(eUnit
,SVG_LENGTH_UNIT_EX
)]
83 | str_p("in") [assign_a(eUnit
,SVG_LENGTH_UNIT_IN
)]
84 | str_p("mm") [assign_a(eUnit
,SVG_LENGTH_UNIT_MM
)]
85 | str_p("pc") [assign_a(eUnit
,SVG_LENGTH_UNIT_PC
)]
86 | str_p("pt") [assign_a(eUnit
,SVG_LENGTH_UNIT_PT
)]
87 | str_p("px") [assign_a(eUnit
,SVG_LENGTH_UNIT_PX
)]
88 | str_p("%") [assign_a(eUnit
,SVG_LENGTH_UNIT_PERCENTAGE
)]
89 | str_p("") [assign_a(eUnit
,SVG_LENGTH_UNIT_USER
)]
98 return convLength(nVal
,eUnit
,rState
,dir
);
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */