NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Font.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using System.Text;
11using NanoXLSX.Utils;
12using static NanoXLSX.Themes.Theme;
13
14namespace NanoXLSX.Styles
15{
19 public class Font : AbstractStyle
20 {
21 #region constants
25 public static readonly float MinFontSize = 1f;
26
30 public static readonly float MaxFontSize = 409f;
31
35 public static readonly float DefaultFontSize = 11f;
36
40 public static readonly string DefaultMajorFont = "Calibri Light";
44 public static readonly string DefaultMinorFont = "Calibri";
45
49 public static readonly string DefaultFontName = DefaultMinorFont;
50
54 public static readonly FontFamilyValue DefaultFontFamily = FontFamilyValue.Swiss;
55
59 public static readonly SchemeValue DefaultFontScheme = SchemeValue.Minor;
60
65 #endregion
66
67 #region enums
71 public enum SchemeValue
72 {
79 }
80
84 {
85 // baseline, // Maybe not used in Excel
92 }
93
97 public enum UnderlineValue
98 {
100#pragma warning disable CA1720 // Suppress: Identifiers should not contain type
104#pragma warning restore CA1707
111 }
112
116 public enum CharsetValue
117 {
125 ANSI = 0,
141 JIS = 128,
145 Hangul = 129,
149 Johab = 130,
153 GBK = 134,
157 Big5 = 136,
161 Greek = 161,
165 Turkish = 162,
173 Hebrew = 177,
177 Arabic = 178,
181 Baltic = 186,
185 Russian = 204,
189 Thai = 222,
197 OEM = 255
198 }
199
266 #endregion
267
268 #region privateFields
269 private float size;
270 private string name = DefaultFontName;
271 //TODO: V3> Refactor to enum according to specs
272 //OOXML: Chp.20.1.6.2(p2839ff)
273 private string colorValue = "";
274 private ColorSchemeElement colorTheme;
275 #endregion
276
277 #region properties
278
279
280
284 [Append]
285 public bool Bold { get; set; }
289 [Append]
290 public bool Italic { get; set; }
294 [Append]
295 public bool Strike { get; set; }
299 [Append]
300 public UnderlineValue Underline { get; set; } = UnderlineValue.None;
301
305 [Append]
306 //TODO: v3> Refactor to enum according to specs
307 // OOXML: Chp.19.2.1.13
308 public CharsetValue Charset { get; set; } = CharsetValue.Default;
309
313 [Append]
314 //TODO: v3> Reference to Theming
315 //OOXML: Chp.18.8.3 and 20.1.6.2
316 public ColorSchemeElement ColorTheme
317 {
318 get => colorTheme;
319 set
320 {
321 if (value == null)
322 {
323 throw new StyleException("A color theme cannot be null");
324 }
325 colorTheme = value;
326 }
327 }
328
333 [Append]
334 public string ColorValue
335 {
336 get => colorValue;
337 set
338 {
339 Validators.ValidateColor(value, true, true);
340 if (value != null)
341 {
342 colorValue = ParserUtils.ToUpper(value);
343 }
344 else
345 {
346 colorValue = value;
347 }
348 }
349 }
350
353 [Append]
354 //TODO: v3> Refactor to enum according to specs (18.18.94)
355 //OOXML: Chp.18.8.18 and 18.18.94
356 public FontFamilyValue Family { get; set; }
360 [Append(Ignore = true)]
361 public bool IsDefaultFont
362 {
363 get
364 {
365 Font temp = new Font();
366 return Equals(temp);
367 }
368 }
369
370
376 [Append]
377 public string Name //OOXML: Chp.18.8.29
378 {
379 get { return name; }
380 set
381 {
382 name = value;
383 ValidateFontScheme();
384 }
385 }
386
389 [Append]
390 public SchemeValue Scheme { get; set; }
394 [Append]
395 public float Size
396 {
397 get { return size; }
398 set
399 {
400 if (value < MinFontSize)
401 { size = MinFontSize; }
402 else if (value > MaxFontSize)
403 { size = MaxFontSize; }
404 else { size = value; }
405 }
406 }
407
411 [Append]
413
414 #endregion
415
416 #region constructors
420 public Font()
421 {
422 size = DefaultFontSize;
425 ColorTheme = ColorSchemeElement.Light1;
426 ColorValue = string.Empty;
429 }
430 #endregion
431
432 #region methods
433
437 private void ValidateFontScheme()
438 {
439 if ((string.IsNullOrEmpty(name)) && !StyleRepository.Instance.ImportInProgress)
440 {
441 throw new StyleException("The font name was null or empty");
442 }
443 if (name.Equals(DefaultMinorFont, System.StringComparison.Ordinal))
444 {
445 Scheme = SchemeValue.Minor;
446 }
447 else if (name.Equals(DefaultMajorFont, System.StringComparison.Ordinal))
448 {
449 Scheme = SchemeValue.Major;
450 }
451 else
452 {
453 Scheme = SchemeValue.None;
454 }
455 }
456
461 public override string ToString()
462 {
463 StringBuilder sb = new StringBuilder();
464 sb.Append("\"Font\": {\n");
465 AddPropertyAsJson(sb, "Bold", Bold);
466 AddPropertyAsJson(sb, "Charset", Charset);
467 AddPropertyAsJson(sb, "ColorTheme", ColorTheme);
468 AddPropertyAsJson(sb, "ColorValue", ColorValue);
469 AddPropertyAsJson(sb, "VerticalAlign", VerticalAlign);
470 AddPropertyAsJson(sb, "Family", Family);
471 AddPropertyAsJson(sb, "Italic", Italic);
472 AddPropertyAsJson(sb, "Name", Name);
473 AddPropertyAsJson(sb, "Scheme", Scheme);
474 AddPropertyAsJson(sb, "Size", Size);
475 AddPropertyAsJson(sb, "Strike", Strike);
476 AddPropertyAsJson(sb, "Underline", Underline);
477 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
478 sb.Append("\n}");
479 return sb.ToString();
480 }
481
486 public override AbstractStyle Copy()
487 {
488 Font copy = new Font
489 {
490 Bold = Bold,
495 Family = Family,
496 Italic = Italic,
497 Name = Name,
498 Scheme = Scheme,
499 Size = Size,
500 Strike = Strike,
502 };
503 return copy;
504 }
505
512 public override int GetHashCode()
513 {
514 unchecked
515 {
516 int hashCode = -924704582;
517 hashCode = hashCode * -1521134295 + size.GetHashCode();
518 hashCode = hashCode * -1521134295 + Bold.GetHashCode();
519 hashCode = hashCode * -1521134295 + Charset.GetHashCode();
520 hashCode = hashCode * -1521134295 + ColorTheme.GetHashCode();
521 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ColorValue);
522 hashCode = hashCode * -1521134295 + Family.GetHashCode();
523 hashCode = hashCode * -1521134295 + Italic.GetHashCode();
524 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
525 hashCode = hashCode * -1521134295 + Scheme.GetHashCode();
526 hashCode = hashCode * -1521134295 + Strike.GetHashCode();
527 hashCode = hashCode * -1521134295 + Underline.GetHashCode();
528 hashCode = hashCode * -1521134295 + VerticalAlign.GetHashCode();
529 return hashCode;
530 }
531 }
532
538 public override bool Equals(object obj)
539 {
540 return obj is Font font &&
541 size == font.size &&
542 Bold == font.Bold &&
543 Italic == font.Italic &&
544 Strike == font.Strike &&
545 Underline == font.Underline &&
546 Charset == font.Charset &&
547 ColorTheme == font.ColorTheme &&
548 ColorValue == font.ColorValue &&
549 Family == font.Family &&
550 Name == font.Name &&
551 Scheme == font.Scheme &&
552 VerticalAlign == font.VerticalAlign;
553 }
554
559 public Font CopyFont()
560 {
561 return (Font)Copy();
562 }
563
564 #endregion
565
566 #region staticMethods
567
571 internal static string GetVerticalTextAlignName(VerticalTextAlignValue align)
572 {
573 string output = "";
574 switch (align)
575 {
576 case VerticalTextAlignValue.Subscript: output = "subscript"; break;
577 case VerticalTextAlignValue.Superscript: output = "superscript"; break;
578 }
579 return output;
580 }
581
585 internal static VerticalTextAlignValue GetVerticalTextAlignEnum(string name)
586 {
587 switch (name)
588 {
589 case "subscript": return VerticalTextAlignValue.Subscript;
590 case "superscript": return VerticalTextAlignValue.Superscript;
591 default:
592 return VerticalTextAlignValue.None;
593 }
594 }
595
599 internal static string GetUnderlineName(UnderlineValue underline)
600 {
601 string output = "";
602 switch (underline)
603 {
604 case UnderlineValue.Double: output = "double"; break;
605 case UnderlineValue.SingleAccounting: output = "singleAccounting"; break;
606 case UnderlineValue.DoubleAccounting: output = "doubleAccounting"; break;
607 }
608 return output;
609 }
610
614 internal static UnderlineValue GetUnderlineEnum(string name)
615 {
616 UnderlineValue output = UnderlineValue.None;
617 switch (name)
618 {
619 case "double": output = UnderlineValue.Double; break;
620 case "singleAccounting": output = UnderlineValue.SingleAccounting; break;
621 case "doubleAccounting": output = UnderlineValue.DoubleAccounting; break;
622 }
623 return output;
624 }
625
626 #endregion
627
628 }
629}
Class for exceptions regarding Style incidents.
Class represents an abstract style component.
CharsetValue Charset
Gets or sets the char set of the Font.
Definition Font.cs:308
VerticalTextAlignValue
Enum for the vertical alignment of the text from baseline, used by the Font class.
Definition Font.cs:84
@ Superscript
Text will be rendered as superscript.
Definition Font.cs:89
@ Subscript
Text will be rendered as subscript.
Definition Font.cs:87
SchemeValue
Enum for the font scheme, used by the Font class.
Definition Font.cs:72
@ Major
Font scheme is major.
Definition Font.cs:74
@ None
No Font scheme is used.
Definition Font.cs:78
@ Minor
Font scheme is minor (default).
Definition Font.cs:76
bool Bold
Gets or sets whether the font is bold. If true, the font is declared as bold.
Definition Font.cs:285
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Font.cs:538
static readonly float MinFontSize
Maximum possible font size.
Definition Font.cs:25
static readonly VerticalTextAlignValue DefaultVerticalAlign
Default vertical alignment.
Definition Font.cs:64
static readonly string DefaultMajorFont
The default font name that is declared as Major Font (See SchemeValue).
Definition Font.cs:40
static readonly float DefaultFontSize
Default font size.
Definition Font.cs:35
static readonly string DefaultFontName
Default font family as constant.
Definition Font.cs:49
static readonly FontFamilyValue DefaultFontFamily
Default font family.
Definition Font.cs:54
ColorSchemeElement ColorTheme
Gets or sets the font color theme, represented by a color scheme.
Definition Font.cs:317
Font CopyFont()
Method to copy the current object to a new one with casting.
Definition Font.cs:559
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Font.cs:486
bool IsDefaultFont
Gets whether the font is equal to the default font.
Definition Font.cs:362
bool Italic
Gets or sets whether the font is italic. If true, the font is declared as italic.
Definition Font.cs:290
VerticalTextAlignValue VerticalAlign
Gets or sets the alignment of the font (Default is none).
Definition Font.cs:412
SchemeValue Scheme
Gets or sets the font scheme (Default is minor).
Definition Font.cs:390
static readonly SchemeValue DefaultFontScheme
Default font scheme.
Definition Font.cs:59
UnderlineValue Underline
Gets or sets the underline style of the font. If set to none no underline will be applied (default).
Definition Font.cs:300
bool Strike
Gets or sets whether the font is struck through. If true, the font is declared as strike-through.
Definition Font.cs:295
static readonly string DefaultMinorFont
The default font name that is declared as Minor Font (See SchemeValue).
Definition Font.cs:44
string ColorValue
Gets or sets the color code of the font color. The value is expressed as hex string with the format A...
Definition Font.cs:335
static readonly float MaxFontSize
Minimum possible font size.
Definition Font.cs:30
float Size
Gets or sets the font size. Valid range is from 1 to 409.
Definition Font.cs:396
string Name
Gets or sets the font name (Default is Calibri).
Definition Font.cs:378
override int GetHashCode()
Returns a hash code for this instance.
Definition Font.cs:512
FontFamilyValue Family
Gets or sets the font family (Default is 2 = Swiss).
Definition Font.cs:356
Font()
Default constructor.
Definition Font.cs:420
FontFamilyValue
Enum for the font family, according to the simple type definition of W3C. Used by the Font class.
Definition Font.cs:204
@ Swiss
The specified font implements a Swiss font.
Definition Font.cs:216
@ Reserved5
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:248
@ Reserved4
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:244
@ Reserved9
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:264
@ Reserved7
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:256
@ NotApplicable
The family is not defined or not applicable.
Definition Font.cs:208
@ Reserved1
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:232
@ Modern
The specified font implements a Modern font.
Definition Font.cs:220
@ Reserved3
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:240
@ Roman
The specified font implements a Roman font.
Definition Font.cs:212
@ Decorative
The specified font implements a Decorative font.
Definition Font.cs:228
@ Reserved2
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:236
@ Reserved6
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:252
@ Reserved8
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:260
@ Script
The specified font implements a Script font.
Definition Font.cs:224
override string ToString()
Override toString method.
Definition Font.cs:461
UnderlineValue
Enum for the style of the underline property of a stylized text, used by the Font class.
Definition Font.cs:98
@ Single
Text contains a single underline.
Definition Font.cs:101
@ SingleAccounting
Text contains a single, accounting underline.
Definition Font.cs:106
@ DoubleAccounting
Text contains a double, accounting underline.
Definition Font.cs:108
@ Double
Text contains a double underline.
Definition Font.cs:103
CharsetValue
Enum for the charset definitions of a font, used by the Font class.
Definition Font.cs:117
@ JIS
Shift JIS charset (shift_jis).
Definition Font.cs:141
@ Greek
Greek charset (windows-1253).
Definition Font.cs:161
@ Big5
Chinese Big Five charset.
Definition Font.cs:157
@ Arabic
Arabic charset (windows-1256).
Definition Font.cs:177
@ Thai
Thai charset (windows-874).
Definition Font.cs:189
@ OEM
OEM characters, not defined by ECMA-376.
Definition Font.cs:197
@ Symbols
Symbols from the private Unicode range U+FF00 to U+FFFF, to display special characters in the range o...
Definition Font.cs:133
@ Hangul
Hangul charset (ks_c_5601-1987).
Definition Font.cs:145
@ Default
Default charset (not defined more specific).
Definition Font.cs:129
@ Vietnamese
Vietnamese charset (windows-1258).
Definition Font.cs:169
@ EasternEuropean
Eastern Europe charset (windows-1250).
Definition Font.cs:193
@ GBK
GBK charset (GB-2312).
Definition Font.cs:153
@ Turkish
Turkish charset (iso-8859-9).
Definition Font.cs:165
@ Macintosh
Macintosh charset, Standard Roman.
Definition Font.cs:137
@ Hebrew
Hebrew charset (windows-1255).
Definition Font.cs:173
@ ANSI
Charset according to iso-8859-1.
Definition Font.cs:125
@ Johab
Johab charset (KSC-5601-1992).
Definition Font.cs:149
@ Russian
Russian charset (windows-1251).
Definition Font.cs:185
@ Baltic
Baltic charset (windows-1257).
Definition Font.cs:181
@ ApplicationDefined
Application-defined (any other value than the defined enum values; can be ignored).
Definition Font.cs:121
Class to manage all styles at runtime, before writing XLSX files. The main purpose is deduplication a...
static StyleRepository Instance
Gets the singleton instance of the repository.
Class representing an Office theme.
Definition Theme.cs:16
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
Class providing general validator methods.
Definition Validators.cs:11
static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills or Fo...
Definition Validators.cs:25