<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Manage images that are tileset or glyphset (for bitmap fonts)
module tileset

import mnit_display

# Efficienly retrieve tiles in a big image
class TileSet
	# The image containing the tileset
	var image: Image

	# The witdh of a tile
	var width: Int

	# The height of a tile
	var height: Int

	init
	do
		self.nb_cols = image.width / width
		self.nb_rows = image.height / height

		for j in [0..nb_rows[ do
			for i in [0..nb_cols[ do
				subimages.add image.subimage(i*width,j*height,width,height)
			end
		end
	end

	# The number of columns of tiles in the image
	var nb_cols: Int is noinit

	# The number of rows of tiles in the image
	var nb_rows: Int is noinit

	# Cache for images of tiles
	var subimages = new Array[Image]

	# The subimage of given tile
	# Aborts if x or y are out of bound
	fun [](x,y: Int): Image
	do
		assert x >= 0 and x < nb_cols and y >= 0 and y <= nb_rows else print "{x}x{y}<?{nb_cols}x{nb_rows}"
		var idx = x + y * nb_cols
		return subimages[idx]
	end
end

# A monospace bitmap font where glyphs are stored in a tileset
class TileSetFont
	super TileSet

	# Each caracter in the image
	# in left->right, then top->bottom order
	# Use space (' ') for holes in the tileset
	var chars: String

	# Additional space to insert horizontally between characters
	# A negave value will display tile overlaped
	var hspace: Numeric = 0.0 is writable

	# Additional space to insert vertically between characters
	# A negave value will display tile overlaped
	var vspace: Numeric = 0.0 is writable

	# The glyph (tile) associated to the caracter `c` according to `chars`
	# Returns null if `c` is not in `chars`
	fun char(c: Char): nullable Image
	do
		var i = chars.index_of(c)<commit_msg>lib/tileset: fix typos in doc<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Manage images that are tileset or glyphset (for bitmap fonts)
module tileset

import mnit_display

# Efficiently retrieve tiles in a big image
class TileSet
	# The image containing the tileset
	var image: Image

	# The witdh of a tile
	var width: Int

	# The height of a tile
	var height: Int

	init
	do
		self.nb_cols = image.width / width
		self.nb_rows = image.height / height

		for j in [0..nb_rows[ do
			for i in [0..nb_cols[ do
				subimages.add image.subimage(i*width,j*height,width,height)
			end
		end
	end

	# The number of columns of tiles in the image
	var nb_cols: Int is noinit

	# The number of rows of tiles in the image
	var nb_rows: Int is noinit

	# Cache for images of tiles
	var subimages = new Array[Image]

	# The subimage of given tile
	# Aborts if x or y are out of bound
	fun [](x,y: Int): Image
	do
		assert x >= 0 and x < nb_cols and y >= 0 and y <= nb_rows else print "{x}x{y}<?{nb_cols}x{nb_rows}"
		var idx = x + y * nb_cols
		return subimages[idx]
	end
end

# A monospace bitmap font where glyphs are stored in a tileset
class TileSetFont
	super TileSet

	# Each character in the image
	# in left->right, then top->bottom order
	# Use space (' ') for holes in the tileset
	var chars: String

	# Additional space to insert horizontally between characters
	# A negative value will display tile overlapped
	var hspace: Numeric = 0.0 is writable

	# Additional space to insert vertically between characters
	# A negative value will display tile overlapped
	var vspace: Numeric = 0.0 is writable

	# The glyph (tile) associated to the character `c` according to `chars`
	# Returns null if `c` is not in `chars`
	fun char(c: Char): nullable Image
	do
		var i = chars.index_of(c)
{'fraction_non_alphanumeric': 0.07208359991469396, 'fraction_numerical': 0.005118362124120281, 'mean_word_length': 3.6252465483234713, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571635', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1444, 'n_words': 681}