-
Notifications
You must be signed in to change notification settings - Fork 0
/
Samples.swift
263 lines (185 loc) · 7.24 KB
/
Samples.swift
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import SwiftBlend2D
import TigerSample
func sample1() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
// Attach a rendering context into `img`.
let ctx = BLContext(image: img)!
// Clear the image.
ctx.compOp = .srcCopy
ctx.fillAll()
// Fill some path.
let path = BLPath()
path.moveTo(x: 26, y: 31)
path.cubicTo(x1: 642, y1: 132, x2: 587, y2: -136, x3: 25, y3: 464)
path.cubicTo(x1: 882, y1: 404, x2: 144, y2: 267, x3: 27, y3: 31)
ctx.compOp = .srcOver
ctx.setFillStyleRgba32(0xFFFFFFFF)
ctx.fillPath(path)
// Detach the rendering context from `img`.
ctx.end()
// Let's use some built-in codecs provided by Blend2D.
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-1.bmp", codec: codec)
}
func sample2() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
// Coordinates can be specified now or changed later.
var linear = BLGradient(linear: BLLinearGradientValues(x0: 0, y0: 0, x1: 0, y1: 480))
// Color stops can be added in any order.
linear.addStop(0.0, BLRgba32(argb: 0xFFFFFFFF))
linear.addStop(0.5, BLRgba32(argb: 0xFF5FAFDF))
linear.addStop(1.0, BLRgba32(argb: 0xFF2F5FDF))
// `setFillStyle()` can be used for both colors and styles.
ctx.setFillStyle(linear)
ctx.compOp = .srcOver
ctx.fillRoundRect(x: 40.0, y: 40.0, width: 400.0, height: 400.0, radius: 45.5)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-2.bmp", codec: codec)
}
func sample3() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
// Read an image from file.
let texture = try BLImage(fromFile: "Resources/texture.jpeg")
// Create a pattern and use it to fill a rounded-rect.
let pattern = BLPattern(image: texture)
ctx.compOp = .srcOver
ctx.setFillStyle(pattern)
ctx.fillRoundRect(x: 40.0, y: 40.0, width: 400.0, height: 400.0, radius: 45.5)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-3.bmp", codec: codec)
}
func sample4() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
// Read an image from file.
let texture = try BLImage(fromFile: "Resources/texture.jpeg")
// Rotate by 45 degrees about a point at [240, 240].
ctx.rotate(angle: 0.785398, x: 240.0, y: 240.0)
// Create a pattern.
ctx.compOp = .srcOver
ctx.setFillStyle(BLPattern(image: texture))
ctx.fillRoundRect(x: 50.0, y: 50.0, width: 380.0, height: 380.0, radius: 80.5)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-4.bmp", codec: codec)
}
func sample5() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
// First shape filld by a radial gradient.
var radial = BLGradient(radial: BLRadialGradientValues(x0: 180, y0: 180, x1: 180, y1: 180, r0: 180, r1: 0))
radial.addStop(0.0, BLRgba32(argb: 0xFFFFFFFF))
radial.addStop(1.0, BLRgba32(argb: 0xFFFF6F3F))
ctx.compOp = .srcOver
ctx.setFillStyle(radial)
ctx.fillCircle(x: 180, y: 180, radius: 160)
// Second shape filled by a linear gradient.
var linear = BLGradient(linear: BLLinearGradientValues(x0: 195, y0: 195, x1: 470, y1: 470))
linear.addStop(0.0, BLRgba32(argb: 0xFFFFFFFF))
linear.addStop(1.0, BLRgba32(argb: 0xFF3F9FFF))
ctx.compOp = .difference
ctx.setFillStyle(linear)
ctx.fillRoundRect(x: 195, y: 195, width: 270, height: 270, radius: 25)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-5.bmp", codec: codec)
}
func sample6() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
var linear = BLGradient(linear: BLLinearGradientValues(x0: 0, y0: 0, x1: 0, y1: 480))
linear.addStop(0.0, BLRgba32(argb: 0xFFFFFFFF))
linear.addStop(1.0, BLRgba32(argb: 0xFF1F7FFF))
let path = BLPath()
path.moveTo(x: 119, y: 49)
path.cubicTo(x1: 259, y1: 29, x2: 99, y2: 279, x3: 275, y3: 267)
path.cubicTo(x1: 537, y1: 245, x2: 300, y2: -170, x3: 274, y3: 430)
ctx.compOp = .srcOver
ctx.setStrokeStyle(linear)
ctx.setStrokeWidth(15)
ctx.setStrokeStartCap(.round)
ctx.setStrokeEndCap(.butt)
ctx.strokePath(path)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-6.bmp", codec: codec)
}
func sample7() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
let face = try BLFontFace(fromFile: "Resources/NotoSans-Regular.ttf")
let font = BLFont(fromFace: face, size: 50)
ctx.setFillStyleRgba32(0xFFFFFFFF)
ctx.fillText("Hello Blend2D!", at: BLPoint(x: 60, y: 80), font: font)
ctx.rotate(angle: 0.785398)
ctx.fillText("Rotated Text", at: BLPoint(x: 250, y: 80), font: font)
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-7.bmp", codec: codec)
}
func sample8() throws {
let img = BLImage(width: 480, height: 480, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.compOp = .srcCopy
ctx.fillAll()
ctx.setFillStyle(BLRgba32(argb: 0xFFFFFFFF))
let face = try BLFontFace(fromFile: "Resources/NotoSans-Regular.ttf")
let font = BLFont(fromFace: face, size: 20.0)
let fm = font.metrics
var p = BLPoint(x: 20, y: 190 + Double(fm.ascent))
let text = """
Hello Blend2D!
I'm a simple multiline text example
that uses BLGlyphBuffer and fillGlyphRun!
"""
let lines = text.split(separator: "\n")
for line in lines {
let gb = BLGlyphBuffer(text: line)
font.shape(gb)
let tm = font.getTextMetrics(gb)
p.x = (480.0 - (tm.boundingBox.x1 - tm.boundingBox.x0)) / 2.0
ctx.fillGlyphRun(gb.glyphRun, at: p, font: font)
p.y += Double(fm.ascent + fm.descent + fm.lineGap)
}
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("bl-getting-started-8.bmp", codec: codec)
}
func tigerSample() throws {
let tiger = Tiger()
let img = BLImage(width: TigerData.width, height: TigerData.height, format: .prgb32)
let ctx = BLContext(image: img)!
ctx.setFillStyle(BLRgba32(argb: 0xFF00007F))
ctx.fillAll()
for tp in tiger.paths {
if tp.fill {
ctx.setFillStyle(tp.fillColor)
ctx.fillRule = tp.fillRule
ctx.fillPath(tp.blPath)
}
if tp.stroke {
ctx.setStrokeStyle(tp.strokeColor)
ctx.setStrokeOptions(tp.blStrokeOptions)
ctx.strokePath(tp.blPath)
}
}
ctx.end()
let codec = BLImageCodec(builtInCodec: .bmp)
try img.writeToFile("tiger.bmp", codec: codec)
}