123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WMFConverter.Svg
- {
- /// <summary>
- /// Scalable Vector Graphics - Represents a SVG Font object.
- /// </summary>
- public class SvgFont : SvgObject,Gdi.IGdiFont
- {
- #region Properties
- /// <summary>
- /// Object height.
- /// </summary>
- public int Height
- {
- get
- {
- return _height;
- }
- }
- /// <summary>
- /// Object width.
- /// </summary>
- public int Width
- {
- get
- {
- return _width;
- }
- }
- /// <summary>
- /// Object escapement.
- /// </summary>
- public int Escapement
- {
- get
- {
- return _escapement;
- }
- }
- /// <summary>
- /// Object orientation
- /// </summary>
- public int Orientation
- {
- get
- {
- return _orientation;
- }
- }
- /// <summary>
- /// Object weight.
- /// </summary>
- public int Weight
- {
- get
- {
- return _weight;
- }
- }
- /// <summary>
- /// Italic font.
- /// </summary>
- public bool IsItalic
- {
- get
- {
- return _italic;
- }
- }
- /// <summary>
- /// Underlined font.
- /// </summary>
- public bool IsUnderlined
- {
- get
- {
- return _underline;
- }
- }
- /// <summary>
- /// Striked font.
- /// </summary>
- public bool IsStrikedOut
- {
- get
- {
- return _strikeout;
- }
- }
- /// <summary>
- /// Object charset.
- /// </summary>
- public int Charset
- {
- get
- {
- return _charset;
- }
- }
- /// <summary>
- /// Object precision.
- /// </summary>
- public int OutPrecision
- {
- get
- {
- return _outPrecision;
- }
- }
- /// <summary>
- /// Object ClipPrecision.
- /// </summary>
- public int ClipPrecision
- {
- get
- {
- return _clipPrecision;
- }
- }
- /// <summary>
- /// Font quality.
- /// </summary>
- public int Quality
- {
- get
- {
- return _quality;
- }
- }
- /// <summary>
- /// Defines pitch and family.
- /// </summary>
- public int PitchAndFamily
- {
- get
- {
- return _pitchAndFamily;
- }
- }
- /// <summary>
- /// Specifies the font name.
- /// </summary>
- public string FaceName
- {
- get
- {
- return _faceName;
- }
- }
- /// <summary>
- /// Font language.
- /// </summary>
- public string Lang
- {
- get
- {
- return _lang;
- }
- }
-
- /// <summary>
- /// Font size.
- /// </summary>
- public int FontSize
- {
- get
- {
- return Math.Abs((int)GDI.DC.ToRelativeY(_height * _heightMultiply));
- }
- }
- #endregion
- #region Local Variables
- private int _height;
- private int _width;
- private int _escapement;
- private int _orientation;
- private int _weight;
- private bool _italic;
- private bool _underline;
- private bool _strikeout;
- private int _charset;
- private int _outPrecision;
- private int _clipPrecision;
- private int _quality;
- private int _pitchAndFamily;
- private string _faceName;
- private double _heightMultiply = 1.0;
- private string _lang;
- #endregion
- #region Constructors
- /// <summary>
- /// Default Constructor.
- /// </summary>
- /// <param name="gdi"></param>
- /// <param name="height"></param>
- /// <param name="width"></param>
- /// <param name="escapement"></param>
- /// <param name="orientation"></param>
- /// <param name="weight"></param>
- /// <param name="italic"></param>
- /// <param name="underline"></param>
- /// <param name="strikeout"></param>
- /// <param name="charset"></param>
- /// <param name="outPrecision"></param>
- /// <param name="clipPrecision"></param>
- /// <param name="quality"></param>
- /// <param name="pitchAndFamily"></param>
- /// <param name="faceName"></param>
- public SvgFont(
- SvgGdi gdi,
- int height,
- int width,
- int escapement,
- int orientation,
- int weight,
- bool italic,
- bool underline,
- bool strikeout,
- int charset,
- int outPrecision,
- int clipPrecision,
- int quality,
- int pitchAndFamily,
- byte[] faceName)
- : base (gdi)
- {
- _height = height;
- _width = width;
- _escapement = escapement;
- _orientation = orientation;
- _weight = weight;
- _italic = italic;
- _underline = underline;
- _strikeout = strikeout;
- _charset = charset;
- _outPrecision = outPrecision;
- _clipPrecision = clipPrecision;
- _quality = quality;
- _pitchAndFamily = pitchAndFamily;
- _faceName = WMFConverter.Gdi.GdiUtils.ConvertString(faceName, charset);
- // xml:lang
- _lang = WMFConverter.Gdi.GdiUtils.GetLanguage(charset);
- string emheight = gdi.GetProperty("font-emheight." + _faceName);
- if (emheight == null)
- {
- string alter = gdi.GetProperty("alternative-font." + _faceName);
- if (alter != null)
- {
- emheight = gdi.GetProperty("font-emheight." + alter);
- }
- }
- if (emheight != null)
- {
- _heightMultiply = Convert.ToDouble(emheight);
- }
- }
- #endregion
- #region Public Methods
- /// <summary>
- ///
- /// </summary>
- /// <param name="chars"></param>
- /// <param name="dx"></param>
- /// <returns></returns>
- public int[] ValidateDx(byte[] chars, int[] dx)
- {
- if (dx == null || dx.Length == 0)
- {
- return null;
- }
- int[,] area = WMFConverter.Gdi.GdiUtils.GetFirstByteArea(_charset);
- if (area == null)
- {
- return dx;
- }
- int n = 0;
- bool skip = false;
- for (int i = 0; i < chars.Length && i < dx.Length; i++)
- {
- int c = (0xFF & chars[i]);
- if (skip)
- {
- dx[n - 1] += dx[i];
- skip = false;
- continue;
- }
- for (int j = 0; j < area.Length; j++)
- {
- if (area[j,0] <= c && c <= area[j,1])
- {
- skip = true;
- break;
- }
- }
- dx[n++] = dx[i];
- }
- int[] ndx = new int[n];
- Array.Copy(dx, 0, ndx, 0, n);
- return ndx;
- }
- /// <summary>
- /// Serves as the default hash function.
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode()
- {
- int PRIME = 31;
- int result = 1;
- result = PRIME * result + _charset;
- result = PRIME * result + _clipPrecision;
- result = PRIME * result + _escapement;
- result = PRIME * result + ((_faceName == null) ? 0 : _faceName.GetHashCode());
- result = PRIME * result + _height;
- result = PRIME * result + (_italic ? 1231 : 1237);
- result = PRIME * result + _orientation;
- result = PRIME * result + _outPrecision;
- result = PRIME * result + _pitchAndFamily;
- result = PRIME * result + _quality;
- result = PRIME * result + (_strikeout ? 1231 : 1237);
- result = PRIME * result + (_underline ? 1231 : 1237);
- result = PRIME * result + _weight;
- result = PRIME * result + _width;
- return result;
- }
- /// <summary>
- /// Determines whether the specified object is equal to the current object.
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public override bool Equals(Object obj)
- {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (typeof(SvgFont) != obj.GetType())
- return false;
- SvgFont other = (SvgFont) obj;
- if (_charset != other._charset)
- return false;
- if (_clipPrecision != other._clipPrecision)
- return false;
- if (_escapement != other._escapement)
- return false;
- if (_faceName == null)
- {
- if (other._faceName != null)
- return false;
- }
- else if (!_faceName.Equals(other._faceName))
- return false;
- if (_height != other._height)
- return false;
- if (_italic != other._italic)
- return false;
- if (_orientation != other._orientation)
- return false;
- if (_outPrecision != other._outPrecision)
- return false;
- if (_pitchAndFamily != other._pitchAndFamily)
- return false;
- if (_quality != other._quality)
- return false;
- if (_strikeout != other._strikeout)
- return false;
- if (_underline != other._underline)
- return false;
- if (_weight != other._weight)
- return false;
- if (_width != other._width)
- return false;
- return true;
- }
- /// <summary>
- /// Create inner text element.
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public System.Xml.XmlText CreateTextNode(string id)
- {
- return GDI.Document.CreateTextNode("." + id + " { " + ToString() + " }\r\n");
- }
- /// <summary>
- /// Returns a string that represents the current object.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- System.Text.StringBuilder buffer = new System.Text.StringBuilder();
- // font-style
- if (_italic)
- buffer.Append("font-style: italic; ");
- // font-weight
- if (_weight != (int)Gdi.FontFWEnum.FW_DONTCARE && _weight != (int)Gdi.FontFWEnum.FW_NORMAL)
- {
- if (_weight < 100)
- _weight = 100;
- else if (_weight > 900)
- _weight = 900;
- else
- _weight = (_weight / 100) * 100;
- if (_weight == (int)Gdi.FontFWEnum.FW_BOLD)
- buffer.Append("font-weight: bold; ");
- else
- buffer.Append("font-weight: " + _weight + "; ");
- }
- int fontSize = FontSize;
- if (fontSize != 0)
- buffer.Append("font-size: ").Append(fontSize).Append("px; ");
- // font-family
- List<string> fontList = new List<string>();
- if (_faceName.Length != 0)
- {
- string fontFamily = _faceName;
- if (_faceName.ElementAt(0) == '@') fontFamily = _faceName.Substring(1);
- fontList.Add(fontFamily);
- string altfont = GDI.GetProperty("alternative-font." + fontFamily);
- if (altfont != null && altfont.Length != 0)
- {
- fontList.Add(altfont);
- }
- }
- // int pitch = pitchAndFamily & 0x00000003;
- int family = _pitchAndFamily & 0x000000F0;
- switch (family)
- {
- case (int)Gdi.FontFFEnum.FF_DECORATIVE:
- fontList.Add("fantasy");
- break;
- case (int)Gdi.FontFFEnum.FF_MODERN:
- fontList.Add("monospace");
- break;
- case (int)Gdi.FontFFEnum.FF_ROMAN:
- fontList.Add("serif");
- break;
- case (int)Gdi.FontFFEnum.FF_SCRIPT:
- fontList.Add("cursive");
- break;
- case (int)Gdi.FontFFEnum.FF_SWISS:
- fontList.Add("sans-serif");
- break;
- }
- if (fontList != null)
- if (fontList.Count()>0)
- {
- buffer.Append("font-family:");
- for (int i = 0; i < fontList.Count(); i ++ )
- {
- string font = fontList[i];
- if (font.IndexOf(" ") != -1)
- buffer.Append(" \"" + font + "\"");
- else
- buffer.Append(" " + font);
- if (i < fontList.Count())
- buffer.Append(",");
- }
- buffer.Append("; ");
- }
- // text-decoration
- if (_underline || _strikeout)
- {
- buffer.Append("text-decoration:");
- if (_underline)
- buffer.Append(" underline");
- if (_strikeout)
- buffer.Append(" overline");
- buffer.Append("; ");
- }
- if (buffer.Length > 0)
- buffer.Length = buffer.Length - 1;
- return buffer.ToString();
- }
- #endregion
- }
- }
|