@papyrus-sdk/engine-native 0.2.28 → 0.2.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -59,6 +59,67 @@ static void PapyrusSyncOnMain(void (^block)(void)) {
|
|
|
59
59
|
|
|
60
60
|
static void PapyrusCleanupComicSources(NSString *engineId);
|
|
61
61
|
|
|
62
|
+
/// True when the non-ASCII bytes come almost only in high/high pairs, as in
|
|
63
|
+
/// GBK/GB18030 Chinese text. Latin-1 text does not qualify, so it keeps
|
|
64
|
+
/// failing instead of turning into Chinese mojibake. Mirrors
|
|
65
|
+
/// looksLikeDoubleByteCjk in nativeTextModel.ts.
|
|
66
|
+
static BOOL PapyrusLooksLikeDoubleByteCjk(NSData *data) {
|
|
67
|
+
const uint8_t *bytes = data.bytes;
|
|
68
|
+
NSUInteger length = data.length;
|
|
69
|
+
NSUInteger highPairs = 0;
|
|
70
|
+
NSUInteger other = 0;
|
|
71
|
+
NSUInteger index = 0;
|
|
72
|
+
while (index < length) {
|
|
73
|
+
uint8_t lead = bytes[index];
|
|
74
|
+
if (lead < 0x80) {
|
|
75
|
+
index += 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
int trail = index + 1 < length ? bytes[index + 1] : -1;
|
|
79
|
+
if (lead >= 0x81 && lead <= 0xfe && trail >= 0x81 && trail <= 0xfe) {
|
|
80
|
+
highPairs += 1;
|
|
81
|
+
index += 2;
|
|
82
|
+
} else {
|
|
83
|
+
other += 1;
|
|
84
|
+
index += (trail >= 0x40 && trail <= 0x7e) ? 2 : 1;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return highPairs > 0 && highPairs * 5 >= (highPairs + other) * 4;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Text that is not UTF-8 and has no BOM (old Chinese, Japanese, Korean or
|
|
91
|
+
/// Windows TXT files): the system detector picks among these encodings only,
|
|
92
|
+
/// without lossy conversion. Latin encodings are left out when the bytes look
|
|
93
|
+
/// like double-byte CJK; GB18030 is the last resort. Mirrors decodeLegacy in
|
|
94
|
+
/// PapyrusNativeTextModel.java.
|
|
95
|
+
static NSString *PapyrusDecodeLegacyTextData(NSData *data) {
|
|
96
|
+
BOOL cjkPairs = PapyrusLooksLikeDoubleByteCjk(data);
|
|
97
|
+
NSStringEncoding gb18030 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
|
|
98
|
+
NSMutableArray<NSNumber *> *candidates = [NSMutableArray arrayWithArray:@[
|
|
99
|
+
@(gb18030),
|
|
100
|
+
@(CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5)),
|
|
101
|
+
@(NSShiftJISStringEncoding),
|
|
102
|
+
@(NSJapaneseEUCStringEncoding),
|
|
103
|
+
@(CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR)),
|
|
104
|
+
]];
|
|
105
|
+
if (!cjkPairs) {
|
|
106
|
+
[candidates addObject:@(NSWindowsCP1252StringEncoding)];
|
|
107
|
+
[candidates addObject:@(NSISOLatin1StringEncoding)];
|
|
108
|
+
}
|
|
109
|
+
NSString *converted = nil;
|
|
110
|
+
BOOL lossy = NO;
|
|
111
|
+
NSStringEncoding detected = [NSString stringEncodingForData:data
|
|
112
|
+
encodingOptions:@{
|
|
113
|
+
NSStringEncodingDetectionSuggestedEncodingsKey: candidates,
|
|
114
|
+
NSStringEncodingDetectionUseOnlySuggestedEncodingsKey: @YES,
|
|
115
|
+
NSStringEncodingDetectionAllowLossyKey: @NO,
|
|
116
|
+
}
|
|
117
|
+
convertedString:&converted
|
|
118
|
+
usedLossyConversion:&lossy];
|
|
119
|
+
if (detected != 0 && converted && !lossy) return converted;
|
|
120
|
+
return cjkPairs ? [[NSString alloc] initWithData:data encoding:gb18030] : nil;
|
|
121
|
+
}
|
|
122
|
+
|
|
62
123
|
static NSString *PapyrusDecodeTextData(NSData *data, NSError **error) {
|
|
63
124
|
if (data.length >= 4) {
|
|
64
125
|
const uint8_t *bytes = data.bytes;
|
|
@@ -84,6 +145,9 @@ static NSString *PapyrusDecodeTextData(NSData *data, NSError **error) {
|
|
|
84
145
|
}
|
|
85
146
|
NSData *payload = offset ? [data subdataWithRange:NSMakeRange(offset, data.length - offset)] : data;
|
|
86
147
|
NSString *text = [[NSString alloc] initWithData:payload encoding:encoding];
|
|
148
|
+
if (!text && encoding == NSUTF8StringEncoding && offset == 0) {
|
|
149
|
+
text = PapyrusDecodeLegacyTextData(data);
|
|
150
|
+
}
|
|
87
151
|
if (!text && error) {
|
|
88
152
|
*error = [NSError errorWithDomain:@"PapyrusText" code:2 userInfo:@{NSLocalizedDescriptionKey: @"Invalid or unsupported text encoding"}];
|
|
89
153
|
}
|