Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/starmath/source/utility.cxx
Line
Count
Source
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 <strings.hrc>
21
#include <smmod.hxx>
22
#include <utility.hxx>
23
#include <dialog.hxx>
24
#include <view.hxx>
25
26
#include <comphelper/lok.hxx>
27
#include <sfx2/lokcomponenthelpers.hxx>
28
29
// return pointer to active SmViewShell, if this is not possible
30
// return 0 instead.
31
//!! Since this method is based on the current focus it is somewhat
32
//!! unreliable and may return unexpected 0 pointers!
33
SmViewShell * SmGetActiveView()
34
9.94k
{
35
9.94k
    SfxViewShell *pView = SfxViewShell::Current();
36
9.94k
    SmViewShell* pSmView = dynamic_cast<SmViewShell*>(pView);
37
9.94k
    if (!pSmView && comphelper::LibreOfficeKit::isActive())
38
0
    {
39
0
        auto* pWindow = static_cast<SmGraphicWindow*>(LokStarMathHelper(pView).GetGraphicWindow());
40
0
        if (pWindow)
41
0
            pSmView = &pWindow->GetGraphicWidget().GetView();
42
0
    }
43
9.94k
    return pSmView;
44
9.94k
}
45
46
47
/**************************************************************************/
48
49
void SmFontPickList::Clear()
50
0
{
51
0
    aFontVec.clear();
52
0
}
53
54
SmFontPickList& SmFontPickList::operator = (const SmFontPickList& rList)
55
0
{
56
0
    Clear();
57
0
    nMaxItems = rList.nMaxItems;
58
0
    aFontVec = rList.aFontVec;
59
60
0
    return *this;
61
0
}
62
63
vcl::Font SmFontPickList::Get(sal_uInt16 nPos) const
64
0
{
65
0
    return nPos < aFontVec.size() ? aFontVec[nPos] : vcl::Font();
66
0
}
67
68
namespace {
69
70
bool lcl_CompareItem(const vcl::Font & rFirstFont, const vcl::Font & rSecondFont)
71
0
{
72
0
  return rFirstFont.GetFamilyName() == rSecondFont.GetFamilyName() &&
73
0
         rFirstFont.GetFamilyType() == rSecondFont.GetFamilyType() &&
74
0
         rFirstFont.GetCharSet()    == rSecondFont.GetCharSet()    &&
75
0
         rFirstFont.GetWeight()     == rSecondFont.GetWeight()     &&
76
0
         rFirstFont.GetItalic()     == rSecondFont.GetItalic();
77
0
}
78
79
OUString lcl_GetStringItem(const vcl::Font &rFont)
80
0
{
81
0
    OUStringBuffer aString(rFont.GetFamilyName());
82
83
0
    if (IsItalic( rFont ))
84
0
    {
85
0
        aString.append(", " + SmResId(RID_FONTITALIC));
86
0
    }
87
0
    if (IsBold( rFont ))
88
0
    {
89
0
        aString.append(", " + SmResId(RID_FONTBOLD));
90
0
    }
91
92
0
    return aString.makeStringAndClear();
93
0
}
94
95
}
96
97
void SmFontPickList::Insert(const vcl::Font &rFont)
98
0
{
99
0
    for (size_t nPos = 0; nPos < aFontVec.size(); nPos++)
100
0
        if (lcl_CompareItem( aFontVec[nPos], rFont))
101
0
        {
102
0
            aFontVec.erase( aFontVec.begin() + nPos );
103
0
            break;
104
0
        }
105
106
0
    aFontVec.push_front( rFont );
107
108
0
    if (aFontVec.size() > nMaxItems)
109
0
    {
110
0
        aFontVec.pop_back();
111
0
    }
112
0
}
113
114
void SmFontPickList::ReadFrom(const SmFontDialog& rDialog)
115
0
{
116
0
    Insert(rDialog.GetFont());
117
0
}
118
119
void SmFontPickList::WriteTo(SmFontDialog& rDialog) const
120
0
{
121
0
    rDialog.SetFont(Get());
122
0
}
123
124
125
/**************************************************************************/
126
127
SmFontPickListBox::SmFontPickListBox(std::unique_ptr<weld::ComboBox> pWidget)
128
0
    : SmFontPickList(4)
129
0
    , m_xWidget(std::move(pWidget))
130
0
{
131
0
    m_xWidget->connect_changed(LINK(this, SmFontPickListBox, SelectHdl));
132
0
}
133
134
IMPL_LINK_NOARG(SmFontPickListBox, SelectHdl, weld::ComboBox&, void)
135
0
{
136
0
    const int nPos = m_xWidget->get_active();
137
0
    if (nPos != 0)
138
0
    {
139
0
        SmFontPickList::Insert(Get(nPos));
140
0
        OUString aString = m_xWidget->get_text(nPos);
141
0
        m_xWidget->remove(nPos);
142
0
        m_xWidget->insert_text(0, aString);
143
0
    }
144
145
0
    m_xWidget->set_active(0);
146
0
}
147
148
SmFontPickListBox& SmFontPickListBox::operator=(const SmFontPickList& rList)
149
0
{
150
0
    *static_cast<SmFontPickList *>(this) = rList;
151
152
0
    for (decltype(aFontVec)::size_type nPos = 0; nPos < aFontVec.size(); nPos++)
153
0
        m_xWidget->insert_text(nPos, lcl_GetStringItem(aFontVec[nPos]));
154
155
0
    if (!aFontVec.empty())
156
0
        m_xWidget->set_active_text(lcl_GetStringItem(aFontVec.front()));
157
158
0
    return *this;
159
0
}
160
161
void SmFontPickListBox::Insert(const vcl::Font &rFont)
162
0
{
163
0
    SmFontPickList::Insert(rFont);
164
165
0
    OUString aEntry(lcl_GetStringItem(aFontVec.front()));
166
0
    int nPos = m_xWidget->find_text(aEntry);
167
0
    if (nPos != -1)
168
0
        m_xWidget->remove(nPos);
169
0
    m_xWidget->insert_text(0, aEntry);
170
0
    m_xWidget->set_active(0);
171
172
0
    while (m_xWidget->get_count() > nMaxItems)
173
0
        m_xWidget->remove(m_xWidget->get_count() - 1);
174
0
}
175
176
bool IsItalic( const vcl::Font &rFont )
177
1.32M
{
178
1.32M
    FontItalic eItalic = rFont.GetItalic();
179
    // the code below leaves only _NONE and _DONTKNOW as not italic
180
1.32M
    return eItalic == ITALIC_OBLIQUE  ||  eItalic == ITALIC_NORMAL;
181
1.32M
}
182
183
184
bool IsBold( const vcl::Font &rFont )
185
1.32M
{
186
1.32M
    FontWeight eWeight = rFont.GetWeight();
187
1.32M
    return eWeight > WEIGHT_NORMAL;
188
1.32M
}
189
190
191
void SmFace::Impl_Init()
192
10.4M
{
193
10.4M
    SetSize( GetFontSize() );
194
10.4M
    SetTransparent( true );
195
10.4M
    SetAlignment( ALIGN_BASELINE );
196
10.4M
    SetColor( COL_AUTO );
197
10.4M
}
198
199
void SmFace::SetSize(const Size& rSize)
200
77.0M
{
201
77.0M
    Size  aSize (rSize);
202
203
    // check the requested size against minimum value
204
77.0M
    const int nMinVal = o3tl::convert(2, o3tl::Length::pt, SmO3tlLengthUnit());
205
206
77.0M
    if (aSize.Height() < nMinVal)
207
12.1M
        aSize.setHeight( nMinVal );
208
209
    //! we don't force a maximum value here because this may prevent eg the
210
    //! parentheses in "left ( ... right )" from matching up with large
211
    //! bodies (eg stack{...} with many entries).
212
    //! Of course this is holds only if characters are used and not polygons.
213
214
77.0M
    Font::SetFontSize(aSize);
215
77.0M
}
216
217
218
tools::Long SmFace::GetBorderWidth() const
219
2.51M
{
220
2.51M
    if (nBorderWidth < 0)
221
2.47M
        return GetDefaultBorderWidth();
222
33.0k
    else
223
33.0k
        return nBorderWidth;
224
2.51M
}
225
226
SmFace & SmFace::operator = (const SmFace &rFace)
227
6.92M
{
228
6.92M
    Font::operator = (rFace);
229
6.92M
    nBorderWidth = -1;
230
6.92M
    return *this;
231
6.92M
}
232
233
234
SmFace & operator *= (SmFace &rFace, const Fraction &rFrac)
235
    // scales the width and height of 'rFace' by 'rFrac' and returns a
236
    // reference to 'rFace'.
237
    // It's main use is to make scaling fonts look easier.
238
58.9M
{   const Size &rFaceSize = rFace.GetFontSize();
239
240
58.9M
    rFace.SetSize(Size(tools::Long(rFaceSize.Width() * rFrac),
241
58.9M
                       tools::Long(rFaceSize.Height() * rFrac)));
242
58.9M
    return rFace;
243
58.9M
}
244
245
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */