1// LAF OS Library
2// Copyright (C) 2020 Igara Studio S.A.
3// Copyright (C) 2016-2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "os/common/freetype_font.h"
13
14#include "base/string.h"
15#include "ft/algorithm.h"
16#include "gfx/point.h"
17#include "gfx/size.h"
18
19namespace os {
20
21FreeTypeFont::FreeTypeFont(ft::Lib& lib,
22 const char* filename,
23 const int height)
24 : m_face(lib.open(filename))
25{
26 if (m_face.isValid())
27 m_face.setSize(height);
28}
29
30FreeTypeFont::~FreeTypeFont()
31{
32}
33
34bool FreeTypeFont::isValid() const
35{
36 return m_face.isValid();
37}
38
39FontType FreeTypeFont::type()
40{
41 return FontType::FreeType;
42}
43
44int FreeTypeFont::height() const
45{
46 return int(m_face.height());
47}
48
49int FreeTypeFont::textLength(const std::string& str) const
50{
51 return ft::calc_text_bounds(m_face, str).w;
52}
53
54bool FreeTypeFont::isScalable() const
55{
56 return true;
57}
58
59void FreeTypeFont::setSize(int size)
60{
61 m_face.setSize(size);
62}
63
64void FreeTypeFont::setAntialias(bool antialias)
65{
66 m_face.setAntialias(antialias);
67}
68
69bool FreeTypeFont::hasCodePoint(int codepoint) const
70{
71 return m_face.hasCodePoint(codepoint);
72}
73
74Ref<FreeTypeFont> load_free_type_font(ft::Lib& lib,
75 const char* filename,
76 const int height)
77{
78 Ref<FreeTypeFont> font = base::make_ref<FreeTypeFont>(lib, filename, height);
79 if (!font->isValid())
80 font.reset(); // delete font
81 return font;
82}
83
84} // namespace os
85