Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/svgio/source/svgreader/SvgNumber.cxx
Line
Count
Source (jump to first uncovered line)
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
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <SvgNumber.hxx>
21
22
#include <o3tl/unit_conversion.hxx>
23
#include <sal/log.hxx>
24
25
namespace svgio::svgreader
26
{
27
28
double SvgNumber::solveNonPercentage(const InfoProvider& rInfoProvider) const
29
0
{
30
0
    if (!isSet())
31
0
    {
32
0
        SAL_WARN("svgio", "SvgNumber not set (!)");
33
0
        return 0.0;
34
0
    }
35
36
0
    switch (meUnit)
37
0
    {
38
        // See https://drafts.csswg.org/css-values-4/#font-relative-length
39
0
        case SvgUnit::em:
40
0
            return mfNumber * rInfoProvider.getCurrentFontSize();
41
0
        case SvgUnit::ex:
42
0
            return mfNumber * rInfoProvider.getCurrentXHeight();
43
0
        case SvgUnit::px:
44
0
            return mfNumber;
45
0
        case SvgUnit::pt:
46
0
            return o3tl::convert(mfNumber, o3tl::Length::pt, o3tl::Length::px);
47
0
        case SvgUnit::pc:
48
0
            return o3tl::convert(mfNumber, o3tl::Length::pc, o3tl::Length::px);
49
0
        case SvgUnit::cm:
50
0
            return o3tl::convert(mfNumber, o3tl::Length::cm, o3tl::Length::px);
51
0
        case SvgUnit::mm:
52
0
            return o3tl::convert(mfNumber, o3tl::Length::mm, o3tl::Length::px);
53
0
        case SvgUnit::in:
54
0
            return o3tl::convert(mfNumber, o3tl::Length::in, o3tl::Length::px);
55
0
        case SvgUnit::none:
56
0
        {
57
0
            SAL_WARN("svgio", "Design error, this case should have been handled in the caller");
58
0
            return mfNumber;
59
0
        }
60
0
        case SvgUnit::percent:
61
0
        {
62
0
            SAL_WARN("svgio", "Do not use with percentage!");
63
0
            break;
64
0
        }
65
0
    }
66
67
0
    return 0.0;
68
0
}
69
70
double SvgNumber::solve(const InfoProvider& rInfoProvider, NumberType aNumberType) const
71
0
{
72
0
    if (!isSet())
73
0
    {
74
0
        SAL_WARN("svgio", "SvgNumber not set (!)");
75
0
        return 0.0;
76
0
    }
77
78
0
    if (meUnit == SvgUnit::percent)
79
0
    {
80
0
        double fRetval(mfNumber * 0.01);
81
0
        basegfx::B2DRange aViewPort = rInfoProvider.getCurrentViewPort();
82
83
0
        if ( aViewPort.isEmpty() )
84
0
        {
85
0
            SAL_WARN("svgio", "Design error, this case should have been handled in the caller");
86
            // no viewPort, assume a normal page size (A4)
87
0
            aViewPort = basegfx::B2DRange(
88
0
                0.0,
89
0
                0.0,
90
0
                o3tl::convert(210.0, o3tl::Length::cm, o3tl::Length::px), // should it be mm?
91
0
                o3tl::convert(297.0, o3tl::Length::cm, o3tl::Length::px));
92
93
0
        }
94
95
0
        if ( !aViewPort.isEmpty() )
96
0
        {
97
0
            if (NumberType::xcoordinate == aNumberType)
98
0
            {
99
                // it's a x-coordinate, relative to current width (w)
100
0
                fRetval *= aViewPort.getWidth();
101
0
            }
102
0
            else if (NumberType::ycoordinate == aNumberType)
103
0
            {
104
                // it's a y-coordinate, relative to current height (h)
105
0
                fRetval *= aViewPort.getHeight();
106
0
            }
107
0
            else // length
108
0
            {
109
                // it's a length, relative to sqrt((w^2 + h^2)/2)
110
0
                const double fDiagLength(std::hypot(aViewPort.getWidth(), aViewPort.getHeight()));
111
0
                fRetval *= fDiagLength / M_SQRT2;
112
0
            }
113
0
        }
114
115
0
        return fRetval;
116
0
    }
117
118
0
    return solveNonPercentage( rInfoProvider);
119
0
}
120
121
} // end of namespace svgio::svgreader
122
123
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */