wordsearch-creator/App.razor

118 lines
3.8 KiB
Plaintext

<!--
Wordsearch Creator
Copyright (C) 2023 Tesses
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using WordSearch
@using SixLabors.ImageSharp
@inject HttpClient HClient
<PageTitle>Wordsearch Creator</PageTitle>
<div class="container">
@if(Font != null)
{
<h1>Wordsearch Creator</h1>
<div class="form-floating">
<textarea @bind="Words" class="form-control" placeholder="Words" id="floatingTextarea2" style="height: 100px"></textarea>
<label for="floatingTextarea2">Words</label>
</div>
<button class="btn btn-primary" @onclick="GenerateWordsearch">Generate</button>
<br>
@if(string.IsNullOrWhiteSpace(imageUrl))
{
<br>
if(failed)
{
<div class="alert alert-danger" role="alert">
Failed to generate wordsearch within 60 seconds, try using a bigger word as your biggest or use less words or try again
</div>
}
else if(generating)
{
<div class="alert alert-info" role="alert">
Generating wordsearch
</div>
}
else {
<div class="alert alert-info" role="alert">
No wordsearch
</div>
}
}else{
<img src="@imageUrl" alt="Wordsearch">
<br>
<a class="btn btn-primary" href="@imageUrl" download="wordsearch.png">Download</a>
}
}else{
<span>Loading...</span>
}
</div>
@code {
protected override async Task OnInitializedAsync()
{
SixLabors.Fonts.FontCollection collection=new();
MemoryStream ms=new (await HClient.GetByteArrayAsync("FreeMonoBold.ttf"));
ms.Position=0;
collection.Add(ms);
Font = new SixLabors.Fonts.Font(collection.Families.First(),16);
}
bool generating{get;set;}=false;
bool failed{get;set;}=false;
string imageUrl {get;set;}="";
string Words {get;set;}="";
SixLabors.Fonts.Font? Font {get;set;}=null;
public void GenerateWordsearch()
{
if(Font ==null) return;
try {
string words = Words;
generating = true;
this.StateHasChanged();
WordSearchGenerator generator=new WordSearchGenerator();
foreach(var word in words.Replace("\r","").Split('\n'))
{
generator.AddWord(word);
}
using(var img=generator.GenerateImage(Font,new SixLabors.ImageSharp.PixelFormats.Rgb24(0,0,0),new SixLabors.ImageSharp.PixelFormats.Rgb24(255,255,255)))
{
using(var ms = new MemoryStream())
{img.SaveAsPng(ms); imageUrl=$"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";}
}
generating=false;
failed=false;
this.StateHasChanged();
} catch(Exception ex)
{
failed=true;
imageUrl = "";
_=ex;
this.StateHasChanged();
}
}
}