This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
93 lines (79 loc) · 2.8 KB
/
draw.go
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
/*
* proofable-image
* Copyright (C) 2020 Southbank Software Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* @Author: guiguan
* @Date: 2020-07-30T00:23:27+10:00
* @Last modified by: guiguan
* @Last modified time: 2020-08-31T16:32:36+10:00
*/
package main
import (
"image"
"image/color"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
func getImageDrawingSize(imgSize image.Point, containerSize image.Point) f32.Point {
imgRatio := float32(imgSize.X) / float32(imgSize.Y)
if float32(containerSize.X)/float32(containerSize.Y) >= imgRatio {
// bound by container height
return f32.Pt(float32(containerSize.Y)*imgRatio, float32(containerSize.Y))
}
// bound by container width
return f32.Pt(float32(containerSize.X), float32(containerSize.X)/imgRatio)
}
func (s *appState) drawImage(ops *op.Ops, containerSize image.Point) float32 {
imageSize := s.imageSize()
canvasSize := image.Pt(
max(imageSize.X, s.imgcertSize.X), max(imageSize.Y, s.imgcertSize.Y))
drawingSize := getImageDrawingSize(canvasSize, containerSize)
clip.RRect{Rect: f32.Rect(0, 0, drawingSize.X, drawingSize.Y)}.Add(ops)
scale := drawingSize.X / float32(canvasSize.X)
paint.ColorOp{Color: color.RGBA{R: 232, G: 232, B: 232, A: 255}}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0,
float32(s.imgcertSize.X)*scale, float32(s.imgcertSize.Y)*scale)}.Add(ops)
imageOp := paint.NewImageOp(s.img)
imageOp.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0,
float32(imageSize.X)*scale, float32(imageSize.Y)*scale)}.Add(ops)
return scale
}
func (s *appState) drawMismatches(ops *op.Ops, scale float32) {
macro := op.Record(ops)
stack := op.Push(ops)
paint.ColorOp{Color: color.RGBA{R: 255, A: 255}}.Add(ops)
bounds := f32.Rect(0, 0, float32(mismatchBoxSize.X)*scale, float32(mismatchBoxSize.Y)*scale)
clip.Border{Rect: bounds, Width: 3}.Add(ops)
paint.PaintOp{Rect: bounds}.Add(ops)
stack.Pop()
box := macro.Stop()
drawBox := func(p image.Point) {
stack := op.Push(ops)
op.Offset(f32.Pt(
float32(p.X*mismatchBoxSize.X)*scale,
float32(p.Y*mismatchBoxSize.Y)*scale)).Add(ops)
box.Add(ops)
stack.Pop()
}
// draw error boxes in mismatches
for _, v := range s.mismatches {
drawBox(v)
}
}