-
Notifications
You must be signed in to change notification settings - Fork 39
/
dimensions.go
59 lines (47 loc) · 1.68 KB
/
dimensions.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
package main
import (
"math"
)
type Dimensions struct {
}
type Rect struct {
Width float64
Height float64
}
func NewDimensions() *Dimensions {
return &Dimensions{}
}
func (d *Dimensions) Compute(contents *Contents, meta *ContentsImage, sourceRect Rect) Rect {
if meta.Size != "" {
return dimensionFromSize(meta)
}
//
// If we don't get an explicit size we have to compute it.
// 1. Find the biggest scale factor in the requested idiom group
// 2. Assuming the source image is the largest possible image we have,
// compute a reduction scale factor (current / highest), and scale down the source.
// e.g if highest scale factor for iPad is x3, and the current image request is x2,
// we need to scale down the source image by 2/3.
//
// an idiom is just meta.Idiom, however with Apple Watch we have the same idiom with different
// screen sizes. Feels like Apple patched this abstraction out with 'screenWidth'. So let's
// compose a new idiom built from the original idiom and this screenWidth concept.
idiom := meta.Idiom + meta.ScreenWidth
highestFactor := 1.0
for _, m := range contents.Images {
if m.Idiom+m.ScreenWidth == idiom {
highestFactor = math.Max(float64(highestFactor), float64(m.GetScale()))
}
}
scaleDownFactor := float64(meta.GetScale()) / highestFactor
//log.Printf("%v scale factor: %1.2f computed from %d and highest %2.0f", meta, scaleDownFactor, meta.GetScale(), highestFactor)
return Rect{
float64(sourceRect.Width) * scaleDownFactor,
float64(sourceRect.Height) * scaleDownFactor,
}
}
func dimensionFromSize(c *ContentsImage) Rect {
w, h := c.GetSize()
factor := float64(c.GetScale())
return Rect{Width: factor * w, Height: factor * h}
}