Skip to content

Instantly share code, notes, and snippets.

@zozovo
Last active November 7, 2024 02:03
Show Gist options
  • Save zozovo/8a76915acf197a873304dd23f2cfd49c to your computer and use it in GitHub Desktop.
Save zozovo/8a76915acf197a873304dd23f2cfd49c to your computer and use it in GitHub Desktop.
Replace the font with Sarasa Gothic (CJK) and the corresponding monospaced one.
// ==UserScript==
// @name Sarasa-Gothic-Font-Substitution-Tool
// @name:en-US Sarasa Gothic Font Substitution Tool
// @name:zh-CN 更纱黑体字体替换工具
// @name:zh-TW 更紗黑體字型替換工具
// @name:zh-HK 更紗黑體字型替換工具
// @name:ja-JP 更紗ゴシックフォント置換ツール
// @name:ko-KR 사라사고딕 폰트 대체 도구
// @namespace https://gist.github.com/zozovo/8a76915acf197a873304dd23f2cfd49c
// @version 8.9
// @author zozovo
// @description Replace the font with Sarasa Gothic (CJK) and the corresponding monospaced one.
// @description:en-US Replace the font with Sarasa Gothic (CJK) and the corresponding monospaced one.
// @description:zh-CN 将字体替换为更纱黑体(中日韩)和对应的等宽字体。
// @description:zh-TW 將字體替換為更紗黑體(中日韓)和對應的等寬字體。
// @description:zh-HK 將字體替換為更紗黑體(中日韓)和對應的等寬字體。
// @description:ja-JP フォントを更紗ゴシック(中日韓)および対応する等幅フォントに置き換えます。
// @description:ko-KR 글꼴을사라사고딕 (중일한) 및해당등폭글꼴로대체합니다.
// @match *://*/*
// @run-at document-start
// @grant GM_addStyle
// @license MIT
// @supportURL https://gist.github.com/zozovo/8a76915acf197a873304dd23f2cfd49c
// ==/UserScript==
const LANG_CODE_OBJ = {
"zh-CN": "SC",
"zh-SG": "SC",
"zh-TW": "TC",
"zh-HK": "HC",
"ja": "J",
"ko": "K",
"ko-KR": "K",
"ko-KP": "K",
};
const lang_code = LANG_CODE_OBJ[navigator.language] || "SC"; // Default to SC if not found
const FONT_FAMILY_TYPE = {
generic: "var(--icon-font-family), var(--generic-font-family), system-ui;",
code: "var(--icon-font-family), var(--code-font-family), monospace;",
};
const EMOJIS_UNICODE_RANGE = // Unicode CLDR: check https://unicode.org/emoji/charts/emoji-style.txt
// "U+00A9, U+00AE" + // BMP: Latin-1 Supplement: copyright and registered
"U+200C, U+200D, U+203C, U+2049, " + // BMP: General Punctuation: ZWNJ and ZWJ, double exclamation mark, exclamation question mark
"U+20E3, " + // BMP: Combining Diacritical Marks for Symbols: combining enclosing keycap
// "U+2122, " + // BMP: Letterlike Symbols: trade mark
"U+2139, " + // BMP: Letterlike Symbols: information
"U+2190-21FF, " + // BMP: Arrows
"U+23??, " + // BMP: Miscellaneous Technical
"U+24C2, " + // BMP: Enclosed Alphanumerics: circled M
"U+25A0-27BF, " + // BMP: Geometric Shapes, Miscellaneous Symbols, Dingbats
"U+2B??, " + // BMP: Miscellaneous Symbols and Arrows
"U+2934, U+2935, " + // BMP: Supplemental Arrows-B: right arrow curving up, right arrow curving down
"U+3030, U+303D, " + // BMP: CJK Symbols and Punctuation: wavy dash, part alternation mark
"U+3297, U+3299, " + // BMP: Enclosed CJK Letters and Months: Japanese “congratulations” button, Japanese “secret” button
"U+FE0E, U+FE0F, " + // BMP: Variation Selectors: VS15, VS16
"U+1F???"; // SMP
GM_addStyle(`
@font-face {
font-family: "Color Emoji";
src: local("Apple Color Emoji"), local("Noto Color Emoji"),
local("Segoe UI Emoji");
unicode-range: ${EMOJIS_UNICODE_RANGE};
}
@font-face {
font-family: "Monochrome Emoji";
src: local("Apple Monochrome Emoji"), local("Noto Emoji"),
local("Segoe Fluent Icons"), local("Segoe MDL2 Assets");
unicode-range: ${EMOJIS_UNICODE_RANGE};
}
@font-face {
font-family: "Consolas";
src: local("Sarasa Fixed ${lang_code}");
}
@font-face {
font-family: "Courier New";
src: local("Sarasa Fixed ${lang_code}");
}
:root {
--icon-font-family: "Color Emoji", "Monochrome Emoji";
--generic-font-family: "Sarasa UI ${lang_code}";
--code-font-family: "Sarasa Fixed ${lang_code}";
}
:is([lang$="TW"], [lang$="Hant"]) {
--generic-font-family: "Sarasa UI TC";
--code-font-family: "Sarasa Fixed TC";
}
:is([lang$="HK"], [lang$="MO"]) {
--generic-font-family: "Sarasa UI HC";
--code-font-family: "Sarasa Fixed HC";
}
:lang(ja) {
--generic-font-family: "Sarasa UI J";
--code-font-family: "Sarasa Fixed J";
}
:lang(ko) {
--generic-font-family: "Sarasa UI K";
--code-font-family: "Sarasa Fixed K";
}
:not(#_#_) {
/** google-symbols https://fonts.google.com/icons,
* font-awesome https://fontawesome.com/iconsother,
* other icons
*/
:not([class*="-symbols"], [class*="fa-"], [class*="icon"]) {
/* generic fonts, except mathjax, <i> <s> for icons, <a> <span> for inherited */
&:not(a, i, s, span, textarea, button *):not(
math,
math *,
mjx-container,
mjx-container *,
[aria-hidden="true"],
[role="presentation"],
[role="none"],
[aria-hidden="true"] *,
[role="presentation"] *,
[role="none"] *
) {
font-family: ${FONT_FAMILY_TYPE.generic}
/** mono fonts and code editors:
* Ace https://ace.c9.io/,
* CodeMirror https://codemirror.net/ https://codemirror.net/5/,
* Monaco https://microsoft.github.io/monaco-editor/,
* TailwindCSS https://tailwindcss.com/docs/font-family
*/
&:where(code, kbd, pre, samp, var),
&:where([class*="ace_editor"], [class*="cm-"], [class*="monaco-editor"], [class*="font-mono"]) {
font-family: ${FONT_FAMILY_TYPE.code}
/* <i> for icon, <a> <span> for inherited */
& :not(a, i, span) {
font-family: ${FONT_FAMILY_TYPE.code}
}
}
/* for github readme*/
&:where([class*="code"]:not(#readme, .readme)) {
font-family: ${FONT_FAMILY_TYPE.code}
}
}
}
/* discuz */
:is(a[class*="xst"]) {
font-family: ${FONT_FAMILY_TYPE.generic}
}
}
`);
@Leoyzen
Copy link

Leoyzen commented Jul 1, 2022

[class*=fa] should be added in order to excluding font awesome.

@zozovo
Copy link
Author

zozovo commented Jul 8, 2022

@Leoyzen brilliant!

@SmileYangQ
Copy link

SmileYangQ commented Oct 29, 2024

想請問要如何讓google slides 顯示標楷體的字體?
原本安裝下方程式裝在chromebook, 瀏覽器,google slides可以正常使用標楷體,但後來就不再顯示標楷體,一直找不出原因,不知道您可否幫忙?

另外可以讓chrome瀏覽器直接讀自己設備(chromebook)的標楷體?而不需要再去讀網頁裡的標楷體嗎?

謝謝,麻煩您了。

// ==UserScript==
// @name                 Font substitution: Sarasa Gothic
// @name:zh-CN           標楷體
// @name:zh-TW           標楷體
// @name:zh-HK           標楷體
// @name:ja              フォント置換:更紗ゴシック
// @name:ko              글꼴 대체:사라사고딕
// @namespace            https://gist.github.com/zozovo/8a76915acf197a873304dd23f2cfd49c
// @version              5.9
// @author               zozovo
// @description          Substitute fonts with Sarasa Gothic CJK and Sarasa Mono.
// @description:zh-CN    標楷體。
// @description:zh-TW    標楷體。
// @description:zh-HK    標楷體。
// @description:ja       フォントを 更紗ゴシック(CJK) および 更紗ゴシック Mono に 置き換えます。
// @description:ko       글꼴을 사라사고딕 CJK 및 사라사고딕 Mono 교체합니다.
// @match                *://*/*
// @run-at               document-body
// @grant                GM_addStyle
// @grant                GM_registerMenuCommand
// @grant                GM_unregisterMenuCommand
// @license              MIT
// ==/UserScript==

GM_unregisterMenuCommand((GM_registerMenuCommand("Fonts selection", () => alert("WIP"))));
GM_addStyle(`
@font-face { font-family: DFKai-SB; src: url('https://raw.githubusercontent.com/peter279k/publisher-system/master/fonts/kaiu.ttf'); }
@font-face { font-family: DFKai-SB_EmbeddedFont; src: url('https://raw.githubusercontent.com/peter279k/publisher-system/master/fonts/kaiu.ttf'); }
@font-face { font-family: DFKai-SB_MSFontService; src: url('https://raw.githubusercontent.com/peter279k/publisher-system/master/fonts/kaiu.ttf'); }
`);

@zozovo
Copy link
Author

zozovo commented Nov 3, 2024

Google Slides 顯示標楷體的字體

想請問要如何讓google slides 顯示標楷體的字體?

我对这个问题的理解:

由于网络环境或者操作系统的变化,Google Slide 字体下拉菜单中,无法找到「标楷体」选项

解决方案:

  1. CTRL + SHIFT + F,显示菜单
  2. 左上角文件
  3. 语言
  4. 弹出菜单,选择中文(台湾)
  5. 可以看到在字体下拉菜单出现了标楷体

将网页中的字体替换为标楷体,用于屏幕显示

原本安裝下方程式裝在chromebook, 瀏覽器,google slides可以正常使用標楷體,但後來就不再顯示標楷體,一直找不出原因,不知道您可否幫忙?

我对这个问题的理解:

之前可以注入 @font-face {},将 familyDFKai-SB 的字体资源,重新映射为 kaiu.ttf

Google Slides 前端某次更新后,<svg> 内的 <text> 的变为内联样式,优先级更高,导致脚本失效。

可能的解决方案:

  1. !important 标记,但幻灯片中将只有这一种字体。

    svg text {
        font-family: DFKai-SB !important;
    }
  2. 改变默认字体的映射,将一个不常用的字体(比如:Comic Sans MS)映射成为标楷体,使用这种字体。

    @font-face {
        font-family: "Comic Sans MS";
        src: local("DFKai-SB");
    }

讓 chrome 瀏覽器直接讀自己設備(chromebook)的標楷體

另外可以讓chrome瀏覽器直接讀自己設備(chromebook)的標楷體?而不需要再去讀網頁裡的標楷體嗎?

解决方案是:

@font-face {
  font-family: DFKai-SB;
  src: local("DFKai-SB"),
    url("https://raw.githubusercontent.com/peter279k/publisher-system/master/fonts/kaiu.ttf");
}

local()url() 的使用方法,可以参考 https://drafts.csswg.org/css-fonts/#local-font-fallback

@SmileYangQ
Copy link

在電腦顯示是沒問題的,它就是標楷體。但是到chrome book 同一個google slides 它顯示的就不是標楷體。已在「語言」中選「中文(台湾)」但是chrome book的字型中沒有出現標楷體,它唯一出現的中文字型是「新細明體」。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment