Skip to content

Commit dc28e35

Browse files
authored
Adds support for any font as rendering resource (#274)
* Added initial forwarding of font sizes to worker * Added font loading check mechanism to workflow to ensure it is loaded before rendering * Integrated dynamic font size generation into already existing workflows * Updated samples and docs * Some cleanup work
1 parent e0dd648 commit dc28e35

File tree

33 files changed

+351
-181
lines changed

33 files changed

+351
-181
lines changed

Documentation/input/_Head.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/themes/prism.min.css" />
22
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/plugins/line-numbers/prism-line-numbers.min.css" />
33
<script src="@Context.GetLink("/assets/js/alphaTab/alphaTab.min.js")"></script>
4+
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap" rel="stylesheet">

Documentation/input/examples/render-customization/colors-fonts.cshtml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Order: 10
1616
</tr>
1717
</thead>
1818
<tbody>
19-
<tr><td>copyrightFont</td> <td>Font</td> <td>Music copyright information in page layouts</td> <td>12px Arial bold</td></tr>
19+
<tr><td>copyrightFont</td> <td>Font</td> <td>Music copyright information in page layouts</td> <td>bold 12px Arial</td></tr>
2020
<tr><td>titleFont</td> <td>Font</td> <td>Title of the song in page layouts</td> <td>32px Georgia</td></tr>
2121
<tr><td>subTitleFont</td> <td>Font</td> <td>Subtitle of the song in page layouts</td> <td>20px Georgia</td></tr>
2222
<tr><td>wordsFont</td> <td>Font</td> <td>Lyrics information of the song in page layouts</td> <td>15px Georgia</td></tr>
@@ -26,7 +26,7 @@ Order: 10
2626
<tr><td>graceFont</td> <td>Font</td> <td>The numbers grace notes in tablature staves</td> <td>11px Arial</td></tr>
2727
<tr><td>barNumberFont</td> <td>Font</td> <td>The numbers above bars</td> <td>11px Arial</td></tr>
2828
<tr><td>fingeringFont</td> <td>Font</td> <td>The numbers shown for fingering information</td><td>14px Georgia</td></tr>
29-
<tr><td>markerFont</td> <td>Font</td> <td>The section marker labels</td> <td>14px Arial bold</td> </tr>
29+
<tr><td>markerFont</td> <td>Font</td> <td>The section marker labels</td> <td>bold 14px Arial</td> </tr>
3030

3131
<tr><td>staffLineColor</td> <td>Color</td> <td>The lines for all staves</td> <td>rgb(165,165,165)</td></tr>
3232
<tr><td>barSeparatorColor</td> <td>Color</td> <td>The lines for bar separators and repeat indicators</td> <td>rgb(34,34,17)</td></tr>
@@ -44,7 +44,7 @@ Order: 10
4444
<h3 class="panel-title">Usage of different fonts</h3>
4545
</div>
4646
<div class="panel-body">
47-
The usage of other fonts can result in alignment or layouting issues and in a slighly worse performance. If such problems are detected, please inform us on <a href="https://github.com/CoderLine/alphaTab/issues/new/choose" target="_blank">GitHub</a>. The format for fonts is similar to the CSS shorthand font declarations but with some limitations: <code>Size Family Style</code>. No relative font sizes are supported and every size beside px will be converted to px. For styles <code>italic</code> and <code>bold</code> are supported.
47+
The usage of other fonts can result in alignment or layouting issues and in a slighly worse performance. If such problems are detected, please inform us on <a href="https://github.com/CoderLine/alphaTab/issues/new/choose" target="_blank">GitHub</a>. The format for fonts is similar to the CSS shorthand font declarations but with some limitations: <code>Style Size Family</code>. No relative font sizes are supported and every size beside px will be converted to px. For styles <code>italic</code> and <code>bold</code> are supported.
4848
</div>
4949
</div>
5050

@@ -76,7 +76,7 @@ Order: 10
7676
$('#alphaTabScriptInit').alphaTab({
7777
file: '@Context.GetLink("/assets/files/features/Skillet.gp5")',
7878
resources: {
79-
copyrightFont: "12px Roboto bold",
79+
copyrightFont: "bold 12px Roboto",
8080
titleFont: "32px 'Roboto Slab'",
8181
subTitleFont: "20px 'Roboto Slab'",
8282
wordsFont: "15px 'Roboto Slab'",
@@ -86,7 +86,7 @@ Order: 10
8686
graceFont: "11px Roboto",
8787
barNumberFont: "11px Roboto",
8888
fingeringFont: "14px 'Roboto Slab'",
89-
markerFont: "14px Roboto bold",
89+
markerFont: "bold 14px Roboto",
9090
9191
staffLineColor: "rgba(255,255,255, 0.8)",
9292
barSeparatorColor: "rgb(255,255,255)",
@@ -105,17 +105,17 @@ Order: 10
105105
.alphaTabSurface { background: #000; }
106106
</style>
107107
<div id="alphaTabDataInit" data-file="@Context.GetLink("/assets/files/features/Skillet.gp5")"
108-
data-resources-copyrightFont="12px Roboto bold"
108+
data-resources-copyrightFont="bold 12px Roboto"
109109
data-resources-titleFont="32px 'Roboto Slab'"
110110
data-resources-subTitleFont="20px 'Roboto Slab'"
111111
data-resources-wordsFont="15px 'Roboto Slab'"
112-
data-resources-effectFont="12px 'Roboto Slab' italic"
112+
data-resources-effectFont=" italic 12px 'Roboto Slab'"
113113
data-resources-fretboardNumberFont="11px Roboto"
114114
data-resources-tablatureFont="13px Roboto"
115115
data-resources-graceFont="11px Roboto"
116116
data-resources-barNumberFont="11px Roboto"
117117
data-resources-fingeringFont="14px 'Roboto Slab'"
118-
data-resources-markerFont="14px Roboto bold"
118+
data-resources-markerFont=" bold 14px Roboto"
119119

120120
data-resources-staffLineColor="rgba(255,255,255, 0.8)"
121121
data-resources-barSeparatorColor="rgb(255,255,255)"

Documentation/input/reference/property/renderingresources.cshtml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ Following resources exist for adjusting the style.
122122

123123
<h3>Fonts</h3>
124124
<p>
125-
For the .net platform any installed font on the system can be used. For JavaScript until <a href="https://github.com/CoderLine/alphaTab/issues/266" target="_blank">this feature</a> is implemented the 2 fonts "Arial" and "Georgia" are officially supported. Due to the nature of Web Workers, no font metrics are available which needs a custom solution for measuring texts.
126-
<br />
127-
On .net simply construct the <code>Font</code> object to configure your desired fonts. For JavaScript you can use any CSS font property compliant string. Relative font sizes with percentual values are not supported.
125+
For the .net platform any installed font on the system can be used. Simply construct the <code>Font</code> object to configure your desired fonts.
126+
</p>
127+
<p>
128+
For the JavaScript platform any font that might be installed on the client machines can be used. Any additional fonts can be added via WebFonts. The rendering of the score will be delayed until it is detected that the font was loaded. Simply use any CSS font property compliant string as configuration. Relative font sizes with percentual values are not supported, remaining values will be considered if supported.
128129
</p>
129130

130131

Phase/Mscorlib/system/CsString.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,5 @@ abstract CsString(String) from String to String
358358
@:op(A + B) public static inline function add16(lhs : system.CsString, rhs : system.Single) : system.CsString return lhs + rhs.toString();
359359
@:op(A + B) public static inline function add17(lhs : system.CsString, rhs : system.Double) : system.CsString return lhs + rhs.toString();
360360
@:op(A + B) public static inline function add18(lhs : system.CsString, rhs : system.CsString) : system.CsString return lhs.toHaxeString() + rhs.toHaxeString();
361+
@:op(A + B) public static inline function add19(lhs : system.CsString, rhs : system.Boolean) : system.CsString return lhs.toHaxeString() + Std.string(rhs);
361362
}

Samples/CSharp/AlphaTab.Samples.PngDump/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void Main(string[] args)
5252
var info = new FileInfo(args[0]);
5353
var path = Path.Combine(info.DirectoryName, Path.GetFileNameWithoutExtension(info.Name) + "-" + i + ".png");
5454

55-
using (var full = SKSurface.Create(totalWidth, totalHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul))
55+
using (var full = SKSurface.Create(new SKImageInfo(totalWidth, totalHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
5656
{
5757
var y = 0;
5858
foreach (var image in images)

Source/AlphaTab.CSharp/Platform/CSharp/ManagedUiFacade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace AlphaTab.Platform.CSharp
1212
{
13-
public abstract class ManagedUiFacade<TSettings> : IUiFacade<TSettings>
13+
internal abstract class ManagedUiFacade<TSettings> : IUiFacade<TSettings>
1414
{
1515
protected ConcurrentQueue<Counter> TotalResultCount { get; private set; }
1616

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace AlphaTab.Platform.Svg
2+
{
3+
/// <summary>
4+
/// This public class stores text widths for several fonts and allows width calculation
5+
/// </summary>
6+
internal partial class FontSizes
7+
{
8+
public static void GenerateFontLookup(string family)
9+
{
10+
if (FontSizeLookupTables == null)
11+
{
12+
Init();
13+
}
14+
15+
if (FontSizeLookupTables.ContainsKey(family))
16+
{
17+
return;
18+
}
19+
20+
// TODO: maybe allow fallback to GDI/Skia based on availability?
21+
FontSizeLookupTables[family] = new byte[]
22+
{
23+
8
24+
};
25+
}
26+
}
27+
}

Source/AlphaTab.JavaScript/Collections/FastDictionary.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
22
using System.Collections.Generic;
3-
using System.Runtime.CompilerServices;
43
using Phase;
54
using Phase.Attributes;
65

Source/AlphaTab.JavaScript/Environment.cs

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AlphaTab.Platform.Svg;
77
using AlphaTab.Rendering.Glyphs;
88
using AlphaTab.Util;
9+
using AlphaTab.Utils;
910
using Haxe;
1011
using Phase;
1112
using StringBuilder = AlphaTab.Collections.StringBuilder;
@@ -20,17 +21,8 @@ namespace AlphaTab
2021
internal partial class Environment
2122
{
2223
public static string ScriptFile { get; set; }
23-
public static bool IsFontLoaded { get; set; }
24-
public static event Action FontLoaded;
2524

26-
private static void OnFontLoaded()
27-
{
28-
var handler = FontLoaded;
29-
if (handler != null)
30-
{
31-
handler();
32-
}
33-
}
25+
public static FontLoadingChecker BravuraFontChecker { get; private set; }
3426

3527
private static void PlatformInit()
3628
{
@@ -133,7 +125,10 @@ private static void PlatformInit()
133125
}
134126
}
135127

136-
CheckForFontAvailability();
128+
BravuraFontChecker = new FontLoadingChecker("alphaTab",
129+
"&#" + (int)MusicFontSymbol.ClefG + ";"
130+
);
131+
BravuraFontChecker.CheckForFontAvailability();
137132
}
138133
else
139134
{
@@ -207,95 +202,5 @@ private static string ScriptFileFromStack(string stack)
207202

208203
return matches[1];
209204
}
210-
211-
public static void CheckForFontAvailability()
212-
{
213-
var isWorker =
214-
Script.Write<bool>(
215-
"untyped __js__(\"typeof(WorkerGlobalScope) !== 'undefined' && self instanceof WorkerGlobalScope\")");
216-
if (isWorker)
217-
{
218-
// no web fonts in web worker
219-
IsFontLoaded = false;
220-
return;
221-
}
222-
223-
var cssFontLoadingModuleSupported = Browser.Document.Fonts.IsTruthy() &&
224-
Browser.Document.Fonts.Member<object>("load").IsTruthy();
225-
if (cssFontLoadingModuleSupported)
226-
{
227-
Action checkFont = null;
228-
checkFont = () =>
229-
{
230-
Browser.Document.Fonts.Load("1em alphaTab").Then(_ =>
231-
{
232-
if (Browser.Document.Fonts.Check("1em alphaTab"))
233-
{
234-
Logger.Info("Rendering", "Font available");
235-
IsFontLoaded = true;
236-
OnFontLoaded();
237-
}
238-
else
239-
{
240-
Browser.Window.SetTimeout((Action)(() =>
241-
{
242-
checkFont();
243-
}),
244-
250);
245-
}
246-
247-
return true;
248-
});
249-
};
250-
checkFont();
251-
}
252-
else
253-
{
254-
Action checkFont = null;
255-
checkFont = () =>
256-
{
257-
var document = Browser.Document;
258-
var testItem = document.GetElementById("alphaTabFontChecker");
259-
260-
if (testItem == null)
261-
{
262-
// create a hidden element with the font style set
263-
testItem = document.CreateElement("div");
264-
testItem.SetAttribute("id", "alphaTabFontChecker");
265-
testItem.Style.Opacity = "0";
266-
testItem.Style.Position = "absolute";
267-
testItem.Style.Left = "0";
268-
testItem.Style.Top = "0";
269-
testItem.Style.FontSize = "100px";
270-
testItem.ClassList.Add("at");
271-
testItem.InnerHTML = "&#" + (int)MusicFontSymbol.ClefG + ";";
272-
273-
document.Body.AppendChild(testItem);
274-
}
275-
276-
// get width
277-
var width = testItem.OffsetWidth;
278-
if (width > 30 && width < 100)
279-
{
280-
IsFontLoaded = true;
281-
document.Body.RemoveChild(testItem);
282-
OnFontLoaded();
283-
}
284-
else
285-
{
286-
Browser.Window.SetTimeout((Action)(() =>
287-
{
288-
checkFont();
289-
}),
290-
250);
291-
}
292-
};
293-
Browser.Window.AddEventListener("DOMContentLoaded",
294-
(Action)(() =>
295-
{
296-
checkFont();
297-
}));
298-
}
299-
}
300205
}
301206
}

Source/AlphaTab.JavaScript/Haxe/IO/HaxeBytesBuffer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Haxe;
2-
using Phase.Attributes;
1+
using Phase.Attributes;
32

43
namespace Haxe.IO
54
{

0 commit comments

Comments
 (0)