1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  */
10 
11 #pragma once
12 
13 #include <o3tl/unit_conversion.hxx>
14 #include <sal/types.h>
15 #include <tools/fldunit.hxx>
16 #include <tools/fract.hxx>
17 #include <tools/mapunit.hxx>
18 
FieldToO3tlLength(FieldUnit eU,o3tl::Length ePixelValue=o3tl::Length::px)19 constexpr o3tl::Length FieldToO3tlLength(FieldUnit eU, o3tl::Length ePixelValue = o3tl::Length::px)
20 {
21     switch (eU)
22     {
23         case FieldUnit::MM:
24             return o3tl::Length::mm;
25         case FieldUnit::CM:
26             return o3tl::Length::cm;
27         case FieldUnit::M:
28             return o3tl::Length::m;
29         case FieldUnit::KM:
30             return o3tl::Length::km;
31         case FieldUnit::TWIP:
32             return o3tl::Length::twip;
33         case FieldUnit::POINT:
34             return o3tl::Length::pt;
35         case FieldUnit::PICA:
36             return o3tl::Length::pc;
37         case FieldUnit::INCH:
38             return o3tl::Length::in;
39         case FieldUnit::FOOT:
40             return o3tl::Length::ft;
41         case FieldUnit::MILE:
42             return o3tl::Length::mi;
43         case FieldUnit::CHAR:
44             return o3tl::Length::ch;
45         case FieldUnit::LINE:
46             return o3tl::Length::line;
47         case FieldUnit::MM_100TH:
48             return o3tl::Length::mm100;
49         case FieldUnit::PIXEL:
50             return ePixelValue;
51         default:
52             return o3tl::Length::invalid;
53     }
54 }
55 
MapToO3tlLength(MapUnit eU,o3tl::Length ePixelValue=o3tl::Length::px)56 constexpr o3tl::Length MapToO3tlLength(MapUnit eU, o3tl::Length ePixelValue = o3tl::Length::px)
57 {
58     switch (eU)
59     {
60         case MapUnit::Map100thMM:
61             return o3tl::Length::mm100;
62         case MapUnit::Map10thMM:
63             return o3tl::Length::mm10;
64         case MapUnit::MapMM:
65             return o3tl::Length::mm;
66         case MapUnit::MapCM:
67             return o3tl::Length::cm;
68         case MapUnit::Map1000thInch:
69             return o3tl::Length::in1000;
70         case MapUnit::Map100thInch:
71             return o3tl::Length::in100;
72         case MapUnit::Map10thInch:
73             return o3tl::Length::in10;
74         case MapUnit::MapInch:
75             return o3tl::Length::in;
76         case MapUnit::MapPoint:
77             return o3tl::Length::pt;
78         case MapUnit::MapTwip:
79             return o3tl::Length::twip;
80         case MapUnit::MapPixel:
81             return ePixelValue;
82         default:
83             return o3tl::Length::invalid;
84     }
85 }
86 
conversionFract(o3tl::Length from,o3tl::Length to)87 inline Fraction conversionFract(o3tl::Length from, o3tl::Length to)
88 {
89     const auto & [ mul, div ] = o3tl::getConversionMulDiv(from, to);
90     return { mul, div };
91 }
92 
convertTwipToMm100(N n)93 template <typename N> constexpr auto convertTwipToMm100(N n)
94 {
95     return o3tl::convert(n, o3tl::Length::twip, o3tl::Length::mm100);
96 }
convertMm100ToTwip(N n)97 template <typename N> constexpr auto convertMm100ToTwip(N n)
98 {
99     return o3tl::convert(n, o3tl::Length::mm100, o3tl::Length::twip);
100 }
101 
sanitiseMm100ToTwip(sal_Int64 n)102 constexpr sal_Int64 sanitiseMm100ToTwip(sal_Int64 n)
103 {
104     return o3tl::convertSaturate(n, o3tl::Length::mm100, o3tl::Length::twip);
105 }
106 
convertPointToMm100(N n)107 template <typename N> constexpr auto convertPointToMm100(N n)
108 {
109     return o3tl::convert(n, o3tl::Length::pt, o3tl::Length::mm100);
110 }
convertMm100ToPoint(N n)111 template <typename N> constexpr auto convertMm100ToPoint(N n)
112 {
113     return o3tl::convert(n, o3tl::Length::mm100, o3tl::Length::pt);
114 }
115 
116 // PPT's "master unit" (1/576 inch) <=> mm/100
convertMasterUnitToMm100(N n)117 template <typename N> constexpr auto convertMasterUnitToMm100(N n)
118 {
119     return o3tl::convert(n, o3tl::Length::master, o3tl::Length::mm100);
120 }
convertMm100ToMasterUnit(N n)121 template <typename N> constexpr auto convertMm100ToMasterUnit(N n)
122 {
123     return o3tl::convert(n, o3tl::Length::mm100, o3tl::Length::master);
124 }
125 
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
127