| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- // Round 190ul (issue #379, stabbing-emoji): text that doesn't fit is cut with "…", never squeezed. JUCE's
- // drawFittedText / Label / ComboBox / TextButton first narrowed the glyphs (horizontal scale down to 0.7, or the value a
- // call passed) and only then cut the text; the snap box's "Line (follows zoom)" in the Playlist read squashed.
- // Included in the app class of Main.cpp; --round190ultest <report> [<shot prefix>] and --selftest "round190ul".
- // 1. JUCE's default minimum scale is 1 (theme::LookAndFeel sets it), the text cache helper ignores a smaller one;
- // 2. a recording renderer sees every glyph run each widget draws: the issue's snap box (Playlist's SnapBox, 128 px,
- // "Line (follows zoom)"), a plain ComboBox, a TextButton (with and without an icon), a Label, a ToggleButton, a
- // popup menu row, a tab button, a translated menu bar title on the tight level and the value-fitting helper, at
- // interface sizes 100 / 150 / 200 % and text sizes 100 / 150 / 200 %: every run at horizontal scale 1, the text cut
- // ends in "…" and keeps its start ("Line (follows zo…");
- // 3. the recorder itself: a drawFittedText asked for 0.7 still shows up as squeezed (the check can fail).
- // Shots (<prefix>-*.png): each widget at 100 / 150 / 200 %, and the Playlist's toolbar at the issue's 1280 x 832.
- struct TextFit190ulRecorder final : juce::LowLevelGraphicsSoftwareRenderer
- {
- struct Run
- {
- juce::Font font;
- std::vector<std::uint16_t> glyphs;
- };
- std::vector<Run> runs;
- explicit TextFit190ulRecorder (const juce::Image& image) : juce::LowLevelGraphicsSoftwareRenderer (image) {}
- void drawGlyphs (juce::Span<const std::uint16_t> glyphs, juce::Span<const juce::Point<float>> positions, const juce::AffineTransform& t) override
- {
- runs.push_back ({ getFont(), std::vector<std::uint16_t> (glyphs.begin(), glyphs.end()) });
- juce::LowLevelGraphicsSoftwareRenderer::drawGlyphs (glyphs, positions, t);
- }
- };
- // What a recorded paint showed of `text`: its runs' smallest horizontal scale, whether it is cut, ends in "…" and keeps
- // the text's start, and the visible text ("Line (follows zo…").
- struct TextFit190ulSeen
- {
- float minScale = 1.0f;
- bool drawn = false, cut = false, ellipsis = false, keepsStart = false;
- juce::String visible;
- };
- static TextFit190ulSeen textFit190ulSeen (const std::vector<TextFit190ulRecorder::Run>& runs, const juce::String& text)
- {
- TextFit190ulSeen seen;
- std::vector<std::uint16_t> drawn;
- juce::Font font (juce::FontOptions {});
- for (const auto& run : runs)
- {
- if (run.glyphs.empty())
- continue;
- if (! seen.drawn)
- font = run.font;
- seen.drawn = true;
- seen.minScale = juce::jmin (seen.minScale, run.font.getHorizontalScale());
- drawn.insert (drawn.end(), run.glyphs.begin(), run.glyphs.end());
- }
- if (! seen.drawn)
- return seen;
- // the text whole in the drawn font (at scale 1) and the ellipsis glyph, their glyph numbers read back through a
- // recorder (PositionedGlyph keeps its number private); whitespace left out on both sides
- const auto unsqueezed = font.withHorizontalScale (1.0f);
- const auto numbersOf = [] (const juce::GlyphArrangement& ga) {
- juce::Image image (juce::Image::ARGB, 4, 4, true, juce::SoftwareImageType());
- TextFit190ulRecorder rec (image);
- {
- juce::Graphics g (rec);
- ga.draw (g);
- }
- std::vector<std::uint16_t> out;
- for (const auto& run : rec.runs)
- out.insert (out.end(), run.glyphs.begin(), run.glyphs.end());
- return out;
- };
- juce::GlyphArrangement full, dots;
- full.addLineOfText (unsqueezed, text.trim(), 0.0f, 0.0f);
- dots.addLineOfText (unsqueezed, juce::String::charToString ((juce::juce_wchar) 0x2026), 0.0f, 0.0f);
- const auto fullNumbers = numbersOf (full), dotNumbers = numbersOf (dots);
- std::set<std::uint16_t> blanks;
- std::vector<std::uint16_t> whole;
- for (int i = 0; i < full.getNumGlyphs() && i < (int) fullNumbers.size(); ++i)
- {
- if (full.getGlyph (i).isWhitespace())
- blanks.insert (fullNumbers[(std::size_t) i]);
- else
- whole.push_back (fullNumbers[(std::size_t) i]);
- }
- drawn.erase (std::remove_if (drawn.begin(), drawn.end(), [&blanks] (std::uint16_t g) { return blanks.count (g) > 0; }), drawn.end());
- const auto dot = dotNumbers.size() == 1 ? dotNumbers.front() : (std::uint16_t) 0xffff;
- seen.ellipsis = ! drawn.empty() && drawn.back() == dot;
- seen.cut = drawn.size() != whole.size() || seen.ellipsis;
- const auto kept = seen.ellipsis ? drawn.size() - 1 : drawn.size();
- seen.keepsStart = kept <= whole.size();
- for (std::size_t i = 0; i < kept && seen.keepsStart; ++i)
- seen.keepsStart = drawn[i] == whole[i];
- // the visible text: the text up to the last kept glyph (with its spaces), then "…"
- if (seen.keepsStart)
- {
- int chars = 0, nonBlank = 0;
- const auto trimmed = text.trim();
- for (; chars < trimmed.length() && nonBlank < (int) kept; ++chars)
- nonBlank += juce::CharacterFunctions::isWhitespace (trimmed[chars]) ? 0 : 1;
- seen.visible = trimmed.substring (0, chars) + (seen.ellipsis ? juce::String::charToString ((juce::juce_wchar) 0x2026) : juce::String());
- }
- return seen;
- }
- juce::String round190ulCheck (juce::String& log, const juce::String& shots = {})
- {
- using namespace lynx;
- juce::String result = "OK";
- int failures = 0;
- const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) {
- log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " [" + detail + "]" : juce::String()) << "\n";
- if (! ok)
- {
- ++failures;
- if (result == "OK")
- result = "ERROR " + what + (detail.isNotEmpty() ? " " + detail : juce::String());
- }
- };
- const auto dots = juce::String::charToString ((juce::juce_wchar) 0x2026);
- const auto describe = [] (const TextFit190ulSeen& s) {
- return "scale " + juce::String (s.minScale, 3) + ", shows \"" + s.visible + "\"";
- };
- juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
- const juce::ScopedValueSetter<bool> shown (ui::showingForShots(), true);
- // --- 1. the defaults ----------------------------------------------------------------------------------------------
- check ("JUCE's default minimum horizontal scale is 1 (no squeeze before the ellipsis)",
- juce::exactlyEqual (juce::Font::getDefaultMinimumHorizontalScaleFactor(), 1.0f),
- juce::String (juce::Font::getDefaultMinimumHorizontalScaleFactor(), 3));
- // paints `c` (or `paint`) at `scale` through the recorder; the picture is kept for the shots
- const auto record = [] (int w, int h, float scale, const std::function<void (juce::Graphics&)>& paint, juce::Image* keep = nullptr) {
- juce::Image image (juce::Image::ARGB, juce::jmax (1, juce::roundToInt ((float) w * scale)), juce::jmax (1, juce::roundToInt ((float) h * scale)), true,
- juce::SoftwareImageType());
- juce::Graphics (image).fillAll (theme::colour::bg1);
- TextFit190ulRecorder rec (image);
- {
- juce::Graphics g (rec);
- g.addTransform (juce::AffineTransform::scale (scale));
- paint (g);
- }
- if (keep != nullptr)
- *keep = image;
- return rec.runs;
- };
- const auto seenOf = [] (const std::vector<TextFit190ulRecorder::Run>& runs, const juce::String& text) { return textFit190ulSeen (runs, text); };
- // --- 3. the recorder sees a squeeze when one is drawn (else this check could not fail) ------------------------------
- {
- const juce::String text ("Line (follows zoom)");
- // a box 85 % of the text's width in this platform's face (Segoe UI on Windows fits 90 px whole)
- const auto boxWidth = juce::roundToInt (juce::GlyphArrangement::getStringWidth (theme::font (theme::type::body), text) * 0.85f);
- const auto runs = record (boxWidth, 24, 1.0f, [&] (juce::Graphics& g) {
- g.setFont (theme::font (theme::type::body));
- g.drawFittedText (text, juce::Rectangle<int> (0, 0, boxWidth, 24), juce::Justification::centredLeft, 1, 0.7f); // (the old way, on purpose)
- });
- const auto s = seenOf (runs, text);
- 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));
- const auto cached = record (boxWidth, 24, 1.0f, [&] (juce::Graphics& g) {
- g.setFont (theme::font (theme::type::body));
- textcache::drawFittedText (g, text, juce::Rectangle<int> (0, 0, boxWidth, 24), juce::Justification::centredLeft, 1, 0.7f);
- });
- const auto c = seenOf (cached, text);
- 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));
- }
- // --- 2. the widgets -----------------------------------------------------------------------------------------------
- const auto textBefore = theme::textSize();
- juce::StringArray saved;
- const auto shot = [&] (const juce::Image& image, const juce::String& name) {
- if (shots.isEmpty() || ! image.isValid())
- return;
- const auto file = juce::File (shots + "-" + name + ".png");
- file.deleteFile();
- if (auto out = file.createOutputStream())
- {
- juce::PNGImageFormat png;
- if (png.writeImageToStream (image, *out))
- saved.add (file.getFileName());
- }
- };
- for (const int textPercent : { 100, 150, 200 })
- {
- theme::setTextSize (textPercent);
- for (const int uiPercent : { 100, 150, 200 })
- {
- const auto scale = (float) uiPercent / 100.0f;
- const auto at = " (text " + juce::String (textPercent) + " %, interface " + juce::String (uiPercent) + " %)";
- const auto keepShot = textPercent == 100 || uiPercent == 100;
- const auto tag = "t" + juce::String (textPercent) + "-ui" + juce::String (uiPercent);
- // a widget painted whole (its children too: the combo's label)
- const auto widget = [&] (juce::Component& c, const juce::String& name, const juce::String& text, bool mustCut) {
- juce::Image image;
- const auto runs = record (c.getWidth(), c.getHeight(), scale, [&c] (juce::Graphics& g) { c.paintEntireComponent (g, false); }, &image);
- const auto s = seenOf (runs, text);
- check (name + ": drawn at scale 1" + at, s.drawn && s.minScale > 0.999f, describe (s));
- if (mustCut)
- check (name + ": cut with \"" + dots + "\", its start kept" + at, s.cut && s.ellipsis && s.keepsStart, describe (s));
- else
- check (name + ": whole, or cut with \"" + dots + "\" keeping its start" + at, s.keepsStart && (! s.cut || s.ellipsis), describe (s));
- if (keepShot)
- shot (image, name.toLowerCase().replaceCharacters (" ()", "---").retainCharacters ("abcdefghijklmnopqrstuvwxyz0123456789-") + "-" + tag);
- };
- // the issue's box: the Playlist's snap (magnet), 128 px, "Line (follows zoom)"
- {
- SnapBox snap (true);
- grid::fillEditorBox (snap);
- snap.setSelectedId (grid::screenOption + 2, juce::dontSendNotification);
- snap.setBounds (0, 0, 128, 24);
- const auto text = snap.getText();
- check ("the snap box shows \"Line (follows zoom)\"" + at, text == tr ("Line (follows zoom)"), text);
- widget (snap, "snap box", text, true);
- }
- {
- juce::ComboBox box;
- box.addItem ("Line (follows zoom)", 1);
- box.setSelectedId (1, juce::dontSendNotification);
- box.setBounds (0, 0, 110, 24);
- widget (box, "combo box", box.getText(), true);
- }
- {
- const juce::String text ("Render the selected clips to audio and replace them");
- juce::TextButton button (text);
- button.setBounds (0, 0, 120, 26);
- widget (button, "text button", text, true);
- button.getProperties().set (theme::LookAndFeel::iconProperty, (int) Icon::magnet);
- widget (button, "icon button", text, true);
- }
- {
- const juce::String text ("Line (follows zoom)");
- juce::Label label ({}, text);
- label.setFont (theme::font (theme::type::body));
- label.setBounds (0, 0, 80, 20);
- widget (label, "label", text, true);
- label.setMinimumHorizontalScale (0.5f); // (a label that still asks for a squeeze gets none)
- widget (label, "label asking 0.5", text, true);
- }
- {
- const juce::String text ("Snap the clip starts and their lengths to the grid of the editor that is open now");
- juce::ToggleButton toggle (text);
- toggle.setBounds (0, 0, 120, 22);
- widget (toggle, "tick box", text, true);
- }
- {
- const juce::String text ("Line (follows zoom) with a longer menu item name");
- juce::Image image;
- const auto runs = record (150, 24, scale, [&] (juce::Graphics& g) {
- lookAndFeel.drawPopupMenuItem (g, { 0, 0, 150, 24 }, false, true, false, false, false, text, {}, nullptr, nullptr);
- }, &image);
- const auto s = seenOf (runs, text);
- 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));
- if (keepShot)
- shot (image, "menu-row-" + tag);
- }
- {
- const juce::String text ("Line (follows zoom)");
- juce::TabbedButtonBar bar (juce::TabbedButtonBar::TabsAtTop);
- bar.addTab (text, theme::colour::bg2, 0);
- bar.setBounds (0, 0, 90, 26);
- if (auto* tab = bar.getTabButton (0))
- {
- tab->setBounds (0, 0, 90, 26);
- widget (*tab, "tab", text, true);
- }
- }
- {
- // a translated menu on the tight level (round 190oo WG-11): smaller letters instead of narrower ones
- juce::MenuBarComponent menuBar;
- menuBar.getProperties().set ("lynxFlMenuBar", true);
- menuBar.getProperties().set ("lynxTightMenuBar", true);
- menuBar.getProperties().set ("lynxCondensedMenuBar", true);
- menuBar.getProperties().set ("lynxMenuBarScale", 0.9);
- const juce::String text ("Werkzeuge");
- const auto width = lookAndFeel.getMenuBarItemWidth (menuBar, 0, text);
- const auto runs = record (width, 24, scale, [&] (juce::Graphics& g) {
- lookAndFeel.drawMenuBarItem (g, width, 24, 0, text, false, false, false, menuBar);
- });
- const auto s = seenOf (runs, text);
- check ("menu bar title on the tight level: drawn at scale 1, whole" + at, s.drawn && s.minScale > 0.999f && ! s.cut, describe (s));
- }
- {
- // FL's caption fitting (plugin windows): the font shrinks to its smallest, then the text is cut
- const juce::String text ("Modulation envelope amount");
- const auto runs = record (60, 14, scale, [&] (juce::Graphics& g) {
- g.setColour (theme::colour::textPrimary);
- ui::drawFittingText (g, text, { 0.0f, 0.0f, 60.0f, 14.0f }, juce::Justification::centredLeft, theme::font (theme::type::small), 7.5f);
- });
- const auto s = seenOf (runs, text);
- check ("caption fitting (plugin windows): smaller font, then \"" + dots + "\", never narrower" + at, s.drawn && s.minScale > 0.999f && s.ellipsis && s.keepsStart,
- describe (s));
- }
- }
- }
- theme::setTextSize (textBefore);
- // --- the issue's view: the Playlist's toolbar at 1280 x 832, the snap on "Line (follows zoom)" --------------------
- #if JUCE_LINUX || JUCE_WINDOWS
- if (shots.isNotEmpty())
- {
- AudioEngine offline (false);
- MainComponent ui (offline, false);
- ui.setSize (1280, 832);
- ui.setVisible (true);
- const auto settle = [&offline] (int ms) {
- const auto end = juce::Time::getMillisecondCounter() + (juce::uint32) ms;
- do
- {
- offline.dispatchPendingMessages();
- for (int i = 0; i < 256 && juce::detail::dispatchNextMessageOnSystemQueue (true); ++i) {}
- juce::Thread::sleep (10);
- } while (juce::Time::getMillisecondCounter() < end);
- };
- settle (300);
- ui.selectTabForSnapshot (2);
- settle (300);
- auto& playlist = ui.getPlaylistForSnapshot();
- SnapBox* snap = nullptr;
- for (auto* child : playlist.getChildren())
- if (auto* s = dynamic_cast<SnapBox*> (child); s != nullptr && s->isVisible())
- snap = s;
- check ("the Playlist has its snap box", snap != nullptr);
- if (snap != nullptr)
- {
- snap->setSelectedId (grid::screenOption + 2, juce::dontSendNotification);
- settle (100);
- const auto strip = juce::Rectangle<int> (0, 0, juce::jmin (playlist.getWidth(), snap->getRight() + 120), snap->getBottom() + 4);
- const auto s = seenOf (record (snap->getWidth(), snap->getHeight(), 1.0f, [snap] (juce::Graphics& g) { snap->paintEntireComponent (g, false); }),
- snap->getText());
- 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),
- describe (s));
- for (const auto sc : { 1.0f, 1.5f, 2.0f })
- shot (playlist.createComponentSnapshot (strip, true, sc), "playlist-toolbar-1280-" + juce::String (juce::roundToInt (sc * 100.0f)));
- }
- }
- #endif
- if (shots.isNotEmpty())
- log << "# shots: " << saved.joinIntoString (", ") << "\n";
- log << "# " << failures << " failures\n";
- return result;
- }
|