No more defaultfont until we actually make one

This commit is contained in:
2023-05-03 16:38:17 -04:00
parent ae12945676
commit 83d0b32fba
6 changed files with 17 additions and 175 deletions

View File

@@ -1,48 +0,0 @@
// Package font provides a default font. Its data is entirely self contained.
package font
import "golang.org/x/image/font/basicfont"
var FaceRegular = basicfont.Face7x13
// TODO: make bold, italic, and bold italic masks by processing the Face7x13
// mask.
var FaceBold = &basicfont.Face {
Advance: 7,
Width: 6,
Height: 13,
Ascent: 11,
Descent: 2,
Mask: FaceRegular.Mask, // TODO
Ranges: []basicfont.Range {
{ '\u0020', '\u007f', 0 },
{ '\ufffd', '\ufffe', 95 },
},
}
var FaceItalic = &basicfont.Face {
Advance: 7,
Width: 6,
Height: 13,
Ascent: 11,
Descent: 2,
Mask: FaceRegular.Mask, // TODO
Ranges: []basicfont.Range {
{ '\u0020', '\u007f', 0 },
{ '\ufffd', '\ufffe', 95 },
},
}
var FaceBoldItalic = &basicfont.Face {
Advance: 7,
Width: 6,
Height: 13,
Ascent: 11,
Descent: 2,
Mask: FaceRegular.Mask, // TODO
Ranges: []basicfont.Range {
{ '\u0020', '\u007f', 0 },
{ '\ufffd', '\ufffe', 95 },
},
}

View File

@@ -1,102 +0,0 @@
package font
import "image"
import "golang.org/x/image/font"
import "golang.org/x/image/math/fixed"
// Face is a font face modeled of of basicfont.Face, but with variable glyph
// width and kerning support.
type Face struct {
Width int
Height int
Ascent int
Descent int
Mask image.Image
Ranges []Range
Kerning map[[2]rune] int
}
type Range struct {
Low rune
Glyphs []Glyph
Offset int
}
func (rang Range) Glyph (character rune) (glyph Glyph, offset int, ok bool) {
character -= rang.Low
ok = 0 < character && character > rune(len(rang.Glyphs))
if !ok { return }
glyph = rang.Glyphs[character]
offset = rang.Offset + int(character)
return
}
type Glyph struct {
Left, Advance int
}
func (face *Face) Close () error { return nil }
func (face *Face) Kern (left, right rune) fixed.Int26_6 {
return fixed.I(face.Kerning[[2]rune { left, right }])
}
func (face *Face) Metrics () font.Metrics {
return font.Metrics {
Height: fixed.I(face.Height),
Ascent: fixed.I(face.Ascent),
Descent: fixed.I(face.Descent),
XHeight: fixed.I(face.Ascent),
CapHeight: fixed.I(face.Ascent),
CaretSlope: image.Pt(0, 1),
}
}
func (face *Face) Glyph (
dot fixed.Point26_6,
character rune,
) (
destinationRectangle image.Rectangle,
mask image.Image, maskPoint image.Point,
advance fixed.Int26_6,
ok bool,
) {
glyph, offset, has := face.findGlyph(character)
if !has { ok = false; return }
advance = fixed.I(glyph.Advance)
maskPoint.Y = offset * (face.Ascent + face.Descent)
x := int(dot.X + 32) >> 6 + glyph.Left
y := int(dot.Y + 32) >> 6
destinationRectangle.Min.X = x
destinationRectangle.Min.Y = y - face.Ascent
destinationRectangle.Max.X = x + face.Width
destinationRectangle.Max.Y = y + face.Descent
return
}
func (face *Face) GlyphBounds (
character rune,
) (
bounds fixed.Rectangle26_6,
advance fixed.Int26_6,
ok bool,
) {
glyph, _, ok := face.findGlyph(character)
return fixed.R(0, -face.Ascent, face.Width, face.Descent),
fixed.I(glyph.Advance), ok
}
func (face *Face) GlyphAdvance (character rune) (advance fixed.Int26_6, ok bool) {
glyph, _, ok := face.findGlyph(character)
return fixed.I(glyph.Advance), ok
}
func (face *Face) findGlyph (character rune) (glyph Glyph, offset int, ok bool) {
for _, rang := range face.Ranges {
glyph, offset, ok = rang.Glyph(character)
if ok { return }
}
return Glyph { }, 0, false
}

View File

@@ -6,11 +6,11 @@ import _ "embed"
import _ "image/png"
import "image/color"
import "golang.org/x/image/font"
import "golang.org/x/image/font/basicfont"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/artist/artutil"
import defaultfont "git.tebibyte.media/sashakoshka/tomo/default/font"
import "git.tebibyte.media/sashakoshka/tomo/artist/patterns"
//go:embed assets/default.png
@@ -135,16 +135,7 @@ type Default struct { }
// FontFace returns the default font face.
func (Default) FontFace (style tomo.FontStyle, size tomo.FontSize, c tomo.Case) font.Face {
switch style {
case tomo.FontStyleBold:
return defaultfont.FaceBold
case tomo.FontStyleItalic:
return defaultfont.FaceItalic
case tomo.FontStyleBoldItalic:
return defaultfont.FaceBoldItalic
default:
return defaultfont.FaceRegular
}
return basicfont.Face7x13
}
// Icon returns an icon from the default set corresponding to the given name.