From 506c29a32dc57379469d69df8b0895c32dc5e04f Mon Sep 17 00:00:00 2001 From: miteruzo Date: Fri, 3 May 2024 16:35:14 +0900 Subject: [PATCH] =?UTF-8?q?[BC]=20=E8=A9=B3=E7=B4=B0=E7=94=BB=E9=9D=A2=20?= =?UTF-8?q?=E6=94=B9=E8=A1=8C=E5=8F=AF=E8=83=BD=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- broadcast/common_module.js | 44 ++++++++++++++++++++++++++++++++++++++ broadcast/talk.frm.php | 2 +- broadcast/talk.js | 24 +++++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 broadcast/common_module.js diff --git a/broadcast/common_module.js b/broadcast/common_module.js new file mode 100644 index 0000000..cacf16d --- /dev/null +++ b/broadcast/common_module.js @@ -0,0 +1,44 @@ +export default class +CommonModule +{ + static + isWide (chr) + { + return chr.match (/[^\x01-\x7f]/) !== null; + } + + static + lenByFull (str) + { + return str.split ('').map (c => this.isWide (c) ? 1 : .5) + .reduce ((a, c) => a + c); + } + + static + indexByFToC (str, index) + { + let i = 0; + let work = ''; + + for (let c of str) + { + work += c; + + if (this.lenByFull (work) > index) + break; + + i += 1; + } + + return i; + } + + static + midByFull (str, start, length) + { + const trimmedLeft = str.slice (this.indexByFToC (str, start)); + + return trimmedLeft.slice (0, this.indexByFToC (trimmedLeft, length)); + } +} + diff --git a/broadcast/talk.frm.php b/broadcast/talk.frm.php index 22f5af4..3e0e1cb 100644 --- a/broadcast/talk.frm.php +++ b/broadcast/talk.frm.php @@ -17,7 +17,7 @@ - + diff --git a/broadcast/talk.js b/broadcast/talk.js index 56e8c33..517a036 100644 --- a/broadcast/talk.js +++ b/broadcast/talk.js @@ -1,3 +1,6 @@ +import CommonModule from './common_module.js'; + + class Talk { @@ -46,10 +49,27 @@ Canvas this.putText (0, 0, 15, $ ('#dt').val ()); this.putImage (this.nizika, 370, 260, 1.1); this.putImage (this.talking, 0, 0, 640 / 1024); - this.putText (75, 43.75, 20, '> ' + $ ('#chat').val (), + this.putText (75, 43.75, 20, + ('> ' + ((CommonModule.lenByFull ($ ('#chat').val ()) <= 21) + ? $ ('#chat').val () + : (CommonModule.midByFull ($ ('#chat').val (), 0, 19.5) + + '...'))), undefined, undefined, true); - this.putText (62.5, 93.75, 31.25, $ ('#answer').val (), 'Nikumaru', '#c00000'); + this.putText (62.5, 93.75, 31.25, + ((CommonModule.lenByFull ($ ('#answer').val ()) <= 16) + ? $ ('#answer').val () + : CommonModule.midByFull ($ ('#answer').val (), 0, 16)), + 'Nikumaru', '#c00000'); + if (CommonModule.lenByFull ($ ('#answer').val ()) > 16) + { + this.putText (62.5, 125, 31.25, + ((CommonModule.lenByFull ($ ('#answer').val ()) <= 32) + ? CommonModule.midByFull ($ ('#answer').val (), 16, 16) + : (CommonModule.midByFull ($ ('#answer').val (), 16, 14.5) + + '...')), + 'Nikumaru', '#c00000'); + } } putBG ()