Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Romanized lyrics shows lines in other alphabets again in Latin letters
My-Name-Is-Jeff079dccf56f5a
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 3c82f5b2..42d2d414 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -88,7 +88,9 @@ Shared:Lyrics/ the lyrics engine for the redesign's Apple Music style lyrics and the lock screen: lines read fromcolor-lyrics and the player's clock (KaraokeSource.x), words timed by estimate inside Spotify's linetimes (KaraokeTiming.m, which splits Thai, Lao, Khmer and Burmese at the system's dictionary words), which line to name where two voices sing at once (the one that came in first,- for the lock screen and the Live Activity), and the Lyrics page's parts. Lines are kept in time order+ for the lock screen and the Live Activity), a line in another alphabet in Latin letters, offline by Apple's+ transforms, kanji read the Japanese way in a song with kana (Romanise.m; checked on the simulator by+ harness/lyrics' -romanise), and the Lyrics page's parts. Lines are kept in time orderwhatever order a source lists them in. With EeveeSpotify injected (SGEeveeSpotifyInjected), its lyricsanswer Spotify's requests and the mod's sources and its own requests stand asideLyricsSources/ the sources lyrics come from, asked in the order the Lyrics page puts them in and merged into the@@ -353,7 +355,10 @@ Redesigned:that breathe and fill over its length on a Core Animation timeline laid against the song's clock; anda line's pronunciation (under the words it spells) and translation, switched on from a glass button inthe lyrics' corner that shows only for a song that has them, in the order of sizes the Lyrics page sets- (LyricsText.h). A word held 0.9 s or more glows and its letters rise in a wave as the sweep reaches+ (LyricsText.h). With the Lyrics page's Romanised lyrics on, a line in another alphabet has the same+ again in Latin letters right under its words (the source's own pronunciation where it is hidden, none+ where it shows), before the translation, at the pronunciation's size, lit with its line but never swept.+ A word held 0.9 s or more glows and its letters rise in a wave as the sweep reachesthem. The size, the room between lines, the blur, the glow and the wave are the Lyrics page's look(LyricsLook.h): five presets and a sheet of sliders, applied at once to every view, under a livepreview of the view playing a song of its own (LyricsLookSettings.m). Laid out on the Mac against@@ -466,8 +471,8 @@ Albums & artists as it is flipped. Tab bar: the tab editor of the stored look, eits own list of tabs. Lyrics, on the main page of its own: in the redesign a live preview of the lyrics first, with the presets of their lookunder it and their sliders in a sheet (Text size, Line spacing, Blur, Glow, Wave), applying at once; then Sing, then the ordered list of lyrics sources,lyrics for every track, naming the source in the redesign, the lock screen, and glass lyrics in the native look; in-the redesign also which of the lyrics, their pronunciation and their translation is set largest, and the-translation's language. Sing, under either look, on the main page and as Lyrics' first row, the row reading out On,+the redesign also which of the lyrics, their pronunciation and their translation is set largest, Romanised lyrics+(applied at once), and the translation's language. Sing, under either look, on the main page and as Lyrics' first row, the row reading out On,Off or how far the voice model's download has come, kept up to date while the page shows: Sing's switch, which turnsthe mic on and off at once, its Status (a tap says more), the Vocals slider (gone, as sung, the vocals alone), Spatial voice(where the iPhone reads headphone motion; a page of its own, reading out On or Off, with a live preview at its top thatdiff --git a/harness/lyrics-page/build.sh b/harness/lyrics-page/build.shindex 27d294c3..dcbe9cab 100755--- a/harness/lyrics-page/build.sh+++ b/harness/lyrics-page/build.sh@@ -15,7 +15,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$SRC"/Redesigned/Lyrics/LyricsLookSettings.m "$SRC"/Redesigned/Lyrics/LyricsLook.m \"$SRC"/Redesigned/Lyrics/SGRKaraokeView.m "$SRC"/Redesigned/Lyrics/LyricsText.m "$SRC"/Redesigned/Lyrics/SGRSingButton.m \"$SRC"/Redesigned/Lyrics/MeaningSheet.m "$SRC"/Shared/LyricsMeanings/Meanings.m \- "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/AdBlock/Protobuf.m \+ "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/Lyrics/Romanise.m "$SRC"/Shared/AdBlock/Protobuf.m \"$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRGlass.m \"$SRC"/Settings/SGPage.m "$SRC"/Settings/SGPageStyle.m "$SRC"/Settings/SGModPage.m "$SRC"/Settings/SGGlowSwitch.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGGlass.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \diff --git a/harness/lyrics/README.md b/harness/lyrics/README.mdindex c764572b..2a8ad14c 100644--- a/harness/lyrics/README.md+++ b/harness/lyrics/README.md@@ -27,6 +27,8 @@ The fixtures are real TTML and LRC with every word swapped for filler of the sam- `spotify-line`, `spotify-static` Spotify's own color-lyrics JSON, LINE_SYNCED and UNSYNCED, read bythe real `SGKaraokeLinesFromBody`; `static` plain text, as LRCLIB's plainLyrics(these four are written by `fixtures/linetimed.py` from `plain`'s timing)+- `scripts` (built in) a line in each of Japanese, Korean, Chinese, Russian, Greek, Thai, Hindi, Arabic and+ Hebrew among English ones, every other one translated, for `-spotifyglass.lyricsRomanised 1`- `rtl` (built in) right to left lines among left to right ones, a second voice, a break before thelast line; `rtlx` the same with translations and a romanization@@ -39,8 +41,9 @@ at launch. `-dumpTo PATH` writes the dump to a file, for when `--console` showsA real file goes in with `-file /path/song.ttml`: the simulator reads the Mac's paths.`-perf LABEL` logs the view's per frame cost (its display link's `tick`) every 240 frames; run with-`simctl launch --console` to read it. `-dump 1` prints the lines as read, with their pronunciations and-translations, and quits. `-openMenu 3` opens the pronunciation and translation menu three seconds in,+`simctl launch --console` to read it. `-dump 1` prints the lines as read, with their pronunciations,+translations and romanised readings, and quits. `-romanise 1` asserts the romanised readings+(`Shared/Lyrics/Romanise.m`) and quits with the number that failed. `-openMenu 3` opens the pronunciation and translation menu three seconds in,`-toggleAt 3` switches both over as the menu would, and `-light 1` puts the window in light mode.A screen recording catches the motion: `simctl io <udid> recordVideo -f run.mp4`, then e.g.diff --git a/harness/lyrics/build.sh b/harness/lyrics/build.shindex 736508e1..fbed510b 100755--- a/harness/lyrics/build.sh+++ b/harness/lyrics/build.sh@@ -19,7 +19,7 @@ SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g ${OPT:--O0} \-I"$SRC" -I"$SRC/Redesigned/Lyrics" -isysroot "$SDK" -Wall -Wno-deprecated-declarations \"$HERE/main.m" "$HERE/../scene.m" "$HERE/stubs.m" \- "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m $( [ -f "$SRC"/Redesigned/Lyrics/LyricsText.m ] && echo "$SRC"/Redesigned/Lyrics/LyricsText.m ) $( [ -f "$SRC"/Redesigned/Lyrics/LyricsLook.m ] && echo "$SRC"/Redesigned/Lyrics/LyricsLook.m ) "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/AdBlock/Protobuf.m \+ "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m $( [ -f "$SRC"/Redesigned/Lyrics/LyricsText.m ] && echo "$SRC"/Redesigned/Lyrics/LyricsText.m ) $( [ -f "$SRC"/Redesigned/Lyrics/LyricsLook.m ] && echo "$SRC"/Redesigned/Lyrics/LyricsLook.m ) "$SRC"/Shared/Lyrics/KaraokeTiming.m $( [ -f "$SRC"/Shared/Lyrics/Romanise.m ] && echo "$SRC"/Shared/Lyrics/Romanise.m ) "$SRC"/Shared/AdBlock/Protobuf.m \"$SRC"/Shared/LyricsSources/SGTTML.m "$SRC"/Redesigned/Kit/SGRTokens.m \$( [ -f "$SRC"/Shared/LyricsMeanings/Meanings.m ] && echo "$SRC"/Shared/LyricsMeanings/Meanings.m "$SRC"/Redesigned/Lyrics/MeaningSheet.m ) \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGGlass.m \diff --git a/harness/lyrics/main.m b/harness/lyrics/main.mindex b7995e98..b6940c92 100644--- a/harness/lyrics/main.m+++ b/harness/lyrics/main.m@@ -4,7 +4,8 @@//// Launch arguments (the argument domain of NSUserDefaults, so a setting's key works as one too):// -song NAME fixtures/NAME.ttml, .lrc, .json (Spotify's own) or .txt (plain) in the app, or rtl,-// built here (default duet)+// built here (default duet); scripts (built here) is a line in each of many alphabets+// and two in English, the second line of each script translated, for romanised lyrics// -file PATH a TTML or LRC file on the Mac instead// -at MS where the clock starts (default 0)// -rate X how fast it runs (default 1)@@ -14,6 +15,8 @@// -light 1 the window in light mode, for the glass's appearance// -perf LABEL logs the cost of the view's frames every 240 of them, under LABEL// -dump 1 prints the lines as read, with their pronunciations and translations, and quits+// -romanise 1 asserts romanised lyrics' readings (Shared/Lyrics/Romanise.m), prints PASS and FAIL+// lines and quits with the number of failures// -openMenu S opens the pronunciation and translation menu S seconds in, as a tap on its button would// -toggleAt S switches the pronunciation and the translation over S seconds in, as the menu would// -translateIn S keeps copies of the lines S seconds in with every other one translated, as@@ -25,7 +28,7 @@// number of failures; run it with -song duet -at 20000 -gemini 1 -geminiDelay 1 -seekLag 0.4// and the lyrics' own settings by their keys: -spotifyglass.lyricsSimulateWords 1 (sweep line timed// lines on the estimate), -spotifyglass.redesign.lyricsPronunciation 1,-// -spotifyglass.redesign.lyricsTranslation 1, -spotifyglass.redesign.lyricsTextOrder '(translation, lyrics, pronunciation)'.+// -spotifyglass.redesign.lyricsTranslation 1, -spotifyglass.lyricsRomanised 1, -spotifyglass.redesign.lyricsTextOrder '(translation, lyrics, pronunciation)'.#import <UIKit/UIKit.h>#import <objc/runtime.h>#import <objc/message.h>@@ -98,6 +101,68 @@ static NSArray<SGKaraokeLine *> *rightToLeftSongWithExtras(void) {return lines;}+// A line in each of the alphabets romanised lyrics reads, among English ones, written here: every+// other one translated, to see the order of the three; a Hebrew and an Arabic line keep their edge.+static NSArray<SGKaraokeLine *> *scriptsSong(void) {+ NSArray<NSArray<NSString *> *> *texts = @[+ @[@"Hello again, my old friend", @""],+ @[@"君の名前を 呼んでいた", @"I was calling your name"],+ @[@"東京の 夜空に 星が 見えない", @""],+ @[@"사랑해요 너를 영원히", @"I love you forever"],+ @[@"我 爱 你 直到 永远", @""],+ @[@"Привет, как дела сегодня?", @"Hi, how are you today?"],+ @[@"Καλημέρα κόσμε", @""],+ @[@"สวัสดี ครับ ฉัน รัก เธอ", @"Hello, I love you"],+ @[@"नमस्ते दुनिया", @""],+ @[@"مرحبا بالعالم", @"Hello world"],+ @[@"שלום עולם", @""],+ @[@"Ich möchte einen Café", @""],+ ];+ NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];+ for (NSUInteger i = 0; i < texts.count; i++) {+ SGKaraokeLine *line = timed(1000 + (NSInteger)i * 3500, texts[i][0], nil);+ if (texts[i][1].length) line.translation = texts[i][1];+ [lines addObject:line];+ }+ return lines;+}++#ifdef SGKeyLyricsRomanised+// The readings Apple's transforms give, as the phone shows them. Failures are counted for the exit code.+static int checkRomanised(void) {+ NSArray<NSArray *> *cases = @[+ @[@"君の名前を呼んでいた", @NO, @"kimi no namae wo yon de i ta"], // kana: Japanese readings of its kanji+ @[@"東京", @YES, @"toukyou"], // kanji alone in a Japanese song+ @[@"東京", @NO, @"dong jing"], // ...and in a Chinese one, pinyin+ @[@"我爱你,中国!", @NO, @"wǒ ài nǐ, zhōng guó!"], // tones kept, punctuation made ASCII+ @[@"사랑해요 너를", @NO, @"salanghaeyo neoleul"],+ @[@"Привет, как дела?", @NO, @"Privet, kak dela?"],+ @[@"Καλημέρα κόσμε", @NO, @"Kalemera kosme"],+ @[@"สวัสดีครับ", @NO, @"swasdi khrab"],+ @[@"नमस्ते दुनिया", @NO, @"namaste duniya"],+ @[@"I love 東京 tonight", @YES, @"I love toukyou tonight"], // English words kept as they are+ @[@"Ich möchte Café", @NO, NSNull.null], // Latin already: nothing to show+ @[@"Tôi yêu em", @NO, NSNull.null],+ @[@"♪", @NO, NSNull.null],+ @[@"", @NO, NSNull.null],+ ];+ int failures = 0;+ for (NSArray *c in cases) {+ NSString *got = SGLyricsRomanised(c[0], [c[1] boolValue]);+ // dong jing is checked without its tones, which ICU may mark differently between versions.+ if ([c[2] isEqual:@"dong jing"]) got = [got stringByApplyingTransform:@"Latin-ASCII" reverse:NO];+ BOOL ok = c[2] == NSNull.null ? got == nil : [got isEqualToString:c[2]];+ failures += !ok;+ printf("%s %s -> %s\n", ok ? "PASS" : "FAIL", [c[0] UTF8String], got ? got.UTF8String : "(none)");+ }+ BOOL japanese = SGLyricsLooksJapanese(@[timed(0, @"Hello", nil), timed(0, @"ラーメン", nil)]);+ BOOL korean = SGLyricsLooksJapanese(@[timed(0, @"사랑해요", nil), timed(0, @"東京", nil)]);+ failures += !japanese + korean;+ printf("%s a song with kana reads as Japanese, one without does not\n", japanese && !korean ? "PASS" : "FAIL");+ return failures;+}+#endif+// [mm:ss.xx] text, as LRCLIB and Spotify's own line-synced lyrics come: timed by the line, the words// estimated, a ♪ or an empty line a break.static NSArray<SGKaraokeLine *> *linesOfLRC(NSString *lrc) {@@ -117,6 +182,7 @@ static NSArray<SGKaraokeLine *> *linesOfLRC(NSString *lrc) {static NSArray<SGKaraokeLine *> *songNamed(NSString *name, NSString *file) {if (!file && [name isEqualToString:@"rtl"]) return rightToLeftSong();if (!file && [name isEqualToString:@"rtlx"]) return rightToLeftSongWithExtras();+ if (!file && [name isEqualToString:@"scripts"]) return scriptsSong();NSString *path = file;for (NSString *type in @[@"ttml", @"lrc", @"json", @"txt"]) path = path ?: [NSBundle.mainBundle pathForResource:name ofType:type];NSString *text = path ? [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] : nil;@@ -148,6 +214,10 @@ static void dump(NSArray<SGKaraokeLine *> *lines) {printf(" %s %s\n", spoken == line.pronunciation ? "pr" : "bp", [words componentsJoinedByString:@" "].UTF8String);}if (line.translation) printf(" tr %s\n", line.translation.UTF8String);+#endif+#ifdef SGKeyLyricsRomanised+ NSString *romanised = SGLyricsRomanised(SGKaraokeLineText(line), SGLyricsLooksJapanese(lines));+ if (romanised) printf(" ro %s\n", romanised.UTF8String);#endif}fflush(stdout);@@ -315,8 +385,13 @@ static void runChecks(SGRKaraokeView *karaoke, UIView *host) {SGKaraokeLine *line = timed(0, @"hold this", @"v1");line.words.lastObject.end = line.words.lastObject.start + 3000;line.end = line.words.lastObject.end;+#ifdef SGKeyLyricsRomanised+ id style = ((id (*)(id, SEL, CGFloat, NSArray *, BOOL, BOOL, BOOL))objc_msgSend)([NSClassFromString(@"SGRKaraokeStyle") alloc],+ NSSelectorFromString(@"initWithSize:order:pronunciation:translation:romanised:"), 30, SGRLyricsTextOrder(), NO, NO, NO);+#elseid style = ((id (*)(id, SEL, CGFloat, NSArray *, BOOL, BOOL))objc_msgSend)([NSClassFromString(@"SGRKaraokeStyle") alloc],NSSelectorFromString(@"initWithSize:order:pronunciation:translation:"), 30, SGRLyricsTextOrder(), NO, NO);+#endifUIView *lineView = ((id (*)(id, SEL, id, CGFloat, id, id, BOOL, BOOL))objc_msgSend)([NSClassFromString(@"SGRKaraokeLineView") alloc],NSSelectorFromString(@"initWithLine:width:style:under:blurred:sweepsEstimates:"), line, 300, style, nil, NO, NO);[karaoke.window addSubview:lineView];@@ -407,6 +482,9 @@ static void runChecks(SGRKaraokeView *karaoke, UIView *host) {#ifdef SGRKeyLyricsTextOrder// Each launch starts from its own arguments, not from what the menu stored on the last one.for (NSString *key in @[SGRKeyLyricsPronunciation, SGRKeyLyricsTranslation, SGRKeyLyricsTextOrder]) [args removeObjectForKey:key];+#endif+#ifdef SGKeyLyricsRomanised+ if ([args boolForKey:@"romanise"]) exit(checkRomanised());#endifNSString *song = [args stringForKey:@"song"] ?: @"duet";SGKaraokeKeepLines(@"harness", songNamed(song, [args stringForKey:@"file"]));diff --git a/harness/player/build.sh b/harness/player/build.shindex 0ad0628c..bbbf4311 100755--- a/harness/player/build.sh+++ b/harness/player/build.sh@@ -24,7 +24,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$SRC"/Redesigned/Kit/SGRedesign.m \"$SRC"/Redesigned/Player/PlayerSettings.m "$SRC"/Redesigned/NowPlayingBar/NowPlayingBarSettings.m \"$SRC"/Settings/SGPage.m "$SRC"/Settings/SGPageStyle.m "$SRC"/Settings/SGModPage.m "$SRC"/Settings/SGGlowSwitch.m \- "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m "$SRC"/Redesigned/Lyrics/LyricsLook.m "$SRC"/Redesigned/Lyrics/SGRSingButton.m "$SRC"/Redesigned/Lyrics/MeaningSheet.m "$SRC"/Redesigned/Lyrics/LyricsText.m "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/AdBlock/Protobuf.m \+ "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m "$SRC"/Redesigned/Lyrics/LyricsLook.m "$SRC"/Redesigned/Lyrics/SGRSingButton.m "$SRC"/Redesigned/Lyrics/MeaningSheet.m "$SRC"/Redesigned/Lyrics/LyricsText.m "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/Lyrics/Romanise.m "$SRC"/Shared/AdBlock/Protobuf.m \-framework UIKit -framework QuartzCore -framework CoreGraphics -framework CoreImage -framework Foundation -framework Symbols -framework AVFoundation -framework CoreMedia \-o "$OUT/PlayerHarness.app/PlayerHarness"diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.mindex d019f746..706c8ada 100644--- a/tweak/Sources/App/Pages.m+++ b/tweak/Sources/App/Pages.m@@ -102,7 +102,7 @@ UIViewController *SGNavbarPage(void) {return SGRedesignedUIStored() ? SGRNavbarSettingsPage() : SGNavbarSettingsPage();}-// Pronunciation, translation, word sweeping and line meanings exist only in the redesign's lyrics view.+// Pronunciation, romanised lines, translation, word sweeping and line meanings exist only in the redesign's lyrics view.static UIViewController *lyricsPage(void) {BOOL redesigned = SGRedesignedUIStored();NSMutableArray<SGModRow *> *more = [NSMutableArray arrayWithObject:SGLockScreenLyricsRow()];@@ -112,7 +112,7 @@ static UIViewController *lyricsPage(void) {NSMutableArray<SGModSection *> *sections = [NSMutableArray arrayWithObjects:SGSection(nil, @[SGWithTile(sing, @"music.mic", UIColor.systemRedColor)]),SGLyricsSourcesSection(redesigned), nil];if (redesigned) {- [sections addObject:SGSection(@"Display", @[SGLyricsWordTimingRow(), SGRLyricsTextSizesRow(), SGLyricsTranslationLanguageRow(), SGLyricsMeaningsRow(),+ [sections addObject:SGSection(@"Display", @[SGLyricsWordTimingRow(), SGRLyricsTextSizesRow(), SGLyricsRomanisedRow(), SGLyricsTranslationLanguageRow(), SGLyricsMeaningsRow(),SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide),SGSwitchRow(@"Landscape lyrics", @"Turn the phone with the lyrics open", SGRKeyLyricsLandscape),SGGeminiKeyRow()])];diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex e3f312d1..efc229a1 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -346,7 +346,13 @@ static double heldStrength(double held) {@property (nonatomic, readonly) NSArray<NSNumber *> *order; // SGRLyricsText, largest first, shown ones only@property (nonatomic, readonly) UIFont *lyrics, *pronunciation, *translation; // nil for a text not shown@property (nonatomic, readonly) SGRKaraokeStyle *backing; // the backing row's: smaller, its translation read with the line's-- (instancetype)initWithSize:(CGFloat)size order:(NSArray<NSNumber *> *)order pronunciation:(BOOL)pronunciation translation:(BOOL)translation;+// The line again in Latin letters (Shared/Lyrics' romanised lyrics), at the pronunciation's place in the+// order, since it is one (the second place's while that is the first); nil while the Lyrics page has+// it off. A backing row has none.+@property (nonatomic, readonly) UIFont *romanised;+@property (nonatomic) BOOL japanese; // the song has kana, so its kanji are read the Japanese way+- (instancetype)initWithSize:(CGFloat)size order:(NSArray<NSNumber *> *)order pronunciation:(BOOL)pronunciation translation:(BOOL)translation+ romanised:(BOOL)romanised;@end// The second and third place's sizes, as shares of the first: Apple Music's 20 and 16 under its 30.@@ -354,9 +360,12 @@ static const CGFloat kSecondShare = 0.67, kThirdShare = 0.54;@implementation SGRKaraokeStyle-- (instancetype)initWithSize:(CGFloat)size order:(NSArray<NSNumber *> *)order pronunciation:(BOOL)pronunciation translation:(BOOL)translation {+- (instancetype)initWithSize:(CGFloat)size order:(NSArray<NSNumber *> *)order pronunciation:(BOOL)pronunciation translation:(BOOL)translation+ romanised:(BOOL)romanised {if (!(self = [super init])) return nil;CGFloat sizes[3] = {size, round(size * kSecondShare), round(size * kThirdShare)};+ NSUInteger spoken = [order indexOfObject:@(SGRLyricsTextPronunciation)];+ if (romanised) _romanised = [UIFont systemFontOfSize:spoken > 0 && spoken < 3 ? sizes[spoken] : sizes[1] weight:UIFontWeightSemibold];NSMutableArray<NSNumber *> *shown = [NSMutableArray array];for (NSUInteger place = 0; place < order.count && place < 3; place++) {SGRLyricsText text = order[place].integerValue;@@ -404,6 +413,8 @@ static const CGFloat kPairTighten = 2, kPairGap = 4, kPartGap = 6;@property (nonatomic, copy) NSArray<NSValue *> *lyricFrames, *spokenFrames;@property (nonatomic, copy) NSArray<NSNumber *> *lyricOffsets, *spokenOffsets;@property (nonatomic) CGRect translation; // CGRectNull without one+@property (nonatomic) CGRect romanised; // likewise+@property (nonatomic, copy) NSString *romanisedText;@property (nonatomic) CGFloat backingTop; // 0 without a backing row@end@@ -573,15 +584,28 @@ static CGFloat layPair(NSArray<SGKaraokeWord *> *lead, UIFont *leadFont, NSArrayreturn bottom;}+// A text set as a block, wrapping as a label does, from `top`.+static CGRect blockFrame(NSString *text, UIFont *font, CGFloat width, CGFloat top) {+ CGRect bounds = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin+ attributes:@{NSFontAttributeName: font} context:nil];+ return CGRectMake(0, top, width, ceil(bounds.size.height));+}+// The texts a line has, in the style's order, the lyrics and their pronunciation set as a pair where-// the order has them side by side, and the backing row after the lyrics. `right` is the side a backing+// the order has them side by side, the line in Latin letters right under the lyrics where it is not+// already spelt out by the pronunciation, and the backing row after that. `right` is the side a backing// row keeps to, its line's; -1 for a line of its own, which takes its side from its voice and script.static SGRKaraokeLayout *layOut(SGKaraokeLine *line, CGFloat width, SGRKaraokeStyle *style, NSInteger right) {SGRKaraokeLayout *layout = [SGRKaraokeLayout new];layout.right = right >= 0 ? right : alignsRight(line);- layout.translation = CGRectNull;+ layout.translation = layout.romanised = CGRectNull;NSArray<SGKaraokeWord *> *spoken = style.pronunciation ? line.pronunciation.words : nil;NSString *translation = style.translation ? line.translation : nil;+ // A source's own pronunciation, hidden, still reads better than the transform's.+ if (style.romanised && !spoken.count) {+ layout.romanisedText = line.pronunciation ? SGKaraokeLineText(line.pronunciation)+ : SGLyricsRomanised(SGKaraokeLineText(line), style.japanese);+ }NSMutableArray<NSNumber *> *parts = [NSMutableArray array];for (NSNumber *text in style.order) {if (text.integerValue == SGRLyricsTextPronunciation && !spoken.count) continue;@@ -595,9 +619,7 @@ static SGRKaraokeLayout *layOut(SGKaraokeLine *line, CGFloat width, SGRKaraokeStSGRLyricsText text = parts[p].integerValue;if (p > 0) y += kPartGap;if (text == SGRLyricsTextTranslation) {- CGRect bounds = [translation boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin- attributes:@{NSFontAttributeName: style.translation} context:nil];- layout.translation = CGRectMake(0, y, width, ceil(bounds.size.height));+ layout.translation = blockFrame(translation, style.translation, width, y);y = CGRectGetMaxY(layout.translation);continue;}@@ -625,6 +647,10 @@ static SGRKaraokeLayout *layOut(SGKaraokeLine *line, CGFloat width, SGRKaraokeStlayout.spokenOffsets = offsets;}}+ if (lyrics && layout.romanisedText.length) {+ layout.romanised = blockFrame(layout.romanisedText, style.romanised, width, y + kPartGap);+ y = CGRectGetMaxY(layout.romanised);+ }if (lyrics && right < 0 && line.backing.words.count) {layout.backingTop = y + kBackingGap;y = layout.backingTop + layOut(line.backing, width, style.backing, layout.right).height;@@ -765,7 +791,8 @@ static double secant(SGSweepKnot *knots, NSUInteger i) {@property (nonatomic, readonly) CGRect bubbleTarget;@end-// The translation of a line being sung, brighter than a line waiting but never as bright as the words.+// The translation and the romanised line of a line being sung, brighter than a line waiting but never+// as bright as the words.static const CGFloat kTranslationLit = 0.6;@implementation SGRKaraokeLineView {@@ -774,7 +801,7 @@ static const CGFloat kTranslationLit = 0.6;NSArray<SGRKaraokeWordView *> *_lyricWords;UIImageView *_bubble;CAShapeLayer *_underline;- UILabel *_translation;+ UILabel *_translation, *_romanised;NSUInteger _generation;SGRKaraokeLineView *_backing;}@@ -823,16 +850,8 @@ static double wholeFrom(SGKaraokeLine *run, BOOL sweepsEstimates) {_words = [sung.words ?: @[] arrayByAddingObjectsFromArray:spoken.words ?: @[]];_lyricWords = sung.words;- if (!CGRectIsNull(layout.translation)) {- _translation = [[UILabel alloc] initWithFrame:layout.translation];- _translation.numberOfLines = 0;- _translation.font = style.translation;- _translation.textColor = UIColor.whiteColor;- _translation.alpha = kDimAlpha;- _translation.textAlignment = _right ? NSTextAlignmentRight : NSTextAlignmentLeft;- _translation.text = line.translation;- [self addSubview:_translation];- }+ if (!CGRectIsNull(layout.translation)) _translation = [self asideAt:layout.translation font:style.translation text:line.translation];+ if (!CGRectIsNull(layout.romanised)) _romanised = [self asideAt:layout.romanised font:style.romanised text:layout.romanisedText];if (layout.backingTop > 0) {_backing = [[SGRKaraokeLineView alloc] initWithLine:line.backing width:width style:style.backing under:self blurred:NOsweepsEstimates:sweepsEstimates];@@ -851,6 +870,19 @@ static double wholeFrom(SGKaraokeLine *run, BOOL sweepsEstimates) {return self;}+// A text of the line set apart from its words, dim until the line is sung.+- (UILabel *)asideAt:(CGRect)frame font:(UIFont *)font text:(NSString *)text {+ UILabel *label = [[UILabel alloc] initWithFrame:frame];+ label.numberOfLines = 0;+ label.font = font;+ label.textColor = UIColor.whiteColor;+ label.alpha = kDimAlpha;+ label.textAlignment = _right ? NSTextAlignmentRight : NSTextAlignmentLeft;+ label.text = text;+ [self addSubview:label];+ return label;+}+// How many line views are made in one frame: the rest follow on the next, nearest the sung line first.static const NSUInteger kLinesPerFrame = 4;@@ -875,9 +907,10 @@ static const NSUInteger kLinesPerFrame = 4;_active = active;_backing.active = active;NSUInteger generation = ++_generation;- if (_translation) {- [UIView animateWithDuration:active ? 0.3 : 0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState- animations:^{ self->_translation.alpha = active ? kTranslationLit : kDimAlpha; } completion:nil];+ if (_translation || _romanised) {+ [UIView animateWithDuration:active ? 0.3 : 0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{+ self->_translation.alpha = self->_romanised.alpha = active ? kTranslationLit : kDimAlpha;+ } completion:nil];}if (active) {NSMutableArray<SGRKaraokeWordView *> *whole = [NSMutableArray array];@@ -929,7 +962,7 @@ static const NSUInteger kLinesPerFrame = 4;word.lit.alpha = 1;[word fillTo:CGFLOAT_MAX];}- _translation.alpha = kTranslationLit;+ _translation.alpha = _romanised.alpha = kTranslationLit;[_backing showPlain];}@@ -1275,6 +1308,7 @@ typedef struct {[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionEndedNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGRLyricsTextDidChangeNotification object:nil];+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGLyricsRomanisedDidChangeNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(lookChanged) name:SGRLyricsLookDidChangeNotification object:nil];// A locked phone leaves the card in its window, so the link has to be put down by the app going// away rather than by the view going: see scheduleLink.@@ -1634,9 +1668,13 @@ static BOOL hasWords(SGKaraokeLine *line) {// What a line shows besides its words: what the song has, of what the lyrics menu has switched on.- (SGRKaraokeStyle *)styleNow {- return [[SGRKaraokeStyle alloc] initWithSize:_fontSize order:SGRLyricsTextOrder()- pronunciation:_hasSpoken && SGFlag(SGRKeyLyricsPronunciation, NO)- translation:_hasTranslation && SGFlag(SGRKeyLyricsTranslation, NO)];+ BOOL romanised = SGFlag(SGKeyLyricsRomanised, NO);+ SGRKaraokeStyle *style = [[SGRKaraokeStyle alloc] initWithSize:_fontSize order:SGRLyricsTextOrder()+ pronunciation:_hasSpoken && SGFlag(SGRKeyLyricsPronunciation, NO)+ translation:_hasTranslation && SGFlag(SGRKeyLyricsTranslation, NO)+ romanised:romanised];+ style.japanese = romanised && SGLyricsLooksJapanese(_lines);+ return style;}// A switch of the menu or the Lyrics page's order: the song is measured again in the new style offdiff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.hindex 0da7344d..c24c90c1 100644--- a/tweak/Sources/Shared/Lyrics/Lyrics.h+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h@@ -11,11 +11,25 @@// LyricsSettings.m: the Lyrics page's parts (App/Pages.m puts the page together): where lyrics come// from, naming the source (read by the redesign's lyrics view only), the lock screen, which// language a line's translation is taken in, of those the lyrics come with, and the one Musixmatch's-// community translations are asked in (the redesign's lyrics being where translations show), and whether lines timed only by the line are swept word by word.+// community translations are asked in (the redesign's lyrics being where translations show), whether lines timed only by the line are swept word by word, and whether lines in other alphabets show again in Latin letters.SGModSection *SGLyricsSourcesSection(BOOL namingSource);SGModRow *SGLockScreenLyricsRow(void);SGModRow *SGLyricsTranslationLanguageRow(void);SGModRow *SGLyricsWordTimingRow(void);+SGModRow *SGLyricsRomanisedRow(void);++// Shows a line written in another alphabet (Japanese, Korean, Chinese, Cyrillic, Greek, Thai, Arabic,+// Hebrew, Devanagari...) again under itself in Latin letters, before its translation. Off by default.+// Only the redesign's lyrics view draws it, and it applies at once: the switch posts the notification.+#define SGKeyLyricsRomanised @"spotifyglass.lyricsRomanised"+extern NSNotificationName const SGLyricsRomanisedDidChangeNotification;+// Romanise.m: the text in Latin letters, offline; nil where it has no letter of another script, or+// reads the same. `japanese` reads kanji the Japanese way, for a song SGLyricsLooksJapanese says is+// (a line of kanji alone would read as Chinese). Safe off the main thread, and kept per text.+NSString *SGLyricsRomanised(NSString *text, BOOL japanese);+// Whether any of the lines has kana in it.+@class SGKaraokeLine;+BOOL SGLyricsLooksJapanese(NSArray<SGKaraokeLine *> *lines);// Sweeps a line timed only by the line word by word, on the estimate of when each word is sung, as if// the source had timed them. Off, such a line lights up whole as it starts. Off by default: thediff --git a/tweak/Sources/Shared/Lyrics/LyricsSettings.m b/tweak/Sources/Shared/Lyrics/LyricsSettings.mindex a348b3e6..6039cefe 100644--- a/tweak/Sources/Shared/Lyrics/LyricsSettings.m+++ b/tweak/Sources/Shared/Lyrics/LyricsSettings.m@@ -43,6 +43,15 @@ SGModRow *SGLyricsTranslationLanguageRow(void) {return row;}+// Only the redesign's lyrics view draws it, and every one there is redrawn as the switch flips.+SGModRow *SGLyricsRomanisedRow(void) {+ SGModRow *row = SGOptionRow(@"Romanised lyrics", @"Lines in other alphabets, again in Latin letters", SGKeyLyricsRomanised);+ row.changed = ^(BOOL on) {+ [NSNotificationCenter.defaultCenter postNotificationName:SGLyricsRomanisedDidChangeNotification object:nil];+ };+ return row;+}+// Only the redesign's lyrics view sweeps words.SGModRow *SGLyricsWordTimingRow(void) {return SGOptionRow(@"Simulate word timing", nil, SGKeyLyricsSimulateWords);diff --git a/tweak/Sources/Shared/Lyrics/Romanise.m b/tweak/Sources/Shared/Lyrics/Romanise.mnew file mode 100644index 00000000..e69c32c5--- /dev/null+++ b/tweak/Sources/Shared/Lyrics/Romanise.m@@ -0,0 +1,70 @@+// A line in the Latin alphabet, offline, by Apple's own transforms (ICU's under them). Japanese goes+// through the tokenizer's Latin transcription, which reads kanji the Japanese way: Any-Latin alone+// reads them as Mandarin (君の名前 as "jūnno míng qián" instead of "kimi no namae"). Pinyin keeps its+// tone marks; every other script is brought down to plain ASCII, which reads better sung along to+// than ICU's scholarly marks (Greek "Kalēméra", Thai "s̄wạs̄dī").+//+// ponytail: ICU transliterates rather than reads aloud: Arabic and Hebrew come out as their consonants+// ("mrhba"), Thai in ISO 11940's spelling rather than the one signs use, and a line mixing Latin with+// another script loses the Latin part's accents ("Café" as "Cafe"). A reading per language is the upgrade.+#import "Lyrics.h"++NSNotificationName const SGLyricsRomanisedDidChangeNotification = @"spotifyglass.lyricsRomanisedDidChange";++static BOOL has(NSString *text, NSString *pattern) {+ return [text rangeOfString:pattern options:NSRegularExpressionSearch].location != NSNotFound;+}++static BOOL hasKana(NSString *text) {+ return has(text, @"[\\p{Hiragana}\\p{Katakana}]");+}++BOOL SGLyricsLooksJapanese(NSArray<SGKaraokeLine *> *lines) {+ for (SGKaraokeLine *line in lines) {+ if (hasKana(SGKaraokeLineText(line))) return YES;+ }+ return NO;+}++// The tokenizer's reading of each word, a space between words; what it has no reading for (punctuation,+// another script) is kept for the transform after it.+static NSString *readJapanese(NSString *text) {+ NSMutableString *read = [NSMutableString string];+ CFLocaleRef japanese = CFLocaleCreate(NULL, CFSTR("ja"));+ CFStringTokenizerRef words = CFStringTokenizerCreate(NULL, (__bridge CFStringRef)text, CFRangeMake(0, text.length),+ kCFStringTokenizerUnitWord, japanese);+ NSUInteger from = 0;+ while (CFStringTokenizerAdvanceToNextToken(words) != kCFStringTokenizerTokenNone) {+ CFRange range = CFStringTokenizerGetCurrentTokenRange(words);+ NSString *between = [text substringWithRange:NSMakeRange(from, range.location - from)];+ NSString *word = CFBridgingRelease(CFStringTokenizerCopyCurrentTokenAttribute(words, kCFStringTokenizerAttributeLatinTranscription));+ if (read.length && !between.length) [read appendString:@" "];+ [read appendString:between];+ [read appendString:word ?: [text substringWithRange:NSMakeRange(range.location, range.length)]];+ from = range.location + range.length;+ }+ [read appendString:[text substringFromIndex:from]];+ CFRelease(words);+ CFRelease(japanese);+ return read;+}++NSString *SGLyricsRomanised(NSString *text, BOOL japanese) {+ // Only a line with a letter of another script: Latin with accents (German, Vietnamese) is left alone.+ if (!text.length || !has(text, @"[\\p{L}&&[^\\p{Latin}]]")) return nil;+ static NSCache<NSString *, NSString *> *kept;+ static dispatch_once_t once;+ dispatch_once(&once, ^{ kept = [NSCache new]; });+ NSString *key = japanese ? [@"ja:" stringByAppendingString:text] : text;+ NSString *romanised = [kept objectForKey:key];+ if (romanised) return romanised.length ? romanised : nil;+ BOOL kana = japanese || hasKana(text);+ NSString *source = kana ? readJapanese(text) : text;+ // Han left over is Chinese: its pinyin keeps the tones, and only its punctuation is made ASCII.+ NSString *transform = !kana && has(text, @"\\p{Han}") ? @"Any-Latin; [[:P:][:Zs:]] Latin-ASCII" : @"Any-Latin; Latin-ASCII";+ romanised = [[source stringByApplyingTransform:transform reverse:NO]+ stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet] ?: @"";+ if ([romanised caseInsensitiveCompare:text] == NSOrderedSame) romanised = @"";+ [kept setObject:romanised forKey:key];+ return romanised.length ? romanised : nil;+}