aboutsummaryrefslogtreecommitdiff
path: root/font_api/registry.lua
blob: 4b49b1ec28a81be59ae1d38f76cf9826c68a0c59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--[[
    font_api mod for Minetest - Library to add font display capability
    to display_api mod.
    (c) Pierre-Yves Rollo

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--]]

-- Global variables
-------------------

font_api.registered_fonts = {}
font_api.registered_fonts_number = 0

-- Local variables
------------------

local default_font = false

-- Local functions
------------------

-- Gets a default (settings or fist font)
local function get_default_font()
	-- First call
	if default_font == false then
		default_font = nil

		-- First, try with settings
		local settings_font = minetest.settings:get("default_font")

		if settings_font ~= nil and settings_font ~= "" then
			default_font = font_api.registered_fonts[settings_font]

			if default_font == nil then
				minetest.log("warning", "Default font in settings (\""..
				             settings_font.."\") is not registered.")
			end
		end

		-- If failed, choose first font without default = false
		if default_font == nil then
			for _, font in pairs(font_api.registered_fonts) do
				if font.default then
					default_font = font
					break
				end
			end
		end

		-- If failed, chose first font
		if default_font == nil then
			for _, font in pairs(font_api.registered_fonts) do
				default_font = font
				break
			end
		end

		-- Error, no font registered
		if default_font == nil then
			minetest.log("error",
			             "No font registred, unable to choose a default font.")
		end
	end

	return default_font
end

--- Returns font object to be used according to font_name
-- @param font_name: Name of the font
-- @return Font object if font found (or default font)

function font_api.get_font(font_name)
	local font = font_api.registered_fonts[font_name]

	if font == nil then
		local message

		if font_name == nil then
			message = "No font given"
		else
			message = "Font \""..font_name.."\" unregistered"
		end

		font = get_default_font()

		if font ~= nil then
			minetest.log("info", message..", using font \""..font.name.."\".")
		end
	end

	return font
end

-- API functions
----------------

--- Returns de default font name
-- @return Default font name

function font_api.get_default_font_name()
	return get_default_font().name
end

--- Register a new font
-- Textures corresponding to the font should be named after following patern :
-- font_<name>_<code>.png
-- <name> : name of the font
-- <code> : 4 digit hexadecimal unicode of the char
-- @param font_name Name of the font to register
-- If registering different sizes of the same font, add size in the font name
-- (e.g. times_10, times_12...).
-- @param def font definition. A associative array with following keys :
-- @key default True (by default) if this font may be used as default font
-- @key height (mandatory) Height in pixels of all font textures
-- @key widths (mandatory) Array of character widths in pixels, indexed by 
-- UTF codepoints
-- @key margintop (optional) Margin (in texture pixels) added on top of each 
-- char texture.
-- @key marginbottom (optional) dded at bottom of each char texture.
-- @key linespacing (optional) Spacing (in texture pixels) between each lines.
-- margintop, marginbottom and linespacing can be negative numbers (default 0)
-- and are to be used to adjust various font styles to each other.

-- TODO: Add something to remove common accent if not defined in font

function font_api.register_font(font_name, font_def)

	if font_api.registered_fonts[font_name] ~= nil then
		minetest.log("error", "Font \""..font_name.."\" already registered.")
		return
	end

	local font = font_api.Font:new(font_def)

	if font == nil then	
		minetest.log("error", "Unable to register font \""..font_name.."\".")
		return
	end
	
	font.name = font_name
	font_api.registered_fonts[font_name] = font
	font_api.registered_fonts_number = font_api.registered_fonts_number + 1
	
	-- Force to choose again default font
	-- (allows use of fonts registered after start)
	default_font = false
	
	minetest.log("action", "New font registered in font_api: "..font_name..".")
end