Remove NewX for each standard font in std. Go through a single exported function instead.

This commit is contained in:
Gunnsteinn Hall 2019-03-12 14:27:32 +00:00
parent a03198000b
commit 9d714ad639
6 changed files with 46 additions and 44 deletions

View File

@ -95,7 +95,11 @@ func (font *PdfFont) ToUnicode() string {
// DefaultFont returns the default font, which is currently the built in Helvetica. // DefaultFont returns the default font, which is currently the built in Helvetica.
func DefaultFont() *PdfFont { func DefaultFont() *PdfFont {
std := stdFontToSimpleFont(fonts.NewFontHelvetica()) helvetica, ok := fonts.NewStdFontByName(HelveticaName)
if !ok {
panic("Helvetica should always be available")
}
std := stdFontToSimpleFont(helvetica)
return &PdfFont{context: &std} return &PdfFont{context: &std}
} }

View File

@ -15,10 +15,10 @@ func init() {
// The aliases seen for the standard 14 font names. // The aliases seen for the standard 14 font names.
// Most of these are from table 5.5.1 in // Most of these are from table 5.5.1 in
// https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf // https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf
RegisterStdFont(CourierName, NewFontCourier, "CourierCourierNew", "CourierNew") RegisterStdFont(CourierName, newFontCourier, "CourierCourierNew", "CourierNew")
RegisterStdFont(CourierBoldName, NewFontCourierBold, "CourierNew,Bold") RegisterStdFont(CourierBoldName, newFontCourierBold, "CourierNew,Bold")
RegisterStdFont(CourierObliqueName, NewFontCourierOblique, "CourierNew,Italic") RegisterStdFont(CourierObliqueName, newFontCourierOblique, "CourierNew,Italic")
RegisterStdFont(CourierBoldObliqueName, NewFontCourierBoldOblique, "CourierNew,BoldItalic") RegisterStdFont(CourierBoldObliqueName, newFontCourierBoldOblique, "CourierNew,BoldItalic")
} }
const ( const (
@ -32,8 +32,8 @@ const (
CourierBoldObliqueName = StdFontName("Courier-BoldOblique") CourierBoldObliqueName = StdFontName("Courier-BoldOblique")
) )
// NewFontCourier returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontCourier returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontCourier() StdFont { func newFontCourier() StdFont {
courierOnce.Do(initCourier) courierOnce.Do(initCourier)
desc := Descriptor{ desc := Descriptor{
Name: CourierName, Name: CourierName,
@ -52,8 +52,8 @@ func NewFontCourier() StdFont {
return NewStdFont(desc, courierCharMetrics) return NewStdFont(desc, courierCharMetrics)
} }
// NewFontCourierBold returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontCourierBold returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontCourierBold() StdFont { func newFontCourierBold() StdFont {
courierOnce.Do(initCourier) courierOnce.Do(initCourier)
desc := Descriptor{ desc := Descriptor{
Name: CourierBoldName, Name: CourierBoldName,
@ -73,7 +73,7 @@ func NewFontCourierBold() StdFont {
} }
// NewFontCourierOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding). // NewFontCourierOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontCourierOblique() StdFont { func newFontCourierOblique() StdFont {
courierOnce.Do(initCourier) courierOnce.Do(initCourier)
desc := Descriptor{ desc := Descriptor{
Name: CourierObliqueName, Name: CourierObliqueName,
@ -94,7 +94,7 @@ func NewFontCourierOblique() StdFont {
// NewFontCourierBoldOblique returns a new instance of the font with a default encoder set // NewFontCourierBoldOblique returns a new instance of the font with a default encoder set
// (WinAnsiEncoding). // (WinAnsiEncoding).
func NewFontCourierBoldOblique() StdFont { func newFontCourierBoldOblique() StdFont {
courierOnce.Do(initCourier) courierOnce.Do(initCourier)
desc := Descriptor{ desc := Descriptor{
Name: CourierBoldObliqueName, Name: CourierBoldObliqueName,

View File

@ -15,10 +15,10 @@ func init() {
// The aliases seen for the standard 14 font names. // The aliases seen for the standard 14 font names.
// Most of these are from table 5.5.1 in // Most of these are from table 5.5.1 in
// https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf // https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf
RegisterStdFont(HelveticaName, NewFontHelvetica, "Arial") RegisterStdFont(HelveticaName, newFontHelvetica, "Arial")
RegisterStdFont(HelveticaBoldName, NewFontHelveticaBold, "Arial,Bold") RegisterStdFont(HelveticaBoldName, newFontHelveticaBold, "Arial,Bold")
RegisterStdFont(HelveticaObliqueName, NewFontHelveticaOblique, "Arial,Italic") RegisterStdFont(HelveticaObliqueName, newFontHelveticaOblique, "Arial,Italic")
RegisterStdFont(HelveticaBoldObliqueName, NewFontHelveticaBoldOblique, "Arial,BoldItalic") RegisterStdFont(HelveticaBoldObliqueName, newFontHelveticaBoldOblique, "Arial,BoldItalic")
} }
const ( const (
@ -32,8 +32,8 @@ const (
HelveticaBoldObliqueName = StdFontName("Helvetica-BoldOblique") HelveticaBoldObliqueName = StdFontName("Helvetica-BoldOblique")
) )
// NewFontHelvetica returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontHelvetica returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontHelvetica() StdFont { func newFontHelvetica() StdFont {
helveticaOnce.Do(initHelvetica) helveticaOnce.Do(initHelvetica)
desc := Descriptor{ desc := Descriptor{
Name: HelveticaName, Name: HelveticaName,
@ -52,9 +52,9 @@ func NewFontHelvetica() StdFont {
return NewStdFont(desc, helveticaCharMetrics) return NewStdFont(desc, helveticaCharMetrics)
} }
// NewFontHelveticaBold returns a new instance of the font with a default encoder set // newFontHelveticaBold returns a new instance of the font with a default encoder set
// (WinAnsiEncoding). // (WinAnsiEncoding).
func NewFontHelveticaBold() StdFont { func newFontHelveticaBold() StdFont {
helveticaOnce.Do(initHelvetica) helveticaOnce.Do(initHelvetica)
desc := Descriptor{ desc := Descriptor{
Name: HelveticaBoldName, Name: HelveticaBoldName,
@ -73,8 +73,8 @@ func NewFontHelveticaBold() StdFont {
return NewStdFont(desc, helveticaBoldCharMetrics) return NewStdFont(desc, helveticaBoldCharMetrics)
} }
// NewFontHelveticaOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontHelveticaOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontHelveticaOblique() StdFont { func newFontHelveticaOblique() StdFont {
helveticaOnce.Do(initHelvetica) helveticaOnce.Do(initHelvetica)
desc := Descriptor{ desc := Descriptor{
Name: HelveticaObliqueName, Name: HelveticaObliqueName,
@ -93,8 +93,8 @@ func NewFontHelveticaOblique() StdFont {
return NewStdFont(desc, helveticaObliqueCharMetrics) return NewStdFont(desc, helveticaObliqueCharMetrics)
} }
// NewFontHelveticaBoldOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontHelveticaBoldOblique returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontHelveticaBoldOblique() StdFont { func newFontHelveticaBoldOblique() StdFont {
helveticaOnce.Do(initHelvetica) helveticaOnce.Do(initHelvetica)
desc := Descriptor{ desc := Descriptor{
Name: HelveticaBoldObliqueName, Name: HelveticaBoldObliqueName,

View File

@ -17,8 +17,8 @@ func init() {
// The aliases seen for the standard 14 font names. // The aliases seen for the standard 14 font names.
// Most of these are from table 5.5.1 in // Most of these are from table 5.5.1 in
// https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf // https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf
RegisterStdFont(SymbolName, NewFontSymbol, "Symbol,Italic", "Symbol,Bold", "Symbol,BoldItalic") RegisterStdFont(SymbolName, newFontSymbol, "Symbol,Italic", "Symbol,Bold", "Symbol,BoldItalic")
RegisterStdFont(ZapfDingbatsName, NewFontZapfDingbats) RegisterStdFont(ZapfDingbatsName, newFontZapfDingbats)
} }
const ( const (
@ -28,8 +28,8 @@ const (
ZapfDingbatsName = StdFontName("ZapfDingbats") ZapfDingbatsName = StdFontName("ZapfDingbats")
) )
// NewFontSymbol returns a new instance of the font with a default encoder set (SymbolEncoder). // newFontSymbol returns a new instance of the font with a default encoder set (SymbolEncoder).
func NewFontSymbol() StdFont { func newFontSymbol() StdFont {
enc := textencoding.NewSymbolEncoder() enc := textencoding.NewSymbolEncoder()
desc := Descriptor{ desc := Descriptor{
Name: SymbolName, Name: SymbolName,
@ -48,8 +48,8 @@ func NewFontSymbol() StdFont {
return NewStdFontWithEncoding(desc, symbolCharMetrics, enc) return NewStdFontWithEncoding(desc, symbolCharMetrics, enc)
} }
// NewFontZapfDingbats returns a new instance of the font with a default encoder set (ZapfDingbatsEncoder). // newFontZapfDingbats returns a new instance of the font with a default encoder set (ZapfDingbatsEncoder).
func NewFontZapfDingbats() StdFont { func newFontZapfDingbats() StdFont {
enc := textencoding.NewZapfDingbatsEncoder() enc := textencoding.NewZapfDingbatsEncoder()
desc := Descriptor{ desc := Descriptor{
Name: ZapfDingbatsName, Name: ZapfDingbatsName,

View File

@ -15,10 +15,10 @@ func init() {
// The aliases seen for the standard 14 font names. // The aliases seen for the standard 14 font names.
// Most of these are from table 5.5.1 in // Most of these are from table 5.5.1 in
// https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf // https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/adobe_supplement_iso32000.pdf
RegisterStdFont(TimesRomanName, NewFontTimesRoman, "TimesNewRoman", "Times") RegisterStdFont(TimesRomanName, newFontTimesRoman, "TimesNewRoman", "Times")
RegisterStdFont(TimesBoldName, NewFontTimesBold, "TimesNewRoman,Bold", "Times,Bold") RegisterStdFont(TimesBoldName, newFontTimesBold, "TimesNewRoman,Bold", "Times,Bold")
RegisterStdFont(TimesItalicName, NewFontTimesItalic, "TimesNewRoman,Italic", "Times,Italic") RegisterStdFont(TimesItalicName, newFontTimesItalic, "TimesNewRoman,Italic", "Times,Italic")
RegisterStdFont(TimesBoldItalicName, NewFontTimesBoldItalic, "TimesNewRoman,BoldItalic", "Times,BoldItalic") RegisterStdFont(TimesBoldItalicName, newFontTimesBoldItalic, "TimesNewRoman,BoldItalic", "Times,BoldItalic")
} }
const ( const (
@ -34,8 +34,8 @@ const (
TimesBoldItalicName = StdFontName("Times-BoldItalic") TimesBoldItalicName = StdFontName("Times-BoldItalic")
) )
// NewFontTimesRoman returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontTimesRoman returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontTimesRoman() StdFont { func newFontTimesRoman() StdFont {
timesOnce.Do(initTimes) timesOnce.Do(initTimes)
desc := Descriptor{ desc := Descriptor{
Name: TimesRomanName, Name: TimesRomanName,
@ -54,8 +54,8 @@ func NewFontTimesRoman() StdFont {
return NewStdFont(desc, timesRomanCharMetrics) return NewStdFont(desc, timesRomanCharMetrics)
} }
// NewFontTimesBold returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontTimesBold returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontTimesBold() StdFont { func newFontTimesBold() StdFont {
timesOnce.Do(initTimes) timesOnce.Do(initTimes)
desc := Descriptor{ desc := Descriptor{
Name: TimesBoldName, Name: TimesBoldName,
@ -74,8 +74,8 @@ func NewFontTimesBold() StdFont {
return NewStdFont(desc, timesBoldCharMetrics) return NewStdFont(desc, timesBoldCharMetrics)
} }
// NewFontTimesItalic returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontTimesItalic returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontTimesItalic() StdFont { func newFontTimesItalic() StdFont {
timesOnce.Do(initTimes) timesOnce.Do(initTimes)
desc := Descriptor{ desc := Descriptor{
Name: TimesItalicName, Name: TimesItalicName,
@ -94,8 +94,8 @@ func NewFontTimesItalic() StdFont {
return NewStdFont(desc, timesItalicCharMetrics) return NewStdFont(desc, timesItalicCharMetrics)
} }
// NewFontTimesBoldItalic returns a new instance of the font with a default encoder set (WinAnsiEncoding). // newFontTimesBoldItalic returns a new instance of the font with a default encoder set (WinAnsiEncoding).
func NewFontTimesBoldItalic() StdFont { func newFontTimesBoldItalic() StdFont {
timesOnce.Do(initTimes) timesOnce.Do(initTimes)
desc := Descriptor{ desc := Descriptor{
Name: TimesBoldItalicName, Name: TimesBoldItalicName,

View File

@ -22,8 +22,6 @@ import (
"github.com/unidoc/unidoc/pdf/core" "github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/core/security" "github.com/unidoc/unidoc/pdf/core/security"
"github.com/unidoc/unidoc/pdf/core/security/crypt" "github.com/unidoc/unidoc/pdf/core/security/crypt"
"github.com/unidoc/unidoc/pdf/model/internal/fonts"
) )
type crossReference struct { type crossReference struct {
@ -505,7 +503,7 @@ func procPage(p *PdfPage) {
} }
// Add font as needed. // Add font as needed.
f := fonts.NewFontHelvetica() f := DefaultFont()
p.Resources.SetFontByName("UF1", f.ToPdfObject()) p.Resources.SetFontByName("UF1", f.ToPdfObject())
var ops []string var ops []string