Skip to content

Commit 810b47c

Browse files
authored
Align text center and right correctly (#126)
* Implemented center and right align correctly * Update ReadMe as well * -PositiveInfinity -> NegativeInfinity * Update ReadMe as well 2 * Fix master tests * Take duplicate code into a variable
1 parent a1a646e commit 810b47c

File tree

289 files changed

+136
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

289 files changed

+136
-52
lines changed

CSharpMath.Avalonia/Extensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,28 @@ public static SolidColorBrush ToSolidColorBrush(this CSharpMathColor color) =>
3333
class DrawVisual<TContent> : Visual where TContent : class {
3434
readonly Painter<AvaloniaCanvas, TContent, AvaloniaColor> painter;
3535
readonly System.Drawing.RectangleF measure;
36+
readonly CSharpMathTextAlignment alignment;
3637
public DrawVisual(Painter<AvaloniaCanvas, TContent, AvaloniaColor> painter,
37-
System.Drawing.RectangleF measure) {
38+
System.Drawing.RectangleF measure, CSharpMathTextAlignment alignment) {
3839
this.painter = painter;
3940
this.measure = measure;
41+
this.alignment = alignment;
4042
}
4143
public override void Render(DrawingContext context) {
4244
base.Render(context);
4345
var canvas = new AvaloniaCanvas(context, new Size(measure.Width, measure.Height));
44-
painter.Draw(canvas, CSharpMathTextAlignment.TopLeft);
46+
painter.Draw(canvas, alignment);
4547
}
4648
}
4749
public static void DrawAsPng<TContent>
4850
(this Painter<AvaloniaCanvas, TContent, AvaloniaColor> painter,
4951
System.IO.Stream target,
50-
float textPainterCanvasWidth = TextPainter.DefaultCanvasWidth) where TContent : class {
52+
float textPainterCanvasWidth = TextPainter.DefaultCanvasWidth,
53+
CSharpMathTextAlignment alignment = CSharpMathTextAlignment.TopLeft) where TContent : class {
5154
if (!(painter.Measure(textPainterCanvasWidth) is { } size)) return;
5255
using var bitmap =
5356
new RenderTargetBitmap(new PixelSize((int)size.Width, (int)size.Height));
54-
bitmap.Render(new DrawVisual<TContent>(painter, size));
57+
bitmap.Render(new DrawVisual<TContent>(painter, size, alignment));
5558
bitmap.Save(target);
5659
}
5760
}

CSharpMath.Forms.Example/CSharpMath.Forms.Example/App.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
66
namespace CSharpMath.Forms.Example {
77
public partial class App : Application {
8-
public static ObservableCollection<MathView> AllViews =
8+
public static ObservableCollection<MathView> AllMathViews =
99
new ObservableCollection<MathView>();
10+
public static ObservableCollection<TextView> AllTextViews =
11+
new ObservableCollection<TextView>();
1012
public App() => InitializeComponent();
1113
int index = -1;
1214
void Handle_ChildAdded(object sender, ElementEventArgs e) {

CSharpMath.Forms.Example/CSharpMath.Forms.Example/ExamplesPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<Label Text="Pan to view more!" HorizontalOptions="FillAndExpand"/>
1010
<Button Text="Reset pan" Clicked="Button_Clicked" HorizontalOptions="End"/>
1111
</StackLayout>
12-
<math:MathView x:Name="View" EnableTouchEvents="true"/>
12+
<math:MathView x:Name="View" EnableTouchEvents="true" EnablePanning="true"/>
1313
</StackLayout>
1414
</ContentPage>

CSharpMath.Forms.Example/CSharpMath.Forms.Example/ExamplesPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace CSharpMath.Forms.Example {
1010
public partial class ExamplesPage : ContentPage {
1111
public ExamplesPage() {
1212
InitializeComponent();
13-
App.AllViews.Add(View);
13+
App.AllMathViews.Add(View);
1414
View.FontSize = 30;
1515
View.LaTeX = latex;
1616
}

CSharpMath.Forms.Example/CSharpMath.Forms.Example/SelectPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CSharpMath.Forms.Example {
99
public partial class SelectPage : ContentPage {
1010
public SelectPage() {
1111
InitializeComponent();
12-
App.AllViews.Add(View);
12+
App.AllMathViews.Add(View);
1313
Size.ItemsSource = TryPage.FontSizes;
1414
Size.SelectedIndexChanged += (sender, e) =>
1515
View.FontSize = (float)Size.SelectedItem;

CSharpMath.Forms.Example/CSharpMath.Forms.Example/SettingsPage.xaml.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Xamarin.Forms;
77
using Xamarin.Forms.Xaml;
88
using CSharpMath.Rendering.FrontEnd;
9+
using System.Globalization;
10+
using System.Net;
911

1012
namespace CSharpMath.Forms.Example {
1113
[XamlCompilation(XamlCompilationOptions.Compile)]
@@ -16,7 +18,7 @@ public SettingsPage() {
1618
var values = typeof(Rendering.FrontEnd.TextAlignment).GetEnumValues();
1719
Array.Reverse(values);
1820
Alignment.ItemsSource = values;
19-
Alignment.SelectedItem = Rendering.FrontEnd.TextAlignment.Center;
21+
Alignment.SelectedItem = Rendering.FrontEnd.TextAlignment.Top;
2022

2123
values = typeof(PaintStyle).GetEnumValues();
2224
PaintStyle.ItemsSource = values;
@@ -31,15 +33,18 @@ public SettingsPage() {
3133
HighlightColor_Completed(this, EventArgs.Empty);
3234
BackColor_Completed(this, EventArgs.Empty);
3335

34-
App.AllViews.CollectionChanged += CollectionChanged;
35-
CollectionChanged(this, new Args(Action.Add, App.AllViews));
36+
App.AllMathViews.CollectionChanged += CollectionChanged<SkiaSharp.MathPainter, Atom.MathList>;
37+
CollectionChanged<SkiaSharp.MathPainter, Atom.MathList>(this, new Args(Action.Add, App.AllMathViews));
38+
App.AllTextViews.CollectionChanged += CollectionChanged<SkiaSharp.TextPainter, Rendering.Text.TextAtom>;
39+
CollectionChanged<SkiaSharp.TextPainter, Rendering.Text.TextAtom>(this, new Args(Action.Add, App.AllTextViews));
3640
}
3741

3842
Color Parse(string color) => global::SkiaSharp.Views.Forms.Extensions.ToFormsColor
3943
(global::SkiaSharp.SKColor.TryParse(color, out var c) ? c : global::SkiaSharp.SKColors.Black);
4044

41-
private void CollectionChanged(object sender, Args e) {
42-
if (e.NewItems != null) foreach (var v in e.NewItems.Cast<MathView>()) {
45+
private void CollectionChanged<TPainter, TContent>(object sender, Args e)
46+
where TPainter : Painter<global::SkiaSharp.SKCanvas, TContent, global::SkiaSharp.SKColor>, new() where TContent : class {
47+
if (e.NewItems != null) foreach (var v in e.NewItems.Cast<BaseView<TPainter, TContent>>()) {
4348
v.GlyphBoxColor = DrawGlyphBoxes.On ? (Parse(GlyphBoxColor.Text), Parse(GlyphRunColor.Text)) : default((Color glyph, Color textRun)?);
4449
v.TextAlignment = (Rendering.FrontEnd.TextAlignment)Alignment.SelectedItem;
4550
v.TextColor = TextColor.LabelColor;
@@ -51,43 +56,63 @@ private void CollectionChanged(object sender, Args e) {
5156
}
5257

5358
private void Alignment_SelectedIndexChanged(object sender, EventArgs e) {
54-
foreach (var v in App.AllViews) {
59+
foreach (var v in App.AllMathViews) {
60+
v.TextAlignment = (Rendering.FrontEnd.TextAlignment)Alignment.SelectedItem;
61+
}
62+
foreach (var v in App.AllTextViews) {
5563
v.TextAlignment = (Rendering.FrontEnd.TextAlignment)Alignment.SelectedItem;
5664
}
5765
}
5866

5967
private void TextColor_Completed(object sender, EventArgs e) {
6068
TextColor.LabelColor = Parse(TextColor.Text);
61-
foreach (var v in App.AllViews) {
69+
foreach (var v in App.AllMathViews) {
70+
v.TextColor = TextColor.LabelColor;
71+
}
72+
foreach (var v in App.AllTextViews) {
6273
v.TextColor = TextColor.LabelColor;
6374
}
6475
}
6576

6677
private void HighlightColor_Completed(object sender, EventArgs e) {
6778
HighlightColor.LabelColor = Parse(HighlightColor.Text);
68-
foreach (var v in App.AllViews) {
79+
foreach (var v in App.AllMathViews) {
80+
v.HighlightColor = HighlightColor.LabelColor;
81+
}
82+
foreach (var v in App.AllTextViews) {
6983
v.HighlightColor = HighlightColor.LabelColor;
7084
}
7185
}
7286

7387
private void BackColor_Completed(object sender, EventArgs e) {
7488
BackColor.LabelColor = Parse(BackColor.Text);
75-
foreach (var v in App.AllViews) {
89+
foreach (var v in App.AllMathViews) {
90+
v.BackgroundColor = BackColor.LabelColor;
91+
}
92+
foreach (var v in App.AllTextViews) {
7693
v.BackgroundColor = BackColor.LabelColor;
7794
}
7895
}
7996

8097
private void GlyphBoxColor_Completed(object sender, EventArgs e) {
8198
GlyphBoxColor.LabelColor = Parse(GlyphBoxColor.Text);
82-
foreach (var v in App.AllViews) if (v.GlyphBoxColor is { } value) {
99+
foreach (var v in App.AllMathViews) if (v.GlyphBoxColor is { } value) {
100+
value.glyph = Parse(GlyphBoxColor.Text);
101+
v.GlyphBoxColor = value;
102+
}
103+
foreach (var v in App.AllTextViews) if (v.GlyphBoxColor is { } value) {
83104
value.glyph = Parse(GlyphBoxColor.Text);
84105
v.GlyphBoxColor = value;
85106
}
86107
}
87108

88109
private void GlyphRunColor_Completed(object sender, EventArgs e) {
89110
GlyphRunColor.LabelColor = Parse(GlyphRunColor.Text);
90-
foreach (var v in App.AllViews) if (v.GlyphBoxColor is { } value) {
111+
foreach (var v in App.AllMathViews) if (v.GlyphBoxColor is { } value) {
112+
value.textRun = Parse(GlyphRunColor.Text);
113+
v.GlyphBoxColor = value;
114+
}
115+
foreach (var v in App.AllTextViews) if (v.GlyphBoxColor is { } value) {
91116
value.textRun = Parse(GlyphRunColor.Text);
92117
v.GlyphBoxColor = value;
93118
}
@@ -98,19 +123,28 @@ private void DrawGlyphBoxes_OnChanged(object sender, ToggledEventArgs e) {
98123
GlyphRunColor.IsEnabled = e.Value;
99124
GlyphBoxColor.LabelColor = Parse(GlyphBoxColor.Text);
100125
GlyphRunColor.LabelColor = Parse(GlyphRunColor.Text);
101-
foreach (var v in App.AllViews) {
126+
foreach (var v in App.AllMathViews) {
127+
v.GlyphBoxColor = e.Value ? (Parse(GlyphBoxColor.Text), Parse(GlyphRunColor.Text)) : default((Color glyph, Color textRun)?);
128+
}
129+
foreach (var v in App.AllTextViews) {
102130
v.GlyphBoxColor = e.Value ? (Parse(GlyphBoxColor.Text), Parse(GlyphRunColor.Text)) : default((Color glyph, Color textRun)?);
103131
}
104132
}
105133

106134
private void PaintStyle_SelectedIndexChanged(object sender, EventArgs e) {
107-
foreach (var v in App.AllViews) {
135+
foreach (var v in App.AllMathViews) {
136+
v.PaintStyle = (PaintStyle)PaintStyle.SelectedItem;
137+
}
138+
foreach (var v in App.AllTextViews) {
108139
v.PaintStyle = (PaintStyle)PaintStyle.SelectedItem;
109140
}
110141
}
111142

112143
private void LineStyle_SelectedIndexChanged(object sender, EventArgs e) {
113-
foreach (var v in App.AllViews) {
144+
foreach (var v in App.AllMathViews) {
145+
v.LineStyle = (Atom.LineStyle)LineStyle.SelectedItem;
146+
}
147+
foreach (var v in App.AllTextViews) {
114148
v.LineStyle = (Atom.LineStyle)LineStyle.SelectedItem;
115149
}
116150
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public partial class TextPage : ContentPage
1414
{
1515
public TextPage() {
1616
InitializeComponent();
17+
App.AllTextViews.Add(View);
1718
//Text.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.";
1819
//However, it is also not yet complete as in not being able to SeparateThisReallyLongWordWhichIsSoLongThatItSpansAcrossTheEntirePageAndWontStopEvenWhenPartOfItIsOutOfBounds.
1920
Size.SelectedItem = View.FontSize;

CSharpMath.Forms.Example/CSharpMath.Forms.Example/TryPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class TryPage : ContentPage {
1212
};
1313
public TryPage() {
1414
InitializeComponent();
15-
App.AllViews.Add(View);
15+
App.AllMathViews.Add(View);
1616
Size.SelectedItem = View.FontSize;
1717
Size.SelectedIndexChanged += (sender, e) =>
1818
View.FontSize = (float)Size.SelectedItem;

0 commit comments

Comments
 (0)