Skip to content

Commit

Permalink
Incomplete font renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
dd01da0465b4542f8f8af4ecedc149ed committed Jan 14, 2021
1 parent f109a49 commit cf2f2f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
62 changes: 62 additions & 0 deletions osu!stream/Graphics/Renderers/NativeTextRendererAndroid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.IO;
using Android.Graphics;
using OpenTK;
using OpenTK.Graphics;
using osum.Helpers;

namespace osum.Graphics.Renderers
{
internal class NativeTextRendererAndroid : NativeTextRenderer
{
internal override pTexture CreateText(string text, float size, Vector2 restrictBounds, Color4 Color4, bool shadow,
bool bold, bool underline, TextAlignment alignment, bool forceAa,
out Vector2 measured,
Color4 background, Color4 border, int borderWidth, bool measureOnly, string fontFace)
{
try
{
Canvas canvas = new Canvas();

using (Bitmap bitmap = Bitmap.CreateBitmap(1024, 1024, Bitmap.Config.Argb8888))
{
canvas.SetBitmap(bitmap);

Paint paint = new Paint();

paint.SetARGB(0, 0, 0, 0);
paint.SetStyle(Paint.Style.Fill);

canvas.DrawPaint(paint);

paint.SetARGB(255, 255, 255, 255);
paint.TextSize = size;

Rect textBounds = new Rect();

paint.GetTextBounds(text, 0, text.Length, textBounds);

measured = new Vector2(textBounds.Width(), textBounds.Height());

canvas.DrawText(text, 0, textBounds.Height(), paint);

using (Bitmap resized = Bitmap.CreateBitmap(bitmap, 0, 0, textBounds.Width(), textBounds.Height()))
{
resized.SetConfig(Bitmap.Config.Argb8888);

pTexture tex = pTexture.FromRawBytes(resized.LockPixels(), resized.Width, resized.Height);
resized.UnlockPixels();
return tex;
}
}
}
catch (Exception)
{
measured = Vector2.Zero;
return null;
}
}

private static float dpiRatio;
}
}
2 changes: 1 addition & 1 deletion osu!stream/Graphics/Sprites/pText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal string Text
#if iOS
private static NativeTextRenderer TextRenderer = new NativeTextRendererIphone();
#elif ANDROID
private static NativeTextRenderer TextRenderer = new NativeTextRenderer(); // TEMP(Cyuubi): Implement an actual NativeTextRenderer for Android.
private static NativeTextRenderer TextRenderer = new NativeTextRendererAndroid();
#else
private static readonly NativeTextRenderer TextRenderer = new NativeTextRendererDesktop();
#endif
Expand Down
1 change: 1 addition & 0 deletions osu!stream/osu!stream_android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Audio\BackgroundAudioPlayerAndroid.cs" />
<Compile Include="Audio\SoundEffectPlayerBass.cs" />
<Compile Include="Audio\SoundEffectPlayerOpenAL.cs" />
<Compile Include="Graphics\Renderers\NativeTextRendererAndroid.cs" />
<Compile Include="Input\Sources\InputSourceAndroid.cs" />
<Compile Include="Input\TrackingPointAndroid.cs" />
<Compile Include="Main.cs" />
Expand Down

0 comments on commit cf2f2f5

Please sign in to comment.