Skip to content

Commit 3bc0ea0

Browse files
authored
Preserve unicode encoding when downgrading to ID3v2.3 (#1259) (#1260)
1 parent 225c73e commit 3bc0ea0

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

taglib/mpeg/id3v2/id3v2tag.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ namespace
5656

5757
constexpr long MinPaddingSize = 1024;
5858
constexpr long MaxPaddingSize = 1024 * 1024;
59+
60+
/*!
61+
* Downgrade ID3v2.4 text \a encoding to value supported by ID3v2.3.
62+
*/
63+
String::Type downgradeTextEncoding(String::Type encoding)
64+
{
65+
return encoding == String::Latin1 ? String::Latin1 : String::UTF16;
66+
}
67+
68+
/*!
69+
* Downgrade ID3v2.4 text encoding to value supported by ID3v2.3.
70+
* \param frame1 first contributing ID3v2.4 frame, can be null
71+
* \param frame2 second contributing ID3v2.4 frame, can be null
72+
* \return ID3v2.3 encoding suitable for both contributing source frames.
73+
*/
74+
String::Type downgradeTextEncoding(ID3v2::TextIdentificationFrame *frame1,
75+
ID3v2::TextIdentificationFrame *frame2)
76+
{
77+
return (!frame1 || frame1->textEncoding() == String::Latin1) &&
78+
(!frame2 || frame2->textEncoding() == String::Latin1)
79+
? String::Latin1 : String::UTF16;
80+
}
5981
} // namespace
6082

6183
class ID3v2::Tag::TagPrivate
@@ -578,7 +600,8 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const
578600
String content = frameTDOR->toString();
579601

580602
if(content.size() >= 4) {
581-
auto frameTORY = new ID3v2::TextIdentificationFrame("TORY", String::Latin1);
603+
auto frameTORY = new ID3v2::TextIdentificationFrame(
604+
"TORY", downgradeTextEncoding(frameTDOR->textEncoding()));
582605
frameTORY->setText(content.substr(0, 4));
583606
frames->append(frameTORY);
584607
newFrames->append(frameTORY);
@@ -587,17 +610,20 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const
587610

588611
if(frameTDRC) {
589612
if(String content = frameTDRC->toString(); content.size() >= 4) {
590-
auto frameTYER = new ID3v2::TextIdentificationFrame("TYER", String::Latin1);
613+
auto frameTYER = new ID3v2::TextIdentificationFrame(
614+
"TYER", downgradeTextEncoding(frameTDRC->textEncoding()));
591615
frameTYER->setText(content.substr(0, 4));
592616
frames->append(frameTYER);
593617
newFrames->append(frameTYER);
594618
if(content.size() >= 10 && content[4] == '-' && content[7] == '-') {
595-
auto frameTDAT = new ID3v2::TextIdentificationFrame("TDAT", String::Latin1);
619+
auto frameTDAT = new ID3v2::TextIdentificationFrame(
620+
"TDAT", downgradeTextEncoding(frameTDRC->textEncoding()));
596621
frameTDAT->setText(content.substr(8, 2) + content.substr(5, 2));
597622
frames->append(frameTDAT);
598623
newFrames->append(frameTDAT);
599624
if(content.size() >= 16 && content[10] == 'T' && content[13] == ':') {
600-
auto frameTIME = new ID3v2::TextIdentificationFrame("TIME", String::Latin1);
625+
auto frameTIME = new ID3v2::TextIdentificationFrame(
626+
"TIME", downgradeTextEncoding(frameTDRC->textEncoding()));
601627
frameTIME->setText(content.substr(11, 2) + content.substr(14, 2));
602628
frames->append(frameTIME);
603629
newFrames->append(frameTIME);
@@ -607,7 +633,8 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const
607633
}
608634

609635
if(frameTIPL || frameTMCL) {
610-
auto frameIPLS = new ID3v2::TextIdentificationFrame("IPLS", String::Latin1);
636+
auto frameIPLS = new ID3v2::TextIdentificationFrame(
637+
"IPLS", downgradeTextEncoding(frameTIPL, frameTMCL));
611638

612639
StringList people;
613640

@@ -653,7 +680,8 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const
653680
if(!genreText.isEmpty())
654681
combined += genreText;
655682

656-
frameTCON = new ID3v2::TextIdentificationFrame("TCON", String::Latin1);
683+
frameTCON = new ID3v2::TextIdentificationFrame(
684+
"TCON", downgradeTextEncoding(frameTCON->textEncoding()));
657685
frameTCON->setText(combined);
658686
frames->append(frameTCON);
659687
newFrames->append(frameTCON);

0 commit comments

Comments
 (0)