20 Commits

Author SHA1 Message Date
minjaesong
73251d70fa completely dropping Slick2d stuffs 2020-01-14 16:26:10 +09:00
minjaesong
971f98beb3 shadow inversion to make recessed texts 2020-01-06 15:19:07 +09:00
minjaesong
abe1da35a0 kana minor edits, fixing wrong case 2019-10-03 01:24:39 +09:00
Minjae Song
29885f3ac9 updating demo.png 2019-05-31 04:14:32 +09:00
Minjae Song
56d2a98a5b hangul update 2019-05-30 14:10:27 +09:00
Minjae Song
43342fff00 demo: updating unicode references 2019-05-14 13:33:14 +09:00
Minjae Song
51d4dec6d3 new redesigned hangul 2019-05-14 13:17:20 +09:00
Minjae Song
81dc38d242 support for Compatibility Hangul 0x3130..0x318F 2019-05-13 02:27:56 +09:00
Minjae Song
863f9d91c8 hangul redraw wip -- less "laundry line" 2019-05-12 23:51:50 +09:00
Minjae Song
4c99cca7ff internal test mode 2019-05-12 17:20:22 +09:00
Minjae Song
b807b96b5f hangul redraw and punct adjustments 2019-05-12 17:10:05 +09:00
Minjae Song
91c9a7a99c TextureRegionPack is now Gdx.disposable 2019-03-10 16:43:32 +09:00
Minjae Song
778e2b0afb tweaks on left/right bumper labels 2019-02-23 23:48:18 +09:00
Minjae Song
c9055ef7b8 PUA: LR buttons are now xbox shaped, added xbox one's View and Menu buttons 2019-02-13 17:52:45 +09:00
Minjae Song
e60b95efb8 bgBG: one dark pixel fixed on 'zhe' 2019-02-08 09:54:54 +09:00
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
20 changed files with 190 additions and 967 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

@@ -22,9 +22,10 @@ class FontTestGDX : Game() {
lateinit var camera: OrthographicCamera
private val testing = false
private val demotextName = "demotext.txt"
private val outimageName = "demo.png"
private val demotextName = if (testing) "testtext.txt" else "demotext.txt"
private val outimageName = if (testing) "testing.PNG" else "demo.PNG"
override fun create() {
font = GameFontBase("./assets", flipY = false, errorOnUnknownChar = false) // must test for two flipY cases

View File

@@ -1,6 +1,4 @@
Terrarum Sans Bitmap
Copyright (c) 2017-2019 Minjae Song (Torvald) and the contributors
Copyright (c) 2017-2020 see CONTRIBUTORS.txt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,17 +1,18 @@
# Terrarum Sans Bitmap
![Font sample](demo.PNG)
![Font sample — necessary information in this image is also provided below.](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/B/C, IPA Extension, Greek, Cyrillic (+ Bulgarian, Serbian variants) and the supplement, Armenian, Devanagari, Bengali, Thai, Georgian (Mkhedruli and Mtavruli), General Punctuations, Super/Subscrips, CJK Punctuations, All of the Kana (minus the Hentaigana), Chinese (limited to Unihan and Ext-A), Hangul (every 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.
#### Little notes
- 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.
- All Han characters are in Chinese variant, no other variants are to be supported as most Chinese, Japanese and Korean people can understand other's variant and as long as I can, we don't bother anyway.
- Indian script in general is not perfect: right now 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
@@ -68,7 +72,7 @@ On your code (Java):
}
### Using on Slick2d
### Using on Slick2d (legacy version only)
On your code (Kotlin):
@@ -126,4 +130,4 @@ Please refer to [CONTRIBUTING.md](https://github.com/minjaesong/Terrarum-sans-bi
Thanks to kind people of [/r/Typography](https://www.reddit.com/r/typography/) for amazing feedbacks.
CJK Ideographs are powered by [WenQuanYi Font](http://wenq.org/wqy2/index.cgi?BitmapSong). The font is distributed under the GNU GPL version 2. Although, in some countries including where I'm based on, the shapes of typefaces are not copyrightable (the program codes—e.g. TTF—do), we would like to give a credit for the font and the people behind it.
CJK Ideographs are powered by [WenQuanYi Font](http://wenq.org/wqy2/index.cgi?BitmapSong). The font is distributed under the GNU GPL version 2. Although the shapes of typefaces are not copyrightable (the program codes—e.g. TTF—do), we would like to give a credit for the font and the people behind it.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 320 KiB

After

Width:  |  Height:  |  Size: 320 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 210 KiB

After

Width:  |  Height:  |  Size: 210 KiB

View File

Before

Width:  |  Height:  |  Size: 160 KiB

After

Width:  |  Height:  |  Size: 160 KiB

View File

Before

Width:  |  Height:  |  Size: 260 KiB

After

Width:  |  Height:  |  Size: 260 KiB

View File

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 324 KiB

After

Width:  |  Height:  |  Size: 324 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 200 KiB

After

Width:  |  Height:  |  Size: 200 KiB

BIN
demo.PNG

Binary file not shown.

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 146 KiB

View File

@@ -45,7 +45,7 @@ How multilingual? Real multilingual!
􏻬あめつちほしそら やまかはみねたに くもきりむろこけ ひといぬうへすゑ ゆわさるおふせよ えの𛀁をなれゐて􀀀
􏻬トリナクコヱス ユメサマセ ミヨアケワタル ヒンカシヲ ソライロハエテ オキツヘニ ホフネムレヰヌ モヤノウチ􀀀
􏻬田居に出で 菜摘むわれをぞ 君召すと 求食り追ひゆく 山城の 打酔へる子ら 藻葉干せよ え舟繋けぬ􀀀
􏻬정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날 하얬다 도럄직한 퀡봹퉪헰ꥸᅦퟗꥸᅦퟗᄋힳᆫ􀀀
􏻬정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날 하얬다 도럄직한 퀡봹퉪헰ꥸᅦퟗꥸᅦퟗᄋힳᆫㅗㅜㅑㄷ􀀀
􏻬Četri psihi faķīri vēlu vakarā zāģēja guļbūvei durvis, fonā šņācot mežam􀀀
􏻬Įlinkdama fechtuotojo špaga sublykčiojusi pragręžė apvalų arbūzą􀀀
􏻬Ѕидарски пејзаж: шугав билмез со чудење џвака ќофте и кељ на туѓ цех􀀀
@@ -108,8 +108,9 @@ How multilingual? Real multilingual!
Basic Latin Latin-1 Supplement Latin Extended-A Latin Extended-B IPA Extension Greek Cyrillic
Cyrillic Supplement Armenian Devanagari Bengali Thai Georgian Hangul Jamo Cherokee Runic
Georgian Extended Greek Extended General Punctuations Superscripts and Subscripts CJK Symbols
Latin Extended-C Kana Kana Phonetic Extension CJK Unihan Extension A CJK Unihan
Hangul Jamo Extended-A Hangul Syllables Hangul Jamo Extended-B Fullwidth Forms Kana Supplement
Latin Extended-C Kana Hangul Compatibility Jamo Kana Phonetic Extensions CJK Unihan Extension A
CJK Unihan Hangul Jamo Extended-A Hangul Syllables Hangul Jamo Extended-B Fullwidth Forms
Kana Supplement
GitHubs issue page is open! You can report any 􏽕errors􀀀, or leave 􏽕suggestions􀀀.
You can help this font to be more versatile. (for more languages, more frameworks) 􏽕Clone􀀀 this repo, make

View File

@@ -1,7 +1,7 @@
/*
* Terrarum Sans Bitmap
*
* Copyright (c) 2017-2018 Minjae Song (Torvald)
* Copyright (c) 2017-2020 Minjae Song (Torvald)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -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,20 +82,33 @@ 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 invertShadow: 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 //
private fun getWanseongHanChosung(hanIndex: Int) = hanIndex / (JUNG_COUNT * JONG_COUNT)
private fun getWanseongHanChoseong(hanIndex: Int) = hanIndex / (JUNG_COUNT * JONG_COUNT)
private fun getWanseongHanJungseong(hanIndex: Int) = hanIndex / JONG_COUNT % JUNG_COUNT
private fun getWanseongHanJongseong(hanIndex: Int) = hanIndex % JONG_COUNT
@@ -103,35 +119,35 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
private fun isJungseongComplex(hanIndex: Int) = jungseongComplex.binarySearch(hanIndex) >= 0
/**
* @param i Initial (Chosung)
* @param i Initial (Choseong)
* @param p Peak (Jungseong)
* @param f Final (Jongseong)
*/
private fun getHanInitialRow(i: Int, p: Int, f: Int): Int {
val ret =
if (isJungseongWide(p)) 2
else if (isJungseongComplex(p)) 4
else 0
if (isJungseongWide(p)) 3
else if (isJungseongComplex(p)) 5
else 1
return if (f == 0) ret else ret + 1
}
private fun getHanMedialRow(i: Int, p: Int, f: Int) = if (f == 0) 6 else 7
private fun getHanMedialRow(i: Int, p: Int, f: Int) = if (f == 0) 7 else 8
private fun getHanFinalRow(i: Int, p: Int, f: Int): Int {
return if (isJungseongWide(p))
8
else
9
else
10
}
private fun isHangulChosung(c: CodePoint) = c in (0x1100..0x115F) || c in (0xA960..0xA97F)
private fun isHangulChoseong(c: CodePoint) = c in (0x1100..0x115F) || c in (0xA960..0xA97F)
private fun isHangulJungseong(c: CodePoint) = c in (0x1160..0x11A7) || c in (0xD7B0..0xD7C6)
private fun isHangulJongseong(c: CodePoint) = c in (0x11A8..0x11FF) || c in (0xD7CB..0xD7FB)
private fun toHangulChosungIndex(c: CodePoint) =
if (!isHangulChosung(c)) throw IllegalArgumentException("This Hangul sequence does not begin with Chosung (${c.toHex()})")
private fun toHangulChoseongIndex(c: CodePoint) =
if (!isHangulChoseong(c)) throw IllegalArgumentException("This Hangul sequence does not begin with Choseong (${c.toHex()})")
else if (c in 0x1100..0x115F) c - 0x1100
else c - 0xA960 + 96
private fun toHangulJungseongIndex(c: CodePoint) =
@@ -146,32 +162,44 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
/**
* X-position in the spritesheet
*
* @param iCP Code point for Initial (Chosung)
* @param iCP Code point for Initial (Choseong)
* @param pCP Code point for Peak (Jungseong)
* @param fCP Code point for Final (Jongseong
* @param fCP Code point for Final (Jongseong)
*/
private fun toHangulIndex(iCP: CodePoint, pCP: CodePoint, fCP: CodePoint): IntArray {
val indexI = toHangulChosungIndex(iCP)
val indexI = toHangulChoseongIndex(iCP)
val indexP = toHangulJungseongIndex(pCP)
val indexF = toHangulJongseongIndex(fCP)
return intArrayOf(indexI, indexP, indexF)
}
/**
* @param iCP 0x1100..0x115F, 0xA960..0xA97F, 0x3130..0x318F
* @param pCP 0x00, 0x1160..0x11A7, 0xD7B0..0xD7CA
* @param fCP 0x00, 0x11A8..0x11FF, 0xD7BB..0xD7FF
*
* @return IntArray pair representing Hangul indices and rows (in this order)
*/
private fun toHangulIndexAndRow(iCP: CodePoint, pCP: CodePoint, fCP: CodePoint): Pair<IntArray, IntArray> {
val (indexI, indexP, indexF) = toHangulIndex(iCP, pCP, fCP)
if (isHangulCompat(iCP)) {
return intArrayOf(iCP - 0x3130, 0, 0) to intArrayOf(0, 7, 9)
}
else {
val (indexI, indexP, indexF) = toHangulIndex(iCP, pCP, fCP)
val rowI = getHanInitialRow(indexI, indexP, indexF)
val rowP = getHanMedialRow(indexI, indexP, indexF)
val rowF = getHanFinalRow(indexI, indexP, indexF)
val rowI = getHanInitialRow(indexI, indexP, indexF)
val rowP = getHanMedialRow(indexI, indexP, indexF)
val rowF = getHanFinalRow(indexI, indexP, indexF)
return intArrayOf(indexI, indexP, indexF) to intArrayOf(rowI, rowP, rowF)
return intArrayOf(indexI, indexP, indexF) to intArrayOf(rowI, rowP, rowF)
}
}
// END Hangul //
private fun isHangul(c: CodePoint) = c in codeRange[SHEET_HANGUL]
private fun isHangul(c: CodePoint) = c in codeRange[SHEET_HANGUL] || c in 0x3130..0x318F
private fun isAscii(c: CodePoint) = c in codeRange[SHEET_ASCII_VARW]
private fun isRunic(c: CodePoint) = c in codeRange[SHEET_RUNIC]
private fun isExtA(c: CodePoint) = c in codeRange[SHEET_EXTA_VARW]
@@ -202,8 +230,10 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
private fun isDiacriticalMarks(c: CodePoint) = c in codeRange[SHEET_DIACRITICAL_MARKS_VARW]
private fun isPolytonicGreek(c: CodePoint) = c in codeRange[SHEET_GREEK_POLY_VARW]
private fun isExtC(c: CodePoint) = c in codeRange[SHEET_EXTC_VARW]
private fun isHangulCompat(c: CodePoint) = c in codeRangeHangulCompat
private fun isCaps(c: CodePoint) = Character.isUpperCase(c) || isKartvelianCaps(c)
// underscored name: not a charset
private fun _isCaps(c: CodePoint) = Character.isUpperCase(c) || isKartvelianCaps(c)
private fun extAindexX(c: CodePoint) = (c - 0x100) % 16
@@ -288,11 +318,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 +338,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 +401,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))
@@ -504,6 +519,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
0x1F00..0x1FFF, // SHEET_GREEK_POLY_VARW
0x2C60..0x2C7F // SHEET_EXTC_VARW
)
private val codeRangeHangulCompat = 0x3130..0x318F
/** Props of all printable Unicode points. */
private val glyphProps: HashMap<CodePoint, GlyphProps> = HashMap()
private val sheets: Array<PixmapRegionPack>
@@ -660,32 +676,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 +703,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 +721,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 +769,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 +795,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 +811,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 +853,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() }
}
@@ -1138,6 +1136,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
this.codeRange[SHEET_CUSTOM_SYM].forEach { glyphProps[it] = GlyphProps(20, 0) }
this.codeRange[SHEET_FW_UNI].forEach { glyphProps[it] = GlyphProps(W_UNIHAN, 0) }
this.codeRange[SHEET_HANGUL].forEach { glyphProps[it] = GlyphProps(W_HANGUL, 0) }
codeRangeHangulCompat.forEach { glyphProps[it] = GlyphProps(W_HANGUL, 0) }
this.codeRange[SHEET_KANA].forEach { glyphProps[it] = GlyphProps(W_KANA, 0) }
this.codeRange[SHEET_RUNIC].forEach { glyphProps[it] = GlyphProps(9, 0) }
this.codeRange[SHEET_UNIHAN].forEach { glyphProps[it] = GlyphProps(W_UNIHAN, 0) }
@@ -1161,11 +1160,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()
}
}
@@ -1213,8 +1213,8 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
}
if (isHangul(thisChar) && !isHangulChosung(thisChar)) {
posXbuffer[charIndex] = if (isHangulChosung(lastNonDiacriticChar))
if (isHangul(thisChar) && !isHangulChoseong(thisChar) && !isHangulCompat(thisChar)) {
posXbuffer[charIndex] = if (isHangulChoseong(lastNonDiacriticChar) || isHangulCompat(lastNonDiacriticChar))
posXbuffer[nonDiacriticCounter]
else
posXbuffer[nonDiacriticCounter] + W_HANGUL
@@ -1365,7 +1365,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
// disassemble Hangul Syllables into Initial-Peak-Final encoding
else if (c in 0xAC00.toChar()..0xD7A3.toChar()) {
val cInt = c.toInt() - 0xAC00
val indexCho = getWanseongHanChosung(cInt)
val indexCho = getWanseongHanChoseong(cInt)
val indexJung = getWanseongHanJungseong(cInt)
val indexJong = getWanseongHanJongseong(cInt) - 1 // no Jongseong will be -1
@@ -1483,10 +1483,14 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
//
// for now, no semitransparency (in colourcode && spritesheet)
val jobQueue = arrayOf(
val jobQueue = if (!invertShadow) arrayOf(
1 to 0,
0 to 1,
1 to 1
) else arrayOf(
-1 to 0,
0 to -1,
-1 to -1
)
jobQueue.forEach {
@@ -1646,7 +1650,7 @@ class GameFontBase(fontDir: String, val noShadow: Boolean = false, val flipY: Bo
internal val JONG_COUNT = 28
internal val W_ASIAN_PUNCT = 10
internal val W_HANGUL = 12
internal val W_HANGUL = 13
internal val W_KANA = 12
internal val W_UNIHAN = 16
internal val W_LATIN_WIDE = 9 // width of regular letters

View File

@@ -27,6 +27,7 @@ package net.torvald.terrarumsansbitmap.gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.utils.Disposable
/**
* Created by minjaesong on 2017-06-15.
@@ -40,7 +41,7 @@ class TextureRegionPack(
val hFrame: Int = 0,
val vFrame: Int = 0,
val xySwapped: Boolean = false // because Unicode chart does, duh
) {
): Disposable {
constructor(ref: String, tileW: Int, tileH: Int, hGap: Int = 0, vGap: Int = 0, hFrame: Int = 0, vFrame: Int = 0, xySwapped: Boolean = false) :
this(Texture(ref), tileW, tileH, hGap, vGap, hFrame, vFrame, xySwapped)
@@ -61,7 +62,7 @@ class TextureRegionPack(
//println("texture: $texture, dim: ${texture.width} x ${texture.height}, grid: $horizontalCount x $verticalCount, cellDim: $tileW x $tileH")
if (!xySwapped) {
regions = Array<TextureRegion>(horizontalCount * verticalCount, {
regions = Array<TextureRegion>(horizontalCount * verticalCount) {
val region = TextureRegion()
val rx = (it % horizontalCount * (tileW + hGap)) + hFrame
val ry = (it / horizontalCount * (tileH + vGap)) + vFrame
@@ -72,10 +73,10 @@ class TextureRegionPack(
region.flip(false, globalFlipY)
/*return*/region
})
}
}
else {
regions = Array<TextureRegion>(horizontalCount * verticalCount, {
regions = Array<TextureRegion>(horizontalCount * verticalCount) {
val region = TextureRegion()
val rx = (it / verticalCount * (tileW + hGap)) + hFrame
val ry = (it % verticalCount * (tileH + vGap)) + vFrame
@@ -86,13 +87,13 @@ class TextureRegionPack(
region.flip(false, globalFlipY)
/*return*/region
})
}
}
}
fun get(x: Int, y: Int) = regions[y * horizontalCount + x]
fun dispose() {
override fun dispose() {
texture.dispose()
}

View File

@@ -1,790 +0,0 @@
/*
* Terrarum Sans Bitmap
*
* Copyright (c) 2017 Minjae Song (Torvald)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.torvald.terrarumsansbitmap.slick2d
/*import net.torvald.terrarumsansbitmap.gdx.GameFontBase
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.JUNG_COUNT
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.JONG_COUNT
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_ASIAN_PUNCT
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_HANGUL
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_KANA
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_UNIHAN
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_LATIN_WIDE
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.W_VAR_INIT
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.HGAP_VAR
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.H
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.H_UNIHAN
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SIZE_CUSTOM_SYM
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_ASCII_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_HANGUL
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_EXTA_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_EXTB_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_KANA
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_CJK_PUNCT
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_UNIHAN
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_CYRILIC_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_FW_UNI
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_UNI_PUNCT_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_GREEK_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_THAI_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_HAYEREN_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_KARTULI_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_IPA_VARW
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_CUSTOM_SYM
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_UNKNOWN
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_RUNIC
import net.torvald.terrarumsansbitmap.gdx.GameFontBase.Companion.SHEET_LATIN_EXT_ADD_VARW
import org.newdawn.slick.Color
import org.newdawn.slick.Font
import org.newdawn.slick.Image
import org.newdawn.slick.SpriteSheet
import org.newdawn.slick.opengl.Texture
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
import java.util.zip.GZIPInputStream
/**
* LibGDX->Slick2D back-port of Terrarum Sans Bitmap implementation
*
* Filename and Extension for the spritesheet is hard-coded, which are:
*
* - ascii_variable.tga
* - hangul_johab.tga
* - LatinExtA_variable.tga
* - LatinExtB_variable.tga
* - kana.tga
* - cjkpunct.tga
* - wenquanyi.tga.gz
* - cyrillic_variable.tga
* - fullwidth_forms.tga
* - unipunct_variable.tga
* - greek_variable.tga
* - thai_variable.tga
* - puae000-e0ff.tga
*
*
* 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.
*
* @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```.
*
* Created by minjaesong on 2017-06-15.
*/
class GameFontBase(fontDir: String, val noShadow: Boolean = false) : Font {
private fun getHanChosung(hanIndex: Int) = hanIndex / (JUNG_COUNT * JONG_COUNT)
private fun getHanJungseong(hanIndex: Int) = hanIndex / JONG_COUNT % JUNG_COUNT
private fun getHanJongseong(hanIndex: Int) = hanIndex % JONG_COUNT
private val jungseongWide = arrayOf(8, 12, 13, 17, 18, 21)
private val jungseongComplex = arrayOf(9, 10, 11, 14, 15, 16, 22)
private fun isJungseongWide(hanIndex: Int) = jungseongWide.contains(getHanJungseong(hanIndex))
private fun isJungseongComplex(hanIndex: Int) = jungseongComplex.contains(getHanJungseong(hanIndex))
private fun getHanInitialRow(hanIndex: Int): Int {
val ret: Int
if (isJungseongWide(hanIndex))
ret = 2
else if (isJungseongComplex(hanIndex))
ret = 4
else
ret = 0
return if (getHanJongseong(hanIndex) == 0) ret else ret + 1
}
private fun getHanMedialRow(hanIndex: Int) = if (getHanJongseong(hanIndex) == 0) 6 else 7
private fun getHanFinalRow(hanIndex: Int): Int {
val jungseongIndex = getHanJungseong(hanIndex)
return if (jungseongWide.contains(jungseongIndex))
8
else
9
}
private fun isHangul(c: Char) = c.toInt() in codeRange[SHEET_HANGUL]
private fun isAscii(c: Char) = c.toInt() in codeRange[SHEET_ASCII_VARW]
private fun isRunic(c: Char) = c.toInt() in codeRange[SHEET_RUNIC]
private fun isExtA(c: Char) = c.toInt() in codeRange[SHEET_EXTA_VARW]
private fun isExtB(c: Char) = c.toInt() in codeRange[SHEET_EXTB_VARW]
private fun isKana(c: Char) = c.toInt() in codeRange[SHEET_KANA]
private fun isCJKPunct(c: Char) = c.toInt() in codeRange[SHEET_CJK_PUNCT]
private fun isUniHan(c: Char) = c.toInt() in codeRange[SHEET_UNIHAN]
private fun isCyrilic(c: Char) = c.toInt() in codeRange[SHEET_CYRILIC_VARW]
private fun isFullwidthUni(c: Char) = c.toInt() in codeRange[SHEET_FW_UNI]
private fun isUniPunct(c: Char) = c.toInt() in codeRange[SHEET_UNI_PUNCT_VARW]
private fun isGreek(c: Char) = c.toInt() in codeRange[SHEET_GREEK_VARW]
private fun isThai(c: Char) = c.toInt() in codeRange[SHEET_THAI_VARW]
private fun isDiacritics(c: Char) = c.toInt() in 0xE34..0xE3A
|| c.toInt() in 0xE47..0xE4E
|| c.toInt() == 0xE31
private fun isCustomSym(c: Char) = c.toInt() in codeRange[SHEET_CUSTOM_SYM]
private fun isArmenian(c: Char) = c.toInt() in codeRange[SHEET_HAYEREN_VARW]
private fun isKartvelian(c: Char) = c.toInt() in codeRange[SHEET_KARTULI_VARW]
private fun isIPA(c: Char) = c.toInt() in codeRange[SHEET_IPA_VARW]
private fun isColourCodeHigh(c: Char) = c.toInt() in 0b110110_1111000000..0b110110_1111111111 // only works with JVM (which uses UTF-16 internally)
private fun isColourCodeLow(c: Char) = c.toInt() in 0b110111_0000000000..0b110111_1111111111 // only works with JVM (which uses UTF-16 internally)
private fun isLatinExtAdd(c: Char) = c.toInt() in 0x1E00..0x1EFF
private fun extAindexX(c: Char) = (c.toInt() - 0x100) % 16
private fun extAindexY(c: Char) = (c.toInt() - 0x100) / 16
private fun extBindexX(c: Char) = (c.toInt() - 0x180) % 16
private fun extBindexY(c: Char) = (c.toInt() - 0x180) / 16
private fun runicIndexX(c: Char) = (c.toInt() - 0x16A0) % 16
private fun runicIndexY(c: Char) = (c.toInt() - 0x16A0) / 16
private fun kanaIndexX(c: Char) = (c.toInt() - 0x3040) % 16
private fun kanaIndexY(c: Char) = (c.toInt() - 0x3040) / 16
private fun cjkPunctIndexX(c: Char) = (c.toInt() - 0x3000) % 16
private fun cjkPunctIndexY(c: Char) = (c.toInt() - 0x3000) / 16
private fun cyrilicIndexX(c: Char) = (c.toInt() - 0x400) % 16
private fun cyrilicIndexY(c: Char) = (c.toInt() - 0x400) / 16
private fun fullwidthUniIndexX(c: Char) = (c.toInt() - 0xFF00) % 16
private fun fullwidthUniIndexY(c: Char) = (c.toInt() - 0xFF00) / 16
private fun uniPunctIndexX(c: Char) = (c.toInt() - 0x2000) % 16
private fun uniPunctIndexY(c: Char) = (c.toInt() - 0x2000) / 16
private fun unihanIndexX(c: Char) = (c.toInt() - 0x3400) % 256
private fun unihanIndexY(c: Char) = (c.toInt() - 0x3400) / 256
private fun greekIndexX(c: Char) = (c.toInt() - 0x370) % 16
private fun greekIndexY(c: Char) = (c.toInt() - 0x370) / 16
private fun thaiIndexX(c: Char) = (c.toInt() - 0xE00) % 16
private fun thaiIndexY(c: Char) = (c.toInt() - 0xE00) / 16
private fun symbolIndexX(c: Char) = (c.toInt() - 0xE000) % 16
private fun symbolIndexY(c: Char) = (c.toInt() - 0xE000) / 16
private fun armenianIndexX(c: Char) = (c.toInt() - 0x530) % 16
private fun armenianIndexY(c: Char) = (c.toInt() - 0x530) / 16
private fun kartvelianIndexX(c: Char) = (c.toInt() - 0x10D0) % 16
private fun kartvelianIndexY(c: Char) = (c.toInt() - 0x10D0) / 16
private fun ipaIndexX(c: Char) = (c.toInt() - 0x250) % 16
private fun ipaIndexY(c: Char) = (c.toInt() - 0x250) / 16
private fun latinExtAddX(c: Char) = (c.toInt() - 0x1E00) % 16
private fun latinExtAddY(c: Char) = (c.toInt() - 0x1E00) / 16
private fun getColour(charHigh: Char, charLow: Char): Color { // input: 0x10ARGB, out: RGBA8888
val codePoint = Character.toCodePoint(charHigh, charLow)
if (colourBuffer.containsKey(codePoint))
return colourBuffer[codePoint]!!
val r = codePoint.and(0xF000).ushr(12)
val g = codePoint.and(0x0F00).ushr(8)
val b = codePoint.and(0x00F0).ushr(4)
val a = codePoint.and(0x000F)
val col = Color(a.shl(28) or a.shl(24) or r.shl(20) or r.shl(16) or g.shl(12) or g.shl(8) or b.shl(4) or b)
colourBuffer[codePoint] = col
return col
}
private val colourBuffer = HashMap<Int, Color>()
private val unihanWidthSheets = arrayOf(
SHEET_UNIHAN,
SHEET_FW_UNI
)
private val variableWidthSheets = arrayOf(
SHEET_ASCII_VARW,
SHEET_EXTA_VARW,
SHEET_EXTB_VARW,
SHEET_CYRILIC_VARW,
SHEET_UNI_PUNCT_VARW,
SHEET_GREEK_VARW,
SHEET_THAI_VARW,
SHEET_HAYEREN_VARW,
SHEET_KARTULI_VARW,
SHEET_IPA_VARW,
SHEET_LATIN_EXT_ADD_VARW
)
private val fontParentDir = if (fontDir.endsWith('/') || fontDir.endsWith('\\')) fontDir else "$fontDir/"
private val fileList = arrayOf( // MUST BE MATCHING WITH SHEET INDICES!!
"ascii_variable.tga",
"hangul_johab.tga",
"latinExtA_variable.tga",
"latinExtB_variable.tga",
"kana.tga",
"cjkpunct.tga",
"wenquanyi.tga.gz",
"cyrilic_variable.tga",
"fullwidth_forms.tga",
"unipunct_variable.tga",
"greek_variable.tga",
"thai_variable.tga",
"hayeren_variable.tga",
"kartuli_variable.tga",
"ipa_ext_variable.tga",
"futhark.tga",
"latinExt_additional_variable.tga",
"puae000-e0ff.tga"
)
private val cyrilic_bg = "cyrilic_bulgarian_variable.tga"
private val cyrilic_sr = "cyrilic_serbian_variable.tga"
private val codeRange = arrayOf( // MUST BE MATCHING WITH SHEET INDICES!!
0..0xFF,
0xAC00..0xD7A3,
0x100..0x17F,
0x180..0x24F,
0x3040..0x30FF,
0x3000..0x303F,
0x3400..0x9FFF,
0x400..0x52F,
0xFF00..0xFF1F,
0x2000..0x205F,
0x370..0x3CE,
0xE00..0xE5F,
0x530..0x58F,
0x10D0..0x10FF,
0x250..0x2AF,
0x16A0..0x16FF,
0x1E00..0x1EFF,
0xE000..0xE0FF
)
private val glyphWidths: HashMap<Int, Int> = HashMap() // if the value is negative, it's diacritics
private val sheets: Array<SpriteSheet>
init {
val sheetsPack = ArrayList<SpriteSheet>()
// first we create pixmap to read pixels, then make texture using pixmap
fileList.forEachIndexed { index, it ->
val isVariable1 = it.endsWith("_variable.tga")
val isVariable2 = variableWidthSheets.contains(index)
val isVariable = isVariable1 && isVariable2
// idiocity check
if (isVariable1 && !isVariable2)
throw Error("[TerrarumSansBitmap] font is named as variable on the name but not enlisted as")
else if (!isVariable1 && isVariable2)
throw Error("[TerrarumSansBitmap] font is enlisted as variable on the name but not named as")
val image: Image
// unpack gz if applicable
if (it.endsWith(".gz")) {
val gzi = GZIPInputStream(FileInputStream(fontParentDir + it))
val wholeFile = gzi.readBytes()
gzi.close()
val fos = BufferedOutputStream(FileOutputStream("tmp_wenquanyi.tga"))
fos.write(wholeFile)
fos.flush()
fos.close()
image = Image("tmp_wenquanyi.tga")
File("tmp_wenquanyi.tga").delete()
}
else {
image = Image(fontParentDir + it)
}
val texture = image.texture
if (isVariable) {
println("[TerrarumSansBitmap] loading texture $it [VARIABLE]")
buildWidthTable(texture, codeRange[index], 16)
}
else {
println("[TerrarumSansBitmap] loading texture $it")
}
val texRegPack = if (isVariable) {
SpriteSheet(image, W_VAR_INIT, H - 1, HGAP_VAR)
}
else if (index == SHEET_UNIHAN) {
SpriteSheet(image, W_UNIHAN, H_UNIHAN) // the only exception that is height is 16
}
// below they all have height of 20 'H'
else if (index == SHEET_FW_UNI) {
SpriteSheet(image, W_UNIHAN, H)
}
else if (index == SHEET_CJK_PUNCT) {
SpriteSheet(image, W_ASIAN_PUNCT, H)
}
else if (index == SHEET_KANA) {
SpriteSheet(image, W_KANA, H)
}
else if (index == SHEET_HANGUL) {
SpriteSheet(image, W_HANGUL, H)
}
else if (index == SHEET_CUSTOM_SYM) {
SpriteSheet(image, SIZE_CUSTOM_SYM, SIZE_CUSTOM_SYM) // TODO variable
}
else if (index == SHEET_RUNIC) {
SpriteSheet(image, W_LATIN_WIDE, H)
}
else throw IllegalArgumentException("[TerrarumSansBitmap] Unknown sheet index: $index")
sheetsPack.add(texRegPack)
}
sheets = sheetsPack.toTypedArray()
}
private var localeBuffer = ""
fun reload(locale: String) {
if (!localeBuffer.startsWith("ru") && locale.startsWith("ru")) {
val image = Image(fontParentDir + fileList[SHEET_CYRILIC_VARW])
sheets[SHEET_CYRILIC_VARW].destroy()
sheets[SHEET_CYRILIC_VARW] = SpriteSheet(image, W_VAR_INIT, H, HGAP_VAR, 0)
}
else if (!localeBuffer.startsWith("bg") && locale.startsWith("bg")) {
val image = Image(fontParentDir + cyrilic_bg)
sheets[SHEET_CYRILIC_VARW].destroy()
sheets[SHEET_CYRILIC_VARW] = SpriteSheet(image, W_VAR_INIT, H, HGAP_VAR, 0)
}
else if (!localeBuffer.startsWith("sr") && locale.startsWith("sr")) {
val image = Image(fontParentDir + cyrilic_sr)
sheets[SHEET_CYRILIC_VARW].destroy()
sheets[SHEET_CYRILIC_VARW] = SpriteSheet(image, W_VAR_INIT, H, HGAP_VAR, 0)
}
localeBuffer = locale
}
override fun getLineHeight(): Int = H
override fun getHeight(p0: String) = lineHeight
private val offsetUnihan = (H - H_UNIHAN) / 2
private val offsetCustomSym = (H - SIZE_CUSTOM_SYM) / 2
private var textBuffer: CharSequence = ""
private var textBWidth = intArrayOf() // absolute posX of glyphs from print-origin
private var textBGSize = intArrayOf() // width of each glyph
override fun drawString(x: Float, y: Float, str: String) {
drawString(x, y, str, Color.white)
}
override fun drawString(p0: Float, p1: Float, p2: String?, p3: Color?, p4: Int, p5: Int) {
throw UnsupportedOperationException()
}
override fun drawString(x: Float, y: Float, str: String, color: Color) {
// always draw at integer position; this is bitmap font after all
val x = Math.round(x).toFloat()
val y = Math.round(y).toFloat()
if (textBuffer != str) {
textBuffer = str
val widths = getWidthOfCharSeq(str)
textBGSize = widths
textBWidth = IntArray(str.length, { charIndex ->
if (charIndex == 0)
0
else {
var acc = 0
(0..charIndex - 1).forEach { acc += maxOf(0, widths[it]) } // don't accumulate diacrtics (which has negative value)
/*return*/acc
}
})
}
//print("[TerrarumSansBitmap] widthTable for $textBuffer: ")
//textBWidth.forEach { print("$it ") }; println()
var mainCol = color
var shadowCol = color.darker(0.5f)
var index = 0
while (index <= textBuffer.lastIndex) {
val c = textBuffer[index]
val sheetID = getSheetType(c)
val sheetXY = getSheetwisePosition(c)
//println("[TerrarumSansBitmap] sprite: $sheetID:${sheetXY[0]}x${sheetXY[1]}")
if (isColourCodeHigh(c)) {
val cchigh = c
val cclow = textBuffer[index + 1]
if (Character.toCodePoint(cchigh, cclow) == 0x100000) {
mainCol = color
shadowCol = color.darker(0.5f)
}
else {
mainCol = getColour(cchigh, cclow)
shadowCol = mainCol.darker(0.5f)
}
index += 1
}
else if (isColourCodeLow(c)) {
throw Error("Unexpected encounter of ColourCodeLow at index $index of String '$textBuffer'")
}
else if (sheetID == SHEET_HANGUL) {
val hangulSheet = sheets[SHEET_HANGUL]
val hIndex = c.toInt() - 0xAC00
val indexCho = getHanChosung(hIndex)
val indexJung = getHanJungseong(hIndex)
val indexJong = getHanJongseong(hIndex)
val choRow = getHanInitialRow(hIndex)
val jungRow = getHanMedialRow(hIndex)
val jongRow = getHanFinalRow(hIndex)
if (!noShadow) {
hangulSheet.getSubImage(indexCho, choRow ).draw(x + textBWidth[index] + 1, y, shadowCol)
hangulSheet.getSubImage(indexCho, choRow ).draw(x + textBWidth[index] , y, shadowCol)
hangulSheet.getSubImage(indexCho, choRow ).draw(x + textBWidth[index] + 1, y, shadowCol)
hangulSheet.getSubImage(indexJung, jungRow).draw(x + textBWidth[index] + 1, y, shadowCol)
hangulSheet.getSubImage(indexJung, jungRow).draw(x + textBWidth[index] , y, shadowCol)
hangulSheet.getSubImage(indexJung, jungRow).draw(x + textBWidth[index] + 1, y, shadowCol)
hangulSheet.getSubImage(indexJong, jongRow).draw(x + textBWidth[index] + 1, y, shadowCol)
hangulSheet.getSubImage(indexJong, jongRow).draw(x + textBWidth[index] , y, shadowCol)
hangulSheet.getSubImage(indexJong, jongRow).draw(x + textBWidth[index] + 1, y, shadowCol)
}
hangulSheet.getSubImage(indexCho, choRow ).draw(x + textBWidth[index], y, mainCol)
hangulSheet.getSubImage(indexJung, jungRow).draw(x + textBWidth[index], y, mainCol)
hangulSheet.getSubImage(indexJong, jongRow).draw(x + textBWidth[index], y, mainCol)
}
else {
try {
val offset = if (!isDiacritics(c)) 0 else {
if (index > 0) // LIMITATION: does not support double (or more) diacritics properly
(textBGSize[index] - textBGSize[index - 1]) / 2
else
textBGSize[index]
}
if (!noShadow) {
sheets[sheetID].getSubImage(sheetXY[0], sheetXY[1]).draw(
x + textBWidth[index] + 1 + offset,
y + (if (sheetID == SHEET_UNIHAN) // evil exceptions
offsetUnihan
else if (sheetID == SHEET_CUSTOM_SYM)
offsetCustomSym
else
0),
shadowCol
)
sheets[sheetID].getSubImage(sheetXY[0], sheetXY[1]).draw(
x + textBWidth[index] + offset,
y + (if (sheetID == SHEET_UNIHAN) // evil exceptions
offsetUnihan + 1
else if (sheetID == SHEET_CUSTOM_SYM)
offsetCustomSym + 1
else
1),
shadowCol
)
sheets[sheetID].getSubImage(sheetXY[0], sheetXY[1]).draw(
x + textBWidth[index] + 1 + offset,
y + (if (sheetID == SHEET_UNIHAN) // evil exceptions
offsetUnihan + 1
else if (sheetID == SHEET_CUSTOM_SYM)
offsetCustomSym + 1
else
1),
shadowCol
)
}
sheets[sheetID].getSubImage(sheetXY[0], sheetXY[1]).draw(
x + textBWidth[index] + offset,
y + if (sheetID == SHEET_UNIHAN) // evil exceptions
offsetUnihan
else if (sheetID == SHEET_CUSTOM_SYM)
offsetCustomSym
else 0,
mainCol
)
}
catch (noSuchGlyph: ArrayIndexOutOfBoundsException) {
}
}
index += 1
}
}
fun dispose() {
sheets.forEach { it.destroy() }
}
private fun getWidthOfCharSeq(s: CharSequence): IntArray {
val len = IntArray(s.length)
for (i in 0..s.lastIndex) {
val chr = s[i]
val ctype = getSheetType(s[i])
if (variableWidthSheets.contains(ctype)) {
if (!glyphWidths.containsKey(chr.toInt())) {
println("[TerrarumSansBitmap] no width data for glyph number ${Integer.toHexString(chr.toInt()).toUpperCase()}")
len[i] = W_LATIN_WIDE
}
len[i] = glyphWidths[chr.toInt()]!!
}
else if (isColourCodeHigh(chr) || isColourCodeLow(chr))
len[i] = 0
else if (ctype == SHEET_CJK_PUNCT)
len[i] = W_ASIAN_PUNCT
else if (ctype == SHEET_HANGUL)
len[i] = W_HANGUL
else if (ctype == SHEET_KANA)
len[i] = W_KANA
else if (unihanWidthSheets.contains(ctype))
len[i] = W_UNIHAN
else if (ctype == SHEET_CUSTOM_SYM)
len[i] = SIZE_CUSTOM_SYM
else
len[i] = W_LATIN_WIDE
if (scale > 1) len[i] *= scale
if (i < s.lastIndex) len[i] += interchar
}
return len
}
private fun getSheetType(c: Char): Int {
if (isHangul(c))
return SHEET_HANGUL
else if (isKana(c))
return SHEET_KANA
else if (isUniHan(c))
return SHEET_UNIHAN
else if (isAscii(c))
return SHEET_ASCII_VARW
else if (isExtA(c))
return SHEET_EXTA_VARW
else if (isExtB(c))
return SHEET_EXTB_VARW
else if (isCyrilic(c))
return SHEET_CYRILIC_VARW
else if (isUniPunct(c))
return SHEET_UNI_PUNCT_VARW
else if (isCJKPunct(c))
return SHEET_CJK_PUNCT
else if (isFullwidthUni(c))
return SHEET_FW_UNI
else if (isGreek(c))
return SHEET_GREEK_VARW
else if (isThai(c))
return SHEET_THAI_VARW
else if (isCustomSym(c))
return SHEET_CUSTOM_SYM
else if (isArmenian(c))
return SHEET_HAYEREN_VARW
else if (isKartvelian(c))
return SHEET_KARTULI_VARW
else if (isIPA(c))
return SHEET_IPA_VARW
else if (isRunic(c))
return SHEET_RUNIC
else if (isLatinExtAdd(c))
return SHEET_LATIN_EXT_ADD_VARW
else
return SHEET_UNKNOWN
// fixed width
// fallback
}
private fun getSheetwisePosition(ch: Char): IntArray {
val sheetX: Int; val sheetY: Int
when (getSheetType(ch)) {
SHEET_UNIHAN -> {
sheetX = unihanIndexX(ch)
sheetY = unihanIndexY(ch)
}
SHEET_EXTA_VARW -> {
sheetX = extAindexX(ch)
sheetY = extAindexY(ch)
}
SHEET_EXTB_VARW -> {
sheetX = extBindexX(ch)
sheetY = extBindexY(ch)
}
SHEET_KANA -> {
sheetX = kanaIndexX(ch)
sheetY = kanaIndexY(ch)
}
SHEET_CJK_PUNCT -> {
sheetX = cjkPunctIndexX(ch)
sheetY = cjkPunctIndexY(ch)
}
SHEET_CYRILIC_VARW -> {
sheetX = cyrilicIndexX(ch)
sheetY = cyrilicIndexY(ch)
}
SHEET_FW_UNI -> {
sheetX = fullwidthUniIndexX(ch)
sheetY = fullwidthUniIndexY(ch)
}
SHEET_UNI_PUNCT_VARW -> {
sheetX = uniPunctIndexX(ch)
sheetY = uniPunctIndexY(ch)
}
SHEET_GREEK_VARW -> {
sheetX = greekIndexX(ch)
sheetY = greekIndexY(ch)
}
SHEET_THAI_VARW -> {
sheetX = thaiIndexX(ch)
sheetY = thaiIndexY(ch)
}
SHEET_CUSTOM_SYM -> {
sheetX = symbolIndexX(ch)
sheetY = symbolIndexY(ch)
}
SHEET_HAYEREN_VARW -> {
sheetX = armenianIndexX(ch)
sheetY = armenianIndexY(ch)
}
SHEET_KARTULI_VARW -> {
sheetX = kartvelianIndexX(ch)
sheetY = kartvelianIndexY(ch)
}
SHEET_IPA_VARW -> {
sheetX = ipaIndexX(ch)
sheetY = ipaIndexY(ch)
}
SHEET_RUNIC -> {
sheetX = runicIndexX(ch)
sheetY = runicIndexY(ch)
}
SHEET_LATIN_EXT_ADD_VARW -> {
sheetX = latinExtAddX(ch)
sheetY = latinExtAddY(ch)
}
else -> {
sheetX = ch.toInt() % 16
sheetY = ch.toInt() / 16
}
}
return intArrayOf(sheetX, sheetY)
}
fun buildWidthTable(texture: Texture, codeRange: IntRange, cols: Int = 16) {
val binaryCodeOffset = W_VAR_INIT
val cellW = W_VAR_INIT + 1
val cellH = H
for (code in codeRange) {
val cellX = ((code - codeRange.start) % cols) * cellW
val cellY = ((code - codeRange.start) / cols) * cellH
val codeStartX = cellX + binaryCodeOffset
val codeStartY = cellY
var glyphWidth = 0
for (downCtr in 0..3) {
// if ALPHA is not zero, assume it's 1
if (texture.textureData[4 * (codeStartX + (codeStartY + downCtr) * texture.textureWidth) + 3] != 0.toByte()) {
glyphWidth = glyphWidth or (1 shl downCtr)
}
}
val isDiacritics = texture.textureData[4 * (codeStartX + (codeStartY + H - 1) * texture.textureWidth) + 3] != 0.toByte()
if (isDiacritics)
glyphWidth = -glyphWidth
glyphWidths[code] = glyphWidth
}
}
override fun getWidth(text: String): Int {
return getWidthOfCharSeq(text).sum()
}
var interchar = 0
var scale = 1
set(value) {
if (value > 0) field = value
else throw IllegalArgumentException("Font scale cannot be zero or negative (input: $value)")
}
fun toColorCode(rgba4444: Int): String = GameFontBase.toColorCode(rgba4444)
fun toColorCode(r: Int, g: Int, b: Int, a: Int = 0x0F): String = toColorCode(r.shl(12) or g.shl(8) or b.shl(4) or a)
val noColorCode = toColorCode(0x0000)
companion object {
fun toColorCode(rgba4444: Int): String = Character.toChars(0x100000 + rgba4444).toColCode()
fun toColorCode(r: Int, g: Int, b: Int, a: Int = 0x0F): String = toColorCode(r.shl(12) or g.shl(8) or b.shl(4) or a)
private fun CharArray.toColCode(): String = "${this[0]}${this[1]}"
val noColorCode = toColorCode(0x0000)
}
}*/