cvdl-ts 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,10 @@
1
- import { Box } from "./Box";
2
1
  import { DataSchema } from "./DataSchema";
3
- import { Elem, SectionLayout } from "./Layout";
4
2
  import { LayoutSchema } from "./LayoutSchema";
5
3
  import { Storage } from "./Storage";
6
4
  import { Resume } from "./Resume";
7
5
  import { ResumeLayout } from "./ResumeLayout";
8
6
  import * as fontkit from 'fontkit';
7
+ import { Layout } from ".";
9
8
  export type ElementPath = {
10
9
  tag: 'none';
11
10
  } | {
@@ -21,14 +20,6 @@ export type ElementPath = {
21
20
  item: number;
22
21
  field: string;
23
22
  };
24
- export declare class ElementBox {
25
- bounding_box: Box;
26
- elements: [Box, Elem][];
27
- path?: ElementPath;
28
- constructor(bounding_box: Box, elements: [Box, Elem][]);
29
- move_y_by(y: number): ElementBox;
30
- move_x_by(x: number): ElementBox;
31
- }
32
23
  export type RenderProps = {
33
24
  resume: Resume;
34
25
  layout_schemas: LayoutSchema[];
@@ -43,4 +34,4 @@ export declare class FontDict {
43
34
  load_fonts_from_schema(schema: LayoutSchema, storage: Storage): Promise<void>;
44
35
  get_font(name: string): fontkit.Font;
45
36
  }
46
- export declare function render({ resume, layout_schemas, data_schemas, resume_layout, storage, fontDict }: RenderProps): Promise<[FontDict, ElementBox[][], SectionLayout[]]>;
37
+ export declare function render({ resume, layout_schemas, data_schemas, resume_layout, storage, fontDict }: RenderProps): Promise<[FontDict, Layout.RenderedLayout[]]>;
package/dist/AnyLayout.js CHANGED
@@ -23,45 +23,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.render = exports.FontDict = exports.ElementBox = void 0;
26
+ exports.render = exports.FontDict = void 0;
27
27
  const ResumeLayout_1 = require("./ResumeLayout");
28
28
  const fontkit = __importStar(require("fontkit"));
29
- class ElementBox {
30
- constructor(bounding_box, elements) {
31
- this.bounding_box = bounding_box;
32
- this.elements = elements;
33
- this.path = { tag: 'none' };
34
- }
35
- move_y_by(y) {
36
- this.bounding_box = this.bounding_box.move_y_by(y);
37
- this.elements = this
38
- .elements
39
- .map(([b, e]) => ([b.move_y_by(y), e]));
40
- return this;
41
- }
42
- move_x_by(x) {
43
- this.bounding_box = this.bounding_box.move_x_by(x);
44
- this.elements = this
45
- .elements
46
- .map(([b, e]) => ([b.move_x_by(x), e]));
47
- return this;
48
- }
49
- }
50
- exports.ElementBox = ElementBox;
29
+ const _1 = require(".");
51
30
  class FontDict {
52
31
  constructor() {
53
32
  this.fonts = new Map();
54
33
  }
55
34
  async load_fonts_from_schema(schema, storage) {
56
35
  for (const font of schema.fonts()) {
57
- console.log(`Loading font ${font.full_name()}`);
58
- if (this.fonts.has(font.full_name())) {
59
- console.log(`Font ${font.full_name()} is already loaded`);
36
+ const fontName = _1.Font.full_name(font);
37
+ console.log(`Loading font ${fontName}`);
38
+ if (this.fonts.has(fontName)) {
39
+ console.log(`Font ${fontName} is already loaded`);
60
40
  continue;
61
41
  }
62
42
  const font_data = await storage.load_font(font);
63
43
  const fontkit_font = fontkit.create(font_data);
64
- this.fonts.set(font.full_name(), fontkit_font);
44
+ this.fonts.set(fontName, fontkit_font);
65
45
  }
66
46
  }
67
47
  get_font(name) {
@@ -75,7 +55,6 @@ class FontDict {
75
55
  exports.FontDict = FontDict;
76
56
  async function render({ resume, layout_schemas, data_schemas, resume_layout, storage, fontDict }) {
77
57
  // Each box contains a set of elements(positioned by 0x0 and projected into its bounding box)
78
- const boxes = [];
79
58
  const font_dict = fontDict !== null && fontDict !== void 0 ? fontDict : new FontDict();
80
59
  // Compute the total usable width by subtracting the margins from the document width
81
60
  const width = resume_layout.width - (resume_layout.margin.left + resume_layout.margin.right);
@@ -84,7 +63,9 @@ async function render({ resume, layout_schemas, data_schemas, resume_layout, sto
84
63
  ? width
85
64
  : (width - (0, ResumeLayout_1.vertical_margin)(resume_layout.column_type) / 2.0);
86
65
  const layouts = [];
66
+ console.error("Rendering sections...");
87
67
  for (const section of resume.sections) {
68
+ console.error("Print section:", section);
88
69
  // Render Section Header
89
70
  // 1. Find the layout schema for the section
90
71
  console.info("Computing section: ", section.section_name);
@@ -104,27 +85,18 @@ async function render({ resume, layout_schemas, data_schemas, resume_layout, sto
104
85
  }
105
86
  start_time = Date.now();
106
87
  // 3. Render the header
107
- const [result, layout] = layout_schema
108
- .header_layout_schema
109
- // .copy()
110
- .instantiate(section.data)
111
- .normalize(column_width, font_dict)
112
- .compute_boxes(font_dict);
113
- console.error("LSH: ", layout);
88
+ const layout = _1.Layout.computeBoxes(_1.Layout.normalize(_1.Layout.instantiate(layout_schema.header_layout_schema, section.data), column_width, font_dict), font_dict);
89
+ console.error("Header is computed");
114
90
  layouts.push(layout);
115
91
  end_time = Date.now();
116
92
  console.info(`Header rendering time: ${end_time - start_time}ms for section ${section.section_name}`);
117
- result.path = {
118
- tag: 'section',
119
- section: section.section_name
120
- };
121
- boxes.push(result);
122
93
  start_time = Date.now();
123
94
  // Render Section Items
124
95
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
125
96
  // @ts-nocheck
126
97
  for (const [index, item] of section.items.entries()) {
127
98
  console.log("Computing item");
99
+ console.error("Item:", item);
128
100
  // 1. Find the layout schema for the section
129
101
  const layout_schema = layout_schemas
130
102
  .find((s) => s.schema_name == section.layout_schema);
@@ -138,41 +110,13 @@ async function render({ resume, layout_schemas, data_schemas, resume_layout, sto
138
110
  const _data_schema = data_schemas
139
111
  .find((s) => s.schema_name == section.data_schema);
140
112
  // 3. Render the item
141
- const [result, layout] = layout_schema
142
- .item_layout_schema
143
- // .copy()
144
- .instantiate(item.fields)
145
- .normalize(column_width, font_dict)
146
- .compute_boxes(font_dict);
147
- console.error("LSS: ", layout);
113
+ const layout = _1.Layout.computeBoxes(_1.Layout.normalize(_1.Layout.instantiate(layout_schema.item_layout_schema, item.fields), column_width, font_dict), font_dict);
148
114
  layouts.push(layout);
149
- result.path = {
150
- tag: 'item',
151
- section: section.section_name,
152
- item: index
153
- };
154
- boxes.push(result);
155
115
  }
156
116
  end_time = Date.now();
157
117
  console.info(`Item rendering time: ${end_time - start_time}ms for section ${section.section_name}`);
158
118
  }
159
- let current_y = resume_layout.margin.top;
160
- let current_x = resume_layout.margin.left;
161
- const pages = [];
162
- pages.push([]);
163
- for (const box of boxes) {
164
- if (current_y + box.bounding_box.height() > resume_layout.height) {
165
- current_y = resume_layout.margin.top;
166
- current_x += column_width + (0, ResumeLayout_1.vertical_margin)(resume_layout.column_type);
167
- if (current_x > width) {
168
- pages.push([]);
169
- current_x = resume_layout.margin.left;
170
- }
171
- }
172
- pages[pages.length - 1].push(box.move_y_by(current_y).move_x_by(current_x));
173
- current_y += box.bounding_box.height();
174
- }
175
119
  console.log("Position calculations are completed.");
176
- return [font_dict, pages, layouts];
120
+ return [font_dict, layouts];
177
121
  }
178
122
  exports.render = render;
package/dist/Elem.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ import * as Font from "./Font";
2
+ import { Alignment, Layout, Margin, Width } from ".";
3
+ import { Color } from "./Layout";
4
+ import { FontDict } from "./AnyLayout";
5
+ import * as Row from "./Row";
6
+ import { ItemContent } from "./Resume";
7
+ export type t = {
8
+ tag: "Elem";
9
+ item: string;
10
+ url: string | null;
11
+ is_ref: boolean;
12
+ is_fill: boolean;
13
+ text_width: Width.t;
14
+ font: Font.t;
15
+ margin: Margin.t;
16
+ alignment: Alignment.t;
17
+ width: Width.t;
18
+ background_color: Color;
19
+ };
20
+ type Elem = t;
21
+ export declare function elem(item: string, url: string | null, is_ref: boolean, is_fill: boolean, text_width: Width.t, font: Font.t, margin: Margin.t, alignment: Alignment.t, width: Width.t, background_color: Color): Elem;
22
+ export declare function copy(e: Elem): {
23
+ tag: "Elem";
24
+ item: string;
25
+ url: string;
26
+ is_ref: boolean;
27
+ is_fill: boolean;
28
+ text_width: Width.t;
29
+ font: Font.t;
30
+ margin: Margin.t;
31
+ alignment: Alignment.t;
32
+ width: Width.t;
33
+ background_color: Layout.Color;
34
+ };
35
+ export declare function default_(): Elem;
36
+ export type Optional<T> = {
37
+ [P in keyof T]?: T[P];
38
+ };
39
+ export declare function with_(e: Elem, w: Optional<Elem>): Elem;
40
+ export declare function from(w: Optional<Elem>): Elem;
41
+ export declare function withItem(e: Elem, item: string): Elem;
42
+ export declare function withUrl(e: Elem, url: string | null): Elem;
43
+ export declare function withIsRef(e: Elem, is_ref: boolean): Elem;
44
+ export declare function asRef(e: Elem): Elem;
45
+ export declare function withIsFill(e: Elem, is_fill: boolean): Elem;
46
+ export declare function withTextWidth(e: Elem, text_width: Width.t): Elem;
47
+ export declare function withFont(e: Elem, font: Font.t): Elem;
48
+ export declare function withMargin(e: Elem, margin: Margin.t): Elem;
49
+ export declare function withAlignment(e: Elem, alignment: Alignment.t): Elem;
50
+ export declare function withWidth(e: Elem, width: Width.t): Elem;
51
+ export declare function withBackgroundColor(e: Elem, background_color: Color): Elem;
52
+ export declare function scaleWidth(e: Elem, scale: number): Elem;
53
+ export declare function fillFonts(e: Elem, fonts: FontDict): Elem;
54
+ export declare function justifiedLines(e: Elem, lines: Elem[], font_dict: FontDict): Row.t[];
55
+ export declare function break_lines(e: Elem, font_dict: FontDict): Layout.t[];
56
+ export declare function boundWidth(e: Elem, width: number): Elem;
57
+ export declare function instantiate(e: Elem, section: Map<string, ItemContent>): Elem;
58
+ export {};
package/dist/Elem.js ADDED
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.instantiate = exports.boundWidth = exports.break_lines = exports.justifiedLines = exports.fillFonts = exports.scaleWidth = exports.withBackgroundColor = exports.withWidth = exports.withAlignment = exports.withMargin = exports.withFont = exports.withTextWidth = exports.withIsFill = exports.asRef = exports.withIsRef = exports.withUrl = exports.withItem = exports.from = exports.with_ = exports.default_ = exports.copy = exports.elem = void 0;
27
+ const Font = __importStar(require("./Font"));
28
+ const _1 = require(".");
29
+ const Row_1 = require("./Row");
30
+ const Resume_1 = require("./Resume");
31
+ function elem(item, url, is_ref, is_fill, text_width, font, margin, alignment, width, background_color) {
32
+ return {
33
+ tag: "Elem",
34
+ item,
35
+ url,
36
+ is_ref,
37
+ is_fill,
38
+ text_width,
39
+ font,
40
+ margin,
41
+ alignment,
42
+ width,
43
+ background_color,
44
+ };
45
+ }
46
+ exports.elem = elem;
47
+ function copy(e) {
48
+ return { ...e };
49
+ }
50
+ exports.copy = copy;
51
+ function default_() {
52
+ return {
53
+ tag: "Elem",
54
+ item: "",
55
+ url: null,
56
+ is_ref: false,
57
+ is_fill: false,
58
+ text_width: _1.Width.default_(),
59
+ font: Font.default_(),
60
+ margin: _1.Margin.default_(),
61
+ alignment: _1.Alignment.default_(),
62
+ width: _1.Width.default_(),
63
+ background_color: "Transparent",
64
+ };
65
+ }
66
+ exports.default_ = default_;
67
+ function with_(e, w) {
68
+ return { ...e, ...w };
69
+ }
70
+ exports.with_ = with_;
71
+ function from(w) {
72
+ return { ...default_(), ...w };
73
+ }
74
+ exports.from = from;
75
+ function withItem(e, item) {
76
+ return { ...e, item };
77
+ }
78
+ exports.withItem = withItem;
79
+ function withUrl(e, url) {
80
+ return { ...e, url };
81
+ }
82
+ exports.withUrl = withUrl;
83
+ function withIsRef(e, is_ref) {
84
+ return { ...e, is_ref };
85
+ }
86
+ exports.withIsRef = withIsRef;
87
+ function asRef(e) {
88
+ return withIsRef(e, true);
89
+ }
90
+ exports.asRef = asRef;
91
+ function withIsFill(e, is_fill) {
92
+ return { ...e, is_fill };
93
+ }
94
+ exports.withIsFill = withIsFill;
95
+ function withTextWidth(e, text_width) {
96
+ return { ...e, text_width };
97
+ }
98
+ exports.withTextWidth = withTextWidth;
99
+ function withFont(e, font) {
100
+ return { ...e, font };
101
+ }
102
+ exports.withFont = withFont;
103
+ function withMargin(e, margin) {
104
+ return { ...e, margin };
105
+ }
106
+ exports.withMargin = withMargin;
107
+ function withAlignment(e, alignment) {
108
+ return { ...e, alignment };
109
+ }
110
+ exports.withAlignment = withAlignment;
111
+ function withWidth(e, width) {
112
+ return { ...e, width };
113
+ }
114
+ exports.withWidth = withWidth;
115
+ function withBackgroundColor(e, background_color) {
116
+ return { ...e, background_color };
117
+ }
118
+ exports.withBackgroundColor = withBackgroundColor;
119
+ function scaleWidth(e, scale) {
120
+ return withWidth(e, _1.Width.scale(e.width, scale));
121
+ }
122
+ exports.scaleWidth = scaleWidth;
123
+ function fillFonts(e, fonts) {
124
+ const text_width_with_font = Font.get_width(e.font, e.item, fonts);
125
+ if (e.is_fill) {
126
+ return withTextWidth(withWidth(e, _1.Width.absolute(Math.min(_1.Width.get_fixed_unchecked(e.width), text_width_with_font))), _1.Width.absolute(text_width_with_font));
127
+ }
128
+ else {
129
+ return withTextWidth(e, _1.Width.absolute(text_width_with_font));
130
+ }
131
+ }
132
+ exports.fillFonts = fillFonts;
133
+ function justifiedLines(e, lines, font_dict) {
134
+ const rowLines = [];
135
+ for (const line of lines.slice(0, -1)) {
136
+ const words = line.item.split(/\s+/);
137
+ const r = (0, Row_1.row)([], line.margin, line.alignment, line.width, false, false);
138
+ words.forEach(word => {
139
+ const word_width = Font.get_width(e.font, word, font_dict);
140
+ r.elements.push(elem(word, null, false, false, _1.Width.absolute(word_width), this.font, _1.Margin.default_(), _1.Alignment.default_(), _1.Width.absolute(word_width), this.background_color));
141
+ });
142
+ rowLines.push(Row_1.row);
143
+ }
144
+ rowLines.push((0, Row_1.row)([withAlignment(lines[lines.length - 1], "Left")], lines[0].margin, "Left", lines[0].width, false, false));
145
+ return rowLines;
146
+ }
147
+ exports.justifiedLines = justifiedLines;
148
+ function break_lines(e, font_dict) {
149
+ if (_1.Width.get_fixed_unchecked(e.text_width) <= _1.Width.get_fixed_unchecked(e.width)) {
150
+ return [e];
151
+ }
152
+ const lines = [];
153
+ // todo: I'm sure this implementation is pretty buggy. Note to future me, fix
154
+ // this.
155
+ const words = e.item.split(/\s+/);
156
+ const widths = words.map((word) => Font.get_width(e.font, word, font_dict));
157
+ const space_width = Font.get_width(e.font, " ", font_dict);
158
+ let start = 0;
159
+ let width = widths[0];
160
+ const max_width = _1.Width.get_fixed_unchecked(e.width);
161
+ for (let i = 1; i < words.length; i++) {
162
+ const candidate_width = width + space_width + widths[i];
163
+ if (candidate_width > max_width) {
164
+ const line = words.slice(start, i).join(" ");
165
+ const line_width = Font.get_width(e.font, line, font_dict);
166
+ lines.push(withTextWidth(withItem(e, line), _1.Width.absolute(line_width)));
167
+ start = i;
168
+ width = widths[i];
169
+ }
170
+ else {
171
+ width += space_width + widths[i];
172
+ }
173
+ }
174
+ const line = words.slice(start).join(" ");
175
+ const line_width = Font.get_width(e.font, line, font_dict);
176
+ lines.push(withTextWidth(withItem(e, line), _1.Width.absolute(line_width)));
177
+ if (e.alignment === "Justified") {
178
+ return justifiedLines(e, lines, font_dict);
179
+ }
180
+ return lines;
181
+ }
182
+ exports.break_lines = break_lines;
183
+ function boundWidth(e, width) {
184
+ if (!_1.Width.is_fill(e.width)) {
185
+ return withIsFill(withWidth(e, _1.Width.absolute(Math.min(_1.Width.get_fixed_unchecked(e.width), width))), false);
186
+ }
187
+ else {
188
+ return withIsFill(withWidth(e, _1.Width.absolute(width)), true);
189
+ }
190
+ }
191
+ exports.boundWidth = boundWidth;
192
+ function instantiate(e, section) {
193
+ if (!e.is_ref) {
194
+ return e;
195
+ }
196
+ const text = section.get(e.item);
197
+ if (text === undefined) {
198
+ return withIsRef(withItem(e, ""), false);
199
+ }
200
+ else {
201
+ if (text.tag === "Url") {
202
+ return withIsRef(withUrl(withItem(e, text.value.text), text.value.url), false);
203
+ }
204
+ else {
205
+ return withIsRef(withItem(e, Resume_1.ItemContent.toString(text)), false);
206
+ }
207
+ }
208
+ }
209
+ exports.instantiate = instantiate;
@@ -4,11 +4,11 @@ import { DataSchema } from "./DataSchema";
4
4
  import { LayoutSchema } from "./LayoutSchema";
5
5
  import { ResumeLayout } from "./ResumeLayout";
6
6
  import { Storage } from "./Storage";
7
- import { Font } from "./Font";
7
+ import * as Font from "./Font";
8
8
  export declare class FileStorage implements Storage {
9
9
  dir: string;
10
10
  constructor(dir: string);
11
- load_font(font: Font): Promise<Buffer>;
11
+ load_font(font: Font.t): Promise<Buffer>;
12
12
  initiate_storage(): Promise<void>;
13
13
  list_resumes(): Promise<string[]>;
14
14
  list_data_schemas(): Promise<string[]>;
@@ -1,5 +1,28 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
3
26
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
28
  };
@@ -30,13 +53,14 @@ const Resume_1 = require("./Resume");
30
53
  const DataSchema_1 = require("./DataSchema");
31
54
  const LayoutSchema_1 = require("./LayoutSchema");
32
55
  const ResumeLayout_1 = require("./ResumeLayout");
56
+ const Font = __importStar(require("./Font"));
33
57
  class FileStorage {
34
58
  constructor(dir) {
35
59
  this.dir = "";
36
60
  this.dir = dir;
37
61
  }
38
62
  load_font(font) {
39
- return Promise.resolve(fs_1.default.readFileSync(this.dir + font.full_name() + ".ttf"));
63
+ return Promise.resolve(fs_1.default.readFileSync(this.dir + Font.full_name(font) + ".ttf"));
40
64
  }
41
65
  initiate_storage() {
42
66
  // Create data_dir/resumes if it does not exist
package/dist/Font.d.ts CHANGED
@@ -1,18 +1,19 @@
1
1
  import { FontDict } from "./AnyLayout";
2
- export declare class Font {
2
+ export type t = {
3
3
  name: string;
4
4
  size: number;
5
5
  weight: FontWeight;
6
6
  style: FontStyle;
7
7
  source: FontSource;
8
- constructor(name: string, size: number, weight: FontWeight, style: FontStyle, source: FontSource);
9
- static fromJson(json: unknown): Font;
10
- toJson(): unknown;
11
- static default_(): Font;
12
- full_name(): string;
13
- get_width(text: string, fonts: FontDict): number;
14
- get_height(fonts: FontDict): number;
15
- }
8
+ };
9
+ type Font = t;
10
+ export declare function font(name: string, size: number, weight: FontWeight, style: FontStyle, source: FontSource): Font;
11
+ export declare function fromJson(json: unknown): Font;
12
+ export declare function default_(): Font;
13
+ export declare function full_name(f: Font): string;
14
+ export declare function get_width(f: Font, text: string, fonts: FontDict): number;
15
+ export declare function get_height(f: Font, fonts: FontDict): number;
16
16
  export type FontSource = "Local" | "System" | "Remote";
17
17
  export type FontWeight = "Light" | "Medium" | "Bold";
18
18
  export type FontStyle = "Normal" | "Italic";
19
+ export {};
package/dist/Font.js CHANGED
@@ -1,47 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Font = void 0;
4
- class Font {
5
- constructor(name, size, weight, style, source) {
6
- this.name = name;
7
- this.size = size;
8
- this.weight = weight;
9
- this.style = style;
10
- this.source = source;
11
- }
12
- static fromJson(json) {
13
- if (typeof json !== "object" || json === null) {
14
- return Font.default_();
15
- }
16
- const name = ("name" in json) ? json.name : "Exo";
17
- const size = ("size" in json) ? json.size : 12;
18
- const weight = ("weight" in json) ? json.weight : "Medium";
19
- const style = ("style" in json) ? json.style : "Normal";
20
- const source = ("source" in json) ? json.source : "System";
21
- return new Font(name, size, weight, style, source);
22
- }
23
- toJson() {
24
- return {
25
- name: this.name,
26
- size: this.size,
27
- weight: this.weight,
28
- style: this.style,
29
- source: this.source
30
- };
31
- }
32
- static default_() {
33
- return new Font("Exo", 12, "Medium", "Normal", "System");
34
- }
35
- full_name() {
36
- return this.name + "-" + this.weight + (this.style === "Italic" ? "Italic" : "");
37
- }
38
- get_width(text, fonts) {
39
- const font = fonts.get_font(this.full_name());
40
- return (font.layout(text).glyphs.reduce((acc, glyph) => acc + glyph.advanceWidth, 0) / font.unitsPerEm) * this.size;
41
- }
42
- get_height(fonts) {
43
- const font = fonts.get_font(this.full_name());
44
- return (font.bbox.height / font.unitsPerEm) * this.size;
45
- }
3
+ exports.get_height = exports.get_width = exports.full_name = exports.default_ = exports.fromJson = exports.font = void 0;
4
+ function font(name, size, weight, style, source) {
5
+ return {
6
+ name,
7
+ size,
8
+ weight,
9
+ style,
10
+ source,
11
+ };
46
12
  }
47
- exports.Font = Font;
13
+ exports.font = font;
14
+ function fromJson(json) {
15
+ return {
16
+ ...default_(),
17
+ name: json["name"],
18
+ size: json["size"],
19
+ weight: json["weight"],
20
+ style: json["style"],
21
+ source: json["source"],
22
+ };
23
+ }
24
+ exports.fromJson = fromJson;
25
+ function default_() {
26
+ return {
27
+ name: "Exo",
28
+ size: 12,
29
+ weight: "Medium",
30
+ style: "Normal",
31
+ source: "System",
32
+ };
33
+ }
34
+ exports.default_ = default_;
35
+ function full_name(f) {
36
+ return f.name + "-" + f.weight + (f.style === "Italic" ? "Italic" : "");
37
+ }
38
+ exports.full_name = full_name;
39
+ function get_width(f, text, fonts) {
40
+ const font = fonts.get_font(full_name(f));
41
+ return (font.layout(text).glyphs.reduce((acc, glyph) => acc + glyph.advanceWidth, 0) / font.unitsPerEm) * f.size;
42
+ }
43
+ exports.get_width = get_width;
44
+ function get_height(f, fonts) {
45
+ const font = fonts.get_font(full_name(f));
46
+ return (font.bbox.height / font.unitsPerEm) * f.size;
47
+ }
48
+ exports.get_height = get_height;