rays 0.3.16 → 0.5.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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +29 -9
  3. data/.doc/ext/rays/bounds.cpp +2 -5
  4. data/.doc/ext/rays/camera.cpp +15 -7
  5. data/.doc/ext/rays/color.cpp +2 -4
  6. data/.doc/ext/rays/color_space.cpp +13 -9
  7. data/.doc/ext/rays/font.cpp +117 -22
  8. data/.doc/ext/rays/image.cpp +31 -7
  9. data/.doc/ext/rays/matrix.cpp +2 -5
  10. data/.doc/ext/rays/painter.cpp +13 -5
  11. data/.doc/ext/rays/point.cpp +2 -5
  12. data/.doc/ext/rays/polygon.cpp +15 -7
  13. data/.doc/ext/rays/polyline.cpp +15 -10
  14. data/.doc/ext/rays/rays.cpp +10 -6
  15. data/.doc/ext/rays/shader.cpp +15 -7
  16. data/.github/workflows/release-gem.yml +2 -40
  17. data/.github/workflows/tag.yml +5 -28
  18. data/.github/workflows/test.yml +1 -28
  19. data/ChangeLog.md +31 -0
  20. data/Gemfile.lock +13 -8
  21. data/Rakefile +2 -2
  22. data/VERSION +1 -1
  23. data/ext/rays/bitmap.cpp +29 -9
  24. data/ext/rays/bounds.cpp +2 -5
  25. data/ext/rays/camera.cpp +16 -7
  26. data/ext/rays/color.cpp +2 -4
  27. data/ext/rays/color_space.cpp +13 -9
  28. data/ext/rays/defs.h +3 -3
  29. data/ext/rays/extconf.rb +6 -5
  30. data/ext/rays/font.cpp +121 -22
  31. data/ext/rays/image.cpp +31 -7
  32. data/ext/rays/matrix.cpp +2 -5
  33. data/ext/rays/painter.cpp +14 -5
  34. data/ext/rays/point.cpp +2 -5
  35. data/ext/rays/polygon.cpp +16 -7
  36. data/ext/rays/polyline.cpp +16 -10
  37. data/ext/rays/rays.cpp +10 -6
  38. data/ext/rays/shader.cpp +16 -7
  39. data/include/rays/bitmap.h +3 -1
  40. data/include/rays/color_space.h +6 -2
  41. data/include/rays/defs.h +14 -4
  42. data/include/rays/font.h +46 -4
  43. data/include/rays/image.h +11 -7
  44. data/include/rays/painter.h +3 -1
  45. data/include/rays/ruby/color_space.h +2 -0
  46. data/include/rays/shader.h +3 -0
  47. data/lib/rays/camera.rb +1 -1
  48. data/lib/rays/extension.rb +1 -1
  49. data/lib/rays/font.rb +4 -1
  50. data/lib/rays/image.rb +4 -2
  51. data/lib/rays/painter.rb +5 -5
  52. data/lib/rays/polygon.rb +1 -1
  53. data/lib/rays/polyline.rb +1 -1
  54. data/lib/rays/shader.rb +1 -1
  55. data/rays.gemspec +3 -3
  56. data/src/bitmap.cpp +107 -0
  57. data/src/color.cpp +17 -0
  58. data/src/color_space.cpp +5 -5
  59. data/src/font.cpp +121 -25
  60. data/src/font.h +7 -5
  61. data/src/image.cpp +104 -102
  62. data/src/ios/bitmap.mm +3 -3
  63. data/src/ios/font.mm +79 -26
  64. data/src/opengl/bitmap.cpp +1 -1
  65. data/src/opengl/color_space.cpp +15 -3
  66. data/src/opengl/color_space.h +1 -1
  67. data/src/opengl/painter.cpp +86 -18
  68. data/src/opengl/shader.cpp +15 -13
  69. data/src/opengl/texture.cpp +7 -7
  70. data/src/opengl/win32/opengl.cpp +17 -12
  71. data/src/osx/bitmap.mm +14 -3
  72. data/src/osx/font.mm +79 -26
  73. data/src/painter.cpp +12 -12
  74. data/src/painter.h +5 -1
  75. data/src/polygon.cpp +21 -21
  76. data/src/polyline.cpp +4 -4
  77. data/src/sdl/bitmap.cpp +43 -18
  78. data/src/sdl/font.cpp +56 -50
  79. data/src/texture.h +1 -1
  80. data/src/win32/bitmap.cpp +92 -45
  81. data/src/win32/font.cpp +43 -44
  82. data/src/win32/gdi.cpp +43 -46
  83. data/src/win32/gdi.h +6 -1
  84. data/src/win32/rays.cpp +18 -1
  85. data/test/test_bitmap.rb +28 -10
  86. data/test/test_font.rb +58 -0
  87. data/test/test_image.rb +29 -6
  88. data/test/test_painter.rb +39 -0
  89. metadata +8 -8
  90. data/.github/workflows/utils.rb +0 -127
data/lib/rays/painter.rb CHANGED
@@ -135,11 +135,11 @@ module Rays
135
135
 
136
136
  private
137
137
 
138
- def each_type(types, reverse: false, &block)
139
- types = [:state, :matrix] if types.empty? || types.include?(:all)
140
- types = types.reverse if reverse
141
- types.each(&block)
142
- end
138
+ def each_type(types, reverse: false, &block)
139
+ types = [:state, :matrix] if types.empty? || types.include?(:all)
140
+ types = types.reverse if reverse
141
+ types.each(&block)
142
+ end
143
143
 
144
144
  end# Painter
145
145
 
data/lib/rays/polygon.rb CHANGED
@@ -11,7 +11,7 @@ module Rays
11
11
  include Comparable
12
12
 
13
13
  def initialize(*args, loop: true, colors: nil, texcoords: nil)
14
- setup args, loop, colors, texcoords
14
+ initialize! args, loop, colors, texcoords
15
15
  end
16
16
 
17
17
  def transform(&block)
data/lib/rays/polyline.rb CHANGED
@@ -12,7 +12,7 @@ module Rays
12
12
  def initialize(
13
13
  *points, loop: false, fill: nil, colors: nil, texcoords: nil, hole: false)
14
14
 
15
- setup points, loop, (fill != nil ? fill : loop), colors, texcoords, hole
15
+ initialize! points, loop, (fill != nil ? fill : loop), colors, texcoords, hole
16
16
  end
17
17
 
18
18
  def with(**kwargs)
data/lib/rays/shader.rb CHANGED
@@ -14,7 +14,7 @@ module Rays
14
14
  ignore_no_uniform_location_error: false,
15
15
  **uniforms, &block)
16
16
 
17
- setup(
17
+ initialize!(
18
18
  fragment_shader_source, vertex_shader_source,
19
19
  builtin_variable_names&.values_at(
20
20
  :attribute_position,
data/rays.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
 
12
12
  ext = Rays::Extension
13
13
  name = ext.name true
14
- rdocs = glob.call *%w[README .doc/ext/**/*.cpp]
14
+ rdocs = glob.call(*%w[README .doc/ext/**/*.cpp])
15
15
 
16
16
  s.name = name
17
17
  s.version = ext.version
@@ -25,8 +25,8 @@ Gem::Specification.new do |s|
25
25
  s.platform = Gem::Platform::RUBY
26
26
  s.required_ruby_version = '>= 3.0.0'
27
27
 
28
- s.add_dependency 'xot', '~> 0.3.15'
29
- s.add_dependency 'rucy', '~> 0.3.15'
28
+ s.add_dependency 'xot', '~> 0.5.0'
29
+ s.add_dependency 'rucy', '~> 0.5.0'
30
30
 
31
31
  s.files = `git ls-files`.split $/
32
32
  s.executables = s.files.grep(%r{^bin/}) {|f| File.basename f}
data/src/bitmap.cpp ADDED
@@ -0,0 +1,107 @@
1
+ #include "bitmap.h"
2
+
3
+
4
+ #include <algorithm>
5
+ #include "rays/exception.h"
6
+
7
+
8
+ namespace Rays
9
+ {
10
+
11
+
12
+ template <typename Fun>
13
+ static void
14
+ each_alpha_pixel (Bitmap* bitmap, Fun fun)
15
+ {
16
+ if (!bitmap)
17
+ argument_error(__FILE__, __LINE__);
18
+ if (!*bitmap)
19
+ argument_error(__FILE__, __LINE__);
20
+
21
+ const ColorSpace& cs = bitmap->color_space();
22
+ if (!cs.has_alpha() || cs.is_alpha())
23
+ return;
24
+
25
+ int width = bitmap->width();
26
+ int height = bitmap->height();
27
+ int Bpp = cs.Bpp();
28
+ int apos = cs.alpha_pos();
29
+
30
+ for (int y = 0; y < height; ++y)
31
+ {
32
+ uchar* p = bitmap->at<uchar>(0, y);
33
+ for (int x = 0; x < width; ++x, p += Bpp)
34
+ fun(p, apos);
35
+ }
36
+ }
37
+
38
+ static void
39
+ premultiply (Bitmap* bitmap)
40
+ {
41
+ if (bitmap && bitmap->color_space().is_float())
42
+ {
43
+ each_alpha_pixel(bitmap, [](uchar* p, int apos)
44
+ {
45
+ float* f = (float*) p;
46
+ float a = f[apos];
47
+ if (a == 1) return;
48
+ for (int i = 0; i < 4; ++i)
49
+ if (i != apos) f[i] *= a;
50
+ });
51
+ }
52
+ else
53
+ {
54
+ each_alpha_pixel(bitmap, [](uchar* p, int apos)
55
+ {
56
+ uint a = p[apos];
57
+ if (a == 255) return;
58
+ for (int i = 0; i < 4; ++i)
59
+ if (i != apos) p[i] = (uchar) ((p[i] * a + 127) / 255);
60
+ });
61
+ }
62
+ }
63
+
64
+ static void
65
+ unpremultiply (Bitmap* bitmap)
66
+ {
67
+ if (bitmap && bitmap->color_space().is_float())
68
+ {
69
+ each_alpha_pixel(bitmap, [](uchar* p, int apos)
70
+ {
71
+ float* f = (float*) p;
72
+ float a = f[apos];
73
+ if (a <= 0 || a == 1) return;
74
+ for (int i = 0; i < 4; ++i)
75
+ if (i != apos) f[i] = std::min(f[i] / a, 1.f);
76
+ });
77
+ }
78
+ else
79
+ {
80
+ each_alpha_pixel(bitmap, [](uchar* p, int apos)
81
+ {
82
+ uint a = p[apos];
83
+ if (a == 0 || a == 255) return;
84
+ for (int i = 0; i < 4; ++i)
85
+ if (i != apos) p[i] = (uchar) std::min<uint>((p[i] * 255 + a / 2) / a, 255);
86
+ });
87
+ }
88
+ }
89
+
90
+ Bitmap
91
+ Bitmap::dup (bool premult) const
92
+ {
93
+ const ColorSpace& cs = color_space();
94
+
95
+ Bitmap bitmap(width(), height(), ColorSpace(cs.type(), premult), pixels());
96
+ if (premult != cs.is_premult())
97
+ {
98
+ if (premult)
99
+ premultiply(&bitmap);
100
+ else
101
+ unpremultiply(&bitmap);
102
+ }
103
+ return bitmap;
104
+ }
105
+
106
+
107
+ }// Rays
data/src/color.cpp CHANGED
@@ -159,6 +159,13 @@ namespace Rays
159
159
  }
160
160
  }
161
161
 
162
+ if (cs.has_alpha() && cs.is_premult() && alpha > 0)
163
+ {
164
+ red = std::min(red / alpha, 1.f);
165
+ green = std::min(green / alpha, 1.f);
166
+ blue = std::min(blue / alpha, 1.f);
167
+ }
168
+
162
169
  return *this;
163
170
  }
164
171
 
@@ -211,6 +218,16 @@ namespace Rays
211
218
  argument_error(__FILE__, __LINE__);
212
219
 
213
220
  const float* c = array;
221
+ float premult[4];
222
+ if (cs.has_alpha() && cs.is_premult())
223
+ {
224
+ premult[0] = red * alpha;
225
+ premult[1] = green * alpha;
226
+ premult[2] = blue * alpha;
227
+ premult[3] = alpha;
228
+ c = premult;
229
+ }
230
+
214
231
  if (cs.is_float())
215
232
  {
216
233
  float* p = (float*) pixel;
data/src/color_space.cpp CHANGED
@@ -26,7 +26,7 @@ namespace Rays
26
26
 
27
27
 
28
28
  ColorSpace::ColorSpace ()
29
- : type_(COLORSPACE_UNKNOWN), premult(false)
29
+ : type_(COLORSPACE_NONE), premult(false)
30
30
  {
31
31
  }
32
32
 
@@ -46,7 +46,7 @@ namespace Rays
46
46
  {
47
47
  static const int BPPS[] =
48
48
  {
49
- 0, // UNKNOWN
49
+ 0, // NONE
50
50
  8, 16, 24, 32, 32, // GRAY
51
51
  8, 16, 24, 32, 32, // ALPHA
52
52
  8, 8, 8, 8, 8, // RGB(A),
@@ -54,7 +54,7 @@ namespace Rays
54
54
  32, 32, 32, // RGB(A) float
55
55
  32, 32, 32, // BGR(A) float
56
56
  };
57
- if (type_ < 0 || COLORSPACE_MAX <= type_) return BPPS[COLORSPACE_UNKNOWN];
57
+ if (type_ < 0 || COLORSPACE_MAX <= type_) return BPPS[COLORSPACE_NONE];
58
58
  return BPPS[type_];
59
59
  }
60
60
 
@@ -69,7 +69,7 @@ namespace Rays
69
69
  {
70
70
  static const int BPPS[] =
71
71
  {
72
- 0, // UNKNOWN
72
+ 0, // NONE
73
73
  8, 16, 24, 32, 32, // GRAY
74
74
  8, 16, 24, 32, 32, // ALPHA
75
75
  24, 32, 32, 32, 32, // RGB(A)
@@ -77,7 +77,7 @@ namespace Rays
77
77
  96, 128, 128, // RGB(A) float
78
78
  96, 128, 128, // BGR(A) float
79
79
  };
80
- if (type_ < 0 || COLORSPACE_MAX <= type_) return BPPS[COLORSPACE_UNKNOWN];
80
+ if (type_ < 0 || COLORSPACE_MAX <= type_) return BPPS[COLORSPACE_NONE];
81
81
  return BPPS[type_];
82
82
  }
83
83
 
data/src/font.cpp CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  #include <string.h>
5
5
  #include <assert.h>
6
+ #include <algorithm>
7
+ #include "rays/exception.h"
6
8
 
7
9
 
8
10
  namespace Rays
@@ -12,53 +14,109 @@ namespace Rays
12
14
  struct Font::Data
13
15
  {
14
16
 
15
- RawFont rawfont;
17
+ String name, path;
18
+
19
+ coord size = 0;
20
+
21
+ int weight = Font::DEFAULT_WEIGHT;
22
+
23
+ bool italic = false;
16
24
 
17
25
  bool smooth = true;
18
26
 
27
+ RawFont rawfont;
28
+
19
29
  mutable RawFont rawfont_for_pixel_density;
20
30
 
21
31
  mutable float for_pixel_density = 1;
22
32
 
23
- const RawFont& get_raw (float pixel_density) const
33
+ RawFont make_rawfont (coord size) const
24
34
  {
25
- assert(pixel_density > 0);
26
-
27
- if (!rawfont || pixel_density == 1)
28
- return rawfont;
35
+ if (!path.empty())
36
+ return RawFont_load(path.c_str(), size);
37
+ else
38
+ return RawFont(name.empty() ? NULL : name.c_str(), size, weight, italic);
39
+ }
29
40
 
30
- if (pixel_density != for_pixel_density)
31
- {
32
- rawfont_for_pixel_density =
33
- RawFont(rawfont, rawfont.size() * pixel_density);
34
- for_pixel_density = pixel_density;
35
- }
41
+ void remake_rawfont ()
42
+ {
43
+ if (!rawfont) return;
36
44
 
37
- return rawfont_for_pixel_density;
45
+ rawfont = make_rawfont(size);
46
+ for_pixel_density = 1;// drop the cache
38
47
  }
39
48
 
40
49
  };// Font::Data
41
50
 
42
51
 
52
+ static int
53
+ clamp_weight (int weight)
54
+ {
55
+ return std::clamp(weight, (int) Font::WEIGHT_MIN, (int) Font::WEIGHT_MAX);
56
+ }
57
+
43
58
  Font
44
59
  load_font (const char* path, coord size)
45
60
  {
46
61
  Font font;
62
+ font.self->path = path ? path : "";
63
+ font.self->size = size;
47
64
  font.self->rawfont = RawFont_load(path, size);
48
65
  return font;
49
66
  }
50
67
 
51
- const Font&
68
+ static constexpr const char* NULL_NAME = "";
69
+
70
+ Font
52
71
  get_default_font ()
53
72
  {
54
- static const Font FONT(NULL);
55
- return FONT;
73
+ static const Font FONT(NULL_NAME);
74
+ return FONT.dup();
56
75
  }
57
76
 
58
77
  const RawFont&
59
78
  Font_get_raw (const Font& font, float pixel_density)
60
79
  {
61
- return font.self->get_raw(pixel_density);
80
+ if (pixel_density <= 0)
81
+ argument_error(__FILE__, __LINE__);
82
+
83
+ Font::Data* self = font.self.get();
84
+
85
+ if (!self->rawfont || pixel_density == 1)
86
+ return self->rawfont;
87
+
88
+ if (pixel_density != self->for_pixel_density)
89
+ {
90
+ self->rawfont_for_pixel_density = self->make_rawfont(self->size * pixel_density);
91
+ self->for_pixel_density = pixel_density;
92
+ }
93
+
94
+ return self->rawfont_for_pixel_density;
95
+ }
96
+
97
+ bool
98
+ Font_has_same_attributes (
99
+ const Font& font, const char* name, coord size,
100
+ int weight, bool italic, bool smooth)
101
+ {
102
+ if (!font.self->path.empty())
103
+ return false;
104
+
105
+ weight = clamp_weight(weight);
106
+
107
+ if (
108
+ size != font.size() ||
109
+ weight != font.weight() ||
110
+ italic != font.italic() ||
111
+ smooth != font.smooth())
112
+ {
113
+ return false;
114
+ }
115
+
116
+ if (!name)
117
+ name = NULL_NAME;
118
+
119
+ return name == font.name(false) || name == font.name(true);
62
120
  }
63
121
 
64
122
 
@@ -66,10 +124,14 @@ namespace Rays
66
124
  {
67
125
  }
68
126
 
69
- Font::Font (const char* name, coord size, bool smooth)
127
+ Font::Font (const char* name, coord size, int weight, bool italic, bool smooth)
70
128
  {
71
- self->rawfont = RawFont(name, size);
129
+ self->name = name ? name : NULL_NAME;
130
+ self->size = size;
131
+ self->weight = clamp_weight(weight);
132
+ self->italic = italic;
72
133
  self->smooth = smooth;
134
+ self->rawfont = self->make_rawfont(size);
73
135
  }
74
136
 
75
137
  Font::~Font ()
@@ -80,27 +142,61 @@ namespace Rays
80
142
  Font::dup () const
81
143
  {
82
144
  Font f;
83
- f.self->rawfont = RawFont(self->rawfont, self->rawfont.size());
84
- f.self->smooth = self->smooth;
145
+ *f.self = *self;
85
146
  return f;
86
147
  }
87
148
 
88
149
  String
89
- Font::name () const
150
+ Font::name (bool resolved) const
90
151
  {
91
- return self->rawfont.name();
152
+ return resolved || self->name.empty() ? self->rawfont.name() : self->name;
92
153
  }
93
154
 
94
155
  void
95
156
  Font::set_size (coord size)
96
157
  {
97
- self->rawfont = RawFont(self->rawfont, size);
158
+ if (size == self->size) return;
159
+
160
+ self->size = size;
161
+ self->remake_rawfont();
98
162
  }
99
163
 
100
164
  coord
101
165
  Font::size () const
102
166
  {
103
- return self->rawfont.size();
167
+ return self->size;
168
+ }
169
+
170
+ void
171
+ Font::set_weight (int weight)
172
+ {
173
+ weight = clamp_weight(weight);
174
+
175
+ if (weight == self->weight) return;
176
+
177
+ self->weight = weight;
178
+ self->remake_rawfont();
179
+ }
180
+
181
+ int
182
+ Font::weight () const
183
+ {
184
+ return self->weight;
185
+ }
186
+
187
+ void
188
+ Font::set_italic (bool italic)
189
+ {
190
+ if (italic == self->italic) return;
191
+
192
+ self->italic = italic;
193
+ self->remake_rawfont();
194
+ }
195
+
196
+ bool
197
+ Font::italic () const
198
+ {
199
+ return self->italic;
104
200
  }
105
201
 
106
202
  void
data/src/font.h CHANGED
@@ -21,9 +21,9 @@ namespace Rays
21
21
 
22
22
  RawFont ();
23
23
 
24
- RawFont (const char* name, coord size);
25
-
26
- RawFont (const This& obj, coord size);
24
+ RawFont (
25
+ const char* name, coord size,
26
+ int weight = Font::DEFAULT_WEIGHT, bool italic = false);
27
27
 
28
28
  ~RawFont ();
29
29
 
@@ -33,8 +33,6 @@ namespace Rays
33
33
 
34
34
  String name () const;
35
35
 
36
- coord size () const;
37
-
38
36
  coord get_width (const char* str) const;
39
37
 
40
38
  coord get_height (
@@ -55,6 +53,10 @@ namespace Rays
55
53
 
56
54
  const RawFont& Font_get_raw (const Font& font, float pixel_density);
57
55
 
56
+ bool Font_has_same_attributes (
57
+ const Font& font, const char* name, coord size,
58
+ int weight, bool italic, bool smooth);
59
+
58
60
 
59
61
  RawFont RawFont_load (const char* path, coord size);
60
62