add online font support
This commit is contained in:
6
css/common/bootstrap.less
vendored
6
css/common/bootstrap.less
vendored
@@ -45,4 +45,10 @@
|
||||
|
||||
.input-group {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.list-group-item {
|
||||
.from {
|
||||
float: right;
|
||||
}
|
||||
}
|
||||
20
src/fonteditor/data/online-font.js
Normal file
20
src/fonteditor/data/online-font.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @file online-font.js
|
||||
* @author mengke01
|
||||
* @date
|
||||
* @description
|
||||
* 在线字体列表
|
||||
*/
|
||||
|
||||
|
||||
define(
|
||||
function(require) {
|
||||
return [
|
||||
{
|
||||
name: 'iconfont.ttf',
|
||||
url: './font/iconfont.ttf',
|
||||
from: '阿里妈妈图标库'
|
||||
}
|
||||
];
|
||||
}
|
||||
);
|
||||
48
src/fonteditor/dialog/font-online.js
Normal file
48
src/fonteditor/dialog/font-online.js
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @file setting-unicode.js
|
||||
* @author mengke01
|
||||
* @date
|
||||
* @description
|
||||
* 在线字体列表
|
||||
*/
|
||||
|
||||
define(
|
||||
function(require) {
|
||||
var string = require('common/string');
|
||||
var onlineList = require('../data/online-font');
|
||||
|
||||
return require('./setting').derive({
|
||||
|
||||
title: '在线字体',
|
||||
|
||||
nofooter: true,
|
||||
|
||||
getTpl: function() {
|
||||
|
||||
var str = '<div class="list-group">';
|
||||
onlineList.forEach(function(item, index) {
|
||||
str += '<a href="'+ item.url +'" class="list-group-item">'
|
||||
+ '<span class="from">'+ string.encodeHTML(item.from) +'</span>'
|
||||
+ (index + 1) + '. '
|
||||
+ string.encodeHTML(item.name)
|
||||
+ '</a>';
|
||||
});
|
||||
str += '</div>';
|
||||
return str;
|
||||
},
|
||||
|
||||
set: function() {
|
||||
var me = this;
|
||||
var dialog = this.getDialog();
|
||||
dialog.find('.list-group').delegate('.list-group-item', 'click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
var url = this.href;
|
||||
dialog.find('.list-group').off('[data-url]', 'click').remove();
|
||||
me.options.onChange && me.options.onChange.call(this, decodeURIComponent(url));
|
||||
$('#model-dialog').modal('hide');
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -25,8 +25,16 @@ define(
|
||||
dlg.find('.modal-title').html(this.title || '设置');
|
||||
dlg.find('.modal-body').html(this.getTpl());
|
||||
|
||||
if (this.nofooter) {
|
||||
dlg.find('.modal-footer').hide();
|
||||
}
|
||||
else {
|
||||
dlg.find('.modal-footer').show();
|
||||
}
|
||||
|
||||
dlg.on('hidden.bs.modal', lang.bind(function (e) {
|
||||
if (dlg) {
|
||||
this.onDispose && this.onDispose();
|
||||
delete this.options;
|
||||
dlg.off('hidden.bs.modal');
|
||||
dlg.find('.btn-confirm').off('click');
|
||||
|
||||
@@ -17,13 +17,15 @@ define(
|
||||
var TTFManager = require('../widget/ttfmanager');
|
||||
var program = require('../widget/program');
|
||||
var controller = require('../widget/controller/default');
|
||||
|
||||
var ajaxBinaryFile = require('common/ajaxBinaryFile');
|
||||
|
||||
var string = require('common/string');
|
||||
|
||||
|
||||
var setting = {
|
||||
'unicode': require('../dialog/setting-unicode'),
|
||||
'name': require('../dialog/setting-name')
|
||||
'name': require('../dialog/setting-name'),
|
||||
'online': require('../dialog/font-online')
|
||||
}
|
||||
|
||||
var actions = {
|
||||
@@ -55,6 +57,19 @@ define(
|
||||
});
|
||||
},
|
||||
|
||||
'add-online': function() {
|
||||
var dlg = new setting.online({
|
||||
onChange: function(url) {
|
||||
// 此处延迟处理
|
||||
setTimeout(function(){
|
||||
loadOnline(url);
|
||||
}, 20);
|
||||
}
|
||||
});
|
||||
|
||||
dlg.show();
|
||||
},
|
||||
|
||||
'setting-unicode': function() {
|
||||
var dlg = new setting.unicode({
|
||||
onChange: function(unicode) {
|
||||
@@ -81,6 +96,32 @@ define(
|
||||
}
|
||||
};
|
||||
|
||||
// 加载在线字体
|
||||
function loadOnline(url) {
|
||||
ajaxBinaryFile({
|
||||
url: url,
|
||||
onSuccess: function(buffer) {
|
||||
loader.load(buffer, {
|
||||
type: 'ttf',
|
||||
success: function(imported) {
|
||||
if (program.ttfmanager.get()) {
|
||||
program.ttfmanager.merge(imported, {
|
||||
scale: true
|
||||
});
|
||||
}
|
||||
else {
|
||||
program.ttfmanager.set(imported);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
},
|
||||
onError: function() {
|
||||
alert('加载文件错误!');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 设置unicode
|
||||
function setUnicode(unicode) {
|
||||
|
||||
@@ -131,7 +172,7 @@ define(
|
||||
type: file.name.slice(file.name.lastIndexOf('.') + 1),
|
||||
success: function(imported) {
|
||||
if (imported.glyf.length) {
|
||||
program.ttfmanager.combine(imported, {scale: true});
|
||||
program.ttfmanager.merge(imported, {scale: true});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -30,7 +30,7 @@ define(
|
||||
* @param {Function} options.success 成功回调
|
||||
* @param {Function} options.error 失败回调
|
||||
*/
|
||||
function loadSFNT(file, options) {
|
||||
function loadSFNTFile(file, options) {
|
||||
loading.show();
|
||||
var fileReader = new FileReader();
|
||||
fileReader.onload = function(e) {
|
||||
@@ -57,6 +57,28 @@ define(
|
||||
fileReader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加载sfnt结构字体
|
||||
*
|
||||
* @param {ArrayBuffer} buffer 二进制流
|
||||
* @param {Object} options 参数
|
||||
* @param {Object} options.type 文件类型
|
||||
* @param {Function} options.success 成功回调
|
||||
* @param {Function} options.error 失败回调
|
||||
*/
|
||||
function loadSFNTBinary(buffer, options) {
|
||||
loading.show();
|
||||
if (options.type == 'woff') {
|
||||
buffer = woff2ttf(buffer, woffOptions);
|
||||
}
|
||||
var ttfReader = new TTFReader();
|
||||
var ttf = ttfReader.read(buffer);
|
||||
ttfReader.dispose();
|
||||
loading.hide();
|
||||
options.success && options.success(ttf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载svg结构字体
|
||||
*
|
||||
@@ -104,7 +126,12 @@ define(
|
||||
loadSVG(file, options);
|
||||
}
|
||||
else if (options.type == 'ttf' || options.type == 'woff'){
|
||||
loadSFNT(file, options);
|
||||
if (file instanceof ArrayBuffer) {
|
||||
loadSFNTBinary(file, options);
|
||||
}
|
||||
else {
|
||||
loadSFNTFile(file, options);
|
||||
}
|
||||
}
|
||||
else {
|
||||
options.error && options.error({
|
||||
|
||||
@@ -37,7 +37,7 @@ define(
|
||||
*
|
||||
* @return {Object} 合并后的ttfObject
|
||||
*/
|
||||
function combine(ttf, imported, options) {
|
||||
function merge(ttf, imported, options) {
|
||||
options = options || {};
|
||||
|
||||
// 调整glyf以适应打开的文件
|
||||
@@ -146,8 +146,8 @@ define(
|
||||
*
|
||||
* @return {this}
|
||||
*/
|
||||
Manager.prototype.combine = function(imported, options) {
|
||||
var count = combine(this.ttf, imported, options);
|
||||
Manager.prototype.merge = function(imported, options) {
|
||||
var count = merge(this.ttf, imported, options);
|
||||
if (count) {
|
||||
this.fireChange(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user