Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/inc/font/LogicalFontInstance.hxx
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
#pragma once
21
22
#include <sal/config.h>
23
24
#include <basegfx/polygon/b2dpolypolygon.hxx>
25
#include <basegfx/range/b2drectangle.hxx>
26
#include <o3tl/hash_combine.hxx>
27
#include <rtl/ref.hxx>
28
#include <salhelper/simplereferenceobject.hxx>
29
#include <tools/gen.hxx>
30
#include <tools/fontenum.hxx>
31
#include <tools/degree.hxx>
32
33
#include <font/FontSelectPattern.hxx>
34
#include <font/FontMetricData.hxx>
35
#include <glyphid.hxx>
36
37
#include <vcl/outdev/OpenTypeMathConstant.hxx>
38
39
#include <optional>
40
#include <unordered_map>
41
42
#include <hb.h>
43
44
class ConvertChar;
45
class ImplFontCache;
46
namespace vcl::font
47
{
48
class PhysicalFontFace;
49
}
50
51
constexpr float ARTIFICIAL_ITALIC_MATRIX_XX = 1 << 16;
52
constexpr float ARTIFICIAL_ITALIC_MATRIX_XY = (1 << 16) / 3.f;
53
constexpr float ARTIFICIAL_ITALIC_SKEW = ARTIFICIAL_ITALIC_MATRIX_XY / ARTIFICIAL_ITALIC_MATRIX_XX;
54
55
// extend std namespace to add custom hash needed for LogicalFontInstance
56
57
namespace std
58
{
59
template <> struct hash<pair<sal_UCS4, FontWeight>>
60
{
61
    size_t operator()(const pair<sal_UCS4, FontWeight>& rData) const
62
0
    {
63
0
        std::size_t seed = 0;
64
0
        o3tl::hash_combine(seed, rData.first);
65
0
        o3tl::hash_combine(seed, rData.second);
66
0
        return seed;
67
0
    }
68
};
69
}
70
71
// TODO: allow sharing of metrics for related fonts
72
73
class VCL_PLUGIN_PUBLIC LogicalFontInstance : public salhelper::SimpleReferenceObject
74
{
75
    // just declaring the factory function doesn't work AKA
76
    // friend LogicalFontInstance* PhysicalFontFace::CreateFontInstance(const FontSelectPattern&) const;
77
    friend class vcl::font::PhysicalFontFace;
78
    friend class ImplFontCache;
79
80
public: // TODO: make data members private
81
    virtual ~LogicalFontInstance() override;
82
83
    FontMetricDataRef mxFontMetric; // Font attributes
84
    const ConvertChar* mpConversion; // used e.g. for StarBats->StarSymbol
85
86
    tools::Long mnLineHeight;
87
    Degree10 mnOwnOrientation; // text angle if lower layers don't rotate text themselves
88
    Degree10 mnOrientation; // text angle in 3600 system
89
    bool mbInit; // true if maFontMetric member is valid
90
91
    SAL_DLLPRIVATE void AddFallbackForUnicode(sal_UCS4 cChar, FontWeight eWeight,
92
                                              const OUString& rFontName, bool bEmbolden,
93
                                              const ItalicMatrix& rMatrix);
94
    SAL_DLLPRIVATE bool GetFallbackForUnicode(sal_UCS4 cInChar, FontWeight eInWeight,
95
                                              OUString* pOutFontName, bool* pOutEmbolden,
96
                                              ItalicMatrix* pOutItalicMatrix) const;
97
    SAL_DLLPRIVATE void IgnoreFallbackForUnicode(sal_UCS4, FontWeight eWeight,
98
                                                 std::u16string_view rFontName);
99
100
    inline hb_font_t* GetHbFont();
101
    SAL_DLLPRIVATE bool IsGraphiteFont();
102
39.5M
    const vcl::font::FontSelectPattern& GetFontSelectPattern() const { return m_aFontSelData; }
103
104
    const std::vector<vcl::font::Variation>& GetVariations() const;
105
106
    void SetPointSize(float fPointSize)
107
127k
    {
108
127k
        m_fPointSize = fPointSize;
109
127k
        mxVariations.reset();
110
127k
    }
111
8.07M
    float GetPointSize() const { return m_fPointSize; }
112
113
32.4M
    const vcl::font::PhysicalFontFace* GetFontFace() const { return m_pFontFace.get(); }
114
618k
    vcl::font::PhysicalFontFace* GetFontFace() { return m_pFontFace.get(); }
115
92.0M
    const ImplFontCache* GetFontCache() const { return mpFontCache; }
116
117
    void GetFontMetric(FontMetricDataRef const&);
118
    bool GetGlyphBoundRect(sal_GlyphId, basegfx::B2DRectangle&, bool) const;
119
    bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon&, bool) const;
120
121
    sal_GlyphId GetGlyphIndex(uint32_t, uint32_t = 0) const;
122
123
    SAL_DLLPRIVATE double GetGlyphWidth(sal_GlyphId, bool = false, bool = true) const;
124
125
    double GetKashidaWidth() const;
126
127
    SAL_DLLPRIVATE void GetScale(double* nXScale, double* nYScale) const;
128
129
    bool NeedsArtificialItalic() const;
130
    bool NeedsArtificialBold() const;
131
132
    double GetOpenTypeMathConstant(vcl::OpenTypeMathConstant aConstant) const;
133
134
protected:
135
    explicit LogicalFontInstance(const vcl::font::PhysicalFontFace&,
136
                                 const vcl::font::FontSelectPattern&);
137
138
    SAL_DLLPRIVATE hb_font_t* InitHbFont();
139
123k
    virtual void ImplInitHbFont(hb_font_t*) {}
140
141
private:
142
    SAL_DLLPRIVATE bool DrawGlyph(hb_font_t*, sal_GlyphId, basegfx::B2DPolyPolygon&) const;
143
144
    struct MapEntry
145
    {
146
        OUString sFontName;
147
        bool bEmbolden;
148
        ItalicMatrix aItalicMatrix;
149
    };
150
    // cache of Unicode characters and replacement font names and attributes
151
    // TODO: a fallback map can be shared with many other ImplFontEntries
152
    // TODO: at least the ones which just differ in orientation, stretching or height
153
    typedef ::std::unordered_map<::std::pair<sal_UCS4, FontWeight>, MapEntry> UnicodeFallbackList;
154
    UnicodeFallbackList maUnicodeFallbackList;
155
    mutable ImplFontCache* mpFontCache;
156
    const vcl::font::FontSelectPattern m_aFontSelData;
157
    hb_font_t* m_pHbFont;
158
    rtl::Reference<vcl::font::PhysicalFontFace> m_pFontFace;
159
    std::optional<bool> m_xbIsGraphiteFont;
160
    std::vector<vcl::font::Variation> m_aVariations;
161
    mutable std::optional<std::vector<vcl::font::Variation>> mxVariations;
162
    bool m_bOpticalSizing = false;
163
    float m_fPointSize = 0;
164
165
    mutable hb_draw_funcs_t* m_pHbDrawFuncs = nullptr;
166
    basegfx::B2DPolygon m_aDrawPolygon;
167
};
168
169
inline hb_font_t* LogicalFontInstance::GetHbFont()
170
19.2M
{
171
19.2M
    if (!m_pHbFont)
172
123k
        m_pHbFont = InitHbFont();
173
19.2M
    return m_pHbFont;
174
19.2M
}
175
176
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */