perf: split font

This commit is contained in:
2025-06-02 22:16:50 +08:00
parent 08646fe984
commit c08a7a94a2
70 changed files with 729 additions and 19 deletions

View File

@@ -76,7 +76,7 @@ onMounted(() => {
class="text-4xl mr-2 fill-(--p-primary-color)" class="text-4xl mr-2 fill-(--p-primary-color)"
mode="svg" mode="svg"
/> />
<h1>SPARK</h1> <h1 class="font-(family-name:--s-title-font)">SPARK</h1>
</NuxtLink> </NuxtLink>
<div class="grow" /> <div class="grow" />
<NuxtLink to="/" class="nav-link" active-class="active"> <NuxtLink to="/" class="nav-link" active-class="active">
@@ -141,7 +141,6 @@ header {
} }
h1 { h1 {
font-family: "KNYuanmo";
font-size: 1.75em; font-size: 1.75em;
color: var(--p-surface-500); color: var(--p-surface-500);
} }

View File

@@ -1,13 +1,10 @@
@import "tailwindcss"; @import "tailwindcss";
@import 'primeicons/primeicons.css'; @import 'primeicons/primeicons.css';
@import '../fonts/KNYuanmo-Regular/result.css';
@font-face {
font-family: 'KNYuanmo';
font-display: swap;
src: url('../fonts/KNYuanmo-Regular.ttf');
}
:root { :root {
font-family: 'BlinkMacSystemFont', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
--s-title-font: 'KN Yuanmo SC', sans-serif;
--s-background: var(--p-secondary-50); --s-background: var(--p-secondary-50);
background-color: var(--s-background) !important; background-color: var(--s-background) !important;
} }

View File

@@ -0,0 +1,455 @@
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<title>字体分包构建报告</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<link
rel="stylesheet"
href="https://fastly.jsdelivr.net/npm/vant@4.9.15/lib/index.css"
/>
<link
href="https://unpkg.com/vue3-easy-data-table@1.5.47/dist/style.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="./result.css" />
<style>
body {
overflow-wrap: break-word;
font-size: 18px;
background-color: #edf0f3;
--easy-table-border: 0;
}
main {
padding: 20px;
margin-top: var(--van-nav-bar-height);
margin-bottom: var(--van-tabbar-height);
}
.light-blue {
--easy-table-body-row-font-color: #427cb7;
}
.light-red {
--easy-table-body-row-font-color: #b74273;
}
.hint {
display: block;
text-align: center;
color: gray;
margin: 1rem;
}
.scroller {
height: 100%;
}
.user {
height: 32%;
padding: 0 12px;
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div id="app">
<van-nav-bar
:title="reporter.state?.css?.family +' 分包报告'"
right-text="Github"
@click-right="toGithub"
style="position: fixed; width: 100%; top: 0"
></van-nav-bar>
<div v-if="reporter.isLoading">加载中</div>
<div v-if="reporter.isError">加载错误</div>
<main :style="style">
<van-cell-group inset title="样式控制">
<van-field
v-model="fontSize"
label="字体大小"
placeholder="请输入字体大小"
></van-field>
<van-field
v-model="fontWeight"
label="字体重量"
placeholder="请输入字重"
></van-field>
</van-cell-group>
<!-- 汇总信息 -->
<display-chart
v-if="reporter.isOk() && activePage === 0"
:reporter="reporter.state"
></display-chart>
<!-- 字体信息表格 -->
<name-table
v-if="reporter.isOk() && activePage === 1"
:reporter="reporter.state"
></name-table>
<!-- 单包查询 -->
<pkg-list
v-if="reporter.isOk() && activePage === 2"
:reporter="reporter.state"
></pkg-list>
<div class="hint">
<a href="https://chinese-font.netlify.app">中文网字计划</a>
<span> cn-font-split 生成 </span>
</div>
</main>
<!-- 底部导航栏 -->
<van-tabbar v-model="activePage">
<van-tabbar-item icon="home-o">汇总</van-tabbar-item>
<van-tabbar-item icon="home-o">字体信息</van-tabbar-item>
<van-tabbar-item icon="search">分包</van-tabbar-item>
</van-tabbar>
</div>
</body>
<script src="https://unpkg.com/vue@3.4.33"></script>
<script src="https://unpkg.com/vant@4/lib/vant.min.js"></script>
<script src="https://unpkg.com/vue3-easy-data-table@1.5.47/dist/vue3-easy-data-table.umd.js"></script>
<script src="https://unpkg.com/echarts@5.5.1"></script>
<script src="https://unpkg.com/vue-echarts@7.0.3"></script>
<script src="https://unpkg.com/protobufjs@7.4.0/dist/protobuf.min.js"></script>
<script>
const { createApp } = Vue;
const app = createApp({
data() {
return {
reporter: PromiseToState(loadReport),
activePage: 0,
fontWeight: '',
fontSize: 16,
};
},
methods: {
toGithub() {
window.open('https://github.com/KonghaYao/cn-font-split');
},
},
async mounted() {
const state = await this.reporter.refetch();
this.fontWeight = state.css.weight;
},
computed: {
style() {
const size = this.fontSize < 12 ? 12 : this.fontSize;
return (
`font-family: '${this.reporter.state?.css?.family}';` +
`font-weight: ${this.fontWeight};` +
`--van-cell-font-size: ${size}px;`
);
},
},
});
/** 封装Promise为状态对象 **/
function PromiseToState(fn) {
return {
state: {},
isOk() {
return !this.isLoading && !this.isError && this.state;
},
isLoading: false,
isError: false,
async refetch() {
this.isLoading = true;
this.isError = false;
try {
this.state = await fn();
} catch (e) {
console.error(e);
this.isError = true;
}
this.isLoading = false;
return this.state;
},
};
}
/** 载入构建出来的二进制报告文件 **/
async function loadReport() {
return new Promise((resolve, reject) => {
protobuf.load('./index.proto', (err, root) => {
if (err) return reject(err);
fetch('./reporter.bin')
.then((res) => res.arrayBuffer())
.then((buf) => {
const OutputReport = root.lookup('OutputReport');
const data = OutputReport.decode(
new Uint8Array(buf),
);
console.log(data);
resolve(data);
})
.catch(reject);
});
});
}
globalThis.app = app;
app.use(vant);
app.component('v-chart', VueECharts);
app.component('easy-data-table', window['vue3-easy-data-table']);
</script>
<!-- 汇总数据 -->
<template id="display-chart">
<van-cell-group inset title="构建信息">
<van-cell title="构建工具" value="cn-font-split"></van-cell>
<van-cell title="构建版本" :value="reporter.version"></van-cell>
<van-cell title="构建平台" :value="reporter.platform"></van-cell>
</van-cell-group>
<van-cell-group inset title="构建详情">
<div style="height: 300px; width: 400px">
<v-chart :option="pieOptions"></v-chart>
</div>
<div style="height: 400px; width: 100%">
<v-chart :option="option"></v-chart>
</div>
</van-cell-group>
</template>
<script>
const tooltip = {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999',
},
},
};
app.component('display-chart', {
template: '#display-chart',
props: {
reporter: Object,
},
computed: {
option() {
const data = this.reporter.subsetDetail ?? [];
return {
title: {
text: '构建耗时',
x: 'center',
},
xAxis: { type: 'category' },
tooltip,
yAxis: [
{
type: 'value',
name: 'time / ms',
},
{
type: 'value',
name: 'bytes / KB',
},
],
series: [
{
name: 'time',
type: 'bar',
data: data.map((i) => i.duration),
},
{
name: 'bytes',
type: 'line',
data: data.map((i) =>
(i.bytes / 1024).toFixed(2),
),
},
],
};
},
pieOptions() {
const message = this.reporter.bundleMessage;
if (!message) return {};
return {
title: {
text: '构建大小对比',
x: 'center',
},
series: [
{
type: 'pie',
radius: ['45%', '60%'],
label: {
formatter(b, c) {
return `${b.name}\n${(
b.value /
(1024 * 1024)
).toFixed(2)}MB`;
},
},
data: [
{
value: message.bundledBytes,
name: 'bundled',
},
{
value: message.originBytes,
name: 'origin',
},
],
},
{
type: 'pie',
radius: [0, '30%'],
label: {
formatter(b, c) {
return `${b.name}\n${b.value}`;
},
},
data: [
{
value: message.bundledSize,
name: 'bundled',
},
{
value: message.originSize,
name: 'origin',
},
],
},
],
};
},
},
});
</script>
<!-- 字体信息表格 -->
<template id="name-table">
<van-cell-group inset title="CSS 信息">
<van-cell
title="font-family"
:value="reporter.css?.family"
></van-cell>
<van-cell
title="font-weight"
:value="reporter.css?.weight"
></van-cell>
<van-cell
title="font-style"
:value="reporter.css?.style"
></van-cell>
<van-cell
title="font-display"
:value="reporter.css?.display"
></van-cell>
</van-cell-group>
<van-cell-group inset title="字体信息">
<easy-data-table
:headers="columns"
:items="reporter.nameTable??[]"
:rows-per-page="1000"
:body-row-class-name="useColor"
></easy-data-table>
</van-cell-group>
</template>
<script>
app.component('name-table', {
template: '#name-table',
props: {
reporter: Object,
},
data() {
return {
color: {
Windows: 'light-blue',
Macintosh: 'light-red',
},
columns: [
{
text: '语言',
value: 'language',
sortable: true,
minWidth: '36px',
},
{
text: '平台',
value: 'platform',
sortable: true,
minWidth: '20px',
},
{
text: '名称',
value: 'name',
sortable: true,
minWidth: '50px',
},
{
text: '值',
value: 'value',
sortable: true,
width: '200px',
},
],
};
},
methods: {
useColor(item) {
return this.color[item.platform];
},
},
});
</script>
<!-- 每个包的数据 -->
<template id="pkg-list">
<van-cell-group inset>
<van-field
v-model="search"
label="搜索"
placeholder="请输入关键字"
></van-field>
</van-cell-group>
<van-cell-group inset title="字体分包详情">
<v-list
ref="list"
:items="items"
:first-render="10"
style="height: 80vh"
>
<template #default="{ item, index }">
<div style="padding: 0 1rem">
<h2 :style="{ width: item.width }">
ITEM: {{ index }} - {{ item.hash }}
</h2>
<span> {{String.fromCodePoint(...item.chars)}} </span>
</div>
</template>
</v-list>
</van-cell-group>
</template>
<script src="https://unpkg.com/@virtual-list/vue/dist/v3/index.iife.js"></script>
<script>
app.component('v-list', virtualListVue);
app.component('pkg-list', {
template: '#pkg-list',
props: {
reporter: Object,
},
data() {
return {
activeNames: [],
search: '',
};
},
mounted() {},
computed: {
items() {
const list = this.reporter.subsetDetail ?? [];
if (this.search) {
return list.filter((i) => i.hash.includes(this.search));
}
return list;
},
},
});
</script>
<script>
app.mount('#app');
</script>
</html>

View File

@@ -0,0 +1,117 @@
syntax = "proto3";
package api_interface;
message InputTemplate {
bytes input = 1; // ttf/woff2 文件的 buffer
optional string out_dir = 2; // 切割后放置文件的文件夹
// ====== 构建产物配置 ======
optional CssProperties css = 5; // CSS 配置
optional string target_type = 6; // 目标类型
repeated bytes subsets = 7; // 子集
optional int32 chunk_size = 9; // 包大小
optional float chunk_size_tolerance = 10; // 包大小容差
optional int32 max_allow_subsets_count = 11; // 最大允许子集数量
optional bool test_html = 13; // 是否生成测试 HTML
optional bool reporter = 14; // 是否生成 reporter.bin
optional PreviewImage preview_image = 15; // 预览图像
optional string rename_output_font = 18; // 重命名输出字体
optional string build_mode = 20; // TODO 构建模式
// ====== 预分包优化项配置 =======
optional bool language_areas = 8; // 是否进行语言区域优化
optional bool multi_threads = 21; // TODO 是否使用多线程
optional bool font_feature = 22; // 是否启用字体特性
optional bool reduce_mins = 23; // 是否减少最小分包,
optional bool auto_subset = 24; // 是否自动子集化
optional bool subset_remain_chars = 25; // 是否自动添加没有声明的字符
// CSS 属性配置
message CssProperties {
optional string font_family = 1; // 字体家族名称
optional string font_weight = 2; // 字体粗细
optional string font_style = 3; // 字体样式
optional string font_display = 4; // 字体显示方式
repeated string local_family = 5; // 本地字体家族名称
repeated PolyfillType polyfill = 6; // Polyfill 类型
// 注释属性
optional bool comment_base = 11; // 基础注释
optional bool comment_name_table = 12; // 名称表注释
optional bool comment_unicodes = 13; // Unicode 注释
optional bool compress = 8; // 是否压缩
optional string file_name = 9; // 文件名
}
// Polyfill 类型
message PolyfillType {
string name = 1; // 名称
string format = 2; // 格式
}
// 预览图像
message PreviewImage {
string text = 1; // 会显示在 svg 中的文本
string name = 2; // svg 文件的名称
}
}
enum EventName {
UNSPECIFIED = 0;
OUTPUT_DATA = 1; // "output_data"
END = 2; // "end"
}
message EventMessage {
EventName event = 1;
string message = 2;
optional bytes data = 3;
}
message MultiMessages {
repeated EventMessage messages = 1;
}
message OutputReport {
string version = 1; // 版本号
Css css = 2; // CSS相关信息可以直接用
string platform = 3; // 平台信息rust 的构建平台
BundleMessage bundle_message = 24; // 构建消息
repeated NameTable name_table = 25; // name 表信息
repeated SubsetDetail subset_detail = 26; // 子集详情
message NameTable {
string platform = 1; // 平台
string language = 2; // 语言
string name = 3; // 名称
string value = 4; // 值
}
message SubsetDetail {
uint32 id = 1; // ID
string hash = 2; // 哈希值
string file_name = 6; // 文件名称
uint32 bytes = 3; // 字节数
repeated uint32 chars = 4; // 字符数组
uint32 duration = 5; // 构建时间
}
message BundleMessage {
uint32 origin_size = 1; // 原始大小
uint32 bundled_size = 2; // 产物大小
uint32 origin_bytes = 3; // 原始字节数
uint32 bundled_bytes = 4; // 产物字节数
}
message Css {
string family = 1; // 字体家族
string style = 2; // 样式
string weight = 3; // 字重
string display = 4; // 显示方式
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -119,16 +119,14 @@ onMounted(() => {
<i class="w-96 h-144 figure-container" /> <i class="w-96 h-144 figure-container" />
<div class="card flex flex-col px-9 py-15 rounded-tl-3xl gap-8"> <div class="card flex flex-col px-9 py-15 rounded-tl-3xl gap-8">
<div class="flex gap-4"> <div class="flex gap-4">
<h1 class="font-bold text-6xl leading-[0.9] -translate-y-1/20"> <h1 class="font-bold text-6xl -translate-y-1/20">星火应用商店</h1>
星火应用商店
</h1>
<div class="flex flex-col items-start justify-between"> <div class="flex flex-col items-start justify-between">
<span <span
class="font-[KNYuanmo] px-4 py-0.5 text-white from-(--p-primary-400) to-(--p-primary-500) bg-linear-to-r rounded-full" class="font-(family-name:--s-title-font) px-4 py-0.5 text-white from-(--p-primary-400) to-(--p-primary-500) bg-linear-to-r rounded-full"
>V4.5.2</span >V4.5.2</span
> >
<h2 <h2
class="text-lg font-[KNYuanmo] text-(--p-primary-500) leading-[0.8]" class="text-lg font-(family-name:--s-title-font) text-(--p-primary-500)"
> >
SPARK STORE SPARK STORE
</h2> </h2>
@@ -207,7 +205,7 @@ onMounted(() => {
class="flex flex-col items-center justify-between mt-2 mb-6 gap-5" class="flex flex-col items-center justify-between mt-2 mb-6 gap-5"
> >
<h2 <h2
class="text-3xl text-(--p-primary-500) font-[KNYuanmo] tracking-widest" class="text-3xl text-(--p-primary-500) font-(family-name:--s-title-font) tracking-widest"
> >
SPARK SPARK
</h2> </h2>
@@ -316,7 +314,9 @@ onMounted(() => {
class="text-6xl fill-(--p-primary-500)" class="text-6xl fill-(--p-primary-500)"
mode="svg" mode="svg"
/> />
<h2 class="text-4xl font-[KNYuanmo] text-(--p-primary-500)"> <h2
class="text-4xl font-(family-name:--s-title-font) text-(--p-primary-500)"
>
WHAT'S NEW ? WHAT'S NEW ?
</h2> </h2>
</div> </div>
@@ -346,7 +346,8 @@ onMounted(() => {
}" }"
/> />
<h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]"> <h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]">
<span class="font-[KNYuanmo] text-(--p-primary-400) font-normal" <span
class="font-(family-name:--s-title-font) text-(--p-primary-400) font-normal"
>COMMUNITY</span >COMMUNITY</span
><br /> ><br />
社区共筑&nbsp;&nbsp;精挑细选 社区共筑&nbsp;&nbsp;精挑细选
@@ -371,7 +372,9 @@ onMounted(() => {
全架构<br /> 全架构<br />
上架应用 上架应用
</p> </p>
<p class="text-7xl font-[KNYuanmo] text-(--p-primary-500)"> <p
class="text-7xl font-(family-name:--s-title-font) text-(--p-primary-500)"
>
{{ {{
Math.floor( Math.floor(
2200 * 2200 *
@@ -433,7 +436,8 @@ onMounted(() => {
}" }"
/> />
<h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]"> <h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]">
<span class="font-[KNYuanmo] text-(--p-primary-400) font-normal" <span
class="font-(family-name:--s-title-font) text-(--p-primary-400) font-normal"
>SUPERSPEED</span >SUPERSPEED</span
><br /> ><br />
极速下载&nbsp;&nbsp;瞬息可达 极速下载&nbsp;&nbsp;瞬息可达
@@ -487,7 +491,8 @@ onMounted(() => {
}" }"
/> />
<h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]"> <h2 class="text-5xl text-(--p-primary-600) font-bold leading-[1.3]">
<span class="font-[KNYuanmo] text-(--p-primary-400) font-normal" <span
class="font-(family-name:--s-title-font) text-(--p-primary-400) font-normal"
>CONVENIENCE</span >CONVENIENCE</span
><br /> ><br />
一键安装&nbsp;&nbsp;即刻掌控 一键安装&nbsp;&nbsp;即刻掌控