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.
- checksums.yaml +4 -4
- data/.doc/ext/rays/bitmap.cpp +29 -9
- data/.doc/ext/rays/bounds.cpp +2 -5
- data/.doc/ext/rays/camera.cpp +15 -7
- data/.doc/ext/rays/color.cpp +2 -4
- data/.doc/ext/rays/color_space.cpp +13 -9
- data/.doc/ext/rays/font.cpp +117 -22
- data/.doc/ext/rays/image.cpp +31 -7
- data/.doc/ext/rays/matrix.cpp +2 -5
- data/.doc/ext/rays/painter.cpp +13 -5
- data/.doc/ext/rays/point.cpp +2 -5
- data/.doc/ext/rays/polygon.cpp +15 -7
- data/.doc/ext/rays/polyline.cpp +15 -10
- data/.doc/ext/rays/rays.cpp +10 -6
- data/.doc/ext/rays/shader.cpp +15 -7
- data/.github/workflows/release-gem.yml +2 -40
- data/.github/workflows/tag.yml +5 -28
- data/.github/workflows/test.yml +1 -28
- data/ChangeLog.md +31 -0
- data/Gemfile.lock +13 -8
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/ext/rays/bitmap.cpp +29 -9
- data/ext/rays/bounds.cpp +2 -5
- data/ext/rays/camera.cpp +16 -7
- data/ext/rays/color.cpp +2 -4
- data/ext/rays/color_space.cpp +13 -9
- data/ext/rays/defs.h +3 -3
- data/ext/rays/extconf.rb +6 -5
- data/ext/rays/font.cpp +121 -22
- data/ext/rays/image.cpp +31 -7
- data/ext/rays/matrix.cpp +2 -5
- data/ext/rays/painter.cpp +14 -5
- data/ext/rays/point.cpp +2 -5
- data/ext/rays/polygon.cpp +16 -7
- data/ext/rays/polyline.cpp +16 -10
- data/ext/rays/rays.cpp +10 -6
- data/ext/rays/shader.cpp +16 -7
- data/include/rays/bitmap.h +3 -1
- data/include/rays/color_space.h +6 -2
- data/include/rays/defs.h +14 -4
- data/include/rays/font.h +46 -4
- data/include/rays/image.h +11 -7
- data/include/rays/painter.h +3 -1
- data/include/rays/ruby/color_space.h +2 -0
- data/include/rays/shader.h +3 -0
- data/lib/rays/camera.rb +1 -1
- data/lib/rays/extension.rb +1 -1
- data/lib/rays/font.rb +4 -1
- data/lib/rays/image.rb +4 -2
- data/lib/rays/painter.rb +5 -5
- data/lib/rays/polygon.rb +1 -1
- data/lib/rays/polyline.rb +1 -1
- data/lib/rays/shader.rb +1 -1
- data/rays.gemspec +3 -3
- data/src/bitmap.cpp +107 -0
- data/src/color.cpp +17 -0
- data/src/color_space.cpp +5 -5
- data/src/font.cpp +121 -25
- data/src/font.h +7 -5
- data/src/image.cpp +104 -102
- data/src/ios/bitmap.mm +3 -3
- data/src/ios/font.mm +79 -26
- data/src/opengl/bitmap.cpp +1 -1
- data/src/opengl/color_space.cpp +15 -3
- data/src/opengl/color_space.h +1 -1
- data/src/opengl/painter.cpp +86 -18
- data/src/opengl/shader.cpp +15 -13
- data/src/opengl/texture.cpp +7 -7
- data/src/opengl/win32/opengl.cpp +17 -12
- data/src/osx/bitmap.mm +14 -3
- data/src/osx/font.mm +79 -26
- data/src/painter.cpp +12 -12
- data/src/painter.h +5 -1
- data/src/polygon.cpp +21 -21
- data/src/polyline.cpp +4 -4
- data/src/sdl/bitmap.cpp +43 -18
- data/src/sdl/font.cpp +56 -50
- data/src/texture.h +1 -1
- data/src/win32/bitmap.cpp +92 -45
- data/src/win32/font.cpp +43 -44
- data/src/win32/gdi.cpp +43 -46
- data/src/win32/gdi.h +6 -1
- data/src/win32/rays.cpp +18 -1
- data/test/test_bitmap.rb +28 -10
- data/test/test_font.rb +58 -0
- data/test/test_image.rb +29 -6
- data/test/test_painter.rb +39 -0
- metadata +8 -8
- data/.github/workflows/utils.rb +0 -127
data/src/image.cpp
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
#include <math.h>
|
|
5
5
|
#include <assert.h>
|
|
6
|
+
#include <memory>
|
|
6
7
|
#include "rays/exception.h"
|
|
7
8
|
#include "rays/debug.h"
|
|
8
9
|
#include "bitmap.h"
|
|
@@ -10,7 +11,7 @@
|
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
#if 0
|
|
13
|
-
#define PRINT_MODIFIED_FLAGS(message)
|
|
14
|
+
#define PRINT_MODIFIED_FLAGS(message) self->print_modified_flags(message)
|
|
14
15
|
#else
|
|
15
16
|
#define PRINT_MODIFIED_FLAGS(message)
|
|
16
17
|
#endif
|
|
@@ -20,7 +21,19 @@ namespace Rays
|
|
|
20
21
|
{
|
|
21
22
|
|
|
22
23
|
|
|
23
|
-
struct
|
|
24
|
+
struct Pixels
|
|
25
|
+
{
|
|
26
|
+
|
|
27
|
+
typedef std::shared_ptr<Pixels> Ptr;
|
|
28
|
+
|
|
29
|
+
mutable Bitmap bitmap;
|
|
30
|
+
|
|
31
|
+
mutable Texture texture;
|
|
32
|
+
|
|
33
|
+
};// Pixels
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
struct Image::Data
|
|
24
37
|
{
|
|
25
38
|
|
|
26
39
|
int width = 0, height = 0;
|
|
@@ -31,50 +44,52 @@ namespace Rays
|
|
|
31
44
|
|
|
32
45
|
ColorSpace color_space;
|
|
33
46
|
|
|
34
|
-
|
|
47
|
+
Pixels::Ptr pixels;
|
|
35
48
|
|
|
36
|
-
|
|
49
|
+
std::unique_ptr<Image::Loader> loader;
|
|
50
|
+
|
|
51
|
+
Data ()
|
|
52
|
+
: pixels(new Pixels())
|
|
53
|
+
{
|
|
54
|
+
}
|
|
37
55
|
|
|
38
56
|
void print_modified_flags (const char* message)
|
|
39
57
|
{
|
|
40
58
|
printf("%s: %d %d %d %d \n",
|
|
41
59
|
message,
|
|
42
|
-
bitmap ? 1 : 0,
|
|
43
|
-
Bitmap_get_modified(bitmap) ? 1 : 0,
|
|
44
|
-
texture ? 1 : 0,
|
|
45
|
-
texture.modified() ? 1 : 0);
|
|
60
|
+
pixels->bitmap ? 1 : 0,
|
|
61
|
+
Bitmap_get_modified(pixels->bitmap) ? 1 : 0,
|
|
62
|
+
pixels->texture ? 1 : 0,
|
|
63
|
+
pixels->texture.modified() ? 1 : 0);
|
|
46
64
|
}
|
|
47
65
|
|
|
48
66
|
};// Image::Data
|
|
49
67
|
|
|
50
68
|
|
|
51
|
-
static ImageData*
|
|
52
|
-
get_data (Image* image)
|
|
53
|
-
{
|
|
54
|
-
return (ImageData*) image->self.get();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
static const ImageData*
|
|
58
|
-
get_data (const Image* image)
|
|
59
|
-
{
|
|
60
|
-
return (const ImageData*) image->self.get();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
69
|
static void
|
|
65
70
|
clear_modified_flags (Image* image)
|
|
66
71
|
{
|
|
67
|
-
|
|
72
|
+
Pixels* pixels = image->self->pixels.get();
|
|
68
73
|
|
|
69
|
-
if (
|
|
70
|
-
|
|
74
|
+
if (pixels->bitmap)
|
|
75
|
+
Bitmap_set_modified(&pixels->bitmap, false);
|
|
76
|
+
|
|
77
|
+
if (pixels->texture)
|
|
78
|
+
pixels->texture.set_modified(false);
|
|
71
79
|
}
|
|
72
80
|
|
|
73
81
|
static void
|
|
74
82
|
invalidate_texture (Image* image)
|
|
75
83
|
{
|
|
76
84
|
image->bitmap();// update bitmap
|
|
77
|
-
|
|
85
|
+
image->self->pixels->texture = Texture();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static void
|
|
89
|
+
check_writable (const Image* image)
|
|
90
|
+
{
|
|
91
|
+
if (image->self->loader)
|
|
92
|
+
invalid_state_error(__FILE__, __LINE__, "image is read-only");
|
|
78
93
|
}
|
|
79
94
|
|
|
80
95
|
static Bitmap&
|
|
@@ -82,31 +97,32 @@ namespace Rays
|
|
|
82
97
|
{
|
|
83
98
|
assert(image);
|
|
84
99
|
|
|
85
|
-
|
|
100
|
+
Image::Data* self = image->self.get();
|
|
101
|
+
Pixels* pixels = self->pixels.get();
|
|
86
102
|
|
|
87
103
|
if (!*image)
|
|
88
104
|
{
|
|
89
|
-
assert(!
|
|
90
|
-
return
|
|
105
|
+
assert(!pixels->bitmap);
|
|
106
|
+
return pixels->bitmap;
|
|
91
107
|
}
|
|
92
108
|
|
|
93
|
-
if (!
|
|
109
|
+
if (!pixels->bitmap)
|
|
94
110
|
{
|
|
95
|
-
if (
|
|
111
|
+
if (pixels->texture)
|
|
96
112
|
{
|
|
97
113
|
PRINT_MODIFIED_FLAGS("new bitmap from texture");
|
|
98
|
-
|
|
114
|
+
pixels->bitmap = Bitmap_from(pixels->texture);
|
|
99
115
|
}
|
|
100
116
|
else
|
|
101
117
|
{
|
|
102
118
|
PRINT_MODIFIED_FLAGS("new bitmap");
|
|
103
|
-
|
|
119
|
+
pixels->bitmap = Bitmap(self->width, self->height, self->color_space);
|
|
104
120
|
}
|
|
105
121
|
clear_modified_flags(image);
|
|
106
122
|
}
|
|
107
|
-
else if (
|
|
123
|
+
else if (pixels->texture && pixels->texture.modified())
|
|
108
124
|
{
|
|
109
|
-
if (Bitmap_get_modified(
|
|
125
|
+
if (Bitmap_get_modified(pixels->bitmap))
|
|
110
126
|
{
|
|
111
127
|
invalid_state_error(
|
|
112
128
|
__FILE__, __LINE__, "bitmap and texture modifications conflicted");
|
|
@@ -114,36 +130,42 @@ namespace Rays
|
|
|
114
130
|
else
|
|
115
131
|
{
|
|
116
132
|
PRINT_MODIFIED_FLAGS("bitmap from texture");
|
|
117
|
-
|
|
133
|
+
pixels->bitmap = Bitmap_from(pixels->texture);
|
|
118
134
|
clear_modified_flags(image);
|
|
119
135
|
}
|
|
120
136
|
}
|
|
121
137
|
|
|
122
|
-
|
|
138
|
+
if (self->loader && self->loader->load(&pixels->bitmap))
|
|
139
|
+
Bitmap_set_modified(&pixels->bitmap);
|
|
140
|
+
|
|
141
|
+
return pixels->bitmap;
|
|
123
142
|
}
|
|
124
143
|
|
|
125
144
|
Texture&
|
|
126
145
|
Image_get_texture (Image& image)
|
|
127
146
|
{
|
|
128
|
-
|
|
147
|
+
Image::Data* self = image.self.get();
|
|
148
|
+
Pixels* pixels = self->pixels.get();
|
|
129
149
|
|
|
130
150
|
if (!image)
|
|
131
151
|
{
|
|
132
|
-
assert(!
|
|
133
|
-
return
|
|
152
|
+
assert(!pixels->texture);
|
|
153
|
+
return pixels->texture;
|
|
134
154
|
}
|
|
135
155
|
|
|
136
|
-
if (
|
|
156
|
+
if (self->loader) get_bitmap(&image);
|
|
157
|
+
|
|
158
|
+
if (!pixels->texture)
|
|
137
159
|
{
|
|
138
|
-
if (
|
|
160
|
+
if (pixels->bitmap)
|
|
139
161
|
{
|
|
140
162
|
PRINT_MODIFIED_FLAGS("new texture from bitmap");
|
|
141
|
-
|
|
163
|
+
pixels->texture = Texture(pixels->bitmap, self->smooth);
|
|
142
164
|
}
|
|
143
165
|
else
|
|
144
166
|
{
|
|
145
167
|
PRINT_MODIFIED_FLAGS("new texture");
|
|
146
|
-
|
|
168
|
+
pixels->texture = Texture(
|
|
147
169
|
self->width, self->height, self->color_space, self->smooth);
|
|
148
170
|
|
|
149
171
|
Painter p = image.painter();
|
|
@@ -153,9 +175,9 @@ namespace Rays
|
|
|
153
175
|
}
|
|
154
176
|
clear_modified_flags(&image);
|
|
155
177
|
}
|
|
156
|
-
else if (
|
|
178
|
+
else if (pixels->bitmap && Bitmap_get_modified(pixels->bitmap))
|
|
157
179
|
{
|
|
158
|
-
if (
|
|
180
|
+
if (pixels->texture.modified())
|
|
159
181
|
{
|
|
160
182
|
invalid_state_error(
|
|
161
183
|
__FILE__, __LINE__, "texture and bitmap modifications conflicted");
|
|
@@ -163,12 +185,12 @@ namespace Rays
|
|
|
163
185
|
else
|
|
164
186
|
{
|
|
165
187
|
PRINT_MODIFIED_FLAGS("texture from bitmap");
|
|
166
|
-
|
|
188
|
+
pixels->texture = pixels->bitmap;
|
|
167
189
|
clear_modified_flags(&image);
|
|
168
190
|
}
|
|
169
191
|
}
|
|
170
192
|
|
|
171
|
-
return
|
|
193
|
+
return pixels->texture;
|
|
172
194
|
}
|
|
173
195
|
|
|
174
196
|
const Texture&
|
|
@@ -178,36 +200,28 @@ namespace Rays
|
|
|
178
200
|
}
|
|
179
201
|
|
|
180
202
|
Image
|
|
181
|
-
load_image (const char* path)
|
|
203
|
+
load_image (const char* path, float pixel_density, bool smooth)
|
|
182
204
|
{
|
|
183
|
-
return Image(Bitmap_load(path));
|
|
205
|
+
return Image(Bitmap_load(path), pixel_density, smooth);
|
|
184
206
|
}
|
|
185
207
|
|
|
186
208
|
|
|
187
|
-
Image::
|
|
188
|
-
{
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
void
|
|
192
|
-
Image::Data::preprocess (const Image*) const
|
|
209
|
+
Image::Loader::~Loader ()
|
|
193
210
|
{
|
|
194
211
|
}
|
|
195
212
|
|
|
196
213
|
|
|
197
214
|
Image::Image ()
|
|
198
|
-
: self(new ImageData())
|
|
199
215
|
{
|
|
200
216
|
}
|
|
201
217
|
|
|
202
218
|
Image::Image (
|
|
203
219
|
int width, int height, const ColorSpace& cs,
|
|
204
220
|
float pixel_density, bool smooth)
|
|
205
|
-
: self(new ImageData())
|
|
206
221
|
{
|
|
207
222
|
if (pixel_density <= 0)
|
|
208
223
|
argument_error(__FILE__, __LINE__, "invalid pixel_density.");
|
|
209
224
|
|
|
210
|
-
ImageData* self = get_data(this);
|
|
211
225
|
self->width = (int) (width * pixel_density);
|
|
212
226
|
self->height = (int) (height * pixel_density);
|
|
213
227
|
self->color_space = cs;
|
|
@@ -216,23 +230,33 @@ namespace Rays
|
|
|
216
230
|
}
|
|
217
231
|
|
|
218
232
|
Image::Image (const Bitmap& bitmap, float pixel_density, bool smooth)
|
|
219
|
-
: self(new ImageData())
|
|
220
233
|
{
|
|
221
234
|
if (pixel_density <= 0)
|
|
222
235
|
argument_error(__FILE__, __LINE__, "invalid pixel_density.");
|
|
223
236
|
|
|
224
|
-
|
|
225
|
-
self->
|
|
226
|
-
self->
|
|
227
|
-
self->
|
|
228
|
-
self->
|
|
229
|
-
self->
|
|
230
|
-
self->smooth = smooth;
|
|
237
|
+
self->pixels->bitmap = bitmap;
|
|
238
|
+
self->width = bitmap.width();
|
|
239
|
+
self->height = bitmap.height();
|
|
240
|
+
self->color_space = bitmap.color_space();
|
|
241
|
+
self->pixel_density = pixel_density;
|
|
242
|
+
self->smooth = smooth;
|
|
231
243
|
}
|
|
232
244
|
|
|
233
|
-
Image::Image (
|
|
234
|
-
: self(data)
|
|
245
|
+
Image::Image (const Image& pixels, Loader* loader)
|
|
235
246
|
{
|
|
247
|
+
std::unique_ptr<Loader> loader_(loader);
|
|
248
|
+
if (!loader)
|
|
249
|
+
argument_error(__FILE__, __LINE__, "loader is NULL.");
|
|
250
|
+
if (!pixels)
|
|
251
|
+
argument_error(__FILE__, __LINE__, "invalid pixels image.");
|
|
252
|
+
|
|
253
|
+
self->width = pixels.self->width;
|
|
254
|
+
self->height = pixels.self->height;
|
|
255
|
+
self->color_space = pixels.self->color_space;
|
|
256
|
+
self->pixel_density = pixels.self->pixel_density;
|
|
257
|
+
self->smooth = pixels.self->smooth;
|
|
258
|
+
self->pixels = pixels.self->pixels;
|
|
259
|
+
self->loader = std::move(loader_);
|
|
236
260
|
}
|
|
237
261
|
|
|
238
262
|
Image::~Image ()
|
|
@@ -242,16 +266,12 @@ namespace Rays
|
|
|
242
266
|
Image
|
|
243
267
|
Image::dup () const
|
|
244
268
|
{
|
|
245
|
-
self->preprocess(this);
|
|
246
|
-
|
|
247
269
|
return Image(bitmap().dup(), pixel_density());
|
|
248
270
|
}
|
|
249
271
|
|
|
250
272
|
void
|
|
251
273
|
Image::save (const char* path)
|
|
252
274
|
{
|
|
253
|
-
self->preprocess(this);
|
|
254
|
-
|
|
255
275
|
if (!*this)
|
|
256
276
|
invalid_state_error(__FILE__, __LINE__);
|
|
257
277
|
|
|
@@ -261,45 +281,31 @@ namespace Rays
|
|
|
261
281
|
coord
|
|
262
282
|
Image::width () const
|
|
263
283
|
{
|
|
264
|
-
self->preprocess(this);
|
|
265
|
-
|
|
266
|
-
const ImageData* self = get_data(this);
|
|
267
284
|
return self->width / self->pixel_density;
|
|
268
285
|
}
|
|
269
286
|
|
|
270
287
|
coord
|
|
271
288
|
Image::height () const
|
|
272
289
|
{
|
|
273
|
-
self->preprocess(this);
|
|
274
|
-
|
|
275
|
-
const ImageData* self = get_data(this);
|
|
276
290
|
return self->height / self->pixel_density;
|
|
277
291
|
}
|
|
278
292
|
|
|
279
293
|
const ColorSpace&
|
|
280
294
|
Image::color_space () const
|
|
281
295
|
{
|
|
282
|
-
self->preprocess(this);
|
|
283
|
-
|
|
284
|
-
const ImageData* self = get_data(this);
|
|
285
296
|
return self->color_space;
|
|
286
297
|
}
|
|
287
298
|
|
|
288
299
|
float
|
|
289
300
|
Image::pixel_density () const
|
|
290
301
|
{
|
|
291
|
-
self->preprocess(this);
|
|
292
|
-
|
|
293
|
-
const ImageData* self = get_data(this);
|
|
294
302
|
return self->pixel_density;
|
|
295
303
|
}
|
|
296
304
|
|
|
297
305
|
void
|
|
298
306
|
Image::set_smooth (bool smooth)
|
|
299
307
|
{
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
ImageData* self = get_data(this);
|
|
308
|
+
check_writable(this);
|
|
303
309
|
|
|
304
310
|
if (smooth == self->smooth) return;
|
|
305
311
|
self->smooth = smooth;
|
|
@@ -309,16 +315,13 @@ namespace Rays
|
|
|
309
315
|
bool
|
|
310
316
|
Image::smooth () const
|
|
311
317
|
{
|
|
312
|
-
self->preprocess(this);
|
|
313
|
-
|
|
314
|
-
const ImageData* self = get_data(this);
|
|
315
318
|
return self->smooth;
|
|
316
319
|
}
|
|
317
320
|
|
|
318
321
|
Painter
|
|
319
322
|
Image::painter ()
|
|
320
323
|
{
|
|
321
|
-
|
|
324
|
+
check_writable(this);
|
|
322
325
|
|
|
323
326
|
Painter p;
|
|
324
327
|
p.bind(*this);
|
|
@@ -328,15 +331,11 @@ namespace Rays
|
|
|
328
331
|
Bitmap&
|
|
329
332
|
Image::bitmap (bool modify)
|
|
330
333
|
{
|
|
331
|
-
|
|
334
|
+
if (modify) check_writable(this);
|
|
332
335
|
|
|
333
|
-
|
|
334
|
-
if (modify)
|
|
335
|
-
|
|
336
|
-
if (!self->bitmap) get_bitmap(this);
|
|
337
|
-
Bitmap_set_modified(&self->bitmap);
|
|
338
|
-
}
|
|
339
|
-
return get_bitmap(this);
|
|
336
|
+
Bitmap& bitmap = get_bitmap(this);
|
|
337
|
+
if (modify) Bitmap_set_modified(&bitmap);
|
|
338
|
+
return bitmap;
|
|
340
339
|
}
|
|
341
340
|
|
|
342
341
|
const Bitmap&
|
|
@@ -345,11 +344,14 @@ namespace Rays
|
|
|
345
344
|
return const_cast<Image*>(this)->bitmap();
|
|
346
345
|
}
|
|
347
346
|
|
|
348
|
-
Image::
|
|
347
|
+
const Image::Loader*
|
|
348
|
+
Image::loader () const
|
|
349
349
|
{
|
|
350
|
-
self->
|
|
350
|
+
return self->loader.get();
|
|
351
|
+
}
|
|
351
352
|
|
|
352
|
-
|
|
353
|
+
Image::operator bool () const
|
|
354
|
+
{
|
|
353
355
|
return
|
|
354
356
|
self->width > 0 &&
|
|
355
357
|
self->height > 0 &&
|
data/src/ios/bitmap.mm
CHANGED
|
@@ -120,7 +120,7 @@ namespace Rays
|
|
|
120
120
|
if (pixels) delete [] (uchar*) pixels;
|
|
121
121
|
|
|
122
122
|
width = height = 0;
|
|
123
|
-
color_space =
|
|
123
|
+
color_space = COLORSPACE_NONE;
|
|
124
124
|
pixels = NULL;
|
|
125
125
|
context = NULL;
|
|
126
126
|
modified = false;
|
|
@@ -323,8 +323,8 @@ namespace Rays
|
|
|
323
323
|
{
|
|
324
324
|
if (!*this)
|
|
325
325
|
{
|
|
326
|
-
static const ColorSpace
|
|
327
|
-
return
|
|
326
|
+
static const ColorSpace NONE = COLORSPACE_NONE;
|
|
327
|
+
return NONE;
|
|
328
328
|
}
|
|
329
329
|
return self->color_space;
|
|
330
330
|
}
|
data/src/ios/font.mm
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
#include <memory>
|
|
6
6
|
#import <CoreGraphics/CGContext.h>
|
|
7
7
|
#import <CoreText/CoreText.h>
|
|
8
|
+
#import <UIKit/UIKit.h>
|
|
8
9
|
#include <xot/string.h>
|
|
9
10
|
#include <xot/util.h>
|
|
10
11
|
#include "rays/exception.h"
|
|
@@ -18,6 +19,8 @@ namespace Rays
|
|
|
18
19
|
|
|
19
20
|
typedef std::shared_ptr<const __CFAttributedString> CFAttributedStringPtr;
|
|
20
21
|
|
|
22
|
+
typedef std::shared_ptr<const __CTFontDescriptor> CTFontDescriptorPtr;
|
|
23
|
+
|
|
21
24
|
typedef std::shared_ptr<CGDataProvider> CGDataProviderPtr;
|
|
22
25
|
|
|
23
26
|
typedef std::shared_ptr<CGFont> CGFontPtr;
|
|
@@ -30,8 +33,6 @@ namespace Rays
|
|
|
30
33
|
|
|
31
34
|
CTFontRef font = NULL;
|
|
32
35
|
|
|
33
|
-
String path;
|
|
34
|
-
|
|
35
36
|
~Data ()
|
|
36
37
|
{
|
|
37
38
|
if (font)
|
|
@@ -44,6 +45,63 @@ namespace Rays
|
|
|
44
45
|
};// RawFont::Data
|
|
45
46
|
|
|
46
47
|
|
|
48
|
+
static const struct {int weight; CGFloat native;} NATIVE_WEIGHTS[] =
|
|
49
|
+
{
|
|
50
|
+
{Font::WEIGHT_MIN, -1.0},
|
|
51
|
+
{Font::WEIGHT_THIN, UIFontWeightUltraLight},
|
|
52
|
+
{Font::WEIGHT_EXTRALIGHT, UIFontWeightThin},
|
|
53
|
+
{Font::WEIGHT_LIGHT, UIFontWeightLight},
|
|
54
|
+
{Font::WEIGHT_NORMAL, UIFontWeightRegular},
|
|
55
|
+
{Font::WEIGHT_MEDIUM, UIFontWeightMedium},
|
|
56
|
+
{Font::WEIGHT_SEMIBOLD, UIFontWeightSemibold},
|
|
57
|
+
{Font::WEIGHT_BOLD, UIFontWeightBold},
|
|
58
|
+
{Font::WEIGHT_EXTRABOLD, UIFontWeightHeavy},
|
|
59
|
+
{Font::WEIGHT_BLACK, UIFontWeightBlack},
|
|
60
|
+
{Font::WEIGHT_MAX, 1.0}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
static CGFloat
|
|
64
|
+
to_native_weight (int weight)
|
|
65
|
+
{
|
|
66
|
+
size_t last = sizeof(NATIVE_WEIGHTS) / sizeof(NATIVE_WEIGHTS[0]) - 1;
|
|
67
|
+
for (size_t i = 1; i <= last; ++i)
|
|
68
|
+
{
|
|
69
|
+
if (weight > NATIVE_WEIGHTS[i].weight) continue;
|
|
70
|
+
|
|
71
|
+
const auto& lo = NATIVE_WEIGHTS[i - 1];
|
|
72
|
+
const auto& hi = NATIVE_WEIGHTS[i];
|
|
73
|
+
CGFloat t = (CGFloat) (weight - lo.weight) / (hi.weight - lo.weight);
|
|
74
|
+
return lo.native + (hi.native - lo.native) * t;
|
|
75
|
+
}
|
|
76
|
+
return NATIVE_WEIGHTS[last].native;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static CTFontRef
|
|
80
|
+
create_styled_font (CTFontRef base, coord size, int weight, bool italic)
|
|
81
|
+
{
|
|
82
|
+
Xot::CFStringPtr family(CTFontCopyFamilyName(base), Xot::safe_cfrelease);
|
|
83
|
+
if (!family) return NULL;
|
|
84
|
+
|
|
85
|
+
NSDictionary* traits =
|
|
86
|
+
@{
|
|
87
|
+
(id) kCTFontWeightTrait: @(to_native_weight(weight)),
|
|
88
|
+
(id) kCTFontSymbolicTrait: @(italic ? kCTFontTraitItalic : 0)
|
|
89
|
+
};
|
|
90
|
+
NSDictionary* attributes =
|
|
91
|
+
@{
|
|
92
|
+
(id) kCTFontFamilyNameAttribute: (__bridge NSString*) family.get(),
|
|
93
|
+
(id) kCTFontTraitsAttribute: traits
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
CTFontDescriptorPtr descriptor(
|
|
97
|
+
CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef) attributes),
|
|
98
|
+
Xot::safe_cfrelease);
|
|
99
|
+
if (!descriptor) return NULL;
|
|
100
|
+
|
|
101
|
+
return CTFontCreateWithFontDescriptor(descriptor.get(), size, NULL);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
47
105
|
static CTLinePtr
|
|
48
106
|
make_line (CTFontRef font, const char* str)
|
|
49
107
|
{
|
|
@@ -64,15 +122,15 @@ namespace Rays
|
|
|
64
122
|
CFDictionaryCreate(
|
|
65
123
|
NULL, (const void**) &keys, (const void**) &values, nkeys,
|
|
66
124
|
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks),
|
|
67
|
-
|
|
125
|
+
Xot::safe_cfrelease);
|
|
68
126
|
|
|
69
127
|
CFAttributedStringPtr attrstr(
|
|
70
|
-
CFAttributedStringCreate(NULL, Xot::
|
|
71
|
-
|
|
128
|
+
CFAttributedStringCreate(NULL, Xot::String(str).to_cfstr().get(), attr.get()),
|
|
129
|
+
Xot::safe_cfrelease);
|
|
72
130
|
|
|
73
131
|
return CTLinePtr(
|
|
74
132
|
CTLineCreateWithAttributedString(attrstr.get()),
|
|
75
|
-
|
|
133
|
+
Xot::safe_cfrelease);
|
|
76
134
|
}
|
|
77
135
|
|
|
78
136
|
const FontFamilyMap&
|
|
@@ -117,7 +175,6 @@ namespace Rays
|
|
|
117
175
|
|
|
118
176
|
RawFont rawfont;
|
|
119
177
|
rawfont.self->font = ctfont;
|
|
120
|
-
rawfont.self->path = path;
|
|
121
178
|
return rawfont;
|
|
122
179
|
}
|
|
123
180
|
|
|
@@ -126,20 +183,23 @@ namespace Rays
|
|
|
126
183
|
{
|
|
127
184
|
}
|
|
128
185
|
|
|
129
|
-
RawFont::RawFont (const char* name, coord size)
|
|
186
|
+
RawFont::RawFont (const char* name, coord size, int weight, bool italic)
|
|
130
187
|
{
|
|
131
|
-
|
|
132
|
-
? CTFontCreateWithName(Xot::
|
|
188
|
+
CTFontRef font = name
|
|
189
|
+
? CTFontCreateWithName(Xot::String(name).to_cfstr().get(), size, NULL)
|
|
133
190
|
: CTFontCreateUIFontForLanguage(kCTFontSystemFontType, size, NULL);
|
|
134
|
-
}
|
|
135
191
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
192
|
+
if (font && (weight != Font::WEIGHT_NORMAL || italic))
|
|
193
|
+
{
|
|
194
|
+
CTFontRef styled = create_styled_font(font, size, weight, italic);
|
|
195
|
+
if (styled)
|
|
196
|
+
{
|
|
197
|
+
CFRelease(font);
|
|
198
|
+
font = styled;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
self->font = font;
|
|
143
203
|
}
|
|
144
204
|
|
|
145
205
|
RawFont::~RawFont ()
|
|
@@ -190,17 +250,10 @@ namespace Rays
|
|
|
190
250
|
{
|
|
191
251
|
if (!*this) return "";
|
|
192
252
|
|
|
193
|
-
Xot::CFStringPtr str(CTFontCopyFullName(self->font),
|
|
253
|
+
Xot::CFStringPtr str(CTFontCopyFullName(self->font), Xot::safe_cfrelease);
|
|
194
254
|
return Xot::to_s(str);
|
|
195
255
|
}
|
|
196
256
|
|
|
197
|
-
coord
|
|
198
|
-
RawFont::size () const
|
|
199
|
-
{
|
|
200
|
-
if (!*this) return 0;
|
|
201
|
-
return CTFontGetSize(self->font);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
257
|
coord
|
|
205
258
|
RawFont::get_width (const char* str) const
|
|
206
259
|
{
|
data/src/opengl/bitmap.cpp
CHANGED
|
@@ -23,7 +23,7 @@ namespace Rays
|
|
|
23
23
|
&bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
|
|
24
24
|
|
|
25
25
|
GLenum format, type;
|
|
26
|
-
ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
|
|
26
|
+
ColorSpace_get_gl_format_and_type(NULL, &format, &type, tex.color_space());
|
|
27
27
|
|
|
28
28
|
FrameBuffer fb(tex);
|
|
29
29
|
FrameBufferBinder binder(fb.id());
|
data/src/opengl/color_space.cpp
CHANGED
|
@@ -10,18 +10,30 @@ namespace Rays
|
|
|
10
10
|
|
|
11
11
|
void
|
|
12
12
|
ColorSpace_get_gl_format_and_type (
|
|
13
|
-
GLenum* format, GLenum* type, const ColorSpace& cs)
|
|
13
|
+
GLenum* internalformat, GLenum* format, GLenum* type, const ColorSpace& cs)
|
|
14
14
|
{
|
|
15
|
-
if (!format && !type)
|
|
15
|
+
if (!internalformat && !format && !type)
|
|
16
16
|
argument_error(__FILE__, __LINE__);
|
|
17
17
|
|
|
18
18
|
if (!cs)
|
|
19
19
|
invalid_state_error(__FILE__, __LINE__);
|
|
20
20
|
|
|
21
|
+
if (internalformat)
|
|
22
|
+
{
|
|
23
|
+
if (cs.is_rgb() || cs.is_bgr()) *internalformat = cs.has_alpha() ? GL_RGBA : GL_RGB;
|
|
24
|
+
else if (cs.is_gray()) *internalformat = GL_LUMINANCE;
|
|
25
|
+
else if (cs.is_alpha()) *internalformat = GL_ALPHA;
|
|
26
|
+
else
|
|
27
|
+
rays_error(__FILE__, __LINE__, "invalid color space.");
|
|
28
|
+
}
|
|
29
|
+
|
|
21
30
|
if (format)
|
|
22
31
|
{
|
|
23
32
|
if (cs.is_rgb()) *format = cs.has_alpha() ? GL_RGBA : GL_RGB;
|
|
24
|
-
#
|
|
33
|
+
#ifdef IOS
|
|
34
|
+
else if (cs.is_bgr() && cs.has_alpha())
|
|
35
|
+
*format = GL_BGRA_EXT;
|
|
36
|
+
#else
|
|
25
37
|
else if (cs.is_bgr()) *format = cs.has_alpha() ? GL_BGRA : GL_BGR;
|
|
26
38
|
#endif
|
|
27
39
|
else if (cs.is_gray()) *format = GL_LUMINANCE;
|