File indexing completed on 2024-05-05 05:53:43

0001 /*
0002     SPDX-FileCopyrightText: 2018 Mariusz Glebocki <mglb@arccos-1.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 /* clang-format off */
0007 «*NOTE:-----------------------------------------------------------------------*»
0008 // Typing in "«" and "»" characters in some keyboard layouts (X11):
0009 //
0010 //   English/UK: AltGr+Z AltGr+X
0011 //   EurKEY:     AltGr+[ AltGr+]
0012 //   German:     AltGr+X AltGr+Y
0013 //   Polish:     AltGr+9 AltGr+0
0014 //   English/US: N/A; You can try EurKEY which extends En/US layout with extra
0015 //               characters available with AltGr[+Shift].
0016 //
0017 // Alternatively, you can use e.g. "<<<" and ">>>" and convert it to the valid
0018 // characters using sed or your editor's replace function.
0019 //
0020 // This text  will not appear in an output file.
0021 «*-----------------------------------------------------------------------:NOTE0022 //
0023 // «gen-file-warning»
0024 //
0025 // CharacterWidth.cpp file is automatically generated - do not edit it.
0026 //
0027 // To build uni2characterwidth binary, add
0028 // -DKONSOLE_BUILD_UNI2CHARACTERWIDTH=TRUE to cmake-options
0029 //
0030 // To change anything here, edit CharacterWidth.src.cpp and regenerate the file
0031 // using following command:
0032 //
0033 // «cmdline»
0034 //
0035 
0036 #include "CharacterWidth.h"
0037 
0038 struct Range {
0039     uint first, last;
0040 };
0041 /* clang-format on */
0042 
0043 struct RangeLut {
0044     int8_t width;
0045     const Range *const lut;
0046     int size;
0047 };
0048 
0049 enum {
0050     InvalidWidth = INT8_MIN,
0051 };
0052 
0053 /* clang-format off */
0054 static constexpr const int8_t DIRECT_LUT[] = {«!fmt "% d"direct-lut:
0055     «!repeat 32:«:«»,»»
0056 »»};
0057 
0058 «ranges-luts:«:
0059 static constexpr const Range «name»[] = {«!fmt "%#.6x"ranges:
0060     «!repeat 8:«:{«first»,«last»},»»
0061 »»};
0062 »»
0063 
0064 static constexpr const RangeLut RANGE_LUT_LIST[] = {«ranges-lut-list:
0065     «:{«!fmt "% d"width»», «!fmt "%-16s"name»», «size»},»
0066 »};
0067 static constexpr const int RANGE_LUT_LIST_SIZE = «ranges-lut-list-size»;
0068 /* clang-format on */
0069 
0070 int characterWidth(uint ucs4)
0071 {
0072     if (Q_LIKELY(ucs4 < sizeof(DIRECT_LUT))) {
0073         return DIRECT_LUT[ucs4];
0074     }
0075 
0076     for (auto rl = RANGE_LUT_LIST; rl->lut != nullptr; ++rl) {
0077         int l = 0;
0078         int r = rl->size - 1;
0079         while (l <= r) {
0080             const int m = (l + r) / 2;
0081             if (rl->lut[m].last < ucs4) {
0082                 l = m + 1;
0083             } else if (rl->lut[m].first > ucs4) {
0084                 r = m - 1;
0085             } else {
0086                 return rl->width;
0087             }
0088         }
0089     }
0090 
0091     return RANGE_LUT_LIST[RANGE_LUT_LIST_SIZE - 1].width;
0092 }