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/polygon.cpp
CHANGED
|
@@ -147,18 +147,18 @@ namespace Rays
|
|
|
147
147
|
{
|
|
148
148
|
draw_polygon(
|
|
149
149
|
painter, MODE_TRIANGLES,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
ptexcoords ?
|
|
150
|
+
points.data(), points.size(),
|
|
151
|
+
indices.data(), indices.size(),
|
|
152
|
+
pcolors->data(),
|
|
153
|
+
ptexcoords ? ptexcoords->data() : NULL);
|
|
154
154
|
}
|
|
155
155
|
else
|
|
156
156
|
{
|
|
157
157
|
draw_polygon(
|
|
158
158
|
painter, MODE_TRIANGLES, color,
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
ptexcoords ?
|
|
159
|
+
points.data(), points.size(),
|
|
160
|
+
indices.data(), indices.size(),
|
|
161
|
+
ptexcoords ? ptexcoords->data() : NULL);
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
|
|
@@ -227,7 +227,7 @@ namespace Rays
|
|
|
227
227
|
size_t index_offset = 0;
|
|
228
228
|
for (const auto& seg : segments)
|
|
229
229
|
{
|
|
230
|
-
Polyline polyline(
|
|
230
|
+
Polyline polyline(points.data() + seg.begin, seg.end - seg.begin);
|
|
231
231
|
if (!seg.hole)
|
|
232
232
|
{
|
|
233
233
|
triangulate(polylines, index_offset);
|
|
@@ -902,7 +902,7 @@ namespace Rays
|
|
|
902
902
|
points.emplace_back(make_ellipse_point(
|
|
903
903
|
x, y, width, height, radian_from, radian_to, nsegment, seg));
|
|
904
904
|
}
|
|
905
|
-
append(Polyline(
|
|
905
|
+
append(Polyline(points.data(), points.size(), true));
|
|
906
906
|
|
|
907
907
|
if (has_hole)
|
|
908
908
|
{
|
|
@@ -916,7 +916,7 @@ namespace Rays
|
|
|
916
916
|
hole_x, hole_y, hole_size.x, hole_size.y,
|
|
917
917
|
radian_from, radian_to, nsegment, seg));
|
|
918
918
|
}
|
|
919
|
-
append(Polyline(
|
|
919
|
+
append(Polyline(points.data(), points.size(), true, NULL, NULL, true));
|
|
920
920
|
}
|
|
921
921
|
}
|
|
922
922
|
|
|
@@ -961,7 +961,7 @@ namespace Rays
|
|
|
961
961
|
}
|
|
962
962
|
}
|
|
963
963
|
|
|
964
|
-
append(Polyline(
|
|
964
|
+
append(Polyline(points.data(), points.size(), true));
|
|
965
965
|
}
|
|
966
966
|
|
|
967
967
|
Point make_ellipse_point (
|
|
@@ -1040,7 +1040,7 @@ namespace Rays
|
|
|
1040
1040
|
array.reserve(size + 1);
|
|
1041
1041
|
array.insert(array.begin(), points, points + size);
|
|
1042
1042
|
array.emplace_back(points[0]);
|
|
1043
|
-
polygon->self->append(Polyline(
|
|
1043
|
+
polygon->self->append(Polyline(array.data(), array.size(), true, false));
|
|
1044
1044
|
}
|
|
1045
1045
|
#endif
|
|
1046
1046
|
return p;
|
|
@@ -1138,9 +1138,9 @@ namespace Rays
|
|
|
1138
1138
|
}
|
|
1139
1139
|
|
|
1140
1140
|
p.self->append(Polyline(
|
|
1141
|
-
|
|
1142
|
-
pcolors_ ?
|
|
1143
|
-
ptexcoords_ ?
|
|
1141
|
+
points_.data(), points_.size(), true,
|
|
1142
|
+
pcolors_ ? pcolors_->data() : NULL,
|
|
1143
|
+
ptexcoords_ ? ptexcoords_->data() : NULL));
|
|
1144
1144
|
if (size >= 4)
|
|
1145
1145
|
{
|
|
1146
1146
|
p.self->append(Polyline(
|
|
@@ -1297,9 +1297,9 @@ namespace Rays
|
|
|
1297
1297
|
}
|
|
1298
1298
|
|
|
1299
1299
|
p.self->append(Polyline(
|
|
1300
|
-
|
|
1301
|
-
pcolors_ ?
|
|
1302
|
-
ptexcoords_ ?
|
|
1300
|
+
points_.data(), points_.size(), true,
|
|
1301
|
+
pcolors_ ? pcolors_->data() : NULL,
|
|
1302
|
+
ptexcoords_ ? ptexcoords_->data() : NULL));
|
|
1303
1303
|
for (size_t i = 2; i < in_last; i += 2)
|
|
1304
1304
|
{
|
|
1305
1305
|
p.self->append(Polyline(
|
|
@@ -1410,7 +1410,7 @@ namespace Rays
|
|
|
1410
1410
|
}
|
|
1411
1411
|
}
|
|
1412
1412
|
|
|
1413
|
-
return create_line(
|
|
1413
|
+
return create_line(result.data(), result.size(), loop);
|
|
1414
1414
|
}
|
|
1415
1415
|
|
|
1416
1416
|
Polygon
|
|
@@ -1632,7 +1632,7 @@ namespace Rays
|
|
|
1632
1632
|
for (const auto& polyline : lhs)
|
|
1633
1633
|
polylines.emplace_back(polyline);
|
|
1634
1634
|
polylines.emplace_back(rhs);
|
|
1635
|
-
return Polygon(
|
|
1635
|
+
return Polygon(polylines.data(), polylines.size());
|
|
1636
1636
|
}
|
|
1637
1637
|
|
|
1638
1638
|
Polygon
|
|
@@ -1641,7 +1641,7 @@ namespace Rays
|
|
|
1641
1641
|
std::vector<Polyline> polylines;
|
|
1642
1642
|
for (const auto& polyline : lhs) polylines.emplace_back(polyline);
|
|
1643
1643
|
for (const auto& polyline : rhs) polylines.emplace_back(polyline);
|
|
1644
|
-
return Polygon(
|
|
1644
|
+
return Polygon(polylines.data(), polylines.size());
|
|
1645
1645
|
}
|
|
1646
1646
|
|
|
1647
1647
|
Polygon
|
data/src/polyline.cpp
CHANGED
|
@@ -129,7 +129,7 @@ namespace Rays
|
|
|
129
129
|
|
|
130
130
|
Polyline pl;
|
|
131
131
|
pl.self->reset(
|
|
132
|
-
|
|
132
|
+
cleaned.data(), NULL, NULL, cleaned.size(), loop, loop, hole,
|
|
133
133
|
[](const IntPoint& point) {return from_clipper(point);});
|
|
134
134
|
return pl;
|
|
135
135
|
}
|
|
@@ -224,21 +224,21 @@ namespace Rays
|
|
|
224
224
|
Polyline::points () const
|
|
225
225
|
{
|
|
226
226
|
const auto& v = self->points;
|
|
227
|
-
return !v.empty() ?
|
|
227
|
+
return !v.empty() ? v.data() : NULL;
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
const Color*
|
|
231
231
|
Polyline::colors () const
|
|
232
232
|
{
|
|
233
233
|
const auto& pv = self->pcolors;
|
|
234
|
-
return pv && !pv->empty() ?
|
|
234
|
+
return pv && !pv->empty() ? pv->data() : NULL;
|
|
235
235
|
}
|
|
236
236
|
|
|
237
237
|
const Coord3*
|
|
238
238
|
Polyline::texcoords () const
|
|
239
239
|
{
|
|
240
240
|
const auto& pv = self->ptexcoords;
|
|
241
|
-
return pv && !pv->empty() ?
|
|
241
|
+
return pv && !pv->empty() ? pv->data() : NULL;
|
|
242
242
|
}
|
|
243
243
|
|
|
244
244
|
size_t
|
data/src/sdl/bitmap.cpp
CHANGED
|
@@ -6,9 +6,12 @@
|
|
|
6
6
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
7
7
|
#include <stb_image_write.h>
|
|
8
8
|
|
|
9
|
+
#include <memory>
|
|
9
10
|
#include <SDL.h>
|
|
10
11
|
#include <xot/util.h>
|
|
11
12
|
#include "rays/exception.h"
|
|
13
|
+
#include "rays/image.h"
|
|
14
|
+
#include "rays/painter.h"
|
|
12
15
|
#include "../font.h"
|
|
13
16
|
#include "../texture.h"
|
|
14
17
|
|
|
@@ -41,7 +44,7 @@ namespace Rays
|
|
|
41
44
|
if (surface) SDL_FreeSurface(surface);
|
|
42
45
|
|
|
43
46
|
surface = NULL;
|
|
44
|
-
color_space =
|
|
47
|
+
color_space = COLORSPACE_NONE;
|
|
45
48
|
modified = false;
|
|
46
49
|
}
|
|
47
50
|
|
|
@@ -142,8 +145,8 @@ namespace Rays
|
|
|
142
145
|
return strrchr(path, '.');
|
|
143
146
|
}
|
|
144
147
|
|
|
145
|
-
void
|
|
146
|
-
|
|
148
|
+
static void
|
|
149
|
+
save_bitmap (const Bitmap& bitmap, const char* path)
|
|
147
150
|
{
|
|
148
151
|
const char* extension = get_ext(path);
|
|
149
152
|
if (!extension)
|
|
@@ -152,14 +155,14 @@ namespace Rays
|
|
|
152
155
|
__FILE__, __LINE__, "invalid image file extension: '%s'", path);
|
|
153
156
|
}
|
|
154
157
|
|
|
155
|
-
const auto& cs =
|
|
156
|
-
int w =
|
|
157
|
-
int h =
|
|
158
|
-
int
|
|
158
|
+
const auto& cs = bitmap.color_space();
|
|
159
|
+
int w = bitmap.width();
|
|
160
|
+
int h = bitmap.height();
|
|
161
|
+
int row_size = w * cs.Bpp();
|
|
159
162
|
|
|
160
|
-
std::unique_ptr<uchar[]> pixels(new uchar[h *
|
|
163
|
+
std::unique_ptr<uchar[]> pixels(new uchar[h * row_size]);
|
|
161
164
|
for (int y = 0; y < h; ++y)
|
|
162
|
-
memcpy(pixels.get() +
|
|
165
|
+
memcpy(pixels.get() + row_size * y, bitmap.at<uchar>(0, y), row_size);
|
|
163
166
|
|
|
164
167
|
String ext = extension;
|
|
165
168
|
ext.downcase();
|
|
@@ -180,6 +183,27 @@ namespace Rays
|
|
|
180
183
|
rays_error(__FILE__, __LINE__, "failed to save: '%s'", path);
|
|
181
184
|
}
|
|
182
185
|
|
|
186
|
+
void
|
|
187
|
+
Bitmap_save (const Bitmap& bitmap, const char* path)
|
|
188
|
+
{
|
|
189
|
+
Bitmap bmp = bitmap;
|
|
190
|
+
ColorSpace cs = bmp.color_space();
|
|
191
|
+
if (!cs.is_rgb() || (cs.has_alpha() && !cs.is_alpha_last()))
|
|
192
|
+
{
|
|
193
|
+
Image img(bmp.width(), bmp.height(), cs.has_alpha() ? RGBA : RGB);
|
|
194
|
+
Painter p = img.painter();
|
|
195
|
+
p.begin();
|
|
196
|
+
p.image(Image(bmp));
|
|
197
|
+
p.end();
|
|
198
|
+
bmp = img.bitmap();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (bmp.color_space().has_alpha())
|
|
202
|
+
bmp = bmp.dup(false);// stb_image_write expects straight alpha
|
|
203
|
+
|
|
204
|
+
save_bitmap(bmp, path);
|
|
205
|
+
}
|
|
206
|
+
|
|
183
207
|
Bitmap
|
|
184
208
|
Bitmap_load (const char* path)
|
|
185
209
|
{
|
|
@@ -187,16 +211,17 @@ namespace Rays
|
|
|
187
211
|
argument_error(__FILE__, __LINE__);
|
|
188
212
|
|
|
189
213
|
int w = 0, h = 0, Bpp = 0;
|
|
190
|
-
uchar
|
|
214
|
+
std::unique_ptr<uchar, decltype(&stbi_image_free)>
|
|
215
|
+
pixels(stbi_load(path, &w, &h, &Bpp, 0), stbi_image_free);
|
|
191
216
|
if (!pixels)
|
|
192
217
|
rays_error(__FILE__, __LINE__, "failed to load: '%s'", path);
|
|
193
218
|
|
|
194
219
|
ColorSpace cs;
|
|
195
220
|
switch (Bpp)
|
|
196
221
|
{
|
|
197
|
-
case 1: cs = GRAY_8;
|
|
198
|
-
case 3: cs = RGB_888;
|
|
199
|
-
case 4: cs = RGBA_8888; break
|
|
222
|
+
case 1: cs = GRAY_8; break;
|
|
223
|
+
case 3: cs = RGB_888; break;
|
|
224
|
+
case 4: cs = ColorSpace(RGBA_8888, false); break;// stb_image gives straight alpha
|
|
200
225
|
default:
|
|
201
226
|
rays_error(__FILE__, __LINE__, "unsupported image file: '%s'", path);
|
|
202
227
|
}
|
|
@@ -205,11 +230,11 @@ namespace Rays
|
|
|
205
230
|
if (!bmp)
|
|
206
231
|
rays_error(__FILE__, __LINE__, "failed to create Bitmap object");
|
|
207
232
|
|
|
208
|
-
int
|
|
233
|
+
int size = Bpp * w;
|
|
209
234
|
for (int y = 0; y < h; ++y)
|
|
210
|
-
memcpy(bmp.at<uchar>(0, y), pixels +
|
|
235
|
+
memcpy(bmp.at<uchar>(0, y), pixels.get() + size * y, size);
|
|
211
236
|
|
|
212
|
-
return bmp;
|
|
237
|
+
return cs.has_alpha() ? bmp.dup(true) : bmp;
|
|
213
238
|
}
|
|
214
239
|
|
|
215
240
|
|
|
@@ -252,8 +277,8 @@ namespace Rays
|
|
|
252
277
|
{
|
|
253
278
|
if (!*this)
|
|
254
279
|
{
|
|
255
|
-
static const ColorSpace
|
|
256
|
-
return
|
|
280
|
+
static const ColorSpace NONE = COLORSPACE_NONE;
|
|
281
|
+
return NONE;
|
|
257
282
|
}
|
|
258
283
|
return self->color_space;
|
|
259
284
|
}
|
data/src/sdl/font.cpp
CHANGED
|
@@ -20,7 +20,11 @@ namespace Rays
|
|
|
20
20
|
|
|
21
21
|
String name;
|
|
22
22
|
|
|
23
|
-
coord size
|
|
23
|
+
coord size = 0;
|
|
24
|
+
|
|
25
|
+
int weight = Font::WEIGHT_NORMAL;
|
|
26
|
+
|
|
27
|
+
bool italic = false;
|
|
24
28
|
|
|
25
29
|
virtual ~Data ()
|
|
26
30
|
{
|
|
@@ -63,7 +67,7 @@ namespace Rays
|
|
|
63
67
|
|
|
64
68
|
String path;
|
|
65
69
|
|
|
66
|
-
SDLFontData (const char* path, coord size)
|
|
70
|
+
SDLFontData (const char* path, coord size, int weight, bool italic)
|
|
67
71
|
{
|
|
68
72
|
if (!path)
|
|
69
73
|
argument_error(__FILE__, __LINE__);
|
|
@@ -72,8 +76,16 @@ namespace Rays
|
|
|
72
76
|
if (!font)
|
|
73
77
|
rays_error(__FILE__, __LINE__, "failed to open font: %s", TTF_GetError());
|
|
74
78
|
|
|
75
|
-
this->path
|
|
76
|
-
this->size
|
|
79
|
+
this->path = path;
|
|
80
|
+
this->size = size;
|
|
81
|
+
this->weight = weight;
|
|
82
|
+
this->italic = italic;
|
|
83
|
+
|
|
84
|
+
int style = TTF_STYLE_NORMAL;
|
|
85
|
+
if (weight >= Font::WEIGHT_SEMIBOLD) style |= TTF_STYLE_BOLD;
|
|
86
|
+
if (italic) style |= TTF_STYLE_ITALIC;
|
|
87
|
+
if (style != TTF_STYLE_NORMAL)
|
|
88
|
+
TTF_SetFontStyle(font, style);
|
|
77
89
|
|
|
78
90
|
const char* family = TTF_FontFaceFamilyName(font);
|
|
79
91
|
if (family) this->name = family;
|
|
@@ -91,8 +103,9 @@ namespace Rays
|
|
|
91
103
|
rays_error(__FILE__, __LINE__, "TTF_RenderUTF8_Blended failed: %s", TTF_GetError());
|
|
92
104
|
|
|
93
105
|
SDL_Rect dst = {(int) x, (int) y, surface->w, surface->h};
|
|
106
|
+
// blending onto zeros premultiplies the straight alpha of SDL_ttf
|
|
94
107
|
SDL_FillRect(target, &dst, 0);
|
|
95
|
-
SDL_SetSurfaceBlendMode(surface,
|
|
108
|
+
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
|
|
96
109
|
SDL_BlitSurface(surface, NULL, target, &dst);
|
|
97
110
|
SDL_FreeSurface(surface);
|
|
98
111
|
}
|
|
@@ -126,7 +139,7 @@ namespace Rays
|
|
|
126
139
|
|
|
127
140
|
Data* dup (coord size) const override
|
|
128
141
|
{
|
|
129
|
-
return new SDLFontData(path.c_str(), size);
|
|
142
|
+
return new SDLFontData(path.c_str(), size, weight, italic);
|
|
130
143
|
}
|
|
131
144
|
|
|
132
145
|
};// SDLData
|
|
@@ -144,22 +157,22 @@ namespace Rays
|
|
|
144
157
|
});
|
|
145
158
|
|
|
146
159
|
EM_JS(double, rays_wasm_font_text_width_, (
|
|
147
|
-
const char* str, const char* name, double size),
|
|
160
|
+
const char* str, const char* name, double size, int weight, int italic),
|
|
148
161
|
{
|
|
149
162
|
const s = UTF8ToString(str);
|
|
150
163
|
const n = UTF8ToString(name);
|
|
151
164
|
const c = Module._raysFontContext;
|
|
152
|
-
c.font = `${size}px "${n}"`;
|
|
165
|
+
c.font = `${italic ? 'italic' : 'normal'} ${weight} ${size}px "${n}"`;
|
|
153
166
|
return c.measureText(s).width;
|
|
154
167
|
});
|
|
155
168
|
|
|
156
169
|
EM_JS(void, rays_wasm_font_get_metrics_, (
|
|
157
|
-
const char* name, double size,
|
|
170
|
+
const char* name, double size, int weight, int italic,
|
|
158
171
|
double* out_ascent, double* out_descent, double* out_leading),
|
|
159
172
|
{
|
|
160
173
|
const n = UTF8ToString(name);
|
|
161
174
|
const c = Module._raysFontContext;
|
|
162
|
-
c.font = `${size}px "${n}"`;
|
|
175
|
+
c.font = `${italic ? 'italic' : 'normal'} ${weight} ${size}px "${n}"`;
|
|
163
176
|
const m = c.measureText('Mgjpqy');
|
|
164
177
|
const asc = Math.max(
|
|
165
178
|
m. fontBoundingBoxAscent ?? 0,
|
|
@@ -174,29 +187,30 @@ namespace Rays
|
|
|
174
187
|
});
|
|
175
188
|
|
|
176
189
|
EM_JS(void*, rays_wasm_font_render_, (
|
|
177
|
-
const char* str, const char* name, double size,
|
|
190
|
+
const char* str, const char* name, double size, int weight, int italic,
|
|
178
191
|
int* out_width, int* out_height, double* out_ascent, double* out_descent),
|
|
179
192
|
{
|
|
180
|
-
const s
|
|
181
|
-
const n
|
|
182
|
-
const c
|
|
183
|
-
const cv
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const
|
|
193
|
+
const s = UTF8ToString(str);
|
|
194
|
+
const n = UTF8ToString(name);
|
|
195
|
+
const c = Module._raysFontContext;
|
|
196
|
+
const cv = Module._raysFontCanvas;
|
|
197
|
+
const font = `${italic ? 'italic' : 'normal'} ${weight} ${size}px "${n}"`;
|
|
198
|
+
c.font = font;
|
|
199
|
+
const m = c.measureText(s);
|
|
200
|
+
const asc = Math.max(
|
|
187
201
|
m. fontBoundingBoxAscent ?? 0,
|
|
188
202
|
m.actualBoundingBoxAscent ?? size * 0.8);
|
|
189
|
-
const dsc
|
|
203
|
+
const dsc = Math.max(
|
|
190
204
|
m. fontBoundingBoxDescent ?? 0,
|
|
191
205
|
m.actualBoundingBoxDescent ?? size * 0.2);
|
|
192
|
-
const w
|
|
193
|
-
const h
|
|
206
|
+
const w = Math.max(1, Math.ceil(m.width));
|
|
207
|
+
const h = Math.max(1, Math.ceil(asc + dsc));
|
|
194
208
|
|
|
195
209
|
if (cv.width < w) cv.width = w;
|
|
196
210
|
if (cv.height < h) cv.height = h;
|
|
197
211
|
|
|
198
212
|
c.clearRect(0, 0, cv.width, cv.height);
|
|
199
|
-
c.font =
|
|
213
|
+
c.font = font;// re-apply after canvas resize
|
|
200
214
|
c.fillStyle = 'white';
|
|
201
215
|
c.textBaseline = 'alphabetic';
|
|
202
216
|
c.fillText(s, 0, asc);
|
|
@@ -234,12 +248,14 @@ namespace Rays
|
|
|
234
248
|
struct CanvasFontData : public RawFont::Data
|
|
235
249
|
{
|
|
236
250
|
|
|
237
|
-
CanvasFontData (const char* name, coord size)
|
|
251
|
+
CanvasFontData (const char* name, coord size, int weight, bool italic)
|
|
238
252
|
{
|
|
239
253
|
rays_wasm_font_init_();
|
|
240
254
|
|
|
241
|
-
this->name
|
|
242
|
-
this->size
|
|
255
|
+
this->name = name && *name != '\0' ? name : "sans-serif";
|
|
256
|
+
this->size = size;
|
|
257
|
+
this->weight = weight;
|
|
258
|
+
this->italic = italic;
|
|
243
259
|
}
|
|
244
260
|
|
|
245
261
|
void draw_string (SDL_Surface* target, const char* str, coord x, coord y) override
|
|
@@ -248,7 +264,7 @@ namespace Rays
|
|
|
248
264
|
double ascent = 0, descent = 0;
|
|
249
265
|
std::shared_ptr<void> pixels(
|
|
250
266
|
rays_wasm_font_render_(
|
|
251
|
-
str, name.c_str(), (double) size,
|
|
267
|
+
str, name.c_str(), (double) size, weight, italic,
|
|
252
268
|
&w, &h, &ascent, &descent),
|
|
253
269
|
free);
|
|
254
270
|
if (!pixels || w <= 0 || h <= 0) return;
|
|
@@ -260,21 +276,24 @@ namespace Rays
|
|
|
260
276
|
rays_error(__FILE__, __LINE__, "SDL_CreateRGBSurfaceFrom failed: %s", SDL_GetError());
|
|
261
277
|
|
|
262
278
|
SDL_Rect dest = {(int) x, (int) y, w, h};
|
|
279
|
+
// blending onto zeros premultiplies the straight alpha of the canvas
|
|
263
280
|
SDL_FillRect(target, &dest, 0);
|
|
264
|
-
SDL_SetSurfaceBlendMode(surface,
|
|
281
|
+
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
|
|
265
282
|
SDL_BlitSurface(surface, NULL, target, &dest);
|
|
266
283
|
SDL_FreeSurface(surface);
|
|
267
284
|
}
|
|
268
285
|
|
|
269
286
|
coord get_width (const char* str) override
|
|
270
287
|
{
|
|
271
|
-
return (coord) rays_wasm_font_text_width_(
|
|
288
|
+
return (coord) rays_wasm_font_text_width_(
|
|
289
|
+
str, name.c_str(), (double) size, weight, italic);
|
|
272
290
|
}
|
|
273
291
|
|
|
274
292
|
coord get_height (coord* ascent, coord* descent, coord* leading) override
|
|
275
293
|
{
|
|
276
294
|
double asc = 0, desc = 0, lead = 0;
|
|
277
|
-
rays_wasm_font_get_metrics_(
|
|
295
|
+
rays_wasm_font_get_metrics_(
|
|
296
|
+
name.c_str(), (double) size, weight, italic, &asc, &desc, &lead);
|
|
278
297
|
|
|
279
298
|
if (ascent) *ascent = (coord) asc;
|
|
280
299
|
if (descent) *descent = (coord) desc;
|
|
@@ -290,7 +309,7 @@ namespace Rays
|
|
|
290
309
|
|
|
291
310
|
Data* dup (coord size) const override
|
|
292
311
|
{
|
|
293
|
-
return new CanvasFontData(name.c_str(), size);
|
|
312
|
+
return new CanvasFontData(name.c_str(), size, weight, italic);
|
|
294
313
|
}
|
|
295
314
|
|
|
296
315
|
};// CanvasData
|
|
@@ -334,7 +353,7 @@ namespace Rays
|
|
|
334
353
|
RawFont_load (const char* path, coord size)
|
|
335
354
|
{
|
|
336
355
|
RawFont rawfont;
|
|
337
|
-
rawfont.self.reset(new SDLFontData(path, size));
|
|
356
|
+
rawfont.self.reset(new SDLFontData(path, size, Font::WEIGHT_NORMAL, false));
|
|
338
357
|
return rawfont;
|
|
339
358
|
}
|
|
340
359
|
|
|
@@ -344,29 +363,23 @@ namespace Rays
|
|
|
344
363
|
}
|
|
345
364
|
|
|
346
365
|
static RawFont::Data*
|
|
347
|
-
create_data (const char* name, coord size)
|
|
366
|
+
create_data (const char* name, coord size, int weight, bool italic)
|
|
348
367
|
{
|
|
349
368
|
#ifdef WASM
|
|
350
|
-
return new CanvasFontData(name, size);
|
|
369
|
+
return new CanvasFontData(name, size, weight, italic);
|
|
351
370
|
#else
|
|
352
371
|
if (name)
|
|
353
372
|
not_implemented_error(__FILE__, __LINE__);
|
|
354
|
-
return new SDLFontData(
|
|
373
|
+
return new SDLFontData(
|
|
374
|
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size, weight, italic);
|
|
355
375
|
#endif
|
|
356
376
|
}
|
|
357
377
|
|
|
358
|
-
RawFont::RawFont (const char* name, coord size)
|
|
359
|
-
: self(create_data(name, size))
|
|
378
|
+
RawFont::RawFont (const char* name, coord size, int weight, bool italic)
|
|
379
|
+
: self(create_data(name, size, weight, italic))
|
|
360
380
|
{
|
|
361
381
|
}
|
|
362
382
|
|
|
363
|
-
RawFont::RawFont (const This& obj, coord size)
|
|
364
|
-
{
|
|
365
|
-
if (!obj) return;
|
|
366
|
-
|
|
367
|
-
self.reset(obj.self->dup(size));
|
|
368
|
-
}
|
|
369
|
-
|
|
370
383
|
RawFont::~RawFont ()
|
|
371
384
|
{
|
|
372
385
|
}
|
|
@@ -398,13 +411,6 @@ namespace Rays
|
|
|
398
411
|
return self->name;
|
|
399
412
|
}
|
|
400
413
|
|
|
401
|
-
coord
|
|
402
|
-
RawFont::size () const
|
|
403
|
-
{
|
|
404
|
-
if (!*this) return 0;
|
|
405
|
-
return self->size;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
414
|
coord
|
|
409
415
|
RawFont::get_width (const char* str) const
|
|
410
416
|
{
|
data/src/texture.h
CHANGED