summaryrefslogtreecommitdiffstats
path: root/starmath/source/mathml/element.cxx
blob: 4f8f2a64ff953fee217ee2415ebe30aea0429de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <mathml/element.hxx>

void SmMlElement::SmImplAttributeType()
{
    switch (m_aElementType)
    {
        case SmMlElementType::NMlEmpty:
            m_aAttributePosList = std::vector<SmMlAttributePos>(0);
            break;
        case SmMlElementType::NMlStructural:
            m_aAttributePosList = std::vector<SmMlAttributePos>(0);
            break;
        case SmMlElementType::NMlSmNode:
            m_aAttributePosList = std::vector<SmMlAttributePos>(0);
            break;
        case SmMlElementType::MlMath:
            m_aAttributePosList = std::vector<SmMlAttributePos>(0);
            //m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMath), std::end(starmathdatabase::MlAttributeListMath));
            break;
        case SmMlElementType::MlMi:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMi),
                                                std::end(starmathdatabase::MlAttributeListMi));
            break;
        case SmMlElementType::MlMerror:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMerror),
                                                std::end(starmathdatabase::MlAttributeListMerror));
            break;
        case SmMlElementType::MlMn:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMn),
                                                std::end(starmathdatabase::MlAttributeListMn));
            break;
        case SmMlElementType::MlMo:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMo),
                                                std::end(starmathdatabase::MlAttributeListMo));
            break;
        case SmMlElementType::MlMrow:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMrow),
                                                std::end(starmathdatabase::MlAttributeListMrow));
            break;
        case SmMlElementType::MlMtext:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMtext),
                                                std::end(starmathdatabase::MlAttributeListMtext));
            break;
        case SmMlElementType::MlMstyle:
            m_aAttributePosList
                = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMstyle),
                                                std::end(starmathdatabase::MlAttributeListMstyle));
            break;
        default:
            break;
    }
    // Create attribute vector with given pattern
    m_aAttributeList = starmathdatabase::makeMlAttributeList(m_aAttributePosList);
}

SmMlAttribute SmMlElement::getAttribute(SmMlAttributeValueType aAttributeType) const
{
    // Look for the attribute position and return if exists
    for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
    {
        if (m_aAttributePosList[i].m_aAttributeValueType == aAttributeType)
            return m_aAttributeList[m_aAttributePosList[i].m_nPos];
    }
    return SmMlAttribute();
}

bool SmMlElement::isAttributeSet(SmMlAttributeValueType aAttributeType) const
{
    // Look for the attribute position and return if exists
    for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
    {
        if (m_aAttributePosList[i].m_aAttributeValueType == aAttributeType)
            return m_aAttributeList[m_aAttributePosList[i].m_nPos].isSet();
    }
    return false;
}

void SmMlElement::setAttribute(const SmMlAttribute* aAttribute)
{
    // Look for the attribute position and assign if exists
    for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
    {
        if (m_aAttributePosList[i].m_aAttributeValueType == aAttribute->getMlAttributeValueType())
        {
            m_aAttributeList[m_aAttributePosList[i].m_nPos].setMlAttributeValue(aAttribute);
            break;
        }
    }
}

void SmMlElement::setSubElement(size_t nPos, SmMlElement* aElement)
{
    // This is the new parent element
    aElement->setParentElement(this);
    aElement->setSubElementId(nPos);
    // Check if the vector is long enough
    // Careful nOldSize can be 0 and -1 will underflow
    // We must put something on the empty locations
    size_t nOldSize = m_aSubElements.size();
    if (nPos + 1 > nOldSize)
    {
        m_aSubElements.resize(nPos + 1);
        for (; nOldSize < nPos; ++nOldSize)
            m_aSubElements[nOldSize] = nullptr;
    }
    // Assign value
    m_aSubElements[nPos] = aElement;
}

std::vector<SmMlAttribute>
starmathdatabase::makeMlAttributeList(std::vector<SmMlAttributePos> aAttributePosList)
{
    std::vector<SmMlAttribute> aAttributeList(aAttributePosList.size());
    for (size_t i = 0; i < aAttributePosList.size(); ++i)
    {
        aAttributeList[i].setMlAttributeValueType(aAttributePosList[i].m_aAttributeValueType);
    }
    return aAttributeList;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */