Skip to content

Commit fdbc0d5

Browse files
committed
#209: Android text clipping and positioning
1 parent 4fae527 commit fdbc0d5

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

Source/Examples/ExampleLibrary/Examples/RenderingCapabilities.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ExampleLibrary
1919
/// <summary>
2020
/// Provides rendering capability examples.
2121
/// </summary>
22-
[Examples("Rendering capabilities")]
22+
[Examples("9 Rendering capabilities")]
2323
public class RenderingCapabilities
2424
{
2525
/// <summary>
@@ -187,14 +187,24 @@ public static PlotModel DrawTextMaxSize()
187187
const double FontWeight = FontWeights.Bold;
188188
const double D = FontSize * 1.6;
189189
const double X = 20;
190-
double y = 20 - D;
191-
var testStrings = new []{"iii","jjj","OxyPlot","Bottom","100","KML"};
192-
foreach (var text in testStrings){
190+
const double X2 = 200;
191+
double y = 20;
192+
var testStrings = new[] { "iii", "jjj", "OxyPlot", "Bottom", "100", "KML" };
193+
foreach (var text in testStrings)
194+
{
193195
var maxSize = rc.MeasureText(text, Font, FontSize, FontWeight);
194-
var p=new ScreenPoint(X, y += D);
196+
var p = new ScreenPoint(X, y);
195197
rc.DrawText(p, text, OxyColors.Black, Font, FontSize, FontWeight, maxSize: maxSize);
196-
var rect=new OxyRect(p,maxSize);
197-
rc.DrawRectangle(rect,OxyColors.Undefined,OxyColors.Black);
198+
var rect = new OxyRect(p, maxSize);
199+
rc.DrawRectangle(rect, OxyColors.Undefined, OxyColors.Black);
200+
201+
var p2 = new ScreenPoint(X2, y);
202+
var maxSize2 = new OxySize(maxSize.Width / 2, maxSize.Height / 2);
203+
rc.DrawText(p2, text, OxyColors.Black, Font, FontSize, FontWeight, maxSize: maxSize2);
204+
var rect2 = new OxyRect(p2, maxSize2);
205+
rc.DrawRectangle(rect2, OxyColors.Undefined, OxyColors.Black);
206+
207+
y += D;
198208
}
199209
}));
200210
return model;
@@ -227,7 +237,7 @@ public static PlotModel DrawTextWithWinFormsMetricsDefault()
227237
[Example("DrawText - WinForms metrics (StringFormat = GenericTypographic)")]
228238
public static PlotModel DrawTextWithWinFormsMetricsTypographic()
229239
{
230-
return DrawTextWithMetrics("OxyPlot", "Arial", 60, 224.1, 71.5, 108, 73, 61, 121, 23,242, "WinForms");
240+
return DrawTextWithMetrics("OxyPlot", "Arial", 60, 224.1, 71.5, 108, 73, 61, 121, 23, 242, "WinForms");
231241
}
232242

233243
/// <summary>

Source/Examples/ExampleLibrary/Showcases/ShowCases.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ExampleLibrary
1818
/// <summary>
1919
/// Showcase models
2020
/// </summary>
21-
[Examples("_ShowCases_")]
21+
[Examples("1 ShowCases")]
2222
[Tags("Showcase")]
2323
public class ShowCases
2424
{

Source/OxyPlot.XamarinAndroid/CanvasRenderContext.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
272272
{
273273
this.paint.Reset();
274274
{
275-
this.paint.AntiAlias = true;
276275
this.paint.TextSize = this.Convert(fontSize);
277-
this.paint.Color = fill.ToColor();
276+
this.SetFill(fill);
278277

279278
float width;
280279
float height;
@@ -283,7 +282,7 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
283282
if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom)
284283
{
285284
this.paint.GetTextBounds(text, 0, text.Length, this.bounds);
286-
width = this.bounds.Width();
285+
width = this.bounds.Left + this.bounds.Width();
287286
height = lineHeight;
288287
}
289288
else
@@ -301,23 +300,28 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
301300
width = maxWidth;
302301
}
303302

304-
if (height > maxSize.Value.Height)
303+
if (height > maxHeight)
305304
{
306305
height = maxHeight;
307306
}
308307
}
309308

310309
var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width);
311310
var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height);
311+
var x0 = -this.bounds.Left;
312+
var y0 = delta;
312313

313314
this.canvas.Save();
314315
this.canvas.Translate(this.Convert(p.X), this.Convert(p.Y));
315316
this.canvas.Rotate((float)rotate);
316-
this.canvas.Translate((float)dx, (float)dy + delta);
317+
this.canvas.Translate((float)dx + x0, (float)dy + y0);
317318

318319
if (maxSize.HasValue)
319320
{
320-
this.canvas.ClipRect(0, -height, this.Convert(maxSize.Value.Width), 0);
321+
var x1 = -x0;
322+
var y1 = -height - y0;
323+
this.canvas.ClipRect(x1, y1, x1 + width, y1 + height);
324+
this.canvas.Translate(0, lineHeight - height);
321325
}
322326

323327
this.canvas.DrawText(text, 0, 0, this.paint);
@@ -438,7 +442,7 @@ public override void CleanUp()
438442
/// <param name="delta">The vertical delta.</param>
439443
private void GetFontMetrics(Paint paint, out float defaultLineHeight, out float delta)
440444
{
441-
var metrics = paint.GetFontMetrics ();
445+
var metrics = paint.GetFontMetrics();
442446
var ascent = -metrics.Ascent;
443447
var descent = metrics.Descent;
444448
var leading = metrics.Leading;

0 commit comments

Comments
 (0)