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

View File

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

View File

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

View File

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