sugarcube 0.16 → 0.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sugarcube/nsdata.rb +5 -4
- data/lib/sugarcube/uiimage.rb +48 -17
- data/lib/sugarcube/version.rb +1 -1
- data/resources/en.lproj/Localizable.strings +1 -0
- data/spec/nsdata_spec.rb +21 -2
- data/spec/nsstring_files_spec.rb +25 -4
- data/spec/nsstring_spec.rb +66 -0
- metadata +5 -2
data/lib/sugarcube/nsdata.rb
CHANGED
@@ -4,18 +4,19 @@ class NSData
|
|
4
4
|
# @return [NSString]
|
5
5
|
def nsstring(encoding=nil)
|
6
6
|
if encoding
|
7
|
-
NSString.stringWithCString(self.bytes, encoding:encoding)
|
7
|
+
return NSString.stringWithCString(self.bytes, encoding:encoding)
|
8
8
|
else
|
9
|
-
|
9
|
+
|
10
|
+
return NSString.stringWithUTF8String(self)
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
14
|
# @return [UIImage]
|
14
15
|
def uiimage(scale=nil)
|
15
16
|
if scale
|
16
|
-
UIImage.imageWithData(self, scale:scale)
|
17
|
+
return UIImage.imageWithData(self, scale:scale)
|
17
18
|
else
|
18
|
-
UIImage.imageWithData(self)
|
19
|
+
return UIImage.imageWithData(self)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
data/lib/sugarcube/uiimage.rb
CHANGED
@@ -29,6 +29,8 @@ class UIImage
|
|
29
29
|
##|
|
30
30
|
|
31
31
|
# Merges the two images. The target is drawn first, `image` is drawn on top.
|
32
|
+
# The two images are centered, and the maximum size is used so that both
|
33
|
+
# images fit on the canvas.
|
32
34
|
def <<(image)
|
33
35
|
size = self.size
|
34
36
|
if image.size.width > size.width
|
@@ -39,9 +41,13 @@ class UIImage
|
|
39
41
|
end
|
40
42
|
|
41
43
|
UIGraphicsBeginImageContextWithOptions(size, false, self.scale)
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
|
45
|
+
self_position = CGPoint.new((size.width - self.size.width) / 2, (size.width - self.size.width) / 2)
|
46
|
+
self.drawAtPoint(self_position)
|
47
|
+
|
48
|
+
image_position = CGPoint.new((size.width - image.size.width) / 2, (size.width - image.size.width) / 2)
|
49
|
+
image.drawAtPoint(image_position)
|
50
|
+
|
45
51
|
new_image = UIGraphicsGetImageFromCurrentImageContext()
|
46
52
|
UIGraphicsEndImageContext()
|
47
53
|
return new_image
|
@@ -62,11 +68,21 @@ class UIImage
|
|
62
68
|
return sub_image
|
63
69
|
end
|
64
70
|
|
65
|
-
# Delegates to scale_to_fill(position: :center)
|
71
|
+
# Delegates to `scale_to_fill(position: :center)`
|
66
72
|
def scale_to_fill(new_size)
|
67
73
|
scale_to_fill(new_size, position: :center)
|
68
74
|
end
|
69
75
|
|
76
|
+
# Delegates to `scale_to_fill(position: :center, scale: scale)`
|
77
|
+
def scale_to_fill(new_size, scale: scale)
|
78
|
+
scale_to_fill(new_size, position: :center, scale: scale)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Delegates to `scale_to_fill(new_size, position: position, scale: self.scale)`
|
82
|
+
def scale_to_fill(new_size, position:position)
|
83
|
+
scale_to_fill(new_size, position: position, scale: self.scale)
|
84
|
+
end
|
85
|
+
|
70
86
|
# Scales an image to fit within the given size, stretching one or both
|
71
87
|
# dimensions so that it completely fills the area. The current aspect ratio
|
72
88
|
# is maintained. If you want to place an image inside a container image, this
|
@@ -87,10 +103,25 @@ class UIImage
|
|
87
103
|
# are: `[:topleft, :top, :topright, :left, :center, :right, :bottomleft,
|
88
104
|
# :bottom, :bottomright]` (if you forget and use an underscore, like
|
89
105
|
# `top_left`, that'll work, too)
|
106
|
+
# @param scale [Numeric] image scale
|
90
107
|
# @return [UIImage]
|
91
|
-
def scale_to_fill(new_size, position:position)
|
108
|
+
def scale_to_fill(new_size, position:position, scale:scale)
|
92
109
|
new_size = SugarCube::CoreGraphics::Size(new_size)
|
93
110
|
my_size = self.size
|
111
|
+
if new_size.width == my_size.width && new_size.height == my_size.height && self.scale == scale
|
112
|
+
return self
|
113
|
+
end
|
114
|
+
|
115
|
+
# first, scale down; then we'll scale back up if we went too far
|
116
|
+
if my_size.width > new_size.width
|
117
|
+
my_size.height *= new_size.width / my_size.width
|
118
|
+
my_size.width = new_size.width
|
119
|
+
end
|
120
|
+
|
121
|
+
if my_size.height > new_size.height
|
122
|
+
my_size.width *= new_size.height / my_size.height
|
123
|
+
my_size.height = new_size.height
|
124
|
+
end
|
94
125
|
|
95
126
|
if my_size.width < new_size.width
|
96
127
|
my_size.height *= new_size.width / my_size.width
|
@@ -109,29 +140,29 @@ class UIImage
|
|
109
140
|
if position.is_a?(Symbol)
|
110
141
|
min_x = 0
|
111
142
|
min_y = 0
|
112
|
-
max_x = my_size.width
|
113
|
-
max_y = my_size.height
|
143
|
+
max_x = my_size.width
|
144
|
+
max_y = my_size.height
|
114
145
|
mid_x = max_x / 2
|
115
146
|
mid_y = max_y / 2
|
116
147
|
case position
|
117
148
|
when :top_left, :topleft
|
118
|
-
position =
|
149
|
+
position = CGPoint.new(min_x, min_y)
|
119
150
|
when :top
|
120
|
-
position =
|
151
|
+
position = CGPoint.new(mid_x, min_y)
|
121
152
|
when :top_right, :topright
|
122
|
-
position =
|
153
|
+
position = CGPoint.new(max_x, min_y)
|
123
154
|
when :left
|
124
|
-
position =
|
155
|
+
position = CGPoint.new(min_x, mid_x)
|
125
156
|
when :center
|
126
|
-
position =
|
157
|
+
position = CGPoint.new(mid_x, mid_x)
|
127
158
|
when :right
|
128
|
-
position =
|
159
|
+
position = CGPoint.new(max_x, mid_x)
|
129
160
|
when :bottom_left, :bottomleft
|
130
|
-
position =
|
161
|
+
position = CGPoint.new(min_x, max_y)
|
131
162
|
when :bottom
|
132
|
-
position =
|
163
|
+
position = CGPoint.new(mid_x, max_y)
|
133
164
|
when :bottom_right, :bottomright
|
134
|
-
position =
|
165
|
+
position = CGPoint.new(max_x, max_y)
|
135
166
|
else
|
136
167
|
raise "Unknown position #{position.inspect}"
|
137
168
|
end
|
@@ -141,7 +172,7 @@ class UIImage
|
|
141
172
|
thumbnail_x = position.x * (new_size.width - my_size.width) / my_size.width
|
142
173
|
thumbnail_y = position.y * (new_size.height - my_size.height) / my_size.height
|
143
174
|
|
144
|
-
UIGraphicsBeginImageContextWithOptions(new_size, false,
|
175
|
+
UIGraphicsBeginImageContextWithOptions(new_size, false, scale)
|
145
176
|
thumbnail_rect = CGRectZero
|
146
177
|
thumbnail_rect.origin = [thumbnail_x, thumbnail_y]
|
147
178
|
thumbnail_rect.size = my_size
|
data/lib/sugarcube/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
"hello" = "howdy";
|
data/spec/nsdata_spec.rb
CHANGED
@@ -1,11 +1,30 @@
|
|
1
1
|
describe "NSData" do
|
2
2
|
|
3
|
-
it "
|
4
|
-
|
3
|
+
it "native methods should work - ASCII" do
|
4
|
+
NSString.stringWithUTF8String('test'.dataUsingEncoding(NSUTF8StringEncoding)).should == 'test'
|
5
|
+
NSString.stringWithUTF8String('test'.nsdata).should == 'test'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "native methods should work - unicode" do
|
9
|
+
NSString.stringWithUTF8String("t\u0113st".dataUsingEncoding(NSUTF8StringEncoding)).should == "t\u0113st"
|
10
|
+
NSString.stringWithUTF8String("t\u0113st".nsdata).should == "t\u0113st"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "native methods should work - turkey" do
|
14
|
+
NSString.stringWithUTF8String("\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb".dataUsingEncoding(NSUTF8StringEncoding)).should == "\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb"
|
15
|
+
NSString.stringWithUTF8String("\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb".nsdata).should == "\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be able to create an ASCII string from data" do
|
19
|
+
'test'.nsdata.nsstring.should == 'test'
|
5
20
|
end
|
6
21
|
|
7
22
|
it "should be able to create a unicode string from data" do
|
8
23
|
"t\u0113st".nsdata.nsstring.should == "t\u0113st"
|
9
24
|
end
|
10
25
|
|
26
|
+
it "should be able to create a turkey string from data" do
|
27
|
+
"\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb".nsdata.nsstring.should == "\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb"
|
28
|
+
end
|
29
|
+
|
11
30
|
end
|
data/spec/nsstring_files_spec.rb
CHANGED
@@ -1,4 +1,25 @@
|
|
1
|
-
describe NSString do
|
1
|
+
describe 'NSString' do
|
2
|
+
|
3
|
+
it "should have a #document method" do
|
4
|
+
'foo'.document.hasPrefix('/Users').should == true
|
5
|
+
end
|
6
|
+
|
7
|
+
it "should have an #exists? method" do
|
8
|
+
'foo'.document.exists?.should == false
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have a remove! method" do
|
12
|
+
unless 'remove_me'.exists?
|
13
|
+
NSData.data.writeToFile('remove_me'.document, atomically: true)
|
14
|
+
end
|
15
|
+
'remove_me'.document.remove!
|
16
|
+
'remove_me'.exists?.should == false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have a resource_exists? method" do
|
20
|
+
'little_square.png'.resource_exists?.should == true
|
21
|
+
'foo'.resource_exists?.should == false
|
22
|
+
end
|
2
23
|
|
3
24
|
describe 'resource()' do
|
4
25
|
describe '"info.plist".resource' do
|
@@ -10,6 +31,7 @@ describe NSString do
|
|
10
31
|
@it.hasSuffix("SugarCube_spec.app/info.plist").should == true
|
11
32
|
end
|
12
33
|
end
|
34
|
+
|
13
35
|
describe '"PkgInfo".resource' do
|
14
36
|
before { @it = "PkgInfo".resource }
|
15
37
|
it 'should start with "/Users"' do
|
@@ -20,7 +42,6 @@ describe NSString do
|
|
20
42
|
end
|
21
43
|
end
|
22
44
|
end
|
23
|
-
|
24
45
|
|
25
46
|
describe 'resource_url()' do
|
26
47
|
=begin
|
@@ -47,7 +68,7 @@ describe NSString do
|
|
47
68
|
end
|
48
69
|
end
|
49
70
|
end
|
50
|
-
|
71
|
+
|
51
72
|
describe 'info_plist' do
|
52
73
|
describe '"CFBundleVersion".info_plist' do
|
53
74
|
before { @it = "CFBundleVersion".info_plist }
|
@@ -55,7 +76,7 @@ describe NSString do
|
|
55
76
|
@it.should == "1.0"
|
56
77
|
end
|
57
78
|
end
|
58
|
-
|
79
|
+
|
59
80
|
describe '"CFBundleSupportedPlatforms".info_plist' do
|
60
81
|
before { @it = "CFBundleSupportedPlatforms".info_plist }
|
61
82
|
it 'should be ["iPhoneOS"]' do
|
@@ -0,0 +1,66 @@
|
|
1
|
+
describe "NSString" do
|
2
|
+
|
3
|
+
it "should have a #nsurl method" do
|
4
|
+
url = 'https://github.com/status'.nsurl
|
5
|
+
NSURL.should === url
|
6
|
+
url.absoluteString.should == 'https://github.com/status'
|
7
|
+
url.host.should == 'github.com'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have a #nsdata method" do
|
11
|
+
data = 'test'.nsdata
|
12
|
+
NSData.should === data
|
13
|
+
bytes = data.bytes
|
14
|
+
bytes[0].should == 116
|
15
|
+
bytes[1].should == 101
|
16
|
+
bytes[2].should == 115
|
17
|
+
bytes[3].should == 116
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have a #uiimage method" do
|
21
|
+
UIImage.imageNamed('little_square').should == 'little_square'.uiimage
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should have a #uiimageview method" do
|
25
|
+
view = 'little_square'.uiimageview
|
26
|
+
UIView.should === view
|
27
|
+
view.image.should == UIImage.imageNamed('little_square')
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have a #uifont method" do
|
31
|
+
font = 'Helvetica'.uifont
|
32
|
+
UIFont.should === font
|
33
|
+
font.familyName.should == 'Helvetica'
|
34
|
+
font.pointSize.should == UIFont.systemFontSize
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have a #uicolor method" do
|
38
|
+
color = '#ffffff'.uicolor
|
39
|
+
UIColor.should === color
|
40
|
+
color.red.should == 1.0
|
41
|
+
color.green.should == 1.0
|
42
|
+
color.blue.should == 1.0
|
43
|
+
|
44
|
+
color = '#808080'.uicolor
|
45
|
+
UIColor.should === color
|
46
|
+
((color.red * 2).round / 2.0).should == 0.5
|
47
|
+
((color.green * 2).round / 2.0).should == 0.5
|
48
|
+
((color.blue * 2).round / 2.0).should == 0.5
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should have a #escape_url method" do
|
52
|
+
' '.escape_url.should == '%20'
|
53
|
+
'?<>&=;%'.escape_url.should == '%3F%3C%3E%26%3D%3B%25'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should have a #unescape_url method" do
|
57
|
+
'%20'.unescape_url.should == ' '
|
58
|
+
'%3F%3C%3E%26%3D%3B%25'.unescape_url.should == '?<>&=;%'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should have a #localized method" do
|
62
|
+
'hello'.localized.should == 'howdy'
|
63
|
+
'hello'._.should == 'howdy'
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sugarcube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.16.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2013-02-
|
16
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
17
17
|
dependencies: []
|
18
18
|
description: ! '== Description
|
19
19
|
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- resources/Default-568h@2x.png
|
110
110
|
- resources/Default.png
|
111
111
|
- resources/Default@2x.png
|
112
|
+
- resources/en.lproj/Localizable.strings
|
112
113
|
- resources/little_square.png
|
113
114
|
- resources/little_square@2x.png
|
114
115
|
- resources/tall-568h@2x.png
|
@@ -128,6 +129,7 @@ files:
|
|
128
129
|
- spec/nsdate_spec.rb
|
129
130
|
- spec/nsdate_upto_spec.rb
|
130
131
|
- spec/nsstring_files_spec.rb
|
132
|
+
- spec/nsstring_spec.rb
|
131
133
|
- spec/numeric_spec.rb
|
132
134
|
- spec/symbol_uicolor_spec.rb
|
133
135
|
- spec/uicolor_components_spec.rb
|
@@ -176,6 +178,7 @@ test_files:
|
|
176
178
|
- spec/nsdate_spec.rb
|
177
179
|
- spec/nsdate_upto_spec.rb
|
178
180
|
- spec/nsstring_files_spec.rb
|
181
|
+
- spec/nsstring_spec.rb
|
179
182
|
- spec/numeric_spec.rb
|
180
183
|
- spec/symbol_uicolor_spec.rb
|
181
184
|
- spec/uicolor_components_spec.rb
|