add read online

This commit is contained in:
mkwiser
2014-10-31 00:44:36 +08:00
parent ee6f9791c5
commit c41331dd65
8 changed files with 102 additions and 31 deletions

View File

@@ -18,11 +18,6 @@ build_tpl() {
sed -e "s#'\.\/src'#'./$version'#g" |
tr -s "\n" " " |
sed 's#[[:space:]]\+# #g' > ./release/index.html
cat ./release/ttf.html |
sed -e "s#'\.\/src'#'./$version'#g" |
tr -s "\n" " " |
sed 's#[[:space:]]\+# #g' > ./release/ttf.html
}

View File

@@ -22,6 +22,7 @@
<ul class="dropdown-menu" role="menu">
<li data-action="add-new"><a>新字形</a></li>
<li data-action="add-online"><a>在线字体</a></li>
<li data-action="add-url"><a>线上地址</a></li>
</ul>
</div>

6
php/readFont.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
header( "Content-Type: application/octet-stream");
$file = $_GET['file'];
if ($file && preg_match("#^https?:\/\/.+?\.(ttf|woff|svg|eot)$#i", $file, $matches)) {
echo file_get_contents($matches[0]);
}

View File

@@ -0,0 +1,34 @@
/**
* @file font-url.js
* @author mengke01
* @date
* @description
* 读取线上字体
*/
define(
function(require) {
var tpl = ''
+ '<div class="input-group input-group-sm">'
+ '<span class="input-group-addon">字体URL</span>'
+ '<input data-field="url" type="text" class="form-control">'
+ '</div>';
return require('./setting').derive({
title: '线上字体',
getTpl: function() {
return tpl;
},
validate: function() {
var setting = this.getFields();
if (setting.url) {
return setting.url;
}
}
});
}
);

View File

@@ -11,7 +11,8 @@ define(
function(require) {
var lang = require('common/lang');
var pad = require('common/string').pad;
var program = require('../widget/program');
/**
* 设置框函数
*
@@ -33,6 +34,7 @@ define(
}
dlg.on('hidden.bs.modal', lang.bind(function (e) {
program.listening = true;
if (dlg) {
this.onDispose && this.onDispose();
delete this.options;
@@ -179,6 +181,7 @@ define(
* @param {Object} setting 设置选项
*/
Setting.prototype.show = function(setting) {
program.listening = false;
$('#model-dialog').modal('show');
this.set(setting);
return this;

View File

@@ -14,6 +14,36 @@ define(
var ajaxFile = require('common/ajaxFile');
var string = require('common/string');
// 读取在线字体
function readOnlineFont(type, url) {
ajaxFile({
type: type === 'svg' ? 'xml' : 'binary',
url: url,
onSuccess: function(buffer) {
program.loader.load(buffer, {
type: type || 'ttf',
success: function(imported) {
program.loading.hide();
if (program.ttfManager.get()) {
program.ttfManager.merge(imported, {
scale: true
});
}
else {
program.ttfManager.set(imported);
program.data.projectName = null;
}
}
});
},
onError: function() {
alert('加载文件错误!');
}
});
}
var actions = {
// 新建
@@ -89,31 +119,7 @@ define(
// 此处延迟处理
setTimeout(function(){
program.loading.show('正在加载..', 1000);
ajaxFile({
type: url.slice(url.lastIndexOf('.') + 1) === 'svg' ? 'xml' : 'binary',
url: url,
onSuccess: function(buffer) {
program.loader.load(buffer, {
type: url.slice(url.lastIndexOf('.') + 1) || 'ttf',
success: function(imported) {
program.loading.hide();
if (program.ttfManager.get()) {
program.ttfManager.merge(imported, {
scale: true
});
}
else {
program.ttfManager.set(imported);
program.data.projectName = null;
}
}
});
},
onError: function() {
alert('加载文件错误!');
}
});
readOnlineFont(url.slice(url.lastIndexOf('.') + 1), url);
}, 20);
}
});
@@ -121,6 +127,22 @@ define(
dlg.show();
},
// 线上地址
'add-url': function() {
var dlg = new setting.url({
onChange: function(url) {
// 此处延迟处理
setTimeout(function(){
program.loading.show('正在加载..', 1000);
var fontUrl = string.format(program.fontUrl, [encodeURIComponent(url)]);
readOnlineFont(url.slice(url.lastIndexOf('.') + 1), fontUrl);
}, 20);
}
});
dlg.show();
},
// 设置unicode
'setting-unicode': function() {
var dlg = new setting.unicode({

View File

@@ -47,6 +47,10 @@ define(
document.body.addEventListener('keydown', function(e) {
if (!program.listening) {
return;
}
// 全选
if (65 === e.keyCode && e.ctrlKey) {
e.preventDefault();
@@ -76,6 +80,9 @@ define(
}
var program = {
// 在线地址读取接口
fontUrl: './php/readFont.php?file=${0}',
/**
* 初始化
@@ -100,6 +107,8 @@ define(
ttfManager: null, // ttf管理器
listening: true, // 正在监听事件
loading: require('./loading')
};

View File

@@ -15,6 +15,7 @@ define(
'adjust-glyf': require('../dialog/setting-adjust-glyf'),
'metrics': require('../dialog/setting-metrics'),
'online': require('../dialog/font-online'),
'url': require('../dialog/font-url'),
'glyf': require('../dialog/setting-glyf')
};
}