diff --git a/src/tools/text-to-styled-letters/text-to-styled-letters.constants.ts b/src/tools/text-to-styled-letters/text-to-styled-letters.constants.ts index 64f18a77..9b1a2d4a 100644 --- a/src/tools/text-to-styled-letters/text-to-styled-letters.constants.ts +++ b/src/tools/text-to-styled-letters/text-to-styled-letters.constants.ts @@ -1,4 +1,65 @@ -export const styledLetters = { +interface StyledLetterMap { [key: string]: string[] } + +export type StyleType = + | 'bold' + | 'italic' + | 'boldItalic' + | 'script' + | 'boldScript' + | 'fraktur' + | 'doubleStruck' + | 'sansSerif' + | 'sansSerifBold' + | 'sansSerifItalic' + | 'sansSerifBoldItalic' + | 'monospace'; + +export const STYLE_INDEX: Record = { + bold: 0, + italic: 1, + boldItalic: 2, + script: 3, + boldScript: 4, + fraktur: 5, + doubleStruck: 6, + sansSerif: 7, + sansSerifBold: 8, + sansSerifItalic: 9, + sansSerifBoldItalic: 10, + monospace: 11, +}; + +export const DIGIT_STYLE_MAP: Record = { + bold: 0, + italic: null, + boldItalic: 0, + script: null, + boldScript: null, + fraktur: null, + doubleStruck: 1, + sansSerifBold: 3, + sansSerifBoldItalic: 3, + sansSerif: 2, + sansSerifItalic: 2, + monospace: 4, +}; + +export const GREEK_STYLE_MAP: Record = { + bold: 0, + italic: 1, + boldItalic: 2, + script: null, + boldScript: null, + fraktur: null, + doubleStruck: null, + sansSerifBold: 3, + sansSerifBoldItalic: 4, + sansSerif: null, + sansSerifItalic: 1, + monospace: null, +}; + +export const styledLetters: StyledLetterMap = { A: ['๐€', '๐ด', '๐‘จ', '๐’œ', '๐“', '๐•ฌ', '๐”ธ', '๐– ', '๐—”', '๐˜ˆ', '๐˜ผ', '๐™ฐ'], B: ['๐', '๐ต', '๐‘ฉ', 'โ„ฌ', '๐“‘', '๐•ญ', '๐”น', '๐–ก', '๐—•', '๐˜‰', '๐˜ฝ', '๐™ฑ'], C: ['๐‚', '๐ถ', '๐‘ช', '๐’ž', '๐“’', '๐•ฎ', 'โ„‚', '๐–ข', '๐—–', '๐˜Š', '๐˜พ', '๐™ฒ'], @@ -54,7 +115,7 @@ export const styledLetters = { z: ['๐ณ', '๐‘ง', '๐’›', '๐“', '๐”ƒ', '๐–Ÿ', '๐•ซ', '๐—“', '๐˜‡', '๐˜ป', '๐™ฏ', '๐šฃ'], }; -export const styledDigits = { +export const styledDigits: StyledLetterMap = { 0: ['๐ŸŽ', '๐Ÿ˜', '๐Ÿข', '๐Ÿฌ', '๐Ÿถ'], 1: ['๐Ÿ', '๐Ÿ™', '๐Ÿฃ', '๐Ÿญ', '๐Ÿท'], 2: ['๐Ÿ', '๐Ÿš', '๐Ÿค', '๐Ÿฎ', '๐Ÿธ'], @@ -67,7 +128,7 @@ export const styledDigits = { 9: ['๐Ÿ—', '๐Ÿก', '๐Ÿซ', '๐Ÿต', '๐Ÿฟ'], }; -export const styledGreek = { +export const styledGreek: StyledLetterMap = { 'ฮ‘': ['๐šจ', '๐›ข', '๐œœ', '๐–', '๐ž'], // Alpha 'ฮ’': ['๐šฉ', '๐›ฃ', '๐œ', '๐—', '๐ž‘'], // Beta 'ฮ“': ['๐šช', '๐›ค', '๐œž', '๐˜', '๐ž’'], // Gamma diff --git a/src/tools/text-to-styled-letters/text-to-styled-letters.service.ts b/src/tools/text-to-styled-letters/text-to-styled-letters.service.ts index fefb9cc1..a2028ce5 100644 --- a/src/tools/text-to-styled-letters/text-to-styled-letters.service.ts +++ b/src/tools/text-to-styled-letters/text-to-styled-letters.service.ts @@ -1,56 +1,9 @@ -import { styledDigits, styledGreek, styledLetters } from './text-to-styled-letters.constants'; +import type { StyleType } from './text-to-styled-letters.constants'; +import { DIGIT_STYLE_MAP, GREEK_STYLE_MAP, STYLE_INDEX, styledDigits, styledGreek, styledLetters } from './text-to-styled-letters.constants'; export { textToStyledLetters }; export type { StyleType }; -// ๆ ทๅผ็ฑปๅž‹ๅฎšไน‰ -type StyleType = - | 'bold' // ็ฒ—ไฝ“ - | 'italic' // ๆ–œไฝ“ - | 'boldItalic' // ็ฒ—ๆ–œไฝ“ - | 'script' // ๆ‰‹ๅ†™ไฝ“ - | 'boldScript' // ็ฒ—ๆ‰‹ๅ†™ไฝ“ - | 'fraktur' // ๅ“ฅ็‰นไฝ“/Fraktur - | 'doubleStruck' // ๅŒ็บฟไฝ“/้ป‘ๆฟ็ฒ—ไฝ“ - | 'sansSerif' // ๆ— ่กฌ็บฟ - | 'sansSerifBold' // ๆ— ่กฌ็บฟ็ฒ—ไฝ“ - | 'sansSerifItalic'// ๆ— ่กฌ็บฟๆ–œไฝ“ - | 'sansSerifBoldItalic' // ๆ— ่กฌ็บฟ็ฒ—ๆ–œไฝ“ - | 'monospace'; // ็ญ‰ๅฎฝ - -// ๆ ทๅผ็ดขๅผ•ๆ˜ ๅฐ„ -const STYLE_INDEX: Record = { - bold: 0, - italic: 1, - boldItalic: 2, - script: 3, - boldScript: 4, - fraktur: 5, - doubleStruck: 6, - sansSerif: 7, - sansSerifBold: 8, - sansSerifItalic: 9, - sansSerifBoldItalic: 10, - monospace: 11, -}; - -// ๅธŒ่…Šๅญ—ๆฏๅชๆœ‰5็งๆ ทๅผ๏ผŒ้œ€่ฆ็‰นๆฎŠๅค„็† -const GREEK_STYLE_MAP: Record = { - bold: 0, - italic: 1, - boldItalic: 2, - script: null, // ๅธŒ่…Šๅญ—ๆฏๆ— ๆญคๆ ทๅผ - boldScript: null, // ๅธŒ่…Šๅญ—ๆฏๆ— ๆญคๆ ทๅผ - fraktur: null, // ๅธŒ่…Šๅญ—ๆฏๆ— ๆญคๆ ทๅผ - doubleStruck: null, // ๅธŒ่…Šๅญ—ๆฏๆ— ๆญคๆ ทๅผ - sansSerifBold: 3, // ๆ˜ ๅฐ„ๅˆฐ sans-serif bold - sansSerifBoldItalic: 4, // ๆ˜ ๅฐ„ๅˆฐ sans-serif bold italic - // ไปฅไธ‹ๆ˜ ๅฐ„ๅˆฐๆœ€ๆŽฅ่ฟ‘็š„ๅฏ็”จๆ ทๅผ - sansSerif: 3, // ๆ—  sans-serif regular๏ผŒ็”จ bold ไปฃๆ›ฟ - sansSerifItalic: 4, // ๆ—  sans-serif italic๏ผŒ็”จ bold italic ไปฃๆ›ฟ - monospace: null, // ๅธŒ่…Šๅญ—ๆฏๆ— ็ญ‰ๅฎฝ -}; - function textToStyledLetters({ text, style, @@ -59,43 +12,30 @@ function textToStyledLetters({ style: StyleType }): string { const latinIndex = STYLE_INDEX[style]; + const digitIndex = DIGIT_STYLE_MAP[style]; const greekIndex = GREEK_STYLE_MAP[style]; + const converters = [ + { map: styledLetters, index: latinIndex }, + { map: styledDigits, index: digitIndex }, + { map: styledGreek, index: greekIndex }, + ]; + let result = ''; for (const char of text) { - let converted = false; + let styled: string | undefined; - // ๅฐ่ฏ•ๅŒน้…ๆ‹‰ไธๅญ—ๆฏ๏ผˆๅคงๅ†™ๆˆ–ๅฐๅ†™๏ผ‰ - if (styledLetters[char]) { - const styled = styledLetters[char][latinIndex]; - if (styled) { - result += styled; - converted = true; - } - } - // ๅฐ่ฏ•ๅŒน้…ๆ•ฐๅญ— - else if (styledDigits[char]) { - // ๆ•ฐๅญ—ๅชๆœ‰7็งๆ ทๅผ๏ผˆ็ดขๅผ•0-6๏ผ‰๏ผŒ่ถ…ๅ‡บ่Œƒๅ›ด้œ€่ฆๅค„็† - const digitIndex = latinIndex <= 6 ? latinIndex : null; - if (digitIndex !== null && styledDigits[char][digitIndex]) { - result += styledDigits[char][digitIndex]; - converted = true; - } - } - // ๅฐ่ฏ•ๅŒน้…ๅธŒ่…Šๅญ—ๆฏ - else if (styledGreek[char] && greekIndex !== null) { - const styled = styledGreek[char][greekIndex]; - if (styled) { - result += styled; - converted = true; + for (const { map, index } of converters) { + if (index !== null) { + styled = map[char]?.[index]; + if (styled) { + break; + } } } - // ๆ— ๆณ•่ฝฌๆข๏ผŒ็…งๆŠ„ๅŽŸๅญ—็ฌฆ - if (!converted) { - result += char; - } + result += styled || char; } return result; diff --git a/src/tools/text-to-styled-letters/text-to-styled-letters.vue b/src/tools/text-to-styled-letters/text-to-styled-letters.vue index dae4be29..b428a002 100644 --- a/src/tools/text-to-styled-letters/text-to-styled-letters.vue +++ b/src/tools/text-to-styled-letters/text-to-styled-letters.vue @@ -7,18 +7,18 @@ const input = ref(''); const selectedStyle = ref('bold'); const styleOptions: { label: string; value: StyleType }[] = [ - { label: 'Bold', value: 'bold' }, - { label: 'Italic', value: 'italic' }, - { label: 'Bold Italic', value: 'boldItalic' }, - { label: 'Script', value: 'script' }, - { label: 'Bold Script', value: 'boldScript' }, - { label: 'Fraktur', value: 'fraktur' }, - { label: 'Double-Struck', value: 'doubleStruck' }, - { label: 'Sans-Serif', value: 'sansSerif' }, - { label: 'Sans-Serif Bold', value: 'sansSerifBold' }, - { label: 'Sans-Serif Italic', value: 'sansSerifItalic' }, - { label: 'Sans-Serif Bold Italic', value: 'sansSerifBoldItalic' }, - { label: 'Monospace', value: 'monospace' }, + { label: '๐›๐จ๐ฅ๐', value: 'bold' }, + { label: '๐‘–๐‘ก๐‘Ž๐‘™๐‘–๐‘', value: 'italic' }, + { label: '๐’ƒ๐’๐’๐’… ๐’Š๐’•๐’‚๐’๐’Š๐’„', value: 'boldItalic' }, + { label: '๐’ฎ๐’ธ๐“‡๐’พ๐“…๐“‰', value: 'script' }, + { label: '๐“‘๐“ธ๐“ต๐“ญ ๐“ข๐“ฌ๐“ป๐“ฒ๐“น๐“ฝ', value: 'boldScript' }, + { label: '๐”‰๐”ฏ๐”ž๐”จ๐”ฑ๐”ฒ๐”ฏ', value: 'fraktur' }, + { label: '๐”ป๐• ๐•ฆ๐•“๐•๐•–-๐•Š๐•ฅ๐•ฃ๐•ฆ๐•”๐•œ', value: 'doubleStruck' }, + { label: '๐–ฒ๐–บ๐—‡๐—Œ-๐–ฒ๐–พ๐—‹๐—‚๐–ฟ', value: 'sansSerif' }, + { label: '๐—ฆ๐—ฎ๐—ป๐˜€-๐—ฆ๐—ฒ๐—ฟ๐—ถ๐—ณ ๐—•๐—ผ๐—น๐—ฑ', value: 'sansSerifBold' }, + { label: '๐˜š๐˜ข๐˜ฏ๐˜ด-๐˜š๐˜ฆ๐˜ณ๐˜ช๐˜ง ๐˜๐˜ต๐˜ข๐˜ญ๐˜ช๐˜ค', value: 'sansSerifItalic' }, + { label: '๐™Ž๐™–๐™ฃ๐™จ-๐™Ž๐™š๐™ง๐™ž๐™› ๐˜ฝ๐™ค๐™ก๐™™ ๐™„๐™ฉ๐™–๐™ก๐™ž๐™˜', value: 'sansSerifBoldItalic' }, + { label: '๐™ผ๐š˜๐š—๐š˜๐šœ๐š™๐šŠ๐šŒ๐šŽ', value: 'monospace' }, ]; const styledText = computed(() =>