Skip to content

Commit

Permalink
Using SkiaSharp instead of System.Drawing. (#1931)
Browse files Browse the repository at this point in the history
* ColorFiels, ColorFieldSetting, Image, ImageResizer

* refactor

* Fix InMemoryStreamForRead.Position.

* DocumentPreviewProvider resize and watermark.

* Fix merge error.

* Inactivate the temporíry test.

* Redaction.

* Remove temporary test.

* Refactor.

* Refactor.
  • Loading branch information
kavics committed Jun 8, 2023
1 parent 41dc071 commit 0622128
Show file tree
Hide file tree
Showing 7 changed files with 539 additions and 346 deletions.
15 changes: 7 additions & 8 deletions src/ContentRepository.InMemory/InMemoryBlobProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public override void Write(byte[] buffer, int offset, int count)
public class InMemoryStreamForRead : Stream
{
private byte[] _buffer;
private long _position;

public override bool CanRead => true;
public override bool CanSeek => true;
Expand All @@ -98,18 +97,18 @@ public override long Seek(long offset, SeekOrigin origin)
switch (origin)
{
case SeekOrigin.Begin:
_position = offset;
Position = offset;
break;
case SeekOrigin.Current:
_position += offset;
Position += offset;
break;
case SeekOrigin.End:
_position -= offset;
Position -= offset;
break;
default:
throw new ArgumentOutOfRangeException(nameof(origin), origin, null);
}
return _position;
return Position;
}

public override void SetLength(long value)
Expand All @@ -119,10 +118,10 @@ public override void SetLength(long value)

public override int Read(byte[] buffer, int offset, int count)
{
var realCount = Math.Min(_buffer.LongLength - _position - offset, count);
var realCount = Math.Min(_buffer.LongLength - Position - offset, count);
if (realCount > 0)
Array.Copy(_buffer, _position, buffer, 0, realCount);
_position += realCount;
Array.Copy(_buffer, Position, buffer, 0, realCount);
Position += realCount;
return Convert.ToInt32(realCount);
}

Expand Down
252 changes: 203 additions & 49 deletions src/ContentRepository/Fields/ColorField.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SkiaSharp;
using System.Globalization;
using SenseNet.ContentRepository.Schema;
using SenseNet.ContentRepository.Schema;
using SenseNet.Diagnostics;

namespace SenseNet.ContentRepository.Fields
{
[ShortName("Color")]
[DataSlot(0, RepositoryDataType.String, typeof(string), typeof(Color))]
[DataSlot(0, RepositoryDataType.String, typeof(string), typeof(SKColor))]
[DefaultFieldSetting(typeof(ColorFieldSetting))]
[DefaultFieldControl("SenseNet.Portal.UI.Controls.ColorPicker")]
public class ColorField : Field
Expand All @@ -26,99 +25,108 @@ protected override void ImportData(System.Xml.XmlNode fieldNode, ImportContext c

protected override string GetXmlData()
{
return ColorToString((Color)GetData());
return ColorToString((SKColor)GetData());
}

protected override object ConvertTo(object[] handlerValues)
{
if (handlerValues[0] == null)
return Color.Black;
return SKColors.Black;
Type propertyType = this.GetHandlerSlot(0);
if (propertyType == typeof(Color))
if (propertyType == typeof(SKColor))
return handlerValues[0];
if (propertyType == typeof(string))
return ColorFromString((string)handlerValues[0]);
throw new NotSupportedException(String.Concat("ColorField not supports this conversion: ", propertyType.FullName, " to ", typeof(Color).FullName));
throw new NotSupportedException(String.Concat("ColorField not supports this conversion: ", propertyType.FullName, " to ", typeof(SKColor).FullName));
}
protected override object[] ConvertFrom(object value)
{
return new object[] { ConvertFromControlInner(value) };
return new[] { ConvertFromControlInner(value) };
}
private object ConvertFromControlInner(object value)
{
Type propertyType = this.GetHandlerSlot(0);
var propertyType = this.GetHandlerSlot(0);
if (value == null)
{
if (propertyType == typeof(Color))
return Color.Empty;
if (propertyType == typeof(SKColor))
return SKColor.Empty;
if (propertyType == typeof(string))
return null;
}
if (value.GetType() == typeof(string))
}
else if (value is string stringValue)
{
if (propertyType == typeof(Color))
return ColorFromString((string)value);
if (propertyType == typeof(SKColor))
return ColorFromString(stringValue);
if (propertyType == typeof(string))
return value;
}
if (value.GetType() == typeof(Color))
}
else if (value is SKColor colorValue)
{
if (propertyType == typeof(Color))
if (propertyType == typeof(SKColor))
return value;
if (propertyType == typeof(string))
return ColorToString((Color)value);
return ColorToString(colorValue);
}
throw new NotSupportedException(String.Concat("ColorField not supports this conversion: ", typeof(Color).FullName, " to ", propertyType.FullName));
throw new NotSupportedException(
$"ColorField not supports this conversion: {value?.GetType().FullName ?? "[null]"} to {propertyType.FullName}");
}

public static string ColorToString(Color color)
public static string ColorToString(SKColor color)
{
if (color == Color.Empty)
if (color == SKColor.Empty)
return "";
return String.Concat("#", color.R.ToString("X2"), color.G.ToString("X2"), color.B.ToString("X2"));
return string.Concat("#",
color.Red.ToString("X2"), color.Green.ToString("X2"), color.Blue.ToString("X2"));
}
public static Color ColorFromString(string value)
public static SKColor ColorFromString(string value)
{
if (String.IsNullOrEmpty(value))
return Color.Empty;
return SKColor.Empty;

if (value.StartsWith("#"))
{
try
{
return Color.FromArgb(
Byte.Parse(value.Substring(1, 2), NumberStyles.HexNumber),
Byte.Parse(value.Substring(3, 2), NumberStyles.HexNumber),
Byte.Parse(value.Substring(5, 2), NumberStyles.HexNumber));
}
catch(Exception e) // logged
{
if (value.StartsWith("#"))
{
try
{
if (value.Length == 7)
{
return new SKColor(byte.Parse(value.Substring(1, 2), NumberStyles.HexNumber),
byte.Parse(value.Substring(3, 2), NumberStyles.HexNumber),
byte.Parse(value.Substring(5, 2), NumberStyles.HexNumber));
}
var r = char.ToString(value[1]);
var g = char.ToString(value[2]);
var b = char.ToString(value[3]);
return new SKColor(byte.Parse(r + r, NumberStyles.HexNumber),
byte.Parse(g + g, NumberStyles.HexNumber),
byte.Parse(b + b, NumberStyles.HexNumber));
}
catch (Exception e) // logged
{
SnLog.WriteException(e);
return Color.Empty;
}
}
return SKColor.Empty;
}
}

var colorString = value;
// "Color [Red]"
colorString = colorString.Replace("Color [", "").Replace("]", "");
var colorString = value.Replace("Color [", "").Replace("]", "");
if (colorString == "Empty")
return Color.Empty;
return SKColor.Empty;

try
{
return ColorTranslator.FromHtml(colorString);
}
{
return !ColorTable.TryGetValue(colorString, out var colorCode) ? SKColor.Empty : SKColor.Parse(colorCode);
}
catch (Exception e) // logged
{
SnLog.WriteException(e);
return Color.Empty;
return SKColor.Empty;
}
}

protected override bool ParseValue(string value)
{
var colorValue = ColorFromString(value);
if (colorValue == Color.Empty)
if (colorValue == SKColor.Empty)
return false;
this.SetData(colorValue);
return true;
Expand All @@ -128,5 +136,151 @@ protected override void WriteXmlData(System.Xml.XmlWriter writer)
{
ExportData(writer, null);
}
}

private static readonly Dictionary<string, string> ColorTable = new()
{
{"transparent", "00FFFFFF"},
{"aliceblue", "FFF0F8FF"},
{"antiquewhite", "FFFAEBD7"},
{"aqua", "FF00FFFF"},
{"aquamarine", "FF7FFFD4"},
{"azure", "FFF0FFFF"},
{"beige", "FFF5F5DC"},
{"bisque", "FFFFE4C4"},
{"black", "FF000000"},
{"blanchedalmond", "FFFFEBCD"},
{"blue", "FF0000FF"},
{"blueviolet", "FF8A2BE2"},
{"brown", "FFA52A2A"},
{"burlywood", "FFDEB887"},
{"cadetblue", "FF5F9EA0"},
{"chartreuse", "FF7FFF00"},
{"chocolate", "FFD2691E"},
{"coral", "FFFF7F50"},
{"cornflowerblue", "FF6495ED"},
{"cornsilk", "FFFFF8DC"},
{"crimson", "FFDC143C"},
{"cyan", "FF00FFFF"},
{"darkblue", "FF00008B"},
{"darkcyan", "FF008B8B"},
{"darkgoldenrod", "FFB8860B"},
{"darkgray", "FFA9A9A9"},
{"darkgreen", "FF006400"},
{"darkkhaki", "FFBDB76B"},
{"darkmagenta", "FF8B008B"},
{"darkolivegreen", "FF556B2F"},
{"darkorange", "FFFF8C00"},
{"darkorchid", "FF9932CC"},
{"darkred", "FF8B0000"},
{"darksalmon", "FFE9967A"},
{"darkseagreen", "FF8FBC8F"},
{"darkslateblue", "FF483D8B"},
{"darkslategray", "FF2F4F4F"},
{"darkturquoise", "FF00CED1"},
{"darkviolet", "FF9400D3"},
{"deeppink", "FFFF1493"},
{"deepskyblue", "FF00BFFF"},
{"dimgray", "FF696969"},
{"dodgerblue", "FF1E90FF"},
{"firebrick", "FFB22222"},
{"floralwhite", "FFFFFAF0"},
{"forestgreen", "FF228B22"},
{"fuchsia", "FFFF00FF"},
{"gainsboro", "FFDCDCDC"},
{"ghostwhite", "FFF8F8FF"},
{"gold", "FFFFD700"},
{"goldenrod", "FFDAA520"},
{"gray", "FF808080"},
{"green", "FF008000"},
{"greenyellow", "FFADFF2F"},
{"honeydew", "FFF0FFF0"},
{"hotpink", "FFFF69B4"},
{"indianred", "FFCD5C5C"},
{"indigo", "FF4B0082"},
{"ivory", "FFFFFFF0"},
{"khaki", "FFF0E68C"},
{"lavender", "FFE6E6FA"},
{"lavenderblush", "FFFFF0F5"},
{"lawngreen", "FF7CFC00"},
{"lemonchiffon", "FFFFFACD"},
{"lightblue", "FFADD8E6"},
{"lightcoral", "FFF08080"},
{"lightcyan", "FFE0FFFF"},
{"lightgoldenrodyellow", "FFFAFAD2"},
{"lightgray", "FFD3D3D3"},
{"lightgreen", "FF90EE90"},
{"lightpink", "FFFFB6C1"},
{"lightsalmon", "FFFFA07A"},
{"lightseagreen", "FF20B2AA"},
{"lightskyblue", "FF87CEFA"},
{"lightslategray", "FF778899"},
{"lightsteelblue", "FFB0C4DE"},
{"lightyellow", "FFFFFFE0"},
{"lime", "FF00FF00"},
{"limegreen", "FF32CD32"},
{"linen", "FFFAF0E6"},
{"magenta", "FFFF00FF"},
{"maroon", "FF800000"},
{"mediumaquamarine", "FF66CDAA"},
{"mediumblue", "FF0000CD"},
{"mediumorchid", "FFBA55D3"},
{"mediumpurple", "FF9370DB"},
{"mediumseagreen", "FF3CB371"},
{"mediumslateblue", "FF7B68EE"},
{"mediumspringgreen", "FF00FA9A"},
{"mediumturquoise", "FF48D1CC"},
{"mediumvioletred", "FFC71585"},
{"midnightblue", "FF191970"},
{"mintcream", "FFF5FFFA"},
{"mistyrose", "FFFFE4E1"},
{"moccasin", "FFFFE4B5"},
{"navajowhite", "FFFFDEAD"},
{"navy", "FF000080"},
{"oldlace", "FFFDF5E6"},
{"olive", "FF808000"},
{"olivedrab", "FF6B8E23"},
{"orange", "FFFFA500"},
{"orangered", "FFFF4500"},
{"orchid", "FFDA70D6"},
{"palegoldenrod", "FFEEE8AA"},
{"palegreen", "FF98FB98"},
{"paleturquoise", "FFAFEEEE"},
{"palevioletred", "FFDB7093"},
{"papayawhip", "FFFFEFD5"},
{"peachpuff", "FFFFDAB9"},
{"peru", "FFCD853F"},
{"pink", "FFFFC0CB"},
{"plum", "FFDDA0DD"},
{"powderblue", "FFB0E0E6"},
{"purple", "FF800080"},
{"red", "FFFF0000"},
{"rosybrown", "FFBC8F8F"},
{"royalblue", "FF4169E1"},
{"saddlebrown", "FF8B4513"},
{"salmon", "FFFA8072"},
{"sandybrown", "FFF4A460"},
{"seagreen", "FF2E8B57"},
{"seashell", "FFFFF5EE"},
{"sienna", "FFA0522D"},
{"silver", "FFC0C0C0"},
{"skyblue", "FF87CEEB"},
{"slateblue", "FF6A5ACD"},
{"slategray", "FF708090"},
{"snow", "FFFFFAFA"},
{"springgreen", "FF00FF7F"},
{"steelblue", "FF4682B4"},
{"tan", "FFD2B48C"},
{"teal", "FF008080"},
{"thistle", "FFD8BFD8"},
{"tomato", "FFFF6347"},
{"turquoise", "FF40E0D0"},
{"violet", "FFEE82EE"},
{"wheat", "FFF5DEB3"},
{"white", "FFFFFFFF"},
{"whitesmoke", "FFF5F5F5"},
{"yellow", "FFFFFF00"},
{"yellowgreen", "FF9ACD32"},
{"rebeccapurple", "FF663399"},
};
}
}
Loading

0 comments on commit 0622128

Please sign in to comment.