Skip to content

Commit

Permalink
Fixed the y-coordinate to start top-left, not bottom-left
Browse files Browse the repository at this point in the history
  • Loading branch information
Happypig375 committed Jul 16, 2018
1 parent dda5558 commit 949a2e4
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 24 deletions.
1 change: 1 addition & 0 deletions CSharpMath.Forms.Example/CSharpMath.Forms.Example/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<local:ExamplesPage/>
<local:MoreExamplesPage/>
<local:CustomExamplePage/>
<local:SlidePage/>
<local:SettingsPage/>
</TabbedPage>
</Application.MainPage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
<EmbeddedResource Update="SettingsPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="SlidePage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ private void PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
var c = e.Surface.Canvas;
//draw outer circle
c.DrawCircle(cx, cy, r + thicknessAdjust, black);
painter.TextColor = SKColors.Orange;
painter.TextColor = SKColors.White;
for (int i = 0; i < count; i++) {
painter.LaTeX = i.ToString();
painter.Draw(c, offsetY: -r);
c.DrawCircle(cx, cy, 10, new SKPaint { Style = SKPaintStyle.Fill, Color = SKColors.Green });
painter.Draw(c, cx, cy - r * 3 / 4);
c.RotateDegrees(θ, cx, cy);
}
//draw inner circle
//c.DrawCircle(cx, cy, r - thicknessAdjust, white);
c.DrawCircle(cx, cy, r - thicknessAdjust, white);
painter.TextColor = SKColors.Black;
painter.LaTeX = @"\raisebox{25mu}{\text{\kern.7222emC\#}\\Math}"; //.7222em is 13/18em
painter.Draw(c);
Expand Down
20 changes: 20 additions & 0 deletions CSharpMath.Forms.Example/CSharpMath.Forms.Example/SlidePage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
x:Class="CSharpMath.Forms.Example.SlidePage"
Title="Slide">
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="{x:Binding Value, Source={x:Reference SliderX}}"/>
<Slider x:Name="SliderX" HorizontalOptions="FillAndExpand" Minimum="-2" Maximum="{Binding Width, Source={x:Reference Canvas}}" ValueChanged="SliderX_ValueChanged"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="{x:Binding Value, Source={x:Reference SliderY}}"/>
<Slider x:Name="SliderY" HorizontalOptions="FillAndExpand" Minimum="-2" Maximum="{Binding Height, Source={x:Reference Canvas}}" ValueChanged="SliderY_ValueChanged"/>
</StackLayout>
<skia:SKCanvasView x:Name="Canvas" HorizontalOptions="Fill" VerticalOptions="FillAndExpand" EnableTouchEvents="True" PaintSurface="Canvas_PaintSurface" Touch="Canvas_Touch"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

using SkiaSharp.Views.Forms;

namespace CSharpMath.Forms.Example
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SlidePage : ContentPage {
SkiaSharp.SkiaMathPainter painter = new SkiaSharp.SkiaMathPainter { LaTeX = @"\text{Press to clear}" };
bool reset;
double x, y;

public SlidePage() => InitializeComponent();

private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
if (reset) { e.Surface.Canvas.Clear(); reset = false; }
e.Surface.Canvas.DrawCircle(0f, 0f, 100f, new global::SkiaSharp.SKPaint { Color = new global::SkiaSharp.SKColor(255, 0, 0) });
painter.Draw(e.Surface.Canvas, (float)x, (float)y);
e.Surface.Canvas.DrawCircle(300f, 0f, 100f, new global::SkiaSharp.SKPaint { Color = new global::SkiaSharp.SKColor(255, 255, 0) });
}

private void Canvas_Touch(object sender, SKTouchEventArgs e) {
if(e.InContact && e.ActionType == SKTouchAction.Pressed) { reset = true; Canvas.InvalidateSurface(); e.Handled = true; }
}

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(); }
}
}
43 changes: 25 additions & 18 deletions CSharpMath.Rendering/MathPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public RectangleF? Measure {

#region Methods
private static PointF GetDisplayPosition(MathListDisplay<TFonts, Glyph> displayList,
TextAlignment alignment, float fontSize,
float width, float height, Thickness padding, float offsetX, float offsetY) {
float fontSize, bool bottomLeftCoords,
float width, float height,
TextAlignment alignment, Thickness padding, float offsetX, float offsetY) {
float x, y;
float displayWidth = displayList.Width;
if ((alignment & TextAlignment.Left) != 0)
Expand All @@ -87,16 +88,24 @@ private static PointF GetDisplayPosition(MathListDisplay<TFonts, Glyph> displayL
if (contentHeight < fontSize / 2) {
contentHeight = fontSize / 2;
}
//Canvas is inverted!
if (!bottomLeftCoords) {
//Canvas is inverted!
if ((alignment & (TextAlignment.Top | TextAlignment.Bottom)) != 0) {
alignment ^= TextAlignment.Top;
alignment ^= TextAlignment.Bottom;
}
//invert y-coordinate as canvas is inverted
offsetY *= -1;
}
if ((alignment & TextAlignment.Top) != 0)
y = height - padding.Bottom - displayList.Ascent;
else if ((alignment & TextAlignment.Bottom) != 0)
y = padding.Top + displayList.Descent;
else if ((alignment & TextAlignment.Bottom) != 0)
y = height - padding.Bottom - displayList.Ascent;
else {
float availableHeight = height - padding.Top - padding.Bottom;
y = ((availableHeight - contentHeight) / 2) + padding.Top + displayList.Descent;
}
return new PointF(x + offsetX, y + offsetY);
return new PointF(x + offsetX, (y + offsetY) - (bottomLeftCoords ? 0 : height));
}

public void UpdateDisplay() {
Expand All @@ -110,32 +119,30 @@ public void UpdateDisplay() {

public abstract ICanvas WrapCanvas(TCanvas canvas);

protected abstract bool CoordinatesFromBottomLeftInsteadOfTopLeft { get; }

public void Draw(TCanvas canvas, TextAlignment alignment = TextAlignment.Center, Thickness padding = default, float offsetX = 0, float offsetY = 0) {
if (MathList == null) return;
if (_displayChanged) UpdateDisplay();
var c = WrapCanvas(canvas);
//invert y-coordinate as canvas is inverted
Draw(c, GetDisplayPosition(_displayList, alignment, FontSize, c.Width, c.Height, padding, offsetX, -offsetY));
Draw(c, GetDisplayPosition(_displayList, FontSize, CoordinatesFromBottomLeftInsteadOfTopLeft, c.Width, c.Height, alignment, padding, offsetX, offsetY));
}

public void Draw(TCanvas canvas, float x, float y) =>
//invert y-coordinate as canvas is inverted
Draw(WrapCanvas(canvas), new PointF(x, -y));
Draw(WrapCanvas(canvas), new PointF(x, CoordinatesFromBottomLeftInsteadOfTopLeft ? y : -y));

public void Draw(TCanvas canvas, PointF position)
{
position.Y *= -1;
//invert y-coordinate as canvas is inverted
public void Draw(TCanvas canvas, PointF position) {
if (CoordinatesFromBottomLeftInsteadOfTopLeft) position.Y *= -1;
Draw(WrapCanvas(canvas), position);
}

private void Draw(ICanvas canvas, PointF position) {
if (MathList != null) {
canvas.Save();
//invert the canvas vertically
canvas.Scale(1, -1);
//canvas is inverted so negate vertical position
canvas.Translate(0, -canvas.Height);
if (!CoordinatesFromBottomLeftInsteadOfTopLeft) {
//invert the canvas vertically
canvas.Scale(1, -1);
}
canvas.Scale(Magnification, Magnification);
canvas.DefaultColor = WrapColor(TextColor);
canvas.CurrentColor = WrapColor(BackgroundColor);
Expand Down
4 changes: 4 additions & 0 deletions CSharpMath.SkiaSharp/SkiaMathPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public SkiaMathPainter(float fontSize = 20f, bool antiAlias = true) : base(fontS
public SKStrokeCap StrokeCap { get; set; }
public bool AntiAlias { get; set; }

public void Draw(SKCanvas canvas, SKPoint point) => Draw(canvas, point.X, point.Y);

protected override bool CoordinatesFromBottomLeftInsteadOfTopLeft => false;

public override SKColor UnwrapColor(Color color) => color.ToNative();

public override Color WrapColor(SKColor color) => color.FromNative();
Expand Down
8 changes: 7 additions & 1 deletion CSharpMath.Text/TextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ public static class TextBuilder {
public static TextAtom Build(string text) {
var breaker = new CustomBreaker();
var breakList = new List<BreakAtInfo>();
breaker.BreakWords(text);
breaker.LoadBreakAtList(breakList);
bool mathMode = false;
foreach (var info in breakList) {
info.wordKind == WordKind.
if (text[info.breakAt] == '$') {
mathMode ^= true;
}

}
return null;
}
public static StringBuilder Unbuild(TextAtom atom, StringBuilder b) {
switch (atom) {
Expand Down
13 changes: 13 additions & 0 deletions CSharpMath._Extensions/CSharpMath._Extensions.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>4ab068f2-6f09-4b3b-8a81-f89f10cdce18</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>CSharpMath._Extensions</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions CSharpMath._Extensions/CSharpMath._Extensions.shproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>4ab068f2-6f09-4b3b-8a81-f89f10cdce18</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="CSharpMath._Extensions.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion CSharpMath.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "zDevelopment utilities", "z
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpMath.NuGetPackageTests.CSharp.UWP", "CSharpMath.NuGetPackageTests.CSharp\CSharpMath.NuGetPackageTests.CSharp.UWP\CSharpMath.NuGetPackageTests.CSharp.UWP.csproj", "{DBB121AA-0271-4719-A709-23AB3B464E8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpMath.NuGetPackageTests.CSharp", "CSharpMath.NuGetPackageTests.CSharp\CSharpMath.NuGetPackageTests.CSharp\CSharpMath.NuGetPackageTests.CSharp.csproj", "{73C44AA1-69ED-4749-BDBA-62A4416F0081}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.NuGetPackageTests.CSharp", "CSharpMath.NuGetPackageTests.CSharp\CSharpMath.NuGetPackageTests.CSharp\CSharpMath.NuGetPackageTests.CSharp.csproj", "{73C44AA1-69ED-4749-BDBA-62A4416F0081}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "zNuGet package tests", "zNuGet package tests", "{FD179998-539F-43BE-B2A1-89B2B726BC7A}"
EndProject
Expand Down

0 comments on commit 949a2e4

Please sign in to comment.