github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/page_test.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package model
     7  
     8  import (
     9  	"testing"
    10  
    11  	. "github.com/unidoc/unidoc/pdf/core"
    12  )
    13  
    14  /*
    15  func makeReaderForText(txt string) *bufio.Reader {
    16  	buf := []byte(txt)
    17  	bufReader := bytes.NewReader(buf)
    18  	bufferedReader := bufio.NewReader(bufReader)
    19  	return bufferedReader
    20  }
    21  */
    22  
    23  // Test PDF date parsing from string.
    24  func TestDateParse(t *testing.T) {
    25  	// Case 1. Test everything.
    26  	str := "D:20080313232937+01'00'"
    27  	date, err := NewPdfDate(str)
    28  	if err != nil {
    29  		t.Errorf("Fail: %s", err)
    30  		return
    31  	}
    32  	if date.year != 2008 {
    33  		t.Errorf("Year != 2008")
    34  		return
    35  	}
    36  	if date.month != 3 {
    37  		t.Errorf("month != 3")
    38  		return
    39  	}
    40  	if date.day != 13 {
    41  		t.Errorf("Day != 13")
    42  		return
    43  	}
    44  	if date.hour != 23 {
    45  		t.Errorf("Hour != 23 (%d)", date.hour)
    46  		return
    47  	}
    48  	if date.minute != 29 {
    49  		t.Errorf("Minute != 29 (%d)", date.minute)
    50  	}
    51  	if date.second != 37 {
    52  		t.Errorf("Second != 37 (%d)", date.second)
    53  		return
    54  	}
    55  	if date.utOffsetSign != '+' {
    56  		t.Errorf("Invalid offset sign")
    57  		return
    58  	}
    59  	if date.utOffsetHours != 1 {
    60  		t.Errorf("Invalid offset hours")
    61  		return
    62  	}
    63  	if date.utOffsetMins != 0 {
    64  		t.Errorf("Invalid offset minutes")
    65  		return
    66  	}
    67  
    68  	// Case 2: Negative sign.
    69  	str = "D:20150811050933-07'00'"
    70  	date, err = NewPdfDate(str)
    71  	if err != nil {
    72  		t.Errorf("Fail: %s", err)
    73  		return
    74  	}
    75  	if date.utOffsetSign != '-' {
    76  		t.Errorf("Invalid offset sign")
    77  		return
    78  	}
    79  	if date.utOffsetHours != 7 {
    80  		t.Errorf("Invalid offset hours")
    81  		return
    82  	}
    83  
    84  	// Case 3. Offset minutes.
    85  	str = "D:20110807220047+09'30'"
    86  	date, err = NewPdfDate(str)
    87  	if err != nil {
    88  		t.Errorf("Fail: %s", err)
    89  		return
    90  	}
    91  	if date.utOffsetMins != 30 {
    92  		t.Errorf("Offset mins != 30")
    93  		return
    94  	}
    95  
    96  	// Case 4.  Another test from failed file.
    97  	// Minutes not specified at end (assume is 0).
    98  	str = "D:20061023115457-04'"
    99  	date, err = NewPdfDate(str)
   100  	if err != nil {
   101  		t.Errorf("Fail: %s", err)
   102  		return
   103  	}
   104  	if date.year != 2006 {
   105  		t.Errorf("Year != 2006")
   106  		return
   107  	}
   108  	if date.month != 10 {
   109  		t.Errorf("month != 10")
   110  		return
   111  	}
   112  	if date.day != 23 {
   113  		t.Errorf("Day != 23")
   114  		return
   115  	}
   116  	if date.hour != 11 {
   117  		t.Errorf("Hour != 11 (%d)", date.hour)
   118  		return
   119  	}
   120  	if date.minute != 54 {
   121  		t.Errorf("Minute != 29 (%d)", date.minute)
   122  	}
   123  	if date.second != 57 {
   124  		t.Errorf("Second != 37 (%d)", date.second)
   125  		return
   126  	}
   127  	if date.utOffsetSign != '-' {
   128  		t.Errorf("Invalid offset sign")
   129  		return
   130  	}
   131  	if date.utOffsetHours != 4 {
   132  		t.Errorf("Invalid offset hours")
   133  		return
   134  	}
   135  	if date.utOffsetMins != 0 {
   136  		t.Errorf("Invalid offset minutes")
   137  		return
   138  	}
   139  
   140  	// Case 5: Missing some more parameters.
   141  	// Seems that many implementations consider some stuff optional...
   142  	// Not following the standard, but we need to handle it.
   143  	// D:20050823042205
   144  	str = "D:20050823042205"
   145  	date, err = NewPdfDate(str)
   146  	if err != nil {
   147  		t.Errorf("Fail: %s", err)
   148  		return
   149  	}
   150  	if date.year != 2005 {
   151  		t.Errorf("Year != 2005")
   152  		return
   153  	}
   154  	if date.month != 8 {
   155  		t.Errorf("month != 8")
   156  		return
   157  	}
   158  	if date.day != 23 {
   159  		t.Errorf("Day != 23")
   160  		return
   161  	}
   162  	if date.hour != 04 {
   163  		t.Errorf("Hour != 11 (%d)", date.hour)
   164  		return
   165  	}
   166  	if date.minute != 22 {
   167  		t.Errorf("Minute != 29 (%d)", date.minute)
   168  	}
   169  	if date.second != 05 {
   170  		t.Errorf("Second != 37 (%d)", date.second)
   171  		return
   172  	}
   173  	if date.utOffsetHours != 0 {
   174  		t.Errorf("Invalid offset hours")
   175  		return
   176  	}
   177  	if date.utOffsetMins != 0 {
   178  		t.Errorf("Invalid offset minutes")
   179  		return
   180  	}
   181  }
   182  
   183  // Test parsing and building the date.
   184  func TestPdfDateBuild(t *testing.T) {
   185  	// Case 1. Test everything.
   186  	dateStr1 := "D:20080313232937+01'00'"
   187  	date, err := NewPdfDate(dateStr1)
   188  	if err != nil {
   189  		t.Errorf("Fail: %s", err)
   190  		return
   191  	}
   192  
   193  	obj := date.ToPdfObject()
   194  	strObj, ok := obj.(*PdfObjectString)
   195  	if !ok {
   196  		t.Errorf("Date PDF object should be a string")
   197  		return
   198  	}
   199  	if string(*strObj) != dateStr1 {
   200  		t.Errorf("Built date string does not match original (%s)", strObj)
   201  		return
   202  	}
   203  }
   204  
   205  // Test page loading.
   206  func TestPdfPage1(t *testing.T) {
   207  	rawText := `
   208  9 0 obj
   209  <<
   210    /Type /Page
   211    /Parent 3 0 R
   212    /MediaBox [0 0 612 459]
   213    /Contents 13 0 R
   214    /Resources <<
   215      /ProcSet 11 0 R
   216      /ExtGState <<
   217        /GS0 << /BM /Normal >>
   218      >>
   219      /XObject <</Im0 12 0 R>>
   220    >>
   221  >>
   222  endobj
   223      `
   224  	//parser := PdfParser{}
   225  	//parser.reader = makeReaderForText(rawText)
   226  	parser := NewParserFromString(rawText)
   227  
   228  	obj, err := parser.ParseIndirectObject()
   229  	if err != nil {
   230  		t.Errorf("Failed to parse indirect obj (%s)", err)
   231  		return
   232  	}
   233  
   234  	pageObj, ok := obj.(*PdfIndirectObject)
   235  	if !ok {
   236  		t.Errorf("Invalid page object type != dictionary (%q)", obj)
   237  		return
   238  	}
   239  	pageDict, ok := pageObj.PdfObject.(*PdfObjectDictionary)
   240  	if !ok {
   241  		t.Errorf("Page object != dictionary")
   242  		return
   243  	}
   244  
   245  	// Bit of a hacky way to do this.  PDF reader is needed if need to resolve external references,
   246  	// but none in this case, so can just use a dummy instance.
   247  	dummyPdfReader := PdfReader{}
   248  	page, err := dummyPdfReader.newPdfPageFromDict(pageDict)
   249  	if err != nil {
   250  		t.Errorf("Unable to load page (%s)", err)
   251  		return
   252  	}
   253  
   254  	if page.MediaBox.Llx != 0 || page.MediaBox.Lly != 0 {
   255  		t.Errorf("llx, lly != 0,0")
   256  		return
   257  	}
   258  
   259  	if page.MediaBox.Urx != 612 || page.MediaBox.Ury != 459 {
   260  		t.Errorf("urx, ury!= 612 (%f), 459 (%f)", page.MediaBox.Urx, page.MediaBox.Ury)
   261  		return
   262  	}
   263  }
   264  
   265  // Test rectangle parsing and loading.
   266  func TestRect(t *testing.T) {
   267  	rawText := `<< /MediaBox [0 0 613.644043 802.772034] >>`
   268  
   269  	//parser := PdfParser{}
   270  	//parser.reader = makeReaderForText(rawText)
   271  	parser := NewParserFromString(rawText)
   272  
   273  	dict, err := parser.ParseDict()
   274  	if err != nil {
   275  		t.Errorf("Failed to parse dict obj (%s)", err)
   276  		return
   277  	}
   278  
   279  	obj := dict.Get("MediaBox")
   280  	arr, ok := obj.(*PdfObjectArray)
   281  	if !ok {
   282  		t.Errorf("Type != Array")
   283  		return
   284  	}
   285  
   286  	rect, err := NewPdfRectangle(*arr)
   287  	if err != nil {
   288  		t.Errorf("Failed to create rectangle (%s)", err)
   289  		return
   290  	}
   291  
   292  	if rect.Llx != 0 {
   293  		t.Errorf("rect.llx != 0 (%f)", rect.Llx)
   294  		return
   295  	}
   296  
   297  	if rect.Lly != 0 {
   298  		t.Errorf("rect.lly != 0 (%f)", rect.Lly)
   299  		return
   300  	}
   301  
   302  	if rect.Urx != 613.644043 {
   303  		t.Errorf("rect.urx != 613.644043 (%f)", rect.Urx)
   304  		return
   305  	}
   306  	if rect.Ury != 802.772034 {
   307  		t.Errorf("rect.urx != 802.772034 (%f)", rect.Ury)
   308  		return
   309  	}
   310  }