Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
FontLoader.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 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 all system fonts.
63///
64/// Every operating system has its own library of installed system fonts. The FontLoader interface
65/// is used to lookup these fonts and fetch the actual font data (raw TTF/OTF file data) for a given
66/// given font description.
67///
68/// ## Usage
69///
70/// To provide your own custom FontLoader implementation, you should inherit from this class,
71/// handle the virtual member functions, and then pass an instance of your class to
72/// Platform::set_font_loader() before calling Renderer::Create() or App::Create().
73///
74/// @note
75/// \parblock
76/// AppCore uses a default OS-specific FontLoader implementation when you call App::Create().
77///
78/// If you are using Renderer::Create(), you can still use AppCore's implementation-- see the
79/// helper functions defined in <AppCore/Platform.h>.
80/// \endparblock
81///
83 public:
84 virtual ~FontLoader();
85
86 ///
87 /// Fallback font family name. Will be used if all other fonts fail to load.
88 ///
89 /// @note This font should be guaranteed to exist (eg, FontLoader::Load won't fail when passed
90 /// this font family name).
91 ///
92 virtual String fallback_font() const = 0;
93
94 ///
95 /// Fallback font family name that can render the specified characters. Mainly used to support
96 /// CJK (Chinese, Japanese, Korean) text display.
97 ///
98 /// @param characters One or more UTF-16 characters. This is almost always a single character.
99 ///
100 /// @param weight Font weight.
101 ///
102 /// @param italic Whether or not italic is requested.
103 ///
104 /// @return Returns a font family name that can render the text.
105 ///
106 virtual String fallback_font_for_characters(const String& characters, int weight,
107 bool italic) const = 0;
108
109 ///
110 /// Get the actual font file data (TTF/OTF) for a given font description.
111 ///
112 /// @param family Font family name.
113 ///
114 /// @param weight Font weight.
115 ///
116 /// @param italic Whether or not italic is requested.
117 ///
118 /// @return A font file matching the given description (either an on-disk font filepath or an
119 /// in-memory file contents). You can return NULL here and the loader will fallback to
120 /// another font.
121 ///
122 virtual RefPtr<FontFile> Load(const String& family, int weight, bool italic) = 0;
123};
124
125} // namespace ultralight
#define UExport
Definition Defines.h:65
Represents a font file, either on-disk path or in-memory file contents.
Definition FontLoader.h:18
FontFile(const FontFile &)
virtual RefPtr< Buffer > buffer() const =0
The in-memory buffer (if any).
static RefPtr< FontFile > Create(const String &filepath)
Create a font file from an on-disk file path.
virtual bool is_in_memory() const =0
Whether or not this font file was created from an in-memory buffer.
void operator=(const FontFile &)
virtual uint32_t hash() const =0
Unique hash (if this is a filepath, only the path string is hashed).
virtual String filepath() const =0
The file path (if any).
static RefPtr< FontFile > Create(RefPtr< Buffer > buffer)
Create a font file from an in-memory buffer.
User-defined font loader interface.
Definition FontLoader.h:82
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
UTF-8 String container with conversions for UTF-16 and UTF-32.
Definition String.h:21
Definition App.h:14