blob: a295f10bd8c831a98796b0ba4a2891d663436a43 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
namespace LivingVillage.Desktop
open System
open System.IO
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
module CjkGlyphAtlas =
let columns = 16
let cellPixels = 24
// Keep this table sorted and stable so the generated PNG has a reproducible layout.
let private characterTable =
"×→一上不与业东两中为主乐乡书买了争事于互些交人今从以件价任众会传位体作使保信倍停先入关内册再农冬准几出分切到前剧力功务动助匀包北单卖南卷原去友取受口只可各同和品回围圃土在地坏块墙备夏多夜大天夫失头好始子存完定家察对将小尚就层居屋工差己已巴巷币布帮常平年庄库应庙度开式当录待得心必态怒总恼情愁意戏成或户打找报拒择指挑捕据接提播操收改效敌数文料新方无日旧时明昏易春是晓晚晨景暂更最有未村束板林果柴标格档桥歉段民水求江没法涨淡清渔游热照物状玩理瓦生用田留白的盐目看真石确社离秋移稳空竹笑第米粮精系紧纭纷线结给绝继绪续置翻耕聊职背能自范茶获菜落衅行西观认讨议记讲论设访试话询说请读谅谈败货贩资走足路跳车转轴过近返还进迫退送选速道邻郎都里量金鉴钱门闭问闯闲间闻附雨零需面页项须食饥饿香鱼黄黛,:"
let characters = characterTable.ToCharArray()
let rowCount = (characters.Length + columns - 1) / columns
let pixelWidth = columns * cellPixels
let pixelHeight = rowCount * cellPixels
let assetRelativePath = "Assets/cjk-glyph-atlas.png"
let manifestRelativePath = "Assets/cjk-glyphs.txt"
let private indices =
characters
|> Array.mapi (fun index character -> character, index)
|> Map.ofArray
let contains (character: char) = Map.containsKey character indices
let sourceRectangle (character: char) =
indices
|> Map.tryFind character
|> Option.map (fun index ->
Rectangle(
(index % columns) * cellPixels,
(index / columns) * cellPixels,
cellPixels,
cellPixels))
let runtimeAssetPath () = Path.Combine(AppContext.BaseDirectory, assetRelativePath)
let runtimeManifestPath () = Path.Combine(AppContext.BaseDirectory, manifestRelativePath)
let load (device: GraphicsDevice) : Texture2D =
let path = runtimeAssetPath ()
if not (File.Exists path) then
raise (FileNotFoundException(sprintf "CJK glyph atlas is missing: %s" path, path))
use stream = File.OpenRead path
let texture = Texture2D.FromStream(device, stream)
if texture.Width <> pixelWidth || texture.Height <> pixelHeight then
texture.Dispose()
invalidOp (sprintf "CJK glyph atlas has dimensions %dx%d; expected %dx%d." texture.Width texture.Height pixelWidth pixelHeight)
else
texture
|