Add 'text' package (#363)

This commit is contained in:
Hajime Hoshi 2017-07-16 03:25:57 +09:00
parent 717efd097d
commit 1e84e21857
2 changed files with 381 additions and 46 deletions

View File

@ -17,15 +17,19 @@
package main
import (
"image"
"fmt"
"image/color"
"io/ioutil"
"log"
"math/rand"
"time"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/text"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
const (
@ -34,73 +38,120 @@ const (
)
var (
textImage *ebiten.Image
sampleText = `The quick brown fox jumps over the lazy dog.`
mplusNormalFont font.Face
mplusBigFont font.Face
counter = 0
kanjiCode = rune(0)
)
var text = []string{
"The quick brown fox jumps over the lazy dog.",
"",
// A head part of a Japanese novel 山月記 (Sangetsuki)
// See http://www.aozora.gr.jp/cards/000119/files/624_14544.html.
"隴西の李徴は博学才穎、天宝の末年、",
"若くして名を虎榜に連ね、",
"ついで江南尉に補せられたが、",
"性、狷介、自ら恃むところ頗厚く、",
"賤吏に甘んずるを潔しとしなかった。",
var jaKanjis = []rune{}
func init() {
// table is the list of Japanese Kanji characters in a part of JIS X 0208.
const table = `
亜唖娃阿哀愛挨姶逢葵茜穐悪握渥旭葦芦鯵梓圧斡扱宛姐虻飴絢綾鮎或粟袷安庵按暗案闇鞍杏以伊位依偉囲夷委威尉惟意慰易椅為畏異移維緯胃萎衣謂違遺医井亥域育郁磯一壱溢逸稲茨芋鰯允印咽員因姻引飲淫胤蔭
院陰隠韻吋右宇烏羽迂雨卯鵜窺丑碓臼渦嘘唄欝蔚鰻姥厩浦瓜閏噂云運雲荏餌叡営嬰影映曳栄永泳洩瑛盈穎頴英衛詠鋭液疫益駅悦謁越閲榎厭円園堰奄宴延怨掩援沿演炎焔煙燕猿縁艶苑薗遠鉛鴛塩於汚甥凹央奥往応
押旺横欧殴王翁襖鴬鴎黄岡沖荻億屋憶臆桶牡乙俺卸恩温穏音下化仮何伽価佳加可嘉夏嫁家寡科暇果架歌河火珂禍禾稼箇花苛茄荷華菓蝦課嘩貨迦過霞蚊俄峨我牙画臥芽蛾賀雅餓駕介会解回塊壊廻快怪悔恢懐戒拐改
魁晦械海灰界皆絵芥蟹開階貝凱劾外咳害崖慨概涯碍蓋街該鎧骸浬馨蛙垣柿蛎鈎劃嚇各廓拡撹格核殻獲確穫覚角赫較郭閣隔革学岳楽額顎掛笠樫橿梶鰍潟割喝恰括活渇滑葛褐轄且鰹叶椛樺鞄株兜竃蒲釜鎌噛鴨栢茅萱
粥刈苅瓦乾侃冠寒刊勘勧巻喚堪姦完官寛干幹患感慣憾換敢柑桓棺款歓汗漢澗潅環甘監看竿管簡緩缶翰肝艦莞観諌貫還鑑間閑関陥韓館舘丸含岸巌玩癌眼岩翫贋雁頑顔願企伎危喜器基奇嬉寄岐希幾忌揮机旗既期棋棄
機帰毅気汽畿祈季稀紀徽規記貴起軌輝飢騎鬼亀偽儀妓宜戯技擬欺犠疑祇義蟻誼議掬菊鞠吉吃喫桔橘詰砧杵黍却客脚虐逆丘久仇休及吸宮弓急救朽求汲泣灸球究窮笈級糾給旧牛去居巨拒拠挙渠虚許距鋸漁禦魚亨享京
供侠僑兇競共凶協匡卿叫喬境峡強彊怯恐恭挟教橋況狂狭矯胸脅興蕎郷鏡響饗驚仰凝尭暁業局曲極玉桐粁僅勤均巾錦斤欣欽琴禁禽筋緊芹菌衿襟謹近金吟銀九倶句区狗玖矩苦躯駆駈駒具愚虞喰空偶寓遇隅串櫛釧屑屈
掘窟沓靴轡窪熊隈粂栗繰桑鍬勲君薫訓群軍郡卦袈祁係傾刑兄啓圭珪型契形径恵慶慧憩掲携敬景桂渓畦稽系経継繋罫茎荊蛍計詣警軽頚鶏芸迎鯨劇戟撃激隙桁傑欠決潔穴結血訣月件倹倦健兼券剣喧圏堅嫌建憲懸拳捲
検権牽犬献研硯絹県肩見謙賢軒遣鍵険顕験鹸元原厳幻弦減源玄現絃舷言諺限乎個古呼固姑孤己庫弧戸故枯湖狐糊袴股胡菰虎誇跨鈷雇顧鼓五互伍午呉吾娯後御悟梧檎瑚碁語誤護醐乞鯉交佼侯候倖光公功効勾厚口向
后喉坑垢好孔孝宏工巧巷幸広庚康弘恒慌抗拘控攻昂晃更杭校梗構江洪浩港溝甲皇硬稿糠紅紘絞綱耕考肯肱腔膏航荒行衡講貢購郊酵鉱砿鋼閤降項香高鴻剛劫号合壕拷濠豪轟麹克刻告国穀酷鵠黒獄漉腰甑忽惚骨狛込
此頃今困坤墾婚恨懇昏昆根梱混痕紺艮魂些佐叉唆嵯左差査沙瑳砂詐鎖裟坐座挫債催再最哉塞妻宰彩才採栽歳済災采犀砕砦祭斎細菜裁載際剤在材罪財冴坂阪堺榊肴咲崎埼碕鷺作削咋搾昨朔柵窄策索錯桜鮭笹匙冊刷
察拶撮擦札殺薩雑皐鯖捌錆鮫皿晒三傘参山惨撒散桟燦珊産算纂蚕讃賛酸餐斬暫残仕仔伺使刺司史嗣四士始姉姿子屍市師志思指支孜斯施旨枝止死氏獅祉私糸紙紫肢脂至視詞詩試誌諮資賜雌飼歯事似侍児字寺慈持時
次滋治爾璽痔磁示而耳自蒔辞汐鹿式識鴫竺軸宍雫七叱執失嫉室悉湿漆疾質実蔀篠偲柴芝屡蕊縞舎写射捨赦斜煮社紗者謝車遮蛇邪借勺尺杓灼爵酌釈錫若寂弱惹主取守手朱殊狩珠種腫趣酒首儒受呪寿授樹綬需囚収周
宗就州修愁拾洲秀秋終繍習臭舟蒐衆襲讐蹴輯週酋酬集醜什住充十従戎柔汁渋獣縦重銃叔夙宿淑祝縮粛塾熟出術述俊峻春瞬竣舜駿准循旬楯殉淳準潤盾純巡遵醇順処初所暑曙渚庶緒署書薯藷諸助叙女序徐恕鋤除傷償
勝匠升召哨商唱嘗奨妾娼宵将小少尚庄床廠彰承抄招掌捷昇昌昭晶松梢樟樵沼消渉湘焼焦照症省硝礁祥称章笑粧紹肖菖蒋蕉衝裳訟証詔詳象賞醤鉦鍾鐘障鞘上丈丞乗冗剰城場壌嬢常情擾条杖浄状畳穣蒸譲醸錠嘱埴飾
拭植殖燭織職色触食蝕辱尻伸信侵唇娠寝審心慎振新晋森榛浸深申疹真神秦紳臣芯薪親診身辛進針震人仁刃塵壬尋甚尽腎訊迅陣靭笥諏須酢図厨逗吹垂帥推水炊睡粋翠衰遂酔錐錘随瑞髄崇嵩数枢趨雛据杉椙菅頗雀裾
澄摺寸世瀬畝是凄制勢姓征性成政整星晴棲栖正清牲生盛精聖声製西誠誓請逝醒青静斉税脆隻席惜戚斥昔析石積籍績脊責赤跡蹟碩切拙接摂折設窃節説雪絶舌蝉仙先千占宣専尖川戦扇撰栓栴泉浅洗染潜煎煽旋穿箭線
繊羨腺舛船薦詮賎践選遷銭銑閃鮮前善漸然全禅繕膳糎噌塑岨措曾曽楚狙疏疎礎祖租粗素組蘇訴阻遡鼠僧創双叢倉喪壮奏爽宋層匝惣想捜掃挿掻操早曹巣槍槽漕燥争痩相窓糟総綜聡草荘葬蒼藻装走送遭鎗霜騒像増憎
臓蔵贈造促側則即息捉束測足速俗属賊族続卒袖其揃存孫尊損村遜他多太汰詑唾堕妥惰打柁舵楕陀駄騨体堆対耐岱帯待怠態戴替泰滞胎腿苔袋貸退逮隊黛鯛代台大第醍題鷹滝瀧卓啄宅托択拓沢濯琢託鐸濁諾茸凧蛸只
叩但達辰奪脱巽竪辿棚谷狸鱈樽誰丹単嘆坦担探旦歎淡湛炭短端箪綻耽胆蛋誕鍛団壇弾断暖檀段男談値知地弛恥智池痴稚置致蜘遅馳築畜竹筑蓄逐秩窒茶嫡着中仲宙忠抽昼柱注虫衷註酎鋳駐樗瀦猪苧著貯丁兆凋喋寵
帖帳庁弔張彫徴懲挑暢朝潮牒町眺聴脹腸蝶調諜超跳銚長頂鳥勅捗直朕沈珍賃鎮陳津墜椎槌追鎚痛通塚栂掴槻佃漬柘辻蔦綴鍔椿潰坪壷嬬紬爪吊釣鶴亭低停偵剃貞呈堤定帝底庭廷弟悌抵挺提梯汀碇禎程締艇訂諦蹄逓
邸鄭釘鼎泥摘擢敵滴的笛適鏑溺哲徹撤轍迭鉄典填天展店添纏甜貼転顛点伝殿澱田電兎吐堵塗妬屠徒斗杜渡登菟賭途都鍍砥砺努度土奴怒倒党冬凍刀唐塔塘套宕島嶋悼投搭東桃梼棟盗淘湯涛灯燈当痘祷等答筒糖統到
董蕩藤討謄豆踏逃透鐙陶頭騰闘働動同堂導憧撞洞瞳童胴萄道銅峠鴇匿得徳涜特督禿篤毒独読栃橡凸突椴届鳶苫寅酉瀞噸屯惇敦沌豚遁頓呑曇鈍奈那内乍凪薙謎灘捺鍋楢馴縄畷南楠軟難汝二尼弐迩匂賑肉虹廿日乳入
如尿韮任妊忍認濡禰祢寧葱猫熱年念捻撚燃粘乃廼之埜嚢悩濃納能脳膿農覗蚤巴把播覇杷波派琶破婆罵芭馬俳廃拝排敗杯盃牌背肺輩配倍培媒梅楳煤狽買売賠陪這蝿秤矧萩伯剥博拍柏泊白箔粕舶薄迫曝漠爆縛莫駁麦
函箱硲箸肇筈櫨幡肌畑畠八鉢溌発醗髪伐罰抜筏閥鳩噺塙蛤隼伴判半反叛帆搬斑板氾汎版犯班畔繁般藩販範釆煩頒飯挽晩番盤磐蕃蛮匪卑否妃庇彼悲扉批披斐比泌疲皮碑秘緋罷肥被誹費避非飛樋簸備尾微枇毘琵眉美
鼻柊稗匹疋髭彦膝菱肘弼必畢筆逼桧姫媛紐百謬俵彪標氷漂瓢票表評豹廟描病秒苗錨鋲蒜蛭鰭品彬斌浜瀕貧賓頻敏瓶不付埠夫婦富冨布府怖扶敷斧普浮父符腐膚芙譜負賦赴阜附侮撫武舞葡蕪部封楓風葺蕗伏副復幅服
福腹複覆淵弗払沸仏物鮒分吻噴墳憤扮焚奮粉糞紛雰文聞丙併兵塀幣平弊柄並蔽閉陛米頁僻壁癖碧別瞥蔑箆偏変片篇編辺返遍便勉娩弁鞭保舗鋪圃捕歩甫補輔穂募墓慕戊暮母簿菩倣俸包呆報奉宝峰峯崩庖抱捧放方朋
法泡烹砲縫胞芳萌蓬蜂褒訪豊邦鋒飽鳳鵬乏亡傍剖坊妨帽忘忙房暴望某棒冒紡肪膨謀貌貿鉾防吠頬北僕卜墨撲朴牧睦穆釦勃没殆堀幌奔本翻凡盆摩磨魔麻埋妹昧枚毎哩槙幕膜枕鮪柾鱒桝亦俣又抹末沫迄侭繭麿万慢満
漫蔓味未魅巳箕岬密蜜湊蓑稔脈妙粍民眠務夢無牟矛霧鵡椋婿娘冥名命明盟迷銘鳴姪牝滅免棉綿緬面麺摸模茂妄孟毛猛盲網耗蒙儲木黙目杢勿餅尤戻籾貰問悶紋門匁也冶夜爺耶野弥矢厄役約薬訳躍靖柳薮鑓愉愈油癒
諭輸唯佑優勇友宥幽悠憂揖有柚湧涌猶猷由祐裕誘遊邑郵雄融夕予余与誉輿預傭幼妖容庸揚揺擁曜楊様洋溶熔用窯羊耀葉蓉要謡踊遥陽養慾抑欲沃浴翌翼淀羅螺裸来莱頼雷洛絡落酪乱卵嵐欄濫藍蘭覧利吏履李梨理璃
痢裏裡里離陸律率立葎掠略劉流溜琉留硫粒隆竜龍侶慮旅虜了亮僚両凌寮料梁涼猟療瞭稜糧良諒遼量陵領力緑倫厘林淋燐琳臨輪隣鱗麟瑠塁涙累類令伶例冷励嶺怜玲礼苓鈴隷零霊麗齢暦歴列劣烈裂廉恋憐漣煉簾練聯
蓮連錬呂魯櫓炉賂路露労婁廊弄朗楼榔浪漏牢狼篭老聾蝋郎六麓禄肋録論倭和話歪賄脇惑枠鷲亙亘鰐詫藁蕨椀湾碗腕
`
for _, c := range table {
if c == '\n' {
continue
}
jaKanjis = append(jaKanjis, c)
}
}
func parseFont() error {
func init() {
f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
if err != nil {
return err
log.Fatal(err)
}
defer func() {
_ = f.Close()
}()
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return err
log.Fatal(err)
}
tt, err := truetype.Parse(b)
if err != nil {
return err
log.Fatal(err)
}
w, h := textImage.Size()
dst := image.NewRGBA(image.Rect(0, 0, w, h))
const size = 24
const dpi = 72
d := &font.Drawer{
Dst: dst,
Src: image.White,
Face: truetype.NewFace(tt, &truetype.Options{
Size: size,
DPI: dpi,
Hinting: font.HintingFull,
}),
}
y := size
for _, s := range text {
d.Dot = fixed.P(0, y)
d.DrawString(s)
y += size
}
textImage.ReplacePixels(dst.Pix)
return nil
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{
Size: 24,
DPI: dpi,
Hinting: font.HintingFull,
})
mplusBigFont = truetype.NewFace(tt, &truetype.Options{
Size: 48,
DPI: dpi,
Hinting: font.HintingFull,
})
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func codeToColor(c rune) color.RGBA {
r := 0x80 + ((uint8(c) & 0x0f) << 3)
g := 0x80 + ((uint8(c>>4) & 0x0f) << 3)
b := 0x80 + ((uint8(c>>8) & 0x0f) << 3)
return color.RGBA{r, g, b, 0xff}
}
func update(screen *ebiten.Image) error {
if counter%ebiten.FPS == 0 {
kanjiCode = jaKanjis[rand.Intn(len(jaKanjis))]
}
counter++
kanjiText := fmt.Sprintf("%s: U+%04x", string(kanjiCode), kanjiCode)
if ebiten.IsRunningSlowly() {
return nil
}
screen.DrawImage(textImage, &ebiten.DrawImageOptions{})
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
const x = 20
text.Draw(screen, mplusNormalFont, msg, x, 40, 30, color.White)
text.Draw(screen, mplusNormalFont, sampleText, x, 80, 30, color.White)
text.Draw(screen, mplusBigFont, kanjiText, x, 160, 30, codeToColor(kanjiCode))
return nil
}
func main() {
textImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err := parseFont(); err != nil {
log.Fatal(err)
}
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

284
text/text.go Normal file
View File

@ -0,0 +1,284 @@
// Copyright 2017 The Ebiten Authors
//
// 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.
// Package text offers functions to draw texts on an Ebiten's image.
package text
import (
"image"
"image/color"
"math"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics" // TODO: Move NextPowerOf2Int to a new different package
"github.com/hajimehoshi/ebiten/internal/sync"
)
var (
monotonicClock int64
)
func now() int64 {
monotonicClock++
return monotonicClock
}
type char struct {
face font.Face
rune rune
}
type glyph struct {
char char
index int
bounds fixed.Rectangle26_6
atime int64
}
func (g *glyph) size() (int, int) {
if g.bounds.Empty() {
g.bounds, _ = font.BoundString(g.char.face, string(g.char.rune))
}
p := g.bounds.Max.Sub(g.bounds.Min)
return p.X.Ceil(), p.Y.Ceil()
}
func (g *glyph) empty() bool {
w, h := g.size()
return w == 0 || h == 0
}
func (g *glyph) atlasGroup() int {
w, h := g.size()
t := w
if t < h {
t = h
}
// Different images for small runes are inefficient.
// Let's use a same texture atlas for typical character sizes.
if t < 32 {
return 32
}
return graphics.NextPowerOf2Int(t)
}
func (g *glyph) draw(dst *ebiten.Image, x, y int, clr color.Color) {
cr, cg, cb, ca := clr.RGBA()
if ca == 0 {
return
}
a := atlases[g.atlasGroup()]
sx, sy := a.at(g)
ox := g.bounds.Min.X.Ceil()
oy := g.bounds.Min.Y.Ceil()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Translate(float64(ox), float64(oy))
rf := float64(cr) / float64(ca)
gf := float64(cg) / float64(ca)
bf := float64(cb) / float64(ca)
af := float64(ca) / 0xffff
op.ColorM.Scale(rf, gf, bf, af)
r := image.Rect(sx, sy, sx+a.size, sy+a.size)
op.SourceRect = &r
dst.DrawImage(a.image, op)
}
var (
glyphs = map[char]*glyph{}
atlases = map[int]*atlas{}
)
type atlas struct {
image *ebiten.Image
tmpImage *ebiten.Image
size int
glyphs []*glyph
count int
}
func (a *atlas) at(glyph *glyph) (int, int) {
if a.size != glyph.atlasGroup() {
panic("not reached")
}
w, _ := a.image.Size()
xnum := w / a.size
x, y := glyph.index%xnum, glyph.index/xnum
return x * a.size, y * a.size
}
func (a *atlas) append(glyph *glyph) {
if a.count == len(a.glyphs) {
idx := -1
t := int64(math.MaxInt64)
for i, g := range a.glyphs {
if g.atime < t {
t = g.atime
idx = i
}
}
if idx < 0 {
panic("not reached")
}
oldest := a.glyphs[idx]
delete(glyphs, oldest.char)
glyph.index = idx
a.glyphs[idx] = glyph
a.draw(glyph)
return
}
idx := -1
for i, g := range a.glyphs {
if g == nil {
idx = i
break
}
}
if idx < 0 {
panic("not reached")
}
a.count++
glyph.index = idx
a.glyphs[idx] = glyph
a.draw(glyph)
}
func (a *atlas) draw(glyph *glyph) {
if a.tmpImage == nil {
a.tmpImage, _ = ebiten.NewImage(a.size, a.size, ebiten.FilterNearest)
}
dst := image.NewRGBA(image.Rect(0, 0, a.size, a.size))
d := font.Drawer{
Dst: dst,
Src: image.White,
Face: glyph.char.face,
}
ox := -glyph.bounds.Min.X.Ceil()
oy := -glyph.bounds.Min.Y.Ceil()
d.Dot = fixed.P(ox, oy)
d.DrawString(string(glyph.char.rune))
a.tmpImage.ReplacePixels(dst.Pix)
op := &ebiten.DrawImageOptions{}
x, y := a.at(glyph)
op.GeoM.Translate(float64(x), float64(y))
op.CompositeMode = ebiten.CompositeModeCopy
a.image.DrawImage(a.tmpImage, op)
a.tmpImage.Clear()
}
func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
ch := char{face, r}
g, ok := glyphs[ch]
if ok {
g.atime = now
return g
}
g = &glyph{
char: ch,
atime: now,
}
if g.empty() {
return g
}
a, ok := atlases[g.atlasGroup()]
if !ok {
// Don't use ebiten.MaxImageSize here.
// It's because the back-end image pixels will be restored from GPU
// whenever a new glyph is rendered on the image, and restoring cost is
// expensive if the image is big.
// The back-end image is updated a temporary image, and the temporary image is
// always cleared after used. This means that there is no clue to restore
// the back-end image without reading from GPU
// (see the package 'restorable' implementation).
//
// TODO: How about making a new function for 'flagile' image?
const size = 1024
i, _ := ebiten.NewImage(size, size, ebiten.FilterNearest)
a = &atlas{
image: i,
size: g.atlasGroup(),
}
w, h := a.image.Size()
xnum := w / a.size
ynum := h / a.size
a.glyphs = make([]*glyph, xnum*ynum)
atlases[g.atlasGroup()] = a
}
a.append(g)
glyphs[g.char] = g
return g
}
var textM sync.Mutex
// Draw draws a given text on a give destination image dst.
//
// face is the font for text rendering.
//
// (x, y) represents a 'dot' position. Be careful that this doesn't represent left-upper corner position.
//
// lineHeight is the Y offset for line spacing.
//
// clr is the color for text rendering.
//
// This function is concurrent-safe.
func Draw(dst *ebiten.Image, face font.Face, text string, x, y int, lineHeight int, clr color.Color) {
textM.Lock()
n := now()
fx := fixed.I(x)
ofx := fx
prevC := rune(-1)
runes := []rune(text)
for _, c := range runes {
if c == '\n' {
fx = ofx
y += lineHeight
prevC = rune(-1)
continue
}
if prevC >= 0 {
fx += face.Kern(prevC, c)
}
g := getGlyphFromCache(face, c, n)
if !g.empty() {
g.draw(dst, fx.Ceil(), y, clr)
}
a, _ := face.GlyphAdvance(c)
fx += a
prevC = c
}
textM.Unlock()
}