Skip to content

Commit

Permalink
Day 3 of Sprint 1 (see #15 (comment))
Browse files Browse the repository at this point in the history
  • Loading branch information
Happypig375 committed Aug 8, 2018
1 parent 258769d commit 19393e4
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 72 deletions.
51 changes: 44 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# This file is the top-most EditorConfig file
root = true

[*.cs]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = false
trim_trailing_whitespace = true

#############################
# Deviates from .NET Formatting Conventions
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#formatting-conventions
#############################

# Organize usings
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#usings
dotnet_sort_system_directives_first = true
# C# formatting settings
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#c-formatting-settings
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
Expand All @@ -11,18 +26,40 @@ csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = false

# Indentation options
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#indent
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = false

# Spacing options
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#spacing
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_call_parameter_list_parenteses = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = true
csharp_space_between_parentheses = false

csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping options
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#wrapping
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true

dotnet_sort_system_directives_first = true


# More Indentation options (Undocumented)
csharp_indent_block_contents = true
csharp_indent_braces = false
# Spacing Options (Undocumented)
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_declaration_statements = do_not_ignore
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_square_brackets = false
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ namespace CSharpMath.Forms.Example
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LayoutPage : ContentPage {
SkiaSharp.TextPainter painter = new SkiaSharp.TextPainter { Text = @"Here are some text. This text is made to be long enough to have the TextPainter of CSharpMath (hopefully) add a line break to this text automatically. To demonstrate the capabilities of the TextPainter, here are some math content: First, a fraction in inline mode: $\frac34$ Next, a summation in inline mode: $\sum_{i=0}^3i^i$ Then, a summation in display mode: $$\sum_{i=0}^3i^i$$ (ah, bugs.) After that, an integral in display mode: $$\int^6_{-56}x\ dx$$ Finally, an escaped dollar sign \$ that represents the start/end of math mode when it is unescaped. Even though colours are currently unsupported, it can be done via math mode with the \\color command with the help of the \\text command. It looks like this: $\color{#F00}{\text{some red text}}$, which is nearly indistinguishable from non-math mode aside from not being able to automatically break up when spaces are inside the coloured text. The SkiaSharp version of this is located at CSharpMath.SkiaSharp.TextPainter; and the Xamarin.Forms version of this is located at CSharpMath.Forms.TextView. Was added in 0.1.0-pre4; working in 0.1.0-pre5." };
SkiaSharp.TextPainter painter = new SkiaSharp.TextPainter { Text = @"Here are some text.
This text is made to be long enough to have the TextPainter of CSharpMath add a line break to this text automatically.
To demonstrate the capabilities of the TextPainter,
here are some math content:
First, a fraction in inline mode: $\frac34$
Next, a summation in inline mode: $\sum_{i=0}^3i^i$
Then, a summation in display mode: $$\sum_{i=0}^3i^i$$
After that, an integral in display mode: $$\int^6_{-56}x\ dx$$
Finally, an escaped dollar sign \$ that represents the start/end of math mode when it is unescaped.
Colors can be achieved via \backslash color{color}{content}, or \backslash \textit{color}{content},
where \textit{color} stands for one of the LaTeX standard colors.
\red{Colored text in text mode are able to automatically break up when spaces are inside the colored text, which the equivalent in math mode cannot do.}
\textbf{Styled} \texttt{text} can be achieved via the LaTeX styling commands.
The SkiaSharp version of this is located at CSharpMath.SkiaSharp.TextPainter;
and the Xamarin.Forms version of this is located at CSharpMath.Forms.TextView.
Was added in 0.1.0-pre4; working in 0.1.0-pre5; fully tested in 0.1.0-pre6." };
bool reset;
double x, y, w;

Expand All @@ -22,13 +37,26 @@ private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
var measure = painter.Measure((float)w).Value;
e.Surface.Canvas.Clear();
e.Surface.Canvas.DrawRect((float)x + measure.X, (float)y + measure.Y, measure.Width, measure.Height, new global::SkiaSharp.SKPaint { Color = global::SkiaSharp.SKColors.Orange });
painter.Draw(e.Surface.Canvas, new System.Drawing.PointF((float)x, (float)y), (float)w);
/*measure = painter._absoluteXCoordDisplay.ComputeDisplayBounds();
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 });
measure = painter._relativeXCoordDisplay.ComputeDisplayBounds();
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 });
*/painter.Draw(e.Surface.Canvas, new System.Drawing.PointF((float)x, (float)y), (float)w);
}

private void SliderX_ValueChanged(object sender, ValueChangedEventArgs e) { x = e.NewValue; SliderW.Maximum = SliderX.Maximum - x; Canvas.InvalidateSurface(); }
//Add Epsilon to prevent SliderW.Minimum == SiderW.Maximum == 0
private void SliderX_ValueChanged(object sender, ValueChangedEventArgs e) { x = e.NewValue; SliderW.Maximum = SliderX.Maximum - x + float.Epsilon; Canvas.InvalidateSurface(); }

private void SliderY_ValueChanged(object sender, ValueChangedEventArgs e) { y = e.NewValue; Canvas.InvalidateSurface(); }

private void SliderW_ValueChanged(object sender, ValueChangedEventArgs e) { w = e.NewValue; Canvas.InvalidateSurface(); }

protected override void OnSizeAllocated(double width, double height) {
base.OnSizeAllocated(width, height);
//+1 to avoid Maximum == Minimum == 0 (which throws) but allow for sliding
//Add Epsilon to prevent SliderW.Minimum == SiderW.Maximum == 0
(SliderX.Maximum, SliderY.Maximum, SliderW.Maximum) = (Canvas.CanvasSize.Width + 1, Canvas.CanvasSize.Height + 1, Canvas.CanvasSize.Width + 1 - SliderX.Value + float.Epsilon);
SliderX.Minimum = SliderY.Minimum = SliderW.Minimum = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ private void Canvas_Touch(object sender, SKTouchEventArgs e) {
private void SliderX_ValueChanged(object sender, ValueChangedEventArgs e) { x = e.NewValue; Canvas.InvalidateSurface(); }

private void SliderY_ValueChanged(object sender, ValueChangedEventArgs e) { y = e.NewValue; Canvas.InvalidateSurface(); }

protected override void OnSizeAllocated(double width, double height) {
base.OnSizeAllocated(width, height);
//+1 to avoid Maximum == Minimum == 0 (which throws) but allow for sliding
(SliderX.Maximum, SliderY.Maximum) = (Canvas.CanvasSize.Width + 1, Canvas.CanvasSize.Height + 1);
SliderX.Minimum = SliderY.Minimum = 0;
}
}
}
6 changes: 3 additions & 3 deletions CSharpMath.Forms/CSharpMath.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>0.1.0-pre5</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie)</Authors>
<PackageVersion>0.1.0-pre6</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
<PackageReleaseNotes>The 0.1.0 SkiaSharp Update brings the SkiaSharp and Xamarin.Forms front ends to CSharpMath.</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
<RepositoryBranch>master</RepositoryBranch>
<RepositoryCommit>aff4172bd424bbd66a47e028bdff96c30573bc12</RepositoryCommit>
<RepositoryCommit>258769db65d722bb29110896d9356d4a88c60d38</RepositoryCommit>
<PackageProjectUrl>https://github.com/verybadcat/CSharpMath</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>
Expand Down
6 changes: 3 additions & 3 deletions CSharpMath.Ios/CSharpMath.Ios.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<NuGetPackageImportStamp></NuGetPackageImportStamp>
<PackOnBuild>true</PackOnBuild>
<PackageId>CSharpMath.Ios</PackageId>
<PackageVersion>0.1.0-pre5</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie)</Authors>
<PackageVersion>0.1.0-pre6</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
<NeutralLanguage>en</NeutralLanguage>
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/verybadcat/CSharpMath</PackageProjectUrl>
Expand All @@ -28,7 +28,7 @@
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryBranch>master</RepositoryBranch>
<RepositoryCommit>aff4172bd424bbd66a47e028bdff96c30573bc12</RepositoryCommit>
<RepositoryCommit>258769db65d722bb29110896d9356d4a88c60d38</RepositoryCommit>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageIconUrl>https://raw.githubusercontent.com/verybadcat/CSharpMath/master/Icon.png</PackageIconUrl>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions CSharpMath.Rendering/CSharpMath.Rendering.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<!--NuGet Properties-->
<PackOnBuild>true</PackOnBuild>
<PackageId>CSharpMath.Rendering</PackageId>
<PackageVersion>0.1.0-pre5</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie)</Authors>
<PackageVersion>0.1.0-pre6</PackageVersion>
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
<PackageLicenseUrl>https://github.com/verybadcat/CSharpMath/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/verybadcat/CSharpMath</PackageProjectUrl>
<PackageReleaseNotes>The 0.1.0 SkiaSharp Update brings the SkiaSharp and Xamarin.Forms front ends to CSharpMath.</PackageReleaseNotes>
Expand All @@ -24,7 +24,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
<RepositoryBranch>master</RepositoryBranch>
<RepositoryCommit>aff4172bd424bbd66a47e028bdff96c30573bc12</RepositoryCommit>
<RepositoryCommit>258769db65d722bb29110896d9356d4a88c60d38</RepositoryCommit>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageIconUrl>https://raw.githubusercontent.com/verybadcat/CSharpMath/master/Icon.png</PackageIconUrl>
</PropertyGroup>
Expand Down
19 changes: 4 additions & 15 deletions CSharpMath.Rendering/MathPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public MathPainter(float fontSize = DefaultFontSize) : base(fontSize) { }
public IMathList MathList { get => Source.MathList; set => Source = new MathSource(value); }
public string LaTeX { get => Source.LaTeX; set => Source = new MathSource(value); }

protected override RectangleF? MeasureCore(float canvasWidth = float.NaN) => _display?.ComputeDisplayBounds();
protected override RectangleF? MeasureCore(float canvasWidth = float.NaN) =>
_display?.ComputeDisplayBounds(!CoordinatesFromBottomLeftInsteadOfTopLeft);
public RectangleF? Measure {
get {
UpdateDisplay();
Expand All @@ -34,21 +35,9 @@ public override void Draw(TCanvas canvas, TextAlignment alignment = TextAlignmen
if (!Source.IsValid) DrawError(c);
else {
UpdateDisplay(c.Width);
Draw(c, _display, IPainterExtensions.GetDisplayPosition(_display.Width, _display.Ascent, _display.Descent, FontSize, CoordinatesFromBottomLeftInsteadOfTopLeft, c.Width, c.Height, alignment, padding, offsetX, offsetY));
DrawCore(c, _display, IPainterExtensions.GetDisplayPosition(_display.Width, _display.Ascent, _display.Descent, FontSize, CoordinatesFromBottomLeftInsteadOfTopLeft, c.Width, c.Height, alignment, padding, offsetX, offsetY));
}
}

public void Draw(TCanvas canvas, float x, float y) {
var c = WrapCanvas(canvas);
UpdateDisplay(c.Width);
Draw(c, _display, new PointF(x, CoordinatesFromBottomLeftInsteadOfTopLeft ? y : -y));
}

public void Draw(TCanvas canvas, PointF position) {
var c = WrapCanvas(canvas);
if (CoordinatesFromBottomLeftInsteadOfTopLeft) position.Y *= -1;
UpdateDisplay(c.Width);
Draw(c, _display, position);
}
public void Draw(TCanvas canvas, float x, float y) => Draw(canvas, TextAlignment.TopLeft, default, x, y);
}
}
8 changes: 5 additions & 3 deletions CSharpMath.Rendering/Painter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public Painter(float fontSize = DefaultFontSize) {
protected abstract void UpdateDisplay(float canvasWidth);

protected abstract RectangleF? MeasureCore(float canvasWidth);

protected void Draw(ICanvas canvas, IDisplay<TFonts, Glyph> display, PointF? position = null) {

protected RectangleF? InvertRect(RectangleF? r) { if (r.HasValue) return new RectangleF(r.Value.X, r.Value.Y, r.Value.Width, r.Value.Height * -1); return r; }

protected void DrawCore(ICanvas canvas, IDisplay<TFonts, Glyph> display, PointF? position = null) {
if (Source.IsValid) {
if(position != null) display.Position = position.Value;
canvas.Save();
Expand All @@ -87,7 +89,7 @@ protected void Draw(ICanvas canvas, IDisplay<TFonts, Glyph> display, PointF? pos
canvas.CurrentColor = WrapColor(HighlightColor);
canvas.CurrentStyle = PaintStyle;
var measure = MeasureCore(canvas.Width) ??
throw new InvalidOperationException($"{nameof(MeasureCore)} returned null. Any conditions leading to this should have already been checked via {nameof(Source)}.{nameof(Source.IsValid)}.");
throw new InvalidCodePathException($"{nameof(MeasureCore)} returned null. Any conditions leading to this should have already been checked via {nameof(Source)}.{nameof(Source.IsValid)}.");
canvas.FillRect(display.Position.X + measure.X, display.Position.Y -
(CoordinatesFromBottomLeftInsteadOfTopLeft ? display.Ascent : display.Descent),
measure.Width, measure.Height);
Expand Down
5 changes: 4 additions & 1 deletion CSharpMath.Rendering/Text/TextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ string ReadInsideBrackets() {
#warning Support single-char arguments
(startAt, endAt, endingChar) = ObtainRange(++i);
if (endingChar != '{') { error = "Missing {"; return null; }
var endingIndex = text.IndexOf('}', endAt);
int numOfBrackets = 0;
int endingIndex = -1;
//+1 to not start at the { we started at
for (int j = startAt + 1; j < text.Length; j++) { if (text[j] == '{') numOfBrackets++; else if (text[j] == '}') if (numOfBrackets > 0) numOfBrackets--; else { endingIndex = j; break; } }
if (endingIndex == -1) { error = "Missing }"; return null; }
var resultText = text.Substring(endAt, endingIndex - endAt);
while (startAt < endingIndex) (startAt, endAt, endingChar) = ObtainRange(++i);
Expand Down
Loading

0 comments on commit 19393e4

Please sign in to comment.