Fix code related golint notices in the model package

This commit is contained in:
Adrian-George Bostan 2019-03-14 18:48:20 +02:00 committed by Gunnsteinn Hall
parent 9cf7868e39
commit 6a887be571
12 changed files with 83 additions and 115 deletions

View File

@ -1521,9 +1521,8 @@ func (cs *PdfColorspaceLab) ColorToRGB(color PdfColor) (PdfColor, error) {
gFunc := func(x float64) float64 { gFunc := func(x float64) float64 {
if x >= 6.0/29 { if x >= 6.0/29 {
return x * x * x return x * x * x
} else {
return 108.0 / 841 * (x - 4/29)
} }
return 108.0 / 841 * (x - 4/29)
} }
lab, ok := color.(*PdfColorLab) lab, ok := color.(*PdfColorLab)
@ -1566,9 +1565,8 @@ func (cs *PdfColorspaceLab) ImageToRGB(img Image) (Image, error) {
g := func(x float64) float64 { g := func(x float64) float64 {
if x >= 6.0/29 { if x >= 6.0/29 {
return x * x * x return x * x * x
} else {
return 108.0 / 841 * (x - 4/29)
} }
return 108.0 / 841 * (x - 4/29)
} }
rgbImage := img rgbImage := img
@ -2111,7 +2109,7 @@ func (cs *PdfColorspaceSpecialPattern) ColorToRGB(color PdfColor) (PdfColor, err
} }
if cs.UnderlyingCS == nil { if cs.UnderlyingCS == nil {
return nil, errors.New("underlying CS not defined.") return nil, errors.New("underlying CS not defined")
} }
return cs.UnderlyingCS.ColorToRGB(patternColor.Color) return cs.UnderlyingCS.ColorToRGB(patternColor.Color)

View File

@ -270,9 +270,9 @@ func newPdfFontFromPdfObject(fontObj core.PdfObject, allowType0 bool) (*PdfFont,
return nil, err return nil, err
} }
return &PdfFont{context: simplefont}, err return &PdfFont{context: simplefont}, err
} else {
return nil, err
} }
return nil, err
} }
font := &PdfFont{} font := &PdfFont{}

View File

@ -105,7 +105,7 @@ func newFontFileFromPdfObject(obj core.PdfObject) (*fontFile, error) {
// Based on pdfbox // Based on pdfbox
func (fontfile *fontFile) loadFromSegments(segment1, segment2 []byte) error { func (fontfile *fontFile) loadFromSegments(segment1, segment2 []byte) error {
common.Log.Trace("loadFromSegments: %d %d", len(segment1), len(segment2)) common.Log.Trace("loadFromSegments: %d %d", len(segment1), len(segment2))
err := fontfile.parseAsciiPart(segment1) err := fontfile.parseASCIIPart(segment1)
if err != nil { if err != nil {
return err return err
} }
@ -117,8 +117,8 @@ func (fontfile *fontFile) loadFromSegments(segment1, segment2 []byte) error {
return nil return nil
} }
// parseAsciiPart parses the ASCII part of the FontFile. // parseASCIIPart parses the ASCII part of the FontFile.
func (fontfile *fontFile) parseAsciiPart(data []byte) error { func (fontfile *fontFile) parseASCIIPart(data []byte) error {
// Uncomment these lines to see the contents of the font file. For debugging. // Uncomment these lines to see the contents of the font file. For debugging.
// fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~^^^~~~~~~~~~~~~~~~~~~~~~~~") // fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~^^^~~~~~~~~~~~~~~~~~~~~~~~")
@ -134,7 +134,7 @@ func (fontfile *fontFile) parseAsciiPart(data []byte) error {
return errors.New("invalid start of ASCII segment") return errors.New("invalid start of ASCII segment")
} }
keySection, encodingSection, err := getAsciiSections(data) keySection, encodingSection, err := getASCIISections(data)
if err != nil { if err != nil {
return err return err
} }
@ -170,14 +170,14 @@ var (
binaryStart = "currentfile eexec" binaryStart = "currentfile eexec"
) )
// getAsciiSections returns two sections of `data`, the ASCII part of the FontFile // getASCIISections returns two sections of `data`, the ASCII part of the FontFile
// - the general key values in `keySection` // - the general key values in `keySection`
// - the encoding in `encodingSection` // - the encoding in `encodingSection`
func getAsciiSections(data []byte) (keySection, encodingSection string, err error) { func getASCIISections(data []byte) (keySection, encodingSection string, err error) {
common.Log.Trace("getAsciiSections: %d ", len(data)) common.Log.Trace("getASCIISections: %d ", len(data))
loc := reDictBegin.FindIndex(data) loc := reDictBegin.FindIndex(data)
if loc == nil { if loc == nil {
common.Log.Debug("ERROR: getAsciiSections. No dict.") common.Log.Debug("ERROR: getASCIISections. No dict.")
return "", "", core.ErrTypeError return "", "", core.ErrTypeError
} }
i0 := loc[1] i0 := loc[1]
@ -192,7 +192,7 @@ func getAsciiSections(data []byte) (keySection, encodingSection string, err erro
i2 := i1 i2 := i1
i = strings.Index(string(data[i2:]), encodingEnd) i = strings.Index(string(data[i2:]), encodingEnd)
if i < 0 { if i < 0 {
common.Log.Debug("ERROR: getAsciiSections. err=%v", err) common.Log.Debug("ERROR: getASCIISections. err=%v", err)
return "", "", core.ErrTypeError return "", "", core.ErrTypeError
} }
i3 := i2 + i i3 := i2 + i

View File

@ -507,10 +507,9 @@ func (f *PdfFunctionType2) ToPdfObject() core.PdfObject {
if f.container != nil { if f.container != nil {
f.container.PdfObject = dict f.container.PdfObject = dict
return f.container return f.container
} else {
return dict
} }
return dict
} }
func (f *PdfFunctionType2) Evaluate(x []float64) ([]float64, error) { func (f *PdfFunctionType2) Evaluate(x []float64) ([]float64, error) {
@ -712,9 +711,9 @@ func (f *PdfFunctionType3) ToPdfObject() core.PdfObject {
if f.container != nil { if f.container != nil {
f.container.PdfObject = dict f.container.PdfObject = dict
return f.container return f.container
} else {
return dict
} }
return dict
} }
// PdfFunctionType4 is a Postscript calculator functions. // PdfFunctionType4 is a Postscript calculator functions.

View File

@ -10,11 +10,13 @@ import (
goimage "image" goimage "image"
gocolor "image/color" gocolor "image/color"
"image/draw" "image/draw"
_ "image/gif"
_ "image/png"
"io" "io"
"math" "math"
// imported for initialization side effects.
_ "image/gif"
_ "image/png"
"github.com/unidoc/unidoc/common" "github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/core" "github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/internal/sampling" "github.com/unidoc/unidoc/pdf/internal/sampling"

View File

@ -135,6 +135,7 @@ func (ttf *TtfType) MakeToUnicode() *cmap.CMap {
return cmap.NewToUnicodeCMap(codeToUnicode) return cmap.NewToUnicodeCMap(codeToUnicode)
} }
// NewEncoder returns a new TrueType font encoder.
func (ttf *TtfType) NewEncoder() textencoding.TextEncoder { func (ttf *TtfType) NewEncoder() textencoding.TextEncoder {
return textencoding.NewTrueTypeFontEncoder(ttf.Chars) return textencoding.NewTrueTypeFontEncoder(ttf.Chars)
} }
@ -547,8 +548,8 @@ func (t *ttfParser) parseCmapFormat0() error {
data := []byte(dataStr) data := []byte(dataStr)
common.Log.Trace("parseCmapFormat0: %s\ndataStr=%+q\ndata=[% 02x]", t.rec.String(), dataStr, data) common.Log.Trace("parseCmapFormat0: %s\ndataStr=%+q\ndata=[% 02x]", t.rec.String(), dataStr, data)
for code, glyphId := range data { for code, glyphID := range data {
t.rec.Chars[rune(code)] = GID(glyphId) t.rec.Chars[rune(code)] = GID(glyphID)
} }
return nil return nil
} }
@ -562,8 +563,8 @@ func (t *ttfParser) parseCmapFormat6() error {
t.rec.String(), firstCode, entryCount) t.rec.String(), firstCode, entryCount)
for i := 0; i < entryCount; i++ { for i := 0; i < entryCount; i++ {
glyphId := GID(t.ReadUShort()) glyphID := GID(t.ReadUShort())
t.rec.Chars[rune(i+firstCode)] = glyphId t.rec.Chars[rune(i+firstCode)] = glyphID
} }
return nil return nil
@ -589,16 +590,12 @@ func (t *ttfParser) parseCmapFormat12() error {
} }
for j := uint32(0); j <= endCode-firstCode; j++ { for j := uint32(0); j <= endCode-firstCode; j++ {
glyphId := startGlyph + j glyphID := startGlyph + j
// if glyphId >= numGlyphs {
// common.Log.Debug("ERROR: Format 12 cmap contains an invalid glyph index")
// break
// }
if firstCode+j > 0x10FFFF { if firstCode+j > 0x10FFFF {
common.Log.Debug("Format 12 cmap contains character beyond UCS-4") common.Log.Debug("Format 12 cmap contains character beyond UCS-4")
} }
t.rec.Chars[rune(i+firstCode)] = GID(glyphId) t.rec.Chars[rune(i+firstCode)] = GID(glyphID)
} }
} }

View File

@ -422,7 +422,7 @@ func (p *PdfPage) getResources() (*PdfPageResources, error) {
if obj := dict.Get("Resources"); obj != nil { if obj := dict.Get("Resources"); obj != nil {
prDict, ok := core.TraceToDirectObject(obj).(*core.PdfObjectDictionary) prDict, ok := core.TraceToDirectObject(obj).(*core.PdfObjectDictionary)
if !ok { if !ok {
return nil, errors.New("invalid resource dict!") return nil, errors.New("invalid resource dict")
} }
resources, err := NewPdfPageResourcesFromDict(prDict) resources, err := NewPdfPageResourcesFromDict(prDict)
@ -553,12 +553,11 @@ func (p *PdfPage) HasXObjectByName(name core.PdfObjectName) bool {
if !has { if !has {
return false return false
} }
if obj := xresDict.Get(name); obj != nil { if obj := xresDict.Get(name); obj != nil {
return true return true
} else {
return false
} }
return false
} }
// GetXObjectByName gets XObject by name. // GetXObjectByName gets XObject by name.
@ -567,12 +566,11 @@ func (p *PdfPage) GetXObjectByName(name core.PdfObjectName) (core.PdfObject, boo
if !has { if !has {
return nil, false return nil, false
} }
if obj := xresDict.Get(name); obj != nil { if obj := xresDict.Get(name); obj != nil {
return obj, true return obj, true
} else {
return nil, false
} }
return nil, false
} }
// HasFontByName checks if has font resource by name. // HasFontByName checks if has font resource by name.
@ -581,12 +579,11 @@ func (p *PdfPage) HasFontByName(name core.PdfObjectName) bool {
if !has { if !has {
return false return false
} }
if obj := fontDict.Get(name); obj != nil { if obj := fontDict.Get(name); obj != nil {
return true return true
} else {
return false
} }
return false
} }
// HasExtGState checks if ExtGState name is available. // HasExtGState checks if ExtGState name is available.
@ -841,28 +838,25 @@ func (p *PdfPage) GetContentStreams() ([]string, error) {
if p.Contents == nil { if p.Contents == nil {
return nil, nil return nil, nil
} }
contents := core.TraceToDirectObject(p.Contents) contents := core.TraceToDirectObject(p.Contents)
if contArray, isArray := contents.(*core.PdfObjectArray); isArray {
// If an array of content streams, append it. var cStreamObjs []core.PdfObject
var cstreams []string if contArray, ok := contents.(*core.PdfObjectArray); ok {
for _, cstreamObj := range contArray.Elements() { cStreamObjs = contArray.Elements()
cstreamStr, err := getContentStreamAsString(cstreamObj)
if err != nil {
return nil, err
}
cstreams = append(cstreams, cstreamStr)
}
return cstreams, nil
} else { } else {
// Only 1 element in place. Wrap inside a new array and add the new one. cStreamObjs = []core.PdfObject{contents}
cstreamStr, err := getContentStreamAsString(contents) }
var cStreams []string
for _, cStreamObj := range cStreamObjs {
cStreamStr, err := getContentStreamAsString(cStreamObj)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cstreams := []string{cstreamStr} cStreams = append(cStreams, cStreamStr)
return cstreams, nil
} }
return cStreams, nil
} }
// GetAllContentStreams gets all the content streams for a page as one string. // GetAllContentStreams gets all the content streams for a page as one string.

View File

@ -76,9 +76,9 @@ type PdfTilingPattern struct {
func (p *PdfTilingPattern) IsColored() bool { func (p *PdfTilingPattern) IsColored() bool {
if p.PaintType != nil && *p.PaintType == 1 { if p.PaintType != nil && *p.PaintType == 1 {
return true return true
} else {
return false
} }
return false
} }
// GetContentStream returns the pattern cell's content stream // GetContentStream returns the pattern cell's content stream

View File

@ -347,15 +347,14 @@ func (r *PdfReader) buildOutlineTree(obj core.PdfObject, parent *PdfOutlineTreeN
} }
return &outlineItem.PdfOutlineTreeNode, &outlineItem.PdfOutlineTreeNode, nil return &outlineItem.PdfOutlineTreeNode, &outlineItem.PdfOutlineTreeNode, nil
} else { }
// Outline dictionary (structure element).
// Outline dictionary (structure element).
outline, err := newPdfOutlineFromIndirectObject(container) outline, err := newPdfOutlineFromIndirectObject(container)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
outline.Parent = parent outline.Parent = parent
//outline.Prev = parent
if firstObj := dict.Get("First"); firstObj != nil { if firstObj := dict.Get("First"); firstObj != nil {
// Has children... // Has children...
@ -373,25 +372,8 @@ func (r *PdfReader) buildOutlineTree(obj core.PdfObject, parent *PdfOutlineTreeN
} }
} }
/*
if nextObj, hasNext := (*dict)["Next"]; hasNext {
nextObj, err = r.traceToObject(nextObj)
if err != nil {
return nil, nil, err
}
if _, isNull := nextObj.(*PdfObjectNull); !isNull {
next, last, err := r.buildOutlineTree(nextObj, parent, &outline.PdfOutlineTreeNode)
if err != nil {
return nil, nil, err
}
outline.Next = next
return &outline.PdfOutlineTreeNode, last, nil
}
}*/
return &outline.PdfOutlineTreeNode, &outline.PdfOutlineTreeNode, nil return &outline.PdfOutlineTreeNode, &outline.PdfOutlineTreeNode, nil
} }
}
// GetOutlineTree returns the outline tree. // GetOutlineTree returns the outline tree.
func (r *PdfReader) GetOutlineTree() *PdfOutlineTreeNode { func (r *PdfReader) GetOutlineTree() *PdfOutlineTreeNode {
@ -531,7 +513,7 @@ func (r *PdfReader) buildPageList(node *core.PdfIndirectObject, parent *core.Pdf
} }
if *objType != "Pages" { if *objType != "Pages" {
common.Log.Debug("ERROR: Table of content containing non Page/Pages object! (%s)", objType) common.Log.Debug("ERROR: Table of content containing non Page/Pages object! (%s)", objType)
return errors.New("table of content containing non Page/Pages object!") return errors.New("table of content containing non Page/Pages object")
} }
// A Pages object. Update the parent. // A Pages object. Update the parent.
@ -680,7 +662,7 @@ func (r *PdfReader) traverseObjectData(o core.PdfObject) error {
if _, isRef := o.(*core.PdfObjectReference); isRef { if _, isRef := o.(*core.PdfObjectReference); isRef {
common.Log.Debug("ERROR: Reader tracing a reference!") common.Log.Debug("ERROR: Reader tracing a reference!")
return errors.New("reader tracing a reference!") return errors.New("reader tracing a reference")
} }
return nil return nil

View File

@ -119,12 +119,11 @@ func (r *PdfPageResources) GetExtGState(keyName core.PdfObjectName) (core.PdfObj
common.Log.Debug("ERROR: Invalid ExtGState entry - not a dict (got %T)", r.ExtGState) common.Log.Debug("ERROR: Invalid ExtGState entry - not a dict (got %T)", r.ExtGState)
return nil, false return nil, false
} }
if obj := dict.Get(keyName); obj != nil { if obj := dict.Get(keyName); obj != nil {
return obj, true return obj, true
} else {
return nil, false
} }
return nil, false
} }
// HasExtGState checks whether a font is defined by the specified keyName. // HasExtGState checks whether a font is defined by the specified keyName.
@ -145,7 +144,6 @@ func (r *PdfPageResources) GetShadingByName(keyName core.PdfObjectName) (*PdfSha
common.Log.Debug("ERROR: Invalid Shading entry - not a dict (got %T)", r.Shading) common.Log.Debug("ERROR: Invalid Shading entry - not a dict (got %T)", r.Shading)
return nil, false return nil, false
} }
if obj := shadingDict.Get(keyName); obj != nil { if obj := shadingDict.Get(keyName); obj != nil {
shading, err := newPdfShadingFromPdfObject(obj) shading, err := newPdfShadingFromPdfObject(obj)
if err != nil { if err != nil {
@ -153,9 +151,9 @@ func (r *PdfPageResources) GetShadingByName(keyName core.PdfObjectName) (*PdfSha
return nil, false return nil, false
} }
return shading, true return shading, true
} else {
return nil, false
} }
return nil, false
} }
// SetShadingByName sets a shading resource specified by keyName. // SetShadingByName sets a shading resource specified by keyName.
@ -185,7 +183,6 @@ func (r *PdfPageResources) GetPatternByName(keyName core.PdfObjectName) (*PdfPat
common.Log.Debug("ERROR: Invalid Pattern entry - not a dict (got %T)", r.Pattern) common.Log.Debug("ERROR: Invalid Pattern entry - not a dict (got %T)", r.Pattern)
return nil, false return nil, false
} }
if obj := patternDict.Get(keyName); obj != nil { if obj := patternDict.Get(keyName); obj != nil {
pattern, err := newPdfPatternFromPdfObject(obj) pattern, err := newPdfPatternFromPdfObject(obj)
if err != nil { if err != nil {
@ -194,9 +191,9 @@ func (r *PdfPageResources) GetPatternByName(keyName core.PdfObjectName) (*PdfPat
} }
return pattern, true return pattern, true
} else {
return nil, false
} }
return nil, false
} }
// SetPatternByName sets a pattern resource specified by keyName. // SetPatternByName sets a pattern resource specified by keyName.
@ -226,12 +223,11 @@ func (r *PdfPageResources) GetFontByName(keyName core.PdfObjectName) (core.PdfOb
common.Log.Debug("ERROR: Font not a dictionary! (got %T)", core.TraceToDirectObject(r.Font)) common.Log.Debug("ERROR: Font not a dictionary! (got %T)", core.TraceToDirectObject(r.Font))
return nil, false return nil, false
} }
if obj := fontDict.Get(keyName); obj != nil { if obj := fontDict.Get(keyName); obj != nil {
return obj, true return obj, true
} else {
return nil, false
} }
return nil, false
} }
// HasFontByName checks whether a font is defined by the specified keyName. // HasFontByName checks whether a font is defined by the specified keyName.
@ -293,9 +289,9 @@ func (r *PdfPageResources) HasXObjectByName(keyName core.PdfObjectName) bool {
obj, _ := r.GetXObjectByName(keyName) obj, _ := r.GetXObjectByName(keyName)
if obj != nil { if obj != nil {
return true return true
} else {
return false
} }
return false
} }
// GenerateXObjectName generates an unused XObject name that can be used for // GenerateXObjectName generates an unused XObject name that can be used for

View File

@ -152,9 +152,9 @@ func NewPdfDate(dateStr string) (PdfDate, error) {
} }
// ToPdfObject converts date to a PDF string object. // ToPdfObject converts date to a PDF string object.
func (date *PdfDate) ToPdfObject() core.PdfObject { func (d *PdfDate) ToPdfObject() core.PdfObject {
str := fmt.Sprintf("D:%.4d%.2d%.2d%.2d%.2d%.2d%c%.2d'%.2d'", str := fmt.Sprintf("D:%.4d%.2d%.2d%.2d%.2d%.2d%c%.2d'%.2d'",
date.year, date.month, date.day, date.hour, date.minute, date.second, d.year, d.month, d.day, d.hour, d.minute, d.second,
date.utOffsetSign, date.utOffsetHours, date.utOffsetMins) d.utOffsetSign, d.utOffsetHours, d.utOffsetMins)
return core.MakeString(str) return core.MakeString(str)
} }

View File

@ -431,7 +431,7 @@ func (w *PdfWriter) AddPage(page *PdfPage) error {
} }
if *otype != "Page" { if *otype != "Page" {
return errors.New("Type != Page (Required).") return errors.New("field Type != Page (Required)")
} }
// Copy inherited fields if missing. // Copy inherited fields if missing.