Loading of multiple fonts for bold and italic

This commit is contained in:
Sasha Koshka 2022-11-29 02:36:24 -05:00
parent e753fc11ca
commit ef5a811140
4 changed files with 96 additions and 19 deletions

View File

@ -86,9 +86,51 @@ func (backend *Backend) drawRune (
// application background color as the background. that way, we won't
// need to redraw the characters *or* composite them.
face := backend.font.normal
highlight := runeStyle & stone.StyleHighlight > 0
bold := runeStyle & stone.StyleBold > 0
italic := runeStyle & stone.StyleItalic > 0
boldTransform := false
italicTransform := false
switch {
case bold && italic:
if backend.font.boldItalic == nil {
switch {
case
backend.font.bold == nil && backend.font.italic != nil,
backend.font.bold != nil && backend.font.italic != nil:
boldTransform = true
face = backend.font.italic
case backend.font.italic == nil && backend.font.bold != nil:
italicTransform = true
face = backend.font.bold
default:
boldTransform = true
italicTransform = true
}
} else {
face = backend.font.boldItalic
}
case bold:
if backend.font.bold == nil {
boldTransform = true
} else {
face = backend.font.bold
}
case italic:
if backend.font.italic == nil {
italicTransform = true
} else {
face = backend.font.italic
}
}
var background xgraphics.BGRA
var foreground xgraphics.BGRA
highlight := runeStyle & stone.StyleHighlight > 0
if highlight {
background = backend.colors[runeColor]
@ -108,7 +150,7 @@ func (backend *Backend) drawRune (
origin := backend.originOfCell(x, y + 1)
if character >= 32 {
destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
destinationRectangle, mask, maskPoint, _, ok := face.Glyph (
fixed.Point26_6 {
X: fixed.I(origin.X),
Y: fixed.I(origin.Y),
@ -139,8 +181,7 @@ func (backend *Backend) drawRune (
backend.sprayRuneMask (
mask, destinationRectangle,
maskPoint, foreground, background,
runeStyle & stone.StyleItalic > 0,
runeStyle & stone.StyleBold > 0)
italicTransform, boldTransform)
// }
}

View File

@ -32,11 +32,20 @@ func factory (
}
// load font
backend.font.face = findAndLoadFont (
backend.config.FontName(),
backend.font.normal = findAndLoadFont (
backend.config.FontNameNormal(),
float64(backend.config.FontSize()))
if backend.font.face == nil {
backend.font.face = basicfont.Face7x13
backend.font.bold = findAndLoadFont (
backend.config.FontNameBold(),
float64(backend.config.FontSize()))
backend.font.italic = findAndLoadFont (
backend.config.FontNameItalic(),
float64(backend.config.FontSize()))
backend.font.boldItalic = findAndLoadFont (
backend.config.FontNameBoldItalic(),
float64(backend.config.FontSize()))
if backend.font.normal == nil {
backend.font.normal = basicfont.Face7x13
}
// pre-calculate colors
@ -56,8 +65,8 @@ func factory (
}
// calculate metrics
metrics := backend.font.face.Metrics()
glyphAdvance, _ := backend.font.face.GlyphAdvance('M')
metrics := backend.font.normal.Metrics()
glyphAdvance, _ := backend.font.normal.GlyphAdvance('M')
backend.metrics.cellWidth = glyphAdvance.Round()
backend.metrics.cellHeight = metrics.Height.Round()
backend.metrics.descent = metrics.Descent.Round()

View File

@ -27,7 +27,10 @@ type Backend struct {
lock sync.Mutex
font struct {
face font.Face
normal font.Face
bold font.Face
italic font.Face
boldItalic font.Face
}
colors [8]xgraphics.BGRA

View File

@ -12,7 +12,10 @@ type Config struct {
padding int
center bool
fontSize int
fontName string
fontNameNormal string
fontNameBold string
fontNameItalic string
fontNameBoldItalic string
}
// Color returns the color value at the specified index.
@ -41,9 +44,27 @@ func (public *Config) FontSize () (fontSize int) {
return
}
// FontName specifies the name of the font to use.
func (public *Config) FontName () (fontName string) {
fontName = public.fontName
// FontNameNormal specifies the name of the font to use for normal text.
func (public *Config) FontNameNormal () (fontName string) {
fontName = public.fontNameNormal
return
}
// FontNameBold specifies the name of the font to use for bold text.
func (public *Config) FontNameBold () (fontName string) {
fontName = public.fontNameBold
return
}
// FontName specifies the name of the font to use for text.
func (public *Config) FontNameItalic () (fontName string) {
fontName = public.fontNameItalic
return
}
// FontName specifies the name of the font to use for text.
func (public *Config) FontNameBoldItalic () (fontName string) {
fontName = public.fontNameBoldItalic
return
}
@ -97,10 +118,13 @@ func (public *Config) load () {
public.private.Load("stone")
params := public.private.Parameters
public.fontName = params["fontNormal"].(string)
public.fontSize = params["fontSize"].(int)
public.padding = params["padding"].(int)
public.center = params["center"].(bool)
public.fontNameNormal = params["fontNormal"].(string)
public.fontNameBold = params["fontBold"].(string)
public.fontNameItalic = params["fontItalic"].(string)
public.fontNameBoldItalic = params["fontBoldItalic"].(string)
public.fontSize = params["fontSize"].(int)
public.padding = params["padding"].(int)
public.center = params["center"].(bool)
public.colors[ColorBackground] = params["colorBackground"].(color.RGBA)
public.colors[ColorForeground] = params["colorForeground"].(color.RGBA)