# ============================================================================
# mupdf-embedpage.test --
#
#	Tests about embedding pages.
#

package require TTXN
  # package under test is tclMuPDF 2.5.1
package require tclMuPDF 2.5.1

set thisDir [file dirname [file normalize [info script]]]
set samplesDir [file join $thisDir samples]
set tmpResultDir [file join $thisDir tmpResults]
file mkdir $tmpResultDir
file delete {*}[glob -nocomplain $tmpResultDir/*]

# =============================================================================


# ====================================================
# All the following tests are based on a common setup:
# ====================================================
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]


Name: graft.neg.1
Description: {graft a wrong(invalid) page}
Test: {
	$pdf graft fakePage
}
Expected: error glob {"*" must be a mupdf::Page}


Name: embed.neg.1
Description: {embed missing params}
Test: {
	$pdf embed
}
Expected: error glob {wrong # args: should be "* embed graftKey pageNum ?arg ...?"}

Name: embed.neg.2
Description: {embed wrong graftKey}
Test: {
	$pdf embed fakeKey 0
}
Expected: error glob {invalid graft-key "fakeKey"}

Name: embed.neg.3
Description: {embed invalid pagenumber}
Setup: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
	set graftID1 [$pdf graft [$stampPdf getpage 12]]
	$pdf embed $graftID1 999
}
Cleanup: { 
	$stampPdf quit
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {invalid page number: 1000}

Name: embed.neg.4
Description: {embed wrong options}
Setup: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
	set graftID1 [$pdf graft [$stampPdf getpage 12]]
	$pdf embed $graftID1 0 -scale 0.5 -wrongopt 123
}
Cleanup: { 
	$stampPdf quit
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {unrecognized option "-wrongopt". Valid options are: -crop,-to,-scale,-rotate,-over}

Name: embed.neg.5
Description: {embed wrong options - malformed -crop}
Setup: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
	set graftID1 [$pdf graft [$stampPdf getpage 12]]
	$pdf embed $graftID1 0 -scale 0.5 -crop {100 100 xxx 1000}
}
Cleanup: { 
	$stampPdf quit
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {malformed sequence of floating point numbers: expected floating-point number but got "xxx"}


Name: graft
Description: {graft (and discard)}
Test: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
	set graftID1 [$pdf graft [$stampPdf getpage 23]] ;# it's a stamp 94x37
	set graftID2 [$pdf graft [$stampPdf getpage 20]] ;# it's a stamp 94x37
	$stampPdf quit

	return [llength [$pdf grafts]]
}
Cleanup: {
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: 2


Name: graft-and-embed
Description: {graft and embed .. check all options}
Test: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
	set changes0 [$pdf haschanges]
	set graftID1 [$pdf graft [$stampPdf getpage 23]] ;# it's a stamp 94x37
	set changes1 [$pdf haschanges]
	set graftID2 [$pdf graft [$stampPdf getpage 20]] ;# it's a stamp 94x37
	$stampPdf quit

	 # embed on the first (0-th) page.
	set pageNumber 0
	$pdf embed $graftID1 $pageNumber
	set changes2 [$pdf haschanges]
	$pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 300} -over true

	 # embed under the text
	$pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 400} -over false

	 # stamp just a portion
	$pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 500} -crop {50 0 94 36}

	 # rotated stamp on last page
	set pageNumber [expr {[$pdf npages]-1}]
	 # note that the pivot for the rotation is the top-left corner
	$pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 500} -rotate -30

	$pdf export  [file join $tmpResultDir Stamped-Book.pdf]
	return [list $changes0 $changes1 $changes2]  ;;#  you should visually check
}
Cleanup: {
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: {0 1 1}


Name: BUGFIX-graft-and-save
	#  NOTE: v2.4.2 before bugfixing, result was a CRASH
Description: {graft and save}
Setup: {
	set basePdf [mupdf::open [file join $samplesDir Sample-01.pdf]]
}
Test: {
	 # page #2 (i.e. the 3rd page) should be inserted in a new pdf
	set basePageObj [$basePdf getpage 2]

	 # create a new empty pdf
	set new_pdfObj [mupdf::new [file join $tmpResultDir OnePage.pdf]]
	 # add a new (blanc) page
	$new_pdfObj addpage end -size {*}[$basePageObj size]
	 # now new_pdfObj has just 1 (blanc) page
	set graftId [$new_pdfObj graft $basePageObj]
	$new_pdfObj embed $graftId 0
	$new_pdfObj close    ;# saved !   ** THIS CAUSED a CRASH *
	file exists [file join $tmpResultDir OnePage.pdf]
}
Cleanup: {
	$basePdf quit
	file delete [file join $tmpResultDir OnePage.pdf]
}
Expected: 1

Name: embed-anchor
Description: {graft and embed .. with -anchor option}
Test: {
	set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
	set graftID1 [$pdf graft [$stampPdf getpage 23]] ;# it's a stamp 94x37
	$stampPdf quit

	 # embed on the first (0-th) page. north side
	set pageNumber 0
	set pagesize [[$pdf getpage 0] size]
	lassign $pagesize w h
	$pdf embed $graftID1 $pageNumber -anchor n -to [list [expr {$w/2.0}] 0]
	return OK
}
Cleanup: {
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: OK



 # =================================================================
 # WARNING: You also need a visual testing !!!
 # =================================================================

#
# -- Common cleanup
#
	$pdf quit


# === cleanup ========================================
::tcltest::cleanupTests
