Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <wchar.h> |
| 30 | |
| 31 | #include "private/icu.h" |
| 32 | |
| 33 | int wcwidth(wchar_t wc) { |
| 34 | // Fast-path ASCII. |
| 35 | if (wc >= 0x20 && wc < 0x7f) return 1; |
| 36 | |
| 37 | // ASCII NUL is a special case. |
| 38 | if (wc == 0) return 0; |
| 39 | |
| 40 | // C0. |
| 41 | if (wc < ' ' || (wc >= 0x7f && wc <= 0xa0)) return -1; |
| 42 | |
| 43 | // Now for the i18n part. This isn't defined or standardized, so a lot of the choices are |
| 44 | // pretty arbitrary. See https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c for more details. |
| 45 | |
| 46 | // Fancy unicode control characters? |
| 47 | switch (__icu_charType(wc)) { |
| 48 | case -1: |
| 49 | // No icu4c available; give up. |
| 50 | return -1; |
| 51 | case U_CONTROL_CHAR: |
| 52 | return -1; |
| 53 | case U_NON_SPACING_MARK: |
| 54 | case U_ENCLOSING_MARK: |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 55 | return 0; |
Elliott Hughes | 20a9f99 | 2024-05-29 21:45:51 +0000 | [diff] [blame^] | 56 | case U_FORMAT_CHAR: |
| 57 | // A special case for soft hyphen (U+00AD) to match historical practice. |
| 58 | // See the tests for more commentary. |
| 59 | return (wc == 0x00ad) ? 1 : 0; |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 60 | } |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 20a9f99 | 2024-05-29 21:45:51 +0000 | [diff] [blame^] | 62 | // Medial and final jamo render as zero width when used correctly, |
| 63 | // so we handle them specially rather than relying on East Asian Width. |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 64 | switch (__icu_getIntPropertyValue(wc, UCHAR_HANGUL_SYLLABLE_TYPE)) { |
| 65 | case U_HST_VOWEL_JAMO: |
| 66 | case U_HST_TRAILING_JAMO: |
| 67 | return 0; |
| 68 | case U_HST_LEADING_JAMO: |
| 69 | case U_HST_LV_SYLLABLE: |
| 70 | case U_HST_LVT_SYLLABLE: |
| 71 | return 2; |
| 72 | } |
| 73 | |
Elliott Hughes | 20a9f99 | 2024-05-29 21:45:51 +0000 | [diff] [blame^] | 74 | // Hangeul choseong filler U+115F is default ignorable, so we check default |
| 75 | // ignorability only after we've already handled Hangeul jamo above. |
| 76 | if (__icu_hasBinaryProperty(wc, UCHAR_DEFAULT_IGNORABLE_CODE_POINT, nullptr)) return 0; |
| 77 | |
| 78 | // A few weird special cases where EastAsianWidth is not helpful for us. |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 79 | if (wc >= 0x3248 && wc <= 0x4dff) { |
| 80 | // Circled two-digit CJK "speed sign" numbers. EastAsianWidth is ambiguous, |
| 81 | // but wide makes more sense. |
| 82 | if (wc <= 0x324f) return 2; |
| 83 | // Hexagrams. EastAsianWidth is neutral, but wide seems better. |
| 84 | if (wc >= 0x4dc0) return 2; |
| 85 | } |
| 86 | |
| 87 | // The EastAsianWidth property is at least defined by the Unicode standard! |
Elliott Hughes | 20a9f99 | 2024-05-29 21:45:51 +0000 | [diff] [blame^] | 88 | // https://www.unicode.org/reports/tr11/ |
Elliott Hughes | c41b560 | 2017-07-27 17:08:08 -0700 | [diff] [blame] | 89 | switch (__icu_getIntPropertyValue(wc, UCHAR_EAST_ASIAN_WIDTH)) { |
| 90 | case U_EA_AMBIGUOUS: |
| 91 | case U_EA_HALFWIDTH: |
| 92 | case U_EA_NARROW: |
| 93 | case U_EA_NEUTRAL: |
| 94 | return 1; |
| 95 | case U_EA_FULLWIDTH: |
| 96 | case U_EA_WIDE: |
| 97 | return 2; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |