/** * ascent descent * SVGascent, descent and baseline https://riptutorial.com/svg/example/26220/ascent--descent-and-baseline * SVG https://www.w3.org/TR/SVG/text.html#DominantBaselineProperty * vert-adv-y 默认值为 ascent 与 descent 总和 */ export function fontTemplate(DEFAULT_CONFIG) { const { font, fontface, glyphs = [] } = DEFAULT_CONFIG const TMPL = ` Copyright (C) 2019 by original authors @ master Gao ${ glyphs.map(glyph => ` `).join('')} ` return TMPL } export function fontCSSTemplate(fontTypes, fontName, fontFamily, fontFamilyClass, glyphs = [], fontCdnUrl = '') { const CSSTMPL = ` @font-face { font-family: '${fontFamily}'; ${fontTypes.includes('eot') && `src: url('${fontCdnUrl}${fontName}.eot'); /* IE9 */`} src: ${fontTypes.map(item => { if(item == 'eot'){ return `url('${fontCdnUrl}${fontName}.eot?#iefix') format('embedded-opentype') /* IE6-IE8 */` }else if(item == 'woff2'){ return `url('${fontCdnUrl}${fontName}.woff2') format('woff2') /* chrome、firefox */` }else if(item == 'woff'){ return `url('${fontCdnUrl}${fontName}.woff') format('woff') /* chrome、firefox */` }else if(item == 'ttf'){ return `url('${fontCdnUrl}${fontName}.ttf') format('truetype') /* chrome、firefox、opera、Safari, Android, iOS 4.2+ */` }else if(item == 'svg'){ return `url('${fontCdnUrl}${fontName}.svg#${fontFamily}') format('svg') /* iOS 4.1- */` } }).join(',\n\t\t')}; } .${fontFamilyClass}{ font-family: '${fontFamily}'; font-size: 16px; font-style: normal; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } ${ glyphs.map(({glyphName, unicode}) => ` .${glyphName}::before { content: "\\${unicode}"; }`).join('\n')}` return CSSTMPL } export function svgSymbolTemplate(fontTypes, fontName, glyphs = []) { const SYMBOLTMPL = ` (function (document) { var symbols = '${ glyphs.map(({glyphName, originDs}) => `${ originDs.map(({d, fill}) => ``).join('') }`).join('')}' document.body.insertAdjacentHTML('afterBegin', symbols) })(document);` return SYMBOLTMPL } export function htmlTemplate(fontTypes, fontName, fontFamilyClass, glyphs = []) { const HTMLTMPL = ` Quarkicon Demo

QuarkIcon

    ${ glyphs.map(({originName, unicode}) => `
  • &#x${unicode};
    ${originName}
    &#x${unicode};
  • `).join('') }

Unicode 引用


Unicode 是字体在网页端最原始的应用方式,特点是:

  • 兼容性最好,支持 IE6+,及所有现代浏览器。
  • 支持按字体的方式去动态调整图标大小,颜色等等。
  • 但是因为是字体,所以不支持多色。只能使用平台里单色的图标,就算项目里有多色图标也会自动去色。

注意:新版 QuarkIcon 支持多色图标,这些多色图标在 Unicode 模式下将不能使用,如果有需求建议使用 symbol 的引用方式

Unicode 使用步骤如下:

第一步:拷贝项目下面生成的 @font-face

@font-face {
    font-family: 'font_family';
    src: url('quarkicon.eot');
    src: url('quarkicon.eot?#iefix') format('embedded-opentype'),
        url('quarkicon.woff2') format('woff2'),
        url('quarkicon.woff') format('woff'),
        url('quarkicon.ttf') format('truetype'),
        url('quarkicon.svg#font_family') format('svg');
  }
  

第二步:定义使用 quarkicon 的样式

.font_family {
    font-family: "font_family" !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
  

第三步:挑选相应图标并获取字体编码,应用于页面

  <span class="font_family">&#x33;</span>
  

"font_family" 是你项目下的 font-family。可以通过编辑项目查看,默认是 "quarkicon"。

    ${ glyphs.map(({glyphName, originName, unicode}) => `
  • ${originName}
    .${glyphName}
  • `).join('') }

font-class 引用


font-class 是 Unicode 使用方式的一种变种,主要是解决 Unicode 书写不直观,语意不明确的问题。

与 Unicode 使用方式相比,具有如下特点:

  • 兼容性良好,支持 IE8+,及所有现代浏览器。
  • 相比于 Unicode 语意明确,书写更直观。可以很容易分辨这个 icon 是什么。
  • 因为使用 class 来定义图标,所以当要替换图标时,只需要修改 class 里面的 Unicode 引用。
  • 不过因为本质上还是使用的字体,所以多色图标还是不支持的。

使用步骤如下:

第一步:引入项目下面生成的 fontclass 代码:

<link rel="stylesheet" href="./quarkicon.css">
  

第二步:挑选相应图标并获取类名,应用于页面:

<span class="font_family icon-xxx"></span>
  

" font_family" 是你项目下的 font-family。可以通过编辑项目查看,默认是 "quarkicon"。

    ${ glyphs.map(({glyphName,originName, unicode}) => `
  • ${originName}
    #${glyphName}
  • `).join('') }

Symbol 引用


这是一种全新的使用方式,应该说这才是未来的主流,也是平台目前推荐的用法。相关介绍可以参考这篇文章 这种用法其实是做了一个 SVG 的集合,与另外两种相比具有如下特点:

  • 支持多色图标了,不再受单色限制。
  • 通过一些技巧,支持像字体那样,通过 font-size, color 来调整样式。
  • 兼容性较差,支持 IE9+,及现代浏览器。
  • 浏览器渲染 SVG 的性能一般,还不如 png。

使用步骤如下:

第一步:引入项目下面生成的 symbol 代码:

<script src="./quarkicon.js"></script>
  

第二步:加入通用 CSS 代码(引入一次就行):

<style>
  .icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
  </style>
  

第三步:挑选相应图标并获取类名,应用于页面:

<svg class="icon" aria-hidden="true">
    <use xlink:href="#icon-xxx"></use>
  </svg>
  
` return HTMLTMPL } export function AndroidTemplate(fontName, glyphs = []) { const AndroidTMPL = ` ${ glyphs.map(({glyphName, originName, unicode}) => `&#x${unicode}; `).join('\n ') } ` return AndroidTMPL } export function iOSTemplate(fontFamily, glyphs = []) { const upCaseFontFamily = fontFamily.replace(fontFamily[0], fontFamily[0].toUpperCase()) const iOSTMPL = ` // // JDIF_${upCaseFontFamily}.h // fontFamily:${fontFamily} // // Version 1.0 // Copyright (C) 2019 by original authors @ master Gao // #ifndef JDIF_${upCaseFontFamily}_H #define JDIF_${upCaseFontFamily}_H NSString * const JDIF_${upCaseFontFamily} = @"${fontFamily}"; ${ glyphs.map(({glyphName, originName, unicode}) => `NSString * const JDIF_${glyphName.replace(/-/g, '_').toUpperCase()} = @"\\U${unicode.padStart(8, 0)}"; // ${originName}`).join('\n ') } #endif` return iOSTMPL } export function RNTemplate(fontFamily, glyphs = []) { const upCaseFontFamily = fontFamily.replace(fontFamily[0], fontFamily[0].toUpperCase()) const RNTMPL = `export default { ${ glyphs.map(({glyphName, originName, unicode}) => `'${glyphName}': ${parseInt(unicode, 16)},// ${originName}`).join('\n ') } }` return RNTMPL }