Skip to content

Commit

Permalink
golint
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 20, 2021
1 parent ad98c4e commit e077290
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 77 deletions.
5 changes: 5 additions & 0 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ func DPI(dpi float64) Resolution {
return Resolution(dpi * inchPerMm)
}

// DPMM returns the resolution in dots-per-millimeter
func (res Resolution) DPMM() float64 {
return float64(res)
}

// DPI returns the resolution in dots-per-inch
func (res Resolution) DPI() float64 {
return float64(res) * mmPerInch
}

// DefaultResolution is the default resolution used for font PPEMs and is set to 96 DPI
const DefaultResolution = Resolution(96.0 * inchPerMm)

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -80,8 +83,10 @@ type Renderer interface {

////////////////////////////////////////////////////////////////

// CoordSystem is the coordinate system, which can be either of the four cartesian quadrants
type CoordSystem int

// see CoordSystem
const (
CartesianI CoordSystem = iota
CartesianII
Expand Down
2 changes: 1 addition & 1 deletion cmd/fontinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func info(args []string) error {
} else if sfnt.Version == "ttcf" {
version = "Collection"
}
fmt.Printf(" sfntVersion: %X (%s)\n", sfnt.Version)
fmt.Printf(" sfntVersion: %X (%s)\n", sfnt.Version, version)

r := newBinaryReader(sfnt.Data)
_ = r.ReadBytes(4)
Expand Down
13 changes: 12 additions & 1 deletion font.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func parseFont(name string, b []byte, index int) (*Font, error) {
return font, nil
}

// Destroy should be called when using HarfBuzz to free the C resources
func (f *Font) Destroy() {
f.shaper.Destroy()
}
Expand All @@ -81,6 +82,7 @@ func (f *Font) Name() string {
return f.name
}

// SubsetID maps a glyphID of the original font to the subsettd font. If the glyphID is not subsetted, it will be added to the map
func (f *Font) SubsetID(glyphID uint16) uint16 {
if subsetGlyphID, ok := f.subsetIDMap[glyphID]; ok {
return subsetGlyphID
Expand All @@ -91,14 +93,17 @@ func (f *Font) SubsetID(glyphID uint16) uint16 {
return subsetGlyphID
}

// SubsetIDs returns all subsetted IDs in the order of appearance
func (f *Font) SubsetIDs() []uint16 {
return f.subsetIDs
}

// SetVariations sets the font variations (not yet supported)
func (f *Font) SetVariations(variations string) {
f.variations = variations
}

// SetFeatures sets the font features (not yet supported)
func (f *Font) SetFeatures(features string) {
f.features = features
}
Expand All @@ -117,18 +122,21 @@ func NewFontFamily(name string) *FontFamily {
}
}

// Destroy should be called when using HarfBuzz to free the C resources
func (family *FontFamily) Destroy() {
for _, font := range family.fonts {
font.Destroy()
}
}

// SetVariations sets the font variations (not yet supported)
func (family *FontFamily) SetVariations(variations string) {
for _, font := range family.fonts {
font.SetVariations(variations)
}
}

// SetFeatures sets the font features (not yet supported)
func (family *FontFamily) SetFeatures(features string) {
for _, font := range family.fonts {
font.SetFeatures(features)
Expand Down Expand Up @@ -318,14 +326,14 @@ type FontFace struct {
// Equals returns true when two font face are equal. In particular this allows two adjacent text spans that use the same decoration to allow the decoration to span both elements instead of two separately.
func (face *FontFace) Equals(other *FontFace) bool {
return reflect.DeepEqual(face, other)
//return face.Font == other.Font && face.Size == other.Size && face.Style == other.Style && face.Variant == other.Variant && face.Color == other.Color && reflect.DeepEqual(face.Deco, other.Deco) && face.Language == other.Language && face.Script == other.Script && face.Direction == other.Direction
}

// Name returns the name of the underlying font
func (face *FontFace) Name() string {
return face.Font.name
}

// HasDecoration returns true if the font face has decoration enabled
func (face *FontFace) HasDecoration() bool {
return 0 < len(face.Deco)
}
Expand Down Expand Up @@ -361,6 +369,7 @@ func (face *FontFace) Metrics() FontMetrics {
}
}

// PPEM returns the pixels-per-EM for a given resolution of the font face
func (face *FontFace) PPEM(resolution Resolution) uint16 {
// ppem is for hinting purposes only, this does not influence glyph advances
return uint16(resolution.DPMM() * face.Size)
Expand Down Expand Up @@ -402,6 +411,7 @@ func (face *FontFace) Decorate(width float64) *Path {
return p
}

// ToPath converts a string to its glyph paths
func (face *FontFace) ToPath(s string) (*Path, float64, error) {
ppem := face.PPEM(DefaultResolution)
glyphs := face.Font.shaper.Shape(s, ppem, face.Direction, face.Script, face.Language, face.Font.features, face.Font.variations)
Expand Down Expand Up @@ -429,6 +439,7 @@ func (face *FontFace) toPath(glyphs []text.Glyph, ppem uint16) (*Path, float64,
return p, face.mmPerEm * float64(x), nil
}

// Boldness returns the CSS boldness value for the font face
func (face *FontFace) Boldness() int {
boldness := 400
if face.Style&0xFF == FontExtraLight {
Expand Down
2 changes: 1 addition & 1 deletion font/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ToSFNT(b []byte) ([]byte, error) {
return nil, fmt.Errorf("unrecognized font file format")
}

// NewReader takes an io.Reader and transforms it into an SFNT reader. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.
// NewSFNTReader takes an io.Reader and transforms it into an SFNT reader. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.
func NewSFNTReader(r io.Reader) (*bytes.Reader, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions font/sfnt_cff.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,8 @@ func (cff *cffTable) ToPath(p Pather, glyphID, ppem uint16, x, y int32, f float6
default:
if 256 <= b0 {
return fmt.Errorf("%v: unsupported operator 12 %d", table, b0-256)
} else {
return fmt.Errorf("%v: unsupported operator %d", table, b0)
}
return fmt.Errorf("%v: unsupported operator %d", table, b0)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions font/sfnt_data.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package font

// PlatformID is the platform identifier for the name table
type PlatformID uint16

// see PlatformID
const (
PlatformUnicode = PlatformID(0)
PlatformMacintosh = PlatformID(1)
PlatformWindows = PlatformID(3)
PlatformCustom = PlatformID(4)
)

// EncodingID is the encoding identifier for the name table
type EncodingID uint16

// see EncodingID
const (
EncodingUnicode2BMP = EncodingID(3)
EncodingUnicode2FullRepertoir = EncodingID(4)
Expand Down Expand Up @@ -59,8 +63,10 @@ const (
EncodingWindowsUnicodeFullRepertoir = EncodingID(10)
)

// NameID is the name identifier for the name table
type NameID uint16

// see NameID
const (
NameCopyrightNotice = NameID(0)
NameFontFamily = NameID(1)
Expand Down
1 change: 1 addition & 0 deletions font/sfnt_subset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
)

// Subset regenerates a font file containing only the passed glyphIDs, thereby resulting in a significant size reduction. The glyphIDs will apear in the specified order in the file, and their dependencies are added to the end. It returns the compressed font file and the glyphIDs in the order in which they appear.
func (sfnt *SFNT) Subset(glyphIDs []uint16) ([]byte, []uint16) {
if sfnt.IsCFF {
// TODO: support CFF
Expand Down
1 change: 1 addition & 0 deletions font/tests/eot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzz

import "github.com/tdewolff/canvas/font"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
_, _ = font.ParseEOT(data)
return 1
Expand Down
1 change: 1 addition & 0 deletions font/tests/woff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzz

import "github.com/tdewolff/canvas/font"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
_, _ = font.ParseWOFF(data)
return 1
Expand Down
1 change: 1 addition & 0 deletions font/tests/woff2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzz

import "github.com/tdewolff/canvas/font"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
_, _ = font.ParseWOFF2(data)
return 1
Expand Down
18 changes: 8 additions & 10 deletions font/woff.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,17 @@ func ParseWOFF(b []byte) ([]byte, error) {
}
if w.Len() != totalSfntSize {
return nil, ErrInvalidFontData
}

if checksumAdjustmentPos == 0 {
} else if checksumAdjustmentPos == 0 {
return nil, ErrInvalidFontData
} else {
checksum := 0xB1B0AFBA - calcChecksum(w.Bytes())
// TODO: (WOFF) master checksum seems right, but we don't throw an error if it is off
//if checkSumAdjustment != checksum {
// return nil, fmt.Errorf("bad checksum")
//}
checksumAdjustment = checksum
}

checksum := 0xB1B0AFBA - calcChecksum(w.Bytes())
// TODO: (WOFF) master checksum seems right, but we don't throw an error if it is off
//if checkSumAdjustment != checksum {
// return nil, fmt.Errorf("bad checksum")
//}
checksumAdjustment = checksum

// replace overal checksum in head table
buf := w.Bytes()
binary.BigEndian.PutUint32(buf[checksumAdjustmentPos:], checksumAdjustment)
Expand Down
9 changes: 4 additions & 5 deletions font/woff2.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,10 @@ func ParseWOFF2(b []byte) ([]byte, error) {
iHead, hasHead := tagTableIndex["head"]
if !hasHead || len(tables[iHead].data) < 18 {
return nil, fmt.Errorf("head: must be present")
} else {
binary.BigEndian.PutUint32(tables[iHead].data[8:], 0x00000000) // clear checkSumAdjustment
if flags := binary.BigEndian.Uint16(tables[iHead].data[16:]); flags&0x0800 == 0 {
return nil, fmt.Errorf("head: bit 11 in flags must be set")
}
}
binary.BigEndian.PutUint32(tables[iHead].data[8:], 0x00000000) // clear checkSumAdjustment
if flags := binary.BigEndian.Uint16(tables[iHead].data[16:]); flags&0x0800 == 0 {
return nil, fmt.Errorf("head: bit 11 in flags must be set")
}

if _, hasDSIG := tagTableIndex["DSIG"]; hasDSIG {
Expand Down
70 changes: 35 additions & 35 deletions layout/layouts.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
package layout

import "math"

type Size struct {
MinWidth, MaxWidth float64
MinHeight, MaxHeight float64
}

func newSize() Size {
return Size{
MinWidth: 0.0,
MaxWidth: math.Inf(1.0),
MinHeight: 0.0,
MaxHeight: math.Inf(1.0),
}
}

type Item interface {
Size() Size
}

type Layout struct {
items []Item
}

func (l *Layout) Size() Size {
size := newSize()
for _, item := range l.items {
isize := item.Size()
size.MinWidth = math.Max(size.MinWidth, isize.MinWidth)
size.MaxWidth = math.Min(size.MaxWidth, isize.MaxWidth)
size.MinHeight += isize.MinHeight
size.MaxHeight += isize.MaxHeight
}
return size
}
//import "math"
//
//type Size struct {
// MinWidth, MaxWidth float64
// MinHeight, MaxHeight float64
//}
//
//func newSize() Size {
// return Size{
// MinWidth: 0.0,
// MaxWidth: math.Inf(1.0),
// MinHeight: 0.0,
// MaxHeight: math.Inf(1.0),
// }
//}
//
//type Item interface {
// Size() Size
//}
//
//type Layout struct {
// items []Item
//}
//
//func (l *Layout) Size() Size {
// size := newSize()
// for _, item := range l.items {
// isize := item.Size()
// size.MinWidth = math.Max(size.MinWidth, isize.MinWidth)
// size.MaxWidth = math.Min(size.MaxWidth, isize.MaxWidth)
// size.MinHeight += isize.MinHeight
// size.MaxHeight += isize.MaxHeight
// }
// return size
//}
4 changes: 2 additions & 2 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (p *Path) Copy() *Path {
return q
}

// Append appends path q to p and returns a new path if succesful (otherwise either p or q are returned).
// Append appends path q to p and returns a new path if successful (otherwise either p or q are returned).
func (p *Path) Append(q *Path) *Path {
if q == nil || q.Empty() {
return p
Expand All @@ -111,7 +111,7 @@ func (p *Path) Append(q *Path) *Path {
return &Path{append(p.d, q.d...)}
}

// Join joins path q to p and returns a new path if succesful (otherwise either p or q are returned). Its like executing the commands in q to p in sequence, where if the first MoveTo of q doesn't coincide with p it will fallback to appending the paths.
// Join joins path q to p and returns a new path if successful (otherwise either p or q are returned). Its like executing the commands in q to p in sequence, where if the first MoveTo of q doesn't coincide with p it will fallback to appending the paths.
func (p *Path) Join(q *Path) *Path {
if q == nil || q.Empty() {
return p
Expand Down
1 change: 1 addition & 0 deletions tests/font/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fuzz

import "github.com/tdewolff/canvas"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
ff := canvas.NewFontFamily("")
_ = ff.LoadFont(data, canvas.FontRegular)
Expand Down
1 change: 1 addition & 0 deletions tests/latex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzz

import "github.com/tdewolff/canvas"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
_, _ = canvas.ParseLaTeX(string(data))
return 1
Expand Down
1 change: 1 addition & 0 deletions tests/svg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzz

import "github.com/tdewolff/canvas"

// Fuzz is a fuzz test.
func Fuzz(data []byte) int {
_, _ = canvas.ParseSVG(string(data))
return 1
Expand Down
Loading

0 comments on commit e077290

Please sign in to comment.