# mupdf-annots.test --
#
#	page annotations
#

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

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/*]

#
# -- Extracting/Setting annotations from sample file Book-annotated.pdf
#    This sample contains many annotations:
#      underline
#      highlight
#      circle
#      ...
#
# All the following tests are based on a common setup:

    set pdf [mupdf::open [file join $samplesDir Book-annotated.pdf]]



Name: blocks-list
Description: {list blocks of a page}
Test: {
    set p [$pdf getpage 0]
    set L [$p blocks]
}
Expected: \
{{textblock 257.57000732421875 78.52999877929688 343.8846435546875 106.48999786376953}\
{textblock 55.91999816894531 151.79920959472656 538.0729370117188 243.25999450683594}\
{textblock 55.91999816894531 428.3299865722656 540.571044921875 499.6300048828125}\
{textblock 55.91999816894531 704.4999389648438 529.6358032226562 775.656005859375}\
{imageblock 221.6999969482422 255.33999633789062 373.25 413.5899963378906}\
{imageblock 140.26998901367188 512.3299560546875 454.66998291015625 690.3799438476562}}

Name: lines-list
Description: {list lines of a page}
Test: {
	set p [$pdf getpage 0]
	llength [$p lines]
}
Expected: 17


Name: annotation-list
Description: {list of annotations}
Test: {
	set p [$pdf getpage 0]
	set L1 [$p annots]       ;# 4 annotations
	set p [$pdf getpage 1]
	set L2 [$p annots]       ;#should be empty
	return [list $L1 $L2]    
}
Expected: {{66 68 70 72} {}}

Name: annot-NEG
Description: {annot wrong args}
Test: {
	set p [$pdf getpage 0]
	set L1 [$p annot]
}
Expected: error glob "wrong # args: *"


Name: annot-ID-NEG
Description: {annot with an invalid ID}
Test: {
	set p [$pdf getpage 0]
	set L1 [$p annot get 999999]
}
Expected: error glob "invalid annotID \"*\""

Name: annot-ID-short-NEG
Description: {annot with an invalid ID (short syntax)}
Test: {
	set p [$pdf getpage 0]
	set L1 [$p annot 999999]
}
Expected: error glob "invalid annotID \"*\""

Name: annot-get-all-attributes-1
Description: {annot get attributes 1}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set attribs [$p annot get [lindex $annots 0]]
	 # attributes are not listed in a fixed order
	 # so, oder them here for comparison purpose
	lsort -stride 2 $attribs
}
Expected: {-color #ff5400 -type highlight -vertices {307.989990234375 217.219970703125 420.1449890136719 228.260009765625}}

Name: annot-get-no-rect
Description: {try to get the -rect attribute for some annotations who doesn't allow them} 
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	$p annot get [lindex $annots 0] -rect
}
Expected: error "Highlight annotations have no Rect property"

Name: annot-get-all-attributes-2
Description: {get attributes 2}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	 # the last annot is a circle , currently only common attributes are supported.
	$p annot [lindex $annots end]
}
Expected: {-type circle -rect {224.2030029296875 66.636962890625 408.1180114746094 130.66400146484375} -color #e42136}

Name: annot-get-attributes-2-short
Description: {get attributes 2 (short syntax)}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	 # the last annot is a circle , currently only common attributes are supported.
	$p annot [lindex $annots end]
}
Expected: {-type circle -rect {224.2030029296875 66.636962890625 408.1180114746094 130.66400146484375} -color #e42136}

Name: annot-get-NEG
Description: {getting and invalid attribute}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set L1 [$p annot get [lindex $annots 0] -wrongattrib]
}
Expected: error glob {unrecognized attribute "-wrongattrib" for a "highlight" annotation. Valid attributes are:*}

Name: annot-get
Description: {getting an attribute}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set L1 [$p annot get [lindex $annots 0] -type]
}
Expected: "highlight"

Name: annot-get-short
Description: {getting an attribute (short syntax)}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set L1 [$p annot [lindex $annots 0] -type]
}
Expected: "highlight"


# ---- changing attributes

Name: annot-set
Description: {setting an attribute}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set annotID [lindex $annots 0]
	$p annot set $annotID -color gray25
	$p annot get $annotID -color
}
Expected: "#404040"

Name: annot-set-short
Description: {setting an attribute (short syntax)}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	$p annot [lindex $annots 0] -color magenta
}
Expected: ""

Name: annot-set-multi-short
Description: {setting an attribute (short syntax)}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set annotID [lindex $annots 0]
	$p annot $annotID -color yellow -vertices {1.0 2.0 3.0 4.0}
	set L []
	lappend L [$p annot $annotID -color]
	lappend L [$p annot $annotID -vertices]
	return $L
}
Expected: {{#ffff00} {1.0 2.0 3.0 4.0}}

Name: annot-set-NEG1
Description: {setting an attribute -wrong value}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	$p annot set [lindex $annots 0] -color badcolor
}
Expected: error glob "unrecognized color *"

Name: annot-set-NEG2
Description: {setting a readonly attribute}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	$p annot set [lindex $annots 0] -type underline
}
Expected: error {"-type" is a read-only option.}

Name: annot-set-NEG3
Description: {annot set with missing (odd) values}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	$p annot set [lindex $annots 0] -color yellow -color
}
Expected: error {Missing value for the last option "-color"}

# -- delete annot -------------------------------------------------------------

Name: annot-delete
Description: {delete an annotation}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set annotID [lindex $annots end]
	$p annot delete $annotID
	 # check if it's still present ...
	expr {$annotID in [$p annots]}
}
Expected: 0

Name: annot-invalidOp-NEG
Description: {invalid op on a given annotation}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set annotID [lindex $annots end]
	$p annot wrongop $annotID ...
}
Expected: error {unrecognized annot's subcmd "wrongop"}

Name: annot-delete-list-NEG
Description: {try to delete a list of annotations with an invalid annot}
Test: {
	set p [$pdf getpage 0]
	set n [llength [$p annots]]
	set res [catch {$p annot delete {*}[$p annots] xxx}]  ;# all annotations plus xxx
	 # check the number of still present annotations ...
	set changed [expr {$n - [llength [$p annots]]}]
	list $res $changed
}
Expected: {1 0}

Name: annot-delete-list
Description: {delete a list of annotations}
Test: {
	set p [$pdf getpage 0]
	$p annot delete {*}[$p annots]   ;# all annotations
	 # check the number of still present annotations ...
	llength [$p annots]
}
Expected: 0


  # ----------------------------------------------------------
  # after these changes, quit and reopen the original document
  # ----------------------------------------------------------
$pdf quit
set pdf [mupdf::open [file join $samplesDir Book-annotated.pdf]]


# -- create annot -------------------------------------------------------------

Name: annot-create-highlight
Description: {create an annotation (highlight)}
Test: {
	set p [$pdf getpage 0]
	# get the 1st line-box and highlight it.
	set R [lindex [$p lines] 0]
	set id [$p annot create highlight -color lightgreen -vertices $R]
	# check new annot
	$p annot get $id -type
}
Expected: "highlight"

Name: annot-create.NEG
Description: {try to create an annotation with a wrong type}
Test: {
	set p [$pdf getpage 0]
	set id [$p annot create wrongtype -color lightblue]
}
Expected: error glob {unrecognized annotation type "wrongtype". Valid annotations are: *}


Name: annot-create-stamp
Description: {create an annotation (stamp)}
Setup: {
	# graft some symbols from Review-Stamps.pdf
	set spdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
	$pdf graft [$spdf getpage 11]
	$pdf graft [$spdf getpage 12]
	$spdf quit
}
Test: {
	set page [$pdf getpage 0]
	
	# get the last graft
	set graftID [lindex [$pdf grafts] end]
	
	set annotID [$page annot create stamp \
		-xobject $graftID -rotate 20 -to {300.0 100.0} -scale {1.5} \
	]
	
	$page annot get $annotID
}
Expected: {-type stamp -rect {224.29164123535156 49.8223876953125 375.7083740234375 150.1776123046875} -color #ff0000 -xobject XObj#1 -rotate 20 -scale 1.5 -to {300.0 100.0}}

Name: annot-change-stamp
Description: {change a stamp}
Setup: {
	##  This test depends on the previous one ##
}
Test: {
	# change graft and position
	set page [$pdf getpage 0]
	
	# get the first graft
	set graftID [lindex [$pdf grafts] 0]
	
	# search for the stamp annotation ( created with the previous test)
	set annotID {}
	foreach aID [$page annots] {
		if { [$page annot get $aID -type] == "stamp" } {
			set annotID $aID
			break
		}
	}
	$page annot set $annotID -to {100.0 600.0} -xobject $graftID
	$page annot get $annotID
}
Expected: {-type stamp -rect {24.291643142700195 549.8224487304688 175.7083740234375 650.1776123046875} -color #ff0000 -xobject XObj#0 -rotate 20 -scale 1.5 -to {100.00000762939453 600.0}}


# -- flatten annot ------------------------------------------------------------

Name: annot-flatten
Description: {flatten an annotation}
Test: {
	set p [$pdf getpage 0]
	set annots [$p annots]
	set annotID [lindex $annots 0]
	$p annot flatten $annotID
	
	if { $annotID in [$p annots] } { return "KO" }
	return "OK"
}
Expected: "OK"

Name: annot-flatten-list
Description: {flatten a list of annotations}
Test: {
	set p [$pdf getpage 0]
	$p annot flatten {*}[$p annots]   ;# all annotations
	 # check the number of still present annotations ...
	llength [$p annots]
}
Expected: 0


  # ----------------------------------------------------------
  # after these changes, quit and reopen the original document
  # ----------------------------------------------------------
	$pdf quit
	set pdf [mupdf::open [file join $samplesDir Book-annotated.pdf]]


# ============================================================================
# -- Common cleanup for tests based on Book-annotated.pdf
# ============================================================================
	$pdf quit
	# don't cleanup TmpResultDir; it may be used for inspection

# #############################################################################



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