docs
Loading...
Searching...
No Matches
FontLoader.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7#include <Ultralight/String.h>
8#include <Ultralight/Buffer.h>
9
10namespace ultralight {
11
12///
13/// Represents a font file, either on-disk path or in-memory file contents.
14///
15class UExport FontFile : public RefCounted {
16 public:
17 ///
18 /// Create a font file from an on-disk file path.
19 ///
20 /// @param filepath The path to the font file on disk. The file should already exist.
21 ///
22 /// @param face_index The index of the face to use when the file is a font collection (eg, a
23 /// `.ttc` holding several families). Pass -1 to let the library choose the
24 /// face that best matches the requested weight and style.
25 ///
26 /// @return Returns a ref-pointer to a new FontFile instance.
27 ///
29
30 ///
31 /// Create a font file from an in-memory buffer.
32 ///
33 /// @param buffer The font file data (TTF/OTF, or a collection).
34 ///
35 /// @param face_index The index of the face to use when the data is a font collection. Pass
36 /// -1 to let the library choose the face that best matches the requested
37 /// weight and style.
38 ///
39 /// @return Returns a ref-pointer to a new FontFile instance.
40 ///
42
43 ///
44 /// Whether or not this font file was created from an in-memory buffer.
45 ///
46 virtual bool is_in_memory() const = 0;
47
48 ///
49 /// The file path (if any).
50 ///
51 virtual String filepath() const = 0;
52
53 ///
54 /// The in-memory buffer (if any).
55 ///
56 virtual RefPtr<Buffer> buffer() const = 0;
57
58 ///
59 /// The index of the face within a font collection, or -1 when the library chooses the face.
60 ///
61 virtual int face_index() const = 0;
62
63 ///
64 /// Unique hash (if this is a filepath, only the path string is hashed).
65 ///
66 virtual uint32_t hash() const = 0;
67
68 protected:
70 virtual ~FontFile();
72 void operator=(const FontFile&);
73};
74
75///
76/// User-defined font loader interface.
77///
78/// The library uses this to load a font file (eg, `Arial.ttf`) for a given font description (eg,
79/// `font-family: Arial;`).
80///
81/// Every OS has its own library of installed system fonts. The FontLoader interface is used to
82/// lookup these fonts and fetch the actual font data (raw TTF/OTF file data) for a given font
83/// description.
84///
85/// You can provide the library with your own font loader implementation so that you can bundle
86/// fonts with your application rather than relying on the system's installed fonts.
87///
88/// ## Default Implementation
89///
90/// A platform-specific implementation of FontLoader is provided for you when you call
91/// App::Create().
92///
93/// If you are using Renderer::Create(), you **must** provide your own. You can still use the
94/// default implementation however-- see the helper functions defined in <AppCore/Platform.h>.
95///
96/// ## Threading
97///
98/// The library calls these methods from the thread you created the Renderer on and from the
99/// threads that run Web Workers. Those calls can overlap, so your implementation **must** be safe
100/// to call from more than one thread at once.
101///
102/// ## Setting the Font Loader
103///
104/// To provide your own custom FontLoader implementation, you should inherit from this class,
105/// handle the virtual member functions, and then pass an instance of your class to
106/// Platform::set_font_loader() before calling Renderer::Create() or App::Create().
107///
109 public:
110 virtual ~FontLoader();
111
112 ///
113 /// Fallback font family name. Will be used if all other fonts fail to load.
114 ///
115 /// @note This font should be guaranteed to exist (eg, FontLoader::Load won't fail when passed
116 /// this font family name).
117 ///
118 virtual String fallback_font() const = 0;
119
120 ///
121 /// Fallback font family name that can render the specified characters. Mainly used to support
122 /// CJK (Chinese, Japanese, Korean) text display.
123 ///
124 /// @param characters One or more UTF-16 characters. This is almost always a single character.
125 ///
126 /// @param weight The CSS numeric font weight, from 100 (thin) to 900 (black), rounded to
127 /// the nearest multiple of 100. Normal is 400, bold is 700.
128 ///
129 /// @param italic Whether or not italic is requested.
130 ///
131 /// @return Returns a font family name that can render the text.
132 ///
133 virtual String fallback_font_for_characters(const String& characters, int weight,
134 bool italic) const = 0;
135
136 ///
137 /// Get the actual font file data (TTF/OTF) for a given font description.
138 ///
139 /// @param family Font family name.
140 ///
141 /// @param weight The CSS numeric font weight, from 100 (thin) to 900 (black), rounded to the
142 /// nearest multiple of 100. Normal is 400, bold is 700.
143 ///
144 /// @param italic Whether or not italic is requested.
145 ///
146 /// @return A font file matching the given description (either an on-disk font filepath or an
147 /// in-memory file contents). You can return NULL here and the library will fallback to
148 /// another font.
149 ///
150 /// @note Return the closest face you have. When the face you return isn't already bold for a
151 /// weight of 600 or heavier, or isn't already slanted for an italic request, the library
152 /// synthesizes the difference by emboldening or slanting the outlines. A page can turn
153 /// that off with the CSS `font-synthesis` property.
154 ///
155 virtual RefPtr<FontFile> Load(const String& family, int weight, bool italic) = 0;
156};
157
158} // namespace ultralight
#define UExport
Definition Exports.h:22
FontFile(const FontFile &)
static RefPtr< FontFile > Create(const String &filepath, int face_index=-1)
Create a font file from an on-disk file path.
virtual int face_index() const =0
The index of the face within a font collection, or -1 when the library chooses the face.
virtual RefPtr< Buffer > buffer() const =0
The in-memory buffer (if any).
virtual uint32_t hash() const =0
Unique hash (if this is a filepath, only the path string is hashed).
virtual bool is_in_memory() const =0
Whether or not this font file was created from an in-memory buffer.
virtual String filepath() const =0
The file path (if any).
void operator=(const FontFile &)
static RefPtr< FontFile > Create(RefPtr< Buffer > buffer, int face_index=-1)
Create a font file from an in-memory buffer.
User-defined font loader interface.
Definition FontLoader.h:108
virtual String fallback_font_for_characters(const String &characters, int weight, bool italic) const =0
Fallback font family name that can render the specified characters.
virtual RefPtr< FontFile > Load(const String &family, int weight, bool italic)=0
Get the actual font file data (TTF/OTF) for a given font description.
virtual String fallback_font() const =0
Fallback font family name.
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:49
A nullable smart pointer.
Definition RefPtr.h:126
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.