Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Happypig375 committed Sep 19, 2018
2 parents e17c491 + 487fe5c commit a51d929
Show file tree
Hide file tree
Showing 67 changed files with 276 additions and 237 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using CoreText;
using CSharpMath.Display.Text;
Expand All @@ -12,7 +12,7 @@ public static class AppleAttributedStringFactory
{
public static NSMutableAttributedString ToNsAttributedString(this AttributedGlyphRun<TFont, TGlyph> glyphRun) {
var font = glyphRun.Font;
var text = glyphRun.Text;
var text = glyphRun.Text.ToString();
var unicodeIndexes = StringInfo.ParseCombiningCharacters(text);
var attributes = new CTStringAttributes
{
Expand All @@ -21,7 +21,7 @@ public static NSMutableAttributedString ToNsAttributedString(this AttributedGlyp
};
var attributedString = new NSMutableAttributedString(text, attributes);
var kernedGlyphs = glyphRun.KernedGlyphs;
for (int i = 0; i < kernedGlyphs.Length; i++) {
for (int i = 0; i < kernedGlyphs.Count; i++) {
var kern = kernedGlyphs[i].KernAfterGlyph;
if (kern!=0) {
var endIndex = (i < unicodeIndexes.Length - 1) ? unicodeIndexes[i + 1] : text.Length;
Expand Down
16 changes: 8 additions & 8 deletions CSharpMath.Apple/Font/AppleMathFont.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TGlyph = System.UInt16;
using TGlyph = System.UInt16;
using Foundation;
using CoreGraphics;
using CoreText;
Expand All @@ -8,21 +8,21 @@
namespace CSharpMath.Apple
{
/// <remarks>Corresponds to MTFont in iosMath.</remarks>
public class AppleMathFont: MathFont<TGlyph>
{
public struct AppleMathFont: IMathFont<TGlyph> {
public float PointSize { get; }
public CGFont CgFont { get; private set; }
public CTFont CtFont { get; private set; }
public string Name { get; private set; }
private AppleMathFont(float pointSize): base(pointSize){}
internal AppleMathFont(string name, CGFont cgFont, float size): this(size)
internal AppleMathFont(string name, CGFont cgFont, float size)
{
PointSize = size;
Name = name;
CgFont = cgFont;
var transform = CGAffineTransform.MakeIdentity();
CtFont = new CTFont(CgFont, size, transform);
CtFont = new CTFont(CgFont, size, CGAffineTransform.MakeIdentity());
}

public AppleMathFont(AppleMathFont cloneMe, float pointSize): this(pointSize) {
public AppleMathFont(AppleMathFont cloneMe, float pointSize) {
PointSize = pointSize;
Name = cloneMe.Name;
CgFont= cloneMe.CgFont;
CtFont = new CTFont(CgFont, pointSize, CGAffineTransform.MakeIdentity());
Expand Down
6 changes: 1 addition & 5 deletions CSharpMath.Apple/Font/CtFontGlyphFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public byte[] ToByteArray(TGlyph[] glyphs) {
return r;
}

private IEnumerable<ushort> FindGlyphsInternal(TFont font, string str) {
public IEnumerable<ushort> FindGlyphs(TFont font, string str) {
// not completely sure this is correct. Need an actual
// example of a composed character sequence coming from LaTeX.
var unicodeIndexes = StringInfo.ParseCombiningCharacters(str);
Expand Down Expand Up @@ -71,9 +71,5 @@ public TGlyph FindGlyphForCharacterAtIndex(TFont font, int index, string str) {
font.CtFont.GetGlyphsForCharacters(chars, glyphs, length);
return glyphs[0];
}

public TGlyph[] FindGlyphs(TFont font, string str)
=> FindGlyphsInternal(font, str).ToArray();

}
}
2 changes: 1 addition & 1 deletion CSharpMath.Apple/Interfaces/IFontMeasurer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace CSharpMath.FrontEnd {
public interface IFontMeasurer<TFont, TGlyph>
where TFont: MathFont<TGlyph> {
where TFont: IMathFont<TGlyph> {
/// <summary>A proportionality constant that is applied when
/// reading from the Json table.</summary>
int GetUnitsPerEm(TFont font);
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Apple/Typesetting/JsonMathTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace CSharpMath.Apple {
/// <summary>Holds lots of constants for spacing between various visible elements by reading a JSON file.</summary>
public class JsonMathTable<TFont, TGlyph> : FontMathTable<TFont, TGlyph>
where TFont : MathFont<TGlyph> {
where TFont : IMathFont<TGlyph> {
/// <summary>Dictionary object containing a zillion constants,
/// typically loaded from a .json file.</summary>
private readonly JToken _mathTable;
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Rendering/Drawing/GraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void DrawGlyphRunWithOffset(Display.Text.AttributedGlyphRun<TFonts, Glyph
Canvas.Save();
Canvas.Translate(textPosition.X, textPosition.Y);
Canvas.CurrentColor = color;
for (int i = 0; i < glyphs.Length; i++) {
for (int i = 0; i < glyphs.Count; i++) {
var typeface = glyphs[i].Glyph.Typeface;
layout.Typeface = typeface;
var pathBuilder = new GlyphPathBuilder(typeface);
Expand Down
14 changes: 9 additions & 5 deletions CSharpMath.Rendering/Font/Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Typography.OpenFont;

namespace CSharpMath.Rendering {
public class Fonts : MathFont<Glyph>, IEnumerable<Typeface> {
public struct Fonts : IMathFont<Glyph>, IEnumerable<Typeface> {
static Fonts() {
var reader = new OpenFontReader();
var latinMathTypeface = reader.Read(new MemoryStream(Resources.LatinModernMath, false));
Expand All @@ -19,20 +19,24 @@ static Fonts() {
GlobalTypefaces.AddStart(amsBlackboardBoldTypeface);
}

public static Typefaces GlobalTypefaces { get; }

public Fonts(IList<Typeface> localTypefaces, float pointSize) : base(pointSize) {
public Fonts(IList<Typeface> localTypefaces, float pointSize) {
PointSize = pointSize;
var typefaces = localTypefaces.Concat(GlobalTypefaces);
Typefaces = typefaces;
MathTypeface = typefaces.First(t => t.HasMathTable());
}
public Fonts(Fonts cloneMe, float pointSize) : base(pointSize) {
public Fonts(Fonts cloneMe, float pointSize) {
PointSize = pointSize;
Typefaces = cloneMe.Typefaces;
MathTypeface = cloneMe.MathTypeface;
}

public static Typefaces GlobalTypefaces { get; }

public float PointSize { get; }
public IEnumerable<Typeface> Typefaces { get; }
public Typeface MathTypeface { get; }

public Typography.OpenFont.MathGlyphs.MathConstants MathConsts => MathTypeface.MathConsts;

public IEnumerator<Typeface> GetEnumerator() => Typefaces.GetEnumerator();
Expand Down
12 changes: 6 additions & 6 deletions CSharpMath.Rendering/Font/GlyphFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public Glyph Lookup(Fonts fonts, int codepoint) {
return new Glyph(fonts.MathTypeface, fonts.MathTypeface.Lookup(GlyphNotFound));
}

public int GetCodepoint(string str, int index) => char.ConvertToUtf32(str, index - (char.IsLowSurrogate(str[index]) ? 1 : 0));
public int GetCodepoint(string str, int index) =>
char.ConvertToUtf32(str, index - (char.IsLowSurrogate(str[index]) ? 1 : 0));

public Glyph FindGlyphForCharacterAtIndex(Fonts fonts, int index, string str) {
return Lookup(fonts, GetCodepoint(str, index));
}
public Glyph FindGlyphForCharacterAtIndex(Fonts fonts, int index, string str) =>
Lookup(fonts, GetCodepoint(str, index));

public Glyph[] FindGlyphs(Fonts fonts, string str) =>
Typography.OpenFont.StringUtils.GetCodepoints(str.ToCharArray()).Select(c => Lookup(fonts, c)).ToArray();
public System.Collections.Generic.IEnumerable<Glyph> FindGlyphs(Fonts fonts, string str) =>
Typography.OpenFont.StringUtils.GetCodepoints(str.ToCharArray()).Select(c => Lookup(fonts, c));

public bool GlyphIsEmpty(Glyph glyph) => glyph.IsEmpty;

Expand Down
4 changes: 2 additions & 2 deletions CSharpMath.Rendering/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public static bool DisableEnhancedTextPainterColors {
set => Rendering.TextBuilder.NoEnhancedColors = value;
}
public static bool DisableWarnings {
get => WarningException.DisableWarnings;
set => WarningException.DisableWarnings = value;
get => Warnings.DisableWarnings;
set => Warnings.DisableWarnings = value;
}
public static Rendering.Typefaces GlobalTypefaces => Rendering.Fonts.GlobalTypefaces;

Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Rendering/Text/TextAtoms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static class TextAtoms {
{ "textordfeminine" , "ª" },
{ "textasteriskcentered" , "" },
{ "textordmasculine" , "º" },
{ "textbackslash" , "" },
{ "textbackslash" , "\\" }, //originally was ∖
{ "P", "textparagraph" , "" },
{ "textbar" , "|" },
{ "textperiodcentered" , "·" },
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Rendering/Text/TextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Result<TextAtom> Build(string latex) {
var breaker = new CustomBreaker { BreakNumberAfterText = true, ThrowIfCharOutOfRange = false };
var breakList = new List<BreakAtInfo>();
breaker.BreakWords(latex);
breaker.LoadBreakAtList(breakList);
breaker.CopyBreakResults(breakList);
Result CheckDollarCount(TextAtomListBuilder atoms) {
switch (dollarCount) {
case 0:
Expand Down
8 changes: 4 additions & 4 deletions CSharpMath.Rendering/Text/TextLayoutter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ void FinalizeInlineDisplay(float ascender, float rawDescender, float lineGap, bo
var glyphs = GlyphFinder.Instance.FindGlyphs(fonts, content);
//Calling Select(g => g.Typeface).Distinct() speeds up query up to 10 times,
//Calling Max(Func<,>) instead of Select(Func<,>).Max() speeds up query 2 times
var typefaces = glyphs.Select(g => g.Typeface).Distinct();
WarningException.WarnIfAny(typefaces,
tf => !Typography.OpenFont.Extensions.TypefaceExtensions.RecommendToUseTypoMetricsForLineSpacing(tf),
var typefaces = glyphs.Select(g => g.Typeface).Distinct().ToList();
Warnings.AssertAll(typefaces,
tf => Typography.OpenFont.Extensions.TypefaceExtensions.RecommendToUseTypoMetricsForLineSpacing(tf),
"This font file is too old. Only font files that support standard typographical metrics are supported.");
display = new TextRunDisplay<Fonts, Glyph>(Display.Text.AttributedGlyphRuns.Create(content, glyphs, fonts, false), t.Range, TypesettingContext.Instance);
FinalizeInlineDisplay(
Expand Down Expand Up @@ -116,7 +116,7 @@ void FinalizeInlineDisplay(float ascender, float rawDescender, float lineGap, bo
AddDisplaysWithLineBreaks(a.Content, fonts, accentDisplayLine, accenteeDisplayList, invalidDisplayMaths, style, color);
float _ = default;
accentDisplayLine.Clear(0, 0, accenteeDisplayList, ref _, false, false, additionalLineSpacing);
WarningException.WarnIf(invalidDisplayMaths.Count > 0, "Display maths inside an accentee is unsupported -- ignoring display maths");
Warnings.Assert(invalidDisplayMaths.Count == 0, "Display maths inside an accentee is unsupported -- ignoring display maths");
var accentee = new Displays(accenteeDisplayList);
var accenteeCodepoint = a.Content.SingleChar(style);
Glyph accenteeSingleGlyph = accenteeCodepoint.HasValue ? GlyphFinder.Instance.Lookup(fonts, accenteeCodepoint.Value) : GlyphFinder.Instance.EmptyGlyph;
Expand Down
1 change: 1 addition & 0 deletions CSharpMath.Rendering/Text/TextSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace CSharpMath.Rendering {

public readonly struct TextSource : ISource {
public TextSource(string latex) {
LaTeX = latex;
Expand Down
1 change: 1 addition & 0 deletions CSharpMath.SkiaSharp/SkiaPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void EndRead() {
_owner.Canvas.DrawPath(_path, _owner.Paint);
_path.Dispose();
_path = null;
_owner = null;
}
public void CloseContour() => _path.Close();
public void Curve3(float x1, float y1, float x2, float y2) => _path.QuadTo(x1, y1, x2, y2);
Expand Down
4 changes: 2 additions & 2 deletions CSharpMath.Tests/Extensions/IDisplayTestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpMath.Display;
using CSharpMath.Display;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -8,7 +8,7 @@

namespace CSharpMath.Tests {
public static class IDisplayTestExtensions {
public static string StringText(this TextLineDisplay<MathFont<char>, char> display)
public static string StringText(this TextLineDisplay<FrontEnd.TestMathFont, char> display)
=> new string(display.Text.ToArray());
}
}
13 changes: 13 additions & 0 deletions CSharpMath.Tests/FrontEnd/TestFont.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSharpMath.Enumerations;

namespace CSharpMath.Tests.FrontEnd {
public struct TestMathFont : Display.IMathFont<char> {
public TestMathFont(float pointSize) => PointSize = pointSize;
public float PointSize { get; }
}
}
6 changes: 3 additions & 3 deletions CSharpMath.Tests/FrontEnd/TestFontMeasurer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpMath.FrontEnd;
using CSharpMath.FrontEnd;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,7 +7,7 @@
using CSharpMath.Display;

namespace CSharpMath.Tests.FrontEnd {
public class TestFontMeasurer : IFontMeasurer<MathFont<char>, char> {
public int GetUnitsPerEm(MathFont<char> font) => 1000;
public class TestFontMeasurer : IFontMeasurer<TestMathFont, char> {
public int GetUnitsPerEm(TestMathFont font) => 1000;
}
}
11 changes: 5 additions & 6 deletions CSharpMath.Tests/FrontEnd/TestGlyphBoundsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpMath.FrontEnd;
using CSharpMath.FrontEnd;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,11 +7,10 @@
using CSharpMath.Display;
using System.Drawing;
using TGlyph = System.Char;
using TFont = CSharpMath.Display.MathFont<System.Char>;
using CSharpMath.Display.Text;

namespace CSharpMath.Tests.FrontEnd {
public class TestGlyphBoundsProvider : IGlyphBoundsProvider<MathFont<TGlyph>, TGlyph> {
public class TestGlyphBoundsProvider : IGlyphBoundsProvider<TestMathFont, TGlyph> {
private const float WidthPerCharacterPerFontSize = 0.5f; // "m" and "M" get double width.
private const float AscentPerFontSize = 0.7f;
private const float DescentPerFontSize = 0.2f; // all constants were chosen to bear some resemblance to a real font.
Expand All @@ -27,14 +26,14 @@ private int GetEffectiveLength(TGlyph[] glyphs) {
return effectiveLength;
}

public float GetTypographicWidth(MathFont<char> font, AttributedGlyphRun<TFont, TGlyph> run) {
public float GetTypographicWidth(TestMathFont font, AttributedGlyphRun<TestMathFont, TGlyph> run) {
int effectiveLength = GetEffectiveLength(run.Glyphs.ToArray());
float width = font.PointSize * effectiveLength * WidthPerCharacterPerFontSize +
run.KernedGlyphs.Sum(g => g.KernAfterGlyph);
return width;
}

public RectangleF[] GetBoundingRectsForGlyphs(TFont font, TGlyph[] glyphs) {
public RectangleF[] GetBoundingRectsForGlyphs(TestMathFont font, TGlyph[] glyphs) {
RectangleF[] r = new RectangleF[glyphs.Length];
for (int i = 0; i < glyphs.Length; i++) {
var glyph = glyphs[i];
Expand All @@ -49,7 +48,7 @@ public RectangleF[] GetBoundingRectsForGlyphs(TFont font, TGlyph[] glyphs) {
return r;
}

public (float[] Advances, float Total) GetAdvancesForGlyphs(MathFont<TGlyph> font, TGlyph[] glyphs) {
public (float[] Advances, float Total) GetAdvancesForGlyphs(TestMathFont font, TGlyph[] glyphs) {
var r = new float[glyphs.Length];
var total = 0f;
for (int i = 0; i < glyphs.Length; i++) {
Expand Down
8 changes: 4 additions & 4 deletions CSharpMath.Tests/FrontEnd/TestGlyphFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

namespace CSharpMath.Tests.FrontEnd
{
class TestGlyphFinder : IGlyphFinder<MathFont<char>, char> {
public char FindGlyphForCharacterAtIndex(MathFont<char> font, int index, string str) {
class TestGlyphFinder : IGlyphFinder<TestMathFont, char> {
public char FindGlyphForCharacterAtIndex(TestMathFont font, int index, string str) {
return str[index];
}

public char[] FindGlyphs(MathFont<char> font, string str) => str.ToArray();
public IEnumerable<char> FindGlyphs(TestMathFont font, string str) => str;

public string FindStringDebugPurposesOnly(char[] glyphs) => new string(glyphs);
public bool GlyphIsEmpty(char glyph)
=> glyph == '\0';
=> glyph is '\0';
public char EmptyGlyph => '\0';
}
}
8 changes: 4 additions & 4 deletions CSharpMath.Tests/FrontEnd/TestTypesettingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace CSharpMath.Tests.FrontEnd {
public static class TestTypesettingContexts {
public static TypesettingContext<MathFont<char>, char> Create() {
public static TypesettingContext<TestMathFont, char> Create() {
var boundsProvider = new TestGlyphBoundsProvider();
return new TypesettingContext<MathFont<char>, char>(
(font, size) => new MathFont<char>(size, font.Style),
return new TypesettingContext<TestMathFont, char>(
(font, size) => new TestMathFont(size),
boundsProvider,
new TestGlyphFinder(),
new DoNothingFontChanger(),
new Apple.JsonMathTable<MathFont<char>, char>(new TestFontMeasurer(), TestResources.LatinMath, new TestGlyphNameProvider(), boundsProvider)
new Apple.JsonMathTable<TestMathFont, char>(new TestFontMeasurer(), TestResources.LatinMath, new TestGlyphNameProvider(), boundsProvider)
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions CSharpMath.Tests/MockTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpMath.Display;
using CSharpMath.Display;
using CSharpMath.Tests.FrontEnd;
using System;
using System.Drawing;
Expand All @@ -13,12 +13,12 @@ public class MockTests {
[Fact]
public void TestGlyphBoundsWithoutM() {
string hello = "Hello";
MathFont<TGlyph> font = new MathFont<TGlyph>(10);
TestMathFont font = new TestMathFont(10);
var provider = new TestGlyphBoundsProvider();
var glyphRun = new AttributedGlyphRun<MathFont<TGlyph>, TGlyph>
var glyphRun = new AttributedGlyphRun<TestMathFont, TGlyph>
{
Font = font,
KernedGlyphs = hello.ToCharArray().Select(c => new KernedGlyph<char>(c)).ToArray(),
KernedGlyphs = hello.Select(c => new KernedGlyph<char>(c)).ToList(),
};
var width = provider.GetTypographicWidth(font, glyphRun);
Assertions.ApproximatelyEqual(width, 25, 0.01);
Expand All @@ -27,12 +27,12 @@ public void TestGlyphBoundsWithoutM() {
[Fact]
public void TestGlyphBoundsWithM() {
string america = "America";
MathFont<TGlyph> font = new MathFont<TGlyph>(10);
TestMathFont font = new TestMathFont(10);
var provider = new TestGlyphBoundsProvider();
var glyphRun = new AttributedGlyphRun<MathFont<TGlyph>, TGlyph>
var glyphRun = new AttributedGlyphRun<TestMathFont, TGlyph>
{
Font = font,
KernedGlyphs = america.ToCharArray().Select(c => new KernedGlyph<char>(c)).ToArray(),
KernedGlyphs = america.Select(c => new KernedGlyph<char>(c)).ToList(),
};
var width = provider.GetTypographicWidth(font, glyphRun);
Assertions.ApproximatelyEqual(width, 40, 0.01);
Expand Down
Loading

0 comments on commit a51d929

Please sign in to comment.