aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
Commit message (Expand)AuthorAge
...
* New HUD element - waypoint.RealBadAngel2014-01-26
* Pass pointed_thing to on_punch and minetest.register_on_punchnode callbacksShadowNinja2014-01-23
* LuaVoxelManip: Add get_param2_data and set_param2_datakwolekr2014-01-19
* Add formspec tableKahrl2014-01-13
* Allow vertical axis particle rotation constraintkhonkhortisan2014-01-13
* Fix some errors reported by clang static analyzer.Ilya Zhuravlev2014-01-13
* Rename get_json_value to read_json_valueShadowNinja2014-01-11
* Add InvRef::get/set_lists()ShadowNinja2014-01-11
* Add forceloadingNovatux2014-01-11
* Fix absence of images when compiled with RUN_IN_PLACE=0.Ilya Zhuravlev2014-01-05
* Fix enum element name in Lua HUD code (position vs. pos)kaeza2013-12-31
* Bugfix to get_all_craft_recipes.RealBadAngel2013-12-21
* Don't throw a error when writing JSON failsShadowNinja2013-12-18
* Add 'minetest.write_json'ShadowNinja2013-12-18
* Handle LuaErrors in Lua -> C++ calls on LuaJITShadowNinja2013-12-18
* Add map feature generation notify Lua APIkwolekr2013-12-14
* Make line_of_sight return blocking node positionstujones112013-12-12
* Implement search tab and version pickersapier2013-12-11
* Fix leak and possible segfault in minetest.set_mapgen_paramskwolekr2013-12-08
* Use a table in set_physics_override()PilzAdam2013-12-03
* Add sneak and sneak_glitch to set_physics_override()PilzAdam2013-12-03
* Fix broken async locking in release buildsapier2013-12-03
* Fix MinGW buildSfan52013-12-01
* Cleanup jthread and fix win32 buildsapier2013-12-01
* Decoration: Add schematic Y-slice probability supportkwolekr2013-12-01
* Add error handler to async stepShadowNinja2013-11-30
* Optimize table creationShadowNinja2013-11-30
* Add minetest.swap_nodeNovatux2013-11-30
* Fix MSVC build.Ilya Zhuravlev2013-11-30
* LuaVoxelManip: Add area parameters back to calc_lighting and set_lighting, ma...kwolekr2013-11-30
* LuaVoxelManip: Add get_light_data() and set_light_data()kwolekr2013-11-30
* Fixed potential NULL pointer and leak when setting node metadataMetaDucky2013-11-29
* Fix broken thread stop handlingsapier2013-11-30
* Fix old gcc buildsapier2013-11-29
* Fix modstore/favourites hang by adding asynchronous lua job supportsapier2013-11-29
* Rollback fixes and get_node_actionsShadowNinja2013-11-27
* Fix line_of_sight (AGAIN)sapier2013-11-25
* LuaVoxelManip: Update parameter index of set_lighting()kwolekr2013-11-21
* Fix issue #1009 (minetest.get_connected_players() returns non-existing players)kwolekr2013-11-17
* Decoration: Stop DecoSimple::resolveNodeNames from complaining about no node ...kwolekr2013-11-17
* Fix invalid listname and listsize not handled correctly in set_sizesapier2013-11-16
* Pass a errfunc to lua_pcall to get a tracebackShadowNinja2013-11-15
* Fix invalid usage of temporary object in mainmenu json conversionsapier2013-11-11
* Add set_name(), set_count(), set_wear() and set_metadata() to Lua ItemStackPilzAdam2013-09-28
* Show git hash in version string at top left corner of windowKahrl2013-09-28
* Re-fix hud_change stat argument retrievalkwolekr2013-09-26
* Fix some warnings and other minor detailskwolekr2013-09-16
* Weather: Clean up getHeat/getHumidity somewhatkwolekr2013-09-16
* Allow non-string arguments for minetest.is_yes()PilzAdam2013-09-10
* Add Settings interface for LuaPilzAdam2013-09-10
">4) % 0x40 end end -- Split multiline text into array of lines, with <maxlines> maximum lines. -- Can not use minetest string.split as it has bug if first line(s) empty local function split_lines(text, maxlines) local lines = {} local pos = 1 repeat local found = string.find(text, "\n", pos) found = found or #text + 1 lines[#lines + 1] = string.sub(text, pos, found - 1) pos = found + 1 until (maxlines and (#lines >= maxlines)) or (pos > (#text + 1)) return lines end -------------------------------------------------------------------------------- --- Font class local Font = {} font_api.Font = Font function Font:new(def) if type(def) ~= "table" then minetest.log("error", "[font_api] Font definition must be a table.") return nil end if def.height == nil or def.height <= 0 then minetest.log("error", "[font_api] Font definition must have a positive height.") return nil end if type(def.widths) ~= "table" then minetest.log("error", "[font_api] Font definition must have a widths array.") return nil end if def.widths[0] == nil then minetest.log("error", "[font_api] Font must have a char with codepoint 0 (=unknown char).") return nil end local font = table.copy(def) setmetatable(font, self) self.__index = self return font end --- Gets the next char of a text -- @return Codepoint of first char, -- @return Remaining string without this first char function Font:get_next_char(text) local bytes = get_char_bytes(text) if bytes == nil then minetest.log("warning", "[font_api] Encountered a non UTF char, not displaying text.") return nil, '' end local codepoint = char_to_codepoint(text) -- Fallback mechanism if self.widths[codepoint] == nil then local char = text:sub(1, bytes) if fallbacks[char] then return self:get_next_char(fallbacks[char]..text:sub(bytes+1)) else return 0, text:sub(bytes+1) -- Ultimate fallback end else return codepoint, text:sub(bytes+1) end end --- Returns the width of a given char -- @param char : codepoint of the char -- @return Char width function Font:get_char_width(char) -- Replace chars with no texture by the NULL(0) char if self.widths[char] ~= nil then return self.widths[char] else return self.widths[0] end end --- Text height for multiline text including margins and line spacing -- @param nb_of_lines : number of text lines (default 1) -- @return Text height function Font:get_height(nb_of_lines) if nb_of_lines == nil then nb_of_lines = 1 end if nb_of_lines > 0 then return ( (self.height or 0) + (self.margintop or 0) + (self.marginbottom or 0) ) * nb_of_lines + (self.linespacing or 0) * (nb_of_lines -1) else return nb_of_lines == 0 and 0 or nil end end --- Computes text width for a given text (ignores new lines) -- @param line Line of text which the width will be computed. -- @return Text width function Font:get_width(line) local codepoint local width = 0 line = line or '' while line ~= "" do codepoint, line = self:get_next_char(line) width = width + self:get_char_width(codepoint) end return width end --- Builds texture part for a text line -- @param line Text line to be rendered -- @param texturew Width of the texture (extra text is not rendered) -- @param x Starting x position in texture -- @param y Vertical position of the line in texture -- @return Texture string function Font:make_line_texture(line, texturew, x, y) local codepoint local texture = "" line = line or '' while line ~= '' do codepoint, line = self:get_next_char(line) -- Add image only if it is visible (at least partly) if x + self.widths[codepoint] >= 0 and x <= texturew then texture = texture.. string.format(":%d,%d=font_%s_%04x.png", x, y, self.name, codepoint) end x = x + self.widths[codepoint] end return texture end --- Builds texture for a multiline colored text -- @param text Text to be rendered -- @param texturew Width of the texture (extra text will be truncated) -- @param textureh Height of the texture -- @param maxlines Maximum number of lines -- @param halign Horizontal text align ("left"/"center"/"right") (optional) -- @param valign Vertical text align ("top"/"center"/"bottom") (optional) -- @param color Color of the text (optional) -- @return Texture string function Font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) local texture = "" local lines = {} local textheight = 0 local y -- Split text into lines (limited to maxlines fist lines) for num, line in pairs(split_lines(text, maxlines)) do lines[num] = { text = line, width = self:get_width(line) } end textheight = self:get_height(#lines) if #lines then if valign == "top" then y = 0 elseif valign == "bottom" then y = textureh - textheight else y = (textureh - textheight) / 2 end end y = y + (self.margintop or 0) for _, line in pairs(lines) do if halign == "left" then texture = texture.. self:make_line_texture(line.text, texturew, 0, y) elseif halign == "right" then texture = texture.. self:make_line_texture(line.text, texturew, texturew - line.width, y) else texture = texture.. self:make_line_texture(line.text, texturew, (texturew - line.width) / 2, y) end y = y + self:get_height() + (self.linespacing or 0) end texture = string.format("[combine:%dx%d", texturew, textureh)..texture if color then texture = texture.."^[colorize:"..color end return texture end