1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21// LOVE
22#include "Font.h"
23#include "BMFontRasterizer.h"
24#include "ImageRasterizer.h"
25
26#include "libraries/utf8/utf8.h"
27
28namespace love
29{
30namespace font
31{
32
33// Default TrueType font.
34#include "Vera.ttf.h"
35
36class DefaultFontData : public love::Data
37{
38public:
39
40 Data *clone() const override { return new DefaultFontData(); }
41 void *getData() const override { return Vera_ttf; }
42 size_t getSize() const override { return sizeof(Vera_ttf); }
43};
44
45Rasterizer *Font::newTrueTypeRasterizer(int size, TrueTypeRasterizer::Hinting hinting)
46{
47 StrongRef<DefaultFontData> data(new DefaultFontData, Acquire::NORETAIN);
48 return newTrueTypeRasterizer(data.get(), size, hinting);
49}
50
51Rasterizer *Font::newTrueTypeRasterizer(int size, float dpiscale, TrueTypeRasterizer::Hinting hinting)
52{
53 StrongRef<DefaultFontData> data(new DefaultFontData, Acquire::NORETAIN);
54 return newTrueTypeRasterizer(data.get(), size, dpiscale, hinting);
55}
56
57Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images, float dpiscale)
58{
59 return new BMFontRasterizer(fontdef, images, dpiscale);
60}
61
62Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text, int extraspacing, float dpiscale)
63{
64 std::vector<uint32> glyphs;
65 glyphs.reserve(text.size());
66
67 try
68 {
69 utf8::iterator<std::string::const_iterator> i(text.begin(), text.begin(), text.end());
70 utf8::iterator<std::string::const_iterator> end(text.end(), text.begin(), text.end());
71
72 while (i != end)
73 glyphs.push_back(*i++);
74 }
75 catch (utf8::exception &e)
76 {
77 throw love::Exception("UTF-8 decoding error: %s", e.what());
78 }
79
80 return newImageRasterizer(data, &glyphs[0], (int) glyphs.size(), extraspacing, dpiscale);
81}
82
83Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int extraspacing, float dpiscale)
84{
85 return new ImageRasterizer(data, glyphs, numglyphs, extraspacing, dpiscale);
86}
87
88GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
89{
90 uint32 codepoint = 0;
91
92 try
93 {
94 codepoint = utf8::peek_next(text.begin(), text.end());
95 }
96 catch (utf8::exception &e)
97 {
98 throw love::Exception("UTF-8 decoding error: %s", e.what());
99 }
100
101 return r->getGlyphData(codepoint);
102}
103
104GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
105{
106 return r->getGlyphData(glyph);
107}
108
109} // font
110} // love
111