You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
13 KiB
C#

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace GeoSigmaDrawLib
{
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
public struct LogFont : IXmlSerializable
{
[XmlAttribute("H")]
public int lfHeight { get; set; }
[XmlAttribute("W")]
public int lfWidth { get; set; }
[XmlIgnore]
public int lfEscapement { get; set; }
[XmlAttribute("O")]
public int lfOrientation { get; set; }
[XmlAttribute("B")]
public int lfWeight;
// [XmlAttribute("B")]
// public bool lfBold;
[XmlAttribute("I")]
[MarshalAs(UnmanagedType.U1)]
public bool lfItalic;
[XmlAttribute("U")]
[MarshalAs(UnmanagedType.U1)]
public bool lfUnderline;
[XmlAttribute("S")]
[MarshalAs(UnmanagedType.U1)]
public bool lfStrikeOut;
[XmlAttribute("CS")]
public byte lfCharSet;
[XmlAttribute("OP")]
public byte lfOutPrecision;
[XmlAttribute("CP")]
public byte lfClipPrecision;
[XmlAttribute("Q")]
public byte lfQuality;
[XmlAttribute("PF")]
public byte lfPitchAndFamily;
[XmlAttribute("FN")]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("LOGFONT\n");
sb.AppendFormat(" lfHeight: {0}\n", lfHeight);
sb.AppendFormat(" lfWidth: {0}\n", lfWidth);
sb.AppendFormat(" lfEscapement: {0}\n", lfEscapement);
sb.AppendFormat(" lfOrientation: {0}\n", lfOrientation);
sb.AppendFormat(" lfWeight: {0}\n", lfWeight);
// sb.AppendFormat(" lfBold: {0}\n", lfBold);
sb.AppendFormat(" lfItalic: {0}\n", lfItalic ? 1 : 0);
sb.AppendFormat(" lfUnderline: {0}\n", lfUnderline);
sb.AppendFormat(" lfStrikeOut: {0}\n", lfStrikeOut);
sb.AppendFormat(" lfCharSet: {0}\n", lfCharSet);
sb.AppendFormat(" lfOutPrecision: {0}\n", lfOutPrecision);
sb.AppendFormat(" lfClipPrecision: {0}\n", lfClipPrecision);
sb.AppendFormat(" lfQuality: {0}\n", lfQuality);
sb.AppendFormat(" lfPitchAndFamily: {0}\n", lfPitchAndFamily);
sb.AppendFormat(" lfFaceName: {0}\n", lfFaceName);
return sb.ToString();
}
/// <summary>
/// 转换为PCG
/// </summary>
/// <returns></returns>
public string ToPcgString()
{
return string.Format(
@"B=""{0}"" I=""{1}"" U=""{2}"" S=""{3}"" CS=""{4}"" OP=""{5}"" CP=""{6}"" Q=""{7}"" PF=""{8}"" FN=""{9}"" H=""{10}"" W=""{11}"" O=""{12}""",
lfWeight,
lfItalic ? 1 : 0,
lfUnderline ? 1 : 0,
lfStrikeOut ? 1 : 0,
lfCharSet,
lfOutPrecision,
lfClipPrecision,
lfQuality,
lfPitchAndFamily,
lfFaceName,
lfHeight,
lfWidth,
lfOrientation);
}
[DllImport("gdi32", EntryPoint = "CreateFontW", CharSet = CharSet.Auto)]
static extern IntPtr CreateFontW(
[In] Int32 nHeight,
[In] Int32 nWidth,
[In] Int32 nEscapement,
[In] Int32 nOrientation,
[In] FontWeight fnWeight,
[In] Boolean fdwItalic,
[In] Boolean fdwUnderline,
[In] Boolean fdwStrikeOut,
[In] FontCharSet fdwCharSet,
[In] FontPrecision fdwOutputPrecision,
[In] FontClipPrecision fdwClipPrecision,
[In] FontQuality fdwQuality,
[In] FontPitchAndFamily fdwPitchAndFamily,
[In] String lpszFace);
/// <inheritdoc/>
public XmlSchema GetSchema()
{
return null;
}
/// <inheritdoc/>
public void ReadXml(XmlReader reader)
{
reader.MoveToFirstAttribute();
int nIntValue = 0;
byte bByteValue = 0;
while (reader.MoveToNextAttribute())
{
if (reader.Name.Equals("H"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfHeight = nIntValue;
}
}
else if(reader.Name.Equals("W"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfWidth = nIntValue;
}
}
else if (reader.Name.Equals("O"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfOrientation = nIntValue;
}
}
else if (reader.Name.Equals("B"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfWeight = nIntValue;
}
}
else if (reader.Name.Equals("I"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfItalic = nIntValue == 1 ? true : false;
}
}
else if (reader.Name.Equals("U"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfUnderline = nIntValue == 1 ? true : false;
}
}
else if (reader.Name.Equals("S"))
{
if (int.TryParse(reader.Value, out nIntValue))
{
lfStrikeOut = nIntValue == 1 ? true : false;
}
}
else if (reader.Name.Equals("CS"))
{
if (byte.TryParse(reader.Value, out bByteValue))
{
lfCharSet = bByteValue;
}
}
else if (reader.Name.Equals("OP"))
{
if (byte.TryParse(reader.Value, out bByteValue))
{
lfOutPrecision = bByteValue;
}
}
else if (reader.Name.Equals("CP"))
{
if (byte.TryParse(reader.Value, out bByteValue))
{
lfClipPrecision = bByteValue;
}
}
else if (reader.Name.Equals("Q"))
{
if (byte.TryParse(reader.Value, out bByteValue))
{
lfQuality = bByteValue;
}
}
else if (reader.Name.Equals("PF"))
{
if (byte.TryParse(reader.Value, out bByteValue))
{
lfPitchAndFamily = bByteValue;
}
}
else if (reader.Name.Equals("FN"))
{
lfFaceName = reader.Value;
}
}
}
/// <inheritdoc/>
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("B", $"{lfWeight}");
writer.WriteAttributeString("I", (lfItalic ? 1 : 0).ToString());
writer.WriteAttributeString("U", (lfUnderline ? 1 : 0).ToString());
writer.WriteAttributeString("S", (lfStrikeOut ? 1 : 0).ToString());
writer.WriteAttributeString("CS", $"{lfCharSet}");
writer.WriteAttributeString("OP", $"{lfOutPrecision}");
writer.WriteAttributeString("CP", $"{lfClipPrecision}");
writer.WriteAttributeString("Q", $"{lfQuality}");
writer.WriteAttributeString("PF", $"{lfPitchAndFamily}");
writer.WriteAttributeString("FN", $"{lfFaceName}");
writer.WriteAttributeString("H", $"{lfHeight}");
writer.WriteAttributeString("W", $"{lfWidth}");
writer.WriteAttributeString("O", $"{lfOrientation}");
}
public static LogFont Default { get; private set; } = LogFontConverter.convert(new Font("Times New Roman", 8));
}
public enum FontWeight : int
{
FW_DONTCARE = 0,
FW_THIN = 100,
FW_EXTRALIGHT = 200,
FW_LIGHT = 300,
FW_NORMAL = 400,
FW_MEDIUM = 500,
FW_SEMIBOLD = 600,
FW_BOLD = 700,
FW_EXTRABOLD = 800,
FW_HEAVY = 900,
}
public enum FontCharSet : byte
{
ANSI_CHARSET = 0,
DEFAULT_CHARSET = 1,
SYMBOL_CHARSET = 2,
SHIFTJIS_CHARSET = 128,
HANGEUL_CHARSET = 129,
HANGUL_CHARSET = 129,
GB2312_CHARSET = 134,
CHINESEBIG5_CHARSET = 136,
OEM_CHARSET = 255,
JOHAB_CHARSET = 130,
HEBREW_CHARSET = 177,
ARABIC_CHARSET = 178,
GREEK_CHARSET = 161,
TURKISH_CHARSET = 162,
VIETNAMESE_CHARSET = 163,
THAI_CHARSET = 222,
EASTEUROPE_CHARSET = 238,
RUSSIAN_CHARSET = 204,
MAC_CHARSET = 77,
BALTIC_CHARSET = 186,
}
public enum FontPrecision : byte
{
OUT_DEFAULT_PRECIS = 0,
OUT_STRING_PRECIS = 1,
OUT_CHARACTER_PRECIS = 2,
OUT_STROKE_PRECIS = 3,
OUT_TT_PRECIS = 4,
OUT_DEVICE_PRECIS = 5,
OUT_RASTER_PRECIS = 6,
OUT_TT_ONLY_PRECIS = 7,
OUT_OUTLINE_PRECIS = 8,
OUT_SCREEN_OUTLINE_PRECIS = 9,
OUT_PS_ONLY_PRECIS = 10,
}
public enum FontClipPrecision : byte
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2,
CLIP_MASK = 0xf,
CLIP_LH_ANGLES = (1<<4),
CLIP_TT_ALWAYS = (2<<4),
CLIP_DFA_DISABLE = (4<<4),
CLIP_EMBEDDED = (8<<4),
}
public enum FontQuality : byte
{
DEFAULT_QUALITY = 0,
DRAFT_QUALITY = 1,
PROOF_QUALITY = 2,
NONANTIALIASED_QUALITY = 3,
ANTIALIASED_QUALITY = 4,
CLEARTYPE_QUALITY = 5,
CLEARTYPE_NATURAL_QUALITY = 6,
}
[Flags]
public enum FontPitchAndFamily : byte
{
DEFAULT_PITCH = 0,
FIXED_PITCH = 1,
VARIABLE_PITCH = 2,
FF_DONTCARE = (0<<4),
FF_ROMAN = (1<<4),
FF_SWISS = (2<<4),
FF_MODERN = (3<<4),
FF_SCRIPT = (4<<4),
FF_DECORATIVE = (5<<4),
}
public class LogFontConverter
{
public static LogFont convert(Font font)
{
LogFont logFont = new LogFont();
logFont.lfCharSet = font.GdiCharSet;
logFont.lfHeight = font.Height;
logFont.lfItalic = font.Italic;
logFont.lfUnderline = font.Underline;
logFont.lfStrikeOut = font.Strikeout;
logFont.lfFaceName = font.Name;
logFont.lfWeight = font.Bold ? 800 : 0;
return logFont;
}
}
//[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
//public class LogFont
//{
// [XmlAttribute("W")]
// public int W;
// [XmlAttribute("H")]
// public int H;
// [XmlAttribute("O")]
// public int Orientation;
// [XmlAttribute("B")]
// public int Weight;
// [XmlAttribute("I")]
// public bool I;
// [XmlAttribute("U")]
// public bool U;
// [XmlAttribute("S")]
// public bool StrikeOut;
// [XmlAttribute("CS")]
// public int CharSet;
// [XmlAttribute("OP")]
// public int OP;
// [XmlAttribute("CP")]
// public int CP;
// [XmlAttribute("Q")]
// public bool Q;
// [XmlAttribute("PF")]
// public int PF;
// [XmlAttribute("FN")]
// public string FN;
// str.Format("<Font W=\"%ld\" H=\"%ld\" O=\"%ld\" B=\"%ld\" I=\"%ld\" U=\"%ld\" S=\"%ld\" CS=\"%ld\" OP=\"%ld\" CP=\"%ld\" Q=\"%ld\" PF=\"%ld\" FN=\"%s\"/>",
//lf.lfWidth, lf.lfHeight, lf.lfOrientation, lf.lfWeight,
//lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
//lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality, lf.lfPitchAndFamily,
//lf.lfFaceName);
//W="7" H="14" O="0" B="400" I="0" U="0" S="0" CS="1" OP="0" CP="16" Q="0" PF="0" FN="Times New Roman"
//}
}