This repository has been archived by the owner on Jul 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFonts.go
67 lines (55 loc) · 1.72 KB
/
Fonts.go
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
56
57
58
59
60
61
62
63
64
65
66
67
package core
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/inkyblackness/res"
"github.com/inkyblackness/res/chunk"
resFont "github.com/inkyblackness/res/font"
"github.com/inkyblackness/shocked-core/io"
model "github.com/inkyblackness/shocked-model"
)
// Fonts represents the game fonts accessor
type Fonts struct {
gamescr *io.DynamicChunkStore
}
// NewFonts returns a new instance of Fonts.
func NewFonts(library io.StoreLibrary) (fonts *Fonts, err error) {
var gamescr *io.DynamicChunkStore
gamescr, err = library.ChunkStore("gamescr.res")
if err == nil {
fonts = &Fonts{gamescr: gamescr}
}
return
}
// Font returns the font data for the identified font.
func (fonts *Fonts) Font(id res.ResourceID) (font *model.Font, err error) {
fontChunk := fonts.gamescr.Get(id)
if fontChunk.ContentType() == chunk.Font {
fontBlockData := fontChunk.BlockData(0)
var fontData resFont.Font
fontData, err = resFont.Load(bytes.NewReader(fontBlockData))
if err == nil {
isMonochrome := fontData.IsMonochrome()
if isMonochrome {
fontData = resFont.EnsureColor(fontData, 1)
}
font = &model.Font{
Monochrome: isMonochrome,
Bitmap: model.RawBitmap{
Width: fontData.BitmapWidth(),
Height: fontData.BitmapHeight(),
Pixels: base64.StdEncoding.EncodeToString(fontData.Bitmap())},
FirstCharacter: fontData.FirstCharacter(),
GlyphXOffsets: make([]int, fontData.LastCharacter()-fontData.FirstCharacter())}
for charIndex := 0; charIndex < len(font.GlyphXOffsets); charIndex++ {
font.GlyphXOffsets[charIndex] = fontData.GlyphXOffset(charIndex)
}
} else {
err = fmt.Errorf("Failed to load font ID %v", id)
}
} else {
err = fmt.Errorf("ID %v is not a font", id)
}
return
}