skia 1.0.0

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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +12 -0
  3. data/LICENSE +21 -0
  4. data/README.md +205 -0
  5. data/Rakefile +8 -0
  6. data/examples/advanced_features.rb +50 -0
  7. data/examples/avatar_generator.rb +74 -0
  8. data/examples/bar_chart.rb +89 -0
  9. data/examples/basic_drawing.rb +43 -0
  10. data/examples/gradient.rb +32 -0
  11. data/examples/pdf_output.rb +48 -0
  12. data/examples/pdf_stream.rb +23 -0
  13. data/examples/picture_recording.rb +62 -0
  14. data/examples/progress_gauge.rb +103 -0
  15. data/examples/runtime_effect.rb +26 -0
  16. data/examples/social_card.rb +121 -0
  17. data/examples/text_drawing.rb +40 -0
  18. data/lib/skia/base.rb +40 -0
  19. data/lib/skia/bitmap.rb +140 -0
  20. data/lib/skia/canvas.rb +239 -0
  21. data/lib/skia/color.rb +82 -0
  22. data/lib/skia/color_filter.rb +23 -0
  23. data/lib/skia/color_space.rb +44 -0
  24. data/lib/skia/data.rb +47 -0
  25. data/lib/skia/document.rb +222 -0
  26. data/lib/skia/font.rb +118 -0
  27. data/lib/skia/image.rb +216 -0
  28. data/lib/skia/image_filter.rb +29 -0
  29. data/lib/skia/image_info.rb +59 -0
  30. data/lib/skia/mask_filter.rb +26 -0
  31. data/lib/skia/matrix.rb +163 -0
  32. data/lib/skia/native/callbacks.rb +6 -0
  33. data/lib/skia/native/functions.rb +384 -0
  34. data/lib/skia/native/types.rb +400 -0
  35. data/lib/skia/native.rb +67 -0
  36. data/lib/skia/paint.rb +144 -0
  37. data/lib/skia/path.rb +166 -0
  38. data/lib/skia/path_effect.rb +30 -0
  39. data/lib/skia/picture.rb +120 -0
  40. data/lib/skia/pixmap.rb +109 -0
  41. data/lib/skia/point.rb +94 -0
  42. data/lib/skia/rect.rb +179 -0
  43. data/lib/skia/rrect.rb +139 -0
  44. data/lib/skia/runtime_effect.rb +88 -0
  45. data/lib/skia/shader.rb +145 -0
  46. data/lib/skia/surface.rb +272 -0
  47. data/lib/skia/text_blob.rb +47 -0
  48. data/lib/skia/typeface.rb +175 -0
  49. data/lib/skia/version.rb +5 -0
  50. data/lib/skia.rb +42 -0
  51. data/skia-ruby.gemspec +30 -0
  52. metadata +107 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae4770cc8ed617609a1ad1263d895417bcfbd5b47ef4b72a296e0a6ec40caddb
4
+ data.tar.gz: d6a33d5ab0a76620204fb48244f432a5fa8ce46bdff711fa614099ac6e7c1444
5
+ SHA512:
6
+ metadata.gz: 330a03251acafcffe574a2cb2b109db26d69a685ec0b4575a6372c8bc71632bb64fe32c34056f0460d858988c50f72a2ea798cae551ba6c54fb91a7913356bb8
7
+ data.tar.gz: 2deab127ab8d8f44ff3c5126f7160c9ceab0258e89865991d74935487799032b195de0abfa1b5b337ff4ba89648722b38200009439bf8e4e45298bb77ff2ae67
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## Unreleased
9
+
10
+ ## 1.0.0 - 2026-02-15
11
+
12
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yudai Takada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # Skia Ruby
2
+
3
+ Skia bindings for Ruby, providing 2D drawing, image processing, PDF generation, and runtime shader capabilities.
4
+
5
+ <img width="400" alt="Image" src="https://github.com/user-attachments/assets/440971a6-bf10-4cc2-abe6-e5cf28949b95" />
6
+
7
+ ## Status
8
+
9
+ This project provides a practical, SkiaSharp-compatible API surface in Ruby, with a mix of:
10
+
11
+ - native-backed features through SkiaSharp C API
12
+ - Ruby convenience/typed layers for ergonomic usage
13
+
14
+ ### Native-backed (current)
15
+
16
+ - `Surface` / `Canvas` / `Paint` / `Path` / `Image` / `Shader`
17
+ - Shape and path drawing (rect, rrect, arc, points, picture playback)
18
+ - `Document` PDF output
19
+ - file output
20
+ - memory stream output
21
+ - metadata (`title`, `author`, `creation`, `pdfa`, etc.)
22
+ - `Picture` recording / playback / serialization
23
+ - `RuntimeEffect` (SkSL compile and shader creation)
24
+ - Encoders (`PNG`, `JPEG`, `WEBP`)
25
+
26
+ ### Ruby layer (current)
27
+
28
+ - Geometry/value types
29
+ - `Point`, `Rect`, `RRect`, `Matrix`, `ImageInfo`, `ColorSpace`
30
+ - Effect wrappers
31
+ - `MaskFilter`, `ColorFilter`, `ImageFilter`, `PathEffect`
32
+ - Pixel APIs
33
+ - `Bitmap`, `Pixmap`, `read_pixels` helpers
34
+ - Text primitives
35
+ - `Font`, `Typeface`, `TextBlob`
36
+
37
+ ### Native build note
38
+
39
+ - Text layout modules (`skunicode`, `skshaper`, `skparagraph`) are not exposed in the current `libSkiaSharp` build.
40
+ - `skottie` is not exposed in the current `libSkiaSharp` build.
41
+
42
+ ## Requirements
43
+
44
+ - Ruby 3.0+
45
+ - SkiaSharp native library (`libSkiaSharp`)
46
+ - macOS: `libSkiaSharp.dylib`
47
+ - Linux: `libSkiaSharp.so`
48
+ - Windows: `libSkiaSharp.dll`
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'skia'
56
+ ```
57
+
58
+ Then run:
59
+
60
+ ```bash
61
+ bundle install
62
+ ```
63
+
64
+ ## Installing Native Library
65
+
66
+ The gem loads `libSkiaSharp` from project root, current working directory, `SKIA_LIBRARY_PATH`, or system path.
67
+
68
+ ### macOS
69
+
70
+ ```bash
71
+ curl -L -o skiasharp.nupkg \
72
+ https://api.nuget.org/v3-flatcontainer/skiasharp.nativeassets.macos/3.119.2/skiasharp.nativeassets.macos.3.119.2.nupkg
73
+ unzip skiasharp.nupkg -d skiasharp-extract
74
+ cp skiasharp-extract/runtimes/osx/native/libSkiaSharp.dylib .
75
+ ```
76
+
77
+ ### Linux
78
+
79
+ ```bash
80
+ curl -L -o skiasharp.nupkg https://www.nuget.org/api/v2/package/SkiaSharp.NativeAssets.Linux.x64
81
+ unzip skiasharp.nupkg -d skiasharp-extract
82
+ cp skiasharp-extract/runtimes/linux-x64/native/libSkiaSharp.so .
83
+ ```
84
+
85
+ ### Windows
86
+
87
+ ```powershell
88
+ Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/SkiaSharp.NativeAssets.Win32 -OutFile skiasharp.nupkg
89
+ Expand-Archive skiasharp.nupkg -DestinationPath skiasharp-extract
90
+ copy skiasharp-extract\runtimes\win-x64\native\libSkiaSharp.dll .
91
+ ```
92
+
93
+ ## Quick Start
94
+
95
+ ```ruby
96
+ require 'skia'
97
+
98
+ surface = Skia::Surface.make_raster(640, 480)
99
+
100
+ surface.draw do |canvas|
101
+ canvas.clear(Skia::Color::WHITE)
102
+
103
+ paint = Skia::Paint.new
104
+ paint.antialias = true
105
+ paint.color = Skia::Color::RED
106
+
107
+ canvas.draw_circle(320, 240, 100, paint)
108
+ end
109
+
110
+ surface.save_png('output.png')
111
+ ```
112
+
113
+ ## PDF Example
114
+
115
+ ```ruby
116
+ require 'skia'
117
+
118
+ Skia::Document.create_pdf('output.pdf', metadata: {
119
+ title: 'My Report',
120
+ author: 'skia-ruby',
121
+ creation: Time.now,
122
+ raster_dpi: 144.0,
123
+ encoding_quality: 90
124
+ }) do |doc|
125
+ doc.page(612, 792) do |canvas|
126
+ canvas.clear(Skia::Color::WHITE)
127
+
128
+ font = Skia::Font.new(nil, 24.0)
129
+ paint = Skia::Paint.new
130
+ paint.color = Skia::Color::BLACK
131
+
132
+ canvas.draw_text('Hello, PDF!', 50, 100, font, paint)
133
+ end
134
+ end
135
+ ```
136
+
137
+ ## Runtime Shader Example
138
+
139
+ ```ruby
140
+ require 'skia'
141
+
142
+ sksl = <<~SKSL
143
+ half4 main(float2 coord) {
144
+ half r = coord.x / 640.0;
145
+ half g = coord.y / 360.0;
146
+ return half4(r, g, 0.35, 1.0);
147
+ }
148
+ SKSL
149
+
150
+ effect = Skia::RuntimeEffect.make_for_shader(sksl)
151
+ shader = effect.make_shader
152
+
153
+ surface = Skia::Surface.make_raster(640, 360)
154
+ paint = Skia::Paint.new
155
+ paint.shader = shader
156
+
157
+ surface.draw do |canvas|
158
+ canvas.draw_rect(Skia::Rect.from_wh(640, 360), paint)
159
+ end
160
+
161
+ surface.save_png('runtime_effect.png')
162
+ ```
163
+
164
+ ## API Coverage (Skia docs parity)
165
+
166
+ | Area | Status | Notes |
167
+ | --- | --- | --- |
168
+ | Core drawing (`Surface`/`Canvas`/`Paint`/`Path`) | Implemented | Main 2D drawing APIs are available |
169
+ | Shape/text primitives (`RRect`, `TextBlob`) | Implemented | Glyph-positioned drawing is supported via `TextBlob` |
170
+ | Effects (`MaskFilter`, `ColorFilter`, `ImageFilter`, `PathEffect`) | Implemented | Exposed through Ruby wrappers and `Paint` setters |
171
+ | Pixel and color APIs (`Bitmap`, `Pixmap`, `ImageInfo`, `ColorSpace`) | Implemented | Read/copy/access pixel-level data |
172
+ | Image and shader extensions | Implemented | Includes gradients and runtime shader compilation |
173
+ | PDF document APIs | Implemented | File/memory output and metadata are supported |
174
+ | GPU-oriented surface constructors | Partial | Requires externally managed native GPU context pointers |
175
+ | Text layout modules (`skunicode`, `skshaper`, `skparagraph`) | Not implemented | Required native symbols are absent in current build |
176
+ | `skottie` (Lottie) | Not implemented | Required native symbols are absent in current build |
177
+
178
+ ## Development
179
+
180
+ After checking out the repo, install dependencies:
181
+
182
+ ```bash
183
+ bundle install
184
+ ```
185
+
186
+ Run examples:
187
+
188
+ ```bash
189
+ ruby examples/basic_drawing.rb
190
+ ruby examples/gradient.rb
191
+ ruby examples/bar_chart.rb
192
+ ruby examples/advanced_features.rb
193
+ ruby examples/pdf_stream.rb
194
+ ruby examples/runtime_effect.rb
195
+ ```
196
+
197
+ ## License
198
+
199
+ This project is available under the [MIT License](LICENSE).
200
+
201
+ ## Contributing
202
+
203
+ Bug reports and pull requests are welcome at:
204
+
205
+ - https://github.com/ydah/skia-ruby
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ info = Skia::ImageInfo.new(
7
+ width: 640,
8
+ height: 360,
9
+ color_type: :rgba_8888,
10
+ alpha_type: :premul,
11
+ color_space: Skia::ColorSpace.srgb
12
+ )
13
+ surface = Skia::Surface.make_raster(image_info: info)
14
+
15
+ surface.draw do |canvas|
16
+ canvas.clear(Skia::Color::WHITE)
17
+
18
+ rrect = Skia::RRect.from_rect_xy(Skia::Rect.from_xywh(40, 40, 560, 200), 28)
19
+ shader = Skia::Shader.two_point_conical_gradient(
20
+ Skia::Point.new(120, 100),
21
+ 8.0,
22
+ Skia::Point.new(360, 220),
23
+ 260.0,
24
+ [Skia::Color::BLUE, Skia::Color::CYAN, Skia::Color::MAGENTA]
25
+ )
26
+
27
+ fill = Skia::Paint.new
28
+ fill.antialias = true
29
+ fill.shader = shader
30
+ canvas.draw_rrect(rrect, fill)
31
+
32
+ stroke = Skia::Paint.new
33
+ stroke.antialias = true
34
+ stroke.style = :stroke
35
+ stroke.stroke_width = 3.0
36
+ stroke.color = Skia::Color::BLACK
37
+ stroke.path_effect = Skia::PathEffect.dash([10.0, 6.0], phase: 2.0)
38
+ canvas.draw_rrect(rrect.clone.inset(4, 4), stroke)
39
+
40
+ font = Skia::Font.new(nil, 48)
41
+ blob = Skia::TextBlob.from_text('Skia Ruby', font, x: 60, y: 300)
42
+ text_paint = Skia::Paint.new
43
+ text_paint.antialias = true
44
+ text_paint.color = Skia::Color::BLACK
45
+ canvas.draw_text_blob(blob, 0, 0, text_paint) if blob
46
+ end
47
+
48
+ surface.save_png('advanced_features.png')
49
+ pixels = surface.read_pixels(width: 64, height: 64)
50
+ puts "Saved advanced_features.png (sample bytes: #{pixels.bytesize})"
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ def generate_avatar(name, size: 200, output: nil)
7
+ initials = name.split.map { |n| n[0].upcase }.join[0, 2]
8
+
9
+ hash = name.chars.map(&:ord).sum
10
+ hue = hash % 360
11
+
12
+ c = 0.6
13
+ x = c * (1 - ((hue / 60.0) % 2 - 1).abs)
14
+ m = 0.2
15
+
16
+ r, g, b = case hue / 60
17
+ when 0 then [c, x, 0]
18
+ when 1 then [x, c, 0]
19
+ when 2 then [0, c, x]
20
+ when 3 then [0, x, c]
21
+ when 4 then [x, 0, c]
22
+ else [c, 0, x]
23
+ end
24
+
25
+ bg_color = Skia::Color.rgb(
26
+ ((r + m) * 255).to_i,
27
+ ((g + m) * 255).to_i,
28
+ ((b + m) * 255).to_i
29
+ )
30
+
31
+ surface = Skia::Surface.make_raster(size, size)
32
+
33
+ surface.draw do |canvas|
34
+ canvas.clear(Skia::Color::TRANSPARENT)
35
+
36
+ paint = Skia::Paint.new
37
+ paint.antialias = true
38
+
39
+ paint.color = bg_color
40
+ paint.style = :fill
41
+ canvas.draw_circle(size / 2.0, size / 2.0, size / 2.0, paint)
42
+
43
+ font_size = size * 0.4
44
+ font = Skia::Font.new(nil, font_size)
45
+ paint.color = Skia::Color::WHITE
46
+
47
+ text_width, text_bounds = font.measure_text(initials)
48
+ metrics = font.metrics
49
+ x = (size - text_width) / 2.0
50
+ y = (size / 2.0) - (metrics.ascent + metrics.descent) / 2.0
51
+
52
+ canvas.draw_text(initials, x, y, font, paint)
53
+ end
54
+
55
+ output_file = output || "avatar_#{name.downcase.gsub(/\s+/, '_')}.png"
56
+ surface.save_png(output_file)
57
+ puts "Generated: #{output_file}"
58
+ output_file
59
+ end
60
+
61
+ names = [
62
+ 'John Doe',
63
+ 'Alice Smith',
64
+ 'Bob Johnson',
65
+ 'Emma Wilson',
66
+ 'Michael Brown'
67
+ ]
68
+
69
+ puts 'Generating avatars...'
70
+ names.each { |name| generate_avatar(name, size: 128) }
71
+
72
+ generate_avatar('Ruby Developer', size: 256, output: 'avatar_large.png')
73
+
74
+ puts "\nAll avatars generated!"
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ data = {
7
+ 'Maguro' => 92,
8
+ 'Salmon' => 88,
9
+ 'Ebi' => 75,
10
+ 'Tamago' => 68,
11
+ 'Ika' => 45
12
+ }
13
+
14
+ WIDTH = 600
15
+ HEIGHT = 400
16
+ MARGIN = { top: 40, right: 30, bottom: 60, left: 80 }.freeze
17
+ CHART_WIDTH = WIDTH - MARGIN[:left] - MARGIN[:right]
18
+ CHART_HEIGHT = HEIGHT - MARGIN[:top] - MARGIN[:bottom]
19
+
20
+ surface = Skia::Surface.make_raster(WIDTH, HEIGHT)
21
+
22
+ surface.draw do |canvas|
23
+ canvas.clear(Skia::Color::WHITE)
24
+
25
+ paint = Skia::Paint.new
26
+ paint.antialias = true
27
+
28
+ font_title = Skia::Font.new(nil, 20.0)
29
+ paint.color = Skia::Color::BLACK
30
+ canvas.draw_text('Sushi Popularity Ranking', 180, 28, font_title, paint)
31
+
32
+ font_axis = Skia::Font.new(nil, 12.0)
33
+ paint.style = :stroke
34
+ paint.stroke_width = 1
35
+ paint.color = Skia::Color.rgb(200, 200, 200)
36
+
37
+ max_value = 100
38
+ 5.times do |i|
39
+ value = i * 25
40
+ y = MARGIN[:top] + CHART_HEIGHT - (value.to_f / max_value * CHART_HEIGHT)
41
+
42
+ canvas.draw_line(MARGIN[:left], y, WIDTH - MARGIN[:right], y, paint)
43
+
44
+ paint.style = :fill
45
+ paint.color = Skia::Color::BLACK
46
+ canvas.draw_text(value.to_s, MARGIN[:left] - 35, y + 4, font_axis, paint)
47
+ paint.style = :stroke
48
+ paint.color = Skia::Color.rgb(200, 200, 200)
49
+ end
50
+
51
+ bar_width = CHART_WIDTH / data.size * 0.7
52
+ bar_spacing = CHART_WIDTH / data.size
53
+
54
+ colors = [
55
+ Skia::Color.rgb(220, 53, 69),
56
+ Skia::Color.rgb(255, 127, 80),
57
+ Skia::Color.rgb(255, 182, 193),
58
+ Skia::Color.rgb(255, 215, 0),
59
+ Skia::Color.rgb(230, 230, 250)
60
+ ]
61
+
62
+ data.each_with_index do |(name, value), i|
63
+ x = MARGIN[:left] + (i * bar_spacing) + (bar_spacing - bar_width) / 2
64
+ bar_height = (value.to_f / max_value) * CHART_HEIGHT
65
+ y = MARGIN[:top] + CHART_HEIGHT - bar_height
66
+
67
+ paint.style = :fill
68
+ paint.color = colors[i]
69
+ canvas.draw_rect(Skia::Rect.from_xywh(x, y, bar_width, bar_height), paint)
70
+
71
+ paint.style = :stroke
72
+ paint.stroke_width = 1
73
+ paint.color = Skia::Color.rgb(100, 100, 100)
74
+ canvas.draw_rect(Skia::Rect.from_xywh(x, y, bar_width, bar_height), paint)
75
+
76
+ paint.style = :fill
77
+ paint.color = Skia::Color::BLACK
78
+ text_width, = font_axis.measure_text(name)
79
+ label_x = x + (bar_width - text_width) / 2
80
+ canvas.draw_text(name, label_x, HEIGHT - MARGIN[:bottom] + 20, font_axis, paint)
81
+
82
+ value_text = "#{value}%"
83
+ value_width, = font_axis.measure_text(value_text)
84
+ canvas.draw_text(value_text, x + (bar_width - value_width) / 2, y - 8, font_axis, paint)
85
+ end
86
+ end
87
+
88
+ surface.save_png('bar_chart.png')
89
+ puts 'Saved to bar_chart.png'
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ surface = Skia::Surface.make_raster(640, 480)
7
+
8
+ surface.draw do |canvas|
9
+ canvas.clear(Skia::Color::WHITE)
10
+
11
+ paint = Skia::Paint.new
12
+ paint.antialias = true
13
+
14
+ paint.color = Skia::Color::RED
15
+ paint.style = :fill
16
+ canvas.draw_rect(Skia::Rect.from_xywh(50, 50, 150, 100), paint)
17
+
18
+ paint.color = Skia::Color::BLUE
19
+ canvas.draw_circle(400, 200, 80, paint)
20
+
21
+ paint.color = Skia::Color::GREEN
22
+ paint.style = :stroke
23
+ paint.stroke_width = 3
24
+ canvas.draw_oval(Skia::Rect.from_xywh(200, 300, 200, 100), paint)
25
+
26
+ path = Skia::Path.build do
27
+ move_to 100, 300
28
+ line_to 200, 400
29
+ line_to 50, 400
30
+ close
31
+ end
32
+ paint.color = Skia::Color.rgb(255, 165, 0)
33
+ paint.style = :fill
34
+ canvas.draw_path(path, paint)
35
+
36
+ paint.color = Skia::Color::MAGENTA
37
+ paint.style = :stroke
38
+ paint.stroke_width = 2
39
+ canvas.draw_line(500, 50, 600, 150, paint)
40
+ end
41
+
42
+ surface.save_png('output.png')
43
+ puts 'Saved to output.png'
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ surface = Skia::Surface.make_raster(400, 400)
7
+
8
+ surface.draw do |canvas|
9
+ canvas.clear(Skia::Color::WHITE)
10
+
11
+ colors = [Skia::Color::RED, Skia::Color::YELLOW, Skia::Color::BLUE]
12
+ shader = Skia::Shader.linear_gradient(
13
+ Skia::Point.new(0, 0),
14
+ Skia::Point.new(400, 0),
15
+ colors
16
+ )
17
+
18
+ paint = Skia::Paint.new
19
+ paint.shader = shader
20
+ canvas.draw_rect(Skia::Rect.from_wh(400, 200), paint)
21
+
22
+ radial_shader = Skia::Shader.radial_gradient(
23
+ Skia::Point.new(200, 300),
24
+ 100,
25
+ [Skia::Color::WHITE, Skia::Color::BLUE]
26
+ )
27
+ paint.shader = radial_shader
28
+ canvas.draw_circle(200, 300, 100, paint)
29
+ end
30
+
31
+ surface.save_png('gradient.png')
32
+ puts 'Saved to gradient.png'
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ Skia::Document.create_pdf('output.pdf') do |doc|
7
+ doc.begin_page(612, 792) do |canvas|
8
+ canvas.clear(Skia::Color::WHITE)
9
+
10
+ font = Skia::Font.new(nil, 36.0)
11
+ paint = Skia::Paint.new
12
+ paint.antialias = true
13
+ paint.color = Skia::Color::BLACK
14
+
15
+ canvas.draw_text('Hello, PDF!', 50, 100, font, paint)
16
+
17
+ paint.color = Skia::Color::RED
18
+ paint.style = :fill
19
+ canvas.draw_rect(Skia::Rect.from_xywh(50, 150, 200, 100), paint)
20
+
21
+ paint.color = Skia::Color::BLUE
22
+ canvas.draw_circle(400, 300, 80, paint)
23
+ end
24
+
25
+ doc.begin_page(612, 792) do |canvas|
26
+ canvas.clear(Skia::Color::WHITE)
27
+
28
+ font = Skia::Font.new(nil, 24.0)
29
+ paint = Skia::Paint.new
30
+ paint.antialias = true
31
+ paint.color = Skia::Color::BLACK
32
+
33
+ canvas.draw_text('Page 2', 50, 100, font, paint)
34
+
35
+ path = Skia::Path.build do
36
+ move_to 100, 200
37
+ line_to 200, 400
38
+ line_to 50, 400
39
+ close
40
+ end
41
+
42
+ paint.color = Skia::Color::GREEN
43
+ paint.style = :fill
44
+ canvas.draw_path(path, paint)
45
+ end
46
+ end
47
+
48
+ puts 'Saved to output.pdf'
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ doc = Skia::Document.create_pdf_stream(metadata: { title: 'Skia PDF Stream', author: 'skia-ruby' })
7
+
8
+ doc.page(595, 842) do |canvas|
9
+ canvas.clear(Skia::Color::WHITE)
10
+
11
+ paint = Skia::Paint.new
12
+ paint.color = Skia::Color::BLACK
13
+
14
+ font = Skia::Font.new(nil, 28)
15
+ canvas.draw_text('PDF from memory stream', 48, 96, font, paint)
16
+
17
+ paint.color = Skia::Color::BLUE
18
+ canvas.draw_circle(160, 220, 64, paint)
19
+ end
20
+
21
+ data = doc.to_data
22
+ File.binwrite('stream_output.pdf', data.to_s)
23
+ puts 'Saved stream_output.pdf'
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/skia'
5
+
6
+ bounds = Skia::Rect.from_xywh(0, 0, 200, 200)
7
+ picture = Skia::Picture.record(bounds) do |canvas|
8
+ paint = Skia::Paint.new
9
+ paint.antialias = true
10
+
11
+ paint.color = Skia::Color::RED
12
+ paint.style = :fill
13
+ canvas.draw_circle(100, 100, 80, paint)
14
+
15
+ paint.color = Skia::Color::BLUE
16
+ paint.style = :stroke
17
+ paint.stroke_width = 5
18
+ canvas.draw_circle(100, 100, 80, paint)
19
+ end
20
+
21
+ puts 'Recorded picture:'
22
+ puts " Unique ID: #{picture.unique_id}"
23
+ puts " Cull rect: #{picture.cull_rect.inspect}"
24
+ puts " Approximate op count: #{picture.approximate_op_count}"
25
+ puts " Approximate bytes used: #{picture.approximate_bytes_used}"
26
+
27
+ data = picture.serialize
28
+ puts " Serialized size: #{data.size} bytes"
29
+
30
+ loaded_picture = Skia::Picture.from_data(data)
31
+ puts " Loaded picture unique ID: #{loaded_picture.unique_id}"
32
+
33
+ surface = Skia::Surface.make_raster(640, 480)
34
+ surface.draw do |canvas|
35
+ canvas.clear(Skia::Color::WHITE)
36
+
37
+ canvas.with_save do
38
+ canvas.translate(50, 50)
39
+ picture.playback(canvas)
40
+ end
41
+
42
+ canvas.with_save do
43
+ canvas.translate(300, 50)
44
+ canvas.scale(0.5, 0.5)
45
+ picture.playback(canvas)
46
+ end
47
+
48
+ canvas.with_save do
49
+ canvas.translate(50, 280)
50
+ canvas.scale(1.5, 1.5)
51
+ picture.playback(canvas)
52
+ end
53
+
54
+ canvas.with_save do
55
+ canvas.translate(400, 280)
56
+ canvas.rotate(45, 100, 100)
57
+ picture.playback(canvas)
58
+ end
59
+ end
60
+
61
+ surface.save_png('picture_output.png')
62
+ puts 'Saved to picture_output.png'