NanoXLSX.Core 3.0.0-rc.5
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 © 2026
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;
10using NanoXLSX.Colors;
12
13namespace NanoXLSX.Styles
14{
18 public class Font : AbstractStyle
19 {
20 #region constants
24 public static readonly float MinFontSize = 1f;
25
29 public static readonly float MaxFontSize = 409f;
30
34 public static readonly float DefaultFontSize = 11f;
35
39 public static readonly string DefaultMajorFont = "Calibri Light";
43 public static readonly string DefaultMinorFont = "Calibri";
44
48 public static readonly string DefaultFontName = DefaultMinorFont;
49
53 public static readonly FontFamilyValue DefaultFontFamily = FontFamilyValue.Swiss;
54
58 public static readonly SchemeValue DefaultFontScheme = SchemeValue.Minor;
59
64 #endregion
65
66 #region enums
70 public enum SchemeValue
71 {
78 }
79
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 #endregion
272
273 #region properties
274
275
276
280 [Append]
281 public bool Bold { get; set; }
285 [Append]
286 public bool Italic { get; set; }
290 [Append]
291 public bool Strike { get; set; }
295 [Append]
296 public UnderlineValue Underline { get; set; } = UnderlineValue.None;
300 [Append]
301 public bool Outline { get; set; }
306 [Append]
307 public bool Shadow { get; set; }
308
313 [Append]
314 public bool Condense { get; set; }
319 [Append]
320 public bool Extend { get; set; }
321
325 [Append]
326 public CharsetValue Charset { get; set; } = CharsetValue.Default;
327
332 [Append]
334 {
335 get; set;
336 }
337
340 [Append]
341 public FontFamilyValue Family { get; set; }
345 [Append(Ignore = true)]
346 public bool IsDefaultFont
347 {
348 get
349 {
350 Font temp = new Font();
351 return Equals(temp);
352 }
353 }
354
360 [Append]
361 public string Name //OOXML: Chp.18.8.29
362 {
363 get { return name; }
364 set
365 {
366 name = value;
367 ValidateFontScheme();
368 }
369 }
370
373 [Append]
374 public SchemeValue Scheme { get; set; }
378 [Append]
379 public float Size
380 {
381 get { return size; }
382 set
383 {
384 if (value < MinFontSize)
385 { size = MinFontSize; }
386 else if (value > MaxFontSize)
387 { size = MaxFontSize; }
388 else { size = value; }
389 }
390 }
391
395 [Append]
397
398 #endregion
399
400 #region constructors
405 public Font()
406 {
407 size = DefaultFontSize;
410 ColorValue = Color.CreateNone();// Default is none
413 }
414 #endregion
415
416 #region methods
417
421 private void ValidateFontScheme()
422 {
423 if ((string.IsNullOrEmpty(name)) && !StyleRepository.Instance.ImportInProgress)
424 {
425 throw new StyleException("The font name was null or empty");
426 }
427 if (name.Equals(DefaultMinorFont, System.StringComparison.Ordinal))
428 {
429 Scheme = SchemeValue.Minor;
430 }
431 else if (name.Equals(DefaultMajorFont, System.StringComparison.Ordinal))
432 {
433 Scheme = SchemeValue.Major;
434 }
435 else
436 {
437 Scheme = SchemeValue.None;
438 }
439 }
440
445 public override string ToString()
446 {
447 StringBuilder sb = new StringBuilder();
448 sb.Append("\"Font\": {\n");
449 AddPropertyAsJson(sb, "Bold", Bold);
450 AddPropertyAsJson(sb, "Charset", Charset);
451 AddPropertyAsJson(sb, "ColorValue", ColorValue);
452 AddPropertyAsJson(sb, "VerticalAlign", VerticalAlign);
453 AddPropertyAsJson(sb, "Family", Family);
454 AddPropertyAsJson(sb, "Italic", Italic);
455 AddPropertyAsJson(sb, "Name", Name);
456 AddPropertyAsJson(sb, "Scheme", Scheme);
457 AddPropertyAsJson(sb, "Size", Size);
458 AddPropertyAsJson(sb, "Strike", Strike);
459 AddPropertyAsJson(sb, "Underline", Underline);
460 AddPropertyAsJson(sb, "Outline", Outline);
461 AddPropertyAsJson(sb, "Shadow", Shadow);
462 AddPropertyAsJson(sb, "Condense", Condense);
463 AddPropertyAsJson(sb, "Extend", Extend);
464 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
465 sb.Append("\n}");
466 return sb.ToString();
467 }
468
473 public override AbstractStyle Copy()
474 {
475 Font copy = new Font
476 {
477 Bold = Bold,
481 Family = Family,
482 Italic = Italic,
483 Name = Name,
484 Scheme = Scheme,
485 Size = Size,
486 Strike = Strike,
489 Shadow = Shadow,
491 Extend = Extend,
492 };
493 return copy;
494 }
495
502 public override int GetHashCode()
503 {
504 unchecked
505 {
506 int hashCode = -924704582;
507 hashCode = hashCode * -1521134295 + size.GetHashCode();
508 hashCode = hashCode * -1521134295 + Bold.GetHashCode();
509 hashCode = hashCode * -1521134295 + Charset.GetHashCode();
510 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(ColorValue);
511 hashCode = hashCode * -1521134295 + Family.GetHashCode();
512 hashCode = hashCode * -1521134295 + Italic.GetHashCode();
513 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
514 hashCode = hashCode * -1521134295 + Scheme.GetHashCode();
515 hashCode = hashCode * -1521134295 + Strike.GetHashCode();
516 hashCode = hashCode * -1521134295 + Underline.GetHashCode();
517 hashCode = hashCode * -1521134295 + Outline.GetHashCode();
518 hashCode = hashCode * -1521134295 + Shadow.GetHashCode();
519 hashCode = hashCode * -1521134295 + Condense.GetHashCode();
520 hashCode = hashCode * -1521134295 + Extend.GetHashCode();
521 hashCode = hashCode * -1521134295 + VerticalAlign.GetHashCode();
522 return hashCode;
523 }
524 }
525
531 public override bool Equals(object obj)
532 {
533 return obj is Font font &&
534 size == font.size &&
535 Bold == font.Bold &&
536 Italic == font.Italic &&
537 Strike == font.Strike &&
538 Underline == font.Underline &&
539 Outline == font.Outline &&
540 Shadow == font.Shadow &&
541 Condense == font.Condense &&
542 Extend == font.Extend &&
543 Charset == font.Charset &&
544 ColorValue == font.ColorValue &&
545 Family == font.Family &&
546 Name == font.Name &&
547 Scheme == font.Scheme &&
548 VerticalAlign == font.VerticalAlign;
549 }
550
555 public Font CopyFont()
556 {
557 return (Font)Copy();
558 }
559
560 #endregion
561
562 #region staticMethods
563
567 internal static string GetVerticalTextAlignName(VerticalTextAlignValue align)
568 {
569 string output = "";
570 switch (align)
571 {
572 case VerticalTextAlignValue.Baseline: output = "baseline"; break;
573 case VerticalTextAlignValue.Subscript: output = "subscript"; break;
574 case VerticalTextAlignValue.Superscript: output = "superscript"; break;
575 }
576 return output;
577 }
578
582 internal static VerticalTextAlignValue GetVerticalTextAlignEnum(string name)
583 {
584 switch (name)
585 {
586 case "baseline": return VerticalTextAlignValue.Baseline;
587 case "subscript": return VerticalTextAlignValue.Subscript;
588 case "superscript": return VerticalTextAlignValue.Superscript;
589 default:
590 return VerticalTextAlignValue.None;
591 }
592 }
593
597 internal static string GetUnderlineName(UnderlineValue underline)
598 {
599 string output = "";
600 switch (underline)
601 {
602 case UnderlineValue.Double: output = "double"; break;
603 case UnderlineValue.SingleAccounting: output = "singleAccounting"; break;
604 case UnderlineValue.DoubleAccounting: output = "doubleAccounting"; break;
605 }
606 return output;
607 }
608
612 internal static UnderlineValue GetUnderlineEnum(string name)
613 {
614 UnderlineValue output = UnderlineValue.None;
615 switch (name)
616 {
617 case "double": output = UnderlineValue.Double; break;
618 case "singleAccounting": output = UnderlineValue.SingleAccounting; break;
619 case "doubleAccounting": output = UnderlineValue.DoubleAccounting; break;
620 }
621 return output;
622 }
623
624 #endregion
625
626 }
627}
Compound class representing a color in various representations (RGB, indexed, theme,...
Definition Color.cs:20
static Color CreateNone()
Creates an Color with no color (empty element).
Definition Color.cs:148
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:326
VerticalTextAlignValue
Enum for the vertical alignment of the text from baseline, used by the Font class.
Definition Font.cs:83
@ Superscript
Text will be rendered as superscript. The text size will be reduced.
Definition Font.cs:89
@ Subscript
Text will be rendered as subscript. The text size will be reduced.
Definition Font.cs:87
@ Baseline
Text will be rendered at the baseline and presented in the same size as surrounding text.
Definition Font.cs:85
SchemeValue
Enum for the font scheme, used by the Font class.
Definition Font.cs:71
@ Major
Font scheme is major.
Definition Font.cs:73
@ None
No Font scheme is used.
Definition Font.cs:77
@ Minor
Font scheme is minor (default).
Definition Font.cs:75
bool Bold
Gets or sets whether the font is bold. If true, the font is declared as bold.
Definition Font.cs:281
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Font.cs:531
static readonly float MinFontSize
Maximum possible font size.
Definition Font.cs:24
bool Outline
Gets or sets whether the font has an outline defined. If true, an outline is rendered around the text...
Definition Font.cs:301
static readonly VerticalTextAlignValue DefaultVerticalAlign
Default vertical alignment.
Definition Font.cs:63
static readonly string DefaultMajorFont
The default font name that is declared as Major Font (See SchemeValue).
Definition Font.cs:39
static readonly float DefaultFontSize
Default font size.
Definition Font.cs:34
static readonly string DefaultFontName
Default font family as constant.
Definition Font.cs:48
static readonly FontFamilyValue DefaultFontFamily
Default font family.
Definition Font.cs:53
Font CopyFont()
Method to copy the current object to a new one with casting.
Definition Font.cs:555
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Font.cs:473
bool IsDefaultFont
Gets whether the font is equal to the default font.
Definition Font.cs:347
bool Italic
Gets or sets whether the font is italic. If true, the font is declared as italic.
Definition Font.cs:286
Color ColorValue
Gets or sets the color code of the font color. The value is an instance of Color To omit the color,...
Definition Font.cs:334
VerticalTextAlignValue VerticalAlign
Gets or sets the alignment of the font (Default is none).
Definition Font.cs:396
SchemeValue Scheme
Gets or sets the font scheme (Default is minor).
Definition Font.cs:374
static readonly SchemeValue DefaultFontScheme
Default font scheme.
Definition Font.cs:58
bool Extend
Gets or sets whether the font is rendered extended. If true, characters are placed more distant to ea...
Definition Font.cs:320
UnderlineValue Underline
Gets or sets the underline style of the font. If set to none no underline will be applied (default).
Definition Font.cs:296
bool Strike
Gets or sets whether the font is struck through. If true, the font is declared as strike-through.
Definition Font.cs:291
static readonly string DefaultMinorFont
The default font name that is declared as Minor Font (See SchemeValue).
Definition Font.cs:43
static readonly float MaxFontSize
Minimum possible font size.
Definition Font.cs:29
bool Condense
Gets or sets whether the font is rendered condenses. If true, characters are placed closer to each ot...
Definition Font.cs:314
float Size
Gets or sets the font size. Valid range is from 1 to 409.
Definition Font.cs:380
string Name
Gets or sets the font name (Default is Calibri).
Definition Font.cs:362
override int GetHashCode()
Returns a hash code for this instance.
Definition Font.cs:502
FontFamilyValue Family
Gets or sets the font family (Default is 2 = Swiss).
Definition Font.cs:341
Font()
Default constructor.
Definition Font.cs:405
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:445
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
bool Shadow
Gets or sets whether the font has a drop shadow. If true, a shadow is rendered on the text.
Definition Font.cs:307
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.