VectSharp  2.2.1
A light library for C# vector graphics
Font.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using System;
19 using System.Collections.Generic;
20 using System.IO;
21 using System.Linq;
22 
23 namespace VectSharp
24 {
25  /// <summary>
26  /// Represents a typeface with a specific size.
27  /// </summary>
28  public class Font
29  {
30  /// <summary>
31  /// Determines whether text kerning is enabled. Note that, even when this is set to <see langword="false" />, text kerning will be applied on some platforms. For the best consistency, leave this set to <see langword="true"/>.
32  /// </summary>
33  public static bool EnableKerning = true;
34 
35  /// <summary>
36  /// Represents options to underline text.
37  /// </summary>
38  public class FontUnderline
39  {
40  /// <summary>
41  /// Determines whether the underline skips the parts of the glyph that would intersect with it or not.
42  /// </summary>
43  public bool SkipDescenders { get; set; }
44 
45  /// <summary>
46  /// Determines the position of the top of the underline with respect to the text baseline. Positive values are below the baseline, negative values are above it. This is expressed as a fraction of the font size.
47  /// </summary>
48  public double Position { get; set; }
49 
50  /// <summary>
51  /// Determines the thickness of the underline, expressed as a fraction of the font size.
52  /// </summary>
53  public double Thickness { get; set; }
54 
55  /// <summary>
56  /// Determines the caps at the start and end of the underline.
57  /// </summary>
58  public LineCaps LineCap { get; set; }
59 
60  /// <summary>
61  /// Determine whether the shape of the underline is slanted to follow the angle of italic fonts.
62  /// </summary>
63  public bool FollowItalicAngle { get; set; }
64 
65  /// <summary>
66  /// Create a new underline with the default settings for the specified font family.
67  /// </summary>
68  /// <param name="family">The font family to which the underline will be applied.</param>
69  internal FontUnderline(FontFamily family)
70  {
71  this.SkipDescenders = true;
72  this.LineCap = LineCaps.Butt;
73  this.FollowItalicAngle = true;
74 
75  if (family.TrueTypeFile != null)
76  {
77  this.Position = -family.TrueTypeFile.Get1000EmUnderlinePosition() / 1000.0;
78  this.Thickness = family.TrueTypeFile.Get1000EmUnderlineThickness() / 1000.0;
79  }
80  else
81  {
82  this.Position = 0.3;
83  this.Thickness = family.IsBold ? 0.15 : 0.075;
84  }
85 
86  if (double.IsNaN(this.Position))
87  {
88  this.Position = 0.3;
89  }
90 
91  if (double.IsNaN(this.Thickness))
92  {
93  this.Thickness = family.IsBold ? 0.15 : 0.075;
94  }
95  }
96  }
97 
98  /// <summary>
99  /// Represents detailed information about the metrics of a text string when drawn with a certain font.
100  /// </summary>
101  public class DetailedFontMetrics
102  {
103  /// <summary>
104  /// Width of the text (measured on the actual glyph outlines).
105  /// </summary>
106  public double Width { get; }
107 
108  /// <summary>
109  /// Height of the text (measured on the actual glyph outlines).
110  /// </summary>
111  public double Height { get; }
112 
113  /// <summary>
114  /// How much the leftmost glyph in the string overhangs the glyph origin on the left. Positive for glyphs that hang past the origin (e.g. italic 'f').
115  /// </summary>
116  public double LeftSideBearing { get; }
117 
118  /// <summary>
119  /// How much the rightmost glyph in the string overhangs the glyph end on the right. Positive for glyphs that hang past the end (e.g. italic 'f').
120  /// </summary>
121  public double RightSideBearing { get; }
122 
123  /// <summary>
124  /// Height of the tallest glyph in the string over the baseline. Always &gt;= 0.
125  /// </summary>
126  public double Top { get; }
127 
128  /// <summary>
129  /// Depth of the deepest glyph in the string below the baseline. Always &lt;= 0.
130  /// </summary>
131  public double Bottom { get; }
132 
133  /// <summary>
134  /// Advance width of the text (excluding any left- or right- side bearing).
135  /// </summary>
136  public double AdvanceWidth { get; }
137 
138  internal DetailedFontMetrics(double width, double height, double leftSideBearing, double rightSideBearing, double top, double bottom, double advanceWidth)
139  {
140  this.Width = width;
141  this.Height = height;
142  this.LeftSideBearing = leftSideBearing;
143  this.RightSideBearing = rightSideBearing;
144  this.Top = top;
145  this.Bottom = bottom;
146  this.AdvanceWidth = advanceWidth;
147  }
148  }
149 
150  /// <summary>
151  /// Font size, in graphics units.
152  /// </summary>
153  public double FontSize { get; }
154 
155  /// <summary>
156  /// Font typeface.
157  /// </summary>
158  public FontFamily FontFamily { get; }
159 
160  /// <summary>
161  /// Create a new Font object, given the base typeface and the font size.
162  /// </summary>
163  /// <param name="fontFamily">Base typeface. See <see cref="FontFamily"/>.</param>
164  /// <param name="fontSize">The font size, in graphics units.</param>
165  public Font(FontFamily fontFamily, double fontSize)
166  {
167  this.FontFamily = fontFamily;
168  this.FontSize = fontSize;
169  }
170 
171  /// <summary>
172  /// Create a new Font object, given the base typeface, the font size, and a boolean value determining whether text using this font should be underlined.
173  /// </summary>
174  /// <param name="fontFamily">Base typeface. See <see cref="FontFamily"/>.</param>
175  /// <param name="fontSize">The font size, in graphics units.</param>
176  /// <param name="underlined">A boolean value determining whether text drawn using this font should be underlined. The appearance of the underline can be tweaked by changing the properties of the <see cref="Underline"/> property after the font has been created.</param>
177  public Font(FontFamily fontFamily, double fontSize, bool underlined)
178  {
179  this.FontFamily = fontFamily;
180  this.FontSize = fontSize;
181 
182  if (underlined)
183  {
184  this.Underline = new FontUnderline(fontFamily);
185  }
186  }
187 
188  /// <summary>
189  /// Create a new Font object, given the base typeface, the font size, and an object describing the underline properties of text drawn using this font.
190  /// </summary>
191  /// <param name="fontFamily">Base typeface. See <see cref="FontFamily"/>.</param>
192  /// <param name="fontSize">The font size, in graphics units.</param>
193  /// <param name="underline">A <see cref="FontUnderline"/> object describing the underline properties of text drawn using this font.</param>
194  public Font(FontFamily fontFamily, double fontSize, FontUnderline underline)
195  {
196  this.FontFamily = fontFamily;
197  this.FontSize = fontSize;
198  this.Underline = underline;
199  }
200 
201  /// <summary>
202  /// Maximum height over the baseline of the usual glyphs in the font (there may be glyphs taller than this). Always &gt;= 0.
203  /// </summary>
204  public double Ascent
205  {
206  get
207  {
208  if (this.FontFamily.TrueTypeFile == null)
209  {
210  return 0;
211  }
212  else
213  {
214  return this.FontFamily.TrueTypeFile.Get1000EmAscent() * this.FontSize / 1000;
215  }
216  }
217  }
218 
219  /// <summary>
220  /// Height above the baseline for a clipping region (Windows ascent). Always &gt;= 0.
221  /// </summary>
222  public double WinAscent
223  {
224  get
225  {
226  if (this.FontFamily.TrueTypeFile == null)
227  {
228  return 0;
229  }
230  else
231  {
232  return this.FontFamily.TrueTypeFile.Get1000EmWinAscent() * this.FontSize / 1000;
233  }
234  }
235  }
236 
237  /// <summary>
238  /// Maximum depth below the baseline of the usual glyphs in the font (there may be glyphs deeper than this). Always &lt;= 0.
239  /// </summary>
240  public double Descent
241  {
242  get
243  {
244  if (this.FontFamily.TrueTypeFile == null)
245  {
246  return 0;
247  }
248  else
249  {
250  return this.FontFamily.TrueTypeFile.Get1000EmDescent() * this.FontSize / 1000;
251  }
252  }
253  }
254 
255  /// <summary>
256  /// Absolute maximum height over the baseline of the glyphs in the font. Always &gt;= 0.
257  /// </summary>
258  public double YMax
259  {
260  get
261  {
262  if (this.FontFamily.TrueTypeFile == null)
263  {
264  return 0;
265  }
266  else
267  {
268  return this.FontFamily.TrueTypeFile.Get1000EmYMax() * this.FontSize / 1000;
269  }
270  }
271  }
272 
273  /// <summary>
274  /// Absolute maximum depth below the baseline of the glyphs in the font. Always &lt;= 0.
275  /// </summary>
276  public double YMin
277  {
278  get
279  {
280  if (this.FontFamily.TrueTypeFile == null)
281  {
282  return 0;
283  }
284  else
285  {
286  return this.FontFamily.TrueTypeFile.Get1000EmYMin() * this.FontSize / 1000;
287  }
288  }
289  }
290 
291  /// <summary>
292  /// Determines the underline style of text drawn using this font. If this is <see langword="null"/>, the text is not underlined.
293  /// </summary>
294  public FontUnderline Underline { get; }
295 
296  /// <summary>
297  /// Measure the size of a text string when typeset with this font.
298  /// </summary>
299  /// <param name="text">The string to measure.</param>
300  /// <returns>A <see cref="Size"/> object representing the width and height of the text.</returns>
301  public Size MeasureText(string text)
302  {
303  if (this.FontFamily.TrueTypeFile != null)
304  {
305  double width = 0;
306  double yMin = 0;
307  double yMax = 0;
308 
309  Point currentGlyphPlacementDelta = new Point();
310  Point currentGlyphAdvanceDelta = new Point();
311  Point nextGlyphPlacementDelta = new Point();
312  Point nextGlyphAdvanceDelta = new Point();
313 
314  for (int i = 0; i < text.Length; i++)
315  {
316  if (Font.EnableKerning && i < text.Length - 1)
317  {
318  currentGlyphPlacementDelta = nextGlyphPlacementDelta;
319  currentGlyphAdvanceDelta = nextGlyphAdvanceDelta;
320  nextGlyphAdvanceDelta = new Point();
321  nextGlyphPlacementDelta = new Point();
322 
323  TrueTypeFile.PairKerning kerning = this.FontFamily.TrueTypeFile.Get1000EmKerning(text[i], text[i + 1]);
324 
325  if (kerning != null)
326  {
327  currentGlyphPlacementDelta = new Point(currentGlyphPlacementDelta.X + kerning.Glyph1Placement.X, currentGlyphPlacementDelta.Y + kerning.Glyph1Placement.Y);
328  currentGlyphAdvanceDelta = new Point(currentGlyphAdvanceDelta.X + kerning.Glyph1Advance.X, currentGlyphAdvanceDelta.Y + kerning.Glyph1Advance.Y);
329 
330  nextGlyphPlacementDelta = new Point(nextGlyphPlacementDelta.X + kerning.Glyph2Placement.X, nextGlyphPlacementDelta.Y + kerning.Glyph2Placement.Y);
331  nextGlyphAdvanceDelta = new Point(nextGlyphAdvanceDelta.X + kerning.Glyph2Advance.X, nextGlyphAdvanceDelta.Y + kerning.Glyph2Advance.Y);
332  }
333  }
334 
335  width += (this.FontFamily.TrueTypeFile.Get1000EmGlyphWidth(text[i]) + currentGlyphAdvanceDelta.X) * this.FontSize / 1000;
336  TrueTypeFile.VerticalMetrics vMet = this.FontFamily.TrueTypeFile.Get1000EmGlyphVerticalMetrics(text[i]);
337 
338  yMin = Math.Min(yMin, (vMet.YMin + currentGlyphPlacementDelta.Y) * this.FontSize / 1000);
339  yMax = Math.Max(yMax, (vMet.YMax + currentGlyphPlacementDelta.Y) * this.FontSize / 1000);
340  }
341 
342  width -= this.FontFamily.TrueTypeFile.Get1000EmGlyphBearings(text[0]).LeftSideBearing * this.FontSize / 1000;
343  width -= this.FontFamily.TrueTypeFile.Get1000EmGlyphBearings(text[text.Length - 1]).RightSideBearing * this.FontSize / 1000;
344 
345  return new Size(width, yMax - yMin);
346  }
347  else
348  {
349  return new Size(0, 0);
350  }
351  }
352 
353  /// <summary>
354  /// Measure all the metrics of a text string when typeset with this font.
355  /// </summary>
356  /// <param name="text">The string to measure.</param>
357  /// <returns>A <see cref="DetailedFontMetrics"/> object representing the metrics of the text.</returns>
359  {
360  if (this.FontFamily.TrueTypeFile != null)
361  {
362  double width = 0;
363  double yMin = 0;
364  double yMax = 0;
365 
366  Point currentGlyphPlacementDelta = new Point();
367  Point currentGlyphAdvanceDelta = new Point();
368  Point nextGlyphPlacementDelta = new Point();
369  Point nextGlyphAdvanceDelta = new Point();
370 
371  for (int i = 0; i < text.Length; i++)
372  {
373  if (Font.EnableKerning && i < text.Length - 1)
374  {
375  currentGlyphPlacementDelta = nextGlyphPlacementDelta;
376  currentGlyphAdvanceDelta = nextGlyphAdvanceDelta;
377  nextGlyphAdvanceDelta = new Point();
378  nextGlyphPlacementDelta = new Point();
379 
380  TrueTypeFile.PairKerning kerning = this.FontFamily.TrueTypeFile.Get1000EmKerning(text[i], text[i + 1]);
381 
382  if (kerning != null)
383  {
384  currentGlyphPlacementDelta = new Point(currentGlyphPlacementDelta.X + kerning.Glyph1Placement.X, currentGlyphPlacementDelta.Y + kerning.Glyph1Placement.Y);
385  currentGlyphAdvanceDelta = new Point(currentGlyphAdvanceDelta.X + kerning.Glyph1Advance.X, currentGlyphAdvanceDelta.Y + kerning.Glyph1Advance.Y);
386 
387  nextGlyphPlacementDelta = new Point(nextGlyphPlacementDelta.X + kerning.Glyph2Placement.X, nextGlyphPlacementDelta.Y + kerning.Glyph2Placement.Y);
388  nextGlyphAdvanceDelta = new Point(nextGlyphAdvanceDelta.X + kerning.Glyph2Advance.X, nextGlyphAdvanceDelta.Y + kerning.Glyph2Advance.Y);
389  }
390  }
391 
392  width += (this.FontFamily.TrueTypeFile.Get1000EmGlyphWidth(text[i]) + currentGlyphAdvanceDelta.X) * this.FontSize / 1000;
393  TrueTypeFile.VerticalMetrics vMet = this.FontFamily.TrueTypeFile.Get1000EmGlyphVerticalMetrics(text[i]);
394 
395  yMin = Math.Min(yMin, (vMet.YMin + currentGlyphPlacementDelta.Y) * this.FontSize / 1000);
396  yMax = Math.Max(yMax, (vMet.YMax + currentGlyphPlacementDelta.Y) * this.FontSize / 1000);
397  }
398 
399  double lsb = this.FontFamily.TrueTypeFile.Get1000EmGlyphBearings(text[0]).LeftSideBearing * this.FontSize / 1000;
400  double rsb = this.FontFamily.TrueTypeFile.Get1000EmGlyphBearings(text[text.Length - 1]).RightSideBearing * this.FontSize / 1000;
401 
402  double advanceWidth = width;
403 
404  width -= lsb;
405  width -= rsb;
406 
407  return new DetailedFontMetrics(width, yMax - yMin, lsb, rsb, yMax, yMin, advanceWidth);
408  }
409  else
410  {
411  return new DetailedFontMetrics(0, 0, 0, 0, 0, 0, 0);
412  }
413  }
414  }
415 
416 
417  /// <summary>
418  /// Represents a typeface.
419  /// </summary>
420  public class FontFamily
421  {
422  /// <summary>
423  /// The default font library used to resolve font family names.
424  /// </summary>
425  public static IFontLibrary DefaultFontLibrary { get; set; } = new DefaultFontLibrary();
426 
427  /// <summary>
428  /// Create a new font family from the specified family name or true type file. If the family name or the true type file are not valid, an exception might be raised. Equivalent to DefaultFontLibrary.ResolveFontFamily.
429  /// </summary>
430  /// <param name="fontFamily">The name of the font family to create, or the path to a TTF file.</param>
431  /// <returns>If the font family name or the true type file is valid, a <see cref="FontFamily"/> object corresponding to the specified font family.</returns>
432  public static FontFamily ResolveFontFamily(string fontFamily) => DefaultFontLibrary.ResolveFontFamily(fontFamily);
433 
434 
435  /// <summary>
436  /// Create a new font family from the specified standard font family name. Equivalent to DefaultFontLibrary.ResolveFontFamily.
437  /// </summary>
438  /// <param name="standardFontFamily">The standard name of the font family.</param>
439  /// <returns>A <see cref="FontFamily"/> object corresponding to the specified font family.</returns>
440  public static FontFamily ResolveFontFamily(StandardFontFamilies standardFontFamily) => DefaultFontLibrary.ResolveFontFamily(standardFontFamily);
441 
442  /// <summary>
443  /// Create a new font family from the specified family name or true type file. If the family name or the true type file are not valid, try to instantiate the font family using
444  /// the <paramref name="fallback"/>. If none of the fallback family names or true type files are valid, an exception might be raised. Equivalent to DefaultFontLibrary.ResolveFontFamily.
445  /// </summary>
446  /// <param name="fontFamily">The name of the font family to create, or the path to a TTF file.</param>
447  /// <param name="fallback">Names of additional font families or TTF files, which will be tried if the first <paramref name="fontFamily"/> is not valid.</param>
448  /// <returns>A <see cref="FontFamily"/> object corresponding to the first of the specified font families that is valid.</returns>
449  public static FontFamily ResolveFontFamily(string fontFamily, params string[] fallback) => DefaultFontLibrary.ResolveFontFamily(fontFamily, fallback);
450 
451  /// <summary>
452  /// Create a new font family from the specified family name or true type file. If the family name or the true type file are not valid, try to instantiate the font family using
453  /// the <paramref name="fallback"/>. If none of the fallback family names or true type files are valid, instantiate a standard font family using the <paramref name="finalFallback"/>. Equivalent to DefaultFontLibrary.ResolveFontFamily.
454  /// </summary>
455  /// <param name="fontFamily">The name of the font family to create, or the path to a TTF file.</param>
456  /// <param name="fallback">Names of additional font families or TTF files, which will be tried if the first <paramref name="fontFamily"/> is not valid.</param>
457  /// <param name="finalFallback">The standard name of the font family that will be used if none of the fallback families are valid.</param>
458  /// <returns>A <see cref="FontFamily"/> object corresponding to the first of the specified font families that is valid.</returns>
459  public static FontFamily ResolveFontFamily(string fontFamily, StandardFontFamilies finalFallback, params string[] fallback) => DefaultFontLibrary.ResolveFontFamily(fontFamily, finalFallback, fallback);
460 
461  internal static object fontFamilyLock = new object();
462  internal static readonly Dictionary<string, Stream> manifestResources = new Dictionary<string, Stream>();
463 
464  internal static Stream GetManifestResourceStream(string name)
465  {
466  if (!manifestResources.ContainsKey(name))
467  {
468  manifestResources.Add(name, typeof(FontFamily).Assembly.GetManifestResourceStream(name));
469  }
470 
471  return manifestResources[name];
472  }
473 
474 
475  /// <summary>
476  /// The names of the 14 standard families that are guaranteed to be displayed correctly.
477  /// </summary>
478  public static string[] StandardFamilies = new string[] { "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic", "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique", "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique", "Symbol", "ZapfDingbats" };
479 
480  /// <summary>
481  /// The names of the resource streams pointing to the included TrueType font files for each of the standard 14 font families.
482  /// </summary>
483  public static string[] StandardFontFamilyResources = new string[]
484  {
485  "VectSharp.StandardFonts.Tinos-Regular.ttf", "VectSharp.StandardFonts.Tinos-Bold.ttf", "VectSharp.StandardFonts.Tinos-Italic.ttf", "VectSharp.StandardFonts.Tinos-BoldItalic.ttf",
486  "VectSharp.StandardFonts.Arimo-Regular.ttf", "VectSharp.StandardFonts.Arimo-Bold.ttf", "VectSharp.StandardFonts.Arimo-Italic.ttf", "VectSharp.StandardFonts.Arimo-BoldItalic.ttf",
487  "VectSharp.StandardFonts.Cousine-Regular.ttf", "VectSharp.StandardFonts.Cousine-Bold.ttf", "VectSharp.StandardFonts.Cousine-Italic.ttf", "VectSharp.StandardFonts.Cousine-BoldItalic.ttf",
488  "VectSharp.StandardFonts.SymbolNeu_GB.ttf", "VectSharp.StandardFonts.Levibats-Regular_GB.ttf"
489  };
490 
491  /// <summary>
492  /// Whether this is one of the 14 standard font families or not.
493  /// </summary>
494  public bool IsStandardFamily { get; internal set; }
495 
496  /// <summary>
497  /// The 14 standard font families.
498  /// </summary>
500  {
501  /// <summary>
502  /// Serif normal regular face.
503  /// </summary>
504  TimesRoman,
505 
506  /// <summary>
507  /// Serif bold regular face.
508  /// </summary>
509  TimesBold,
510 
511  /// <summary>
512  /// Serif normal italic face.
513  /// </summary>
514  TimesItalic,
515 
516  /// <summary>
517  /// Serif bold italic face.
518  /// </summary>
519  TimesBoldItalic,
520 
521  /// <summary>
522  /// Sans-serif normal regular face.
523  /// </summary>
524  Helvetica,
525 
526  /// <summary>
527  /// Sans-serif bold regular face.
528  /// </summary>
529  HelveticaBold,
530 
531  /// <summary>
532  /// Sans-serif normal oblique face.
533  /// </summary>
534  HelveticaOblique,
535 
536  /// <summary>
537  /// Sans-serif bold oblique face.
538  /// </summary>
539  HelveticaBoldOblique,
540 
541  /// <summary>
542  /// Monospace normal regular face.
543  /// </summary>
544  Courier,
545 
546  /// <summary>
547  /// Monospace bold regular face.
548  /// </summary>
549  CourierBold,
550 
551  /// <summary>
552  /// Monospace normal oblique face.
553  /// </summary>
554  CourierOblique,
555 
556  /// <summary>
557  /// Monospace bold oblique face.
558  /// </summary>
559  CourierBoldOblique,
560 
561  /// <summary>
562  /// Symbol font.
563  /// </summary>
564  Symbol,
565 
566  /// <summary>
567  /// Dingbat font.
568  /// </summary>
569  ZapfDingbats
570  }
571 
572  /// <summary>
573  /// Full path to the TrueType font file for this font family (or, if this is a standard font family, name of the font family).
574  /// </summary>
575  public string FileName { get; internal set; }
576 
577  /// <summary>
578  /// Name of the font family, including any variantes.
579  /// </summary>
580  public string FamilyName { get; internal set; }
581 
582  /// <summary>
583  /// Parsed TrueType font file for this font family.
584  /// See also: <seealso cref="VectSharp.TrueTypeFile"/>.
585  /// </summary>
586  public TrueTypeFile TrueTypeFile { get; }
587 
588  /// <summary>
589  /// Whether this font is bold or not. This is set based on the information included in the OS/2 table of the TrueType file.
590  /// </summary>
591  public bool IsBold { get; internal set; }
592 
593  /// <summary>
594  /// Whether this font is italic or oblique or not. This is set based on the information included in the OS/2 table of the TrueType file.
595  /// </summary>
596  public bool IsItalic { get; internal set; }
597 
598  /// <summary>
599  /// Whether this font is oblique or not. This is set based on the information included in the OS/2 table of the TrueType file.
600  /// </summary>
601  public bool IsOblique { get; internal set; }
602 
603  /// <summary>
604  /// Create a new <see cref="FontFamily"/>.
605  /// </summary>
606  /// <param name="fileName">The full path to the TrueType font file for this font family or the name of a standard font family.</param>
607  [Obsolete("Please use the FontFamily.ResolveFontFamily(string) method instead!", true)]
608  public FontFamily(string fileName)
609  {
610  lock (fontFamilyLock)
611  {
612  FontFamily resolved = DefaultFontLibrary.ResolveFontFamily(fileName);
613 
614  this.FileName = resolved.FileName;
615  this.FamilyName = resolved.FamilyName;
616  this.TrueTypeFile = resolved.TrueTypeFile;
617  this.IsOblique = resolved.IsOblique;
618  this.IsBold = resolved.IsBold;
619  this.IsItalic = resolved.IsItalic;
620  this.IsStandardFamily = resolved.IsStandardFamily;
621  }
622  }
623 
624  internal FontFamily()
625  {
626 
627  }
628 
629  /// <summary>
630  /// Create a new <see cref="FontFamily"/>.
631  /// </summary>
632  /// <param name="ttfStream">A stream containing a file in TTF format.</param>
633  public FontFamily(Stream ttfStream)
634  {
635  lock (fontFamilyLock)
636  {
637  IsStandardFamily = false;
638 
639  TrueTypeFile = TrueTypeFile.CreateTrueTypeFile(ttfStream);
640 
643  this.IsBold = TrueTypeFile.IsBold();
644  this.IsItalic = TrueTypeFile.IsItalic();
646  }
647  }
648 
649  /// <summary>
650  /// Create a new <see cref="FontFamily"/>.
651  /// </summary>
652  /// <param name="ttf">A font file in TTF format.</param>
654  {
655  lock (fontFamilyLock)
656  {
657  IsStandardFamily = false;
658 
659  TrueTypeFile = ttf;
660 
663  this.IsBold = TrueTypeFile.IsBold();
664  this.IsItalic = TrueTypeFile.IsItalic();
666  }
667  }
668 
669  /// <summary>
670  /// Create a new standard <see cref="FontFamily"/>.
671  /// </summary>
672  /// <param name="standardFontFamily">The standard font family.</param>
673  [Obsolete("Please use the FontFamily.ResolveFontFamily(StandardFontFamilies) method instead!", true)]
674  public FontFamily(StandardFontFamilies standardFontFamily)
675  {
676  lock (fontFamilyLock)
677  {
678  FontFamily resolved = DefaultFontLibrary.ResolveFontFamily(standardFontFamily);
679 
680  this.FileName = resolved.FileName;
681  this.FamilyName = resolved.FamilyName;
682  this.TrueTypeFile = resolved.TrueTypeFile;
683  this.IsOblique = resolved.IsOblique;
684  this.IsBold = resolved.IsBold;
685  this.IsItalic = resolved.IsItalic;
686  this.IsStandardFamily = resolved.IsStandardFamily;
687  }
688  }
689  }
690 
691  /// <summary>
692  /// Represents a FontFamily created from a resource stream.
693  /// </summary>
695  {
696  /// <summary>
697  /// The name of the embedded resource, which will be parsed using <code>Avalonia.Media.FontFamily.Parse(string, Uri)</code>.
698  /// </summary>
699  public string ResourceName;
700 
701  /// <summary>
702  /// Create a new <see cref="ResourceFontFamily"/> from the specified <paramref name="resourceStream"/> containing a TTF file, passing the specified <paramref name="resourceName"/> to the <code>Avalonia.Media.FontFamily.Parse(string, Uri)"</code> method.
703  /// </summary>
704  /// <param name="resourceStream">A resource stream containing a TTF file.</param>
705  /// <param name="resourceName">The name of the embedded resource, which will be parsed using <code>Avalonia.Media.FontFamily.Parse(string, Uri)</code>.</param>
706  public ResourceFontFamily(System.IO.Stream resourceStream, string resourceName) : base(resourceStream)
707  {
708  this.ResourceName = resourceName;
709  }
710  }
711 
712 }
VectSharp.TrueTypeFile
Represents a font file in TrueType format. Reference: http://stevehanov.ca/blog/?id=143,...
Definition: TrueType.cs:31
VectSharp.Font.Underline
FontUnderline Underline
Determines the underline style of text drawn using this font. If this is null, the text is not underl...
Definition: Font.cs:294
VectSharp.TrueTypeFile.Get1000EmKerning
PairKerning Get1000EmKerning(char glyph1, char glyph2)
Gets the kerning between two glyphs.
Definition: TrueType.cs:2490
VectSharp.TrueTypeFile.Get1000EmUnderlinePosition
double Get1000EmUnderlinePosition()
Computes the distance of the top of the underline from the baseline, in thousandths of em unit.
Definition: TrueType.cs:2288
VectSharp.Font.DetailedFontMetrics.Width
double Width
Width of the text (measured on the actual glyph outlines).
Definition: Font.cs:106
VectSharp.Font.Font
Font(FontFamily fontFamily, double fontSize)
Create a new Font object, given the base typeface and the font size.
Definition: Font.cs:165
VectSharp.Font.DetailedFontMetrics.LeftSideBearing
double LeftSideBearing
How much the leftmost glyph in the string overhangs the glyph origin on the left. Positive for glyphs...
Definition: Font.cs:116
VectSharp.TrueTypeFile.Get1000EmYMax
double Get1000EmYMax()
Computes the maximum height over the baseline of the font, in thousandths of em unit.
Definition: TrueType.cs:2150
VectSharp.FontFamily.FontFamily
FontFamily(StandardFontFamilies standardFontFamily)
Create a new standard FontFamily.
Definition: Font.cs:674
VectSharp.FontFamily.FontFamily
FontFamily(Stream ttfStream)
Create a new FontFamily.
Definition: Font.cs:633
VectSharp.Font.DetailedFontMetrics.RightSideBearing
double RightSideBearing
How much the rightmost glyph in the string overhangs the glyph end on the right. Positive for glyphs ...
Definition: Font.cs:121
VectSharp
Definition: Brush.cs:26
VectSharp.FontFamily.IsItalic
bool IsItalic
Whether this font is italic or oblique or not. This is set based on the information included in the O...
Definition: Font.cs:596
VectSharp.Font.FontUnderline.FollowItalicAngle
bool FollowItalicAngle
Determine whether the shape of the underline is slanted to follow the angle of italic fonts.
Definition: Font.cs:63
VectSharp.Font.MeasureTextAdvanced
DetailedFontMetrics MeasureTextAdvanced(string text)
Measure all the metrics of a text string when typeset with this font.
Definition: Font.cs:358
VectSharp.Font.WinAscent
double WinAscent
Height above the baseline for a clipping region (Windows ascent). Always >= 0.
Definition: Font.cs:223
VectSharp.Font.DetailedFontMetrics.Top
double Top
Height of the tallest glyph in the string over the baseline. Always >= 0.
Definition: Font.cs:126
VectSharp.ResourceFontFamily.ResourceName
string ResourceName
The name of the embedded resource, which will be parsed using
Definition: Font.cs:699
VectSharp.TrueTypeFile.Bearings.RightSideBearing
int RightSideBearing
The right-side bearing of the glyph.
Definition: TrueType.cs:2195
VectSharp.LineCaps
LineCaps
Represents line caps.
Definition: Enums.cs:71
VectSharp.FontFamily.DefaultFontLibrary
static IFontLibrary DefaultFontLibrary
The default font library used to resolve font family names.
Definition: Font.cs:425
VectSharp.IFontLibrary
Represents a font library with methods to create FontFamily objects from a string or from FontFamily....
Definition: FontLibrary.cs:30
VectSharp.FontFamily.IsStandardFamily
bool IsStandardFamily
Whether this is one of the 14 standard font families or not.
Definition: Font.cs:494
VectSharp.Font.YMax
double YMax
Absolute maximum height over the baseline of the glyphs in the font. Always >= 0.
Definition: Font.cs:259
VectSharp.ResourceFontFamily.ResourceFontFamily
ResourceFontFamily(System.IO.Stream resourceStream, string resourceName)
Create a new ResourceFontFamily from the specified resourceStream containing a TTF file,...
Definition: Font.cs:706
VectSharp.Font
Represents a typeface with a specific size.
Definition: Font.cs:29
VectSharp.Font.FontUnderline.Position
double Position
Determines the position of the top of the underline with respect to the text baseline....
Definition: Font.cs:48
VectSharp.FontFamily.ResolveFontFamily
static FontFamily ResolveFontFamily(string fontFamily)
Create a new font family from the specified family name or true type file. If the family name or the ...
VectSharp.FontFamily.ResolveFontFamily
static FontFamily ResolveFontFamily(string fontFamily, params string[] fallback)
Create a new font family from the specified family name or true type file. If the family name or the ...
VectSharp.FontFamily.ResolveFontFamily
static FontFamily ResolveFontFamily(string fontFamily, StandardFontFamilies finalFallback, params string[] fallback)
Create a new font family from the specified family name or true type file. If the family name or the ...
VectSharp.FontFamily.StandardFontFamilyResources
static string[] StandardFontFamilyResources
The names of the resource streams pointing to the included TrueType font files for each of the standa...
Definition: Font.cs:483
VectSharp.TrueTypeFile.IsItalic
bool IsItalic()
Determines whether the typeface is Italic or Oblique or not.
Definition: TrueType.cs:1943
VectSharp.Font.DetailedFontMetrics.Bottom
double Bottom
Depth of the deepest glyph in the string below the baseline. Always <= 0.
Definition: Font.cs:131
VectSharp.Font.FontUnderline.LineCap
LineCaps LineCap
Determines the caps at the start and end of the underline.
Definition: Font.cs:58
VectSharp.FontFamily
Represents a typeface.
Definition: Font.cs:421
VectSharp.FontFamily.FamilyName
string FamilyName
Name of the font family, including any variantes.
Definition: Font.cs:580
VectSharp.TrueTypeFile.Get1000EmYMin
double Get1000EmYMin()
Computes the maximum depth below the baseline of the font, in thousandths of em unit.
Definition: TrueType.cs:2159
VectSharp.DefaultFontLibrary
A default font library that resolves standard families using the embedded fonts.
Definition: FontLibrary.cs:463
VectSharp.FontFamily.IsOblique
bool IsOblique
Whether this font is oblique or not. This is set based on the information included in the OS/2 table ...
Definition: Font.cs:601
VectSharp.Font.DetailedFontMetrics.AdvanceWidth
double AdvanceWidth
Advance width of the text (excluding any left- or right- side bearing).
Definition: Font.cs:136
VectSharp.TrueTypeFile.Get1000EmAscent
double Get1000EmAscent()
Computes the font ascent, in thousandths of em unit.
Definition: TrueType.cs:2131
VectSharp.Font.Font
Font(FontFamily fontFamily, double fontSize, bool underlined)
Create a new Font object, given the base typeface, the font size, and a boolean value determining whe...
Definition: Font.cs:177
VectSharp.Font.EnableKerning
static bool EnableKerning
Determines whether text kerning is enabled. Note that, even when this is set to false,...
Definition: Font.cs:33
VectSharp.Font.FontUnderline
Represents options to underline text.
Definition: Font.cs:39
VectSharp.Font.DetailedFontMetrics
Represents detailed information about the metrics of a text string when drawn with a certain font.
Definition: Font.cs:102
VectSharp.TrueTypeFile.GetFullFontFamilyName
string GetFullFontFamilyName()
Obtains the full font family name from the TrueType file.
Definition: TrueType.cs:1873
VectSharp.FontFamily.IsBold
bool IsBold
Whether this font is bold or not. This is set based on the information included in the OS/2 table of ...
Definition: Font.cs:591
VectSharp.TrueTypeFile.Bearings.LeftSideBearing
int LeftSideBearing
The left-side bearing of the glyph.
Definition: TrueType.cs:2190
VectSharp.Font.FontUnderline.SkipDescenders
bool SkipDescenders
Determines whether the underline skips the parts of the glyph that would intersect with it or not.
Definition: Font.cs:43
VectSharp.Font.FontUnderline.Thickness
double Thickness
Determines the thickness of the underline, expressed as a fraction of the font size.
Definition: Font.cs:53
VectSharp.Point.X
double X
Horizontal (x) coordinate, measured to the right of the origin.
Definition: Point.cs:32
VectSharp.Font.Ascent
double Ascent
Maximum height over the baseline of the usual glyphs in the font (there may be glyphs taller than thi...
Definition: Font.cs:205
VectSharp.TrueTypeFile.Get1000EmGlyphWidth
double Get1000EmGlyphWidth(char glyph)
Computes the advance width of a glyph, in thousandths of em unit.
Definition: TrueType.cs:2082
VectSharp.ResourceFontFamily
Represents a FontFamily created from a resource stream.
Definition: Font.cs:695
VectSharp.TrueTypeFile.Get1000EmWinAscent
double Get1000EmWinAscent()
Computes the font's Win ascent, in thousandths of em unit.
Definition: TrueType.cs:2111
VectSharp.TrueTypeFile.VerticalMetrics
Represents the maximum heigth above and depth below the baseline of a glyph.
Definition: TrueType.cs:2241
VectSharp.TrueTypeFile.IsOblique
bool IsOblique()
Determines whether the typeface is Oblique or not.
Definition: TrueType.cs:1954
VectSharp.FontFamily.StandardFontFamilies
StandardFontFamilies
The 14 standard font families.
Definition: Font.cs:500
VectSharp.FontFamily.StandardFamilies
static string[] StandardFamilies
The names of the 14 standard families that are guaranteed to be displayed correctly.
Definition: Font.cs:478
VectSharp.Font.MeasureText
Size MeasureText(string text)
Measure the size of a text string when typeset with this font.
Definition: Font.cs:301
VectSharp.Size
Represents the size of an object.
Definition: Point.cs:146
VectSharp.FontFamily.ResolveFontFamily
static FontFamily ResolveFontFamily(StandardFontFamilies standardFontFamily)
Create a new font family from the specified standard font family name. Equivalent to DefaultFontLibra...
VectSharp.TrueTypeFile.PairKerning
Contains information describing how the position of two glyphs in a kerning pair should be altered.
Definition: TrueType.cs:4252
VectSharp.Font.YMin
double YMin
Absolute maximum depth below the baseline of the glyphs in the font. Always <= 0.
Definition: Font.cs:277
VectSharp.FontFamily.FontFamily
FontFamily(TrueTypeFile ttf)
Create a new FontFamily.
Definition: Font.cs:653
VectSharp.FontFamily.FontFamily
FontFamily(string fileName)
Create a new FontFamily.
Definition: Font.cs:608
VectSharp.Font.Font
Font(FontFamily fontFamily, double fontSize, FontUnderline underline)
Create a new Font object, given the base typeface, the font size, and an object describing the underl...
Definition: Font.cs:194
VectSharp.TrueTypeFile.Get1000EmUnderlineThickness
double Get1000EmUnderlineThickness()
Computes the thickness of the underline, in thousandths of em unit.
Definition: TrueType.cs:2304
VectSharp.Font.FontSize
double FontSize
Font size, in graphics units.
Definition: Font.cs:153
VectSharp.Point
Represents a point relative to an origin in the top-left corner.
Definition: Point.cs:28
VectSharp.TrueTypeFile.IsBold
bool IsBold()
Determines whether the typeface is Bold or not.
Definition: TrueType.cs:1965
VectSharp.Font.Descent
double Descent
Maximum depth below the baseline of the usual glyphs in the font (there may be glyphs deeper than thi...
Definition: Font.cs:241
VectSharp.Font.DetailedFontMetrics.Height
double Height
Height of the text (measured on the actual glyph outlines).
Definition: Font.cs:111
VectSharp.TrueTypeFile.Get1000EmDescent
double Get1000EmDescent()
Computes the font descent, in thousandths of em unit.
Definition: TrueType.cs:2141
VectSharp.DefaultFontLibrary.ResolveFontFamily
override FontFamily ResolveFontFamily(string fontFamily)
Create a new font family from the specified family name or true type file. If the family name or the ...
Definition: FontLibrary.cs:465
VectSharp.TrueTypeFile.GetFontFamilyName
string GetFontFamilyName()
Obtains the font family name from the TrueType file.
Definition: TrueType.cs:1846
VectSharp.Point.Y
double Y
Vertical (y) coordinate, measured to the bottom of the origin.
Definition: Point.cs:37
VectSharp.FontFamily.TrueTypeFile
TrueTypeFile TrueTypeFile
Parsed TrueType font file for this font family. See also: VectSharp.TrueTypeFile.
Definition: Font.cs:586
VectSharp.FontFamily.FileName
string FileName
Full path to the TrueType font file for this font family (or, if this is a standard font family,...
Definition: Font.cs:575