Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ASCII font glyphs using block drawing characters. #20

Merged
merged 3 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions Demos/Sharpie.Demos.Font/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (c) 2022, Alexandru Ciobanu
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System.Diagnostics.CodeAnalysis;
using Sharpie;
using Sharpie.Abstractions;
using Sharpie.Backend;

[assembly: ExcludeFromCodeCoverage]

// Create a new terminal instance with an invisible cursor.
using var terminal = new Terminal(NativeCursesProvider.Instance, new(CaretMode: CaretMode.Invisible));

// Setup the message and a number of rotating styles that will be applied for each letter of the message.
var message = "\x001 Let the ASCII fun begin! \x003";
var styles = Enumerable.Range(0, message.Length)
.Select(i => new Style
{
Attributes = VideoAttribute.None,
ColorMixture = terminal.Colors.MixColors((short) (i + 10), (short) StandardColor.Default)
})
.ToArray();

// This method draws the given string and applies color starting with a specific shift.
void DrawFunAsciiMessage(ITerminal t, string str, int colorShift)
{
var x = 0;
var y = 0;

foreach (var ch in str)
{
var gl = new AsciiGlyph((byte) ch, styles[colorShift % styles.Length]);
t.Screen.Draw(new(x, y), gl);

x += gl.Size.Width;
if (x >= t.Screen.Size.Width - gl.Size.Width)
{
x = 0;
y += gl.Size.Height;
}

colorShift++;
}
}

// A repeating timer that draws the message with different colors.
var colorShift = 0;
terminal.Repeat(t =>
{
DrawFunAsciiMessage(t, message, colorShift++);
t.Screen.Refresh();

return Task.CompletedTask;
}, 100);

// The main loop -- we need to monitor for resizes.
terminal.Run((t, e) =>
{
if (e is TerminalResizeEvent)
{
t.Screen.Clear();
}

return Task.CompletedTask;
});
32 changes: 32 additions & 0 deletions Demos/Sharpie.Demos.Font/Sharpie.Demos.Font.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>sharpie-demos-font</AssemblyName>
<LangVersion>11</LangVersion>
<IsPackable>false</IsPackable>
<Title>A Curses-based terminal manipulation library.</Title>
<Description>Demo application for Sharpie library.</Description>
<Copyright>Alexandru Ciobanu</Copyright>
<PackageProjectUrl>https://github.com/pavkam/sharpie/</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/pavkam/sharpie/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/pavkam/sharpie.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageVersion>1.0.0</PackageVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Authors>Alexandru Ciobanu</Authors>
<Company>sharpie-demos-font</Company>
<Product>sharpie-demos-font</Product>
<PackageId>sharpie-demos-font</PackageId>
</PropertyGroup>


<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion Demos/Sharpie.Demos.Snake/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,3 @@ public void Update(ISurface surface)
}
}
}

61 changes: 61 additions & 0 deletions Sharpie.Tests/AsciiGlyphTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (c) 2022, Alexandru Ciobanu
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace Sharpie.Tests;

[TestClass]
public class AsciiGlyphTests
{
private readonly Style _style1 = new() { Attributes = VideoAttribute.Bold, ColorMixture = new() { Handle = 99 } };

[TestMethod]
public void Size_Returns_4By4()
{
var glyph = new AsciiGlyph((byte) 'A', _style1);
glyph.Size.ShouldBe(new(4, 4));
}

[TestMethod]
public void DrawTo_DrawsTheExpectedGlyph()
{
var glyph = new AsciiGlyph((byte) 'A', _style1);
var contents = glyph.GetContents();

var cols = new[,]
{
{ (new('▗'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
{ (new('█'), _style1), (new(' '), _style1), (new('▀'), _style1), (new(' '), _style1) },
{ (new('▖'), _style1), (new('█'), _style1), (new('█'), _style1), (new('▀'), _style1) },
{ (new(' '), _style1), (new(' '), _style1), (new(' '), _style1), (new Rune(' '), _style1) }
};

contents.ShouldBe(cols);
}
}
1 change: 0 additions & 1 deletion Sharpie.Tests/ColorManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,3 @@ public void UnMixColors_ReturnsColors_IfCursesSucceeds()
_cursesMock.Verify(v => v.pair_content(1, out It.Ref<short>.IsAny, out It.Ref<short>.IsAny), Times.Once);
}
}

1 change: 0 additions & 1 deletion Sharpie.Tests/ColorMixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,3 @@ public void NotEqualOperator_ReturnsFalse_IfSameHandle()
Assert.IsFalse(new ColorMixture { Handle = 0 } != ColorMixture.Default);
}
}

1 change: 0 additions & 1 deletion Sharpie.Tests/CursesExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ public void Ctor_StoresTheMessage()
ex.Message.ShouldBe("message");
}
}

1 change: 0 additions & 1 deletion Sharpie.Tests/CursesInitializationExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ public void HasTheCorrectMessage()
ex.Message.ShouldBe("Failed to load or initialize the Curses library.");
}
}

1 change: 0 additions & 1 deletion Sharpie.Tests/CursesOperationExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ public void Ctor_StoresTheMessage()
ex.Message.ShouldBe("The call to operation failed: text");
}
}

Loading