gosu 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL/include/SDL.h +108 -14
  3. data/dependencies/SDL/include/SDL_assert.h +81 -50
  4. data/dependencies/SDL/include/SDL_atomic.h +135 -35
  5. data/dependencies/SDL/include/SDL_audio.h +960 -355
  6. data/dependencies/SDL/include/SDL_bits.h +11 -6
  7. data/dependencies/SDL/include/SDL_blendmode.h +91 -14
  8. data/dependencies/SDL/include/SDL_clipboard.h +30 -7
  9. data/dependencies/SDL/include/SDL_config.h +277 -27
  10. data/dependencies/SDL/include/SDL_config_android.h +13 -38
  11. data/dependencies/SDL/include/SDL_config_iphoneos.h +21 -62
  12. data/dependencies/SDL/include/SDL_config_macosx.h +23 -92
  13. data/dependencies/SDL/include/SDL_config_minimal.h +1 -4
  14. data/dependencies/SDL/include/SDL_config_pandora.h +15 -22
  15. data/dependencies/SDL/include/SDL_config_psp.h +16 -37
  16. data/dependencies/SDL/include/SDL_config_windows.h +28 -91
  17. data/dependencies/SDL/include/SDL_config_winrt.h +33 -61
  18. data/dependencies/SDL/include/SDL_config_wiz.h +28 -56
  19. data/dependencies/SDL/include/SDL_copying.h +1 -1
  20. data/dependencies/SDL/include/SDL_cpuinfo.h +331 -71
  21. data/dependencies/SDL/include/SDL_egl.h +906 -280
  22. data/dependencies/SDL/include/SDL_endian.h +101 -47
  23. data/dependencies/SDL/include/SDL_error.h +70 -19
  24. data/dependencies/SDL/include/SDL_events.h +387 -79
  25. data/dependencies/SDL/include/SDL_filesystem.h +73 -64
  26. data/dependencies/SDL/include/SDL_gamecontroller.h +585 -125
  27. data/dependencies/SDL/include/SDL_gesture.h +36 -6
  28. data/dependencies/SDL/include/SDL_haptic.h +304 -210
  29. data/dependencies/SDL/include/SDL_hidapi.h +451 -0
  30. data/dependencies/SDL/include/SDL_hints.h +1286 -897
  31. data/dependencies/SDL/include/SDL_joystick.h +577 -130
  32. data/dependencies/SDL/include/SDL_keyboard.h +162 -63
  33. data/dependencies/SDL/include/SDL_keycode.h +7 -5
  34. data/dependencies/SDL/include/SDL_loadso.h +42 -8
  35. data/dependencies/SDL/include/SDL_locale.h +34 -32
  36. data/dependencies/SDL/include/SDL_log.h +212 -19
  37. data/dependencies/SDL/include/SDL_main.h +72 -17
  38. data/dependencies/SDL/include/SDL_messagebox.h +70 -23
  39. data/dependencies/SDL/include/SDL_metal.h +27 -32
  40. data/dependencies/SDL/include/SDL_misc.h +19 -15
  41. data/dependencies/SDL/include/SDL_mouse.h +262 -110
  42. data/dependencies/SDL/include/SDL_mutex.h +286 -66
  43. data/dependencies/SDL/include/SDL_name.h +1 -1
  44. data/dependencies/SDL/include/SDL_opengl.h +1 -1
  45. data/dependencies/SDL/include/SDL_opengles.h +1 -1
  46. data/dependencies/SDL/include/SDL_opengles2.h +2 -2
  47. data/dependencies/SDL/include/SDL_pixels.h +199 -34
  48. data/dependencies/SDL/include/SDL_platform.h +39 -2
  49. data/dependencies/SDL/include/SDL_power.h +23 -10
  50. data/dependencies/SDL/include/SDL_quit.h +1 -1
  51. data/dependencies/SDL/include/SDL_rect.h +78 -28
  52. data/dependencies/SDL/include/SDL_render.h +1204 -472
  53. data/dependencies/SDL/include/SDL_revision.h +2 -2
  54. data/dependencies/SDL/include/SDL_rwops.h +605 -33
  55. data/dependencies/SDL/include/SDL_scancode.h +1 -1
  56. data/dependencies/SDL/include/SDL_sensor.h +76 -42
  57. data/dependencies/SDL/include/SDL_shape.h +38 -27
  58. data/dependencies/SDL/include/SDL_stdinc.h +96 -24
  59. data/dependencies/SDL/include/SDL_surface.h +571 -139
  60. data/dependencies/SDL/include/SDL_system.h +339 -101
  61. data/dependencies/SDL/include/SDL_syswm.h +50 -20
  62. data/dependencies/SDL/include/SDL_test.h +1 -1
  63. data/dependencies/SDL/include/SDL_test_assert.h +2 -2
  64. data/dependencies/SDL/include/SDL_test_common.h +23 -6
  65. data/dependencies/SDL/include/SDL_test_compare.h +1 -1
  66. data/dependencies/SDL/include/SDL_test_crc32.h +1 -1
  67. data/dependencies/SDL/include/SDL_test_font.h +3 -3
  68. data/dependencies/SDL/include/SDL_test_fuzzer.h +28 -26
  69. data/dependencies/SDL/include/SDL_test_harness.h +6 -6
  70. data/dependencies/SDL/include/SDL_test_images.h +1 -1
  71. data/dependencies/SDL/include/SDL_test_log.h +1 -1
  72. data/dependencies/SDL/include/SDL_test_md5.h +1 -1
  73. data/dependencies/SDL/include/SDL_test_memory.h +1 -1
  74. data/dependencies/SDL/include/SDL_test_random.h +2 -2
  75. data/dependencies/SDL/include/SDL_thread.h +226 -128
  76. data/dependencies/SDL/include/SDL_timer.h +129 -22
  77. data/dependencies/SDL/include/SDL_touch.h +48 -8
  78. data/dependencies/SDL/include/SDL_types.h +1 -1
  79. data/dependencies/SDL/include/SDL_version.h +72 -46
  80. data/dependencies/SDL/include/SDL_video.h +1266 -460
  81. data/dependencies/SDL/include/SDL_vulkan.h +100 -161
  82. data/dependencies/SDL/include/begin_code.h +22 -1
  83. data/dependencies/SDL/include/close_code.h +1 -1
  84. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  85. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  86. data/dependencies/SDL_sound/SDL_sound.c +83 -7
  87. data/dependencies/SDL_sound/SDL_sound.h +4 -4
  88. data/dependencies/SDL_sound/SDL_sound_aiff.c +9 -12
  89. data/dependencies/SDL_sound/SDL_sound_au.c +7 -7
  90. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +3 -3
  91. data/dependencies/SDL_sound/SDL_sound_flac.c +1 -1
  92. data/dependencies/SDL_sound/SDL_sound_internal.h +17 -10
  93. data/dependencies/SDL_sound/SDL_sound_modplug.c +25 -27
  94. data/dependencies/SDL_sound/SDL_sound_mp3.c +5 -17
  95. data/dependencies/SDL_sound/SDL_sound_raw.c +11 -11
  96. data/dependencies/SDL_sound/SDL_sound_shn.c +8 -7
  97. data/dependencies/SDL_sound/SDL_sound_voc.c +6 -4
  98. data/dependencies/SDL_sound/SDL_sound_vorbis.c +6 -11
  99. data/dependencies/SDL_sound/SDL_sound_wav.c +35 -29
  100. data/dependencies/SDL_sound/dr_flac.h +618 -220
  101. data/dependencies/SDL_sound/dr_mp3.h +263 -94
  102. data/dependencies/SDL_sound/libmodplug/fastmix.c +58 -64
  103. data/dependencies/SDL_sound/libmodplug/libmodplug.h +25 -103
  104. data/dependencies/SDL_sound/libmodplug/load_669.c +14 -17
  105. data/dependencies/SDL_sound/libmodplug/load_amf.c +11 -7
  106. data/dependencies/SDL_sound/libmodplug/load_ams.c +65 -22
  107. data/dependencies/SDL_sound/libmodplug/load_dbm.c +8 -4
  108. data/dependencies/SDL_sound/libmodplug/load_dmf.c +55 -25
  109. data/dependencies/SDL_sound/libmodplug/load_far.c +9 -13
  110. data/dependencies/SDL_sound/libmodplug/load_gdm.c +448 -0
  111. data/dependencies/SDL_sound/libmodplug/load_it.c +45 -49
  112. data/dependencies/SDL_sound/libmodplug/load_mdl.c +80 -53
  113. data/dependencies/SDL_sound/libmodplug/load_med.c +20 -12
  114. data/dependencies/SDL_sound/libmodplug/load_mod.c +40 -15
  115. data/dependencies/SDL_sound/libmodplug/load_mt2.c +29 -17
  116. data/dependencies/SDL_sound/libmodplug/load_okt.c +12 -8
  117. data/dependencies/SDL_sound/libmodplug/load_psm.c +101 -78
  118. data/dependencies/SDL_sound/libmodplug/load_ptm.c +18 -17
  119. data/dependencies/SDL_sound/libmodplug/load_s3m.c +9 -7
  120. data/dependencies/SDL_sound/libmodplug/load_stm.c +3 -2
  121. data/dependencies/SDL_sound/libmodplug/load_ult.c +2 -2
  122. data/dependencies/SDL_sound/libmodplug/load_umx.c +315 -35
  123. data/dependencies/SDL_sound/libmodplug/load_xm.c +25 -21
  124. data/dependencies/SDL_sound/libmodplug/mmcmp.c +295 -149
  125. data/dependencies/SDL_sound/libmodplug/modplug.c +7 -123
  126. data/dependencies/SDL_sound/libmodplug/modplug.h +32 -29
  127. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +0 -1
  128. data/dependencies/SDL_sound/libmodplug/snd_flt.c +2 -2
  129. data/dependencies/SDL_sound/libmodplug/snd_fx.c +24 -18
  130. data/dependencies/SDL_sound/libmodplug/sndfile.c +55 -156
  131. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -12
  132. data/dependencies/SDL_sound/libmodplug/tables.h +10 -15
  133. data/dependencies/SDL_sound/stb_vorbis.h +508 -325
  134. data/dependencies/{al_soft → mojoAL}/AL/al.h +38 -30
  135. data/dependencies/{al_soft → mojoAL}/AL/alc.h +27 -56
  136. data/dependencies/mojoAL/mojoal.c +4594 -0
  137. data/ext/gosu/extconf.rb +29 -30
  138. data/include/Gosu/Audio.hpp +70 -85
  139. data/include/Gosu/Color.hpp +19 -11
  140. data/include/Gosu/Font.hpp +40 -44
  141. data/include/Gosu/Graphics.hpp +58 -71
  142. data/include/Gosu/GraphicsBase.hpp +26 -33
  143. data/include/Gosu/Image.hpp +56 -62
  144. data/include/Gosu/ImageData.hpp +23 -27
  145. data/include/Gosu/Inspection.hpp +1 -4
  146. data/include/Gosu/TextInput.hpp +34 -40
  147. data/include/Gosu/Version.hpp +1 -1
  148. data/include/Gosu/Window.hpp +71 -70
  149. data/lib/SDL2.dll +0 -0
  150. data/lib/gosu/compat.rb +24 -37
  151. data/lib/gosu.rb +2 -2
  152. data/lib64/SDL2.dll +0 -0
  153. data/src/Audio.cpp +86 -86
  154. data/src/AudioFile.hpp +6 -6
  155. data/src/AudioFileAudioToolbox.cpp +1 -1
  156. data/src/AudioFileSDLSound.cpp +1 -1
  157. data/src/AudioImpl.hpp +5 -5
  158. data/src/BitmapIO.cpp +0 -20
  159. data/src/BlockAllocator.cpp +2 -1
  160. data/src/Channel.cpp +22 -20
  161. data/src/Color.cpp +12 -9
  162. data/src/EmptyImageData.hpp +15 -17
  163. data/src/FileUnix.cpp +1 -1
  164. data/src/FileWin.cpp +1 -1
  165. data/src/Font.cpp +48 -53
  166. data/src/Graphics.cpp +135 -143
  167. data/src/Image.cpp +41 -42
  168. data/src/Input.cpp +1 -1
  169. data/src/InputUIKit.cpp +1 -1
  170. data/src/LargeImageData.cpp +108 -101
  171. data/src/LargeImageData.hpp +17 -15
  172. data/src/Log.hpp +6 -6
  173. data/src/Macro.cpp +35 -37
  174. data/src/Macro.hpp +11 -11
  175. data/src/Math.cpp +8 -1
  176. data/src/Resolution.cpp +12 -7
  177. data/src/RubyGosu.cxx +5 -5
  178. data/src/TexChunk.cpp +50 -41
  179. data/src/TexChunk.hpp +22 -22
  180. data/src/Text.cpp +37 -37
  181. data/src/TextBuilder.cpp +60 -57
  182. data/src/TextBuilder.hpp +20 -20
  183. data/src/TextInput.cpp +127 -135
  184. data/src/TrueTypeFont.cpp +107 -107
  185. data/src/TrueTypeFont.hpp +39 -38
  186. data/src/TrueTypeFontApple.cpp +19 -22
  187. data/src/TrueTypeFontUnix.cpp +21 -26
  188. data/src/TrueTypeFontWin.cpp +30 -30
  189. data/src/Window.cpp +95 -86
  190. data/src/WindowUIKit.cpp +46 -49
  191. metadata +7 -17
  192. data/dependencies/SDL/include/SDL_config_os2.h +0 -188
  193. data/dependencies/SDL_sound/libmodplug/load_abc.c +0 -4725
  194. data/dependencies/SDL_sound/libmodplug/load_mid.c +0 -1405
  195. data/dependencies/SDL_sound/libmodplug/load_pat.c +0 -1143
  196. data/dependencies/SDL_sound/libmodplug/load_pat.h +0 -25
  197. data/dependencies/al_soft/AL/alext.h +0 -585
  198. data/dependencies/al_soft/AL/efx-creative.h +0 -3
  199. data/dependencies/al_soft/AL/efx-presets.h +0 -402
  200. data/dependencies/al_soft/AL/efx.h +0 -762
  201. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  202. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  203. data/lib/OpenAL32.dll +0 -0
  204. data/lib64/OpenAL32.dll +0 -0
data/src/TrueTypeFont.cpp CHANGED
@@ -1,89 +1,78 @@
1
1
  #include "TrueTypeFont.hpp"
2
2
  #include <Gosu/IO.hpp>
3
3
  #include <Gosu/Text.hpp>
4
+ #include <Gosu/Utility.hpp>
5
+ #include <algorithm>
6
+ #include <map>
4
7
  #include <stdexcept>
5
8
 
6
- // Disable comma warnings in stb headers.
7
- #ifdef __GNUC__
8
- #pragma GCC diagnostic push
9
- #pragma GCC diagnostic ignored "-Wcomma"
10
- #endif
11
-
12
9
  #define STB_TRUETYPE_IMPLEMENTATION
13
10
  #include <stb_truetype.h>
14
11
 
15
- #ifdef __GNUC__
16
- #pragma GCC diagnostic pop
17
- #endif
12
+ struct Gosu::TrueTypeFont::Impl : Gosu::Noncopyable
13
+ {
14
+ stbtt_fontinfo info{};
15
+ std::shared_ptr<TrueTypeFont> fallback;
18
16
 
19
- #include <algorithm>
20
- #include <map>
21
- using namespace std;
17
+ // The height of the ascent in internal font metrics (an arbitrary integer scale). The ascent is
18
+ // the part of the font above the baseline (which TrueType considers to be at y = 0).
19
+ int ascent{};
22
20
 
23
- struct Gosu::TrueTypeFont::Impl
24
- {
25
- stbtt_fontinfo info;
26
- shared_ptr<TrueTypeFont> fallback;
27
-
28
- // The ascent in internal font metrics (an arbitrary integer scale), that is, the part of the
29
- // font above the baseline, which TrueType considers to be at y = 0.
30
- int ascent;
31
-
32
21
  // Scaling factor from internal font metrics (an arbitrary integer scale) to a font with
33
22
  // height = 1px.
34
23
  double base_scale;
35
-
36
- Impl(const unsigned char* ttf_data, shared_ptr<TrueTypeFont> fallback)
37
- : fallback(move(fallback))
24
+
25
+ Impl(const unsigned char* ttf_data, std::shared_ptr<TrueTypeFont> fallback)
26
+ : fallback{move(fallback)}
38
27
  {
39
28
  auto offset = stbtt_GetFontOffsetForIndex(ttf_data, 0);
40
29
  int success = stbtt_InitFont(&info, ttf_data, offset);
41
- if (!success) throw runtime_error("Invalid TrueType font data");
42
-
30
+ if (!success) throw std::runtime_error{"Invalid TrueType font data"};
31
+
43
32
  // Calculate metrics.
44
- int descent, lineGap;
45
- stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);
46
- int height = ascent - descent + lineGap;
47
- base_scale = 1.0f / height;
33
+ int descent, line_gap;
34
+ stbtt_GetFontVMetrics(&info, &ascent, &descent, &line_gap);
35
+ int height = ascent - descent + line_gap;
36
+ base_scale = 1.0 / height;
48
37
  }
49
-
38
+
50
39
  // This method always measures text, and also draws it if (bitmap != nullptr).
51
- double draw_text(const u32string& text, bool is_end, double height,
52
- Bitmap* bitmap, double x, double y, Color c)
40
+ double draw_text(const std::u32string& text, bool is_end, double height, Bitmap* bitmap,
41
+ double x, double y, Color c)
53
42
  {
54
43
  if (text.empty()) return 0;
55
44
 
56
45
  // The 'x' parameter is used as the running cursor variable in this method.
57
-
46
+
58
47
  double scale = base_scale * height;
59
48
  int last_glyph = 0;
60
49
  int last_advance = 0;
61
50
 
62
- for (u32string::size_type index = 0; index < text.size(); ++index) {
51
+ for (std::size_t index = 0; index < text.size(); ++index) {
63
52
  auto codepoint = text[index];
64
53
  // Silently skip control characters, including the \r in Windows-style line breaks.
65
54
  if (codepoint < ' ') continue;
66
-
67
- int glyph = stbtt_FindGlyphIndex(&info, codepoint);
55
+
56
+ int glyph = stbtt_FindGlyphIndex(&info, static_cast<int>(codepoint));
68
57
  // Handle missing characters in this font...
69
58
  if (glyph == 0) {
70
59
  if (fallback) {
71
60
  // Missing characters often come in clusters, so build a substring of
72
61
  // codepoints that this font doesn't contain and then defer to the fallback
73
62
  // font.
74
- u32string fallback_string = string_of_missing_glyphs(text, index);
63
+ std::u32string fallback_string = string_of_missing_glyphs(text, index);
75
64
  index += fallback_string.length();
76
- x = fallback->pimpl->draw_text(fallback_string, index == text.size(), height,
77
- bitmap, x, y, c);
65
+ x = fallback->m_impl->draw_text(fallback_string, index == text.size(), height,
66
+ bitmap, x, y, c);
78
67
  last_glyph = 0;
79
68
  }
80
69
  continue;
81
70
  }
82
-
71
+
83
72
  if (last_glyph) {
84
73
  x += stbtt_GetGlyphKernAdvance(&info, last_glyph, glyph) * scale;
85
74
  }
86
-
75
+
87
76
  // Now finally draw the glyph (if a bitmap was passed).
88
77
  if (bitmap) {
89
78
  // Do not take lsb into account when positioning the glyph: It seems to correspond
@@ -92,93 +81,103 @@ struct Gosu::TrueTypeFont::Impl
92
81
  // Ref: https://github.com/nothings/stb/issues/281#issuecomment-361264014
93
82
  draw_glyph(*bitmap, x, y, c, glyph, scale);
94
83
  }
95
-
84
+
96
85
  int advance;
97
86
  stbtt_GetGlyphHMetrics(&info, glyph, &advance, nullptr);
98
-
87
+
99
88
  x += advance * scale;
100
89
  last_glyph = glyph;
101
90
  last_advance = advance;
102
91
  }
103
-
92
+
104
93
  // If this is the end of the string, we need to take another look at the last glyph to avoid
105
94
  // cutting off some pixels that extend to the right of the character.
106
95
  if (is_end && last_glyph) {
107
96
  int ix = static_cast<int>(x);
97
+ float shift_x = static_cast<float>(x - ix);
98
+ float fscale = static_cast<float>(scale);
108
99
  int last_xoff, last_width;
109
100
  // TODO: Don't allocate a buffer just to get metrics!
110
- free(stbtt_GetGlyphBitmapSubpixel(&info, scale, scale, x - ix, 0, last_glyph,
111
- &last_width, nullptr, &last_xoff, nullptr));
101
+ std::shared_ptr<unsigned char> unused_data{
102
+ stbtt_GetGlyphBitmapSubpixel(&info, fscale, fscale, shift_x, 0, last_glyph,
103
+ &last_width, nullptr, &last_xoff, nullptr),
104
+ std::free};
112
105
  // Move the cursor to the right if pixels have been touched by draw_glyph that are
113
106
  // to the right of the current cursor.
114
107
  // If the last character extends to the right of the cursor, then this prevents the
115
108
  // rightmost pixels from being truncated.
116
109
  // If the last character was whitespace, then last_width will be 0 (no pixel data)
117
110
  // and the cursor is what counts.
118
- x = max<double>(x, x - last_advance * scale + last_xoff + last_width);
111
+ x = std::max<double>(x, x - last_advance * scale + last_xoff + last_width);
119
112
  }
120
-
113
+
121
114
  // Never return a negative value from this method because it is used to determine bitmap
122
115
  // dimensions.
123
- return max<double>(0, x);
116
+ return std::max<double>(0, x);
124
117
  }
125
-
126
- u32string string_of_missing_glyphs(const u32string& text, u32string::size_type from_index)
118
+
119
+ std::u32string string_of_missing_glyphs(const std::u32string& text, std::size_t from_index)
127
120
  {
128
- u32string result;
129
-
130
- for (u32string::size_type index = from_index; index < text.size(); ++index) {
121
+ std::u32string result;
122
+
123
+ for (std::size_t index = from_index; index < text.size(); ++index) {
131
124
  auto codepoint = text[index];
132
125
  // Stop as soon as a glyph (except control characters) is available in the current font.
133
- if (codepoint >= ' ' && stbtt_FindGlyphIndex(&info, codepoint) != 0) break;
134
-
126
+ if (codepoint >= ' ' && stbtt_FindGlyphIndex(&info, static_cast<int>(codepoint)) != 0) {
127
+ break;
128
+ }
129
+
135
130
  result.push_back(codepoint);
136
131
  }
137
-
132
+
138
133
  return result;
139
134
  }
140
-
135
+
141
136
  void draw_glyph(Bitmap& bitmap, double fx, double fy, Color c, int glyph, double scale)
142
137
  {
143
138
  int x = static_cast<int>(fx);
144
139
  int y = static_cast<int>(fy);
140
+ float fscale = static_cast<float>(scale);
141
+ float shift_x = static_cast<float>(fx - x);
142
+ float shift_y = static_cast<float>(fy - y);
143
+
145
144
  int w, h, xoff, yoff;
146
-
147
145
  // As an optimization, this method/class could try to re-use a buffer for rasterization
148
146
  // instead of having stb_truetype allocate a fresh one for each draw_glyph call.
149
- unsigned char* pixels = stbtt_GetGlyphBitmapSubpixel(&info, scale, scale, fx - x, fy - y,
150
- glyph, &w, &h, &xoff, &yoff);
151
-
152
- blend_into_bitmap(bitmap, pixels, x + xoff, y + ascent * scale + yoff, w, h, c);
153
-
154
- free(pixels);
147
+ std::shared_ptr<unsigned char> pixels{stbtt_GetGlyphBitmapSubpixel(&info, fscale, fscale,
148
+ shift_x, shift_y, glyph,
149
+ &w, &h, &xoff, &yoff),
150
+ std::free};
151
+
152
+ int target_y = static_cast<int>(y + ascent * scale + yoff);
153
+ blend_into_bitmap(bitmap, pixels.get(), x + xoff, target_y, w, h, c);
155
154
  }
156
-
157
- void blend_into_bitmap(Bitmap& bitmap, const unsigned char* pixels, int x, int y, int w, int h,
158
- Color c)
155
+
156
+ static void blend_into_bitmap(Bitmap& bitmap, const unsigned char* pixels, //
157
+ int x, int y, int w, int h, Color c)
159
158
  {
160
159
  int stride = w;
161
-
160
+
162
161
  // Instead of transferring all pixels in the range [0; w) x [0; h) into the bitmap, clip
163
162
  // these values because Bitmap::blend_pixel does not perform bounds checking.
164
-
163
+
165
164
  int src_x = 0;
166
165
  if (x < 0) {
167
166
  src_x -= x;
168
167
  w += x;
169
168
  x = 0;
170
169
  }
171
-
170
+
172
171
  int src_y = 0;
173
172
  if (y < 0) {
174
173
  src_y -= y;
175
174
  h += y;
176
175
  y = 0;
177
176
  }
178
-
179
- w = min<int>(w, bitmap.width() - x);
180
- h = min<int>(h, bitmap.height() - y);
181
-
177
+
178
+ w = std::min<int>(w, bitmap.width() - x);
179
+ h = std::min<int>(h, bitmap.height() - y);
180
+
182
181
  for (int rel_y = 0; rel_y < h; ++rel_y) {
183
182
  for (int rel_x = 0; rel_x < w; ++rel_x) {
184
183
  int pixel = pixels[(src_y + rel_y) * stride + src_x + rel_x];
@@ -189,65 +188,67 @@ struct Gosu::TrueTypeFont::Impl
189
188
  }
190
189
  };
191
190
 
192
- Gosu::TrueTypeFont::TrueTypeFont(const unsigned char* ttf_data, shared_ptr<TrueTypeFont> fallback)
193
- : pimpl(new Impl(ttf_data, fallback))
191
+ Gosu::TrueTypeFont::TrueTypeFont(const unsigned char* ttf_data,
192
+ std::shared_ptr<TrueTypeFont> fallback)
193
+ : m_impl{new Impl(ttf_data, std::move(fallback))}
194
194
  {
195
195
  }
196
196
 
197
- double Gosu::TrueTypeFont::draw_text(const u32string &text, double height,
198
- Bitmap *bitmap, double x, double y, Color c)
197
+ double Gosu::TrueTypeFont::draw_text(const std::u32string& text, double height, Bitmap* bitmap,
198
+ double x, double y, Color c)
199
199
  {
200
- return pimpl->draw_text(text, true, height, bitmap, x, y, c);
200
+ return m_impl->draw_text(text, true, height, bitmap, x, y, c);
201
201
  }
202
202
 
203
- bool Gosu::TrueTypeFont::matches(const unsigned char* ttf_data,
204
- const string& font_name, unsigned font_flags)
203
+ bool Gosu::TrueTypeFont::matches(const unsigned char* ttf_data, const std::string& font_name,
204
+ unsigned font_flags)
205
205
  {
206
206
  // Gosu::FontFlags uses the same values as the STBTT_ macros, except for this one.
207
- int flags = (font_flags == 0 ? STBTT_MACSTYLE_NONE : font_flags);
207
+ int flags = (font_flags == 0 ? STBTT_MACSTYLE_NONE : static_cast<int>(font_flags));
208
208
 
209
209
  return stbtt_FindMatchingFont(ttf_data, font_name.c_str(), flags) >= 0 ||
210
210
  stbtt_FindMatchingFont(ttf_data, font_name.c_str(), STBTT_MACSTYLE_DONTCARE) >= 0;
211
211
  }
212
212
 
213
- static Gosu::TrueTypeFont& font_with_stack(vector<const unsigned char*> ttf_stack)
213
+ static Gosu::TrueTypeFont& font_with_stack(std::vector<const unsigned char*> ttf_stack)
214
214
  {
215
215
  // TODO: Make this cache thread-safe.
216
- static map<const unsigned char*, shared_ptr<Gosu::TrueTypeFont>> cache_by_data;
217
-
216
+ static std::map<const unsigned char*, std::shared_ptr<Gosu::TrueTypeFont>> cache_by_data;
217
+
218
218
  // Filter out any fonts that could not be found, as well as duplicates.
219
219
  auto end = unique(ttf_stack.begin(), ttf_stack.end());
220
220
  end = remove(ttf_stack.begin(), end, nullptr);
221
221
  ttf_stack.erase(end, ttf_stack.end());
222
-
222
+
223
223
  // This cannot happen because ttf_stack contains ttf_fallback_data(), which never returns null.
224
- if (ttf_stack.empty()) throw logic_error("Empty font stack");
225
-
226
- shared_ptr<Gosu::TrueTypeFont> head_of_stack = nullptr;
224
+ if (ttf_stack.empty()) throw std::logic_error{"Empty font stack"};
225
+
226
+ std::shared_ptr<Gosu::TrueTypeFont> head_of_stack = nullptr;
227
227
  for (const unsigned char* ttf_data : ttf_stack) {
228
228
  auto& font_ptr = cache_by_data[ttf_data];
229
229
  if (!font_ptr) {
230
- font_ptr = make_shared<Gosu::TrueTypeFont>(ttf_data, head_of_stack);
230
+ font_ptr = std::make_shared<Gosu::TrueTypeFont>(ttf_data, head_of_stack);
231
231
  }
232
232
  head_of_stack = font_ptr;
233
233
  }
234
234
  return *head_of_stack;
235
235
  }
236
236
 
237
- Gosu::TrueTypeFont& Gosu::font_by_name(const string& font_name, unsigned font_flags)
237
+ Gosu::TrueTypeFont& Gosu::font_by_name(const std::string& font_name, unsigned font_flags)
238
238
  {
239
239
  // TODO: Make this cache thread-safe.
240
- static map<pair<string, unsigned>, TrueTypeFont*> cache_by_name_and_flags;
241
-
240
+ static std::map<std::pair<std::string, unsigned>, TrueTypeFont*> cache_by_name_and_flags;
241
+
242
242
  auto& font_ptr = cache_by_name_and_flags[make_pair(font_name, font_flags)];
243
243
  if (!font_ptr) {
244
- // Build a stack from worst-case fallback to the desired font.
245
- vector<const unsigned char*> ttf_stack;
244
+ // Build a stack of TTF data in order of preference, where the fallback font is at the
245
+ // front, and the desired font with all the right font flags (most preferable) at the back.
246
+ std::vector<const unsigned char*> ttf_stack;
246
247
  ttf_stack.push_back(ttf_fallback_data());
247
248
  ttf_stack.push_back(ttf_data_by_name(default_font_name(), 0));
248
249
  if (font_flags != 0) ttf_stack.push_back(ttf_data_by_name(default_font_name(), font_flags));
249
-
250
- if (font_name.find_first_of("./\\") != string::npos) {
250
+
251
+ if (font_name.find_first_of("./\\") != std::string::npos) {
251
252
  // A filename? Load it and add it to the stack.
252
253
  ttf_stack.push_back(ttf_data_from_file(font_name));
253
254
  }
@@ -256,25 +257,24 @@ Gosu::TrueTypeFont& Gosu::font_by_name(const string& font_name, unsigned font_fl
256
257
  ttf_stack.push_back(ttf_data_by_name(font_name, 0));
257
258
  if (font_flags != 0) ttf_stack.push_back(ttf_data_by_name(font_name, font_flags));
258
259
  }
259
-
260
+
260
261
  font_ptr = &font_with_stack(move(ttf_stack));
261
262
  }
262
-
263
+
263
264
  return *font_ptr;
264
265
  }
265
266
 
266
- const unsigned char* Gosu::ttf_data_from_file(const string& filename)
267
+ const unsigned char* Gosu::ttf_data_from_file(const std::string& filename)
267
268
  {
268
269
  // TODO: Make this cache thread-safe.
269
- static map<string, shared_ptr<Buffer>> ttf_file_cache;
270
-
270
+ static std::map<std::string, std::shared_ptr<Buffer>> ttf_file_cache;
271
+
271
272
  auto& buffer_ptr = ttf_file_cache[filename];
272
273
  if (!buffer_ptr) {
273
- buffer_ptr = make_shared<Buffer>();
274
+ buffer_ptr = std::make_shared<Buffer>();
274
275
  try {
275
276
  load_file(*buffer_ptr, filename);
276
- }
277
- catch (...) {
277
+ } catch (...) {
278
278
  // Prevent partially loaded files from getting stuck in the cache.
279
279
  buffer_ptr = nullptr;
280
280
  throw;
data/src/TrueTypeFont.hpp CHANGED
@@ -12,51 +12,52 @@ namespace Gosu
12
12
  class TrueTypeFont
13
13
  {
14
14
  struct Impl;
15
- std::shared_ptr<Impl> pimpl;
16
-
15
+ std::shared_ptr<Impl> m_impl;
16
+
17
17
  public:
18
- //! The caller must ensure that the ttf_data pointer will remain valid indefinitely.
18
+ /// The caller must ensure that the ttf_data pointer will outlive this object.
19
19
  TrueTypeFont(const unsigned char* ttf_data, std::shared_ptr<TrueTypeFont> fallback);
20
-
21
- //! Returns the right edge of a string when rendered onto a bitmap at the given position,
22
- //! and with the given height.
23
- //! If (bitmap != nullptr), the text is also rendered onto the bitmap.
24
- double draw_text(const std::u32string& text, double height,
20
+
21
+ /// Returns the right edge of a string when rendered onto a bitmap at the given position,
22
+ /// and with the given height.
23
+ /// If (bitmap != nullptr), the text is also rendered onto the bitmap.
24
+ double draw_text(const std::u32string& text, double height, //
25
25
  Bitmap* bitmap, double x, double y, Color c);
26
-
27
- //! Returns true if the supplied buffer seems to be a font of the given name.
28
- static bool matches(const unsigned char* ttf_data,
26
+
27
+ /// Returns true if the supplied buffer seems to be a font of the given name.
28
+ static bool matches(const unsigned char* ttf_data, //
29
29
  const std::string& font_name, unsigned font_flags);
30
30
  };
31
-
31
+
32
32
  TrueTypeFont& font_by_name(const std::string& font_name, unsigned font_flags);
33
-
34
- //! Loads the contents of a file into memory and returns a pointer to it.
35
- //! The pointer is guaranteed to be valid indefinitely.
36
- //! In case of failure, this method must not return nullptr, but raise an exception.
37
- //! Note that this method does not accept any font flags, and so it will always load the first
38
- //! font in a TTC font collection.
39
- //!
40
- //! This function does not yet support Gosu::FontFlags, and consequently, custom TTF fonts do
41
- //! not support markup or bold/italic text right now.
42
- //!
43
- //! Options for the future:
44
- //! 1. Use stbtt_FindMatchingFont. This will only work for TTC font collections, and we will
45
- //! have to patch stb_truetype to look for fonts only based on `int flags`, while ignoring
46
- //! the name of fonts inside a bundle (who wants to deal with strings, anyway).
47
- //! 2. Maybe Gosu should accept filename patterns like "LibreBaskerville-*.ttf" as the font
48
- //! name and then replace the * with "Regular", "Bold", "Italic" etc.?
49
- //! 3. As a last resort, Gosu could implement faux bold and faux italics. I think faux
50
- //! underlines are a must anyway, since no font provides a dedicated TTF file for that.
51
- //! These options are not mutually exclusive.
33
+
34
+ /// Loads the contents of a file into memory and returns a pointer to it.
35
+ /// The pointer is guaranteed to be valid indefinitely.
36
+ /// In case of failure, this method must not return nullptr, but raise an exception.
37
+ /// Note that this method does not accept any font flags, and so it will always load the first
38
+ /// font in a TTC font collection.
39
+ ///
40
+ /// This function does not yet support Gosu::FontFlags, and consequently, custom TTF fonts do
41
+ /// not support markup or bold/italic text right now.
42
+ ///
43
+ /// Options for the future:
44
+ /// 1. Use stbtt_FindMatchingFont. This will only work for TTC font collections, and we will
45
+ /// have to patch stb_truetype to look for fonts only based on `int flags`, while ignoring
46
+ /// the name of fonts inside a bundle (because we don't want to deal with strings).
47
+ /// 2. Maybe Gosu should accept filename patterns like "LibreBaskerville-*.ttf" as the font
48
+ /// name and then replace the * with "Regular", "Bold", "Italic" etc.?
49
+ /// 3. As a last resort, Gosu could implement faux bold and faux italics. I think faux
50
+ /// underlines are a must anyway, since no font provides a dedicated TTF file for that.
51
+ /// These options are not mutually exclusive.
52
52
  const unsigned char* ttf_data_from_file(const std::string& filename);
53
-
54
- //! This method loads a TODO
55
- //! This method has a different implementation on each platform.
56
- //! In case of failure, this method returns nullptr.
53
+
54
+ /// This method loads a system font with the given flags.
55
+ /// This method has a different implementation on each platform.
56
+ /// In case of failure, this method returns nullptr.
57
57
  const unsigned char* ttf_data_by_name(const std::string& font_name, unsigned font_flags);
58
-
59
- //! This method has a different implementation on each platform.
60
- //! In case of failure, this method must not return nullptr, but raise an exception.
58
+
59
+ /// Returns the TTF data of a font that supports as many glyphs as possible.
60
+ /// This method has a different implementation on each platform.
61
+ /// In case of failure, this method must not return nullptr, but raise an exception.
61
62
  const unsigned char* ttf_fallback_data();
62
63
  }
@@ -1,39 +1,35 @@
1
1
  #include <Gosu/Platform.hpp>
2
2
  #if defined(GOSU_IS_MAC)
3
3
 
4
- #include "TrueTypeFont.hpp"
5
- #include "Log.hpp"
6
-
7
4
  #include <Gosu/Text.hpp>
8
-
9
- #import <CoreText/CoreText.h>
10
- #import <Foundation/Foundation.h>
11
-
5
+ #include "Log.hpp"
6
+ #include "TrueTypeFont.hpp"
7
+ #include <CoreText/CoreText.h>
8
+ #include <Foundation/Foundation.h>
12
9
  #include <map>
13
- using namespace std;
14
10
 
15
- const unsigned char* Gosu::ttf_data_by_name(const string& font_name, unsigned font_flags)
11
+ const unsigned char* Gosu::ttf_data_by_name(const std::string& font_name, unsigned font_flags)
16
12
  {
17
13
  // TODO: Make this cache thread-safe.
18
- static map<pair<string, unsigned>, const unsigned char*> ttf_file_cache;
19
-
14
+ static std::map<std::pair<std::string, unsigned>, const unsigned char*> ttf_file_cache;
15
+
20
16
  auto& ttf_ptr = ttf_file_cache[make_pair(font_name, font_flags)];
21
17
  if (ttf_ptr) return ttf_ptr;
22
18
 
23
19
  log("Trying to find a font named '%s', flags=%x", font_name.c_str(), font_flags);
24
20
 
25
21
  unsigned symbolic_traits = 0;
26
- if (font_flags & Gosu::FF_BOLD) symbolic_traits |= kCTFontTraitBold;
22
+ if (font_flags & Gosu::FF_BOLD) symbolic_traits |= kCTFontTraitBold;
27
23
  if (font_flags & Gosu::FF_ITALIC) symbolic_traits |= kCTFontTraitItalic;
28
24
 
29
- NSDictionary *attributes = @{
25
+ NSDictionary* attributes = @{
30
26
  ((__bridge id) kCTFontNameAttribute): [NSString stringWithUTF8String:font_name.c_str()],
31
- ((__bridge id) kCTFontTraitsAttribute): @{
32
- ((__bridge id) kCTFontSymbolicTrait): @(symbolic_traits)
33
- }
27
+ ((__bridge id) kCTFontTraitsAttribute):
28
+ @{((__bridge id) kCTFontSymbolicTrait): @(symbolic_traits)}
34
29
  };
35
30
  CTFontDescriptorRef descriptor =
36
- CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef) attributes);
31
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
32
+ CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef) attributes);
37
33
 
38
34
  if (descriptor) {
39
35
  CTFontRef font = CTFontCreateWithFontDescriptorAndOptions(descriptor, 20, nullptr, 0);
@@ -42,11 +38,12 @@ const unsigned char* Gosu::ttf_data_by_name(const string& font_name, unsigned fo
42
38
  // For some reason the initial call to CTFontCreateWithFontDescriptorAndOptions does
43
39
  // not respect the requested font traits. Explicitly requesting the traits again
44
40
  // seems to help. Tested on macOS 11.2.2.
45
- CTFontRef correct_font = CTFontCreateCopyWithSymbolicTraits(font, 0.0, nullptr, symbolic_traits, symbolic_traits);
41
+ CTFontRef correct_font = CTFontCreateCopyWithSymbolicTraits(
42
+ font, 0.0, nullptr, symbolic_traits, symbolic_traits);
46
43
  CFRelease(font);
47
44
  font = correct_font;
48
45
  }
49
- NSURL *url = CFBridgingRelease(CTFontCopyAttribute(font, kCTFontURLAttribute));
46
+ NSURL* url = CFBridgingRelease(CTFontCopyAttribute(font, kCTFontURLAttribute));
50
47
  if (url && url.fileSystemRepresentation) {
51
48
  log("Loading file '%s'", url.fileSystemRepresentation);
52
49
  ttf_ptr = ttf_data_from_file(url.fileSystemRepresentation);
@@ -55,7 +52,7 @@ const unsigned char* Gosu::ttf_data_by_name(const string& font_name, unsigned fo
55
52
  }
56
53
  CFRelease(descriptor);
57
54
  }
58
-
55
+
59
56
  return ttf_ptr;
60
57
  }
61
58
 
@@ -64,11 +61,11 @@ const unsigned char* Gosu::ttf_fallback_data()
64
61
  // Prefer Arial Unicode MS as a fallback because it covers a lot of Unicode.
65
62
  static const unsigned char* arial_unicode = ttf_data_by_name("Arial Unicode MS", 0);
66
63
  if (arial_unicode) return arial_unicode;
67
-
64
+
68
65
  return ttf_data_from_file("/Library/Fonts/Arial.ttf");
69
66
  }
70
67
 
71
- string Gosu::default_font_name()
68
+ std::string Gosu::default_font_name()
72
69
  {
73
70
  return "Arial";
74
71
  }