/src/libreoffice/vcl/inc/justificationdata.hxx
Line | Count | Source |
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. |
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 | | #pragma once |
10 | | |
11 | | #include <sal/types.h> |
12 | | #include <o3tl/typed_flags_set.hxx> |
13 | | |
14 | | #include <optional> |
15 | | #include <vector> |
16 | | |
17 | | enum class ClusterJustificationFlags |
18 | | { |
19 | | NONE = 0x0000, |
20 | | KashidaPosition = 0x0001, |
21 | | }; |
22 | | |
23 | | namespace o3tl |
24 | | { |
25 | | template <> |
26 | | struct typed_flags<ClusterJustificationFlags> : is_typed_flags<ClusterJustificationFlags, 0x0001> |
27 | | { |
28 | | }; |
29 | | } |
30 | | |
31 | | class ClusterJustification |
32 | | { |
33 | | public: |
34 | | double m_nTotalAdvance = 0.0; |
35 | | ClusterJustificationFlags m_nFlags = ClusterJustificationFlags::NONE; |
36 | | }; |
37 | | |
38 | | class JustificationData |
39 | | { |
40 | | private: |
41 | | std::vector<ClusterJustification> m_aClusters; |
42 | | sal_Int32 m_nBaseIndex = 0; |
43 | | sal_Int32 m_nEndIndex = 0; |
44 | | bool m_bContainsAdvances = false; |
45 | | bool m_bContainsKashidaPositions = false; |
46 | | |
47 | | [[nodiscard]] inline std::optional<size_t> GetIndex(sal_Int32 nClusterId) const |
48 | 11.7M | { |
49 | 11.7M | auto nIndex = nClusterId - m_nBaseIndex; |
50 | 11.7M | if (nIndex >= 0 && nIndex < static_cast<sal_Int32>(m_aClusters.size())) |
51 | 11.7M | { |
52 | 11.7M | return static_cast<size_t>(nIndex); |
53 | 11.7M | } |
54 | | |
55 | 0 | return std::nullopt; |
56 | 11.7M | } |
57 | | |
58 | | public: |
59 | 23.0M | JustificationData() = default; |
60 | | JustificationData(sal_Int32 nBaseIndex, sal_Int32 nSize) |
61 | 330k | : m_nBaseIndex(nBaseIndex) |
62 | 330k | , m_nEndIndex(nBaseIndex + nSize) |
63 | 330k | { |
64 | 330k | m_aClusters.resize(nSize); |
65 | 330k | } |
66 | | |
67 | 18.7M | [[nodiscard]] bool empty() const { return m_aClusters.empty(); } |
68 | | |
69 | 0 | [[nodiscard]] bool ContainsAdvances() const { return m_bContainsAdvances; } |
70 | 0 | [[nodiscard]] bool ContainsKashidaPositions() const { return m_bContainsKashidaPositions; } |
71 | | |
72 | | [[nodiscard]] double GetTotalAdvance(sal_Int32 nClusterId) const |
73 | 34.9M | { |
74 | 34.9M | if (nClusterId < m_nBaseIndex || m_aClusters.empty()) |
75 | 139k | { |
76 | 139k | return 0.0; |
77 | 139k | } |
78 | | |
79 | 34.8M | if (nClusterId < m_nEndIndex) |
80 | 21.3M | { |
81 | 21.3M | return m_aClusters.at(nClusterId - m_nBaseIndex).m_nTotalAdvance; |
82 | 21.3M | } |
83 | | |
84 | 13.5M | return m_aClusters.back().m_nTotalAdvance; |
85 | 34.8M | } |
86 | | |
87 | | [[nodiscard]] std::optional<bool> GetPositionHasKashida(sal_Int32 nClusterId) const |
88 | 1.11M | { |
89 | 1.11M | if (auto nIndex = GetIndex(nClusterId); nIndex.has_value()) |
90 | 1.11M | { |
91 | 1.11M | return std::optional<bool>{ m_aClusters.at(*nIndex).m_nFlags |
92 | 1.11M | & ClusterJustificationFlags::KashidaPosition }; |
93 | 1.11M | } |
94 | | |
95 | 0 | return std::nullopt; |
96 | 1.11M | } |
97 | | |
98 | | void SetTotalAdvance(sal_Int32 nClusterId, double nValue) |
99 | 10.5M | { |
100 | 10.5M | if (auto nIndex = GetIndex(nClusterId); nIndex.has_value()) |
101 | 10.5M | { |
102 | 10.5M | m_aClusters.at(*nIndex).m_nTotalAdvance = nValue; |
103 | 10.5M | m_bContainsAdvances = true; |
104 | 10.5M | } |
105 | 10.5M | } |
106 | | |
107 | | void SetKashidaPosition(sal_Int32 nClusterId, bool bValue) |
108 | 40.5k | { |
109 | 40.5k | if (auto nIndex = GetIndex(nClusterId); nIndex.has_value()) |
110 | 40.5k | { |
111 | 40.5k | if (bValue) |
112 | 14.9k | { |
113 | 14.9k | m_aClusters.at(*nIndex).m_nFlags |= ClusterJustificationFlags::KashidaPosition; |
114 | 14.9k | } |
115 | 25.5k | else |
116 | 25.5k | { |
117 | 25.5k | m_aClusters.at(*nIndex).m_nFlags &= ~ClusterJustificationFlags::KashidaPosition; |
118 | 25.5k | } |
119 | | |
120 | 40.5k | m_bContainsKashidaPositions = true; |
121 | 40.5k | } |
122 | 40.5k | } |
123 | | }; |
124 | | |
125 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |