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/src/win32/bitmap.cpp CHANGED
@@ -1,12 +1,18 @@
1
1
  #include "../bitmap.h"
2
2
 
3
3
 
4
+ #define STBI_WINDOWS_UTF8
5
+ #define STBIW_WINDOWS_UTF8
6
+
4
7
  #define STB_IMAGE_IMPLEMENTATION
5
8
  #include <stb_image.h>
6
9
  #define STB_IMAGE_WRITE_IMPLEMENTATION
7
10
  #include <stb_image_write.h>
8
11
 
12
+ #include <memory>
9
13
  #include "rays/exception.h"
14
+ #include "rays/image.h"
15
+ #include "rays/painter.h"
10
16
  #include "../font.h"
11
17
  #include "../texture.h"
12
18
  #include "gdi.h"
@@ -44,7 +50,7 @@ namespace Rays
44
50
  if (memdc) memdc = Win32::MemoryDC();
45
51
 
46
52
  width = height = pitch = 0;
47
- color_space = COLORSPACE_UNKNOWN;
53
+ color_space = COLORSPACE_NONE;
48
54
  pixels = NULL;
49
55
  modified = false;
50
56
  }
@@ -52,6 +58,26 @@ namespace Rays
52
58
  };// Bitmap::Data
53
59
 
54
60
 
61
+ static HBITMAP
62
+ create_hbitmap (const Bitmap& bitmap, const Win32::DC& dc, void** pixels)
63
+ {
64
+ Bitmap::Data* self = bitmap.self.get();
65
+
66
+ BITMAPINFO bmpinfo;
67
+ memset(&bmpinfo, 0, sizeof(bmpinfo));
68
+
69
+ BITMAPINFOHEADER& header = bmpinfo.bmiHeader;
70
+ header.biSize = sizeof(BITMAPINFOHEADER);
71
+ header.biWidth = self->width;
72
+ header.biHeight = -self->height;
73
+ header.biPlanes = 1;
74
+ header.biBitCount = self->color_space.bpp();
75
+ header.biCompression = BI_RGB;
76
+
77
+ return CreateDIBSection(
78
+ dc.handle(), &bmpinfo, DIB_RGB_COLORS, pixels, NULL, 0);
79
+ }
80
+
55
81
  void
56
82
  Bitmap_setup (
57
83
  Bitmap* bitmap, int w, int h, const ColorSpace& cs,
@@ -77,21 +103,8 @@ namespace Rays
77
103
  int padding = 4 - self->pitch % 4;
78
104
  if (padding < 4) self->pitch += padding;
79
105
 
80
- BITMAPINFO bmpinfo;
81
- memset(&bmpinfo, 0, sizeof(bmpinfo));
82
-
83
- BITMAPINFOHEADER& header = bmpinfo.bmiHeader;
84
- header.biSize = sizeof(BITMAPINFOHEADER);
85
- header.biWidth = self->width;
86
- header.biHeight = -self->height;
87
- header.biPlanes = 1;
88
- header.biBitCount = self->color_space.bpp();
89
- header.biCompression = BI_RGB;
90
-
91
106
  Win32::DC dc = Win32::screen_dc();
92
-
93
- HBITMAP hbmp = CreateDIBSection(
94
- dc.handle(), &bmpinfo, DIB_RGB_COLORS, (void**) &self->pixels, NULL, 0);
107
+ HBITMAP hbmp = create_hbitmap(*bitmap, dc, (void**) &self->pixels);
95
108
  if (!hbmp)
96
109
  rays_error(__FILE__, __LINE__);
97
110
 
@@ -99,11 +112,10 @@ namespace Rays
99
112
  if (!self->memdc)
100
113
  rays_error(__FILE__, __LINE__);
101
114
 
102
- size_t size = self->pitch * self->height;
103
115
  if (pixels)
104
- memcpy(self->pixels, pixels, size);
116
+ memcpy(self->pixels, pixels, bitmap->size());
105
117
  else if (clear_pixels)
106
- memset(self->pixels, 0, size);
118
+ memset(self->pixels, 0, bitmap->size());
107
119
  }
108
120
 
109
121
  void
@@ -147,36 +159,36 @@ namespace Rays
147
159
  return strrchr(path, '.');
148
160
  }
149
161
 
150
- void
151
- Bitmap_save (const Bitmap& bmp, const char* path)
162
+ static void
163
+ save_bitmap (const Bitmap& bitmap, const char* path)
152
164
  {
153
- const char* ext = get_ext(path);
154
- if (!ext)
165
+ const char* extension = get_ext(path);
166
+ if (!extension)
155
167
  {
156
168
  argument_error(
157
169
  __FILE__, __LINE__, "invalid image file extension: '%s'", path);
158
170
  }
159
171
 
160
- const auto& cs = bmp.color_space();
161
- size_t w = bmp.width();
162
- size_t h = bmp.height();
163
- size_t pitch = w * cs.Bpp();
172
+ const auto& cs = bitmap.color_space();
173
+ int w = bitmap.width();
174
+ int h = bitmap.height();
175
+ int row_size = w * cs.Bpp();
176
+
177
+ std::unique_ptr<uchar[]> pixels(new uchar[h * row_size]);
178
+ for (int y = 0; y < h; ++y)
179
+ memcpy(pixels.get() + row_size * y, bitmap.at<uchar>(0, y), row_size);
164
180
 
165
- std::unique_ptr<uchar[]> pixels(new uchar[h * pitch]);
166
- for (size_t y = 0; y < h; ++y)
167
- memcpy(pixels.get() + pitch * y, bmp.at<uchar>(0, y), pitch);
181
+ String ext = extension;
182
+ ext.downcase();
168
183
 
169
184
  int ret = 0;
170
- if (stricmp(ext, ".bmp") == 0)
185
+ if (ext == ".bmp")
171
186
  ret = stbi_write_bmp(path, w, h, cs.Bpp(), pixels.get());
172
- else
173
- if (stricmp(ext, ".png") == 0)
187
+ else if (ext == ".png")
174
188
  ret = stbi_write_png(path, w, h, cs.Bpp(), pixels.get(), 0);
175
- else
176
- if (stricmp(ext, ".jpg") == 0 || stricmp(ext, ".jpeg") == 0)
189
+ else if (ext == ".jpg" || ext == ".jpeg")
177
190
  ret = stbi_write_jpg(path, w, h, cs.Bpp(), pixels.get(), 90);
178
- else
179
- if (stricmp(ext, ".tga") == 0)
191
+ else if (ext == ".tga")
180
192
  ret = stbi_write_tga(path, w, h, cs.Bpp(), pixels.get());
181
193
  else
182
194
  argument_error(__FILE__, __LINE__, "unknown image file type");
@@ -185,6 +197,27 @@ namespace Rays
185
197
  rays_error(__FILE__, __LINE__, "failed to save: '%s'", path);
186
198
  }
187
199
 
200
+ void
201
+ Bitmap_save (const Bitmap& bitmap, const char* path)
202
+ {
203
+ Bitmap bmp = bitmap;
204
+ ColorSpace cs = bmp.color_space();
205
+ if (!cs.is_rgb() || (cs.has_alpha() && !cs.is_alpha_last()))
206
+ {
207
+ Image img(bmp.width(), bmp.height(), cs.has_alpha() ? RGBA : RGB);
208
+ Painter p = img.painter();
209
+ p.begin();
210
+ p.image(Image(bmp));
211
+ p.end();
212
+ bmp = img.bitmap();
213
+ }
214
+
215
+ if (bmp.color_space().has_alpha())
216
+ bmp = bmp.dup(false);// stb_image_write expects straight alpha
217
+
218
+ save_bitmap(bmp, path);
219
+ }
220
+
188
221
  Bitmap
189
222
  Bitmap_load (const char* path)
190
223
  {
@@ -192,16 +225,17 @@ namespace Rays
192
225
  argument_error(__FILE__, __LINE__);
193
226
 
194
227
  int w = 0, h = 0, Bpp = 0;
195
- uchar* pixels = stbi_load(path, &w, &h, &Bpp, 0);
228
+ std::unique_ptr<uchar, decltype(&stbi_image_free)>
229
+ pixels(stbi_load(path, &w, &h, &Bpp, 0), stbi_image_free);
196
230
  if (!pixels)
197
231
  rays_error(__FILE__, __LINE__, "failed to load: '%s'", path);
198
232
 
199
233
  ColorSpace cs;
200
234
  switch (Bpp)
201
235
  {
202
- case 1: cs = GRAY_8; break;
203
- case 3: cs = RGB_888; break;
204
- case 4: cs = RGBA_8888; break;
236
+ case 1: cs = GRAY_8; break;
237
+ case 3: cs = RGB_888; break;
238
+ case 4: cs = ColorSpace(RGBA_8888, false); break;// stb_image gives straight alpha
205
239
  default:
206
240
  rays_error(__FILE__, __LINE__, "unsupported image file: '%s'", path);
207
241
  }
@@ -210,11 +244,24 @@ namespace Rays
210
244
  if (!bmp)
211
245
  rays_error(__FILE__, __LINE__, "failed to create Bitmap object");
212
246
 
213
- int pitch = Bpp * w;
247
+ int size = Bpp * w;
214
248
  for (int y = 0; y < h; ++y)
215
- memcpy(bmp.at<uchar>(0, y), pixels + pitch * y, pitch);
249
+ memcpy(bmp.at<uchar>(0, y), pixels.get() + size * y, size);
250
+
251
+ return cs.has_alpha() ? bmp.dup(true) : bmp;
252
+ }
253
+
254
+ HBITMAP
255
+ Bitmap_get_hbitmap (const Bitmap& bmp)
256
+ {
257
+ if (!bmp) return NULL;
258
+
259
+ void* pixels = NULL;
260
+ HBITMAP hbmp = create_hbitmap(bmp, Win32::screen_dc(), &pixels);
261
+ if (!hbmp) return NULL;
216
262
 
217
- return bmp;
263
+ memcpy(pixels, bmp.pixels(), bmp.size());
264
+ return hbmp;
218
265
  }
219
266
 
220
267
 
@@ -257,8 +304,8 @@ namespace Rays
257
304
  {
258
305
  if (!*this)
259
306
  {
260
- static const ColorSpace UNKNOWN = COLORSPACE_UNKNOWN;
261
- return UNKNOWN;
307
+ static const ColorSpace NONE = COLORSPACE_NONE;
308
+ return NONE;
262
309
  }
263
310
  return self->color_space;
264
311
  }
data/src/win32/font.cpp CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
 
4
4
  #include <assert.h>
5
+ #include <string>
5
6
  #include <set>
6
7
  #include "rays/exception.h"
7
8
  #include "gdi.h"
@@ -16,7 +17,7 @@ namespace Rays
16
17
 
17
18
  Win32::Font font;
18
19
 
19
- String path;
20
+ mutable coord height = -1, ascent = 0, descent = 0, leading = 0;
20
21
 
21
22
  };// RawFont::Data
22
23
 
@@ -40,15 +41,18 @@ namespace Rays
40
41
 
41
42
  static int CALLBACK
42
43
  enum_callback (
43
- const ENUMLOGFONT* elf, const NEWTEXTMETRIC* ntm, DWORD font_type, LPARAM lp)
44
+ const ENUMLOGFONTW* elf, const NEWTEXTMETRICW* ntm, DWORD font_type, LPARAM lp)
44
45
  {
45
- const auto* params = (EnumFontFamiliesCallbackParams*) lp;
46
- const char* name = params->fullname
47
- ? (const char*) elf->elfFullName
48
- : (const char*) elf->elfLogFont.lfFaceName;
46
+ const auto* params = (EnumFontFamiliesCallbackParams*) lp;
47
+ const wchar_t* name = params->fullname
48
+ ? elf->elfFullName
49
+ : elf->elfLogFont.lfFaceName;
49
50
 
50
- if (name && *name != '\0' && *name != '@')
51
- params->names->insert(name);
51
+ if (name && *name != L'\0' && *name != L'@')
52
+ {
53
+ size_t max_ = params->fullname ? LF_FULLFACESIZE : LF_FACESIZE;
54
+ params->names->insert(String(name, wcsnlen(name, max_)));
55
+ }
52
56
 
53
57
  return TRUE;
54
58
  }
@@ -57,16 +61,19 @@ namespace Rays
57
61
  get_font_names (
58
62
  StringSet* names, HDC hdc, const char* query = NULL, bool fullname = false)
59
63
  {
60
- assert(!query || strlen(query) < LF_FACESIZE);
61
-
62
- LOGFONT lf = {0};
64
+ LOGFONTW lf = {0};
63
65
  lf.lfCharSet = DEFAULT_CHARSET;
64
66
 
65
- if (query) strcpy(lf.lfFaceName, query);
67
+ if (query)
68
+ {
69
+ std::wstring wquery = String(query).to_wstr();
70
+ assert(wquery.size() < LF_FACESIZE);
71
+ wcsncpy(lf.lfFaceName, wquery.c_str(), LF_FACESIZE - 1);
72
+ }
66
73
 
67
74
  EnumFontFamiliesCallbackParams params(names, fullname);
68
- EnumFontFamiliesEx(
69
- hdc, &lf, (FONTENUMPROC) &enum_callback, (LPARAM) &params, 0);
75
+ EnumFontFamiliesExW(
76
+ hdc, &lf, (FONTENUMPROCW) &enum_callback, (LPARAM) &params, 0);
70
77
  }
71
78
 
72
79
  const FontFamilyMap&
@@ -105,18 +112,9 @@ namespace Rays
105
112
  {
106
113
  }
107
114
 
108
- RawFont::RawFont (const char* name, coord size)
109
- {
110
- self->font = Win32::Font(name, size);
111
- }
112
-
113
- RawFont::RawFont (const This& obj, coord size)
115
+ RawFont::RawFont (const char* name, coord size, int weight, bool italic)
114
116
  {
115
- const char* path = obj.self->path.empty() ? NULL : obj.self->path.c_str();
116
- if (path)
117
- *this = RawFont_load(path, size);
118
- else
119
- self->font = Win32::Font(obj.name().c_str(), size);
117
+ self->font = Win32::Font(name, size, weight, italic);
120
118
  }
121
119
 
122
120
  RawFont::~RawFont ()
@@ -142,8 +140,10 @@ namespace Rays
142
140
  if (!*this)
143
141
  invalid_state_error(__FILE__, __LINE__);
144
142
 
143
+ std::wstring wstr = String(str).to_wstr();
144
+
145
145
  coord width = 0, height = 0;
146
- if (!self->font.get_extent(&width, &height, str))
146
+ if (!self->font.get_extent(&width, &height, wstr.c_str()))
147
147
  rays_error(__FILE__, __LINE__, "failed to get font extent.");
148
148
 
149
149
  DC dc = hdc;
@@ -155,7 +155,7 @@ namespace Rays
155
155
  dc.set_text_color(RGB(255, 255, 255));
156
156
  dc.set_back_color(RGB(0, 0, 0));
157
157
 
158
- BOOL ret = TextOutA(dc.handle(), x, y, str, strlen(str));
158
+ BOOL ret = TextOutW(dc.handle(), x, y, wstr.c_str(), (int) wstr.size());
159
159
 
160
160
  dc.set_font(font);
161
161
  dc.set_text_color(text_color);
@@ -172,13 +172,6 @@ namespace Rays
172
172
  return self->font.name();
173
173
  }
174
174
 
175
- coord
176
- RawFont::size () const
177
- {
178
- if (!*this) return 0;
179
- return self->font.size();
180
- }
181
-
182
175
  coord
183
176
  RawFont::get_width (const char* str) const
184
177
  {
@@ -203,24 +196,30 @@ namespace Rays
203
196
  if (!*this)
204
197
  invalid_state_error(__FILE__, __LINE__);
205
198
 
206
- if (ascent || descent || leading)
199
+ if (self->height < 0)
207
200
  {
208
201
  Win32::DC dc(GetDC(NULL), true, Win32::DC::RELEASE_DC);
209
202
  dc.set_font(self->font);
210
203
 
211
- TEXTMETRIC tm;
212
- GetTextMetrics(dc.handle(), &tm);
204
+ TEXTMETRICW tm;
205
+ if (!GetTextMetricsW(dc.handle(), &tm))
206
+ rays_error(__FILE__, __LINE__, "failed to get font metrics");
207
+
208
+ coord height;
209
+ if (!self->font.get_extent(NULL, &height, L"X"))
210
+ rays_error(__FILE__, __LINE__, "failed to get font height");
213
211
 
214
- if (ascent) *ascent = tm.tmAscent;
215
- if (descent) *descent = tm.tmDescent;
216
- if (leading) *leading = tm.tmExternalLeading;
212
+ self->height = height;
213
+ self->ascent = tm.tmAscent;
214
+ self->descent = tm.tmDescent;
215
+ self->leading = tm.tmExternalLeading;
217
216
  }
218
217
 
219
- coord height;
220
- if (!self->font.get_extent(NULL, &height, "X"))
221
- rays_error(__FILE__, __LINE__, "failed to get font height");
218
+ if (ascent) *ascent = self->ascent;
219
+ if (descent) *descent = self->descent;
220
+ if (leading) *leading = self->leading;
222
221
 
223
- return height;
222
+ return self->height;
224
223
  }
225
224
 
226
225
  RawFont::operator bool () const
data/src/win32/gdi.cpp CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
  #include <assert.h>
5
+ #include <math.h>
6
+ #include <string>
5
7
  #include <vector>
6
8
 
7
9
 
@@ -117,7 +119,7 @@ namespace Rays
117
119
  return false;
118
120
 
119
121
  LOGPEN logpen;
120
- if (!GetObject(handle(), sizeof(logpen), &logpen))
122
+ if (!GetObjectW(handle(), sizeof(logpen), &logpen))
121
123
  return false;
122
124
 
123
125
  if (red) *red = GetRValue(logpen.lopnColor);
@@ -179,7 +181,7 @@ namespace Rays
179
181
  return false;
180
182
 
181
183
  LOGBRUSH logbrush;
182
- if (!GetObject(handle(), sizeof(logbrush), &logbrush))
184
+ if (!GetObjectW(handle(), sizeof(logbrush), &logbrush))
183
185
  return false;
184
186
 
185
187
  if (red) *red = GetRValue(logbrush.lbColor);
@@ -215,34 +217,34 @@ namespace Rays
215
217
 
216
218
 
217
219
  static HFONT
218
- create_font (const char* name, coord size = 0)
220
+ create_font (const char* name, coord size, int weight, bool italic)
219
221
  {
220
- NONCLIENTMETRICSA metrics;
222
+ NONCLIENTMETRICSW metrics;
221
223
  memset(&metrics, 0, sizeof(metrics));
222
224
  metrics.cbSize = sizeof(metrics);
223
225
 
224
- if (!SystemParametersInfoA(
226
+ if (!SystemParametersInfoW(
225
227
  SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0))
226
228
  {
227
229
  return NULL;
228
230
  }
229
231
 
230
- LOGFONTA& logfont = metrics.lfMessageFont;
232
+ LOGFONTW& logfont = metrics.lfMessageFont;
231
233
 
232
234
  if (name)
233
- strcpy(logfont.lfFaceName, name);
234
-
235
- if (size == 0)
236
- logfont.lfHeight = 0;
237
- else
238
235
  {
239
- logfont.lfHeight = -MulDiv(
240
- size,
241
- GetDeviceCaps(screen_dc().handle(), LOGPIXELSY),
242
- 72);
236
+ std::wstring wname = String(name).to_wstr();
237
+ wcsncpy(logfont.lfFaceName, wname.c_str(), LF_FACESIZE - 1);
238
+ logfont.lfFaceName[LF_FACESIZE - 1] = L'\0';
243
239
  }
244
240
 
245
- return CreateFontIndirectA(&logfont);
241
+ logfont.lfHeight = size == 0 ? 0 : -(LONG) fmax(1, round(size));
242
+
243
+ if (weight == 0) weight = 1;// 0 == FW_DONTCARE
244
+ if (weight >= 0) logfont.lfWeight = weight;
245
+ if (italic) logfont.lfItalic = TRUE;
246
+
247
+ return CreateFontIndirectW(&logfont);
246
248
  }
247
249
 
248
250
 
@@ -252,9 +254,9 @@ namespace Rays
252
254
  self->handle.reset(handle, owner);
253
255
  }
254
256
 
255
- Font::Font (const char* name, coord size)
257
+ Font::Font (const char* name, coord size, int weight, bool italic)
256
258
  {
257
- self->handle.reset(create_font(name, size));
259
+ self->handle.reset(create_font(name, size, weight, italic));
258
260
  }
259
261
 
260
262
  Font::~Font ()
@@ -262,16 +264,15 @@ namespace Rays
262
264
  }
263
265
 
264
266
  static bool
265
- calc_size (
266
- coord* width, coord* height, HDC hdc, const char* str)
267
+ calc_size (coord* width, coord* height, HDC hdc, const wchar_t* str)
267
268
  {
268
269
  if (!width || !height || !hdc || !str) return false;
269
270
 
270
- bool empty = *str == '\0';
271
- if (empty) str = " ";
271
+ bool empty = *str == L'\0';
272
+ if (empty) str = L" ";
272
273
 
273
274
  RECT rect = {0, 0, 0, 0};
274
- int ret = DrawTextA(
275
+ int ret = DrawTextW(
275
276
  hdc, str, -1, &rect,
276
277
  DT_EXPANDTABS | DT_NOPREFIX | DT_CALCRECT);
277
278
  if (!ret) return false;
@@ -282,10 +283,16 @@ namespace Rays
282
283
  }
283
284
 
284
285
  bool
285
- Font::get_extent (
286
- coord* width, coord* height, const char* str, HDC hdc)
286
+ Font::get_extent (coord* width, coord* height, const char* str, HDC hdc)
287
+ {
288
+ if (!str) return false;
289
+ return get_extent(width, height, String(str).to_wstr().c_str(), hdc);
290
+ }
291
+
292
+ bool
293
+ Font::get_extent (coord* width, coord* height, const wchar_t* str, HDC hdc)
287
294
  {
288
- if (!*this || (!width && !height))
295
+ if (!*this || (!width && !height) || !str)
289
296
  return false;
290
297
 
291
298
  if (width) *width = 0;
@@ -312,12 +319,12 @@ namespace Rays
312
319
  {
313
320
  if (!*this) return "";
314
321
 
315
- LOGFONT logfont;
322
+ LOGFONTW logfont;
316
323
  int size = sizeof(logfont);
317
- if (GetObject(self->handle.handle(), size, &logfont) != size)
324
+ if (GetObjectW(self->handle.handle(), size, &logfont) != size)
318
325
  return "";
319
326
 
320
- return logfont.lfFaceName;
327
+ return String(logfont.lfFaceName, wcsnlen(logfont.lfFaceName, LF_FACESIZE));
321
328
  }
322
329
 
323
330
  coord
@@ -325,22 +332,12 @@ namespace Rays
325
332
  {
326
333
  if (!*this) return 0;
327
334
 
328
- LOGFONT logfont;
335
+ LOGFONTW logfont;
329
336
  int size = sizeof(logfont);
330
- if (GetObject(self->handle.handle(), size, &logfont) != size)
337
+ if (GetObjectW(self->handle.handle(), size, &logfont) != size)
331
338
  return 0;
332
339
 
333
- if (logfont.lfHeight >= 0)
334
- return logfont.lfHeight;
335
-
336
- if (logfont.lfHeight < 0)
337
- {
338
- return -MulDiv(
339
- logfont.lfHeight, 72,
340
- GetDeviceCaps(screen_dc().handle(), LOGPIXELSY));
341
- }
342
- else
343
- return logfont.lfHeight;
340
+ return logfont.lfHeight >= 0 ? logfont.lfHeight : -logfont.lfHeight;
344
341
  }
345
342
 
346
343
  HFONT
@@ -373,8 +370,8 @@ namespace Rays
373
370
  static bool
374
371
  load_bitmap (uint id, HINSTANCE hinst = NULL)
375
372
  {
376
- if (!hinst) hinst = (HINSTANCE) GetModuleHandle(NULL);
377
- self->handle.reset(LoadBitmap(hinst, MAKEINTRESOURCE(id)));
373
+ if (!hinst) hinst = (HINSTANCE) GetModuleHandleW(NULL);
374
+ self->handle.reset(LoadBitmapW(hinst, MAKEINTRESOURCEW(id)));
378
375
  return !!self->handle;
379
376
  }
380
377
  #endif
@@ -398,7 +395,7 @@ namespace Rays
398
395
  Bitmap::width () const
399
396
  {
400
397
  BITMAP bmp;
401
- if (!*this || !GetObject(handle(), sizeof(bmp), &bmp))
398
+ if (!*this || !GetObjectW(handle(), sizeof(bmp), &bmp))
402
399
  return 0;
403
400
  return bmp.bmWidth;
404
401
  }
@@ -407,7 +404,7 @@ namespace Rays
407
404
  Bitmap::height () const
408
405
  {
409
406
  BITMAP bmp;
410
- if (!*this || !GetObject(handle(), sizeof(bmp), &bmp))
407
+ if (!*this || !GetObjectW(handle(), sizeof(bmp), &bmp))
411
408
  return 0;
412
409
  return bmp.bmHeight;
413
410
  }
data/src/win32/gdi.h CHANGED
@@ -76,7 +76,9 @@ namespace Rays
76
76
 
77
77
  Font (HFONT handle = NULL, bool owner = false);
78
78
 
79
- Font (const char* name, coord size = 0);
79
+ Font (
80
+ const char* name, coord size = 0,
81
+ int weight = -1, bool italic = false);
80
82
 
81
83
  ~Font ();
82
84
 
@@ -87,6 +89,9 @@ namespace Rays
87
89
  bool get_extent (
88
90
  coord* width, coord* height, const char* str, HDC hdc = NULL);
89
91
 
92
+ bool get_extent (
93
+ coord* width, coord* height, const wchar_t* str, HDC hdc = NULL);
94
+
90
95
  HFONT handle () const;
91
96
 
92
97
  operator bool () const;
data/src/win32/rays.cpp CHANGED
@@ -9,14 +9,28 @@ namespace Rays
9
9
  {
10
10
 
11
11
 
12
+ typedef void (*PreInitFun) ();
13
+
14
+
12
15
  namespace global
13
16
  {
14
17
 
15
- static bool initialized = false;
18
+ static bool initialized = false;
19
+
20
+ static PreInitFun pre_init_fun = NULL;
16
21
 
17
22
  }// global
18
23
 
19
24
 
25
+ void
26
+ Rays_set_pre_init_fun (PreInitFun fun)
27
+ {
28
+ global::pre_init_fun = fun;
29
+
30
+ if (fun && global::initialized)
31
+ fun();
32
+ }
33
+
20
34
  void
21
35
  init ()
22
36
  {
@@ -25,6 +39,9 @@ namespace Rays
25
39
 
26
40
  global::initialized = true;
27
41
 
42
+ if (global::pre_init_fun)
43
+ global::pre_init_fun();
44
+
28
45
  Renderer_init();
29
46
  }
30
47