Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

21.1 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package api

import (
	"fmt"
	"html"
	"math"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/siyuan-note/siyuan/kernel/apicontract"
	"github.com/siyuan-note/siyuan/kernel/conf"
	"github.com/siyuan-note/siyuan/kernel/model"
	"github.com/siyuan-note/siyuan/kernel/util"
)

var iconHanPattern = regexp.MustCompile(`[\p{Han}]`)

type ColorScheme struct {
	Primary   string
	Secondary string
}

var colorSchemes = map[string]ColorScheme{
	"red":    {"#d13d51", "#ba2c3f"},
	"blue":   {"#3eb0ea", "#0097e6"},
	"yellow": {"#eec468", "#d89b18"},
	"green":  {"#52E0B8", "#19b37a"},
	"purple": {"#a36cda", "#8952d5"},
	"pink":   {"#f183aa", "#e05b8a"},
	"orange": {"#f3865e", "#ef5e2a"},
	"grey":   {"#576574", "#374a60"},
}

func getColorScheme(color string) ColorScheme {
	color = strings.TrimSpace(color)

	// 检查是否是预定义的颜色
	if scheme, ok := colorSchemes[strings.ToLower(color)]; ok {
		return scheme
	}
	// 支持自定义颜色
	// 如果颜色值不以#开头,自动添加#
	if !strings.HasPrefix(color, "#") && len(color) > 0 {
		color = "#" + color
	}

	// 检查是否是十六进制颜色值
	hexColorPattern := `^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$`
	if matched, _ := regexp.MatchString(hexColorPattern, color); matched {
		// 确保颜色值有#前缀
		if !strings.HasPrefix(color, "#") {
			color = "#" + color
		}

		// 如果是3位十六进制,转换为6位
		if len(color) == 4 {
			r := string(color[1])
			g := string(color[2])
			b := string(color[3])
			color = "#" + r + r + g + g + b + b
		}

		// 生成次要颜色(将主色调变深)
		secondary := darkenColor(color, 0.1)

		return ColorScheme{
			Primary:   color,
			Secondary: secondary,
		}
	}

	// 如果既不是预定义颜色也不是有效的十六进制值,返回默认颜色
	return colorSchemes["red"]
}

func darkenColor(hexColor string, factor float64) string {
	// 去掉#号
	hex := hexColor[1:]

	// 将十六进制转换为RGB
	r, _ := strconv.ParseInt(hex[0:2], 16, 64)
	g, _ := strconv.ParseInt(hex[2:4], 16, 64)
	b, _ := strconv.ParseInt(hex[4:6], 16, 64)

	// 使颜色变深
	r = int64(float64(r) * (1 - factor))
	g = int64(float64(g) * (1 - factor))
	b = int64(float64(b) * (1 - factor))

	// 确保值在0-255范围内
	r = int64(math.Max(0, float64(r)))
	g = int64(math.Max(0, float64(g)))
	b = int64(math.Max(0, float64(b)))

	// 转回十六进制
	return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}

var getDynamicIcon = contractHandler(apicontract.GetDynamicIcon, func(c *gin.Context, request apicontract.EmptyRequest) apicontract.Response[apicontract.BinaryContent] {
	confAppearanceSnapshot := model.Conf.LoadAppearance()

	// Add internal kernel API `/api/icon/getDynamicIcon` https://github.com/siyuan-note/siyuan/pull/12939

	iconType := c.Query("type")
	if "" == iconType {
		iconType = "1"
	}
	color := c.Query("color") // 不要预设默认值,不然type6返回星期就没法自动设置周末颜色了
	date := c.Query("date")
	lang := c.Query("lang")
	if "" == lang {
		lang = util.Lang
	}
	lang = util.LangToBCP47(lang)
	weekdayType := c.Query("weekdayType") // 设置星期几的格式,zh-CN {1:周日,2:周天, 3:星期日,4:星期天,}, en {1: Mon, 2: MON,3: Monday, 4. MONDAY,}
	if "" == weekdayType {
		weekdayType = "1"
	}

	dateInfo := getDateInfo(date, lang, weekdayType)
	var svg string
	switch iconType {
	case "1":
		// Type 1: 显示年月日星期
		svg = generateTypeOneSVG(color, dateInfo)
	case "2":
		// Type 2: 显示年月日
		svg = generateTypeTwoSVG(color, dateInfo)
	case "3":
		// Type 3: 显示年月
		svg = generateTypeThreeSVG(color, dateInfo)
	case "4":
		// Type 4: 仅显示年
		svg = generateTypeFourSVG(color, dateInfo)
	case "5":
		// Type 5: 显示周数
		svg = generateTypeFiveSVG(color, dateInfo)
	case "6":
		// Type 6: 仅显示星期
		svg = generateTypeSixSVG(color, lang, weekdayType, dateInfo)
	case "7":
		// Type 7: 倒数日
		svg = generateTypeSevenSVG(color, lang, dateInfo)
	case "8":
		// Type 8: 文字图标
		content := c.Query("content")
		id := c.Query("id")
		if strings.Contains(content, ".action{") {
			// 模板内容会按 id 读取工作区数据,只读角色必须通过发布访问控制后才能执行
			// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-whcx-xxqh-c838
			// 只读角色的模板源码由内核从块已保存的图标属性中取得,不使用请求中的 content
			// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-cxwr-r7cq-xw52
			if !dynamicIconContentAccessable(c, id) {
				// 空内容保持与 id 不存在时一致的响应结构,避免泄露文档的可访问状态
				svg = generateTypeEightSVG(color, "")
				break
			}
			content = model.RenderDynamicIconContentTemplate(c, content, id)
		}
		svg = generateTypeEightSVG(color, content)
	default:
		// 默认为Type 1
		svg = generateTypeOneSVG(color, dateInfo)
	}

	var fonts []*conf.EditorFont
	if "8" == iconType && nil != confAppearanceSnapshot {
		fonts = confAppearanceSnapshot.GlobalFontFamilies
	}
	svg = applyDynamicIconFont(svg, fonts)

	if !model.Conf.Editor.Load().AllowSVGScript {
		var err error
		svg, err = util.SanitizeSVG(svg)
		if err != nil {
			return apicontract.EmptyHTTPResponse[apicontract.BinaryContent](http.StatusInternalServerError)
		}
	}

	c.Header("Content-Type", "image/svg+xml")
	c.Header("Content-Security-Policy", "script-src 'none'; object-src 'none'; base-uri 'none'")
	c.Header("X-Content-Type-Options", "nosniff")
	// 响应内容随角色与发布密码 Cookie 变化,避免中间缓存跨调用方回放
	c.Header("Vary", "Cookie")
	c.Header("Cache-Control", "no-cache")
	c.Header("Pragma", "no-cache")
	return apicontract.SuccessBinary("image/svg+xml", []byte(svg))
})

// applyDynamicIconFont 将全局字体和首选字重应用到 SVG 根节点,供所有文本继承。
func applyDynamicIconFont(svg string, fonts []*conf.EditorFont) string {
	const fallback = "-apple-system, BlinkMacSystemFont, 'Noto Sans', 'Noto Sans CJK SC', 'Microsoft YaHei'"
	families := make([]string, 0, len(fonts)+1)
	weight := 400
	for _, font := range fonts {
		if nil == font || "" == font.Family {
			continue
		}
		if 0 == len(families) && 1 <= font.Weight && font.Weight <= 1000 {
			weight = font.Weight
		}
		// 字体名称作为 CSS 字符串编码,再对整个属性进行 XML 转义。
		var family strings.Builder
		family.WriteByte('"')
		for _, char := range font.Family {
			if '\\' == char || '"' == char || char < 0x20 || 0x7f == char {
				fmt.Fprintf(&family, "\\%x ", char)
			} else {
				family.WriteRune(char)
			}
		}
		family.WriteByte('"')
		families = append(families, family.String())
	}
	families = append(families, fallback)
	attributes := fmt.Sprintf(`<svg font-family="%s" font-weight="%d" `, html.EscapeString(strings.Join(families, ", ")), weight)
	return strings.Replace(svg, "<svg ", attributes, 1)
}

// dynamicIconContentAccessable 判断调用方是否可读取动态图标模板内容所引用的块。
// 管理员与编辑者拥有工作区读权限,只读角色则需要通过发布访问控制(禁用、密码与加密笔记本门禁)。
func dynamicIconContentAccessable(c *gin.Context, id string) bool {
	if !model.IsReadOnlyRoleContext(c) {
		return true
	}
	return model.CheckBlockIdMetadataAccessableByPublishAccess(c, model.GetPublishAccess(), id)
}

func getDateInfo(dateStr string, lang string, weekdayType string) map[string]any {
	// 设置默认值
	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-TW":
		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 and ISO year
	isoYear, weekNum := date.ISOWeek()
	weekNumStr := fmt.Sprintf("%dW", weekNum)

	switch lang {
	case "zh-CN":
		weekNumStr = fmt.Sprintf("%d周", weekNum)
	case "zh-TW":
		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的时间戳最大值
	countDown := daysBetween(today, date)

	return map[string]any{
		"year":      year,
		"isoYear":   isoYear,
		"month":     month,
		"day":       day,
		"date":      fmt.Sprintf("%02d-%02d", date.Month(), date.Day()),
		"weekday":   weekdayStr,
		"week":      weekNumStr,
		"countDown": countDown,
		"isWeekend": isWeekend,
	}
}

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 += 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, dateInfo map[string]any) string {
	colorScheme := getColorScheme(color)

	return fmt.Sprintf(`
    <svg id="dynamic_icon_type1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
    <path d="M512,447.5c0,32-25,57-57,57H57c-32,0-57-25-57-57V120.5c0-31,25-57,57-57h398c32,0,57,26,57,57v327Z" style="fill: #ecf2f7;"/>
    <path d="M39,0h434c21.52,0,39,17.48,39,39v146H0V39C0,17.48,17.48,0,39,0Z" style="fill: %s;"/>
    <text transform="translate(22 146.5)" style="fill: #fff; font-size: 100px;">%s</text>
    <text x="50%%" y="392.5" style="fill: #66757f; font-size: 240px; text-anchor: middle">%d</text>
    <text x="50%%" y="472.5" style="fill: #66757f; font-size: 64px; text-anchor: middle">%s</text>
    <text transform="translate(331.03 148.44)" style="fill: #fff; font-size: 71.18px;">%d</text>
    </svg>
    `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["weekday"], dateInfo["year"])
}

// Type 2: 显示年月日
func generateTypeTwoSVG(color string, dateInfo map[string]any) string {
	colorScheme := getColorScheme(color)

	return fmt.Sprintf(`
    <svg id="dynamic_icon_type2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
    <path d="M512,447.5c0,32-25,57-57,57H57c-32,0-57-25-57-57V120.5c0-31,25-57,57-57h398c32,0,57,26,57,57v327Z" style="fill: #ecf2f7;"/>
    <path d="M39,0h434c21.52,0,39,17.48,39,39v146H0V39C0,17.48,17.48,0,39,0Z" style="fill: %s;"/>
    <text transform="translate(22 146.5)" style="fill: #fff; font-size: 100px;">%s</text>
    <text x="50%%" y="420.5"  style="fill: #66757f; font-size: 256px;text-anchor: middle">%d</text>
    <text transform="translate(331.03 148.44)" style="fill: #fff; font-size: 71.18px;">%d</text>
    </svg>
    `, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["year"])
}

// calendarTextSVG 渲染动态图标 type 3、4、5 共用的日历外框、配色与装饰圆点,body 为底部文字区域。
// iconType 只用于生成 SVG 元素 id(如 type3),不参与样式计算。
func calendarTextSVG(iconType string, color string, body string) string {
	colorScheme := getColorScheme(color)

	return fmt.Sprintf(`
    <svg id="dynamic_icon_%s" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 512 512">
        <path class="cls-6" d="M512,447.5c0,32-25,57-57,57H57c-32,0-57-25-57-57V120.5c0-31,25-57,57-57h398c32,0,57,26,57,57v327Z" style="fill: #ecf2f7;"/>
        <path class="cls-1" d="M39,0h434c21.5,0,39,17.5,39,39v146H0V39C0,17.5,17.5,0,39,0Z" style="fill: %s;"/>
        <g style="fill: %s;">
            <circle  cx="468.5" cy="135" r="14"/>
            <circle  cx="468.5" cy="93" r="14"/>
            <circle  cx="425.5" cy="135" r="14"/>
            <circle  cx="425.5" cy="93" r="14"/>
            <circle  cx="382.5" cy="135" r="14"/>
            <circle  cx="382.5" cy="93" r="14"/>
        </g>
%s
    </svg>
    `, iconType, colorScheme.Primary, colorScheme.Secondary, body)
}

// Type 3: 显示年月
func generateTypeThreeSVG(color string, dateInfo map[string]any) string {
	body := fmt.Sprintf(`        <text transform="translate(22 146.5)" style="fill: #fff;font-size: 120px; ">%d</text>
        <text x="50%%" y="410.5" style="fill: #66757f;font-size: 200px;text-anchor: middle;">%s</text>`,
		dateInfo["year"], dateInfo["month"])
	return calendarTextSVG("type3", color, body)
}

// Type 4: 仅显示年
func generateTypeFourSVG(color string, dateInfo map[string]any) string {
	body := fmt.Sprintf(`        <text x="50%%" y="410.5" style="fill: #66757f;font-size: 200px;text-anchor: middle;">%d</text>`,
		dateInfo["year"])
	return calendarTextSVG("type4", color, body)
}

// Type 5:: 显示周数
func generateTypeFiveSVG(color string, dateInfo map[string]any) string {
	body := fmt.Sprintf(`        <text transform="translate(22 146.5)" style="fill: #fff;font-size: 120px; ">%d</text>
        <text x="50%%" y="410.5" style="fill: #66757f;font-size: 200px;text-anchor: middle;">%s</text>`,
		dateInfo["isoYear"], dateInfo["week"])
	return calendarTextSVG("type5", color, body)
}

// Type 6: 仅显示星期
func generateTypeSixSVG(color string, lang string, weekdayType string, dateInfo map[string]any) 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-TW":
		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(`
    <svg id="dynamic_icon_type6" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
    <path id="center" d="M512,447.5c0,32-25,57-57,57H57c-32,0-57-25-57-57V120.5c0-31,25-57,57-57h398c32,0,57,26,57,57v327Z" style="fill: #ecf2f7;"/>
    <path id="top" d="M39,0h434c21.5,0,39,14,39,31.2v116.8H0V31.2C0,14,17.5,0,39,0Z" style="fill: %s;"/>
    <g id="cirle" style="fill: %s;">
        <circle cx="468.5" cy="113.5" r="14"/>
        <circle cx="468.5" cy="71.5" r="14"/>
        <circle cx="425.5" cy="113.5" r="14"/>
        <circle cx="425.5" cy="71.5" r="14"/>
        <circle cx="382.5" cy="113.5" r="14"/>
        <circle cx="382.5" cy="71.5" r="14"/>
    </g>
    <text id="weekday" x="50%%"  y="65%%" style="fill: %s; font-size: %.2fpx; text-anchor: middle; dominant-baseline:middle;">%s</text>
    </svg>`, colorScheme.Primary, colorScheme.Secondary, colorScheme.Primary, fontSize, weekday)
}

// Type7: 倒数日
func generateTypeSevenSVG(color string, lang string, dateInfo map[string]any) string {
	colorScheme := getColorScheme(color)

	diffDays := dateInfo["countDown"].(int)

	var tipText, diffDaysText string

	// 设置输出字符
	switch {
	case diffDays == 0:
		switch lang {
		case "zh-CN", "zh-TW":
			tipText = "今天"
		default:
			tipText = "Today"
		}
		diffDaysText = "--"
	case diffDays > 0:
		switch lang {
		case "zh-CN":
			tipText = "还有"
		case "zh-TW":
			tipText = "還有"
		default:
			tipText = "Left"
		}
		diffDaysText = fmt.Sprintf("%d", diffDays)
	default:
		switch lang {
		case "zh-CN":
			tipText = "已过"
		case "zh-TW":
			tipText = "已過"
		default:
			tipText = "Past"
		}
		absDiffDays := -diffDays
		diffDaysText = fmt.Sprintf("%d", absDiffDays)
	}

	var dayStr string
	switch lang {
	case "zh-CN", "zh-TW":
		dayStr = "天"
	default:
		dayStr = "days"
	}
	// 动态变化字体大小
	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(`
    <svg id="dynamic_icon_type7" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
        <path id="bottom" d="M512,447.5c0,32-25,57-57,57H57c-32,0-57-25-57-57V120.5c0-31,25-57,57-57h398c32,0,57,26,57,57v327Z" style="fill: #ecf2f7;"/>
        <path id="top" d="M39,0h434c21.52,0,39,17.48,39,39v146H0V39C0,17.48,17.48,0,39,0Z" style="fill: %s;"/>
        <text id="year" transform="translate(46.1 78.92)" style="fill: #fff; font-size: 60px;">%d</text>
        <text id="day" transform="translate(43.58 148.44)" style="fill: #fff; font-size: 60px;">%s</text>
        <text id="passStr" transform="translate(400 148.44)" style="fill: #fff; text-anchor: middle;font-size: 71.18px;">%s</text>
        <text id="diffDays" x="50%%" y="65%%" style="font-size: %.0fpx; fill: #66757f; text-anchor: middle; dominant-baseline:middle;">%s</text>
        <text id="dayStr" x="50%%" y="472.5" style="font-size: 64px; text-anchor: middle; fill: #66757f;">%s</text>
    </svg>`, colorScheme.Primary, dateInfo["year"], dateInfo["date"], tipText, fontSize, diffDaysText, dayStr)
}

// Type 8: 文字图标
func generateTypeEightSVG(color, content string) string {
	colorScheme := getColorScheme(color)

	contentLen := len([]rune(content))
	if 0 == contentLen {
		// 内容为空时不输出文本,避免字号按零长度计算得到无效值
		return fmt.Sprintf(`
    <svg id="dynamic_icon_type8" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
        <path d="M39,0h434c20.97,0,38,17.03,38,38v412c0,33.11-26.89,60-60,60H60c-32.56,0-59-26.44-59-59V38C1,17.03,18.03,0,39,0Z" style="fill: %s;"/>
	</svg>
    `, colorScheme.Primary)
	}

	// 动态变化字体大小
	isChinese := iconHanPattern.MatchString(content)
	var fontSize float64
	if isChinese {
		switch contentLen {
		case 1:
			fontSize = 320
		default:
			fontSize = 480 / float64(contentLen)
		}
	} else {
		switch contentLen {
		case 1:
			fontSize = 480
		case 2:
			fontSize = 300
		case 3:
			fontSize = 240
		default:
			fontSize = 750 / float64(contentLen)
		}
	}
	// 当内容为单个字符时,一些小写字母需要调整文字位置(暂时没法批量解决)
	dy := "0%"
	if contentLen == 1 {
		switch content {
		case "g", "p", "y", "q":
			dy = "-10%"
		case "j":
			dy = "-5%"
		default:
			dy = "0%"
		}
	}

	escapedContent := html.EscapeString(content)
	return fmt.Sprintf(`
    <svg id="dynamic_icon_type8" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
        <path d="M39,0h434c20.97,0,38,17.03,38,38v412c0,33.11-26.89,60-60,60H60c-32.56,0-59-26.44-59-59V38C1,17.03,18.03,0,39,0Z" style="fill: %s;"/>
        <text x="50%%" y="55%%" dy="%s" style="font-size: %.2fpx; fill: #fff; text-anchor: middle; dominant-baseline:middle;">%s</text>
	</svg>
    `, colorScheme.Primary, dy, fontSize, escapedContent)
}