Round190ulCheck.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Round 190ul (issue #379, stabbing-emoji): text that doesn't fit is cut with "…", never squeezed. JUCE's
  2. // drawFittedText / Label / ComboBox / TextButton first narrowed the glyphs (horizontal scale down to 0.7, or the value a
  3. // call passed) and only then cut the text; the snap box's "Line (follows zoom)" in the Playlist read squashed.
  4. // Included in the app class of Main.cpp; --round190ultest <report> [<shot prefix>] and --selftest "round190ul".
  5. // 1. JUCE's default minimum scale is 1 (theme::LookAndFeel sets it), the text cache helper ignores a smaller one;
  6. // 2. a recording renderer sees every glyph run each widget draws: the issue's snap box (Playlist's SnapBox, 128 px,
  7. // "Line (follows zoom)"), a plain ComboBox, a TextButton (with and without an icon), a Label, a ToggleButton, a
  8. // popup menu row, a tab button, a translated menu bar title on the tight level and the value-fitting helper, at
  9. // interface sizes 100 / 150 / 200 % and text sizes 100 / 150 / 200 %: every run at horizontal scale 1, the text cut
  10. // ends in "…" and keeps its start ("Line (follows zo…");
  11. // 3. the recorder itself: a drawFittedText asked for 0.7 still shows up as squeezed (the check can fail).
  12. // Shots (<prefix>-*.png): each widget at 100 / 150 / 200 %, and the Playlist's toolbar at the issue's 1280 x 832.
  13. struct TextFit190ulRecorder final : juce::LowLevelGraphicsSoftwareRenderer
  14. {
  15. struct Run
  16. {
  17. juce::Font font;
  18. std::vector<std::uint16_t> glyphs;
  19. };
  20. std::vector<Run> runs;
  21. explicit TextFit190ulRecorder (const juce::Image& image) : juce::LowLevelGraphicsSoftwareRenderer (image) {}
  22. void drawGlyphs (juce::Span<const std::uint16_t> glyphs, juce::Span<const juce::Point<float>> positions, const juce::AffineTransform& t) override
  23. {
  24. runs.push_back ({ getFont(), std::vector<std::uint16_t> (glyphs.begin(), glyphs.end()) });
  25. juce::LowLevelGraphicsSoftwareRenderer::drawGlyphs (glyphs, positions, t);
  26. }
  27. };
  28. // What a recorded paint showed of `text`: its runs' smallest horizontal scale, whether it is cut, ends in "…" and keeps
  29. // the text's start, and the visible text ("Line (follows zo…").
  30. struct TextFit190ulSeen
  31. {
  32. float minScale = 1.0f;
  33. bool drawn = false, cut = false, ellipsis = false, keepsStart = false;
  34. juce::String visible;
  35. };
  36. static TextFit190ulSeen textFit190ulSeen (const std::vector<TextFit190ulRecorder::Run>& runs, const juce::String& text)
  37. {
  38. TextFit190ulSeen seen;
  39. std::vector<std::uint16_t> drawn;
  40. juce::Font font (juce::FontOptions {});
  41. for (const auto& run : runs)
  42. {
  43. if (run.glyphs.empty())
  44. continue;
  45. if (! seen.drawn)
  46. font = run.font;
  47. seen.drawn = true;
  48. seen.minScale = juce::jmin (seen.minScale, run.font.getHorizontalScale());
  49. drawn.insert (drawn.end(), run.glyphs.begin(), run.glyphs.end());
  50. }
  51. if (! seen.drawn)
  52. return seen;
  53. // the text whole in the drawn font (at scale 1) and the ellipsis glyph, their glyph numbers read back through a
  54. // recorder (PositionedGlyph keeps its number private); whitespace left out on both sides
  55. const auto unsqueezed = font.withHorizontalScale (1.0f);
  56. const auto numbersOf = [] (const juce::GlyphArrangement& ga) {
  57. juce::Image image (juce::Image::ARGB, 4, 4, true, juce::SoftwareImageType());
  58. TextFit190ulRecorder rec (image);
  59. {
  60. juce::Graphics g (rec);
  61. ga.draw (g);
  62. }
  63. std::vector<std::uint16_t> out;
  64. for (const auto& run : rec.runs)
  65. out.insert (out.end(), run.glyphs.begin(), run.glyphs.end());
  66. return out;
  67. };
  68. juce::GlyphArrangement full, dots;
  69. full.addLineOfText (unsqueezed, text.trim(), 0.0f, 0.0f);
  70. dots.addLineOfText (unsqueezed, juce::String::charToString ((juce::juce_wchar) 0x2026), 0.0f, 0.0f);
  71. const auto fullNumbers = numbersOf (full), dotNumbers = numbersOf (dots);
  72. std::set<std::uint16_t> blanks;
  73. std::vector<std::uint16_t> whole;
  74. for (int i = 0; i < full.getNumGlyphs() && i < (int) fullNumbers.size(); ++i)
  75. {
  76. if (full.getGlyph (i).isWhitespace())
  77. blanks.insert (fullNumbers[(std::size_t) i]);
  78. else
  79. whole.push_back (fullNumbers[(std::size_t) i]);
  80. }
  81. drawn.erase (std::remove_if (drawn.begin(), drawn.end(), [&blanks] (std::uint16_t g) { return blanks.count (g) > 0; }), drawn.end());
  82. const auto dot = dotNumbers.size() == 1 ? dotNumbers.front() : (std::uint16_t) 0xffff;
  83. seen.ellipsis = ! drawn.empty() && drawn.back() == dot;
  84. seen.cut = drawn.size() != whole.size() || seen.ellipsis;
  85. const auto kept = seen.ellipsis ? drawn.size() - 1 : drawn.size();
  86. seen.keepsStart = kept <= whole.size();
  87. for (std::size_t i = 0; i < kept && seen.keepsStart; ++i)
  88. seen.keepsStart = drawn[i] == whole[i];
  89. // the visible text: the text up to the last kept glyph (with its spaces), then "…"
  90. if (seen.keepsStart)
  91. {
  92. int chars = 0, nonBlank = 0;
  93. const auto trimmed = text.trim();
  94. for (; chars < trimmed.length() && nonBlank < (int) kept; ++chars)
  95. nonBlank += juce::CharacterFunctions::isWhitespace (trimmed[chars]) ? 0 : 1;
  96. seen.visible = trimmed.substring (0, chars) + (seen.ellipsis ? juce::String::charToString ((juce::juce_wchar) 0x2026) : juce::String());
  97. }
  98. return seen;
  99. }
  100. juce::String round190ulCheck (juce::String& log, const juce::String& shots = {})
  101. {
  102. using namespace lynx;
  103. juce::String result = "OK";
  104. int failures = 0;
  105. const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) {
  106. log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " [" + detail + "]" : juce::String()) << "\n";
  107. if (! ok)
  108. {
  109. ++failures;
  110. if (result == "OK")
  111. result = "ERROR " + what + (detail.isNotEmpty() ? " " + detail : juce::String());
  112. }
  113. };
  114. const auto dots = juce::String::charToString ((juce::juce_wchar) 0x2026);
  115. const auto describe = [] (const TextFit190ulSeen& s) {
  116. return "scale " + juce::String (s.minScale, 3) + ", shows \"" + s.visible + "\"";
  117. };
  118. juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  119. const juce::ScopedValueSetter<bool> shown (ui::showingForShots(), true);
  120. // --- 1. the defaults ----------------------------------------------------------------------------------------------
  121. check ("JUCE's default minimum horizontal scale is 1 (no squeeze before the ellipsis)",
  122. juce::exactlyEqual (juce::Font::getDefaultMinimumHorizontalScaleFactor(), 1.0f),
  123. juce::String (juce::Font::getDefaultMinimumHorizontalScaleFactor(), 3));
  124. // paints `c` (or `paint`) at `scale` through the recorder; the picture is kept for the shots
  125. const auto record = [] (int w, int h, float scale, const std::function<void (juce::Graphics&)>& paint, juce::Image* keep = nullptr) {
  126. juce::Image image (juce::Image::ARGB, juce::jmax (1, juce::roundToInt ((float) w * scale)), juce::jmax (1, juce::roundToInt ((float) h * scale)), true,
  127. juce::SoftwareImageType());
  128. juce::Graphics (image).fillAll (theme::colour::bg1);
  129. TextFit190ulRecorder rec (image);
  130. {
  131. juce::Graphics g (rec);
  132. g.addTransform (juce::AffineTransform::scale (scale));
  133. paint (g);
  134. }
  135. if (keep != nullptr)
  136. *keep = image;
  137. return rec.runs;
  138. };
  139. const auto seenOf = [] (const std::vector<TextFit190ulRecorder::Run>& runs, const juce::String& text) { return textFit190ulSeen (runs, text); };
  140. // --- 3. the recorder sees a squeeze when one is drawn (else this check could not fail) ------------------------------
  141. {
  142. const juce::String text ("Line (follows zoom)");
  143. // a box 85 % of the text's width in this platform's face (Segoe UI on Windows fits 90 px whole)
  144. const auto boxWidth = juce::roundToInt (juce::GlyphArrangement::getStringWidth (theme::font (theme::type::body), text) * 0.85f);
  145. const auto runs = record (boxWidth, 24, 1.0f, [&] (juce::Graphics& g) {
  146. g.setFont (theme::font (theme::type::body));
  147. g.drawFittedText (text, juce::Rectangle<int> (0, 0, boxWidth, 24), juce::Justification::centredLeft, 1, 0.7f); // (the old way, on purpose)
  148. });
  149. const auto s = seenOf (runs, text);
  150. check ("the recorder: JUCE's drawFittedText asked for 0.7 draws squeezed glyphs (the check can tell)", s.drawn && s.minScale < 0.99f, describe (s));
  151. const auto cached = record (boxWidth, 24, 1.0f, [&] (juce::Graphics& g) {
  152. g.setFont (theme::font (theme::type::body));
  153. textcache::drawFittedText (g, text, juce::Rectangle<int> (0, 0, boxWidth, 24), juce::Justification::centredLeft, 1, 0.7f);
  154. });
  155. const auto c = seenOf (cached, text);
  156. check ("the text cache's drawFittedText never squeezes, even asked for 0.7", c.drawn && c.minScale > 0.999f && c.ellipsis && c.keepsStart, describe (c));
  157. }
  158. // --- 2. the widgets -----------------------------------------------------------------------------------------------
  159. const auto textBefore = theme::textSize();
  160. juce::StringArray saved;
  161. const auto shot = [&] (const juce::Image& image, const juce::String& name) {
  162. if (shots.isEmpty() || ! image.isValid())
  163. return;
  164. const auto file = juce::File (shots + "-" + name + ".png");
  165. file.deleteFile();
  166. if (auto out = file.createOutputStream())
  167. {
  168. juce::PNGImageFormat png;
  169. if (png.writeImageToStream (image, *out))
  170. saved.add (file.getFileName());
  171. }
  172. };
  173. for (const int textPercent : { 100, 150, 200 })
  174. {
  175. theme::setTextSize (textPercent);
  176. for (const int uiPercent : { 100, 150, 200 })
  177. {
  178. const auto scale = (float) uiPercent / 100.0f;
  179. const auto at = " (text " + juce::String (textPercent) + " %, interface " + juce::String (uiPercent) + " %)";
  180. const auto keepShot = textPercent == 100 || uiPercent == 100;
  181. const auto tag = "t" + juce::String (textPercent) + "-ui" + juce::String (uiPercent);
  182. // a widget painted whole (its children too: the combo's label)
  183. const auto widget = [&] (juce::Component& c, const juce::String& name, const juce::String& text, bool mustCut) {
  184. juce::Image image;
  185. const auto runs = record (c.getWidth(), c.getHeight(), scale, [&c] (juce::Graphics& g) { c.paintEntireComponent (g, false); }, &image);
  186. const auto s = seenOf (runs, text);
  187. check (name + ": drawn at scale 1" + at, s.drawn && s.minScale > 0.999f, describe (s));
  188. if (mustCut)
  189. check (name + ": cut with \"" + dots + "\", its start kept" + at, s.cut && s.ellipsis && s.keepsStart, describe (s));
  190. else
  191. check (name + ": whole, or cut with \"" + dots + "\" keeping its start" + at, s.keepsStart && (! s.cut || s.ellipsis), describe (s));
  192. if (keepShot)
  193. shot (image, name.toLowerCase().replaceCharacters (" ()", "---").retainCharacters ("abcdefghijklmnopqrstuvwxyz0123456789-") + "-" + tag);
  194. };
  195. // the issue's box: the Playlist's snap (magnet), 128 px, "Line (follows zoom)"
  196. {
  197. SnapBox snap (true);
  198. grid::fillEditorBox (snap);
  199. snap.setSelectedId (grid::screenOption + 2, juce::dontSendNotification);
  200. snap.setBounds (0, 0, 128, 24);
  201. const auto text = snap.getText();
  202. check ("the snap box shows \"Line (follows zoom)\"" + at, text == tr ("Line (follows zoom)"), text);
  203. widget (snap, "snap box", text, true);
  204. }
  205. {
  206. juce::ComboBox box;
  207. box.addItem ("Line (follows zoom)", 1);
  208. box.setSelectedId (1, juce::dontSendNotification);
  209. box.setBounds (0, 0, 110, 24);
  210. widget (box, "combo box", box.getText(), true);
  211. }
  212. {
  213. const juce::String text ("Render the selected clips to audio and replace them");
  214. juce::TextButton button (text);
  215. button.setBounds (0, 0, 120, 26);
  216. widget (button, "text button", text, true);
  217. button.getProperties().set (theme::LookAndFeel::iconProperty, (int) Icon::magnet);
  218. widget (button, "icon button", text, true);
  219. }
  220. {
  221. const juce::String text ("Line (follows zoom)");
  222. juce::Label label ({}, text);
  223. label.setFont (theme::font (theme::type::body));
  224. label.setBounds (0, 0, 80, 20);
  225. widget (label, "label", text, true);
  226. label.setMinimumHorizontalScale (0.5f); // (a label that still asks for a squeeze gets none)
  227. widget (label, "label asking 0.5", text, true);
  228. }
  229. {
  230. const juce::String text ("Snap the clip starts and their lengths to the grid of the editor that is open now");
  231. juce::ToggleButton toggle (text);
  232. toggle.setBounds (0, 0, 120, 22);
  233. widget (toggle, "tick box", text, true);
  234. }
  235. {
  236. const juce::String text ("Line (follows zoom) with a longer menu item name");
  237. juce::Image image;
  238. const auto runs = record (150, 24, scale, [&] (juce::Graphics& g) {
  239. lookAndFeel.drawPopupMenuItem (g, { 0, 0, 150, 24 }, false, true, false, false, false, text, {}, nullptr, nullptr);
  240. }, &image);
  241. const auto s = seenOf (runs, text);
  242. check ("menu row: drawn at scale 1, cut with \"" + dots + "\", its start kept" + at, s.drawn && s.minScale > 0.999f && s.ellipsis && s.keepsStart, describe (s));
  243. if (keepShot)
  244. shot (image, "menu-row-" + tag);
  245. }
  246. {
  247. const juce::String text ("Line (follows zoom)");
  248. juce::TabbedButtonBar bar (juce::TabbedButtonBar::TabsAtTop);
  249. bar.addTab (text, theme::colour::bg2, 0);
  250. bar.setBounds (0, 0, 90, 26);
  251. if (auto* tab = bar.getTabButton (0))
  252. {
  253. tab->setBounds (0, 0, 90, 26);
  254. widget (*tab, "tab", text, true);
  255. }
  256. }
  257. {
  258. // a translated menu on the tight level (round 190oo WG-11): smaller letters instead of narrower ones
  259. juce::MenuBarComponent menuBar;
  260. menuBar.getProperties().set ("lynxFlMenuBar", true);
  261. menuBar.getProperties().set ("lynxTightMenuBar", true);
  262. menuBar.getProperties().set ("lynxCondensedMenuBar", true);
  263. menuBar.getProperties().set ("lynxMenuBarScale", 0.9);
  264. const juce::String text ("Werkzeuge");
  265. const auto width = lookAndFeel.getMenuBarItemWidth (menuBar, 0, text);
  266. const auto runs = record (width, 24, scale, [&] (juce::Graphics& g) {
  267. lookAndFeel.drawMenuBarItem (g, width, 24, 0, text, false, false, false, menuBar);
  268. });
  269. const auto s = seenOf (runs, text);
  270. check ("menu bar title on the tight level: drawn at scale 1, whole" + at, s.drawn && s.minScale > 0.999f && ! s.cut, describe (s));
  271. }
  272. {
  273. // FL's caption fitting (plugin windows): the font shrinks to its smallest, then the text is cut
  274. const juce::String text ("Modulation envelope amount");
  275. const auto runs = record (60, 14, scale, [&] (juce::Graphics& g) {
  276. g.setColour (theme::colour::textPrimary);
  277. ui::drawFittingText (g, text, { 0.0f, 0.0f, 60.0f, 14.0f }, juce::Justification::centredLeft, theme::font (theme::type::small), 7.5f);
  278. });
  279. const auto s = seenOf (runs, text);
  280. check ("caption fitting (plugin windows): smaller font, then \"" + dots + "\", never narrower" + at, s.drawn && s.minScale > 0.999f && s.ellipsis && s.keepsStart,
  281. describe (s));
  282. }
  283. }
  284. }
  285. theme::setTextSize (textBefore);
  286. // --- the issue's view: the Playlist's toolbar at 1280 x 832, the snap on "Line (follows zoom)" --------------------
  287. #if JUCE_LINUX || JUCE_WINDOWS
  288. if (shots.isNotEmpty())
  289. {
  290. AudioEngine offline (false);
  291. MainComponent ui (offline, false);
  292. ui.setSize (1280, 832);
  293. ui.setVisible (true);
  294. const auto settle = [&offline] (int ms) {
  295. const auto end = juce::Time::getMillisecondCounter() + (juce::uint32) ms;
  296. do
  297. {
  298. offline.dispatchPendingMessages();
  299. for (int i = 0; i < 256 && juce::detail::dispatchNextMessageOnSystemQueue (true); ++i) {}
  300. juce::Thread::sleep (10);
  301. } while (juce::Time::getMillisecondCounter() < end);
  302. };
  303. settle (300);
  304. ui.selectTabForSnapshot (2);
  305. settle (300);
  306. auto& playlist = ui.getPlaylistForSnapshot();
  307. SnapBox* snap = nullptr;
  308. for (auto* child : playlist.getChildren())
  309. if (auto* s = dynamic_cast<SnapBox*> (child); s != nullptr && s->isVisible())
  310. snap = s;
  311. check ("the Playlist has its snap box", snap != nullptr);
  312. if (snap != nullptr)
  313. {
  314. snap->setSelectedId (grid::screenOption + 2, juce::dontSendNotification);
  315. settle (100);
  316. const auto strip = juce::Rectangle<int> (0, 0, juce::jmin (playlist.getWidth(), snap->getRight() + 120), snap->getBottom() + 4);
  317. const auto s = seenOf (record (snap->getWidth(), snap->getHeight(), 1.0f, [snap] (juce::Graphics& g) { snap->paintEntireComponent (g, false); }),
  318. snap->getText());
  319. check ("the Playlist's snap box at 1280 px: scale 1, \"" + s.visible + "\"", s.drawn && s.minScale > 0.999f && s.keepsStart && (! s.cut || s.ellipsis),
  320. describe (s));
  321. for (const auto sc : { 1.0f, 1.5f, 2.0f })
  322. shot (playlist.createComponentSnapshot (strip, true, sc), "playlist-toolbar-1280-" + juce::String (juce::roundToInt (sc * 100.0f)));
  323. }
  324. }
  325. #endif
  326. if (shots.isNotEmpty())
  327. log << "# shots: " << saved.joinIntoString (", ") << "\n";
  328. log << "# " << failures << " failures\n";
  329. return result;
  330. }