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 {
if x >= 6.0/29 {
return x * x * x
} else {
return 108.0 / 841 * (x - 4/29)
}
return 108.0 / 841 * (x - 4/29)
}
lab, ok := color.(*PdfColorLab)
@ -1566,9 +1565,8 @@ func (cs *PdfColorspaceLab) ImageToRGB(img Image) (Image, error) {
g := func(x float64) float64 {
if x >= 6.0/29 {
return x * x * x
} else {
return 108.0 / 841 * (x - 4/29)
}
return 108.0 / 841 * (x - 4/29)
}
rgbImage := img
@ -2111,7 +2109,7 @@ func (cs *PdfColorspaceSpecialPattern) ColorToRGB(color PdfColor) (PdfColor, err
}
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)

View File

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

View File

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

View File

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

View File

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

View File

@ -135,6 +135,7 @@ func (ttf *TtfType) MakeToUnicode() *cmap.CMap {
return cmap.NewToUnicodeCMap(codeToUnicode)
}
// NewEncoder returns a new TrueType font encoder.
func (ttf *TtfType) NewEncoder() textencoding.TextEncoder {
return textencoding.NewTrueTypeFontEncoder(ttf.Chars)
}
@ -547,8 +548,8 @@ func (t *ttfParser) parseCmapFormat0() error {
data := []byte(dataStr)
common.Log.Trace("parseCmapFormat0: %s\ndataStr=%+q\ndata=[% 02x]", t.rec.String(), dataStr, data)
for code, glyphId := range data {
t.rec.Chars[rune(code)] = GID(glyphId)
for code, glyphID := range data {
t.rec.Chars[rune(code)] = GID(glyphID)
}
return nil
}
@ -562,8 +563,8 @@ func (t *ttfParser) parseCmapFormat6() error {
t.rec.String(), firstCode, entryCount)
for i := 0; i < entryCount; i++ {
glyphId := GID(t.ReadUShort())
t.rec.Chars[rune(i+firstCode)] = glyphId
glyphID := GID(t.ReadUShort())
t.rec.Chars[rune(i+firstCode)] = glyphID
}
return nil
@ -589,16 +590,12 @@ func (t *ttfParser) parseCmapFormat12() error {
}
for j := uint32(0); j <= endCode-firstCode; j++ {
glyphId := startGlyph + j
// if glyphId >= numGlyphs {
// common.Log.Debug("ERROR: Format 12 cmap contains an invalid glyph index")
// break
// }
glyphID := startGlyph + j
if firstCode+j > 0x10FFFF {
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 {
prDict, ok := core.TraceToDirectObject(obj).(*core.PdfObjectDictionary)
if !ok {
return nil, errors.New("invalid resource dict!")
return nil, errors.New("invalid resource dict")
}
resources, err := NewPdfPageResourcesFromDict(prDict)
@ -553,12 +553,11 @@ func (p *PdfPage) HasXObjectByName(name core.PdfObjectName) bool {
if !has {
return false
}
if obj := xresDict.Get(name); obj != nil {
return true
} else {
return false
}
return false
}
// GetXObjectByName gets XObject by name.
@ -567,12 +566,11 @@ func (p *PdfPage) GetXObjectByName(name core.PdfObjectName) (core.PdfObject, boo
if !has {
return nil, false
}
if obj := xresDict.Get(name); obj != nil {
return obj, true
} else {
return nil, false
}
return nil, false
}
// HasFontByName checks if has font resource by name.
@ -581,12 +579,11 @@ func (p *PdfPage) HasFontByName(name core.PdfObjectName) bool {
if !has {
return false
}
if obj := fontDict.Get(name); obj != nil {
return true
} else {
return false
}
return false
}
// HasExtGState checks if ExtGState name is available.
@ -841,28 +838,25 @@ func (p *PdfPage) GetContentStreams() ([]string, error) {
if p.Contents == nil {
return nil, nil
}
contents := core.TraceToDirectObject(p.Contents)
if contArray, isArray := contents.(*core.PdfObjectArray); isArray {
// If an array of content streams, append it.
var cstreams []string
for _, cstreamObj := range contArray.Elements() {
cstreamStr, err := getContentStreamAsString(cstreamObj)
if err != nil {
return nil, err
}
cstreams = append(cstreams, cstreamStr)
}
return cstreams, nil
var cStreamObjs []core.PdfObject
if contArray, ok := contents.(*core.PdfObjectArray); ok {
cStreamObjs = contArray.Elements()
} else {
// Only 1 element in place. Wrap inside a new array and add the new one.
cstreamStr, err := getContentStreamAsString(contents)
cStreamObjs = []core.PdfObject{contents}
}
var cStreams []string
for _, cStreamObj := range cStreamObjs {
cStreamStr, err := getContentStreamAsString(cStreamObj)
if err != nil {
return nil, err
}
cstreams := []string{cstreamStr}
return cstreams, nil
cStreams = append(cStreams, cStreamStr)
}
return cStreams, nil
}
// 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 {
if p.PaintType != nil && *p.PaintType == 1 {
return true
} else {
return false
}
return false
}
// GetContentStream returns the pattern cell's content stream

View File

@ -347,50 +347,32 @@ func (r *PdfReader) buildOutlineTree(obj core.PdfObject, parent *PdfOutlineTreeN
}
return &outlineItem.PdfOutlineTreeNode, &outlineItem.PdfOutlineTreeNode, nil
} else {
// Outline dictionary (structure element).
}
outline, err := newPdfOutlineFromIndirectObject(container)
// Outline dictionary (structure element).
outline, err := newPdfOutlineFromIndirectObject(container)
if err != nil {
return nil, nil, err
}
outline.Parent = parent
if firstObj := dict.Get("First"); firstObj != nil {
// Has children...
firstObj, err = r.traceToObject(firstObj)
if err != nil {
return nil, nil, err
}
outline.Parent = parent
//outline.Prev = parent
if firstObj := dict.Get("First"); firstObj != nil {
// Has children...
firstObj, err = r.traceToObject(firstObj)
if _, isNull := firstObj.(*core.PdfObjectNull); !isNull {
first, last, err := r.buildOutlineTree(firstObj, &outline.PdfOutlineTreeNode, nil)
if err != nil {
return nil, nil, err
}
if _, isNull := firstObj.(*core.PdfObjectNull); !isNull {
first, last, err := r.buildOutlineTree(firstObj, &outline.PdfOutlineTreeNode, nil)
if err != nil {
return nil, nil, err
}
outline.First = first
outline.Last = last
}
outline.First = first
outline.Last = last
}
/*
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.
@ -531,7 +513,7 @@ func (r *PdfReader) buildPageList(node *core.PdfIndirectObject, parent *core.Pdf
}
if *objType != "Pages" {
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.
@ -680,7 +662,7 @@ func (r *PdfReader) traverseObjectData(o core.PdfObject) error {
if _, isRef := o.(*core.PdfObjectReference); isRef {
common.Log.Debug("ERROR: Reader tracing a reference!")
return errors.New("reader tracing a reference!")
return errors.New("reader tracing a reference")
}
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)
return nil, false
}
if obj := dict.Get(keyName); obj != nil {
return obj, true
} else {
return nil, false
}
return nil, false
}
// 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)
return nil, false
}
if obj := shadingDict.Get(keyName); obj != nil {
shading, err := newPdfShadingFromPdfObject(obj)
if err != nil {
@ -153,9 +151,9 @@ func (r *PdfPageResources) GetShadingByName(keyName core.PdfObjectName) (*PdfSha
return nil, false
}
return shading, true
} else {
return nil, false
}
return nil, false
}
// 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)
return nil, false
}
if obj := patternDict.Get(keyName); obj != nil {
pattern, err := newPdfPatternFromPdfObject(obj)
if err != nil {
@ -194,9 +191,9 @@ func (r *PdfPageResources) GetPatternByName(keyName core.PdfObjectName) (*PdfPat
}
return pattern, true
} else {
return nil, false
}
return nil, false
}
// 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))
return nil, false
}
if obj := fontDict.Get(keyName); obj != nil {
return obj, true
} else {
return nil, false
}
return nil, false
}
// 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)
if obj != nil {
return true
} else {
return false
}
return false
}
// 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.
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'",
date.year, date.month, date.day, date.hour, date.minute, date.second,
date.utOffsetSign, date.utOffsetHours, date.utOffsetMins)
d.year, d.month, d.day, d.hour, d.minute, d.second,
d.utOffsetSign, d.utOffsetHours, d.utOffsetMins)
return core.MakeString(str)
}

View File

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