Skip to content

Commit ca4674b

Browse files
committed
0.1.0-pre9
1 parent e02f5b3 commit ca4674b

File tree

17 files changed

+104
-81
lines changed

17 files changed

+104
-81
lines changed

CSharpMath.Forms.Example/CSharpMath.Forms.Example/LayoutPage.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
4141
e.Surface.Canvas.DrawRect((float)x + measure.X, (float)y + measure.Y, measure.Width, measure.Height, new global::SkiaSharp.SKPaint { Color = global::SkiaSharp.SKColors.Red, IsStroke = true });
4242
measure = painter._relativeXCoordDisplay.ComputeDisplayBounds();
4343
e.Surface.Canvas.DrawRect((float)x + measure.X, (float)y + measure.Y, measure.Width, measure.Height, new global::SkiaSharp.SKPaint { Color = global::SkiaSharp.SKColors.Blue, IsStroke = true });
44-
*/painter.Draw(e.Surface.Canvas, new System.Drawing.PointF((float)x, (float)y), (float)w);
44+
*///painter.Draw(e.Surface.Canvas, new System.Drawing.PointF((float)x, (float)y), (float)w);
45+
painter.DrawOneLine(e.Surface.Canvas, (float)x, (float)y);
4546
}
4647

4748
//Add Epsilon to prevent SliderW.Minimum == SiderW.Maximum == 0

CSharpMath.Forms.Example/CSharpMath.Forms.Example/TextPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void Size_SelectedIndexChanged(object sender, EventArgs e) {
4141
}
4242

4343
private void Text_TextChanged(object sender, TextChangedEventArgs e) {
44-
View.Text = e.NewTextValue;
44+
View.LaTeX = e.NewTextValue;
4545
View.InvalidateSurface();
4646
}
4747
}

CSharpMath.Forms/CSharpMath.Forms.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
55
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
6-
<PackageVersion>0.1.0-pre8.2</PackageVersion>
6+
<PackageVersion>0.1.0-pre9</PackageVersion>
77
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
88
<PackageReleaseNotes>The 0.1.0 SkiaSharp Update brings the SkiaSharp and Xamarin.Forms front ends to CSharpMath.</PackageReleaseNotes>
99
<RepositoryType>git</RepositoryType>
1010
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
1111
<RepositoryBranch>master</RepositoryBranch>
12-
<RepositoryCommit>427b0615080dd2a85da3ae676088f7b48a38a8c7</RepositoryCommit>
12+
<RepositoryCommit>e02f5b39a1346affa843b3001f95d1185b695029</RepositoryCommit>
1313
<PackageProjectUrl>https://github.com/verybadcat/CSharpMath</PackageProjectUrl>
1414
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1515
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>

CSharpMath.Forms/TextView.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
namespace CSharpMath.Forms
1+
namespace CSharpMath.Forms
22
{
33
using Rendering;
44
using SkiaSharp;
55
using Xamarin.Forms;
66
using Xamarin.Forms.Xaml;
77

8-
[ContentProperty(nameof(Text)), XamlCompilation(XamlCompilationOptions.Compile)]
8+
[ContentProperty(nameof(LaTeX)), XamlCompilation(XamlCompilationOptions.Compile)]
99
public class TextView : BaseView<TextPainter, TextSource>
1010
{
1111
public TextAtom Atom { get => Source.Atom; set => Source = new TextSource(value); }
12-
public string Text { get => Source.Text; set => Source = new TextSource(value); }
12+
public string LaTeX { get => Source.LaTeX; set => Source = new TextSource(value); }
1313

14-
public float? LineWidth { get => (float?)GetValue(LineWidthProperty); set => SetValue(LineWidthProperty, value); }
15-
public static readonly BindableProperty LineWidthProperty = BindableProperty.Create(nameof(LineWidth), typeof(float?), typeof(TextView));
14+
//public float? LineWidth { get => (float?)GetValue(LineWidthProperty); set => SetValue(LineWidthProperty, value); }
15+
//public static readonly BindableProperty LineWidthProperty = BindableProperty.Create(nameof(LineWidth), typeof(float?), typeof(TextView));
1616
}
1717
}

CSharpMath.Ios/CSharpMath.Ios.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<NuGetPackageImportStamp></NuGetPackageImportStamp>
1515
<PackOnBuild>true</PackOnBuild>
1616
<PackageId>CSharpMath.Ios</PackageId>
17-
<PackageVersion>0.1.0-pre8.2</PackageVersion>
17+
<PackageVersion>0.1.0-pre9</PackageVersion>
1818
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
1919
<NeutralLanguage>en</NeutralLanguage>
2020
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>
@@ -28,7 +28,7 @@
2828
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
2929
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
3030
<RepositoryBranch>master</RepositoryBranch>
31-
<RepositoryCommit>427b0615080dd2a85da3ae676088f7b48a38a8c7</RepositoryCommit>
31+
<RepositoryCommit>e02f5b39a1346affa843b3001f95d1185b695029</RepositoryCommit>
3232
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
3333
<PackageIconUrl>https://raw.githubusercontent.com/verybadcat/CSharpMath/master/Icon.png</PackageIconUrl>
3434
</PropertyGroup>

CSharpMath.Rendering/CSharpMath.Rendering.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<!--NuGet Properties-->
1212
<PackOnBuild>true</PackOnBuild>
1313
<PackageId>CSharpMath.Rendering</PackageId>
14-
<PackageVersion>0.1.0-pre8.2</PackageVersion>
14+
<PackageVersion>0.1.0-pre9</PackageVersion>
1515
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
1616
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>
1717
<PackageProjectUrl>https://github.com/verybadcat/CSharpMath</PackageProjectUrl>
@@ -24,7 +24,7 @@
2424
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
2525
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
2626
<RepositoryBranch>master</RepositoryBranch>
27-
<RepositoryCommit>427b0615080dd2a85da3ae676088f7b48a38a8c7</RepositoryCommit>
27+
<RepositoryCommit>e02f5b39a1346affa843b3001f95d1185b695029</RepositoryCommit>
2828
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2929
<PackageIconUrl>https://raw.githubusercontent.com/verybadcat/CSharpMath/master/Icon.png</PackageIconUrl>
3030
</PropertyGroup>

CSharpMath.Rendering/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace CSharpMath {
22
using System.Collections.Generic;
33
public static class Settings {
44
public static bool DisableEnhancedTextPainterColors {
5-
get => Rendering.TextSource.NoEnhancedColors;
6-
set => Rendering.TextSource.NoEnhancedColors = value;
5+
get => Rendering.TextBuilder.NoEnhancedColors;
6+
set => Rendering.TextBuilder.NoEnhancedColors = value;
77
}
88
public static bool DisableWarnings {
99
get => WarningException.DisableWarnings;

CSharpMath.Rendering/Text/TextBuilder.cs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@ namespace CSharpMath.Rendering {
88
using Structures;
99
using static Structures.Result;
1010
public static class TextBuilder {
11-
/* //Paste this into the C# Interactive, fill <username> yourself
11+
/* //Paste this into the C# Interactive, fill <username> yourself
1212
#r "C:/Users/<username>/source/repos/CSharpMath/Typography/Build/NetStandard/Typography.TextBreak/bin/Debug/netstandard1.3/Typography.TextBreak.dll"
1313
using Typography.TextBreak;
1414
(int, WordKind, char)[] BreakText(string text) {
15-
var breaker = new CustomBreaker();
16-
var breakList = new List<BreakAtInfo>();
17-
breaker.BreakWords(text);
18-
breaker.LoadBreakAtList(breakList);
19-
//index is after the boundary -> last one will be out of range
20-
return breakList.Select(i => (i.breakAt, i.wordKind, text.ElementAtOrDefault(i.breakAt))).ToArray();
15+
var breaker = new CustomBreaker();
16+
var breakList = new List<BreakAtInfo>();
17+
breaker.BreakWords(text);
18+
breaker.LoadBreakAtList(breakList);
19+
//index is after the boundary -> last one will be out of range
20+
return breakList.Select(i => (i.breakAt, i.wordKind, text.ElementAtOrDefault(i.breakAt))).ToArray();
2121
}
2222
BreakText(@"Here are some text $1 + 12 \frac23 \sqrt4$ $$Display$$ text")
23-
*/
24-
/* //Version 2
23+
*/
24+
/* //Version 2
2525
#r "C:/Users/<username>/source/repos/CSharpMath/Typography/Build/NetStandard/Typography.TextBreak/bin/Debug/netstandard1.3/Typography.TextBreak.dll"
2626
using Typography.TextBreak;
2727
string BreakText(string text, string seperator = "|")
2828
{
29-
var breaker = new CustomBreaker();
30-
var breakList = new List<BreakAtInfo>();
31-
breaker.BreakWords(text);
32-
breaker.LoadBreakAtList(breakList);
33-
//reverse to ensure earlier inserts do not affect later ones
34-
foreach (var @break in breakList.Select(i => i.breakAt).Reverse())
35-
text = text.Insert(@break, seperator);
36-
return text;
29+
var breaker = new CustomBreaker();
30+
var breakList = new List<BreakAtInfo>();
31+
breaker.BreakWords(text);
32+
breaker.LoadBreakAtList(breakList);
33+
//reverse to ensure earlier inserts do not affect later ones
34+
foreach (var @break in breakList.Select(i => i.breakAt).Reverse())
35+
text = text.Insert(@break, seperator);
36+
return text;
3737
}
3838
BreakText(@"Here are some text $1 + 12 \frac23 \sqrt4$ $$Display$$ text")
39-
*/
40-
public static Result<TextAtom> Build(string text, bool enhancedColors) {
41-
if (string.IsNullOrEmpty(text)) return new TextAtom.List(Array.Empty<TextAtom>(), 0);
39+
*/
40+
public static bool NoEnhancedColors { get; set; }
41+
public static Result<TextAtom> Build(string latex) {
42+
if (string.IsNullOrEmpty(latex)) return new TextAtom.List(Array.Empty<TextAtom>(), 0);
4243
bool? displayMath = null;
4344
StringBuilder mathLaTeX = null;
4445
bool backslashEscape = false;
@@ -47,7 +48,7 @@ public static Result<TextAtom> Build(string text, bool enhancedColors) {
4748
var atoms = new TextAtomListBuilder();
4849
var breaker = new CustomBreaker();
4950
var breakList = new List<BreakAtInfo>();
50-
breaker.BreakWords(text, false);
51+
breaker.BreakWords(latex, false);
5152
breaker.LoadBreakAtList(breakList);
5253
Result CheckDollarCount() {
5354
switch (dollarCount) {
@@ -93,7 +94,7 @@ Result CheckDollarCount() {
9394
return Ok();
9495
}
9596
(int startAt, int endAt, char endingChar, WordKind wordKind) ObtainRange(int i) =>
96-
(i == 0 ? 0 : breakList[i - 1].breakAt, breakList[i].breakAt, text[breakList[i].breakAt - 1], breakList[i].wordKind);
97+
(i == 0 ? 0 : breakList[i - 1].breakAt, breakList[i].breakAt, latex[breakList[i].breakAt - 1], breakList[i].wordKind);
9798
for (var i = 0; i < breakList.Count; i++) {
9899
var (startAt, endAt, endingChar, wordKind) = ObtainRange(i);
99100
bool SetNextRange() {
@@ -105,7 +106,7 @@ Result<string> ReadArgument() {
105106
afterCommand = false;
106107
if (!SetNextRange()) return Err("Missing argument");
107108
if (endingChar != '{') {
108-
var toReturn = text[startAt].ToString();
109+
var toReturn = latex[startAt].ToString();
109110
#warning Not one char only, should skip spaces then read next char, and it is a possible command
110111
//range contains one char only
111112
if (startAt == endAt)
@@ -116,14 +117,14 @@ Result<string> ReadArgument() {
116117
}
117118
int endingIndex = -1;
118119
//startAt + 1 to not start at the { we started at
119-
for (int j = startAt + 1, bracketDepth = 0; j < text.Length; j++) {
120-
if (text[j] == '{') bracketDepth++;
121-
else if (text[j] == '}')
120+
for (int j = startAt + 1, bracketDepth = 0; j < latex.Length; j++) {
121+
if (latex[j] == '{') bracketDepth++;
122+
else if (latex[j] == '}')
122123
if (bracketDepth > 0) bracketDepth--;
123124
else { endingIndex = j; break; }
124125
}
125126
if (endingIndex == -1) return Err("Missing }");
126-
var resultText = text.Substring(endAt, endingIndex - endAt);
127+
var resultText = latex.Substring(endAt, endingIndex - endAt);
127128
while (startAt < endingIndex)
128129
_ = SetNextRange(); //this never fails because the above check
129130
return Ok(resultText);
@@ -143,7 +144,7 @@ Result<string> ReadArgument() {
143144

144145
//Normal unescaped text section, could be in display/inline math mode
145146
if (!backslashEscape) {
146-
var textSection = text.Substring(startAt, endAt - startAt);
147+
var textSection = latex.Substring(startAt, endAt - startAt);
147148
switch (endingChar) {
148149
case '$':
149150
throw new InvalidCodePathException("The $ case should have been accounted for.");
@@ -223,7 +224,7 @@ Result<string> ReadArgument() {
223224
}
224225
break;
225226
default:
226-
mathLaTeX.Append($@"\{text.Substring(startAt, endAt - startAt)}");
227+
mathLaTeX.Append($@"\{latex.Substring(startAt, endAt - startAt)}");
227228
break;
228229
}
229230
backslashEscape = false;
@@ -232,7 +233,7 @@ Result<string> ReadArgument() {
232233

233234
//Escaped text section and not in inline/display math mode
234235
afterCommand = true;
235-
switch (text.Substring(startAt, endAt - startAt)) {
236+
switch (latex.Substring(startAt, endAt - startAt)) {
236237
case "(":
237238
mathLaTeX = new StringBuilder();
238239
displayMath = false;
@@ -273,7 +274,7 @@ Result<string> ReadArgument() {
273274
Ok(parsedResult) :
274275
Err("Invalid font size")
275276
).Bind(
276-
ReadArgument().Bind(resizedContent => Build(resizedContent, enhancedColors)),
277+
ReadArgument().Bind(Build),
277278
(fontSize, resizedContent) =>
278279
atoms.Add(resizedContent, fontSize, "fontsize".Length)
279280
).Error is string error
@@ -282,24 +283,24 @@ Result<string> ReadArgument() {
282283
}
283284
case "color": {
284285
if (ReadArgument().Bind(color =>
285-
Color.Create(color, enhancedColors) is Color value ?
286+
Color.Create(color, !NoEnhancedColors) is Color value ?
286287
Ok(value) :
287288
Err("Invalid color")
288289
).Bind(
289-
ReadArgument().Bind(coloredContent => Build(coloredContent, enhancedColors)),
290+
ReadArgument().Bind(Build),
290291
(color, coloredContent) =>
291292
atoms.Add(coloredContent, color, "color".Length)
292293
).Error is string error
293294
) return error;
294295
break;
295296
}
296297
//case "red", "yellow", ...
297-
case var shortColor when enhancedColors && Color.PredefinedColors.Contains(shortColor): {
298-
if (Ok(Color.Create(shortColor, enhancedColors) ??
298+
case var shortColor when !NoEnhancedColors && Color.PredefinedColors.Contains(shortColor): {
299+
if (Ok(Color.Create(shortColor, !NoEnhancedColors) ??
299300
throw new InvalidCodePathException(
300301
"This case's condition should have checked the validity of shortColor.")
301302
).Bind(
302-
ReadArgument().Bind(coloredContent => Build(coloredContent, enhancedColors)),
303+
ReadArgument().Bind(Build),
303304
(color, coloredContent) =>
304305
atoms.Add(coloredContent, color, shortColor.Length)
305306
).Error is string error
@@ -309,7 +310,7 @@ Result<string> ReadArgument() {
309310
//case "textbf", "textit", ...
310311
case var command when !command.Contains("math") && FontStyleExtensions.FontStyles.TryGetByFirst(command.Replace("text", "math"), out var fontStyle): {
311312
if (ReadArgument()
312-
.Bind(content => Build(content, enhancedColors))
313+
.Bind(Build)
313314
.Bind(builtContent => atoms.Add(builtContent, fontStyle, command.Length))
314315
.Error is string error)
315316
return error;

CSharpMath.Rendering/Text/TextDisplayLineBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Add(Display display, float ascender, float descender, float gapAfter
2525
_queue.Enqueue(display);
2626
}
2727

28-
public void Clear(float x, float y, ICollection<Display> accumulator, ref float verticalAdvance, bool appendLineGap) {
28+
public void Clear(float x, float y, ICollection<Display> accumulator, ref float verticalAdvance, bool appendLineGap, float additionalLineSpacing) {
2929
verticalAdvance += Ascent;
3030
for (int i = _queue.Count; i > 0; i--) {
3131
var display = _queue.Dequeue();
@@ -34,7 +34,7 @@ public void Clear(float x, float y, ICollection<Display> accumulator, ref float
3434
accumulator.Add(display);
3535
}
3636
verticalAdvance += Descent;
37-
if(appendLineGap) verticalAdvance += GapAfterLine;
37+
if(appendLineGap) verticalAdvance += GapAfterLine + additionalLineSpacing;
3838
_widthOffset = Ascent = Descent = Width = GapAfterLine = 0;
3939
}
4040
}

CSharpMath.Rendering/Text/TextLayoutter.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ namespace CSharpMath.Rendering {
77
using Displays = Display.MathListDisplay<Fonts, Glyph>;
88

99
public static class TextLayoutter {
10-
public static (Displays relative, Displays absolute) Layout(TextAtom input, Fonts inputFont, float canvasWidth) {
10+
public static (Displays relative, Displays absolute) Layout(TextAtom input, Fonts inputFont, float canvasWidth, float additionalLineSpacing) {
1111
if (input == null) return
1212
(new Displays(Array.Empty<IDisplay<Fonts, Glyph>>()),
1313
new Displays(Array.Empty<IDisplay<Fonts, Glyph>>()));
1414
float accumulatedHeight = 0;
1515
TextDisplayLineBuilder line = new TextDisplayLineBuilder();
1616
void BreakLine(List<IDisplay<Fonts, Glyph>> displayList, bool appendLineGap = true) =>
17-
line.Clear(0, -accumulatedHeight, displayList, ref accumulatedHeight, appendLineGap);
18-
void AddDisplaysWithLineBreaks(TextAtom atom, Fonts fonts,
17+
line.Clear(0, -accumulatedHeight, displayList, ref accumulatedHeight, appendLineGap, additionalLineSpacing);
18+
void AddDisplaysWithLineBreaks(
19+
TextAtom atom,
20+
Fonts fonts,
1921
List<IDisplay<Fonts, Glyph>> displayList,
2022
List<IDisplay<Fonts, Glyph>> displayMathList,
21-
FontStyle style = FontStyle.Roman, /*FontStyle.Default is FontStyle.Italic, FontStyle.Roman is no change to characters*/
22-
Structures.Color? color = null) {
23+
FontStyle style,
24+
Structures.Color? color
25+
) {
2326

2427
IDisplay<Fonts, Glyph> display;
2528
switch (atom) {
@@ -44,7 +47,7 @@ void AddDisplaysWithLineBreaks(TextAtom atom, Fonts fonts,
4447
BreakLine(displayList);
4548
break;
4649
case TextAtom.Math m when m.DisplayStyle:
47-
BreakLine(displayList);
50+
BreakLine(displayList, false);
4851
#warning Replace 12 with a more appropriate spacing
4952
accumulatedHeight += 12;
5053
display = Typesetter<Fonts, Glyph>.CreateLine(m.Content, fonts, TypesettingContext.Instance, LineStyle.Display);
@@ -71,7 +74,7 @@ void FinalizeInlineDisplay(float ascender, float rawDescender, float lineGap, bo
7174
//Calling Select(g => g.Typeface).Distinct() speeds up query up to 10 times,
7275
//Calling Max(Func<,>) instead of Select(Func<,>).Max() speeds up query 2 times
7376
var typefaces = glyphs.Select(g => g.Typeface).Distinct();
74-
WarningException.WarnIf(typefaces,
77+
WarningException.WarnIfAny(typefaces,
7578
tf => !Typography.OpenFont.Extensions.TypefaceExtensions.RecommendToUseTypoMetricsForLineSpacing(tf),
7679
"This font file is too old. Only font files that support standard typographical metrics are supported.");
7780
display = new TextRunDisplay<Fonts, Glyph>(Display.Text.AttributedGlyphRuns.Create(content, glyphs, fonts, false), t.Range, TypesettingContext.Instance);
@@ -102,7 +105,14 @@ void FinalizeInlineDisplay(float ascender, float rawDescender, float lineGap, bo
102105
}
103106
var relativePositionList = new List<IDisplay<Fonts, Glyph>>();
104107
var absolutePositionList = new List<IDisplay<Fonts, Glyph>>();
105-
AddDisplaysWithLineBreaks(input, inputFont, relativePositionList, absolutePositionList);
108+
AddDisplaysWithLineBreaks(
109+
input,
110+
inputFont,
111+
relativePositionList,
112+
absolutePositionList,
113+
FontStyle.Roman /*FontStyle.Default is FontStyle.Italic, FontStyle.Roman is no change to characters*/,
114+
null
115+
);
106116
BreakLine(relativePositionList); //remember to finalize the last line
107117
return (new Displays(relativePositionList),
108118
new Displays(absolutePositionList));

0 commit comments

Comments
 (0)