forked from graphicore/specimenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagram.js
150 lines (140 loc) · 5.14 KB
/
Diagram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
define([
'specimenTools/_BaseWidget'
, 'specimenTools/services/svgDrawing/DiagramRenderer'
], function(
Parent
, DiagramRenderer
) {
"use strict";
/**
* This is a stub!
*
* The aim of the Diagram widget is to enable the rendering of SVG
* based diagrams to illustrate certain aspects of the font.
* Examples are right now a rather generic x-height diagram and a
* rather specific `stylisticSets` diagram, that renders two glyph
* alternates on top of each other.
* Eventually, a small domain specific language or something similar
* should be available to describe such diagrams, so that it is easy
* for a designer or a specialized tool, to create these diagrams
* for specific fonts.
*/
function Diagram(container, pubSub, fontsData, webFontProvider, options) {
Parent.call(this, options);
this._container = container;
this._pubSub = pubSub;
this._fontsData = fontsData;
this._webFontProvider = webFontProvider;
this._svg = null;
this._pubSub.subscribe('activateFont', this._onActivateFont.bind(this));
}
var _p = Diagram.prototype = Object.create(Parent.prototype);
_p.constructor = Diagram;
Diagram.defaultOptions = {
glyphClass: 'diagram__glyph'
, ylineClass: 'diagram__yline'
, boxClass: 'diagram__box'
, layoutClass: 'diagram__layout'
, textClass: 'diagram__text'
};
var instructions = {
xHeight: ['box',
[
['layout', [
['glyph', 'x', {style: 'highlighted'}]
, ['glyph', 'X', {style: 'normal'}]
]
, {spacing: 40}
]
, ['yline', 'baseLine', {style: 'normal', insert: 0}]// 0 = insert as first element, -1 = insert as last element
, ['yline', 'xHeight', {style: 'highlighted', insert: 0}]
]
, {
minLeftSideBearing: 50
, minRightSideBearing: 50
}
]
, stylisticSets: ['layout',
[
['box', [
['glyph', 'G.ss04', {style: 'highlighted'}]
, ['glyph', 'G', {style: 'muted'}]
]
, {align: 'left'}
]
, ['box', [
['glyph', 'g.ss01', {style: 'highlighted'}]
, ['glyph', 'g', {style: 'muted'}]
]
, {align: 'right'}
]
, ['box', [
['glyph', 'R.ss03', {style: 'highlighted'}]
, ['glyph', 'R', {style: 'muted'}]
]
, {align: 'left'}
]
, ['box', [
['glyph', 'l.ss02', {style: 'highlighted'}]
, ['glyph', 'l', {style: 'muted'}]
]
, {align: 'left'}
]
]
]
, basicLines: function() {
/* jshint validthis:true */
var text = this._container.hasAttribute('data-text-content')
? this._container.getAttribute('data-text-content')
: 'Hamburger'
;
return ['box',
[
['text', text, {align: 'center', style: 'normal'}]
, ['yline', 'baseLine', {style: 'normal', insert: 0}]// 0 = insert as first element, -1 = insert as last element
, ['yline', 'xHeight', {style: 'normal', insert: 0}]
, ['yline', 'capHeight', {style: 'normal', insert: 0}]
, ['yline', 'descender', {style: 'normal', insert: 0}]
]
, {
minLeftSideBearing: 350
, minRightSideBearing: 350
}
];
}
};
_p._onActivateFont = function(fontIndex) {
var instructionsKey = this._container.getAttribute('data-diagram-name')
, instructionSet = instructions[instructionsKey]
, result
, optionKeys = {
ascent: 'data-diagram-ascent'
, descent: 'data-diagram-descent'
, height: 'data-diagram-height'
}
, k
, options = Object.create(null)
, renderer
;
if(this._svg)
this._container.removeChild(this._svg);
if(typeof instructionSet === 'function')
instructionSet = instructionSet.call(this);
if(!instructionSet)
return;
renderer = new DiagramRenderer(
this._container.ownerDocument
, this._fontsData
, this._webFontProvider
, this._options
);
for(k in optionKeys)
if(this._container.hasAttribute(optionKeys[k]))
options[k] = parseFloat(this._container.getAttribute(optionKeys[k]));
result = renderer.render(instructionSet, fontIndex, options);
this._svg = result.element;
this._container.appendChild(this._svg);
result.onHasDocument();
};
return Diagram;
});