Fix: split multibyte sentence with unicodeIndex

This commit is contained in:
2019-06-25 11:10:57 +09:00
parent cb9ac3a017
commit 5638266178
4 changed files with 144 additions and 37 deletions
+48
View File
@@ -177,6 +177,54 @@ StringUtil.isSameKoreanAlphabet = function(firstAlphabet, secondAlphabet) {
return false;
};
StringUtil.isKoreanAlphabet = function(char) {
c = char.charCodeAt(0);
if(0x1100 <= c && c <= 0x11FF)
return true;
if(0x3130 <= c && c <= 0x318F)
return true;
if(0xAC00 <= c && c <= 0xD7A3)
return true;
return false;
};
StringUtil.getUnicodeAlphabetCount = function(text) {
var count = 0;
var textLen = text.length
for(var i = 0; i < textLen; i++) {
if(StringUtil.isKoreanAlphabet(text[i]))
count += 2;
else
count++
}
return count;
};
StringUtil.getMultibyteIndex = function(multibyteText, unicodeCharIndex) {
var multibyteTextIndex = 0;
var index = 0;
while(index < unicodeCharIndex) {
var unicodeChar = multibyteText[multibyteTextIndex];
if(typeof unicodeChar == "undefined")
break;
if(StringUtil.isKoreanAlphabet(unicodeChar)) {
if(index + 2 <= unicodeCharIndex)
index += 2;
else
break;
} else
index += 1;
multibyteTextIndex++;
}
return multibyteTextIndex;
};
StringUtil.getScoreUnit = function(playingAppID) {
if(playingAppID == 104)
return " 초";
+56 -25
View File
@@ -108,43 +108,73 @@ TypingTextManager.prototype.makeExaminationContents = function(arr) {
if(sentence.length == 0)
continue;
if(sentence.length > TypingTextManager.SENTENCE_MAX_CHARACTER)
this.add(this.makeShortSentences(sentence));
this.add(this.getSplitedSentences(sentence));
else
this.add(sentence);
}
}
TypingTextManager.prototype.makeShortSentences = function(sentence) {
TypingTextManager.prototype.isWordTooLong = function(word) {
var uniCharCount = StringUtil.getUnicodeAlphabetCount(word);
// console.log("word : " + word + "*");
// console.log("uniCharCount : " + uniCharCount);
if(uniCharCount > TypingTextManager.SENTENCE_MAX_CHARACTER)
return true;
return false;
}
TypingTextManager.prototype.isNotEmptySentence = function(sentence) {
if(sentence.legnth > 0)
return true;
return false;
}
/*
TypingTextManager.prototype.getSplitedWord = function(word, splitIndex) {
var startIndex = splitIndex * TypingTextManager.SENTENCE_MAX_CHARACTER;
var endIndex = (splitIndex + 1) * TypingTextManager.SENTENCE_MAX_CHARACTER;
return word.substring(startIndex, endIndex);
}
*/
TypingTextManager.prototype.getSplitedSentences = function(sentence) {
var words = sentence.split(" ");
var wordCount = words.length;
var shortSentences = [];
var splitedSentences = [];
var sentenceIndex = 0;
shortSentences[sentenceIndex] = "";
splitedSentences[sentenceIndex] = "";
// console.log(sentence);
// console.log(words);
for(var i = 0; i < wordCount; i++) {
// console.log(words[i]);
// console.log(shortSentences[sentenceIndex].length);
// console.log(splitedSentences[sentenceIndex].length);
var wordUniCharCount = StringUtil.getUnicodeAlphabetCount(words[i]);
// if a word is longer than SENTENCE_MAX_CHARACTER
// then split the word with SENTENCE_MAX_CHARACTER length
if(this.isWordTooLong(words[i])) {
console.log(words[i]);
if(this.isNotEmptySentence(splitedSentences[sentenceIndex]))
sentenceIndex++; // start with new empty sentence
var wordLength = words[i].length;
if(wordLength > TypingTextManager.SENTENCE_MAX_CHARACTER) {
// console.log(words[i]);
if(shortSentences[sentenceIndex].legnth > 0)
sentenceIndex++;
var tooLongWord = words[i];
while(typeof tooLongWord != "undefined" && tooLongWord.length > 0) {
// console.log("tooLongWord before : " + tooLongWord);
var loopCount = Math.ceil(wordLength / TypingTextManager.SENTENCE_MAX_CHARACTER);
var multibyteIndex = StringUtil.getMultibyteIndex(tooLongWord, TypingTextManager.SENTENCE_MAX_CHARACTER);
// console.log("multibyteIndex : " + multibyteIndex);
var splitedSentence = tooLongWord.substring(0, multibyteIndex);
// console.log("splitedSentence : " + splitedSentence);
tooLongWord = tooLongWord.substring(multibyteIndex);
// console.log("tooLongWord : after " + tooLongWord);
for(var j = 0; j < loopCount; j++) {
var startIndex = j * TypingTextManager.SENTENCE_MAX_CHARACTER;
var endIndex = (j + 1) * TypingTextManager.SENTENCE_MAX_CHARACTER;
var partialText = words[i].substring(startIndex, endIndex);
// console.log(partialText);
shortSentences[sentenceIndex] = partialText;
splitedSentences[sentenceIndex] = splitedSentence;
sentenceIndex++;
}
@@ -152,22 +182,23 @@ TypingTextManager.prototype.makeShortSentences = function(sentence) {
}
if(shortSentences[sentenceIndex].length + words[i].length < TypingTextManager.SENTENCE_MAX_CHARACTER)
// console.log("splitedSentences[" + sentenceIndex + "] : " + splitedSentences[sentenceIndex]);
var shortSentenceUniCharCount = StringUtil.getUnicodeAlphabetCount(splitedSentences[sentenceIndex]);
if(shortSentenceUniCharCount + wordUniCharCount < TypingTextManager.SENTENCE_MAX_CHARACTER)
if(i < wordCount - 1)
shortSentences[sentenceIndex] = shortSentences[sentenceIndex].concat(words[i], " ");
splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i], " ");
else // last word
shortSentences[sentenceIndex] = shortSentences[sentenceIndex].concat(words[i]);
splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i]);
else {
sentenceIndex++;
if(i < wordCount - 1)
shortSentences[sentenceIndex] = words[i] + " ";
splitedSentences[sentenceIndex] = words[i] + " ";
else // last word
shortSentences[sentenceIndex] = words[i];
splitedSentences[sentenceIndex] = words[i];
}
}
return shortSentences;
return splitedSentences;
}
@@ -205,4 +236,4 @@ TypingTextManager.ENGLISH_LETTER_RIGHT = [
];
TypingTextManager.SENTENCE_MAX_CHARACTER = 28;
TypingTextManager.SENTENCE_MAX_CHARACTER = 40;
+1 -1
View File
@@ -45,7 +45,7 @@
<script src="../../game/lib/util/number_util.js"></script>
<script src="../../game/lib/util/record_util.js"></script>
<script src="../../game/lib/text/input_type_text.js"></script>
<script src="../../game/lib/text/input_type_text.js?update=20190622"></script>
<script src="../../game/lib/text/time_over_text.js"></script>
<script src="../../game/lib/text/mission_clear_text.js"></script>
<script src="../../game/lib/text/screen_top_ui.js"></script>