From 7a8333f91550da4471b792b12bfb8e1ef535f24d Mon Sep 17 00:00:00 2001 From: duorameng <2997944583@qq.com> Date: Thu, 30 Apr 2026 09:01:02 +0800 Subject: [PATCH] chore: update sentence loading error --- internal/constant/sentence.go | 35 ++++++++++++++++++++--------- web/src/assets/index.css | 30 ++++++++++++++++++------- web/src/components/TextOverflow.vue | 4 ++-- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/internal/constant/sentence.go b/internal/constant/sentence.go index 083e185..8e8974a 100644 --- a/internal/constant/sentence.go +++ b/internal/constant/sentence.go @@ -1,7 +1,6 @@ package constant import ( - "bufio" "bytes" _ "embed" "encoding/json" @@ -26,13 +25,25 @@ var ( // initSentences 初始化:统计行数和记录每行偏移 func initSentences() { sentenceOnce.Do(func() { - scanner := bufio.NewScanner(bytes.NewReader(sentenceData)) + data := sentenceData var offset int64 = 0 - for scanner.Scan() { + for { + // 记录当前行的起始位置 lineOffsets = append(lineOffsets, offset) - // scanner.Bytes() returns the line without newline - offset += int64(len(scanner.Bytes())) + 1 // +1 for newline lineCount++ + + // 寻找下一个换行符 + idx := bytes.IndexByte(data[offset:], '\n') + if idx == -1 { + // 最后一行没有换行符 + break + } + + // 移动到下一行的起始位置 + offset += int64(idx) + 1 + if offset >= int64(len(data)) { + break + } } }) } @@ -40,24 +51,26 @@ func initSentences() { // GetRandomSentence 随机获取一条古诗词 func GetRandomSentence() string { initSentences() - if lineCount == 0 { + if lineCount <= 0 { return "欢迎使用白虎面板" } targetIndex := rand.Intn(lineCount) start := lineOffsets[targetIndex] - // 找到行结束位置(通过下一个偏移量或文件末尾) + // 确定当前行的结束位置 var end int64 if targetIndex < lineCount-1 { - end = lineOffsets[targetIndex+1] - 1 // -1 移除换行符 + end = lineOffsets[targetIndex+1] } else { end = int64(len(sentenceData)) } - // 确保不越界并移除末尾可能的回车符 (Windows \r\n) - line := sentenceData[start:end] - line = bytes.TrimRight(line, "\r\n") + // 提取行并清理两端的空白字符(包括 \r, \n) + line := bytes.TrimSpace(sentenceData[start:end]) + if len(line) == 0 { + return "欢迎使用白虎面板" + } var sData []string if err := json.Unmarshal(line, &sData); err == nil && len(sData) >= 1 { diff --git a/web/src/assets/index.css b/web/src/assets/index.css index e4a947e..bebd82f 100644 --- a/web/src/assets/index.css +++ b/web/src/assets/index.css @@ -95,12 +95,12 @@ --sidebar-border: #e5e7eb; --sidebar-ring: #9ca3af; - /* 字体栈定义 */ - --font-main: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; + /* 字体栈定义 - 优先使用 Inter Variable 以获得最佳的可变字体支持 */ + --font-main: 'Inter Variable', 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; } .is-windows { - --font-main: 'Inter', 'Noto Sans SC', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; + --font-main: 'Inter Variable', 'Inter', 'Noto Sans SC', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; } /* OKLCH support targeting modern browsers */ @@ -216,12 +216,23 @@ } body { - @apply bg-background text-foreground antialiased; - /* 增强字体栈:根据平台差异化 Noto Sans SC 与 system-ui 的优先级 */ + @apply bg-background text-foreground; + /* 增强字体栈与高级特性优化 */ font-family: var(--font-main); font-size: 14px; line-height: 1.5; - letter-spacing: -0.01em; + letter-spacing: -0.011em; + + /* Inter 字体高级特性优化: + cv05: l 增加尾钩 (提升识别) + cv08: G 增加尖角 + cv11: 单层小写 a + ss01: 开口数字 (更现代) + ss03: 圆形 0 + */ + font-feature-settings: "cv05", "cv08", "cv11", "ss01", "ss03"; + font-optical-sizing: auto; + -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-rendering: optimizeLegibility; @@ -247,8 +258,11 @@ body { font-size: 15.5px; line-height: 1.55; - letter-spacing: -0.01em; - /* 移动端使用 antialiased 以匹配高 DPI 渲染 */ + letter-spacing: -0.011em; + /* 移动端增强字体渲染 */ + font-family: var(--font-main); + font-feature-settings: "cv05", "cv08", "cv11", "ss01", "ss03"; + font-optical-sizing: auto; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } diff --git a/web/src/components/TextOverflow.vue b/web/src/components/TextOverflow.vue index 3b72bc1..0087002 100644 --- a/web/src/components/TextOverflow.vue +++ b/web/src/components/TextOverflow.vue @@ -22,7 +22,7 @@ function handleClick() {