Loading...
Searching...
No Matches
FontLoader.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
10#include <Ultralight/String.h>
11#include <Ultralight/Buffer.h>
12
13namespace ultralight {
14
15///
16/// Represents a font file, either on-disk path or in-memory file contents.
17///
18class UExport FontFile : public RefCounted {
19 public:
20 ///
21 /// Create a font file from an on-disk file path.
22 ///
23 /// @note The file path should already exist.
24 ///
25 static RefPtr<FontFile> Create(const String& filepath);
26
27 ///
28 /// Create a font file from an in-memory buffer.
29 ///
31
32 ///
33 /// Whether or not this font file was created from an in-memory buffer.
34 ///
35 virtual bool is_in_memory() const = 0;
36
37 ///
38 /// The file path (if any).
39 ///
40 virtual String filepath() const = 0;
41
42 ///
43 /// The in-memory buffer (if any).
44 ///
45 virtual RefPtr<Buffer> buffer() const = 0;
46
47 ///
48 /// Unique hash (if this is a filepath, only the path string is hashed).
49 ///
50 virtual uint32_t hash() const = 0;
51
52 protected:
54 virtual ~FontFile();
56 void operator=(const FontFile&);
57};
58
59///
60/// User-defined font loader interface.
61///
62/// The library uses this to load a font file (eg, `Arial.ttf`) for a given font description (eg,
63/// `font-family: Arial;`).
64///
65/// Every OS has its own library of installed system fonts. The FontLoader interface is used to
66/// lookup these fonts and fetch the actual font data (raw TTF/OTF file data) for a given font
67/// description.
68///
69/// You can provide the library with your own font loader implementation so that you can bundle
70/// fonts with your application rather than relying on the system's installed fonts.
71///
72/// ## Default Implementation
73///
74/// A platform-specific implementation of FontLoader is provided for you when you call
75/// App::Create().
76///
77/// If you are using Renderer::Create(), you **must** provide your own. You can still use AppCore's
78/// implementation however-- see the helper functions defined in <AppCore/Platform.h>.
79///
80/// ## Setting the Font Loader
81///
82/// To provide your own custom FontLoader implementation, you should inherit from this class,
83/// handle the virtual member functions, and then pass an instance of your class to
84/// Platform::set_font_loader() before calling Renderer::Create() or App::Create().
85///
87 public:
88 virtual ~FontLoader();
89
90 ///
91 /// Fallback font family name. Will be used if all other fonts fail to load.
92 ///
93 /// @note This font should be guaranteed to exist (eg, FontLoader::Load won't fail when passed
94 /// this font family name).
95 ///
96 virtual String fallback_font() const = 0;
97
98 ///
99 /// Fallback font family name that can render the specified characters. Mainly used to support
100 /// CJK (Chinese, Japanese, Korean) text display.
101 ///
102 /// @param characters One or more UTF-16 characters. This is almost always a single character.
103 ///
104 /// @param weight Font weight.
105 ///
106 /// @param italic Whether or not italic is requested.
107 ///
108 /// @return Returns a font family name that can render the text.
109 ///
110 virtual String fallback_font_for_characters(const String& characters, int weight,
111 bool italic) const = 0;
112
113 ///
114 /// Get the actual font file data (TTF/OTF) for a given font description.
115 ///
116 /// @param family Font family name.
117 ///
118 /// @param weight Font weight.
119 ///
120 /// @param italic Whether or not italic is requested.
121 ///
122 /// @return A font file matching the given description (either an on-disk font filepath or an
123 /// in-memory file contents). You can return NULL here and the loader will fallback to
124 /// another font.
125 ///
126 virtual RefPtr<FontFile> Load(const String& family, int weight, bool italic) = 0;
127};
128
129} // namespace ultralight
#define UExport
Definition Exports.h:25
Represents a font file, either on-disk path or in-memory file contents.
Definition FontLoader.h:18
FontFile(const FontFile &)
static RefPtr< FontFile > Create(RefPtr< Buffer > buffer)
Create a font file from an in-memory buffer.
static RefPtr< FontFile > Create(const String &filepath)
Create a font file from an on-disk file path.
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 &)
User-defined font loader interface.
Definition FontLoader.h:86
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:47
A nullable smart pointer.
Definition RefPtr.h:79
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:34
Definition App.h:14