Skip to content

Commit

Permalink
font/{liberation,lm},cmd/mtex-render: expose ttf.Fonts collections
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Jul 9, 2024
1 parent 85a9afb commit a2db7b8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 55 deletions.
59 changes: 4 additions & 55 deletions cmd/mtex-render/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,18 @@ import (
"github.com/go-fonts/liberation/liberationserifbolditalic"
"github.com/go-fonts/liberation/liberationserifitalic"
"github.com/go-fonts/liberation/liberationserifregular"
"golang.org/x/image/font/sfnt"

"github.com/go-latex/latex/font/liberation"
"github.com/go-latex/latex/font/lm"
"github.com/go-latex/latex/font/ttf"
)

func liberationFonts() *ttf.Fonts {
rm, err := sfnt.Parse(liberationserifregular.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

it, err := sfnt.Parse(liberationserifitalic.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

bf, err := sfnt.Parse(liberationserifbold.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

bfit, err := sfnt.Parse(liberationserifbolditalic.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

return &ttf.Fonts{
Default: rm,
Rm: rm,
It: it,
Bf: bf,
BfIt: bfit,
}
return liberation.Fonts()
}

func lmromanFonts() *ttf.Fonts {
rm, err := sfnt.Parse(lmromanregular.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

it, err := sfnt.Parse(lmromanitalic.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

bf, err := sfnt.Parse(lmromanbold.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

bfit, err := sfnt.Parse(lmromanbolditalic.TTF)
if err != nil {
log.Fatalf("could not parse fonts: %+v", err)
}

return &ttf.Fonts{
Default: rm,
Rm: rm,
It: it,
Bf: bf,
BfIt: bfit,
}
return lm.Fonts()
}

func registerFont(fnt text.Font, name string, raw []byte) text.FontFace {
Expand Down
58 changes: 58 additions & 0 deletions font/liberation/liberation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright ©2021 The go-latex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package liberation provides a ttf.Fonts value populated with Liberation fonts.
package liberation // import "github.com/go-latex/latex/font/liberation"

import (
"log"
"sync"

"github.com/go-fonts/liberation/liberationserifbold"
"github.com/go-fonts/liberation/liberationserifbolditalic"
"github.com/go-fonts/liberation/liberationserifitalic"
"github.com/go-fonts/liberation/liberationserifregular"
"golang.org/x/image/font/sfnt"

"github.com/go-latex/latex/font/ttf"
)

var (
once sync.Once
fnts *ttf.Fonts
)

// Fonts returns a ttf.Fonts value populated with Liberation fonts.
func Fonts() *ttf.Fonts {
once.Do(func() {
rm, err := sfnt.Parse(liberationserifregular.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

it, err := sfnt.Parse(liberationserifitalic.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

bf, err := sfnt.Parse(liberationserifbold.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

bfit, err := sfnt.Parse(liberationserifbolditalic.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

fnts = &ttf.Fonts{
Default: rm,
Rm: rm,
It: it,
Bf: bf,
BfIt: bfit,
}
})
return fnts
}
60 changes: 60 additions & 0 deletions font/lm/lm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright ©2021 The go-latex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package lm provides a ttf.Fonts value populated with latin-modern,
// a LaTeX-looking font.
package lm // import "github.com/go-latex/latex/font/lm"

import (
"log"
"sync"

lmromanbold "github.com/go-fonts/latin-modern/lmroman10bold"
lmromanbolditalic "github.com/go-fonts/latin-modern/lmroman10bolditalic"
lmromanitalic "github.com/go-fonts/latin-modern/lmroman10italic"
lmromanregular "github.com/go-fonts/latin-modern/lmroman10regular"
"golang.org/x/image/font/sfnt"

"github.com/go-latex/latex/font/ttf"
)

var (
once sync.Once
fnts *ttf.Fonts
)

// Fonts returns a ttf.Fonts value populated with latin-modern fonts.
func Fonts() *ttf.Fonts {
once.Do(func() {
rm, err := sfnt.Parse(lmromanregular.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

it, err := sfnt.Parse(lmromanitalic.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

bf, err := sfnt.Parse(lmromanbold.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

bfit, err := sfnt.Parse(lmromanbolditalic.TTF)
if err != nil {
log.Panicf("could not parse fonts: %+v", err)
}

fnts = &ttf.Fonts{
Default: rm,
Rm: rm,
It: it,
Bf: bf,
BfIt: bfit,
}
})

return fnts
}

0 comments on commit a2db7b8

Please sign in to comment.