From c1a79958626dcc74f54bf1a08e2f60d307cf25c7 Mon Sep 17 00:00:00 2001 From: Jixiong Su Date: Sun, 27 Oct 2024 22:20:15 +0800 Subject: [PATCH 1/3] :technologist: Add internal kernel API `/api/icon/getDynamicIcon` (#12939) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改locale参数为lang 新增weekdayType参数,支持设置weekday格式 --- kernel/api/icon.go | 453 +++++++++++++++++++++++++++++++++++++++++++ kernel/api/router.go | 3 +- 2 files changed, 455 insertions(+), 1 deletion(-) create mode 100644 kernel/api/icon.go diff --git a/kernel/api/icon.go b/kernel/api/icon.go new file mode 100644 index 000000000..c2d25b06b --- /dev/null +++ b/kernel/api/icon.go @@ -0,0 +1,453 @@ +// SiYuan - Refactor your thinking +// Copyright (c) 2020-present, b3log.org +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package api + +import ( + "fmt" + "math" + "net/http" + "regexp" + "strings" + "time" + + "github.com/gin-gonic/gin" + "github.com/siyuan-note/siyuan/kernel/util" +) + +type ColorScheme struct { + Primary string + Secondary string +} + +var colorSchemes = map[string]ColorScheme{ + "red": {"#DD2F45", "#F4BEC3"}, + "blue": {"#2bb7ff", "#0097e6"}, + "yellow": {"#feca57", "#ff9f43"}, + "green": {"#55efc4", "#19b37a"}, + "purple": {"#a55eea", "#8854d0"}, + "pink": {"#fd79a8", "#e05b8a"}, + "orange": {"#ff7f50", "#ff6348"}, + "grey": {"#576574", "#222f3e"}, +} + +func getColorScheme(color string) ColorScheme { + // 检查是否是预定义的颜色 + if scheme, ok := colorSchemes[strings.ToLower(color)]; ok { + return scheme + } + + // 如果不是预定义颜色,返回默认颜色 + return colorSchemes["red"] +} + +func getDynamicIcon(c *gin.Context) { + iconType := c.DefaultQuery("type", "1") + color := c.Query("color") // 不要预设默认值,不然type6返回星期就没法自动设置周末颜色了 + date := c.Query("date") + lang := c.DefaultQuery("lang", util.Lang) + content := c.Query("content") + weekdayType := c.DefaultQuery("weekdayType", "1") // 设置星期几的格式,zh_CH {1:周日,2:周天, 3:星期日,4:星期天,}, en_US {1: Mon, 2: MON,3: Monday, 4. MONDAY,} + + dateInfo := getDateInfo(date, lang, weekdayType) + var svg string + switch iconType { + case "1": + // Type 1: 显示年月日星期 + svg = generateTypeOneSVG(color, lang, dateInfo) + case "2": + // Type 2: 显示年月日 + svg = generateTypeTwoSVG(color, lang, dateInfo) + case "3": + // Type 3: 显示年月 + svg = generateTypeThreeSVG(color, lang, dateInfo) + case "4": + // Type 4: 仅显示年 + svg = generateTypeFourSVG(color, lang, dateInfo) + case "5": + // Type 5: 显示周数 + svg = generateTypeFiveSVG(color, lang, dateInfo) + case "6": + // Type 6: 仅显示星期 + svg = generateTypeSixSVG(color, lang, weekdayType, dateInfo) + case "7": + // Type 7: 倒数日 + svg = generateTypeSevenSVG(color, lang, dateInfo) + case "8": + // Type 8: 文字图标 + svg = generateTypeEightSVG(color, content) + default: + // 默认为Type 1 + svg = generateTypeOneSVG(color, lang, dateInfo) + } + + c.Header("Content-Type", "image/svg+xml") + c.Header("Cache-Control", "no-cache") + c.Header("Pragma", "no-cache") + c.String(http.StatusOK, svg) +} + +func getDateInfo(dateStr string, lang string, weekdayType string) map[string]interface{} { + // 设置默认值 + var date time.Time + var err error + if dateStr == "" { + date = time.Now() + } else { + date, err = time.Parse("2006-01-02", dateStr) + if err != nil { + date = time.Now() + } + } + // 获取年月日星期 + year := date.Year() + month := date.Format("Jan") + day := date.Day() + var weekdayStr string + var weekdays []string + + switch lang { + case "zh_CN": + month = date.Format("1月") + switch weekdayType { + case "1": + weekdays = []string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"} + case "2": + weekdays = []string{"周天", "周一", "周二", "周三", "周四", "周五", "周六"} + case "3": + weekdays = []string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"} + case "4": + weekdays = []string{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"} + default: + weekdays = []string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"} + } + weekdayStr = weekdays[date.Weekday()] + case "zh_CHT": + month = date.Format("1月") + switch weekdayType { + case "1": + weekdays = []string{"週日", "週一", "週二", "週三", "週四", "週五", "週六"} + case "2": + weekdays = []string{"週天", "週一", "週二", "週三", "週四", "週五", "週六"} + case "3": + weekdays = []string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"} + case "4": + weekdays = []string{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"} + default: + weekdays = []string{"週日", "週一", "週二", "週三", "週四", "週五", "週六"} + } + weekdayStr = weekdays[date.Weekday()] + + + default: + // 其他语言 + switch weekdayType { + case "1": + weekdayStr = date.Format("Mon") + case "2": + weekdayStr = date.Format("Mon") + weekdayStr = strings.ToUpper(weekdayStr) + case "3": + weekdayStr = date.Format("Monday") + case "4": + weekdayStr = date.Format("Monday") + weekdayStr = strings.ToUpper(weekdayStr) + default: + weekdayStr = date.Format("Mon") + } + } + // Calculate week number + _, weekNum := date.ISOWeek() + weekNumStr := fmt.Sprintf("%dW", weekNum) + + switch lang { + case "zh_CN": + weekNumStr = fmt.Sprintf("%d周", weekNum) + case "zh_CHT": + weekNumStr = fmt.Sprintf("%d週", weekNum) + } + // 判断是否是周末 + isWeekend := date.Weekday() == time.Saturday || date.Weekday() == time.Sunday + // Calculate days until today + today := time.Now() + today = time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()) + date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location()) + countDown := int(math.Floor(date.Sub(today).Hours() / 24)) // 注意最大返回106751天,go的时间戳最大值 + + return map[string]interface{}{ + "year": year, + "month": month, + "day": day, + "date": fmt.Sprintf("%02d-%02d", date.Month(), date.Day()), + "weekday": weekdayStr, + "week": weekNumStr, + "countDown": countDown, + "isWeekend": isWeekend, + } +} + +// Type 1: 显示年月日星期 +func generateTypeOneSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + return fmt.Sprintf(` + + + + %s + %d + %s + %d + + `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["weekday"], dateInfo["year"]) +} + +// Type 2: 显示年月日 +func generateTypeTwoSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + return fmt.Sprintf(` + + + + %s + %d + %d + + `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["year"]) +} + +// Type 3: 显示年月 +func generateTypeThreeSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + return fmt.Sprintf(` + + + + + + + + + + + + %d + %s + + `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["month"]) +} + +// Type 4: 仅显示年 +func generateTypeFourSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + return fmt.Sprintf(` + + + + + + + + + + + + %d + + `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"]) +} + +// Type 5:: 显示周数 +func generateTypeFiveSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + return fmt.Sprintf(` + + + + + + + + + + + + %d + %s + + `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["week"]) +} + +// Type 6: 仅显示星期 +func generateTypeSixSVG(color string, lang string, weekdayType string, dateInfo map[string]interface{}) string { + + weekday := dateInfo["weekday"].(string) + isWeekend := dateInfo["isWeekend"].(bool) + + // 如果不设置颜色,周末默认使用蓝色,工作日默认使用红色 + var colorScheme ColorScheme + if color == "" { + if isWeekend { + colorScheme = colorSchemes["blue"] + } else { + colorScheme = colorSchemes["red"] + } + } else { + colorScheme = getColorScheme(color) + } + // 动态变化字体大小 + var fontSize float64 + switch lang{ + case "zh_CN", "zh_CHT": + fontSize = 460 / float64(len([]rune(weekday))) + default: + switch weekdayType { + case "1": + fontSize = 690 / float64(len([]rune(weekday))) + case "2": + fontSize = 600 / float64(len([]rune(weekday))) + case "3": + fontSize = 720 / float64(len([]rune(weekday))) + case "4": + fontSize = 630 / float64(len([]rune(weekday))) + default: + fontSize = 750 / float64(len([]rune(weekday))) + } + } + + return fmt.Sprintf(` + + + + + + + + + + + + %s + `, colorScheme.Primary, colorScheme.Secondary, colorScheme.Primary, fontSize, weekday) +} + +// Type7: 倒数日 +func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interface{}) string { + colorScheme := getColorScheme(color) + + diffDays := dateInfo["countDown"].(int) + + var tipText, diffDaysText string + + // 设置输出字符 + if diffDays == 0 { + switch lang { + case "zh_CN": + tipText = "今天" + case "zh_CHT": + tipText = "今天" + default: + tipText = "Today" + } + diffDaysText = "--" + } else if diffDays > 0 { + switch lang { + case "zh_CN": + tipText = "还有" + case "zh_CHT": + tipText = "還有" + default: + tipText = "Left" + } + diffDaysText = fmt.Sprintf("%d", diffDays) + } else { + switch lang { + case "zh_CN": + tipText = "已过" + case "zh_CHT": + tipText = "已過" + default: + tipText = "Past" + } + diffDaysText = fmt.Sprintf("%d", int(math.Abs(float64(diffDays)))) + } + + dayStr := map[string]string{ + "zh_CN": "天", + "zh_CHT": "天", + "default": "days", + }[lang] + if dayStr == "" { + dayStr = "days" + } + + fontSize := 240.0 + if len(diffDaysText) >= 6 { + fontSize = 130 + } else if len(diffDaysText) == 5 { + fontSize = 140 + } else if len(diffDaysText) == 4 { + fontSize = 190 + } + return fmt.Sprintf(` + + + + %d + %s + %s + %s + %s + `, colorScheme.Primary, dateInfo["year"], dateInfo["date"], tipText, fontSize, diffDaysText, dayStr) +} + +// Type 8: 文字图标 +func generateTypeEightSVG(color, content string) string { + colorScheme := getColorScheme(color) + + isChinese := regexp.MustCompile(`[\p{Han}]`).MatchString(content) + + var fontSize float64 + switch { + case len([]rune(content)) == 1: + fontSize = 320 + case len([]rune(content)) == 2: + fontSize = 240 + case len([]rune(content)) == 3: + fontSize = 160 + case len([]rune(content)) == 4: + fontSize = 120 + case len([]rune(content)) == 5: + fontSize = 95 + default: + if isChinese { + fontSize = 480 / float64(len([]rune(content))) + } else { + fontSize = 750 / float64(len([]rune(content))) + } + } + + return fmt.Sprintf(` + + + %s + + `, colorScheme.Primary, fontSize, content) +} diff --git a/kernel/api/router.go b/kernel/api/router.go index a315d5293..1956e0757 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -33,7 +33,8 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/system/loginAuth", model.LoginAuth) ginServer.Handle("POST", "/api/system/logoutAuth", model.LogoutAuth) ginServer.Handle("GET", "/api/system/getCaptcha", model.GetCaptcha) - + ginServer.Handle("GET", "/api/icon/getDynamicIcon", getDynamicIcon) // 添加动态图标路由 + // 需要鉴权 ginServer.Handle("POST", "/api/system/getEmojiConf", model.CheckAuth, getEmojiConf) From ed4661c4adddceb9935613ed0981bed4d5ef12fd Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 27 Oct 2024 22:22:47 +0800 Subject: [PATCH 2/3] :art: Clean code https://github.com/siyuan-note/siyuan/pull/12939 --- kernel/api/icon.go | 17 +++++++++-------- kernel/api/router.go | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/kernel/api/icon.go b/kernel/api/icon.go index c2d25b06b..666b4773a 100644 --- a/kernel/api/icon.go +++ b/kernel/api/icon.go @@ -55,6 +55,8 @@ func getColorScheme(color string) ColorScheme { } func getDynamicIcon(c *gin.Context) { + // Add internal kernel API `/api/icon/getDynamicIcon` https://github.com/siyuan-note/siyuan/pull/12939 + iconType := c.DefaultQuery("type", "1") color := c.Query("color") // 不要预设默认值,不然type6返回星期就没法自动设置周末颜色了 date := c.Query("date") @@ -122,7 +124,7 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int switch lang { case "zh_CN": month = date.Format("1月") - switch weekdayType { + switch weekdayType { case "1": weekdays = []string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"} case "2": @@ -137,7 +139,7 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int weekdayStr = weekdays[date.Weekday()] case "zh_CHT": month = date.Format("1月") - switch weekdayType { + switch weekdayType { case "1": weekdays = []string{"週日", "週一", "週二", "週三", "週四", "週五", "週六"} case "2": @@ -151,10 +153,9 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int } weekdayStr = weekdays[date.Weekday()] - default: // 其他语言 - switch weekdayType { + switch weekdayType { case "1": weekdayStr = date.Format("Mon") case "2": @@ -314,9 +315,9 @@ func generateTypeSixSVG(color string, lang string, weekdayType string, dateInfo } // 动态变化字体大小 var fontSize float64 - switch lang{ + switch lang { case "zh_CN", "zh_CHT": - fontSize = 460 / float64(len([]rune(weekday))) + fontSize = 460 / float64(len([]rune(weekday))) default: switch weekdayType { case "1": @@ -390,8 +391,8 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa } dayStr := map[string]string{ - "zh_CN": "天", - "zh_CHT": "天", + "zh_CN": "天", + "zh_CHT": "天", "default": "days", }[lang] if dayStr == "" { diff --git a/kernel/api/router.go b/kernel/api/router.go index 1956e0757..adcffed98 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -33,8 +33,8 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/system/loginAuth", model.LoginAuth) ginServer.Handle("POST", "/api/system/logoutAuth", model.LogoutAuth) ginServer.Handle("GET", "/api/system/getCaptcha", model.GetCaptcha) - ginServer.Handle("GET", "/api/icon/getDynamicIcon", getDynamicIcon) // 添加动态图标路由 - + ginServer.Handle("GET", "/api/icon/getDynamicIcon", getDynamicIcon) + // 需要鉴权 ginServer.Handle("POST", "/api/system/getEmojiConf", model.CheckAuth, getEmojiConf) From c22a98b6cd4c16d48cd0f9029f38e89a70cf091c Mon Sep 17 00:00:00 2001 From: Jixiong Su Date: Mon, 28 Oct 2024 09:24:50 +0800 Subject: [PATCH 3/3] :art: Improve kernel API `/api/icon/getDynamicIcon` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * `/api/icon/getDynamicIcon` 倒数日图标文字始终垂直居中 * type=8 文字图标样式改进 文字图标fontsize动态变化规则改进,之前代码忘记改了,导致英文字体偏小,动态变化规则不对。 并调整文字居中。 * 调整缩进 * 调整缩进 * 优化居中文字:使用x="50%"进行水平居中 * type=3 显示年月图标,年文字fontsize增加 * type=7 倒数日算法改进,支持超过106751天的计算 * Update icon.go * Type 8: 文字图标单独调整"g", "p", "y", "q" * Update icon.go --- kernel/api/icon.go | 151 ++++++++++++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 49 deletions(-) diff --git a/kernel/api/icon.go b/kernel/api/icon.go index 666b4773a..a7fb8b265 100644 --- a/kernel/api/icon.go +++ b/kernel/api/icon.go @@ -18,7 +18,6 @@ package api import ( "fmt" - "math" "net/http" "regexp" "strings" @@ -186,7 +185,8 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int today := time.Now() today = time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()) date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location()) - countDown := int(math.Floor(date.Sub(today).Hours() / 24)) // 注意最大返回106751天,go的时间戳最大值 + // countDown := int(math.Floor(date.Sub(today).Hours() / 24)) // 注意最大返回106751天,go的时间戳最大值 + countDown := daysBetween(today, date) return map[string]interface{}{ "year": year, @@ -200,6 +200,43 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int } } +func daysBetween(date1, date2 time.Time) int { + // 将两个日期都调整到UTC时间的0点 + date1 = time.Date(date1.Year(), date1.Month(), date1.Day(), 0, 0, 0, 0, time.UTC) + date2 = time.Date(date2.Year(), date2.Month(), date2.Day(), 0, 0, 0, 0, time.UTC) + + // 确保date1不晚于date2 + swap := false + if date1.After(date2) { + date1, date2 = date2, date1 + swap = true + } + + // 计算天数差 + days := 0 + for y := date1.Year(); y < date2.Year(); y++ { + if isLeapYear(y) { + days += 366 + } else { + days += 365 + } + } + + // 加上最后一年的天数 + days += int(date2.YearDay() - date1.YearDay()) + + // 如果原始的date1晚于date2,返回负值 + if swap { + return -days + } + return days +} + +// 判断是否为闰年 +func isLeapYear(year int) bool { + return year%4 == 0 && (year%100 != 0 || year%400 == 0) +} + // Type 1: 显示年月日星期 func generateTypeOneSVG(color string, lang string, dateInfo map[string]interface{}) string { colorScheme := getColorScheme(color) @@ -209,8 +246,8 @@ func generateTypeOneSVG(color string, lang string, dateInfo map[string]interface %s - %d - %s + %d + %s %d `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["weekday"], dateInfo["year"]) @@ -225,7 +262,7 @@ func generateTypeTwoSVG(color string, lang string, dateInfo map[string]interface %s - %d + %d %d `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["year"]) @@ -248,7 +285,7 @@ func generateTypeThreeSVG(color string, lang string, dateInfo map[string]interfa %d - %s + %s `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["month"]) } @@ -269,7 +306,7 @@ func generateTypeFourSVG(color string, lang string, dateInfo map[string]interfac - %d + %d `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"]) } @@ -291,7 +328,7 @@ func generateTypeFiveSVG(color string, lang string, dateInfo map[string]interfac %d - %s + %s `, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["week"]) } @@ -345,7 +382,7 @@ func generateTypeSixSVG(color string, lang string, weekdayType string, dateInfo - %s + %s `, colorScheme.Primary, colorScheme.Secondary, colorScheme.Primary, fontSize, weekday) } @@ -358,17 +395,16 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa var tipText, diffDaysText string // 设置输出字符 - if diffDays == 0 { + switch { + case diffDays == 0: switch lang { - case "zh_CN": - tipText = "今天" - case "zh_CHT": + case "zh_CN", "zh_CHT": tipText = "今天" default: tipText = "Today" } diffDaysText = "--" - } else if diffDays > 0 { + case diffDays > 0: switch lang { case "zh_CN": tipText = "还有" @@ -378,7 +414,7 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa tipText = "Left" } diffDaysText = fmt.Sprintf("%d", diffDays) - } else { + default: switch lang { case "zh_CN": tipText = "已过" @@ -387,26 +423,30 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa default: tipText = "Past" } - diffDaysText = fmt.Sprintf("%d", int(math.Abs(float64(diffDays)))) + absDiffDays := -diffDays + diffDaysText = fmt.Sprintf("%d", absDiffDays) } - dayStr := map[string]string{ - "zh_CN": "天", - "zh_CHT": "天", - "default": "days", - }[lang] - if dayStr == "" { + var dayStr string + switch lang { + case "zh_CN", "zh_CHT": + dayStr = "天" + default: dayStr = "days" } - - fontSize := 240.0 - if len(diffDaysText) >= 6 { - fontSize = 130 - } else if len(diffDaysText) == 5 { - fontSize = 140 - } else if len(diffDaysText) == 4 { + // 动态变化字体大小 + var fontSize float64 + switch { + case len(diffDaysText) <= 3: + fontSize = 240 + case len(diffDaysText) == 4: fontSize = 190 + case len(diffDaysText) == 5: + fontSize = 140 + case len(diffDaysText) >= 6: + fontSize = 780 / float64(len(diffDaysText)) } + return fmt.Sprintf(` @@ -414,8 +454,8 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa %d %s %s - %s - %s + %s + %s `, colorScheme.Primary, dateInfo["year"], dateInfo["date"], tipText, fontSize, diffDaysText, dayStr) } @@ -423,32 +463,45 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa func generateTypeEightSVG(color, content string) string { colorScheme := getColorScheme(color) + // 动态变化字体大小 isChinese := regexp.MustCompile(`[\p{Han}]`).MatchString(content) - var fontSize float64 - switch { - case len([]rune(content)) == 1: - fontSize = 320 - case len([]rune(content)) == 2: - fontSize = 240 - case len([]rune(content)) == 3: - fontSize = 160 - case len([]rune(content)) == 4: - fontSize = 120 - case len([]rune(content)) == 5: - fontSize = 95 - default: - if isChinese { + if isChinese { + switch { + case len([]rune(content)) == 1: + fontSize = 320 + default: fontSize = 480 / float64(len([]rune(content))) - } else { + } + } else { + switch { + case len([]rune(content)) == 1: + fontSize = 480 + case len([]rune(content)) == 2: + fontSize = 300 + case len([]rune(content)) == 3: + fontSize = 240 + default: fontSize = 750 / float64(len([]rune(content))) } } + // 当内容为单个字符时,一些小写字母需要调整文字位置(暂时没法批量解决) + dy := "0%" + if len([]rune(content)) == 1 { + switch content { + case "g", "p", "y", "q": + dy = "-10%" + case "j": + dy = "-5%" + default: + dy = "0%" + } + } return fmt.Sprintf(` - %s - - `, colorScheme.Primary, fontSize, content) + %s + + `, colorScheme.Primary, dy, fontSize, content) }