Implemented normal text element.
Added `require` to load external script.
This commit is contained in:
@@ -5,7 +5,7 @@ using SkiaSharp;
|
||||
|
||||
namespace Progrart.Core.Graphics
|
||||
{
|
||||
public class Circle : BaseElement
|
||||
public class Circle : BaseElement
|
||||
{
|
||||
|
||||
float StrokeWidth;
|
||||
|
||||
105
Progrart.Core/Graphics/Text.cs
Normal file
105
Progrart.Core/Graphics/Text.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using Jint;
|
||||
using Jint.Native;
|
||||
using Progrart.Core.JSExecution;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Progrart.Core.Graphics
|
||||
{
|
||||
public class Text : BaseElement
|
||||
{
|
||||
|
||||
float StrokeWidth;
|
||||
SKPoint Position;
|
||||
float Size;
|
||||
SKColorF Color;
|
||||
string str = "";
|
||||
bool IsStroke;
|
||||
SKShader? shader = null;
|
||||
string? fontFamily = null;
|
||||
public override void SetupProperties(Engine engine)
|
||||
{
|
||||
base.SetupProperties(engine);
|
||||
if (__object != null)
|
||||
{
|
||||
{
|
||||
JsObject point = new JsObject(engine);
|
||||
point.Set("x", 0);
|
||||
point.Set("y", 0);
|
||||
__object.Set("Position", point);
|
||||
}
|
||||
__object.Set("StrokeWidth", 1);
|
||||
__object.Set("Size", 1);
|
||||
__object.Set("Text", "");
|
||||
__object.Set("IsStroke", true);
|
||||
__object.Set("Color", ProgrartFunctions.color(engine, 1, 1, 1, 1));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public override void LoadProperties()
|
||||
{
|
||||
if (__object is not null)
|
||||
{
|
||||
StrokeWidth = (float)__object.Get("StrokeWidth").AsNumber();
|
||||
str = __object.Get("Text").AsString();
|
||||
Size = (float)__object.Get("Size").AsNumber();
|
||||
IsStroke = (bool)__object.Get("IsStroke").AsBoolean();
|
||||
{
|
||||
if (__object.Get("Position") is JsObject Start)
|
||||
{
|
||||
this.Position = ProgrartConversion.ObtainSKPointFromJsObject(Start);
|
||||
}
|
||||
}
|
||||
{
|
||||
if (__object.Get("Color") is JsObject Color)
|
||||
{
|
||||
this.Color = ProgrartConversion.ObtainSKColorFFromJsObject(Color);
|
||||
}
|
||||
}
|
||||
|
||||
if (__object.Get("Shader") is JsObject shaderObj)
|
||||
shader = ProgrartConversion.ObtainFromJsObject(shaderObj);
|
||||
if (__object.Get("Font") is JsString fontObj)
|
||||
fontFamily = fontObj.AsString();
|
||||
}
|
||||
}
|
||||
public override void Render(RenderContext context)
|
||||
{
|
||||
base.Render(context);
|
||||
LoadProperties();
|
||||
SKPoint pos = context.TranslatePoint(Position);
|
||||
float Size = context.TranslateSize(this.Size);
|
||||
if (fontFamily is not null)
|
||||
{
|
||||
var face = context.GetFont(fontFamily);
|
||||
if (face != null)
|
||||
{
|
||||
|
||||
context.canvas.DrawText(str,
|
||||
pos.X, pos.Y, SKTextAlign.Center, new SKFont(face, Size) { },
|
||||
new SKPaint()
|
||||
{
|
||||
ColorF = Color,
|
||||
StrokeWidth = context.TranslateSize(StrokeWidth),
|
||||
Shader = shader,
|
||||
IsStroke = IsStroke,
|
||||
IsAntialias = true
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
context.canvas.DrawText(str,
|
||||
pos.X, pos.Y, SKTextAlign.Center, new SKFont(SKTypeface.Default, Size) { },
|
||||
new SKPaint()
|
||||
{
|
||||
ColorF = Color,
|
||||
StrokeWidth = context.TranslateSize(StrokeWidth),
|
||||
Shader = shader,
|
||||
IsStroke = IsStroke,
|
||||
IsAntialias = true
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ namespace Progrart.Core.Graphics
|
||||
float rotate;
|
||||
void Transform(RenderContext context)
|
||||
{
|
||||
Trace.WriteLine($"Visual Root: Rotation:{rotate}");
|
||||
//Trace.WriteLine($"Visual Root: Rotation:{rotate}");
|
||||
context.canvas.Translate(tx, ty);
|
||||
context.canvas.RotateDegrees(rotate, context.DrawingCore.Width / 2, context.DrawingCore.Height / 2);
|
||||
if (scale != 1)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Jint;
|
||||
using Jint.Native;
|
||||
using Jint.Runtime.Modules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -43,6 +44,7 @@ namespace Progrart.Core.JSExecution
|
||||
JsObject _obj = new JsObject(Engine);
|
||||
Engine.SetValue("math", _obj);
|
||||
Random r = new Random();
|
||||
|
||||
_obj.Set("random", JsObject.FromObject(Engine, new Func<double>(() =>
|
||||
{
|
||||
return r.NextDouble();
|
||||
|
||||
@@ -29,6 +29,26 @@ namespace Progrart.Core.JSExecution
|
||||
else
|
||||
Trace.WriteLine(v);
|
||||
}));
|
||||
engine.Engine.SetValue("require", (string str) =>
|
||||
{
|
||||
require(str, () =>
|
||||
{
|
||||
if (str.IndexOf(".") < 0)
|
||||
{
|
||||
require(str + ".progrart", () =>
|
||||
{
|
||||
require(str + ".js", () =>
|
||||
{
|
||||
throw new Exception("Module not found: " + str);
|
||||
});
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Module not found: " + str);
|
||||
}
|
||||
});
|
||||
});
|
||||
engine.Engine.SetValue("visual_root", visual_root);
|
||||
engine.Engine.SetValue("line", line);
|
||||
engine.Engine.SetValue("rectangle", rectangle);
|
||||
@@ -40,10 +60,26 @@ namespace Progrart.Core.JSExecution
|
||||
engine.Engine.SetValue("circle", circle);
|
||||
engine.Engine.SetValue("color4", color4);
|
||||
engine.Engine.SetValue("color3", color3);
|
||||
engine.Engine.SetValue("text", text);
|
||||
engine.Engine.SetValue("color_hex", color_hex);
|
||||
engine.Engine.SetValue("linear_gradient", linear_gradient);
|
||||
engine.Engine.SetValue("radial_gradient", radial_gradient);
|
||||
}
|
||||
|
||||
private void require(string str, Action onNoFound)
|
||||
{
|
||||
var task = StorageProvider.TryOpenRead(str);
|
||||
task.Wait();
|
||||
var stream = task.GetAwaiter().GetResult();
|
||||
if (stream is null)
|
||||
{
|
||||
onNoFound();
|
||||
return;
|
||||
}
|
||||
using var sr = new StreamReader(stream);
|
||||
engine.Engine.Execute(sr.ReadToEnd());
|
||||
}
|
||||
|
||||
public JsObject color4(JsNumber r, JsNumber g, JsNumber b, JsNumber a)
|
||||
{
|
||||
return ProgrartFunctions.color(engine.Engine
|
||||
@@ -93,6 +129,7 @@ namespace Progrart.Core.JSExecution
|
||||
public JsObject circle() => ProgrartFunctions.CreateElement<Circle>(this);
|
||||
public JsObject oval() => ProgrartFunctions.CreateElement<Oval>(this);
|
||||
public JsObject triangle() => ProgrartFunctions.CreateElement<Triangle>(this);
|
||||
public JsObject text() => ProgrartFunctions.CreateElement<Text>(this);
|
||||
|
||||
public RenderContext RenderImage(int Scale, string script, ExecuteArguments arguments)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Diagnostics;
|
||||
|
||||
namespace Progrart.Core
|
||||
{
|
||||
public class PrimitiveDrawingCore : IDisposable
|
||||
public class PrimitiveDrawingCore : IDisposable
|
||||
{
|
||||
internal bool isDisposed = false;
|
||||
SKSurface surface;
|
||||
@@ -13,6 +13,26 @@ namespace Progrart.Core
|
||||
public readonly int Height;
|
||||
public SKCanvas canvas { get; }
|
||||
internal IStorageProvider StorageProvider;
|
||||
Dictionary<string, SKTypeface> Fonts = new Dictionary<string, SKTypeface>();
|
||||
public SKTypeface? GetFont(string fontName)
|
||||
{
|
||||
if (Fonts.ContainsKey(fontName))
|
||||
return Fonts[fontName];
|
||||
else
|
||||
{
|
||||
var task=StorageProvider.TryOpenRead(fontName);
|
||||
task.Wait();
|
||||
var stream=task.Result;
|
||||
if(stream != null)
|
||||
{
|
||||
|
||||
SKTypeface typeface = SKTypeface.FromStream(stream);
|
||||
Fonts[fontName] = typeface;
|
||||
return typeface;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public PrimitiveDrawingCore(int W, int H, IStorageProvider storageProvider)
|
||||
{
|
||||
Width = W;
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace Progrart.Core
|
||||
{
|
||||
this.DrawingCore = core;
|
||||
}
|
||||
public SKTypeface? GetFont(string fontName)
|
||||
{
|
||||
return DrawingCore.GetFont(fontName);
|
||||
}
|
||||
public SKPoint TranslatePoint(float x, float y)
|
||||
{
|
||||
return new SKPoint(x * DrawingCore.Width, y * DrawingCore.Height);
|
||||
|
||||
Reference in New Issue
Block a user