Edit

kc3-lang/libxkbcommon/src/keysym.h

Branch :

  • Browse

    Commit

  • Author : Pierre Le Marre
    Date : 2024-02-23 17:10:15
    Hash : e83d08dd
    Message : keysyms: Fast and complete case mappings (Unicode 15.1) The current code to handle keysym case mappings is quite complex and slow. It is also incomplete, as it does not cover recent Unicode database. Finally, it does not handle title case correctly. It would be easier if we were to use only a lookup table, but a trivial implementation would lead to a huge array: the cased characters range from `U+0041` to `U+`1F189, i.e. a span of 127 304 elements. Thus we need some tricks to compress the lookup table. We based our work on the post: https://github.com/apankrat/notes/blob/3c551cb028595fd34046c5761fd12d1692576003/fast-case-conversion/README.md The compression algorithm is roughly: 1. Compute the delta between the characters and their mappings. 2. Split the delta array in chunk of a given size. 3. Rearrange the order of the chunks in order to optimize consecutive chunks overlap. 4. Create a data table with the reordered chunks and an index table that maps the original chunk index to its offset in the data table. The compression algorithm is then applied a second time to the previous index table. The complete algorithm optimizes the two chunk sizes in order to get the lowest total data size. The mappings were generated using CPython 3.12.4, PyICU 2.13, PyYaml 6.0.1 and ICU 75.1. Also: - Added explicit list of named keysyms and their case mappings. - Added benchmark for case mappings. - Rework ICU tests. Note: 13b30f4f0dccc08dfea426d73570b913596ed602 introduced a fix for sharp S `U+00DF`. With the new implementation, the *conversion* functions `xkb_keysym_to_{lower,upper}` leave it *unchanged*, while the *predicate* functions `xkb_keysym_is_{lower,upper_or_title}` produce the expected results: ```c xkb_keysym_to_upper(XKB_KEY_ssharp) == XKB_KEY_ssharp; xkb_keysym_to_lower(XKB_KEY_ssharp) == XKB_KEY_ssharp; xkb_keysym_to_lower(XKB_KEY_Ssharp) == XKB_KEY_ssharp; xkb_keysym_is_lower (XKB_KEY_ssharp) == true; xkb_keysym_is_upper_or_title(XKB_KEY_Ssharp) == true; ```

  • src/keysym.h
  • /*
     * Copyright 1985, 1987, 1990, 1998  The Open Group
     *
     * Permission is hereby granted, free of charge, to any person obtaining a
     * copy of this software and associated documentation files (the "Software"),
     * to deal in the Software without restriction, including without limitation
     * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     * and/or sell copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     *
     * Except as contained in this notice, the names of the authors or their
     * institutions shall not be used in advertising or otherwise to promote the
     * sale, use or other dealings in this Software without prior written
     * authorization from the authors.
     */
    
    /*
     * Copyright © 2009 Dan Nicholson
     *
     * Permission is hereby granted, free of charge, to any person obtaining a
     * copy of this software and associated documentation files (the "Software"),
     * to deal in the Software without restriction, including without limitation
     * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     * and/or sell copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice (including the next
     * paragraph) shall be included in all copies or substantial portions of the
     * Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     * DEALINGS IN THE SOFTWARE.
     */
    
    #ifndef KEYSYM_H
    #define KEYSYM_H
    
    #include <stdbool.h>
    #include "xkbcommon/xkbcommon.h"
    
    /*
     * NOTE: this is not defined in xkbcommon.h, because if we did, it may add
     * overhead for library user: when handling keysyms they would also need to
     * check min keysym when previously there was no reason to.
     */
    /** Minimum keysym value */
    #define XKB_KEYSYM_MIN            0x00000000
    /** Minimum keysym value assigned */
    #define XKB_KEYSYM_MIN_ASSIGNED   ((xkb_keysym_t)0x00000000)
    /** Maximum keysym value assigned */
    #define XKB_KEYSYM_MAX_ASSIGNED   0x1008ffb8
    /** Minimum keysym value with explicit name */
    #define XKB_KEYSYM_MIN_EXPLICIT   0x00000000
    /** Maximum keysym value with explicit name */
    #define XKB_KEYSYM_MAX_EXPLICIT   0x1008ffb8
    /** Count of keysym value with explicit name */
    #define XKB_KEYSYM_COUNT_EXPLICIT 2446
    /** Offset to use when converting a Unicode code point to a keysym */
    #define XKB_KEYSYM_UNICODE_OFFSET 0x01000000
    /** Minimum Unicode keysym. NOTE: code points in 0..0xff cannot be converted. */
    #define XKB_KEYSYM_UNICODE_MIN    0x01000100
    /** Maximum Unicode keysym, correspoding to the maximum Unicode code point */
    #define XKB_KEYSYM_UNICODE_MAX    0x0110ffff
    /** Unicode version used for case mappings */
    #define XKB_KEYSYM_UNICODE_VERSION { 15, 1, 0, 0 }
    /** Maximum keysym name length */
    #define XKB_KEYSYM_NAME_MAX_SIZE  27
    /** Maximum bytes to encode the Unicode representation of a keysym in UTF-8:
     * 4 bytes + NULL-terminating byte */
    #define XKB_KEYSYM_UTF8_MAX_SIZE  5
    
    bool
    xkb_keysym_is_assigned(xkb_keysym_t ks);
    
    struct xkb_keysym_iterator;
    
    struct xkb_keysym_iterator*
    xkb_keysym_iterator_new(bool explicit);
    
    struct xkb_keysym_iterator*
    xkb_keysym_iterator_unref(struct xkb_keysym_iterator *iter);
    
    bool
    xkb_keysym_iterator_next(struct xkb_keysym_iterator *iter);
    
    xkb_keysym_t
    xkb_keysym_iterator_get_keysym(struct xkb_keysym_iterator *iter);
    
    int
    xkb_keysym_iterator_get_name(struct xkb_keysym_iterator *iter,
                                 char *buffer, size_t size);
    
    bool
    xkb_keysym_iterator_is_explicitly_named(struct xkb_keysym_iterator *iter);
    
    bool
    xkb_keysym_is_lower(xkb_keysym_t keysym);
    
    bool
    xkb_keysym_is_upper_or_title(xkb_keysym_t keysym);
    
    bool
    xkb_keysym_is_keypad(xkb_keysym_t keysym);
    
    bool
    xkb_keysym_is_modifier(xkb_keysym_t keysym);
    
    #endif