rays 0.1.6 → 0.1.7
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 +7 -0
- data/.doc/ext/rays/bitmap.cpp +70 -233
- data/.doc/ext/rays/bounds.cpp +339 -57
- data/.doc/ext/rays/color.cpp +58 -48
- data/.doc/ext/rays/color_space.cpp +174 -0
- data/.doc/ext/rays/font.cpp +31 -53
- data/.doc/ext/rays/image.cpp +64 -67
- data/.doc/ext/rays/matrix.cpp +22 -50
- data/.doc/ext/rays/native.cpp +9 -2
- data/.doc/ext/rays/painter.cpp +276 -259
- data/.doc/ext/rays/point.cpp +186 -52
- data/.doc/ext/rays/rays.cpp +25 -20
- data/.doc/ext/rays/shader.cpp +61 -0
- data/.doc/ext/rays/texture.cpp +47 -59
- data/{README → README.md} +0 -0
- data/Rakefile +6 -5
- data/VERSION +1 -1
- data/ext/rays/bitmap.cpp +88 -248
- data/ext/rays/bounds.cpp +437 -141
- data/ext/rays/color.cpp +79 -69
- data/ext/rays/color_space.cpp +185 -0
- data/ext/rays/extconf.rb +14 -63
- data/ext/rays/font.cpp +44 -65
- data/ext/rays/image.cpp +82 -81
- data/ext/rays/matrix.cpp +32 -60
- data/ext/rays/native.cpp +9 -2
- data/ext/rays/painter.cpp +345 -321
- data/ext/rays/point.cpp +212 -69
- data/ext/rays/rays.cpp +29 -23
- data/ext/rays/shader.cpp +63 -0
- data/ext/rays/texture.cpp +64 -74
- data/include/rays/bitmap.h +21 -12
- data/include/rays/bounds.h +67 -9
- data/include/rays/color.h +23 -7
- data/include/rays/{colorspace.h → color_space.h} +6 -3
- data/include/rays/exception.h +17 -11
- data/include/rays/font.h +4 -3
- data/include/rays/image.h +11 -6
- data/include/rays/matrix.h +15 -12
- data/include/rays/opengl.h +54 -1
- data/include/rays/painter.h +98 -108
- data/include/rays/point.h +45 -5
- data/include/rays/rays.h +2 -2
- data/include/rays/ruby/bitmap.h +2 -16
- data/include/rays/ruby/bounds.h +4 -16
- data/include/rays/ruby/color.h +3 -16
- data/include/rays/ruby/color_space.h +27 -0
- data/include/rays/ruby/font.h +2 -16
- data/include/rays/ruby/image.h +2 -16
- data/include/rays/ruby/matrix.h +2 -16
- data/include/rays/ruby/painter.h +2 -16
- data/include/rays/ruby/point.h +3 -16
- data/include/rays/ruby/shader.h +27 -0
- data/include/rays/ruby/texture.h +2 -16
- data/include/rays/ruby.h +1 -0
- data/include/rays/shader.h +48 -0
- data/include/rays/texture.h +13 -2
- data/include/rays.h +2 -1
- data/lib/rays/bitmap.rb +20 -11
- data/lib/rays/bounds.rb +29 -68
- data/lib/rays/color.rb +39 -0
- data/lib/rays/color_space.rb +33 -0
- data/lib/rays/font.rb +29 -0
- data/lib/rays/image.rb +22 -0
- data/lib/rays/module.rb +11 -7
- data/lib/rays/painter.rb +103 -40
- data/lib/rays/point.rb +19 -36
- data/lib/rays/shader.rb +13 -0
- data/lib/rays/texture.rb +9 -0
- data/lib/rays.rb +4 -0
- data/rays.gemspec +3 -4
- data/src/bounds.cpp +272 -63
- data/src/color.cpp +168 -21
- data/src/{colorspace.cpp → color_space.cpp} +38 -1
- data/src/exception.cpp +24 -15
- data/src/frame_buffer.cpp +275 -0
- data/src/frame_buffer.h +79 -0
- data/src/image.cpp +80 -36
- data/src/ios/bitmap.mm +340 -0
- data/src/ios/font.mm +206 -0
- data/src/{cocoa → ios}/helper.h +2 -2
- data/src/{cocoa → ios}/helper.mm +0 -0
- data/src/ios/opengl.mm +21 -0
- data/src/ios/program.cpp +122 -0
- data/src/{cocoa → ios}/rays.mm +8 -7
- data/src/matrix.cpp +10 -22
- data/src/opengl.cpp +64 -0
- data/src/{cocoa → osx}/bitmap.mm +121 -70
- data/src/{cocoa → osx}/font.mm +32 -24
- data/src/osx/helper.h +26 -0
- data/src/osx/helper.mm +25 -0
- data/src/osx/opengl.mm +103 -0
- data/src/osx/rays.mm +43 -0
- data/src/painter.cpp +596 -422
- data/src/point.cpp +154 -11
- data/src/program.cpp +513 -0
- data/src/program.h +73 -0
- data/src/render_buffer.cpp +120 -0
- data/src/render_buffer.h +47 -0
- data/src/shader.cpp +117 -0
- data/src/texture.cpp +104 -134
- data/test/helper.rb +10 -3
- data/test/test_bitmap.rb +18 -0
- data/test/test_bounds.rb +81 -35
- data/test/test_color.rb +29 -2
- data/test/test_image.rb +63 -0
- data/test/test_painter.rb +120 -0
- data/test/test_point.rb +30 -9
- data/test/test_shader.rb +37 -0
- data/test/test_texture.rb +18 -0
- metadata +75 -58
- data/.gitignore +0 -14
- data/ChangeLog +0 -8
data/src/{cocoa → ios}/rays.mm
RENAMED
@@ -2,7 +2,8 @@
|
|
2
2
|
#include "rays/rays.h"
|
3
3
|
|
4
4
|
|
5
|
-
#import <
|
5
|
+
#import <Foundation/Foundation.h>
|
6
|
+
#include "rays/exception.h"
|
6
7
|
|
7
8
|
|
8
9
|
namespace Rays
|
@@ -19,23 +20,23 @@ namespace Rays
|
|
19
20
|
}// global
|
20
21
|
|
21
22
|
|
22
|
-
|
23
|
+
void
|
23
24
|
init ()
|
24
25
|
{
|
25
|
-
if (global::pool)
|
26
|
+
if (global::pool)
|
27
|
+
rays_error(__FILE__, __LINE__, "Rays::init(): already initialized.");
|
26
28
|
|
27
29
|
global::pool = [[NSAutoreleasePool alloc] init];
|
28
|
-
return true;
|
29
30
|
}
|
30
31
|
|
31
|
-
|
32
|
+
void
|
32
33
|
fin ()
|
33
34
|
{
|
34
|
-
if (!global::pool)
|
35
|
+
if (!global::pool)
|
36
|
+
rays_error(__FILE__, __LINE__, "Rays::fin(): not initialized.");
|
35
37
|
|
36
38
|
[global::pool release];
|
37
39
|
global::pool = nil;
|
38
|
-
return true;
|
39
40
|
}
|
40
41
|
|
41
42
|
|
data/src/matrix.cpp
CHANGED
@@ -7,7 +7,7 @@ namespace Rays
|
|
7
7
|
|
8
8
|
Matrix::Matrix (float value)
|
9
9
|
{
|
10
|
-
|
10
|
+
reset(value);
|
11
11
|
}
|
12
12
|
|
13
13
|
Matrix::Matrix (
|
@@ -16,12 +16,12 @@ namespace Rays
|
|
16
16
|
float c1, float c2, float c3, float c4,
|
17
17
|
float d1, float d2, float d3, float d4)
|
18
18
|
{
|
19
|
-
|
19
|
+
reset(a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4);
|
20
20
|
}
|
21
21
|
|
22
22
|
Matrix::Matrix (const float* elements, size_t size)
|
23
23
|
{
|
24
|
-
|
24
|
+
reset(elements, size);
|
25
25
|
}
|
26
26
|
|
27
27
|
Matrix
|
@@ -31,9 +31,9 @@ namespace Rays
|
|
31
31
|
}
|
32
32
|
|
33
33
|
Matrix&
|
34
|
-
Matrix::
|
34
|
+
Matrix::reset (float value)
|
35
35
|
{
|
36
|
-
return
|
36
|
+
return reset(
|
37
37
|
value, 0, 0, 0,
|
38
38
|
0, value, 0, 0,
|
39
39
|
0, 0, value, 0,
|
@@ -41,7 +41,7 @@ namespace Rays
|
|
41
41
|
}
|
42
42
|
|
43
43
|
Matrix&
|
44
|
-
Matrix::
|
44
|
+
Matrix::reset (
|
45
45
|
float a1, float a2, float a3, float a4,
|
46
46
|
float b1, float b2, float b3, float b4,
|
47
47
|
float c1, float c2, float c3, float c4,
|
@@ -55,10 +55,10 @@ namespace Rays
|
|
55
55
|
}
|
56
56
|
|
57
57
|
Matrix&
|
58
|
-
Matrix::
|
58
|
+
Matrix::reset (const float* elements, size_t size)
|
59
59
|
{
|
60
60
|
if (size != 16) return *this;
|
61
|
-
float* e = array
|
61
|
+
float* e = array;
|
62
62
|
for (int i = 0; i < 16; ++i) *e++ = *elements++;
|
63
63
|
return *this;
|
64
64
|
}
|
@@ -66,7 +66,7 @@ namespace Rays
|
|
66
66
|
float&
|
67
67
|
Matrix::at (int row, int column)
|
68
68
|
{
|
69
|
-
return array
|
69
|
+
return array[column * 4 + row];
|
70
70
|
}
|
71
71
|
|
72
72
|
float
|
@@ -75,22 +75,10 @@ namespace Rays
|
|
75
75
|
return const_cast<Matrix*>(this)->at(row, column);
|
76
76
|
}
|
77
77
|
|
78
|
-
float*
|
79
|
-
Matrix::array ()
|
80
|
-
{
|
81
|
-
return (float*) this;
|
82
|
-
}
|
83
|
-
|
84
|
-
const float*
|
85
|
-
Matrix::array () const
|
86
|
-
{
|
87
|
-
return const_cast<Matrix*>(this)->array();
|
88
|
-
}
|
89
|
-
|
90
78
|
float&
|
91
79
|
Matrix::operator [] (int index)
|
92
80
|
{
|
93
|
-
return array
|
81
|
+
return array[index];
|
94
82
|
}
|
95
83
|
|
96
84
|
float
|
data/src/opengl.cpp
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#include "rays/opengl.h"
|
2
|
+
|
3
|
+
|
4
|
+
#include "rays/exception.h"
|
5
|
+
|
6
|
+
|
7
|
+
namespace Rays
|
8
|
+
{
|
9
|
+
|
10
|
+
|
11
|
+
GLenum
|
12
|
+
get_error ()
|
13
|
+
{
|
14
|
+
return glGetError();
|
15
|
+
}
|
16
|
+
|
17
|
+
bool
|
18
|
+
no_error ()
|
19
|
+
{
|
20
|
+
return get_error() == GL_NO_ERROR;
|
21
|
+
}
|
22
|
+
|
23
|
+
bool
|
24
|
+
is_error (GLenum err)
|
25
|
+
{
|
26
|
+
return get_error() == err;
|
27
|
+
}
|
28
|
+
|
29
|
+
static String
|
30
|
+
get_error_name (GLenum error)
|
31
|
+
{
|
32
|
+
switch (error)
|
33
|
+
{
|
34
|
+
case GL_NO_ERROR: return "GL_NO_ERROR";
|
35
|
+
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
|
36
|
+
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
|
37
|
+
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
|
38
|
+
#ifndef IOS
|
39
|
+
case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
|
40
|
+
case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
|
41
|
+
#endif
|
42
|
+
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
|
43
|
+
case 0x506: return "GL_INVALID_FRAMEBUFFER_OPERATION";
|
44
|
+
case 0x8031: return "GL_TABLE_TOO_LARGE";
|
45
|
+
}
|
46
|
+
return "UNKNOWN ERROR";
|
47
|
+
}
|
48
|
+
|
49
|
+
void
|
50
|
+
check_error (const char* file, int line)
|
51
|
+
{
|
52
|
+
GLenum e = get_error();
|
53
|
+
if (e != GL_NO_ERROR)
|
54
|
+
opengl_error(file, line, "OpenGL error %s", get_error_name(e).c_str());
|
55
|
+
}
|
56
|
+
|
57
|
+
void
|
58
|
+
clear_error ()
|
59
|
+
{
|
60
|
+
get_error();
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
}// Rays
|
data/src/{cocoa → osx}/bitmap.mm
RENAMED
@@ -1,9 +1,13 @@
|
|
1
1
|
// -*- objc -*-
|
2
|
-
#import
|
2
|
+
#import "rays/bitmap.h"
|
3
3
|
|
4
4
|
|
5
5
|
#include <assert.h>
|
6
|
+
#include <boost/scoped_array.hpp>
|
6
7
|
#import <Cocoa/Cocoa.h>
|
8
|
+
#include "rays/exception.h"
|
9
|
+
#include "rays/texture.h"
|
10
|
+
#include "../frame_buffer.h"
|
7
11
|
#include "helper.h"
|
8
12
|
|
9
13
|
|
@@ -53,7 +57,7 @@ namespace Rays
|
|
53
57
|
|
54
58
|
int width, height;
|
55
59
|
|
56
|
-
ColorSpace
|
60
|
+
ColorSpace color_space;
|
57
61
|
|
58
62
|
void* pixels;
|
59
63
|
|
@@ -62,50 +66,34 @@ namespace Rays
|
|
62
66
|
bool dirty;
|
63
67
|
|
64
68
|
Data ()
|
65
|
-
:
|
66
|
-
{
|
67
|
-
}
|
68
|
-
|
69
|
-
~Data ()
|
69
|
+
: pixels(NULL), context(NULL)
|
70
70
|
{
|
71
71
|
clear();
|
72
72
|
}
|
73
73
|
|
74
|
-
|
74
|
+
~Data ()
|
75
75
|
{
|
76
|
-
if (w <= 0 || h <= 0 || !cs) return false;
|
77
|
-
|
78
76
|
clear();
|
79
|
-
|
80
|
-
width = w;
|
81
|
-
height = h;
|
82
|
-
colorspace = cs;
|
83
|
-
|
84
|
-
size_t size = w * h * cs.Bpp();
|
85
|
-
pixels = new uchar[size];
|
86
|
-
memset(pixels, 0, size);
|
87
|
-
|
88
|
-
return true;
|
89
77
|
}
|
90
78
|
|
91
79
|
CGContextRef get_context ()
|
92
80
|
{
|
93
81
|
if (context) return context;
|
94
82
|
|
95
|
-
int bpc =
|
96
|
-
int pitch = width *
|
83
|
+
int bpc = color_space.bpc();
|
84
|
+
int pitch = width * color_space.Bpp();
|
97
85
|
if (bpc <= 0 || pitch <= 0) return NULL;
|
98
86
|
|
99
87
|
CGColorSpaceRef cgcs = NULL;
|
100
|
-
if (
|
88
|
+
if (color_space.is_gray())
|
101
89
|
cgcs = CGColorSpaceCreateDeviceGray();
|
102
|
-
else if (
|
90
|
+
else if (color_space.is_rgb() || color_space.is_bgr())
|
103
91
|
cgcs = CGColorSpaceCreateDeviceRGB();
|
104
92
|
else
|
105
93
|
return NULL;
|
106
94
|
|
107
95
|
context = CGBitmapContextCreate(
|
108
|
-
pixels, width, height, bpc, pitch, cgcs, make_bitmapinfo(
|
96
|
+
pixels, width, height, bpc, pitch, cgcs, make_bitmapinfo(color_space));
|
109
97
|
CGColorSpaceRelease(cgcs);
|
110
98
|
return context;
|
111
99
|
}
|
@@ -123,28 +111,84 @@ namespace Rays
|
|
123
111
|
if (pixels) delete [] (uchar*) pixels;
|
124
112
|
|
125
113
|
width = height = 0;
|
126
|
-
|
127
|
-
pixels
|
128
|
-
context
|
129
|
-
dirty
|
114
|
+
color_space = COLORSPACE_UNKNOWN;
|
115
|
+
pixels = NULL;
|
116
|
+
context = NULL;
|
117
|
+
dirty = false;
|
130
118
|
}
|
131
119
|
|
132
120
|
};// Bitmap::Data
|
133
121
|
|
134
122
|
|
123
|
+
static void
|
124
|
+
setup_bitmap (
|
125
|
+
Bitmap* this_,
|
126
|
+
int w, int h, const ColorSpace& cs,
|
127
|
+
const void* pixels_ = NULL, bool clear_pixels = true)
|
128
|
+
{
|
129
|
+
if (!this_ || w <= 0 || h <= 0 || !cs)
|
130
|
+
argument_error(__FILE__, __LINE__);
|
131
|
+
|
132
|
+
this_->self->clear();
|
133
|
+
|
134
|
+
this_->self->width = w;
|
135
|
+
this_->self->height = h;
|
136
|
+
this_->self->color_space = cs;
|
137
|
+
this_->self->dirty = true;
|
138
|
+
|
139
|
+
size_t size = w * h * cs.Bpp();
|
140
|
+
this_->self->pixels = new uchar[size];
|
141
|
+
|
142
|
+
if (pixels_)
|
143
|
+
memcpy(this_->self->pixels, pixels_, size);
|
144
|
+
else if (clear_pixels)
|
145
|
+
memset(this_->self->pixels, 0, size);
|
146
|
+
}
|
147
|
+
|
148
|
+
static void
|
149
|
+
setup_bitmap (Bitmap* this_, const Texture& tex)
|
150
|
+
{
|
151
|
+
if (!this_ || !tex)
|
152
|
+
argument_error(__FILE__, __LINE__);
|
153
|
+
|
154
|
+
setup_bitmap(this_, tex.width(), tex.height(), tex.color_space(), NULL, false);
|
155
|
+
|
156
|
+
GLenum format, type;
|
157
|
+
tex.color_space().get_gl_enums(&format, &type, tex.alpha_only());
|
158
|
+
|
159
|
+
FrameBuffer fb(tex);
|
160
|
+
FrameBufferBinder binder(fb.id());
|
161
|
+
|
162
|
+
for (int y = 0; y < this_->height(); ++y)
|
163
|
+
glReadPixels(0, y, this_->width(), 1, format, type, (GLvoid*) this_->at<uchar>(0, y));
|
164
|
+
}
|
165
|
+
|
166
|
+
|
135
167
|
Bitmap::Bitmap ()
|
136
168
|
{
|
137
169
|
}
|
138
170
|
|
139
|
-
Bitmap::Bitmap (
|
171
|
+
Bitmap::Bitmap (
|
172
|
+
int width, int height, const ColorSpace& color_space, const void* pixels)
|
140
173
|
{
|
141
|
-
|
174
|
+
setup_bitmap(this, width, height, color_space, pixels);
|
175
|
+
}
|
176
|
+
|
177
|
+
Bitmap::Bitmap (const Texture& texture)
|
178
|
+
{
|
179
|
+
setup_bitmap(this, texture);
|
142
180
|
}
|
143
181
|
|
144
182
|
Bitmap::~Bitmap ()
|
145
183
|
{
|
146
184
|
}
|
147
185
|
|
186
|
+
Bitmap
|
187
|
+
Bitmap::copy () const
|
188
|
+
{
|
189
|
+
return Bitmap(width(), height(), color_space(), pixels());
|
190
|
+
}
|
191
|
+
|
148
192
|
int
|
149
193
|
Bitmap::width () const
|
150
194
|
{
|
@@ -167,13 +211,13 @@ namespace Rays
|
|
167
211
|
static const ColorSpace UNKNOWN = COLORSPACE_UNKNOWN;
|
168
212
|
return UNKNOWN;
|
169
213
|
}
|
170
|
-
return self->
|
214
|
+
return self->color_space;
|
171
215
|
}
|
172
216
|
|
173
217
|
int
|
174
218
|
Bitmap::pitch () const
|
175
219
|
{
|
176
|
-
return width() * self->
|
220
|
+
return width() * self->color_space.Bpp();
|
177
221
|
}
|
178
222
|
|
179
223
|
size_t
|
@@ -182,35 +226,35 @@ namespace Rays
|
|
182
226
|
return pitch() * height();
|
183
227
|
}
|
184
228
|
|
185
|
-
|
186
|
-
Bitmap::
|
229
|
+
void*
|
230
|
+
Bitmap::pixels ()
|
187
231
|
{
|
188
|
-
return
|
232
|
+
if (!*this) return NULL;
|
233
|
+
return self->pixels;
|
189
234
|
}
|
190
235
|
|
191
|
-
void
|
192
|
-
Bitmap::
|
236
|
+
const void*
|
237
|
+
Bitmap::pixels () const
|
193
238
|
{
|
194
|
-
|
239
|
+
return const_cast<This*>(this)->pixels();
|
195
240
|
}
|
196
241
|
|
197
|
-
|
198
|
-
Bitmap::
|
242
|
+
bool
|
243
|
+
Bitmap::dirty () const
|
199
244
|
{
|
200
|
-
|
201
|
-
return self->pixels;
|
245
|
+
return self->dirty;
|
202
246
|
}
|
203
247
|
|
204
|
-
|
205
|
-
Bitmap::
|
248
|
+
void
|
249
|
+
Bitmap::set_dirty (bool b)
|
206
250
|
{
|
207
|
-
|
251
|
+
self->dirty = b;
|
208
252
|
}
|
209
253
|
|
210
254
|
Bitmap::operator bool () const
|
211
255
|
{
|
212
256
|
return
|
213
|
-
self->width > 0 && self->height > 0 && self->
|
257
|
+
self->width > 0 && self->height > 0 && self->color_space && self->pixels;
|
214
258
|
}
|
215
259
|
|
216
260
|
bool
|
@@ -220,69 +264,76 @@ namespace Rays
|
|
220
264
|
}
|
221
265
|
|
222
266
|
|
223
|
-
|
224
|
-
load_bitmap (
|
267
|
+
Bitmap
|
268
|
+
load_bitmap (const char* path_)
|
225
269
|
{
|
226
|
-
if (!
|
270
|
+
if (!path_ || path_[0] == '\0')
|
271
|
+
argument_error(__FILE__, __LINE__);
|
227
272
|
|
228
273
|
NSString* path = [NSString stringWithUTF8String: path_];
|
229
274
|
NSBitmapImageRep* imagerep =
|
230
275
|
[NSBitmapImageRep imageRepWithContentsOfFile: path];
|
231
|
-
if (!imagerep)
|
276
|
+
if (!imagerep)
|
277
|
+
rays_error(__FILE__, __LINE__, "[NSBitmapImageRep imageRepWithContentsOfFile] failed.");
|
232
278
|
|
233
279
|
CGImageRef image = [imagerep CGImage];
|
234
|
-
if (!image)
|
280
|
+
if (!image)
|
281
|
+
rays_error(__FILE__, __LINE__, "[imagerep CGImage] failed.");
|
235
282
|
|
236
283
|
size_t width = CGImageGetWidth(image);
|
237
284
|
size_t height = CGImageGetHeight(image);
|
238
285
|
|
239
|
-
|
240
|
-
if (
|
286
|
+
Bitmap bmp((int) width, (int) height, RGBA);
|
287
|
+
if (!bmp)
|
288
|
+
rays_error(__FILE__, __LINE__, "invalid bitmap.");
|
241
289
|
|
242
|
-
CGContextRef context = bmp
|
243
|
-
if (!context)
|
290
|
+
CGContextRef context = bmp.self->get_context();
|
291
|
+
if (!context)
|
292
|
+
rays_error(__FILE__, __LINE__, "creating CGContext failed.");
|
244
293
|
|
245
294
|
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
|
246
|
-
return
|
295
|
+
return bmp;
|
247
296
|
}
|
248
297
|
|
249
|
-
|
298
|
+
void
|
250
299
|
save_bitmap (const Bitmap& bmp, const char* path_)
|
251
300
|
{
|
252
301
|
boost::shared_ptr<CGImage> img(
|
253
302
|
bmp.self->get_image(), CGImageRelease);
|
254
|
-
if (!img)
|
303
|
+
if (!img)
|
304
|
+
rays_error(__FILE__, __LINE__, "getting CGImage failed.");
|
255
305
|
|
256
306
|
NSString* path = [NSString stringWithUTF8String: path_];
|
257
307
|
NSURL* url = [NSURL fileURLWithPath: path];
|
258
|
-
if (!url)
|
308
|
+
if (!url)
|
309
|
+
rays_error(__FILE__, __LINE__, "creating NSURL failed.");
|
259
310
|
|
260
311
|
boost::shared_ptr<CGImageDestination> dest(
|
261
312
|
CGImageDestinationCreateWithURL((CFURLRef) url, kUTTypePNG, 1, NULL),
|
262
313
|
safe_cfrelease);
|
263
|
-
if (!dest)
|
314
|
+
if (!dest)
|
315
|
+
rays_error(__FILE__, __LINE__, "CGImageDestinationCreateWithURL() failed.");
|
264
316
|
|
265
317
|
CGImageDestinationAddImage(dest.get(), img.get(), NULL);
|
266
|
-
|
318
|
+
if (!CGImageDestinationFinalize(dest.get()))
|
319
|
+
rays_error(__FILE__, __LINE__, "CGImageDestinationFinalize() failed.");
|
267
320
|
}
|
268
321
|
|
269
322
|
|
270
|
-
|
323
|
+
void draw_string (
|
271
324
|
CGContextRef, coord, const char*, coord, coord, const Font&);
|
272
325
|
|
273
|
-
|
326
|
+
void
|
274
327
|
draw_string (
|
275
328
|
Bitmap* bmp, const char* str, coord x, coord y, const Font& font)
|
276
329
|
{
|
277
|
-
if (!bmp || !*bmp || !str || !font)
|
278
|
-
|
279
|
-
if (*str == '\0') return true;
|
330
|
+
if (!bmp || !*bmp || !str || !font)
|
331
|
+
argument_error(__FILE__, __LINE__);
|
280
332
|
|
281
|
-
if (
|
282
|
-
return false;
|
333
|
+
if (*str == '\0') return;
|
283
334
|
|
335
|
+
draw_string(bmp->self->get_context(), bmp->height(), str, x, y, font);
|
284
336
|
bmp->set_dirty();
|
285
|
-
return true;
|
286
337
|
}
|
287
338
|
|
288
339
|
|
data/src/{cocoa → osx}/font.mm
RENAMED
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
#include <ApplicationServices/ApplicationServices.h>
|
6
|
+
#include "rays/exception.h"
|
6
7
|
#include "helper.h"
|
7
8
|
|
8
9
|
|
@@ -78,6 +79,12 @@ namespace Rays
|
|
78
79
|
{
|
79
80
|
}
|
80
81
|
|
82
|
+
Font
|
83
|
+
Font::copy () const
|
84
|
+
{
|
85
|
+
return Font(name(), size());
|
86
|
+
}
|
87
|
+
|
81
88
|
String
|
82
89
|
Font::name () const
|
83
90
|
{
|
@@ -101,42 +108,42 @@ namespace Rays
|
|
101
108
|
return CTFontGetSize(self->font);
|
102
109
|
}
|
103
110
|
|
104
|
-
|
105
|
-
Font::get_width (
|
111
|
+
coord
|
112
|
+
Font::get_width (const char* str) const
|
106
113
|
{
|
107
|
-
if (!
|
114
|
+
if (!str)
|
115
|
+
argument_error(__FILE__, __LINE__);
|
108
116
|
|
109
|
-
if (
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
}
|
117
|
+
if (!*this)
|
118
|
+
invalid_state_error(__FILE__, __LINE__);
|
119
|
+
|
120
|
+
if (*str == '\0') return 0;
|
114
121
|
|
115
122
|
CTLineRef line = make_line(self->font, str);
|
116
|
-
if (!line)
|
123
|
+
if (!line)
|
124
|
+
rays_error(__FILE__, __LINE__, "creating CTLineRef failed.");
|
117
125
|
|
118
|
-
|
126
|
+
coord w = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
|
119
127
|
CFRelease(line);
|
120
128
|
|
121
|
-
return
|
129
|
+
return w;
|
122
130
|
}
|
123
131
|
|
124
|
-
|
125
|
-
Font::get_height (
|
126
|
-
coord* height, coord* ascent, coord* descent, coord* leading) const
|
132
|
+
coord
|
133
|
+
Font::get_height (coord* ascent, coord* descent, coord* leading) const
|
127
134
|
{
|
128
|
-
if (
|
135
|
+
if (!*this)
|
136
|
+
invalid_state_error(__FILE__, __LINE__);
|
129
137
|
|
130
138
|
CGFloat asc = CTFontGetAscent(self->font);
|
131
139
|
CGFloat desc = CTFontGetDescent(self->font);
|
132
140
|
CGFloat lead = CTFontGetLeading(self->font);
|
133
141
|
|
134
|
-
if (height) *height = asc + desc + lead;
|
135
142
|
if (ascent) *ascent = asc;
|
136
143
|
if (descent) *descent = desc;
|
137
144
|
if (leading) *leading = lead;
|
138
145
|
|
139
|
-
return
|
146
|
+
return asc + desc + lead;
|
140
147
|
}
|
141
148
|
|
142
149
|
Font::operator bool () const
|
@@ -159,21 +166,23 @@ namespace Rays
|
|
159
166
|
}
|
160
167
|
|
161
168
|
|
162
|
-
|
169
|
+
void
|
163
170
|
draw_string (
|
164
171
|
CGContextRef context, coord context_height,
|
165
172
|
const char* str, coord x, coord y, const Font& font)
|
166
173
|
{
|
167
|
-
if (!context || !str || !font)
|
174
|
+
if (!context || !str || !font)
|
175
|
+
argument_error(__FILE__, __LINE__);
|
168
176
|
|
169
|
-
if (*str == '\0') return
|
177
|
+
if (*str == '\0') return;
|
170
178
|
|
171
179
|
CTLineRef line = make_line(font.self->font, str);
|
172
|
-
if (!line)
|
180
|
+
if (!line)
|
181
|
+
rays_error(__FILE__, __LINE__, "creating CTLineRef failed.");
|
173
182
|
|
174
183
|
coord width = 0, height = 0, ascent = 0;
|
175
|
-
|
176
|
-
|
184
|
+
width = font.get_width(str);
|
185
|
+
height = font.get_height(&ascent);
|
177
186
|
|
178
187
|
height = ceil(height);
|
179
188
|
ascent = floor(ascent);
|
@@ -191,7 +200,6 @@ namespace Rays
|
|
191
200
|
CGContextRestoreGState(context);
|
192
201
|
|
193
202
|
CFRelease(line);
|
194
|
-
return true;
|
195
203
|
}
|
196
204
|
|
197
205
|
|
data/src/osx/helper.h
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
// -*- c++ -*-
|
2
|
+
#pragma once
|
3
|
+
#ifndef __RAYS_OSX_HELPER_H__
|
4
|
+
#define __RAYS_OSX_HELPER_H__
|
5
|
+
|
6
|
+
|
7
|
+
#include <boost/shared_ptr.hpp>
|
8
|
+
#include <CoreFoundation/CoreFoundation.h>
|
9
|
+
|
10
|
+
|
11
|
+
namespace Rays
|
12
|
+
{
|
13
|
+
|
14
|
+
|
15
|
+
void safe_cfrelease (CFTypeRef ref);
|
16
|
+
|
17
|
+
|
18
|
+
typedef boost::shared_ptr<const __CFString> CFString;
|
19
|
+
|
20
|
+
CFString cfstring (const char* str);
|
21
|
+
|
22
|
+
|
23
|
+
}// Rays
|
24
|
+
|
25
|
+
|
26
|
+
#endif//EOH
|
data/src/osx/helper.mm
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
// -*- objc -*-
|
2
|
+
#include "helper.h"
|
3
|
+
|
4
|
+
|
5
|
+
namespace Rays
|
6
|
+
{
|
7
|
+
|
8
|
+
|
9
|
+
void
|
10
|
+
safe_cfrelease (CFTypeRef ref)
|
11
|
+
{
|
12
|
+
if (ref) CFRelease(ref);
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
CFString
|
17
|
+
cfstring (const char* str)
|
18
|
+
{
|
19
|
+
CFStringRef ref = CFStringCreateWithCString(
|
20
|
+
kCFAllocatorDefault, str, kCFStringEncodingUTF8);
|
21
|
+
return CFString(ref, safe_cfrelease);
|
22
|
+
}
|
23
|
+
|
24
|
+
|
25
|
+
}// Rays
|