Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/obj_bin_transform_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import { assert, FeatureTest } from "../shared/util.js";
import {
CSS_FONT_INFO,
FONT_INFO,
InfoUtils,
PATTERN_INFO,
SYSTEM_FONT_INFO,
} from "../shared/obj_bin_transform_utils.js";

function compileCssFontInfo(info) {
const encoder = new TextEncoder();
const { encoder } = InfoUtils;
const encodedStrings = {};
let stringsLength = 0;
for (const prop of CSS_FONT_INFO.strings) {
Expand All @@ -48,7 +49,7 @@ function compileCssFontInfo(info) {
}

function compileSystemFontInfo(info) {
const encoder = new TextEncoder();
const { encoder } = InfoUtils;
const encodedStrings = {};
let stringsLength = 0;
for (const prop of SYSTEM_FONT_INFO.strings) {
Expand Down Expand Up @@ -106,7 +107,7 @@ function compileFontInfo(font) {
? compileCssFontInfo(font.cssFontInfo)
: null;

const encoder = new TextEncoder();
const { encoder } = InfoUtils;
const encodedStrings = {};
let stringsLength = 0;
for (const prop of FONT_INFO.strings) {
Expand Down
27 changes: 10 additions & 17 deletions src/display/obj_bin_transform_display.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import { assert, BBOX_INIT, FeatureTest, Util } from "../shared/util.js";
import {
CSS_FONT_INFO,
FONT_INFO,
InfoUtils,
PATTERN_INFO,
SYSTEM_FONT_INFO,
} from "../shared/obj_bin_transform_utils.js";

class CssFontInfo {
#buffer;

#decoder = new TextDecoder();

#view;

constructor(buffer) {
Expand All @@ -35,14 +34,13 @@ class CssFontInfo {

#readString(index) {
assert(index < CSS_FONT_INFO.strings.length, "Invalid string index");
const { decoder } = InfoUtils;
let offset = 0;
for (let i = 0; i < index; i++) {
offset += this.#view.getUint32(offset) + 4;
}
const length = this.#view.getUint32(offset);
return this.#decoder.decode(
new Uint8Array(this.#buffer, offset + 4, length)
);
return decoder.decode(new Uint8Array(this.#buffer, offset + 4, length));
}

get fontFamily() {
Expand All @@ -61,8 +59,6 @@ class CssFontInfo {
class SystemFontInfo {
#buffer;

#decoder = new TextDecoder();

#view;

constructor(buffer) {
Expand All @@ -76,14 +72,13 @@ class SystemFontInfo {

#readString(index) {
assert(index < SYSTEM_FONT_INFO.strings.length, "Invalid string index");
const { decoder } = InfoUtils;
let offset = 5;
for (let i = 0; i < index; i++) {
offset += this.#view.getUint32(offset) + 4;
}
const length = this.#view.getUint32(offset);
return this.#decoder.decode(
new Uint8Array(this.#buffer, offset + 4, length)
);
return decoder.decode(new Uint8Array(this.#buffer, offset + 4, length));
}

get css() {
Expand All @@ -103,15 +98,16 @@ class SystemFontInfo {
}

get style() {
const { decoder } = InfoUtils;
let offset = 1;
offset += 4 + this.#view.getUint32(offset);
const styleLength = this.#view.getUint32(offset);
const style = this.#decoder.decode(
const style = decoder.decode(
new Uint8Array(this.#buffer, offset + 4, styleLength)
);
offset += 4 + styleLength;
const weightLength = this.#view.getUint32(offset);
const weight = this.#decoder.decode(
const weight = decoder.decode(
new Uint8Array(this.#buffer, offset + 4, weightLength)
);
return { style, weight };
Expand All @@ -121,8 +117,6 @@ class SystemFontInfo {
class FontInfo {
#buffer;

#decoder = new TextDecoder();

#view;

constructor({ buffer, extra }) {
Expand Down Expand Up @@ -242,14 +236,13 @@ class FontInfo {

#readString(index) {
assert(index < FONT_INFO.strings.length, "Invalid string index");
const { decoder } = InfoUtils;
let offset = FONT_INFO.OFFSET_STRINGS + 4;
for (let i = 0; i < index; i++) {
offset += this.#view.getUint32(offset) + 4;
}
const length = this.#view.getUint32(offset);
return this.#decoder.decode(
new Uint8Array(this.#buffer, offset + 4, length)
);
return decoder.decode(new Uint8Array(this.#buffer, offset + 4, length));
}

get fallbackName() {
Expand Down
14 changes: 13 additions & 1 deletion src/shared/obj_bin_transform_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

import { shadow } from "./util.js";

class CSS_FONT_INFO {
static strings = ["fontFamily", "fontWeight", "italicAngle"];
}
Expand Down Expand Up @@ -68,4 +70,14 @@ class PATTERN_INFO {
static N_FIGURES = 16; // number of figures
}

export { CSS_FONT_INFO, FONT_INFO, PATTERN_INFO, SYSTEM_FONT_INFO };
class InfoUtils {
static get decoder() {
return shadow(this, "decoder", new TextDecoder());
}

static get encoder() {
return shadow(this, "encoder", new TextEncoder());
}
}

export { CSS_FONT_INFO, FONT_INFO, InfoUtils, PATTERN_INFO, SYSTEM_FONT_INFO };
7 changes: 4 additions & 3 deletions test/unit/obj_bin_transform_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
SystemFontInfo,
} from "../../src/display/obj_bin_transform_display.js";
import { FeatureTest } from "../../src/shared/util.js";
import { InfoUtils } from "../../src/shared/obj_bin_transform_utils.js";

describe("obj_bin_transform", function () {
describe("Font data", function () {
Expand Down Expand Up @@ -80,7 +81,7 @@ describe("obj_bin_transform", function () {
describe("font data serialization and deserialization", function () {
describe("CssFontInfo", function () {
it("must roundtrip correctly for CssFontInfo", function () {
const encoder = new TextEncoder();
const { encoder } = InfoUtils;
let sizeEstimate = 0;
for (const string of ["Sample Family", "not a number", "angle"]) {
sizeEstimate += 4 + encoder.encode(string).length;
Expand All @@ -97,7 +98,7 @@ describe("obj_bin_transform", function () {

describe("SystemFontInfo", function () {
it("must roundtrip correctly for SystemFontInfo", function () {
const encoder = new TextEncoder();
const { encoder } = InfoUtils;
let sizeEstimate = 1 + 4;
for (const string of [
"some string",
Expand Down Expand Up @@ -127,7 +128,7 @@ describe("obj_bin_transform", function () {
describe("FontInfo", function () {
it("must roundtrip correctly for FontInfo", function () {
let sizeEstimate = 92; // fixed offset until the strings
const encoder = new TextEncoder();
const { encoder } = InfoUtils;
sizeEstimate += 4 + 4 * (4 + encoder.encode("string").length);
sizeEstimate += 4 + 4; // cssFontInfo and systemFontInfo
sizeEstimate += 4 + fontInfo.data.length;
Expand Down