| 498 | } |
| 499 | |
| 500 | public static fromJson(v: unknown): Font | undefined { |
| 501 | if (v instanceof Font) { |
| 502 | return v; |
| 503 | } |
| 504 | |
| 505 | switch (typeof v) { |
| 506 | case 'undefined': |
| 507 | return undefined; |
| 508 | case 'object': { |
| 509 | const m = v as Map<string, unknown>; |
| 510 | const families = m.get('families') as string[]; |
| 511 | // tslint:disable-next-line: no-unnecessary-type-assertion |
| 512 | const size = m.get('size')! as number; |
| 513 | const style = JsonHelper.parseEnum<FontStyle>(m.get('style'), FontStyle)!; |
| 514 | const weight = JsonHelper.parseEnum<FontWeight>(m.get('weight'), FontWeight)!; |
| 515 | return Font.withFamilyList(families, size, style, weight); |
| 516 | } |
| 517 | case 'string': { |
| 518 | const parser = new FontParser(v as string); |
| 519 | parser.parse(); |
| 520 | |
| 521 | const families: string[] = parser.families; |
| 522 | const fontSizeString: string = parser.size.toLowerCase(); |
| 523 | let fontSize: number = 0; |
| 524 | // as per https://websemantics.uk/articles/font-size-conversion/ |
| 525 | switch (fontSizeString) { |
| 526 | case 'xx-small': |
| 527 | fontSize = 7; |
| 528 | break; |
| 529 | case 'x-small': |
| 530 | fontSize = 10; |
| 531 | break; |
| 532 | case 'small': |
| 533 | case 'smaller': |
| 534 | fontSize = 13; |
| 535 | break; |
| 536 | case 'medium': |
| 537 | fontSize = 16; |
| 538 | break; |
| 539 | case 'large': |
| 540 | case 'larger': |
| 541 | fontSize = 18; |
| 542 | break; |
| 543 | case 'x-large': |
| 544 | fontSize = 24; |
| 545 | break; |
| 546 | case 'xx-large': |
| 547 | fontSize = 32; |
| 548 | break; |
| 549 | default: |
| 550 | try { |
| 551 | if (fontSizeString.endsWith('em')) { |
| 552 | fontSize = Number.parseFloat(fontSizeString.substr(0, fontSizeString.length - 2)) * 16; |
| 553 | } else if (fontSizeString.endsWith('pt')) { |
| 554 | fontSize = |
| 555 | (Number.parseFloat(fontSizeString.substr(0, fontSizeString.length - 2)) * 16.0) / |
| 556 | 12.0; |
| 557 | } else if (fontSizeString.endsWith('px')) { |