|
| 1 | +// Copyright (c) 2022, Codelessly. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE.md file. |
| 4 | + |
| 5 | +import 'package:codelessly_api/codelessly_api.dart'; |
| 6 | +import 'package:codelessly_json_annotation/codelessly_json_annotation.dart'; |
| 7 | +import 'package:equatable/equatable.dart'; |
| 8 | + |
| 9 | +import '../../codelessly_sdk.dart'; |
| 10 | +import 'privacy_base.dart'; |
| 11 | + |
| 12 | +part 'font_family.g.dart'; |
| 13 | + |
| 14 | +/// Holds information about font family. |
| 15 | +@JsonSerializable() |
| 16 | +class FontFamilyModel extends PrivacyBase { |
| 17 | + /// Unique ID of the font family. |
| 18 | + final String id; |
| 19 | + |
| 20 | + /// Name of the font family. |
| 21 | + final String name; |
| 22 | + |
| 23 | + /// Different types of font variants this family contains. |
| 24 | + final List<FontVariantModel> fontVariants; |
| 25 | + |
| 26 | + /// Creates an instance of [FontFamilyModel] with given data. |
| 27 | + const FontFamilyModel({ |
| 28 | + String? id, |
| 29 | + required this.name, |
| 30 | + required this.fontVariants, |
| 31 | + |
| 32 | + // Privacy |
| 33 | + required super.teams, |
| 34 | + required super.users, |
| 35 | + required super.roles, |
| 36 | + required super.public, |
| 37 | + }) : id = id ?? name; |
| 38 | + |
| 39 | + FontFamilyModel.private({ |
| 40 | + String? id, |
| 41 | + required this.name, |
| 42 | + required this.fontVariants, |
| 43 | + |
| 44 | + // Privacy |
| 45 | + required super.privacy, |
| 46 | + }) : id = id ?? name, |
| 47 | + super.private(); |
| 48 | + |
| 49 | + /// Factory constructor for creating a new [FontFamilyModel] instance from |
| 50 | + /// JSON data. |
| 51 | + factory FontFamilyModel.fromJson(Map<String, dynamic> json) => |
| 52 | + _$FontFamilyModelFromJson(json); |
| 53 | + |
| 54 | + /// Serializes this object into JSON data. |
| 55 | + @override |
| 56 | + Map<String, dynamic> toJson() => _$FontFamilyModelToJson(this); |
| 57 | + |
| 58 | + @override |
| 59 | + List<Object?> get props => [id, name, fontVariants]; |
| 60 | +} |
| 61 | + |
| 62 | +/// Holds information about a font variant in a font family. |
| 63 | +@JsonSerializable() |
| 64 | +class FontVariantModel extends Equatable { |
| 65 | + /// Name of the variant. Ex. regular, thin, bold, etc. |
| 66 | + final String name; |
| 67 | + |
| 68 | + /// Font weight for this variant. |
| 69 | + final FontWeightNumeric weight; |
| 70 | + |
| 71 | + /// Font style for this variant. |
| 72 | + final String style; |
| 73 | + |
| 74 | + /// Source URL of this font from where it can be downloaded/accessed. |
| 75 | + final String fontURL; |
| 76 | + |
| 77 | + /// A URL for the preview image for this font. |
| 78 | + final String previewURL; |
| 79 | + |
| 80 | + /// Creates an instance of [FontVariantModel] with given data. |
| 81 | + const FontVariantModel({ |
| 82 | + this.name = 'regular', |
| 83 | + this.weight = FontWeightNumeric.w400, |
| 84 | + this.style = 'Normal', |
| 85 | + this.fontURL = '', |
| 86 | + this.previewURL = '', |
| 87 | + }); |
| 88 | + |
| 89 | + /// Factory constructor for creating a new [FontVariantModel] instance from |
| 90 | + /// JSON data. |
| 91 | + factory FontVariantModel.fromJson(Map<String, dynamic> json) => |
| 92 | + _$FontVariantModelFromJson(json); |
| 93 | + |
| 94 | + /// Serializes this object into JSON data. |
| 95 | + Map<String, dynamic> toJson() => _$FontVariantModelToJson(this); |
| 96 | + |
| 97 | + @override |
| 98 | + List<Object?> get props => [weight, style]; |
| 99 | + |
| 100 | + /// A factory constructor that creates an instance of [FontVariantModel] by |
| 101 | + /// parsing given [name]. |
| 102 | + factory FontVariantModel.fromName(String name, |
| 103 | + {String fontURL = '', String previewURL = ''}) { |
| 104 | + FontWeightNumeric fileFontWeight = FontWeightNumeric.w400; |
| 105 | + String fileFontStyle = 'Normal'; |
| 106 | + |
| 107 | + // Convert 'extra-bold' or 'extra bold' into 'extrabold'. |
| 108 | + final String modifiedName = |
| 109 | + name.toLowerCase().replaceAll('-', '').replaceAll(' ', ''); |
| 110 | + |
| 111 | + // Check font weight. |
| 112 | + if (modifiedName.contains('thin')) { |
| 113 | + fileFontWeight = FontWeightNumeric.w100; |
| 114 | + } else if (modifiedName.contains('extralight')) { |
| 115 | + fileFontWeight = FontWeightNumeric.w200; |
| 116 | + } else if (modifiedName.contains('light')) { |
| 117 | + fileFontWeight = FontWeightNumeric.w300; |
| 118 | + } else if (modifiedName.contains('normal')) { |
| 119 | + fileFontWeight = FontWeightNumeric.w400; |
| 120 | + } else if (modifiedName.contains('regular')) { |
| 121 | + fileFontWeight = FontWeightNumeric.w400; |
| 122 | + } else if (modifiedName.contains('medium')) { |
| 123 | + fileFontWeight = FontWeightNumeric.w500; |
| 124 | + } else if (modifiedName.contains('semibold')) { |
| 125 | + fileFontWeight = FontWeightNumeric.w600; |
| 126 | + } else if (modifiedName.contains('extrabold')) { |
| 127 | + // Order matters, extrabold must come before bold. |
| 128 | + fileFontWeight = FontWeightNumeric.w800; |
| 129 | + } else if (modifiedName.contains('ultrabold')) { |
| 130 | + // Order matters, ultrabold must come before bold. |
| 131 | + fileFontWeight = FontWeightNumeric.w900; |
| 132 | + } else if (modifiedName.contains('bold')) { |
| 133 | + fileFontWeight = FontWeightNumeric.w700; |
| 134 | + } else if (modifiedName.contains('black')) { |
| 135 | + fileFontWeight = FontWeightNumeric.w900; |
| 136 | + } else if (modifiedName.contains('heavy')) { |
| 137 | + fileFontWeight = FontWeightNumeric.w900; |
| 138 | + } else if (modifiedName.contains('solid')) { |
| 139 | + // Exception for FontAwesome font. |
| 140 | + // TODO: Might need to remove this and use 'bold' for 'solid' style. |
| 141 | + fileFontWeight = FontWeightNumeric.w700; |
| 142 | + } |
| 143 | + |
| 144 | + // Check font style. |
| 145 | + if (modifiedName.contains('italic')) { |
| 146 | + fileFontStyle = 'Italic'; |
| 147 | + } |
| 148 | + |
| 149 | + if (modifiedName.contains('oblique')) { |
| 150 | + fileFontStyle = 'Oblique'; |
| 151 | + } |
| 152 | + |
| 153 | + return FontVariantModel( |
| 154 | + name: name, |
| 155 | + weight: fileFontWeight, |
| 156 | + style: fileFontStyle, |
| 157 | + fontURL: fontURL, |
| 158 | + previewURL: previewURL, |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments