github.com/boki/go-xmp@v1.0.1/models/pdf/model.go (about)

     1  // Copyright (c) 2017-2018 Alexander Eichhorn
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  // Package pdf implements metadata for PDF files as defined by XMP Specification Part 2.
    16  package pdf
    17  
    18  import (
    19  	"fmt"
    20  	"trimmer.io/go-xmp/models/dc"
    21  	"trimmer.io/go-xmp/models/xmp_base"
    22  	"trimmer.io/go-xmp/xmp"
    23  )
    24  
    25  var (
    26  	NsPDF = xmp.NewNamespace("pdf", "http://ns.adobe.com/pdf/1.3/", NewModel)
    27  )
    28  
    29  func init() {
    30  	xmp.Register(NsPDF, xmp.XmpMetadata)
    31  }
    32  
    33  func NewModel(name string) xmp.Model {
    34  	return &PDFInfo{}
    35  }
    36  
    37  func MakeModel(d *xmp.Document) (*PDFInfo, error) {
    38  	m, err := d.MakeModel(NsPDF)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	x, _ := m.(*PDFInfo)
    43  	return x, nil
    44  }
    45  
    46  func FindModel(d *xmp.Document) *PDFInfo {
    47  	if m := d.FindModel(NsPDF); m != nil {
    48  		return m.(*PDFInfo)
    49  	}
    50  	return nil
    51  }
    52  
    53  // Part 2: 3.1 Adobe PDF namespace
    54  type PDFInfo struct {
    55  	Title        xmp.AltString  `xmp:"dc:title"`
    56  	Author       xmp.StringList `xmp:"dc:creator"`
    57  	Subject      xmp.AltString  `xmp:"dc:description"`
    58  	Keywords     string         `xmp:"pdf:Keywords"`
    59  	Creator      xmp.AgentName  `xmp:"xmp:CreatorTool"`
    60  	Producer     xmp.AgentName  `xmp:"pdf:Producer"`
    61  	PDFVersion   string         `xmp:"pdf:PDFVersion"`
    62  	CreationDate xmp.Date       `xmp:"xmp:CreateDate"`
    63  	ModifyDate   xmp.Date       `xmp:"xmp:ModifyDate"`
    64  	Trapped      xmp.Bool       `xmp:"pdf:Trapped"`
    65  	Copyright    string         `xmp:"pdf:Copyright"`
    66  	Marked       xmp.Bool       `xmp:"pdf:Marked"`
    67  }
    68  
    69  func (x PDFInfo) Can(nsName string) bool {
    70  	return NsPDF.GetName() == nsName
    71  }
    72  
    73  func (x PDFInfo) Namespaces() xmp.NamespaceList {
    74  	return xmp.NamespaceList{NsPDF}
    75  }
    76  
    77  func (x *PDFInfo) SyncModel(d *xmp.Document) error {
    78  	return nil
    79  }
    80  
    81  func (x *PDFInfo) SyncFromXMP(d *xmp.Document) error {
    82  	if m := dc.FindModel(d); m != nil {
    83  		x.Title = m.Title
    84  		x.Author = m.Creator
    85  		x.Subject = m.Description
    86  		if s := m.Rights.Default(); s != "" {
    87  			x.Copyright = s
    88  		}
    89  	}
    90  	if base := xmpbase.FindModel(d); base != nil {
    91  		x.Creator = base.CreatorTool
    92  		x.CreationDate = base.CreateDate
    93  		x.ModifyDate = base.ModifyDate
    94  	}
    95  	return nil
    96  }
    97  
    98  func (x PDFInfo) SyncToXMP(d *xmp.Document) error {
    99  	m, err := dc.MakeModel(d)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	if len(m.Title) == 0 {
   104  		m.Title = x.Title
   105  	}
   106  	if len(m.Creator) == 0 {
   107  		m.Creator = x.Author
   108  	}
   109  	if len(m.Description) == 0 {
   110  		m.Description = x.Subject
   111  	}
   112  	if len(m.Rights) == 0 {
   113  		m.Rights.AddDefault("", x.Copyright)
   114  	}
   115  
   116  	// XMP base
   117  	base, err := xmpbase.MakeModel(d)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	if base.CreateDate.IsZero() {
   122  		base.CreateDate = x.CreationDate
   123  	}
   124  	if base.ModifyDate.IsZero() {
   125  		base.ModifyDate = x.ModifyDate
   126  	}
   127  	if base.CreatorTool == "" {
   128  		base.CreatorTool = x.Creator
   129  	}
   130  	return nil
   131  }
   132  
   133  func (x *PDFInfo) CanTag(tag string) bool {
   134  	_, err := xmp.GetNativeField(x, tag)
   135  	return err == nil
   136  }
   137  
   138  func (x *PDFInfo) GetTag(tag string) (string, error) {
   139  	if v, err := xmp.GetNativeField(x, tag); err != nil {
   140  		return "", fmt.Errorf("%s: %v", NsPDF.GetName(), err)
   141  	} else {
   142  		return v, nil
   143  	}
   144  }
   145  
   146  func (x *PDFInfo) SetTag(tag, value string) error {
   147  	if err := xmp.SetNativeField(x, tag, value); err != nil {
   148  		return fmt.Errorf("%s: %v", NsPDF.GetName(), err)
   149  	}
   150  	return nil
   151  }