# ant-rare-words-utils
[![npm package](https://img.shields.io/npm/v/ant-rare-words-utils.svg?style=flat-square)](https://www.npmjs.org/package/ant-rare-words-utils)
![NPM downloads](http://img.shields.io/npm/dm/ant-rare-words-utils.svg?style=flat-square)
## 字体加载
生僻字普遍存在无法显示的问题,这是由于缺乏能够显示当前生僻字字符的字体导致的,如果在显示字符的过程中在字体文件中找不到字符的字形数据,字符就会被显示成 �▤□ 这类字符。
我们提供了一个加载生僻字字体的方法,在需要展示生僻字的页面调用我们的方法,就可以解决生僻字显示问题了。
### 参数
| 参数 | 类型 | 说明 |
| ----------- | ------------------------ | --------------------------------------------------- |
| fontName | string | 自定义字体名称,默认是 rare-words-font |
| fontSrc | string | 生僻字字体远程下载地址,可以不传,系统有默认配置 |
| autoSetFont | boolean | 下载字体之后是否自动设置字体到 body 上,默认为 true |
| onSuccess | (font: FontFace) => void | 字体下载成功的回调 |
| onError | (err: Error) => void | 字体下载失败的回调 |
### 返回值
| 参数 | 类型 | 说明 |
| -------- | -------- | -------- |
| fontFace | FontFace | 字体对象 |
### 用法
#### npm 包方式引用
1. 安装工具包
```bash
npm install ant-rare-words-utils --save
```
2. 在页面初始化的逻辑里执行字体加载逻辑
```js
import { FontLoader } from 'ant-rare-words-utils';
new FontLoader({
fontName: 'rare-words-font',
onSuccess: () => {
console.log('字体加载成功', fontFace);
},
onError: err => {
console.log('字体加载失败', err);
}
});
```
加载成功以后会自动在 body 上设置上 font-family: 'rare-words-font'的样式属性,字体继承 body 元素的其他元素正常就可以支持展示生僻字了。
![字体加载完成示意图](https://mdn.alipayobjects.com/huamei_2fq7mt/afts/img/A*X5J2TolUxpMAAAAAAAAAAAAADh58AQ/original)
如果存在需要展示生僻字的元素,自定义了 css font-family 属性,则需要覆写 css font-family 属性,可以通过在样式文件里直接指定字体属性,也可以通过执行 js 的方式来附加字体到原有 font-family 属性上。
css 直接覆写
```css
.xxx {
font-familay: 原有字体, 'rare-words-font';
}
```
js 覆写
```javascript
const element = document.querySelector('.xxx');
const curBodyFontAttr = getComputedStyle(element).fontFamily;
const newBodyFontAttr = `${curBodyFontAttr}, '${fontFace.family}'`;
element.style.fontFamily = newBodyFontAttr;
```
#### cdn 方式引用
在一些没有使用 webpack 此类打包工具的业务中,无法做到使用 npm 包,可以在 html 内通过引入 cdn script 链接的方式加载生僻字工具库。
```html
<script type="text/javascript" src="https://unpkg.com/ant-rare-words-utils/dist/index.web.js" />
<script type="text/javascript">
new window.RareWordsUtils.FontLoader({
fontName: 'rare-words-font',
onSuccess: fontFace => {
console.log('字体加载成功', fontFace);
},
onError: err => {
console.log('字体加载失败', err);
}
});
</script>
```
> 如果 unpkg.com 被墙了,可以替换成 npm.elemecdn.com 的国内镜像,使用地址 https://npm.elemecdn.com/ant-rare-words-utils/dist/index.web.js
>
> 如果不希望跟随版本更新,可以限制引入 cdn 时的版本,使用地址 https://npm.elemecdn.com/ant-rare-words-utils@0.0.1/dist/index.web.js
## 判断是否为生僻字
有时候我们需要判断一串字符中是否包含生僻字,以决定是否需要为其加载生僻字字体,我们也提供了生僻字判断的一些函数。
- isContainRareWords 检查给定的字符串是否是包含生僻字的字符串
- isChineseNameValid 常规的中文校验规则[\u4e00-\u9fa5]是不完全的,使用此方法用于校验中文姓名是否符合规范,包含 GB18030-2022 全部汉字、少数民族 ·、以及 PUA 码段 E000-F8FF。
```js
import { isContainRareWords, isChineseNameValid } from 'ant-rare-words-utils';
isChineseNameValid('刘𪚔'); // return true
isContainRareWords('刘𪚔'); // return true
```
## 检索候选字
获取到字库数据以后,根据用户输入的拼音或者拆字信息,检索字库得到匹配的候选字列表
### 参数
| 参数 | 类型 | 说明 |
| ---------- | ---------- | ----------------------------------------- |
| wordsData | IWordsData | 字库数据 |
| inputValue | string | 当前输入的值 |
| filterKey | string | 过滤依据的 key 值,'pinyin','split','all' |
### 返回值
| 参数 | 类型 | 说明 |
| ---- | ---------- | ------------------------------ |
| list | IWordsData | 符合要求并且排序好的候选项列表 |
### 用法
```js
import { matchWordsRecommend } from 'ant-rare-words-utils';
matchWordsRecommend(wordsData, 'YAN', 'all');
```
## 获取字库数据
返回 demo 字库数据,数据格式如下:
```typescript
interface IWordsItem {
charId?: string; // 生僻字唯一ID
unicodeChar: string; // unicode 字符
unicodeCodePoint: string; // unicode 码点
unicodeFont: string | null; // 单个字字体文件地址
pinYinChars: string[]; // 拼音
splitChars: string[]; // 拆字
weight?: number | null; // 权重
type?: string; // 类型
extInfo?: string | null; // 其他信息
sort?: number; // 排序得分
}
type IWordsData = IWordsItem[];
export interface IZDatas {
version: string; // 字库版本
fontUrl: string; // 字体文件地址
data: IWordsData; // 字库数据
}
```
> !!! 一般情况下不需要调用此方法,除非想要自己实现输入键盘的情况下,或者研究 Z 字库数据结构。
### 参数
| 参数 | 类型 | 说明 |
| ----------- | ------- | ------------ |
| forceUpdate | boolean | 是否强制更新 |
### 返回值
| 参数 | 类型 | 说明 |
| -------- | -------- | -------- |
| fontFace | FontFace | 字体对象 |
| data | Object | 字库数据 |
### 用法
```js
import { getWordsData } from 'ant-rare-words-utils';
getWordsData({ forceUpdate: false })
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Z-RareCharacterSolution 是蚂蚁集团开源生僻字解决方案,围绕生僻字领域长期存在的各种痛点,提供了最小粒度的基础能力。蚂蚁集团研发的生僻字解决方案,致力于解决蚂蚁内部录入、显示、存储,及机构之间的生僻字信息互通传输的问题,并可以作为开源方案提供给全社会公共服务系统接入使用。
资源推荐
资源详情
资源评论
收起资源包目录
Z-RareCharacterSolution 是蚂蚁集团开源生僻字解决方案 (269个子文件)
.babelrc 67B
nginx.conf 361B
668.3a893a3a.chunk.css 97KB
umi.623c3751.css 22KB
demos.441fee59.chunk.css 20KB
docs__server__index.md.ffc024b8.chunk.css 2KB
docs__input__apmini.md.ffc024b8.chunk.css 2KB
docs__input__index.md.ffc024b8.chunk.css 2KB
docs__server__transfer.md.ffc024b8.chunk.css 2KB
docs__input__desktop.md.ffc024b8.chunk.css 2KB
docs__index.md.ffc024b8.chunk.css 2KB
docs__display__index.md.ffc024b8.chunk.css 2KB
docs__server__judge.md.ffc024b8.chunk.css 2KB
docs__server__same.md.ffc024b8.chunk.css 2KB
docs__server__recommend.md.ffc024b8.chunk.css 2KB
docs__input__mobile.md.ffc024b8.chunk.css 2KB
docs__input__native.md.ffc024b8.chunk.css 2KB
nm__dumi__dist__client__pages__404.8b85f2d9.chunk.css 514B
nm__dumi-theme-mobile__dist__layouts__DemoLayout.effca390.chunk.css 83B
nm__dumi__dist__client__pages__Demo__index.578aa5c0.chunk.css 25B
z_data.csv 58KB
Dockerfile 374B
Dockerfile 157B
Dockerfile 125B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.editorconfig 214B
.editorconfig 214B
.editorconfig 214B
.eslintrc 355B
.gitignore 413B
.gitignore 413B
.gitignore 101B
.gitignore 75B
.gitignore 75B
.gitignore 60B
.gitignore 20B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
404.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
index.html 374B
rarecharacterdemo-0.0.1-SNAPSHOT.jar 31.17MB
RareNameTransferManagerImpl.java 29KB
NameUtilTest.java 23KB
NameUtil.java 22KB
RareNameManagerImpl.java 16KB
RareNameApiImpl.java 8KB
RareNameServiceImpl.java 7KB
RareCharacterDO.java 6KB
InitDataController.java 5KB
InitRareCharactersData.java 5KB
RareCharacterControllerTest.java 4KB
RareCharacter.java 4KB
RareCharacterController.java 4KB
RareNameApi.java 4KB
RareCharacterDAOImpl.java 4KB
RareCharacterVO.java 4KB
EncodeTypeEnumUtil.java 3KB
RareCharacterDAO.java 3KB
RareCharacterResultCodeEnum.java 3KB
RareNameTransferManager.java 3KB
RareNameService.java 3KB
RareNameManager.java 2KB
BaseOrikaMapperImpl.java 2KB
RareNameAssociateResult.java 2KB
RareNameCommonResult.java 1KB
RareCharacterQueryResult.java 1KB
EncodeTypeEnum.java 1KB
CorsFilter.java 1KB
RareNameInfo.java 1KB
ServiceContext.java 957B
ObjectMapper.java 677B
ObjectMapperImpl.java 532B
RarecharacterdemoApplication.java 495B
RarecharacterdemoApplicationTests.java 232B
umi.6c50c721.js 774KB
831.fce72eee.async.js 714KB
668.6d6c5c0f.async.js 73KB
demos.51b33c37.async.js 35KB
969.3640aab2.async.js 8KB
nm__dumi-theme-mobile__dist__layouts__DemoLayout.f4854b73.async.js 8KB
docs__input__apmini.md.594d05f5.async.js 3KB
docs__input__mobile.md.3e4ba74a.async.js 3KB
docs__input__index.md.2c60783e.async.js 3KB
docs__input__desktop.md.f6ec06db.async.js 2KB
docs__server__judge.md.586e5aee.async.js 1KB
nm__dumi__theme-default__layouts__DocLayout__index.04639507.async.js 1KB
docs__server__recommend.md.6ef04239.async.js 1KB
docs__server__transfer.md.fff6f999.async.js 1KB
共 269 条
- 1
- 2
- 3
资源评论
- 小小程序员-2023-06-22总算找到了自己想要的资源,对自己的启发很大,感谢分享~
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6651
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功