workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / include / vcl / kernarray.hxx
blobe9e5462faf950f7a8f0a28cff5b8845adff1f885
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
9 #pragma once
11 #include <sal/config.h>
13 #include <sal/types.h>
14 #include <cmath>
15 #include <span>
16 #include <vector>
18 class KernArraySpan final
20 private:
21 int m_nSubUnitFactor;
22 std::span<const sal_Int32> m_DXArray;
24 public:
25 KernArraySpan()
26 : m_nSubUnitFactor(1)
30 KernArraySpan(std::span<const sal_Int32> DXArray, int nSubUnitFactor = 1)
31 : m_nSubUnitFactor(nSubUnitFactor)
32 , m_DXArray(DXArray)
35 size_t size() const { return m_DXArray.size(); }
36 bool empty() const { return m_DXArray.empty(); }
37 sal_Int32 operator[](size_t nIndex) const { return get(nIndex); }
38 sal_Int32 get(size_t nIndex) const
40 return std::round(static_cast<double>(m_DXArray[nIndex]) / m_nSubUnitFactor);
43 int get_factor() const { return m_nSubUnitFactor; }
44 sal_Int32 get_subunit(size_t nIndex) const { return m_DXArray[nIndex]; }
47 class KernArray final
49 private:
50 int m_nSubUnitFactor;
51 std::vector<sal_Int32> m_aDXArray;
53 public:
54 KernArray(int nSubUnitFactor = 1)
55 : m_nSubUnitFactor(nSubUnitFactor)
59 sal_Int32 operator[](size_t nIndex) const { return get(nIndex); }
60 sal_Int32 get(size_t nIndex) const
62 return std::round(static_cast<double>(m_aDXArray[nIndex]) / m_nSubUnitFactor);
65 int get_factor() const { return m_nSubUnitFactor; }
66 sal_Int32 get_subunit(size_t nIndex) const { return m_aDXArray[nIndex]; }
68 void set_subunit(size_t nIndex, sal_Int32 nValue) { m_aDXArray[nIndex] = nValue; }
69 std::vector<sal_Int32>& get_subunit_array() { return m_aDXArray; }
71 void adjust(size_t nIndex, sal_Int32 nDiff) { m_aDXArray[nIndex] += nDiff * m_nSubUnitFactor; }
72 void set(size_t nIndex, sal_Int32 nValue) { m_aDXArray[nIndex] = nValue * m_nSubUnitFactor; }
73 void push_back(sal_Int32 nUnit) { m_aDXArray.push_back(nUnit * m_nSubUnitFactor); }
74 sal_Int32 back() const { return m_aDXArray.back() * m_nSubUnitFactor; }
75 size_t size() const { return m_aDXArray.size(); }
76 bool empty() const { return m_aDXArray.empty(); }
77 void clear() { m_aDXArray.clear(); }
78 void assign(KernArraySpan other)
80 m_nSubUnitFactor = other.get_factor();
81 m_aDXArray.clear();
82 size_t nLen = other.size();
83 m_aDXArray.reserve(nLen);
84 for (size_t i = 0; i < nLen; ++i)
85 m_aDXArray.push_back(other.get_subunit(i));
87 void resize(size_t nSize) { m_aDXArray.resize(nSize); }
88 void resize(size_t nSize, sal_Int32 nDefault)
90 m_aDXArray.resize(nSize, nDefault * m_nSubUnitFactor);
92 void reserve(size_t nCapacity) { m_aDXArray.reserve(nCapacity); }
94 bool operator==(const KernArray& rOther) const
96 size_t nSize = size();
97 if (nSize != rOther.size())
98 return false;
99 for (size_t i = 0; i < nSize; ++i)
100 if (m_aDXArray[i] != rOther.m_aDXArray[i])
101 return false;
102 return true;
105 operator KernArraySpan() const { return KernArraySpan(m_aDXArray, m_nSubUnitFactor); }
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */