5 Commits

Author SHA1 Message Date
Minjae Song
9ca70a601b fixing memory leak caused by my sloppy-ass coding 2019-02-07 17:30:15 +09:00
Minjae Song
6cacc56ea8 Update README.md 2019-02-07 03:25:59 +09:00
Minjae Song
6d142f082c readme: gitlab link -> github link 2019-01-29 23:42:20 +09:00
Minjae Song
db3b13800b Merge branch 'cpu-render-pixmap-cache' 2019-01-22 22:39:15 +09:00
Minjae Song
dfeab56bb5 Update CONTRIBUTING.md 2018-12-02 02:35:58 +09:00
5 changed files with 113 additions and 131 deletions

View File

@@ -42,15 +42,15 @@ Green-tinted area (should be 10 px tall) contains the tags. Tags are defined as
```
(LSB) 0 == Use Compiler Directive (Undefined right now, keep it as 0)
1 -+ 1 | Align to this X pos of prev char,
1 | 2 | only valid if write-on-top is 1
1 | 4 | and is centre-aligned and non-zero
1 -+ 8 | (if this is zero, floorOf(width/2) will be used instead)
0 -+ 0 Align 1 Align 0 Align 1 Align before
1 -+ 0 left 0 right 1 centre 1 the glyph
1 == write-on-top, usually it's diatritics but some are not (e.g. devanagari vowel sign O)
1 == 0 Stack 1 Stack 0 Before 1 Up &
(MSB) 0 == 0 up 0 down 1 &After 1 Down (e.g. U+0C48)
1 -, 1 = Align to this X pos of prev char,
1 | 2 = only valid if write-on-top is 1
1 | 4 = and is centre-aligned and non-zero
1 -' 8 = (if this is zero, floorOf(width/2) will be used instead)
0 -, 0 Align 1 Align 0 Align 1 Align before
1 -' 0 left 0 right 1 centre 1 the glyph
1 == write-on-top, usually it's diatritics but not always (e.g. devanagari vowel sign O)
1 -, 0 Stack 1 Stack 0 Before 1 Up &
(MSB) 0 -' 0 up 0 down 1 &After 1 Down (e.g. U+0C48)
```
#### Stack Up/Down
@@ -60,7 +60,7 @@ character is lowercase.
#### Align-To-This-X-Pos
Since this tag does not make sense for diacritics, they will use the value for compeletely different perporse:
Since this tag does not make sense for diacritics, they will use the value for compeletely different purpose:
0 : Nothing special
1 : Covers previous character; it's neither stack-up nor down.
@@ -76,7 +76,7 @@ To implement those, this two extra code points are needed, which are provided in
For working examples, take a note at the bengali sprite sheet.
This tag can be used as a general "replace this with these" directive, as long as you're replacing it into two letters. (e.g. U+0B94; TAMIL LETTER AU, which is a combination of U+0B92 and U+0BD7
This tag can be used as a general "replace this with these" directive, as long as you're replacing it into two letters. This directive is exploited to construct dutch ligature "IJ" (U+0132 and U+0133), in the sheet LatinExtA.
Also note that the font compiler will not "stack" these diacritics.
@@ -101,12 +101,16 @@ On this font, Hangul letters are printed by assemblying two or three letter piec
This is a Kotlin-like pseudocode for assembling the glyph:
// NOTE: this code implements modern Hangul only, in the unicode range of 0xAC00..0xD7A3.
// the spritesheet is made to accomodate Johab encoding scheme, but can still be used with the following code.
// for the code for full Johab encoding (U+1100.. that includes Old Korean), please refer to the actual code in the repo.
function getHanChosung(hanIndex: Int) = hanIndex / (21 * 28)
function getHanJungseong(hanIndex: Int) = hanIndex / 28 % 21
function getHanJongseong(hanIndex: Int) = hanIndex % 28
jungseongWide = arrayOf(8, 12, 13, 17, 18, 21)
jungseongComplex = arrayOf(9, 10, 11, 14, 15, 16, 22)
jungseongWide = arrayOf(9,13,14,18,19)
jungseongComplex = arrayOf(10,11,12,15,16,17,20,23)
function getHanInitialRow(hanIndex: Int): Int {
val ret: Int

View File

@@ -2,9 +2,9 @@
![Font sample](demo.PNG)
This font is a bitmap font used in [my game project called Terrarum](https://gitlab.com/minjaesong/terrarum) (hence the name). The font supports more than 90 % of european languages, as well as Chinese, Japanese and Korean. More technical side, it supports Latin-1 Supplement, Latin Ext-A, Latin Ext-B, IPA Extension (required by some languages), Greek, Cyrillic (+ Bulgarian, Serbian variants) and the supplement, Armenian, Thai (beta version), Georgian, Unicode Punctuations, CJK Punctuations, Kana, Chinese (limited to Unihan and Ext-A), Hangul (all 11 172 possible syllables) and Fullwidth forms.
This font is a bitmap font used in [my game project called Terrarum](https://github.com/minjaesong/Terrarum) (hence the name). The font supports more than 90 % of european languages, as well as Chinese, Japanese and Korean. More technical side, it supports Latin-1 Supplement, Latin Ext-A, Latin Ext-B, IPA Extension (required by some languages), Greek, Cyrillic (+ Bulgarian, Serbian variants) and the supplement, Armenian, Thai (beta version), Georgian, Unicode Punctuations, CJK Punctuations, Kana, Chinese (limited to Unihan and Ext-A), Hangul (all 11 172 possible syllables) and Fullwidth forms.
The JAR package is meant to be used with Slick2d (extends ```Font``` class) and LibGDX (extends ```BitmapFont``` class). If you are not using the framework, please refer to the __Font metrics__ section to implement the font metrics correctly on your system.
The JAR package is meant to be used with LibGDX (extends ```BitmapFont``` class). If you are not using the framework, please refer to the __Font metrics__ section to implement the font metrics correctly on your system.
The issue page is open. If you have some issues to submit, or have a question, please leave it on the page.
@@ -12,6 +12,7 @@ The issue page is open. If you have some issues to submit, or have a question, p
- To display Bulgarian/Serbian variants, you need special Control Characters. (GameFontBase.charsetOverrideBulgarian -- U+FFFC1; GameFontBase.charsetOverrideSerbian -- U+FFFC2)
- All Han characters are in Chinese variant, no other variants are to be supported as most Chinese, Japanese and Korean can understand other's variant and to be honest, we don't bother anyway.
- Indian script in general is not perfect: this font will never do the proper ligatures (I can't draw all the 1 224 possible combinations). Hopefully it's still be able to understand without them.
- Slick2d versions are now unsupported. I couldn't extend myself to work on both versions, but I'm still welcome to merge your pull requests.
### Design Goals
@@ -20,6 +21,9 @@ The issue page is open. If you have some issues to submit, or have a question, p
- Combininig with the sans-serif, this stands for **no over-simplification**
- Condensed capitals for efficient space usage
## Download
- Go ahead to the [release tab](https://github.com/minjaesong/Terrarum-sans-bitmap/releases), and download the most recent version. It is **not** advised to use the .jar found within the repository, they're experimental builds I use during the development, and may contain bugs like leaking memory.
## Using on your game

BIN
demo.PNG

Binary file not shown.

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 145 KiB

View File

@@ -34,10 +34,12 @@ import net.torvald.terrarumsansbitmap.GlyphProps
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.security.MessageDigest
import java.util.*
import java.util.zip.CRC32
import java.util.zip.GZIPInputStream
import kotlin.NullPointerException
import kotlin.math.roundToInt
import kotlin.math.sign
typealias CodepointSequence = ArrayList<CodePoint>
internal typealias CodePoint = Int
@@ -67,7 +69,8 @@ internal typealias Hash = Long
* Glyphs are drawn lazily (calculated on-the-fly, rather than load up all), which is inevitable as we just can't load
* up 40k+ characters on the machine, which will certainly make loading time painfully long.
*
* Color Codes have following Unicode mapping: U+10RGBA, A must be non-zero to be visible. U+100000 reverts any colour code effects.
* Color Codes have following Unicode mapping: U+10RGBA, A must be non-zero to be visible. U+100000 reverts any colour
* code effects.
*
* ## Control Characters
*
@@ -79,16 +82,19 @@ internal typealias Hash = Long
*
* ## Auto Shift Down
*
* Certain characters (e.g. Combining Diacritical Marks) will automatically shift down to accomodate lowercase letters. Shiftdown only occurs when non-diacritic character before the mark is lowercase, and the mark itself would stack up. Stack-up or down is defined using Tag system.
* Certain characters (e.g. Combining Diacritical Marks) will automatically shift down to accomodate lowercase letters.
* Shiftdown only occurs when non-diacritic character before the mark is lowercase, and the mark itself would stack up.
* Stack-up or down is defined using Tag system.
*
*
*
* @param noShadow Self-explanatory
* @param flipY If you have Y-down coord system implemented on your GDX (e.g. legacy codebase), set this to ```true``` so that the shadow won't be upside-down. For glyph getting upside-down, set ```TextureRegionPack.globalFlipY = true```.
* @param flipY If you have Y-down coord system implemented on your GDX (e.g. legacy codebase), set this to ```true```
* so that the shadow won't be upside-down. For glyph getting upside-down, set ```TextureRegionPack.globalFlipY = true```.
*
* Created by minjaesong on 2017-06-15.
*/
class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Boolean = false, val minFilter: Texture.TextureFilter = Texture.TextureFilter.Nearest, val magFilter: Texture.TextureFilter = Texture.TextureFilter.Nearest, var errorOnUnknownChar: Boolean = false, val textCacheSize: Int = 64, val debug: Boolean = false) : BitmapFont() {
class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Boolean = false, val minFilter: Texture.TextureFilter = Texture.TextureFilter.Nearest, val magFilter: Texture.TextureFilter = Texture.TextureFilter.Nearest, var errorOnUnknownChar: Boolean = false, val textCacheSize: Int = 256, val debug: Boolean = false) : BitmapFont() {
// Hangul Implementation Specific //
@@ -288,11 +294,19 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
// TODO (val posXbuffer: IntArray, val posYbuffer: IntArray) -> (val linotype: Pixmap)
private data class ShittyGlyphLayout(val textBuffer: CodepointSequence, val linotype: Pixmap, val width: Int)
private data class ShittyGlyphLayout(val textBuffer: CodepointSequence, val linotype: Texture, val width: Int)
//private val textCache = HashMap<CharSequence, ShittyGlyphLayout>()
private data class TextCacheObj(var age: Int, val hash: Long, val glyphLayout: ShittyGlyphLayout?)
private data class TextCacheObj(var age: Int, val hash: Long, val glyphLayout: ShittyGlyphLayout?): Comparable<TextCacheObj> {
fun dispose() {
glyphLayout?.linotype?.dispose()
}
override fun compareTo(other: TextCacheObj): Int {
return (this.hash - other.hash).sign
}
}
private var textCacheCap = 0
private val textCache = Array(textCacheSize) { TextCacheObj(-1, -1L, null) }
@@ -300,53 +314,50 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
/**
* Insertion sorts the last element fo the textCache
*/
private fun addToCache(text: CodepointSequence, linotype: Pixmap, width: Int) {
// make room first
if (textCacheCap == textCacheSize - 1) {
var c = 0
var mark = -1
while (c < textCacheSize - 1) {
if (textCache[c].age == 0 && mark == -1) // if unmarked and age == 0, mark it
mark = c
private fun addToCache(text: CodepointSequence, linotype: Texture, width: Int) {
// Caching rules:
// 1. always accept new element.
// 2. often-called element have higher chance of survival (see: getCache(long))
if (mark >= 0) { // if marked then ...
// shift left everyting by 1
textCache[c] = textCache[c + 1]
if (textCacheCap < textCacheSize) {
textCache[textCacheCap] = TextCacheObj(0, text.getHash(), ShittyGlyphLayout(text, linotype, width))
textCacheCap += 1
// make everybody age
textCache.forEach {
it.age += 1
}
}
else {
// search for an oldest element
var oldestElemIndex = 0
var ageOfOldest = textCacheCap
textCache.forEachIndexed { index, it ->
// make everybody age
textCache[index].age += 1
// mark oldest
if (it.age > ageOfOldest) {
oldestElemIndex = index
}
c += 1
}
// dispose of the oldest one before overwriting
textCache[oldestElemIndex].dispose()
// overwrite oldest one
textCache[oldestElemIndex] = TextCacheObj(0, text.getHash(), ShittyGlyphLayout(text, linotype, width))
}
// count down all the elements' age by 1
textCache.forEach { it.age -= 1 }
// put new element at the end
textCache[textCacheCap] = TextCacheObj(textCacheCap, text.getHash(), ShittyGlyphLayout(text, linotype, width))
// insertion sort last elem (sorted by hash)
if (textCacheCap >= 1) { // when there's two or more elem...
var j = textCacheCap - 1
val x = textCache[textCacheCap]
while (j >= 0 && textCache[j].hash > x.hash) {
textCache[j + 1] = textCache[j]
j -= 1
}
textCache[j + 1] = x
}
if (textCacheCap < textCacheSize - 1) {
textCacheCap++
}
// sort the list
textCache.sortBy { it.hash }
}
private fun getCache(hash: Long): TextCacheObj {
private fun getCache(hash: Long): TextCacheObj? {
var low = 0
var high = textCacheCap
var high = textCacheCap - 1
var key = -1
@@ -366,34 +377,14 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
}
if (key < 0)
throw NullPointerException("No element found")
return null
// increment age count (see: addToCache(CodepointSequence, Pixmap, Int))
textCache[key].age += 1
return textCache[key]
}
private fun cacheContains(hash: Long): Boolean {
var low = 0
var high = textCacheCap
var key = -1
while (low <= high) {
val mid = (low + high).ushr(1) // safe from overflows
val midVal = textCache[mid]
if (hash > midVal.hash)
low = mid + 1
else if (hash < midVal.hash)
high = mid - 1
else {
key = mid
break // the condition is not enough to break the loop
}
}
return (key >= 0)
}
private fun getColour(codePoint: Int): Int { // input: 0x10F_RGB, out: RGBA8888
if (colourBuffer.containsKey(codePoint))
@@ -660,32 +651,24 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
private var flagFirstRun = true
private var textBuffer = CodepointSequence(256)
//private var oldCharSequence = ""
//private var posXbuffer = intArrayOf() // absolute posX of glyphs from print-origin
//private var posYbuffer = intArrayOf() // absolute posY of glyphs from print-origin
private var linotype: Pixmap? = null
private lateinit var tempLinotype: Texture
private lateinit var originalColour: Color
private var nullProp = GlyphProps(15, 0)
private var pixmapTextureHolder: Texture? = null
//private var pixmapHolder: Pixmap? = null
private val pixmapOffsetY = 10
override fun draw(batch: Batch, charSeq: CharSequence, x: Float, y: Float): GlyphLayout? {
if (debug)
println("[TerrarumSansBitmap] max age: $textCacheCap")
fun Int.flipY() = this * if (flipY) 1 else -1
// always draw at integer position; this is bitmap font after all
val x = Math.round(x).toInt()//.toFloat()
val y = Math.round(y).toInt()//.toFloat()
val x = Math.round(x)
val y = Math.round(y)
originalColour = batch.color.cpy()
val mainColObj = originalColour
@@ -695,12 +678,13 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
if (charSeq.isNotBlank()) {
if (!cacheContains(charSeqHash) || flagFirstRun) {
val cacheObj = getCache(charSeqHash)
if (cacheObj == null || flagFirstRun) {
textBuffer = charSeq.toCodePoints()
val (posXbuffer, posYbuffer) = buildWidthAndPosBuffers(textBuffer)
linotype = null // use new linotype
flagFirstRun = false
//println("text not in buffer: $charSeq")
@@ -712,18 +696,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
resetHash(charSeq, x.toFloat(), y.toFloat())
//pixmapTextureHolder?.dispose() /* you CAN'T do this however */
linotype = Pixmap(posXbuffer.last(), H + (pixmapOffsetY * 2), Pixmap.Format.RGBA8888)
// TEST: does the new instance of pixmap is all zero?
/*repeat(pixmapHolder?.pixels?.capacity() ?: 0) {
if (pixmapHolder?.pixels?.get() != 0.toByte()) {
throw InternalError("pixmap is not all zero, wtf?!")
}
}
pixmapHolder?.pixels?.rewind()*/
val linotypePixmap = Pixmap(posXbuffer.last(), H + (pixmapOffsetY * 2), Pixmap.Format.RGBA8888)
var index = 0
@@ -771,10 +744,10 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
val jungTex = hangulSheet.get(indexJung, jungRow)
val jongTex = hangulSheet.get(indexJong, jongRow)
linotype?.setColor(mainCol)
linotype?.drawPixmap(choTex, posXbuffer[index], pixmapOffsetY, mainCol)
linotype?.drawPixmap(jungTex, posXbuffer[index], pixmapOffsetY, mainCol)
linotype?.drawPixmap(jongTex, posXbuffer[index], pixmapOffsetY, mainCol)
linotypePixmap.setColor(mainCol)
linotypePixmap.drawPixmap(choTex, posXbuffer[index], pixmapOffsetY, mainCol)
linotypePixmap.drawPixmap(jungTex, posXbuffer[index], pixmapOffsetY, mainCol)
linotypePixmap.drawPixmap(jongTex, posXbuffer[index], pixmapOffsetY, mainCol)
//batch.color = mainCol
//batch.draw(choTex, x + posXbuffer[index].toFloat(), y)
@@ -797,7 +770,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
val posX = posXbuffer[index]
val texture = sheets[sheetID].get(sheetX, sheetY)
linotype?.drawPixmap(texture, posX, posY + pixmapOffsetY, mainCol)
linotypePixmap.drawPixmap(texture, posX, posY + pixmapOffsetY, mainCol)
//batch.color = mainCol
//batch.draw(texture, posX, posY)
@@ -813,34 +786,34 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
}
makeShadow(linotype)
makeShadow(linotypePixmap)
tempLinotype = Texture(linotypePixmap)
tempLinotype.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
// put things into cache
//textCache[charSeq] = ShittyGlyphLayout(textBuffer, linotype!!)
addToCache(textBuffer, linotype!!, posXbuffer.last())
addToCache(textBuffer, tempLinotype, posXbuffer.last())
linotypePixmap.dispose()
}
else {
val bufferObj = getCache(charSeqHash)
textBuffer = bufferObj.glyphLayout!!.textBuffer
linotype = bufferObj.glyphLayout!!.linotype
textBuffer = cacheObj.glyphLayout!!.textBuffer
tempLinotype = cacheObj.glyphLayout!!.linotype
}
batch.color = mainColObj
pixmapTextureHolder = Texture(linotype)
if (!flipY) {
batch.draw(pixmapTextureHolder, x.toFloat(), (y - pixmapOffsetY).toFloat())
batch.draw(tempLinotype, x.toFloat(), (y - pixmapOffsetY).toFloat())
}
else {
batch.draw(pixmapTextureHolder,
batch.draw(tempLinotype,
x.toFloat(),
(y - pixmapOffsetY + (pixmapTextureHolder?.height ?: 0)).toFloat(),
(pixmapTextureHolder?.width?.toFloat()) ?: 0f,
-(pixmapTextureHolder?.height?.toFloat() ?: 0f)
(y - pixmapOffsetY + (tempLinotype.height)).toFloat(),
(tempLinotype.width.toFloat()),
-(tempLinotype.height.toFloat())
)
}
@@ -855,7 +828,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
override fun dispose() {
super.dispose()
textCache.forEach { it.dispose() }
sheets.forEach { it.dispose() }
}
@@ -1161,11 +1134,12 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
fun getWidth(text: String) = getWidth(text.toCodePoints())
fun getWidth(s: CodepointSequence): Int {
try {
val cacheObj = getCache(s.getHash())
val cacheObj = getCache(s.getHash())
if (cacheObj != null) {
return cacheObj.glyphLayout!!.width
}
catch (e: NullPointerException) {
else {
return buildWidthAndPosBuffers(s).first.last()
}
}