Skip to content

Custom font #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
76a8ef1
Available to set any font (WIP)
hidakatsuya Mar 18, 2018
657d264
Fix UI for FontFamilyProperty
hidakatsuya Mar 19, 2018
57fcb36
Refactor
hidakatsuya Mar 19, 2018
1394411
Check the specified family is a valid font
hidakatsuya Mar 21, 2018
c10ba30
Make selectable the font once used
hidakatsuya Mar 24, 2018
7c7b48c
Fixed undo/redo does not work when cusor is in a text-field
hidakatsuya Apr 4, 2018
eb8ed51
Save custom-fonts in *.tlf
hidakatsuya May 4, 2018
eaaa04d
custom-fonts is sorted by acending
hidakatsuya May 4, 2018
38fdc97
Fixed AllUsedFontFamilies does not include font in List
hidakatsuya May 4, 2018
c2bc6e4
Fix comment format
hidakatsuya May 5, 2018
456dcaf
Restore custom-fonts from .tlf
hidakatsuya May 5, 2018
e5a055a
Remove unnecessary code
hidakatsuya May 5, 2018
43c39cc
Fixed Combobox value breaks when undoing/redoing
hidakatsuya May 6, 2018
8dcd16e
make disposable
hidakatsuya May 6, 2018
789807d
fix typo
hidakatsuya May 6, 2018
e5fe5d8
Show warning in the font option when the font is not installed
hidakatsuya May 6, 2018
e652785
Use yarn also in app
hidakatsuya Jun 5, 2018
aaa6f11
Install FontMetrics
hidakatsuya Jun 5, 2018
28fc56c
Add accesor for FontMetrics
hidakatsuya Jun 5, 2018
9565c6c
Implement accessor for App
hidakatsuya Jun 5, 2018
e1af945
(WIP) Use FontMetrics for calculating font metrics
hidakatsuya Jun 6, 2018
9c1de34
Fix height of PageNumber is not applied by changing the font-family
hidakatsuya Jun 8, 2018
62ec24b
Add anotation
hidakatsuya Jun 8, 2018
8e4dcbf
Add accessor for CustomFontRegistry
hidakatsuya Jun 8, 2018
c171cfa
Revert "Fix height of PageNumber is not applied by changing the font-…
hidakatsuya Jun 8, 2018
36c3fe6
Fix height of PageNumber is not applied by changing the font-family
hidakatsuya Jun 8, 2018
f291280
Add buffer size to text width
hidakatsuya Jun 8, 2018
5df2885
Set ぽ as char for calculationg Ascent of Font
hidakatsuya Jun 8, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/assets/css-files.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2012 Matsukei Co.,Ltd.
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

Expand Down Expand Up @@ -43,3 +43,4 @@
- toolbox.css
- view.css
- workspace.css
- fontvalidator.css
7 changes: 7 additions & 0 deletions app/assets/fontoption.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
box-sizing: border-box;
}

.thin-font-option-invalid .thin-font-option-content {
background-image: url(icons/exclamation-octagon.png);
background-position: 0% 50%;
background-repeat: no-repeat;
padding-left: 18px;
}

/*
* FontOptionMenu
*/
Expand Down
15 changes: 15 additions & 0 deletions app/assets/fontvalidator.css

Large diffs are not rendered by default.

105 changes: 67 additions & 38 deletions app/editor/base/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
goog.provide('thin.Font');

goog.require('goog.array');
goog.require('goog.Disposable');
goog.require('thin.platform.Font');
goog.require('thin.platform.FontValidator');


/**
* @param {string} family
* @param {string=} opt_name
* @param {boolean=} opt_builtin
* @constructor
* @extends {goog.Disposable}
*/
thin.Font = function(family, opt_name, opt_builtin) {
thin.Font = function(family, opt_name) {
/**
* @type {string}
* @private
Expand All @@ -37,20 +39,15 @@ thin.Font = function(family, opt_name, opt_builtin) {
* @private
*/
this.name_ = opt_name || family;

/**
* @type {boolean}
* @private
*/
this.builtin_ = opt_builtin || false;
};
goog.inherits(thin.Font, goog.Disposable);


/**
* @type {Array.<thin.Font>}
* @private
*/
thin.Font.fontRegistry_ = [];
thin.Font.builtinFontRegistry_ = [];


/**
Expand All @@ -63,12 +60,11 @@ thin.Font.defaultFont_;
/**
* @param {string} family
* @param {string=} opt_name
* @param {boolean=} opt_builtin
* @return {thin.Font}
*/
thin.Font.register = function(family, opt_name, opt_builtin) {
var font = new thin.Font(family, opt_name || family, opt_builtin);
thin.Font.fontRegistry_.push(font);
thin.Font.register = function(family, opt_name) {
var font = new thin.Font(family, opt_name || family);
thin.Font.builtinFontRegistry_.push(font);
return font;
};

Expand All @@ -78,12 +74,12 @@ thin.Font.init = function() {

font.defaultFont_ = font.register('Helvetica', null, true);

font.register('Courier New', null, true);
font.register('Times New Roman', null, true);
font.register('IPAMincho', 'IPA ' + thin.t('font_mincho'), true);
font.register('IPAPMincho', 'IPA P' + thin.t('font_mincho'), true);
font.register('IPAGothic', 'IPA ' + thin.t('font_gothic'), true);
font.register('IPAPGothic', 'IPA P' + thin.t('font_gothic'), true);
font.register('Courier New');
font.register('Times New Roman');
font.register('IPAMincho', 'IPA ' + thin.t('font_mincho'));
font.register('IPAPMincho', 'IPA P' + thin.t('font_mincho'));
font.register('IPAGothic', 'IPA ' + thin.t('font_gothic'));
font.register('IPAPGothic', 'IPA P' + thin.t('font_gothic'));
};


Expand All @@ -98,19 +94,38 @@ thin.Font.getDefaultFontFamily = function() {
/**
* @return {Array.<thin.Font>}
*/
thin.Font.getFonts = function() {
return thin.Font.fontRegistry_;
thin.Font.getBuiltinFonts = function() {
return thin.Font.builtinFontRegistry_;
};


/**
* @param {string} family
* @return {boolean}
*/
thin.Font.isRegistered = function (family) {
var detected = thin.Font.findFontByFamily(family);
return detected !== null;
};


/**
* @param {string} family
* @return {thin.Font?}
*/
thin.Font.findFontByFamily = function (family) {
return goog.array.find(thin.Font.fontRegistry_,
function (font) {
return font.getFamily() == family;
});
};


/**
* @type {Object.<Object>}
* @private
*/
thin.Font.infoRegistry_ = {
ascent: {},
height: {}
};
thin.Font.infoRegistry_ = {};


/**
Expand All @@ -131,28 +146,33 @@ thin.Font.generateRegistryKey_ = function(var_args) {
*/
thin.Font.getAscent = function(family, fontSize, isBold) {
var registryKey = thin.Font.generateRegistryKey_(family, fontSize, isBold);
var ascent = thin.Font.infoRegistry_.ascent[registryKey];
if (!goog.isDef(ascent)) {
ascent = thin.platform.Font.getAscent(family, fontSize, isBold);
thin.Font.infoRegistry_.ascent[registryKey] = ascent;
var info = thin.Font.infoRegistry_[registryKey];

if (!goog.isDef(info)) {
info = thin.platform.Font.getMetrics(family, fontSize, isBold);
thin.Font.infoRegistry_[registryKey] = info;
}
return ascent;

return info.ascent;
};


/**
* @param {string} family
* @param {number} fontSize
* @param {boolean} isBold
* @return {number}
*/
thin.Font.getHeight = function(family, fontSize) {
thin.Font.getHeight = function(family, fontSize, isBold) {
var registryKey = thin.Font.generateRegistryKey_(family, fontSize);
var height = thin.Font.infoRegistry_.height[registryKey];
if (!goog.isDef(height)) {
height = thin.platform.Font.getHeight(family, fontSize);
thin.Font.infoRegistry_.height[registryKey] = height;
var info = thin.Font.infoRegistry_[registryKey];

if (!goog.isDef(info)) {
info = thin.platform.Font.getMetrics(family, fontSize, isBold);
thin.Font.infoRegistry_[registryKey] = info;
}
return height;

return info.height;
};


Expand All @@ -175,6 +195,15 @@ thin.Font.prototype.getName = function() {
/**
* @return {boolean}
*/
thin.Font.prototype.isBuiltin = function() {
return this.builtin_;
thin.Font.prototype.isValid = function () {
return thin.platform.FontValidator.validate(this.family_);
};


/** @override */
thin.Font.prototype.disposeInternal = function () {
goog.base(this, 'disposeInternal');

this.family_ = null;
this.name_ = null;
};
82 changes: 22 additions & 60 deletions app/editor/base/platform/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,32 @@ goog.provide('thin.platform.Font');

goog.require('goog.dom');
goog.require('thin.platform');
goog.require('thin.core.TextHelper');


/**
* @type {string}
* @private
*/
thin.platform.FIRST_LINE_TEXT_ = 'Agjぽ';


/**
* @param {string} family
* @param {number} fontSize
* @param {boolean} isBold
* @param {string} family
* @param {number} fontSize
* @param {boolean} isBold
* @return {Object}
*/
thin.platform.Font.getTextLineSpec = function(family, fontSize, isBold) {
var layout = thin.core.getActiveWorkspace().getLayout();

var textHelper = new thin.core.TextHelper(layout);
textHelper.setFontSize(fontSize);
textHelper.setFontFamily(family);
textHelper.setFontBold(isBold);
textHelper.setVisibled(false);
textHelper.setFirstLine(thin.platform.FIRST_LINE_TEXT_);

var helper = layout.getHelpers();
helper.appendBack(textHelper);

var firstLine = textHelper.getFirstLine();
var spec = {
height: firstLine.getHeight(),
ascent: firstLine.getAscent(),
descent: firstLine.getDescent()
thin.platform.Font.getMetrics = function (family, fontSize, isBold) {
var FontMetrics = thin.app('FontMetrics');

FontMetrics.settings.chars.ascent = 'ぽ';

var spec = /** @type {Object} */ (
FontMetrics({
'fontFamily': family,
'fontWeight': isBold ? 'bold' : 'normal'
})
);

var ascent = Math.abs(spec.ascent * fontSize);
var descent = Math.abs(spec.descent * fontSize);

return {
ascent: ascent,
descent: descent,
height: ascent + descent
};

goog.dom.removeNode(textHelper.getElement());
// cannot be deleted in ES5 strict mode
firstLine = null;

return spec;
};


/**
* @param {string} family
* @param {number} fontSize
* @return {number}
*/
thin.platform.Font.getHeight = function(family, fontSize) {
var spec = thin.platform.Font.getTextLineSpec(family, fontSize, false);
return spec.height;
};


/**
* @param {string} family
* @param {number} fontSize
* @param {boolean} isBold
* @return {number}
*/
thin.platform.Font.getAscent = function(
family, fontSize, isBold) {

var spec = thin.platform.Font.getTextLineSpec(family, fontSize, isBold);
return spec.ascent;
};
68 changes: 68 additions & 0 deletions app/editor/base/platform/fontvalidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) 2011 Matsukei Co.,Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

goog.provide('thin.platform.FontValidator');

goog.require('goog.dom');
goog.require('goog.style');


/**
* @constructor
*/
thin.platform.FontValidator = function () {
/**
* @type {Element}
* @private
*/
this.element_ = goog.dom.getElement('font-validator');
};
goog.addSingletonGetter(thin.platform.FontValidator);


/**
* @param {string} family
* @return {boolean}
*/
thin.platform.FontValidator.validate = function (family) {
return thin.platform.FontValidator.getInstance().validate(family);
};


/**
* @define {string}
*/
thin.platform.FontValidator.BLANK_FONT_FAMILY = 'AdobeBlank';


/**
* @param {string} family
* @return {boolean}
*/
thin.platform.FontValidator.prototype.validate = function (family) {
this.setFontFamily_(family);
return goog.style.getSize(this.element_).width > 1;
};


/**
* @param {string} family
* @return {void}
* @private
*/
thin.platform.FontValidator.prototype.setFontFamily_ = function (family) {
var style = family + ',' + thin.platform.FontValidator.BLANK_FONT_FAMILY;
goog.style.setStyle(this.element_, 'font-family', style);
};
Loading