Skip to content

Commit

Permalink
Handling maxp, post, vhea` versions as Version16Dot16
Browse files Browse the repository at this point in the history
Signed-off-by: Vsevolod Volkov <[email protected]>
  • Loading branch information
Vsevolod Volkov committed Apr 10, 2023
1 parent 6b9538a commit 30acdf8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/tables/maxp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as r from 'restructure';
import { version16Dot16 } from '../utils';

// maxiumum profile
export default new r.Struct({
version: r.int32,
version: version16Dot16,
numGlyphs: r.uint16, // The number of glyphs in the font
maxPoints: r.uint16, // Maximum points in a non-composite glyph
maxContours: r.uint16, // Maximum contours in a non-composite glyph
Expand Down
3 changes: 2 additions & 1 deletion src/tables/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as r from 'restructure';
import { version16Dot16 } from '../utils';

// PostScript information
export default new r.VersionedStruct(r.fixed32, {
export default new r.VersionedStruct(version16Dot16, {
header: { // these fields exist at the top of all versions
italicAngle: r.fixed32, // Italic angle in counter-clockwise degrees from the vertical.
underlinePosition: r.int16, // Suggested distance of the top of the underline from the baseline
Expand Down
4 changes: 2 additions & 2 deletions src/tables/vhea.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as r from 'restructure';
import { version16Dot16 } from '../utils';

// Vertical Header Table
export default new r.Struct({
majorVersion: r.uint16, // Major version number of the Vertical Header Table
minorVersion: r.uint16, // Minor version number of the Vertical Header Table
version: version16Dot16,
ascent: r.int16, // The vertical typographic ascender for this font
descent: r.int16, // The vertical typographic descender for this font
lineGap: r.int16, // The vertical typographic line gap for this font
Expand Down
36 changes: 36 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DecodeStream, EncodeStream } from 'restructure';

export function binarySearch(arr, cmp) {
let min = 0;
let max = arr.length - 1;
Expand Down Expand Up @@ -60,3 +62,37 @@ export function decodeBase64(base64) {

return bytes;
}

export class Version16Dot16 {
fromBuffer(buffer) {
let stream = new DecodeStream(buffer);
return this.decode(stream);
}

toBuffer(value) {
let size = this.size(value);
let buffer = new Uint8Array(size);
let stream = new EncodeStream(buffer);
this.encode(stream, value);
return buffer;
}

size() {
return 4;
}

decode(stream) {
let major = stream.readUInt16BE();
let minor = stream.readUInt16BE() >> 12;
return major + minor / 10;
}

encode(stream, val) {
let major = Math.trunc(val);
let minor = (val - major) * 10;
stream.writeUInt16BE(major);
return stream.writeUInt16BE(minor << 12);
}
}

export const version16Dot16 = new Version16Dot16();

0 comments on commit 30acdf8

Please sign in to comment.