File: termpaint_char_width.h

package info (click to toggle)
termpaint 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,740 kB
  • sloc: cpp: 40,344; ansic: 10,323; python: 402; sh: 36; makefile: 14
file content (74 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: BSL-1.0
#define NEW_WIDTH(num, width) ((num << 2) | (((unsigned)width) & 3))

#include "charclassification.inc"
#include "charclassification_konsole_2018.inc"
#include "charclassification_konsole_2022.inc"

#undef NEW_WIDTH

typedef struct termpaintp_width_ {
    const uint16_t* termpaint_char_width_offsets;
    const uint16_t* termpaint_char_width_data;
} termpaintp_width;

static const termpaintp_width termpaintp_char_width_default = {
    .termpaint_char_width_offsets = termpaint_char_width_offsets_default,
    .termpaint_char_width_data = termpaint_char_width_data_default
};

static const termpaintp_width termpaintp_char_width_konsole2018 = {
    .termpaint_char_width_offsets = termpaint_char_width_offsets_konsole_2018,
    .termpaint_char_width_data = termpaint_char_width_data_konsole_2018
};

static const termpaintp_width termpaintp_char_width_konsole2022 = {
    .termpaint_char_width_offsets = termpaint_char_width_offsets_konsole_2022,
    .termpaint_char_width_data = termpaint_char_width_data_konsole_2022
};

static int termpaintp_char_width(const termpaintp_width *table, int ch) {
    if (ch >= 0x10ffff) {
        // outside of unicode, assume narrow
        return 1;
    }

    const uint16_t* termpaint_char_width_offsets = table->termpaint_char_width_offsets;
    const uint16_t* termpaint_char_width_data = table->termpaint_char_width_data;

    int low = termpaint_char_width_offsets[ch >> 14];
    int high = termpaint_char_width_offsets[(ch >> 14) + 1] - 1;

    int search = (ch & 0x3fff) << 2;

    while (1) {
        if (low == high) {
            if ((termpaint_char_width_data[low] & 0xfffc) > search) {
                // this is save as each section always starts with data for 0
                --low;
            }
            int val = termpaint_char_width_data[low] & 3;
            if (val == 3) {
                return -1;
            }
            return val;
        }

        int mid = low + (high - low) / 2;

        if (termpaint_char_width_data[mid] < search) {
            low = mid + 1;
        } else {
            if ((termpaint_char_width_data[mid] & 0xfffc) == search) {
                int val = termpaint_char_width_data[mid] & 3;
                if (val == 3) {
                    return -1;
                }
                return val;
            } else {
                high = mid;
            }
        }

    }
}