Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/inc/font/PhysicalFontFace.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 <i18nlangtag/languagetag.hxx>
25
#include <rtl/ref.hxx>
26
#include <salhelper/simplereferenceobject.hxx>
27
#include <tools/color.hxx>
28
#include <vcl/dllapi.h>
29
#include <vcl/fontcapabilities.hxx>
30
#include <vcl/fontcharmap.hxx>
31
32
#include <fontattributes.hxx>
33
#include <fontsubset.hxx>
34
35
#include <hb.h>
36
#include <hb-ot.h>
37
38
class LogicalFontInstance;
39
namespace vcl::font
40
{
41
class FontSelectPattern;
42
}
43
44
namespace vcl::font
45
{
46
struct FontMatchStatus
47
{
48
public:
49
    int mnFaceMatch;
50
    const OUString* mpTargetStyleName;
51
};
52
53
struct RawFontData
54
{
55
public:
56
    RawFontData(hb_blob_t* pBlob = nullptr)
57
7.20M
        : mpBlob(pBlob ? pBlob : hb_blob_get_empty())
58
7.20M
    {
59
7.20M
    }
60
61
    RawFontData(const RawFontData& rOther)
62
0
        : mpBlob(hb_blob_reference(rOther.mpBlob))
63
0
    {
64
0
    }
65
66
7.20M
    ~RawFontData() { hb_blob_destroy(mpBlob); }
67
68
    RawFontData& operator=(const RawFontData& rOther)
69
49.1k
    {
70
49.1k
        hb_blob_destroy(mpBlob);
71
49.1k
        mpBlob = hb_blob_reference(rOther.mpBlob);
72
49.1k
        return *this;
73
49.1k
    }
74
75
8.63M
    size_t size() const { return hb_blob_get_length(mpBlob); }
76
7.64M
    bool empty() const { return size() == 0; }
77
    const uint8_t* data() const
78
996k
    {
79
996k
        return reinterpret_cast<const uint8_t*>(hb_blob_get_data(mpBlob, nullptr));
80
996k
    }
81
82
private:
83
    hb_blob_t* mpBlob;
84
};
85
86
struct ColorLayer
87
{
88
    sal_GlyphId nGlyphIndex;
89
    uint32_t nColorIndex;
90
};
91
92
typedef std::vector<Color> ColorPalette;
93
94
// https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-ids
95
typedef enum : hb_ot_name_id_t {
96
    NAME_ID_COPYRIGHT = 0,
97
    NAME_ID_FONT_FAMILY = 1,
98
    NAME_ID_FONT_SUBFAMILY = 2,
99
    NAME_ID_UNIQUE_ID = 3,
100
    NAME_ID_FULL_NAME = 4,
101
    NAME_ID_VERSION_STRING = 5,
102
    NAME_ID_POSTSCRIPT_NAME = 6,
103
    NAME_ID_TRADEMARK = 7,
104
    NAME_ID_MANUFACTURER = 8,
105
    NAME_ID_DESIGNER = 9,
106
    NAME_ID_DESCRIPTION = 10,
107
    NAME_ID_VENDOR_URL = 11,
108
    NAME_ID_DESIGNER_URL = 12,
109
    NAME_ID_LICENSE = 13,
110
    NAME_ID_LICENSE_URL = 14,
111
    //NAME_ID_RESERVED = 15,
112
    NAME_ID_TYPOGRAPHIC_FAMILY = 16,
113
    NAME_ID_TYPOGRAPHIC_SUBFAMILY = 17,
114
    NAME_ID_MAC_FULL_NAME = 18,
115
    NAME_ID_SAMPLE_TEXT = 19,
116
    NAME_ID_CID_FINDFONT_NAME = 20,
117
    NAME_ID_WWS_FAMILY = 21,
118
    NAME_ID_WWS_SUBFAMILY = 22,
119
    NAME_ID_LIGHT_BACKGROUND = 23,
120
    NAME_ID_DARK_BACKGROUND = 24,
121
    NAME_ID_VARIATIONS_PS_PREFIX = 25,
122
} NameID;
123
124
// TODO: no more direct access to members
125
// TODO: get rid of height/width for scalable fonts
126
// TODO: make cloning cheaper
127
128
/**
129
 * abstract base class for physical font faces
130
 *
131
 * It acts as a factory for its corresponding LogicalFontInstances and
132
 * can be extended to cache device and font instance specific data.
133
 */
134
class VCL_PLUGIN_PUBLIC PhysicalFontFace : public FontAttributes,
135
                                           public salhelper::SimpleReferenceObject
136
{
137
public:
138
    ~PhysicalFontFace();
139
140
    virtual rtl::Reference<LogicalFontInstance>
141
    CreateFontInstance(const vcl::font::FontSelectPattern&) const = 0;
142
143
    virtual sal_IntPtr GetFontId() const = 0;
144
    virtual FontCharMapRef GetFontCharMap() const;
145
    virtual bool GetFontCapabilities(vcl::FontCapabilities&) const;
146
147
    SAL_DLLPRIVATE RawFontData GetRawFontData(uint32_t) const;
148
149
    bool IsBetterMatch(const vcl::font::FontSelectPattern&, FontMatchStatus&) const;
150
    sal_Int32 CompareIgnoreSize(const PhysicalFontFace&) const;
151
152
    // CreateFontSubset: a method to get a subset of glyphs of a font inside a
153
    // new valid font file
154
    // returns true if creation of subset was successful
155
    // parameters: rOutBuffer: vector to write the subset to
156
    //             pGlyphIDs: the glyph ids to be extracted
157
    //             pEncoding: the character code corresponding to each glyph
158
    //             nGlyphs: the number of glyphs
159
    //             rInfo: additional outgoing information
160
    // implementation note: encoding 0 with glyph id 0 should be added implicitly
161
    // as "undefined character"
162
    SAL_DLLPRIVATE bool CreateFontSubset(std::vector<sal_uInt8>&, const sal_GlyphId*,
163
                                         const sal_uInt8*, const int, FontSubsetInfo&) const;
164
165
6.61M
    bool IsColorFont() const { return HasColorLayers() || HasColorBitmaps(); }
166
167
    bool HasColorLayers() const;
168
    SAL_DLLPRIVATE std::vector<ColorLayer> GetGlyphColorLayers(sal_GlyphId) const;
169
170
    SAL_DLLPRIVATE const std::vector<ColorPalette>& GetColorPalettes() const;
171
172
    bool HasColorBitmaps() const;
173
    SAL_DLLPRIVATE RawFontData GetGlyphColorBitmap(sal_GlyphId, tools::Rectangle&) const;
174
175
    OString GetGlyphName(sal_GlyphId, bool = false) const;
176
177
29.1M
    uint32_t UnitsPerEm() const { return hb_face_get_upem(GetHbFace()); }
178
179
    OUString GetName(NameID, const LanguageTag&) const;
180
0
    OUString GetName(NameID aNameID) const { return GetName(aNameID, LanguageTag(LANGUAGE_NONE)); }
181
182
    virtual hb_face_t* GetHbFace() const;
183
    virtual hb_blob_t* GetHbTable(hb_tag_t) const
184
0
    {
185
        assert(false);
186
0
        return nullptr;
187
0
    }
188
189
    virtual const std::vector<hb_variation_t>& GetVariations(const LogicalFontInstance&) const;
190
191
protected:
192
    mutable hb_face_t* mpHbFace;
193
    mutable hb_font_t* mpHbUnscaledFont;
194
    mutable FontCharMapRef mxCharMap;
195
    mutable std::optional<vcl::FontCapabilities> mxFontCapabilities;
196
    mutable std::optional<std::vector<ColorPalette>> mxColorPalettes;
197
    mutable std::optional<std::vector<hb_variation_t>> mxVariations;
198
199
    explicit PhysicalFontFace(const FontAttributes&);
200
201
    SAL_DLLPRIVATE hb_font_t* GetHbUnscaledFont() const;
202
};
203
}
204
205
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */