NanoXLSX.Core 3.0.0-rc.4
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;
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
83 {
84 // baseline, // Maybe not used in Excel
91 }
92
96 public enum UnderlineValue
97 {
99#pragma warning disable CA1720 // Suppress: Identifiers should not contain type
103#pragma warning restore CA1707
110 }
111
115 public enum CharsetValue
116 {
124 ANSI = 0,
140 JIS = 128,
144 Hangul = 129,
148 Johab = 130,
152 GBK = 134,
156 Big5 = 136,
160 Greek = 161,
164 Turkish = 162,
172 Hebrew = 177,
176 Arabic = 178,
180 Baltic = 186,
184 Russian = 204,
188 Thai = 222,
196 OEM = 255
197 }
198
265 #endregion
266
267 #region privateFields
268 private float size;
269 private string name = DefaultFontName;
270 #endregion
271
272 #region properties
273
274
275
279 [Append]
280 public bool Bold { get; set; }
284 [Append]
285 public bool Italic { get; set; }
289 [Append]
290 public bool Strike { get; set; }
294 [Append]
295 public UnderlineValue Underline { get; set; } = UnderlineValue.None;
299 [Append]
300 public bool Outline { get; set; }
305 [Append]
306 public bool Shadow { get; set; }
307
312 [Append]
313 public bool Condense { get; set; }
318 [Append]
319 public bool Extend { get; set; }
320
324 [Append]
325 public CharsetValue Charset { get; set; } = CharsetValue.Default;
326
331 [Append]
333 {
334 get; set;
335 }
336
339 [Append]
340 public FontFamilyValue Family { get; set; }
344 [Append(Ignore = true)]
345 public bool IsDefaultFont
346 {
347 get
348 {
349 Font temp = new Font();
350 return Equals(temp);
351 }
352 }
353
359 [Append]
360 public string Name //OOXML: Chp.18.8.29
361 {
362 get { return name; }
363 set
364 {
365 name = value;
366 ValidateFontScheme();
367 }
368 }
369
372 [Append]
373 public SchemeValue Scheme { get; set; }
377 [Append]
378 public float Size
379 {
380 get { return size; }
381 set
382 {
383 if (value < MinFontSize)
384 { size = MinFontSize; }
385 else if (value > MaxFontSize)
386 { size = MaxFontSize; }
387 else { size = value; }
388 }
389 }
390
394 [Append]
396
397 #endregion
398
399 #region constructors
404 public Font()
405 {
406 size = DefaultFontSize;
409 ColorValue = Color.CreateNone();// Default is none
412 }
413 #endregion
414
415 #region methods
416
420 private void ValidateFontScheme()
421 {
422 if ((string.IsNullOrEmpty(name)) && !StyleRepository.Instance.ImportInProgress)
423 {
424 throw new StyleException("The font name was null or empty");
425 }
426 if (name.Equals(DefaultMinorFont, System.StringComparison.Ordinal))
427 {
428 Scheme = SchemeValue.Minor;
429 }
430 else if (name.Equals(DefaultMajorFont, System.StringComparison.Ordinal))
431 {
432 Scheme = SchemeValue.Major;
433 }
434 else
435 {
436 Scheme = SchemeValue.None;
437 }
438 }
439
444 public override string ToString()
445 {
446 StringBuilder sb = new StringBuilder();
447 sb.Append("\"Font\": {\n");
448 AddPropertyAsJson(sb, "Bold", Bold);
449 AddPropertyAsJson(sb, "Charset", Charset);
450 AddPropertyAsJson(sb, "ColorValue", ColorValue);
451 AddPropertyAsJson(sb, "VerticalAlign", VerticalAlign);
452 AddPropertyAsJson(sb, "Family", Family);
453 AddPropertyAsJson(sb, "Italic", Italic);
454 AddPropertyAsJson(sb, "Name", Name);
455 AddPropertyAsJson(sb, "Scheme", Scheme);
456 AddPropertyAsJson(sb, "Size", Size);
457 AddPropertyAsJson(sb, "Strike", Strike);
458 AddPropertyAsJson(sb, "Underline", Underline);
459 AddPropertyAsJson(sb, "Outline", Outline);
460 AddPropertyAsJson(sb, "Shadow", Shadow);
461 AddPropertyAsJson(sb, "Condense", Condense);
462 AddPropertyAsJson(sb, "Extend", Extend);
463 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
464 sb.Append("\n}");
465 return sb.ToString();
466 }
467
472 public override AbstractStyle Copy()
473 {
474 Font copy = new Font
475 {
476 Bold = Bold,
480 Family = Family,
481 Italic = Italic,
482 Name = Name,
483 Scheme = Scheme,
484 Size = Size,
485 Strike = Strike,
488 Shadow = Shadow,
490 Extend = Extend,
491 };
492 return copy;
493 }
494
501 public override int GetHashCode()
502 {
503 unchecked
504 {
505 int hashCode = -924704582;
506 hashCode = hashCode * -1521134295 + size.GetHashCode();
507 hashCode = hashCode * -1521134295 + Bold.GetHashCode();
508 hashCode = hashCode * -1521134295 + Charset.GetHashCode();
509 hashCode = hashCode * -1521134295 + EqualityComparer<Color>.Default.GetHashCode(ColorValue);
510 hashCode = hashCode * -1521134295 + Family.GetHashCode();
511 hashCode = hashCode * -1521134295 + Italic.GetHashCode();
512 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
513 hashCode = hashCode * -1521134295 + Scheme.GetHashCode();
514 hashCode = hashCode * -1521134295 + Strike.GetHashCode();
515 hashCode = hashCode * -1521134295 + Underline.GetHashCode();
516 hashCode = hashCode * -1521134295 + Outline.GetHashCode();
517 hashCode = hashCode * -1521134295 + Shadow.GetHashCode();
518 hashCode = hashCode * -1521134295 + Condense.GetHashCode();
519 hashCode = hashCode * -1521134295 + Extend.GetHashCode();
520 hashCode = hashCode * -1521134295 + VerticalAlign.GetHashCode();
521 return hashCode;
522 }
523 }
524
530 public override bool Equals(object obj)
531 {
532 return obj is Font font &&
533 size == font.size &&
534 Bold == font.Bold &&
535 Italic == font.Italic &&
536 Strike == font.Strike &&
537 Underline == font.Underline &&
538 Outline == font.Outline &&
539 Shadow == font.Shadow &&
540 Condense == font.Condense &&
541 Extend == font.Extend &&
542 Charset == font.Charset &&
543 ColorValue == font.ColorValue &&
544 Family == font.Family &&
545 Name == font.Name &&
546 Scheme == font.Scheme &&
547 VerticalAlign == font.VerticalAlign;
548 }
549
554 public Font CopyFont()
555 {
556 return (Font)Copy();
557 }
558
559 #endregion
560
561 #region staticMethods
562
566 internal static string GetVerticalTextAlignName(VerticalTextAlignValue align)
567 {
568 string output = "";
569 switch (align)
570 {
571 case VerticalTextAlignValue.Subscript: output = "subscript"; break;
572 case VerticalTextAlignValue.Superscript: output = "superscript"; break;
573 }
574 return output;
575 }
576
580 internal static VerticalTextAlignValue GetVerticalTextAlignEnum(string name)
581 {
582 switch (name)
583 {
584 case "subscript": return VerticalTextAlignValue.Subscript;
585 case "superscript": return VerticalTextAlignValue.Superscript;
586 default:
587 return VerticalTextAlignValue.None;
588 }
589 }
590
594 internal static string GetUnderlineName(UnderlineValue underline)
595 {
596 string output = "";
597 switch (underline)
598 {
599 case UnderlineValue.Double: output = "double"; break;
600 case UnderlineValue.SingleAccounting: output = "singleAccounting"; break;
601 case UnderlineValue.DoubleAccounting: output = "doubleAccounting"; break;
602 }
603 return output;
604 }
605
609 internal static UnderlineValue GetUnderlineEnum(string name)
610 {
611 UnderlineValue output = UnderlineValue.None;
612 switch (name)
613 {
614 case "double": output = UnderlineValue.Double; break;
615 case "singleAccounting": output = UnderlineValue.SingleAccounting; break;
616 case "doubleAccounting": output = UnderlineValue.DoubleAccounting; break;
617 }
618 return output;
619 }
620
621 #endregion
622
623 }
624}
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:325
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.
Definition Font.cs:88
@ Subscript
Text will be rendered as subscript.
Definition Font.cs:86
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:280
override bool Equals(object obj)
Returns whether two instances are the same.
Definition Font.cs:530
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:300
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:554
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Font.cs:472
bool IsDefaultFont
Gets whether the font is equal to the default font.
Definition Font.cs:346
bool Italic
Gets or sets whether the font is italic. If true, the font is declared as italic.
Definition Font.cs:285
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:333
VerticalTextAlignValue VerticalAlign
Gets or sets the alignment of the font (Default is none).
Definition Font.cs:395
SchemeValue Scheme
Gets or sets the font scheme (Default is minor).
Definition Font.cs:373
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:319
UnderlineValue Underline
Gets or sets the underline style of the font. If set to none no underline will be applied (default).
Definition Font.cs:295
bool Strike
Gets or sets whether the font is struck through. If true, the font is declared as strike-through.
Definition Font.cs:290
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:313
float Size
Gets or sets the font size. Valid range is from 1 to 409.
Definition Font.cs:379
string Name
Gets or sets the font name (Default is Calibri).
Definition Font.cs:361
override int GetHashCode()
Returns a hash code for this instance.
Definition Font.cs:501
FontFamilyValue Family
Gets or sets the font family (Default is 2 = Swiss).
Definition Font.cs:340
Font()
Default constructor.
Definition Font.cs:404
FontFamilyValue
Enum for the font family, according to the simple type definition of W3C. Used by the Font class.
Definition Font.cs:203
@ Swiss
The specified font implements a Swiss font.
Definition Font.cs:215
@ Reserved5
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:247
@ Reserved4
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:243
@ Reserved9
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:263
@ Reserved7
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:255
@ NotApplicable
The family is not defined or not applicable.
Definition Font.cs:207
@ Reserved1
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:231
@ Modern
The specified font implements a Modern font.
Definition Font.cs:219
@ Reserved3
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:239
@ Roman
The specified font implements a Roman font.
Definition Font.cs:211
@ Decorative
The specified font implements a Decorative font.
Definition Font.cs:227
@ Reserved2
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:235
@ Reserved6
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:251
@ Reserved8
The specified font implements a not yet defined font archetype (reserved / do not use).
Definition Font.cs:259
@ Script
The specified font implements a Script font.
Definition Font.cs:223
override string ToString()
Override toString method.
Definition Font.cs:444
UnderlineValue
Enum for the style of the underline property of a stylized text, used by the Font class.
Definition Font.cs:97
@ Single
Text contains a single underline.
Definition Font.cs:100
@ SingleAccounting
Text contains a single, accounting underline.
Definition Font.cs:105
@ DoubleAccounting
Text contains a double, accounting underline.
Definition Font.cs:107
@ Double
Text contains a double underline.
Definition Font.cs:102
bool Shadow
Gets or sets whether the font has a drop shadow. If true, a shadow is rendered on the text.
Definition Font.cs:306
CharsetValue
Enum for the charset definitions of a font, used by the Font class.
Definition Font.cs:116
@ JIS
Shift JIS charset (shift_jis).
Definition Font.cs:140
@ Greek
Greek charset (windows-1253).
Definition Font.cs:160
@ Big5
Chinese Big Five charset.
Definition Font.cs:156
@ Arabic
Arabic charset (windows-1256).
Definition Font.cs:176
@ Thai
Thai charset (windows-874).
Definition Font.cs:188
@ OEM
OEM characters, not defined by ECMA-376.
Definition Font.cs:196
@ Symbols
Symbols from the private Unicode range U+FF00 to U+FFFF, to display special characters in the range o...
Definition Font.cs:132
@ Hangul
Hangul charset (ks_c_5601-1987).
Definition Font.cs:144
@ Default
Default charset (not defined more specific).
Definition Font.cs:128
@ Vietnamese
Vietnamese charset (windows-1258).
Definition Font.cs:168
@ EasternEuropean
Eastern Europe charset (windows-1250).
Definition Font.cs:192
@ GBK
GBK charset (GB-2312).
Definition Font.cs:152
@ Turkish
Turkish charset (iso-8859-9).
Definition Font.cs:164
@ Macintosh
Macintosh charset, Standard Roman.
Definition Font.cs:136
@ Hebrew
Hebrew charset (windows-1255).
Definition Font.cs:172
@ ANSI
Charset according to iso-8859-1.
Definition Font.cs:124
@ Johab
Johab charset (KSC-5601-1992).
Definition Font.cs:148
@ Russian
Russian charset (windows-1251).
Definition Font.cs:184
@ Baltic
Baltic charset (windows-1257).
Definition Font.cs:180
@ ApplicationDefined
Application-defined (any other value than the defined enum values; can be ignored).
Definition Font.cs:120
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.