|
| 1 | +import { exports, string_to_ascii_ptr, utf8_ptr_to_string } from "./helpers"; |
| 2 | + |
| 3 | +/** |
| 4 | + * The direction of a text segment or buffer. |
| 5 | + * |
| 6 | + * A segment can also be tested for horizontal or vertical orientation |
| 7 | + * (irrespective of specific direction). |
| 8 | + */ |
| 9 | +export class Direction { |
| 10 | + /** Initial, unset direction. */ |
| 11 | + static readonly INVALID = new Direction(0); |
| 12 | + /** Text is set horizontally from left to right. */ |
| 13 | + static readonly LTR = new Direction(4); |
| 14 | + /** Text is set horizontally from right to left. */ |
| 15 | + static readonly RTL = new Direction(5); |
| 16 | + /** Text is set vertically from top to bottom. */ |
| 17 | + static readonly TTB = new Direction(6); |
| 18 | + /** Text is set vertically from bottom to top. */ |
| 19 | + static readonly BTT = new Direction(7); |
| 20 | + |
| 21 | + readonly value: number; |
| 22 | + |
| 23 | + /** |
| 24 | + * Converts a string to a Direction. Matching is loose and case-insensitive; |
| 25 | + * the first letter determines the direction (`l`/`L`: LTR, `r`/`R`: RTL, |
| 26 | + * `t`/`T`: TTB, `b`/`B`: BTT). Other strings yield `Direction.INVALID`. |
| 27 | + * @param name A string like `"ltr"`, `"rtl"`, `"ttb"`, or `"btt"`. |
| 28 | + */ |
| 29 | + constructor(name: string); |
| 30 | + /** @internal Wrap an existing hb_direction_t. */ |
| 31 | + constructor(existingValue: number); |
| 32 | + constructor(arg: string | number) { |
| 33 | + if (typeof arg === "number") { |
| 34 | + this.value = arg; |
| 35 | + } else { |
| 36 | + const strPtr = string_to_ascii_ptr(arg); |
| 37 | + this.value = exports.hb_direction_from_string(strPtr.ptr, -1); |
| 38 | + strPtr.free(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Converts the Direction to a string. |
| 44 | + * @returns A string like `"ltr"`, `"rtl"`, `"ttb"`, `"btt"`, or `"invalid"`. |
| 45 | + */ |
| 46 | + toString(): string { |
| 47 | + return utf8_ptr_to_string(exports.hb_direction_to_string(this.value)); |
| 48 | + } |
| 49 | +} |
0 commit comments