File: constraints.tcl

package info (click to toggle)
libtk-img 1%3A2.0.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,852 kB
  • sloc: ansic: 224,696; sh: 20,589; tcl: 8,921; makefile: 2,272; cpp: 1,933; ada: 1,681; pascal: 1,139; cs: 879; awk: 794; asm: 587; python: 542; xml: 95
file content (171 lines) | stat: -rwxr-xr-x 4,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
namespace eval imgtest {

    namespace export haveTk86 haveTk87
    namespace export haveGs
    namespace export refSize refSize2 refResolution
    namespace export imageInit imageFinish imageCleanup
    namespace export imageNames imageSize
    namespace export imageCompare imageResolution
    namespace export readFile

    variable ImageNames

    proc _versionCompare { version1 version2 } {
        # A wrapper around package vcompare to handle alpha or beta versions
        # containing "a" or "b", ex. 8.7a4.
        set version(1) $version1
        set version(2) $version2
        set versionList(1) [split $version1 "."]
        set versionList(2) [split $version2 "."]
        foreach num { 1 2 } {
            if { [llength $versionList($num)] == 2 } {
                set major [lindex $versionList($num) 0]
                set minor [lindex $versionList($num) 1]
                if { ! [string is integer -strict $minor] } {
                    lassign [split $minor "ab"] minor patch
                    set version($num) [format "%d.%d.%d" $major $minor $patch]
                }
            }
        }
        return [package vcompare $version(1) $version(2)]
    }

    proc haveTk86 {} {
        return [expr [_versionCompare "8.6" $::tk_patchLevel] <= 0]
    }

    proc haveTk87 {} {
        return [expr [_versionCompare "8.7" $::tk_patchLevel] <= 0]
    }

    proc haveGs {} {
        set gsCmds [list "gs" "gswin64c.exe" "gswin32c.exe"]
        foreach gsCmd $gsCmds {
            if { [auto_execok $gsCmd] ne "" } {
                return true
            }
        }
        return false
    }


    proc refSize {} {
        return [list 40 7]
    }

    proc refSize2 {} {
        return [list 80 14]
    }

    proc refResolution {} {
        return [list 80 14]
    }


    proc imageInit {} {
        variable ImageNames

        if {! [info exists ImageNames]} {
            set ImageNames [lsort -dictionary [image names]]
        }
        imageCleanup
        if {[lsort -dictionary [image names]] ne $ImageNames} {
            return -code error "IMAGE NAMES mismatch: [image names] != $ImageNames"
        }
    }

    proc imageFinish {} {
        variable ImageNames

        if {[lsort -dictionary [image names]] ne $ImageNames} {
            return -code error "images remaining: [image names] != $ImageNames"
        }
        imageCleanup
    }

    proc imageCleanup {} {
        variable ImageNames

        foreach img [image names] {
            if {$img ni $ImageNames} {
                image delete $img
            }
        }
    }

    proc imageNames {} {
        variable ImageNames

        set r [list]
        foreach img [image names] {
            if {$img ni $ImageNames} {
                lappend r $img
            }
        }
        return $r
    }

    proc imageSize { phImg } {
        return [list [image width  $phImg] [image height $phImg]]
    }

    proc imageResolution { phImg } {
        set retVal [catch {$phImg cget -metadata} metaDict]
        if { $retVal != 0 } {
            return [list -1 -1]
        }

        if { [dict exists $metaDict DPI] } {
            set dpi [dict get $metaDict DPI]
        }
        if { [dict exists $metaDict aspect] } {
            set aspect [dict get $metaDict aspect]
        }
        if { [info exists dpi] && [info exists aspect] } {
            return [list [expr {int($dpi)}] [expr {int($dpi / $aspect)}]]
        }
        return [list 0 0]
    }

    proc imageCompare { phImg1 phImg2 } {
        set w1 [image width  $phImg1]
        set h1 [image height $phImg1]
        set w2 [image width  $phImg2]
        set h2 [image height $phImg2]
        if { $w1 != $w2 && $h1 != $h2 } {
            return 0
        }
        for { set y 0 } { $y < $h1 } { incr y } {
            for { set x 0 } { $x < $w1 } { incr x } {
                set left  [$phImg1 get $x $y]
                set right [$phImg2 get $x $y]

                set dr [expr { [lindex $right 0] - [lindex $left 0] }]
                if { $dr != 0 } { return 0 }

                set dg [expr { [lindex $right 1] - [lindex $left 1] }]
                if { $dg != 0 } { return 0 }

                set db [expr { [lindex $right 2] - [lindex $left 2] }]
                if { $db != 0 } { return 0 }
            }
        }
        return 1
    }

    proc readFile { name } {
        set fp [open $name r]
        fconfigure $fp -translation binary
        set imgData [read -nonewline $fp]
        close $fp
        return $imgData
    }
}

namespace import -force imgtest::*

tcltest::testConstraint Tk86 [haveTk86]
tcltest::testConstraint Tk87 [haveTk87]

tcltest::testConstraint Ghostscript [haveGs]
tcltest::testConstraint PDF [expr 0]