diff --git a/COPYING.Unicode b/COPYING.Unicode deleted file mode 100644 --- a/COPYING.Unicode +++ /dev/null @@ -1,64 +0,0 @@ -This license applies to the original wcwidth.c which is used in -src/konsole_wcwidth.cpp. See that file for more info. - -EXHIBIT 1 -UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE - - Unicode Data Files include all data files under the directories - http://www.unicode.org/Public/, http://www.unicode.org/reports/, - and http://www.unicode.org/cldr/data/. Unicode Data Files do not - include PDF online code charts under the directory - http://www.unicode.org/Public/. Software includes any source code - published in the Unicode Standard or under the directories - http://www.unicode.org/Public/, http://www.unicode.org/reports/, - and http://www.unicode.org/cldr/data/. - - NOTICE TO USER: Carefully read the following legal agreement. BY - DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S - DATA FILES ("DATA FILES"), AND/OR SOFTWARE ("SOFTWARE"), YOU - UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE TERMS - AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT - DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR - SOFTWARE. - - COPYRIGHT AND PERMISSION NOTICE - - Copyright © 1991-2012 Unicode, Inc. All rights - reserved. Distributed under the Terms of Use in - http://www.unicode.org/copyright.html. - - Permission is hereby granted, free of charge, to any person - obtaining a copy of the Unicode data files and any associated - documentation (the "Data Files") or Unicode software and any - associated documentation (the "Software") to deal in the Data - Files or Software without restriction, including without - limitation the rights to use, copy, modify, merge, publish, - distribute, and/or sell copies of the Data Files or Software, and - to permit persons to whom the Data Files or Software are furnished - to do so, provided that (a) the above copyright notice(s) and this - permission notice appear with all copies of the Data Files or - Software, (b) both the above copyright notice(s) and this - permission notice appear in associated documentation, and (c) - there is clear notice in each modified Data File or in the - Software as well as in the documentation associated with the Data - File(s) or Software that the data or software has been modified. - - THE DATA FILES AND SOFTWARE ARE 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 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE - COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR - ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR - ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER - TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - PERFORMANCE OF THE DATA FILES OR SOFTWARE. - - Except as contained in this notice, the name of a copyright holder - shall not be used in advertising or otherwise to promote the sale, - use or other dealings in these Data Files or Software without - prior written authorization of the copyright holder. - - Unicode and the Unicode logo are trademarks of Unicode, Inc. in - the United States and other countries. All third party trademarks - referenced herein are the property of their respective owners. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,9 +33,10 @@ ### Security concerns about sendText and runCommand dbus methods being public option(REMOVE_SENDTEXT_RUNCOMMAND_DBUS_METHODS "Konsole: remove sendText and runCommand dbus methods" OFF) -### Font Embedder and LineFont.h +### Development tools option(KONSOLE_BUILD_FONTEMBEDDER "Konsole: build fontembedder executable" OFF) option(KONSOLE_GENERATE_LINEFONT "Konsole: regenerate LineFont file" OFF) +option(KONSOLE_BUILD_UNI2CHARACTERWIDTH "Konsole: build uni2characterwidth executable" OFF) ### Konsole source files shared between embedded terminal and main application # qdbuscpp2xml -m Session.h -o org.kde.konsole.Session.xml @@ -101,8 +102,8 @@ Vt102Emulation.cpp ZModemDialog.cpp PrintOptions.cpp - konsole_wcwidth.cpp WindowSystemInfo.cpp + CharacterWidth.cpp ${CMAKE_CURRENT_BINARY_DIR}/org.kde.konsole.Window.xml ${CMAKE_CURRENT_BINARY_DIR}/org.kde.konsole.Session.xml) diff --git a/src/Character.h b/src/Character.h --- a/src/Character.h +++ b/src/Character.h @@ -25,7 +25,7 @@ // Konsole #include "CharacterColor.h" -#include "konsole_wcwidth.h" +#include "CharacterWidth.h" // Qt #include @@ -165,7 +165,7 @@ } static int width(uint ucs4) { - return konsole_wcwidth(ucs4); + return characterWidth(ucs4); } static int stringWidth(const uint *ucs4Str, int len) { diff --git a/src/CharacterWidth.h b/src/CharacterWidth.h new file mode 100644 --- /dev/null +++ b/src/CharacterWidth.h @@ -0,0 +1,8 @@ +#ifndef CHARACTERINFO_H +#define CHARACTERINFO_H + +#include + +int characterWidth(uint ucs4); + +#endif diff --git a/src/CharacterWidth.cpp b/src/CharacterWidth.cpp new file mode 100644 --- /dev/null +++ b/src/CharacterWidth.cpp @@ -0,0 +1,159 @@ +/* + This file is part of Konsole, a terminal emulator for KDE. + + Copyright 2018 by Mariusz Glebocki + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +// +// THIS IS A GENERATED FILE. DO NOT EDIT. +// +// CharacterWidth.cpp file is automatically generated - do not edit it. +// To change anything here, edit CharacterWidth.src.cpp and regenerate the file +// using following command: +// +// uni2characterwidth -U "https://unicode.org/Public/11.0.0/ucd/UnicodeData.txt" -A "https://unicode.org/Public/11.0.0/ucd/EastAsianWidth.txt" -E "https://unicode.org/Public/emoji/11.0/emoji-data.txt" -W "tools/uni2characterwidth/overrides.txt" --ambiguous-width=1 --emoji=presentation -g "code:src/CharacterWidth.src.cpp" "src/CharacterWidth.cpp" +// + +#include "CharacterWidth.h" +#include "konsoledebug.h" +#include "konsoleprivate_export.h" + + +struct Range { + uint first, last; +}; + +struct RangeLut { + int8_t width; + const Range * const lut; + int size; +}; + +enum { + InvalidWidth = INT8_MIN, +}; + + +static constexpr const int8_t DIRECT_LUT[] = { + 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + + +static constexpr const Range LUT_NONPRINTABLE[] = { + {0x00d800,0x00dfff}, +}; + +static constexpr const Range LUT_2[] = { + {0x001100,0x00115f},{0x00231a,0x00231b},{0x002329,0x00232a},{0x0023e9,0x0023ec},{0x0023f0,0x0023f0},{0x0023f3,0x0023f3},{0x0025fd,0x0025fe},{0x002614,0x002615}, + {0x002648,0x002653},{0x00267f,0x00267f},{0x002693,0x002693},{0x0026a1,0x0026a1},{0x0026aa,0x0026ab},{0x0026bd,0x0026be},{0x0026c4,0x0026c5},{0x0026ce,0x0026ce}, + {0x0026d4,0x0026d4},{0x0026ea,0x0026ea},{0x0026f2,0x0026f3},{0x0026f5,0x0026f5},{0x0026fa,0x0026fa},{0x0026fd,0x0026fd},{0x002705,0x002705},{0x00270a,0x00270b}, + {0x002728,0x002728},{0x00274c,0x00274c},{0x00274e,0x00274e},{0x002753,0x002755},{0x002757,0x002757},{0x002795,0x002797},{0x0027b0,0x0027b0},{0x0027bf,0x0027bf}, + {0x002b1b,0x002b1c},{0x002b50,0x002b50},{0x002b55,0x002b55},{0x002e80,0x002e99},{0x002e9b,0x002ef3},{0x002f00,0x002fd5},{0x002ff0,0x002ffb},{0x003000,0x003029}, + {0x00302e,0x00303e},{0x003041,0x003096},{0x00309b,0x0030ff},{0x003105,0x00312f},{0x003131,0x00318e},{0x003190,0x0031ba},{0x0031c0,0x0031e3},{0x0031f0,0x00321e}, + {0x003220,0x003247},{0x003250,0x0032fe},{0x003300,0x004dbf},{0x004e00,0x00a48c},{0x00a490,0x00a4c6},{0x00a960,0x00a97c},{0x00ac00,0x00d7a3},{0x00f900,0x00faff}, + {0x00fe10,0x00fe19},{0x00fe30,0x00fe52},{0x00fe54,0x00fe66},{0x00fe68,0x00fe6b},{0x00ff01,0x00ff60},{0x00ffe0,0x00ffe6},{0x016fe0,0x016fe1},{0x017000,0x0187f1}, + {0x018800,0x018af2},{0x01b000,0x01b11e},{0x01b170,0x01b2fb},{0x01f004,0x01f004},{0x01f0cf,0x01f0cf},{0x01f18e,0x01f18e},{0x01f191,0x01f19a},{0x01f1e6,0x01f202}, + {0x01f210,0x01f23b},{0x01f240,0x01f248},{0x01f250,0x01f251},{0x01f260,0x01f265},{0x01f300,0x01f320},{0x01f32d,0x01f335},{0x01f337,0x01f37c},{0x01f37e,0x01f393}, + {0x01f3a0,0x01f3ca},{0x01f3cf,0x01f3d3},{0x01f3e0,0x01f3f0},{0x01f3f4,0x01f3f4},{0x01f3f8,0x01f43e},{0x01f440,0x01f440},{0x01f442,0x01f4fc},{0x01f4ff,0x01f53d}, + {0x01f54b,0x01f54e},{0x01f550,0x01f567},{0x01f57a,0x01f57a},{0x01f595,0x01f596},{0x01f5a4,0x01f5a4},{0x01f5fb,0x01f64f},{0x01f680,0x01f6c5},{0x01f6cc,0x01f6cc}, + {0x01f6d0,0x01f6d2},{0x01f6eb,0x01f6ec},{0x01f6f4,0x01f6f9},{0x01f910,0x01f93e},{0x01f940,0x01f970},{0x01f973,0x01f976},{0x01f97a,0x01f97a},{0x01f97c,0x01f9a2}, + {0x01f9b0,0x01f9b9},{0x01f9c0,0x01f9c2},{0x01f9d0,0x01f9ff},{0x020000,0x02fffd},{0x030000,0x03fffd}, +}; + +static constexpr const Range LUT_0[] = { + {0x000300,0x00036f},{0x000483,0x000489},{0x000591,0x0005bd},{0x0005bf,0x0005bf},{0x0005c1,0x0005c2},{0x0005c4,0x0005c5},{0x0005c7,0x0005c7},{0x000600,0x000605}, + {0x000610,0x00061a},{0x00061c,0x00061c},{0x00064b,0x00065f},{0x000670,0x000670},{0x0006d6,0x0006dd},{0x0006df,0x0006e4},{0x0006e7,0x0006e8},{0x0006ea,0x0006ed}, + {0x00070f,0x00070f},{0x000711,0x000711},{0x000730,0x00074a},{0x0007a6,0x0007b0},{0x0007eb,0x0007f3},{0x0007fd,0x0007fd},{0x000816,0x000819},{0x00081b,0x000823}, + {0x000825,0x000827},{0x000829,0x00082d},{0x000859,0x00085b},{0x0008d3,0x000902},{0x00093a,0x00093a},{0x00093c,0x00093c},{0x000941,0x000948},{0x00094d,0x00094d}, + {0x000951,0x000957},{0x000962,0x000963},{0x000981,0x000981},{0x0009bc,0x0009bc},{0x0009c1,0x0009c4},{0x0009cd,0x0009cd},{0x0009e2,0x0009e3},{0x0009fe,0x0009fe}, + {0x000a01,0x000a02},{0x000a3c,0x000a3c},{0x000a41,0x000a42},{0x000a47,0x000a48},{0x000a4b,0x000a4d},{0x000a51,0x000a51},{0x000a70,0x000a71},{0x000a75,0x000a75}, + {0x000a81,0x000a82},{0x000abc,0x000abc},{0x000ac1,0x000ac5},{0x000ac7,0x000ac8},{0x000acd,0x000acd},{0x000ae2,0x000ae3},{0x000afa,0x000aff},{0x000b01,0x000b01}, + {0x000b3c,0x000b3c},{0x000b3f,0x000b3f},{0x000b41,0x000b44},{0x000b4d,0x000b4d},{0x000b56,0x000b56},{0x000b62,0x000b63},{0x000b82,0x000b82},{0x000bc0,0x000bc0}, + {0x000bcd,0x000bcd},{0x000c00,0x000c00},{0x000c04,0x000c04},{0x000c3e,0x000c40},{0x000c46,0x000c48},{0x000c4a,0x000c4d},{0x000c55,0x000c56},{0x000c62,0x000c63}, + {0x000c81,0x000c81},{0x000cbc,0x000cbc},{0x000cbf,0x000cbf},{0x000cc6,0x000cc6},{0x000ccc,0x000ccd},{0x000ce2,0x000ce3},{0x000d00,0x000d01},{0x000d3b,0x000d3c}, + {0x000d41,0x000d44},{0x000d4d,0x000d4d},{0x000d62,0x000d63},{0x000dca,0x000dca},{0x000dd2,0x000dd4},{0x000dd6,0x000dd6},{0x000e31,0x000e31},{0x000e34,0x000e3a}, + {0x000e47,0x000e4e},{0x000eb1,0x000eb1},{0x000eb4,0x000eb9},{0x000ebb,0x000ebc},{0x000ec8,0x000ecd},{0x000f18,0x000f19},{0x000f35,0x000f35},{0x000f37,0x000f37}, + {0x000f39,0x000f39},{0x000f71,0x000f7e},{0x000f80,0x000f84},{0x000f86,0x000f87},{0x000f8d,0x000f97},{0x000f99,0x000fbc},{0x000fc6,0x000fc6},{0x00102d,0x001030}, + {0x001032,0x001037},{0x001039,0x00103a},{0x00103d,0x00103e},{0x001058,0x001059},{0x00105e,0x001060},{0x001071,0x001074},{0x001082,0x001082},{0x001085,0x001086}, + {0x00108d,0x00108d},{0x00109d,0x00109d},{0x001160,0x001160},{0x00135d,0x00135f},{0x001712,0x001714},{0x001732,0x001734},{0x001752,0x001753},{0x001772,0x001773}, + {0x0017b4,0x0017b5},{0x0017b7,0x0017bd},{0x0017c6,0x0017c6},{0x0017c9,0x0017d3},{0x0017dd,0x0017dd},{0x00180b,0x00180e},{0x001885,0x001886},{0x0018a9,0x0018a9}, + {0x001920,0x001922},{0x001927,0x001928},{0x001932,0x001932},{0x001939,0x00193b},{0x001a17,0x001a18},{0x001a1b,0x001a1b},{0x001a56,0x001a56},{0x001a58,0x001a5e}, + {0x001a60,0x001a60},{0x001a62,0x001a62},{0x001a65,0x001a6c},{0x001a73,0x001a7c},{0x001a7f,0x001a7f},{0x001ab0,0x001abe},{0x001b00,0x001b03},{0x001b34,0x001b34}, + {0x001b36,0x001b3a},{0x001b3c,0x001b3c},{0x001b42,0x001b42},{0x001b6b,0x001b73},{0x001b80,0x001b81},{0x001ba2,0x001ba5},{0x001ba8,0x001ba9},{0x001bab,0x001bad}, + {0x001be6,0x001be6},{0x001be8,0x001be9},{0x001bed,0x001bed},{0x001bef,0x001bf1},{0x001c2c,0x001c33},{0x001c36,0x001c37},{0x001cd0,0x001cd2},{0x001cd4,0x001ce0}, + {0x001ce2,0x001ce8},{0x001ced,0x001ced},{0x001cf4,0x001cf4},{0x001cf8,0x001cf9},{0x001dc0,0x001df9},{0x001dfb,0x001dff},{0x00200b,0x00200f},{0x00202a,0x00202e}, + {0x002060,0x002064},{0x002066,0x00206f},{0x0020d0,0x0020f0},{0x002cef,0x002cf1},{0x002d7f,0x002d7f},{0x002de0,0x002dff},{0x00302a,0x00302d},{0x003099,0x00309a}, + {0x00a66f,0x00a672},{0x00a674,0x00a67d},{0x00a69e,0x00a69f},{0x00a6f0,0x00a6f1},{0x00a802,0x00a802},{0x00a806,0x00a806},{0x00a80b,0x00a80b},{0x00a825,0x00a826}, + {0x00a8c4,0x00a8c5},{0x00a8e0,0x00a8f1},{0x00a8ff,0x00a8ff},{0x00a926,0x00a92d},{0x00a947,0x00a951},{0x00a980,0x00a982},{0x00a9b3,0x00a9b3},{0x00a9b6,0x00a9b9}, + {0x00a9bc,0x00a9bc},{0x00a9e5,0x00a9e5},{0x00aa29,0x00aa2e},{0x00aa31,0x00aa32},{0x00aa35,0x00aa36},{0x00aa43,0x00aa43},{0x00aa4c,0x00aa4c},{0x00aa7c,0x00aa7c}, + {0x00aab0,0x00aab0},{0x00aab2,0x00aab4},{0x00aab7,0x00aab8},{0x00aabe,0x00aabf},{0x00aac1,0x00aac1},{0x00aaec,0x00aaed},{0x00aaf6,0x00aaf6},{0x00abe5,0x00abe5}, + {0x00abe8,0x00abe8},{0x00abed,0x00abed},{0x00fb1e,0x00fb1e},{0x00fe00,0x00fe0f},{0x00fe20,0x00fe2f},{0x00feff,0x00feff},{0x00fff9,0x00fffb},{0x0101fd,0x0101fd}, + {0x0102e0,0x0102e0},{0x010376,0x01037a},{0x010a01,0x010a03},{0x010a05,0x010a06},{0x010a0c,0x010a0f},{0x010a38,0x010a3a},{0x010a3f,0x010a3f},{0x010ae5,0x010ae6}, + {0x010d24,0x010d27},{0x010f46,0x010f50},{0x011001,0x011001},{0x011038,0x011046},{0x01107f,0x011081},{0x0110b3,0x0110b6},{0x0110b9,0x0110ba},{0x0110bd,0x0110bd}, + {0x0110cd,0x0110cd},{0x011100,0x011102},{0x011127,0x01112b},{0x01112d,0x011134},{0x011173,0x011173},{0x011180,0x011181},{0x0111b6,0x0111be},{0x0111c9,0x0111cc}, + {0x01122f,0x011231},{0x011234,0x011234},{0x011236,0x011237},{0x01123e,0x01123e},{0x0112df,0x0112df},{0x0112e3,0x0112ea},{0x011300,0x011301},{0x01133b,0x01133c}, + {0x011340,0x011340},{0x011366,0x01136c},{0x011370,0x011374},{0x011438,0x01143f},{0x011442,0x011444},{0x011446,0x011446},{0x01145e,0x01145e},{0x0114b3,0x0114b8}, + {0x0114ba,0x0114ba},{0x0114bf,0x0114c0},{0x0114c2,0x0114c3},{0x0115b2,0x0115b5},{0x0115bc,0x0115bd},{0x0115bf,0x0115c0},{0x0115dc,0x0115dd},{0x011633,0x01163a}, + {0x01163d,0x01163d},{0x01163f,0x011640},{0x0116ab,0x0116ab},{0x0116ad,0x0116ad},{0x0116b0,0x0116b5},{0x0116b7,0x0116b7},{0x01171d,0x01171f},{0x011722,0x011725}, + {0x011727,0x01172b},{0x01182f,0x011837},{0x011839,0x01183a},{0x011a01,0x011a0a},{0x011a33,0x011a38},{0x011a3b,0x011a3e},{0x011a47,0x011a47},{0x011a51,0x011a56}, + {0x011a59,0x011a5b},{0x011a8a,0x011a96},{0x011a98,0x011a99},{0x011c30,0x011c36},{0x011c38,0x011c3d},{0x011c3f,0x011c3f},{0x011c92,0x011ca7},{0x011caa,0x011cb0}, + {0x011cb2,0x011cb3},{0x011cb5,0x011cb6},{0x011d31,0x011d36},{0x011d3a,0x011d3a},{0x011d3c,0x011d3d},{0x011d3f,0x011d45},{0x011d47,0x011d47},{0x011d90,0x011d91}, + {0x011d95,0x011d95},{0x011d97,0x011d97},{0x011ef3,0x011ef4},{0x016af0,0x016af4},{0x016b30,0x016b36},{0x016f8f,0x016f92},{0x01bc9d,0x01bc9e},{0x01bca0,0x01bca3}, + {0x01d167,0x01d169},{0x01d173,0x01d182},{0x01d185,0x01d18b},{0x01d1aa,0x01d1ad},{0x01d242,0x01d244},{0x01da00,0x01da36},{0x01da3b,0x01da6c},{0x01da75,0x01da75}, + {0x01da84,0x01da84},{0x01da9b,0x01da9f},{0x01daa1,0x01daaf},{0x01e000,0x01e006},{0x01e008,0x01e018},{0x01e01b,0x01e021},{0x01e023,0x01e024},{0x01e026,0x01e02a}, + {0x01e8d0,0x01e8d6},{0x01e944,0x01e94a},{0x0e0001,0x0e0001},{0x0e0020,0x0e007f},{0x0e0100,0x0e01ef}, +}; + + +static constexpr const RangeLut RANGE_LUT_LIST[] = { + {-1, LUT_NONPRINTABLE, 1}, + { 2, LUT_2 , 109}, + { 0, LUT_0 , 325}, + { 1, nullptr , 1}, +}; +static constexpr const int RANGE_LUT_LIST_SIZE = 4; + + +int KONSOLEPRIVATE_EXPORT characterWidth(uint ucs4) { + if(Q_LIKELY(ucs4 < sizeof(DIRECT_LUT))) + return DIRECT_LUT[ucs4]; + + for(auto rl = RANGE_LUT_LIST; rl->lut != nullptr; ++rl) { + int l = 0; + int r = rl->size - 1; + while(l <= r) { + const int m = (l + r) / 2; + if(rl->lut[m].last < ucs4) + l = m + 1; + else if(rl->lut[m].first > ucs4) + r = m - 1; + else + return rl->width; + } + } + + return RANGE_LUT_LIST[RANGE_LUT_LIST_SIZE - 1].width; +} + diff --git a/src/CharacterWidth.src.cpp b/src/CharacterWidth.src.cpp new file mode 100644 --- /dev/null +++ b/src/CharacterWidth.src.cpp @@ -0,0 +1,102 @@ +/* + This file is part of Konsole, a terminal emulator for KDE. + + Copyright 2018 by Mariusz Glebocki + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ +«*NOTE:-----------------------------------------------------------------------*» +// Typing in "«" and "»" characters in some keyboard layouts (X11): +// +// English/UK: AltGr+Z AltGr+X +// EurKEY: AltGr+[ AltGr+] +// German: AltGr+X AltGr+Y +// Polish: AltGr+9 AltGr+0 +// English/US: N/A; You can try EurKEY which extends En/US layout with extra +// characters available with AltGr[+Shift]. +// +// Alternatively, you can use e.g. "<<<" and ">>>" and convert it to the valid +// characters using sed or your editor's replace function. +// +// This text will not appear in an output file. +«*-----------------------------------------------------------------------:NOTE*» +// +// «gen-file-warning» +// +// CharacterWidth.cpp file is automatically generated - do not edit it. +// To change anything here, edit CharacterWidth.src.cpp and regenerate the file +// using following command: +// +// «cmdline» +// + +#include "CharacterWidth.h" +#include "konsoledebug.h" +#include "konsoleprivate_export.h" + + +struct Range { + uint first, last; +}; + +struct RangeLut { + int8_t width; + const Range * const lut; + int size; +}; + +enum { + InvalidWidth = INT8_MIN, +}; + + +static constexpr const int8_t DIRECT_LUT[] = {«!fmt "% d":«direct-lut: + «!repeat 32:«:«»,»» +»»}; + +«ranges-luts:«: +static constexpr const Range «name»[] = {«!fmt "%#.6x":«ranges: + «!repeat 8:«:{«first»,«last»},»» +»»}; +»» + +static constexpr const RangeLut RANGE_LUT_LIST[] = {«ranges-lut-list: + «:{«!fmt "% d":«width»», «!fmt "%-16s":«name»», «size»},» +»}; +static constexpr const int RANGE_LUT_LIST_SIZE = «ranges-lut-list-size»; + + +int KONSOLEPRIVATE_EXPORT characterWidth(uint ucs4) { + if(Q_LIKELY(ucs4 < sizeof(DIRECT_LUT))) + return DIRECT_LUT[ucs4]; + + for(auto rl = RANGE_LUT_LIST; rl->lut != nullptr; ++rl) { + int l = 0; + int r = rl->size - 1; + while(l <= r) { + const int m = (l + r) / 2; + if(rl->lut[m].last < ucs4) + l = m + 1; + else if(rl->lut[m].first > ucs4) + r = m - 1; + else + return rl->width; + } + } + + return RANGE_LUT_LIST[RANGE_LUT_LIST_SIZE - 1].width; +} + diff --git a/src/Filter.cpp b/src/Filter.cpp --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -224,7 +224,7 @@ if (_linePositions->value(i) <= position && position < nextLine) { startLine = i; - startColumn = string_width(buffer()->mid(_linePositions->value(i), + startColumn = Character::stringWidth(buffer()->mid(_linePositions->value(i), position - _linePositions->value(i))); return; } diff --git a/src/TerminalCharacterDecoder.cpp b/src/TerminalCharacterDecoder.cpp --- a/src/TerminalCharacterDecoder.cpp +++ b/src/TerminalCharacterDecoder.cpp @@ -139,7 +139,7 @@ if (chars != nullptr) { const QString s = QString::fromUcs4(chars, extendedCharLength); plainText.append(s); - i += qMax(1, string_width(s)); + i += qMax(1, Character::stringWidth(s)); } else { ++i; } diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp --- a/src/TerminalDisplay.cpp +++ b/src/TerminalDisplay.cpp @@ -3450,7 +3450,7 @@ QRect TerminalDisplay::preeditRect() const { - const int preeditLength = string_width(_inputMethodData.preeditString); + const int preeditLength = Character::stringWidth(_inputMethodData.preeditString); if (preeditLength == 0) { return QRect(); @@ -3910,11 +3910,15 @@ void TerminalDisplay::doDrag() { + const QMimeData *clipboardMimeData = QApplication::clipboard()->mimeData(QClipboard::Selection); + if (!clipboardMimeData) { + return; + } + auto mimeData = new QMimeData(); _dragInfo.state = diDragging; _dragInfo.dragObject = new QDrag(this); - auto mimeData = new QMimeData(); - mimeData->setText(QApplication::clipboard()->mimeData(QClipboard::Selection)->text()); - mimeData->setHtml(QApplication::clipboard()->mimeData(QClipboard::Selection)->html()); + mimeData->setText(clipboardMimeData->text()); + mimeData->setHtml(clipboardMimeData->html()); _dragInfo.dragObject->setMimeData(mimeData); _dragInfo.dragObject->exec(Qt::CopyAction); } diff --git a/src/autotests/CharacterWidthTest.cpp b/src/autotests/CharacterWidthTest.cpp --- a/src/autotests/CharacterWidthTest.cpp +++ b/src/autotests/CharacterWidthTest.cpp @@ -61,14 +61,18 @@ QTest::newRow("0x1F943 tumbler glass") << uint(0x1F943) << 2; QTest::newRow("0x1F944 spoon") << uint(0x1F944) << 2; + + QTest::newRow("0x26A1 high voltage sign (BUG 378124)") << uint(0x026A1) << 2; + QTest::newRow("0x2615 hot beverage (BUG 392171)") << uint(0x02615) << 2; + QTest::newRow("0x26EA church (BUG 392171)") << uint(0x026EA) << 2; + QTest::newRow("0x1D11E musical symbol g clef (BUG 339439)") << uint(0x1D11E) << 1; + } void CharacterWidthTest::testWidth() { QFETCH(uint, character); - QEXPECT_FAIL("0x1F943 tumbler glass", "emoji width currently broken", Continue); - QEXPECT_FAIL("0x1F944 spoon", "emoji width currently broken", Continue); QTEST(Character::width(character), "width"); } diff --git a/src/konsole_wcwidth.h b/src/konsole_wcwidth.h deleted file mode 100644 --- a/src/konsole_wcwidth.h +++ /dev/null @@ -1,16 +0,0 @@ -/* $XFree86: xc/programs/xterm/wcwidth.h,v 1.5 2005/05/03 00:38:25 dickey Exp $ */ - -/* Markus Kuhn -- 2001-01-12 -- public domain */ -/* Adaptions for KDE by Waldo Bastian */ - -#ifndef KONSOLE_WCWIDTH_H -#define KONSOLE_WCWIDTH_H - -// Qt -#include - -int konsole_wcwidth(uint ucs); - -int string_width(const QString &text); - -#endif diff --git a/src/konsole_wcwidth.cpp b/src/konsole_wcwidth.cpp deleted file mode 100644 --- a/src/konsole_wcwidth.cpp +++ /dev/null @@ -1,238 +0,0 @@ -// krazy:excludeall=copyright,license -// krazy does not recognize Unicode License/Copyright see COPYING.Unicode - -/* $XFree86: xc/programs/xterm/wcwidth.characters,v 1.9 2006/06/19 00:36:52 dickey Exp $ */ - -/* - * This is an implementation of wcwidth() and wcswidth() (defined in - * IEEE Std 1002.1-2001) for Unicode. - * - * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html - * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html - * - * In fixed-width output devices, Latin characters all occupy a single - * "cell" position of equal width, whereas ideographic CJK characters - * occupy two such cells. Interoperability between terminal-line - * applications and (teletype-style) character terminals using the - * UTF-8 encoding requires agreement on which character should advance - * the cursor by how many cell positions. No established formal - * standards exist at present on which Unicode character shall occupy - * how many cell positions on character terminals. These routines are - * a first attempt of defining such behavior based on simple rules - * applied to data provided by the Unicode Consortium. - * - * For some graphical characters, the Unicode standard explicitly - * defines a character-cell width via the definition of the East Asian - * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes. - * In all these cases, there is no ambiguity about which width a - * terminal shall use. For characters in the East Asian Ambiguous (A) - * class, the width choice depends purely on a preference of backward - * compatibility with either historic CJK or Western practice. - * Choosing single-width for these characters is easy to justify as - * the appropriate long-term solution, as the CJK practice of - * displaying these characters as double-width comes from historic - * implementation simplicity (8-bit encoded characters were displayed - * single-width and 16-bit ones double-width, even for Greek, - * Cyrillic, etc.) and not any typographic considerations. - * - * Much less clear is the choice of width for the Not East Asian - * (Neutral) class. Existing practice does not dictate a width for any - * of these characters. It would nevertheless make sense - * typographically to allocate two character cells to characters such - * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be - * represented adequately with a single-width glyph. The following - * routines at present merely assign a single-cell width to all - * neutral characters, in the interest of simplicity. This is not - * entirely satisfactory and should be reconsidered before - * establishing a formal standard in this area. At the moment, the - * decision which Not East Asian (Neutral) characters should be - * represented by double-width glyphs cannot yet be answered by - * applying a simple rule from the Unicode database content. Setting - * up a proper standard for the behavior of UTF-8 character terminals - * will require a careful analysis not only of each Unicode character, - * but also of each presentation form, something the author of these - * routines has avoided to do so far. - * - * http://www.unicode.org/unicode/reports/tr11/ - * - * Markus Kuhn -- 2007-05-25 (Unicode 5.0) - * - * Permission to use, copy, modify, and distribute this software - * for any purpose and without fee is hereby granted. The author - * disclaims all warranties with regard to this software. - * - * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c - */ -/* - * Adaptions for KDE by Waldo Bastian and - * Francesco Cecconi - * See COPYING.Unicode for the license for the original wcwidth.c - */ - -// Own -#include "konsole_wcwidth.h" -#include "konsoleprivate_export.h" - -// Qt -#include - -struct interval { - unsigned long first; - unsigned long last; -}; - -/* auxiliary function for binary search in interval table */ -static int bisearch(unsigned long ucs, const struct interval* table, int max) -{ - int min = 0; - - if (ucs < table[0].first || ucs > table[max].last) { - return 0; - } - while (max >= min) { - const int mid = (min + max) / 2; - if (ucs > table[mid].last) { - min = mid + 1; - } else if (ucs < table[mid].first) { - max = mid - 1; - } else { - return 1; - } - } - - return 0; -} - -/* The following functions define the column width of an ISO 10646 - * character as follows: - * - * - The null character (U+0000) has a column width of 0. - * - * - Other C0/C1 control characters and DEL will lead to a return - * value of -1. - * - * - Non-spacing and enclosing combining characters (general - * category code Mn or Me in the Unicode database) have a - * column width of 0. - * - * - Other format characters (general category code Cf in the Unicode - * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. - * - * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) - * have a column width of 0. - * - * - Spacing characters in the East Asian Wide (W) or East Asian - * FullWidth (F) category as defined in Unicode Technical - * Report #11 have a column width of 2. - * - * - All remaining characters (including all printable - * ISO 8859-1 and WGL4 characters, Unicode control characters, - * etc.) have a column width of 1. - * - * This implementation assumes that quint16 characters are encoded - * in ISO 10646. - */ - -int KONSOLEPRIVATE_EXPORT konsole_wcwidth(uint ucs) -{ - /* sorted list of non-overlapping intervals of non-spacing characters */ - /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ - static const struct interval combining[] = { - { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 }, - { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 }, - { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 }, - { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 }, - { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, - { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A }, - { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 }, - { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D }, - { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, - { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, - { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C }, - { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, - { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, - { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, - { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C }, - { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D }, - { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 }, - { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 }, - { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC }, - { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, - { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, - { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, - { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, - { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, - { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, - { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, - { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, - { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, - { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, - { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F }, - { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, - { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, - { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, - { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, - { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B }, - { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 }, - { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 }, - { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF }, - { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 }, - { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F }, - { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B }, - { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, - { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, - { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F }, - { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 }, - { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD }, - { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F }, - { 0xE0100, 0xE01EF } - }; - - /* test for 8-bit control characters */ - if (ucs == 0 || QChar::isLowSurrogate(ucs)) { - return 0; - } - - /* Always assume double width, otherwise we have to go back and move characters */ - if (QChar::isHighSurrogate(ucs)) { - return 2; - } - - if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) { - return -1; - } - - /* binary search in table of non-spacing characters */ - if (bisearch(ucs, combining, - sizeof(combining) / sizeof(struct interval) - 1) != 0) { - return 0; - } - - /* if we arrive here, ucs is not a combining or C0/C1 control character */ - - return 1 + - static_cast(ucs >= 0x1100 && - (ucs <= 0x115f || /* Hangul Jamo init. consonants */ - ucs == 0x2329 || ucs == 0x232a || - (ucs >= 0x2e80 && ucs <= 0xa4cf && - ucs != 0x303f) || /* CJK ... Yi */ - (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */ - (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */ - (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */ - (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */ - (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */ - (ucs >= 0xffe0 && ucs <= 0xffe6) || - (ucs >= 0x20000 && ucs <= 0x2fffd) || - (ucs >= 0x30000 && ucs <= 0x3fffd))); -} - -int string_width(const QString& text) -{ - int w = 0; - auto ucs4 = text.toUcs4(); - for (auto i : ucs4) { - w += konsole_wcwidth(i); - } - return w; -} - diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -17,3 +17,4 @@ target_link_libraries(fontembedder Qt5::Core) endif() +add_subdirectory( uni2characterwidth ) diff --git a/tools/uni2characterwidth/CMakeLists.txt b/tools/uni2characterwidth/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/CMakeLists.txt @@ -0,0 +1,30 @@ +### uni2characterwidth +### +### Converts Unicode Character Database files into character width lookup +### tables. Uses a template file to place the tables in a source code file +### together with a function for finding the width for specified character. +### +### See `uni2characterwidth --help` for usage information +if(KONSOLE_BUILD_UNI2CHARACTERWIDTH) + + find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED + Core + ) + find_package(KF5 ${KF5_MIN_VERSION} REQUIRED + KIO + ) + + set(uni2characterwidth_SRC + uni2characterwidth.cpp + properties.h + template.cpp + template.h + ) + + add_executable(uni2characterwidth ${uni2characterwidth_SRC}) + target_link_libraries(uni2characterwidth + Qt5::Core + KF5::KIOCore + ) + +endif() diff --git a/tools/uni2characterwidth/overrides.txt b/tools/uni2characterwidth/overrides.txt new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/overrides.txt @@ -0,0 +1,3 @@ +000AD ; 1 # (­) Soft Hyphen (originally 0) +01160 ; 0 # (ᅠ) Hangul Jungseong Filler (originally 1) +1F1E6..1F1FF ; 2 # (🇦..🇿) Regional indicators/flag codes (originally 1) diff --git a/tools/uni2characterwidth/properties.h b/tools/uni2characterwidth/properties.h new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/properties.h @@ -0,0 +1,78 @@ +#ifndef CATEGORY_PROPERTY_VALUE +#define CATEGORY_PROPERTY_VALUE(val, sym, intVal) +#endif +#ifndef CATEGORY_PROPERTY_GROUP +#define CATEGORY_PROPERTY_GROUP(val, sym, intVal) +#endif + +CATEGORY_PROPERTY_VALUE(Lu, UppercaseLetter, 1<<0) // an uppercase letter +CATEGORY_PROPERTY_VALUE(Ll, LowercaseLetter, 1<<1) // a lowercase letter +CATEGORY_PROPERTY_VALUE(Lt, TitlecaseLetter, 1<<2) // a digraphic character, with first part uppercase +CATEGORY_PROPERTY_GROUP(LC, CasedLetter, 1<<0|1<<1|1<<2) +CATEGORY_PROPERTY_VALUE(Lm, ModifierLetter, 1<<3) // a modifier letter +CATEGORY_PROPERTY_VALUE(Lo, OtherLetter, 1<<4) // other letters, including syllables and ideographs +CATEGORY_PROPERTY_GROUP(L, Letter, 1<<0|1<<1|1<<2|1<<3|1<<4) +CATEGORY_PROPERTY_VALUE(Mn, NonspacingMark, 1<<5) // a nonspacing combining mark (zero advance width) +CATEGORY_PROPERTY_VALUE(Mc, SpacingMark, 1<<6) // a spacing combining mark (positive advance width) +CATEGORY_PROPERTY_VALUE(Me, EnclosingMark, 1<<7) // an enclosing combining mark +CATEGORY_PROPERTY_GROUP(M, Mark, 1<<5|1<<6|1<<7) +CATEGORY_PROPERTY_VALUE(Nd, DecimalNumber, 1<<8) // a decimal digit +CATEGORY_PROPERTY_VALUE(Nl, LetterNumber, 1<<9) // a letterlike numeric character +CATEGORY_PROPERTY_VALUE(No, OtherNumber, 1<<10) // a numeric character of other type +CATEGORY_PROPERTY_GROUP(N, Number, 1<<8|1<<9|1<<10) +CATEGORY_PROPERTY_VALUE(Pc, ConnectorPunctuation, 1<<11) // a connecting punctuation mark, like a tie +CATEGORY_PROPERTY_VALUE(Pd, DashPunctuation, 1<<12) // a dash or hyphen punctuation mark +CATEGORY_PROPERTY_VALUE(Ps, OpenPunctuation, 1<<13) // an opening punctuation mark (of a pair) +CATEGORY_PROPERTY_VALUE(Pe, ClosePunctuation, 1<<14) // a closing punctuation mark (of a pair) +CATEGORY_PROPERTY_VALUE(Pi, InitialPunctuation, 1<<15) // an initial quotation mark +CATEGORY_PROPERTY_VALUE(Pf, FinalPunctuation, 1<<16) // a final quotation mark +CATEGORY_PROPERTY_VALUE(Po, OtherPunctuation, 1<<17) // a punctuation mark of other type +CATEGORY_PROPERTY_GROUP(P, Punctuation, 1<<11|1<<12|1<<13|1<<14|1<<15|1<<16|1<<17) +CATEGORY_PROPERTY_VALUE(Sm, MathSymbol, 1<<18) // a symbol of mathematical use +CATEGORY_PROPERTY_VALUE(Sc, CurrencySymbol, 1<<19) // a currency sign +CATEGORY_PROPERTY_VALUE(Sk, ModifierSymbol, 1<<20) // a non-letterlike modifier symbol +CATEGORY_PROPERTY_VALUE(So, OtherSymbol, 1<<21) // a symbol of other type +CATEGORY_PROPERTY_GROUP(S, Symbol, 1<<18|1<<19|1<<20|1<<21) +CATEGORY_PROPERTY_VALUE(Zs, SpaceSeparator, 1<<22) // a space character (of various non-zero widths) +CATEGORY_PROPERTY_VALUE(Zl, LineSeparator, 1<<23) // U+2028 LINE SEPARATOR only +CATEGORY_PROPERTY_VALUE(Zp, ParagraphSeparator, 1<<24) // U+2029 PARAGRAPH SEPARATOR only +CATEGORY_PROPERTY_GROUP(Z, Separator, 1<<22|1<<23|1<<24) +CATEGORY_PROPERTY_VALUE(Cc, Control, 1<<25) // a C0 or C1 control code +CATEGORY_PROPERTY_VALUE(Cf, Format, 1<<26) // a format control character +CATEGORY_PROPERTY_VALUE(Cs, Surrogate, 1<<27) // a surrogate code point +CATEGORY_PROPERTY_VALUE(Co, PrivateUse, 1<<28) // a private-use character +CATEGORY_PROPERTY_VALUE(Cn, Unassigned, 1<<29) // a reserved unassigned code point or a noncharacter +CATEGORY_PROPERTY_GROUP(C, Other, 1<<25|1<<26|1<<27|1<<28|1<<29) + +#undef CATEGORY_PROPERTY_VALUE +#undef CATEGORY_PROPERTY_GROUP + +/**************************************/ + +#ifndef EAST_ASIAN_WIDTH_PROPERTY_VALUE +#define EAST_ASIAN_WIDTH_PROPERTY_VALUE(val, sym, intVal) +#endif + +EAST_ASIAN_WIDTH_PROPERTY_VALUE(A, Ambiguous, 1<<0) +EAST_ASIAN_WIDTH_PROPERTY_VALUE(F, Fullwidth, 1<<1) +EAST_ASIAN_WIDTH_PROPERTY_VALUE(H, Halfwidth, 1<<2) +EAST_ASIAN_WIDTH_PROPERTY_VALUE(N, Neutral, 1<<3) +EAST_ASIAN_WIDTH_PROPERTY_VALUE(Na, Narrow, 1<<4) +EAST_ASIAN_WIDTH_PROPERTY_VALUE(W, Wide, 1<<5) + +#undef EAST_ASIAN_WIDTH_PROPERTY_VALUE + +/**************************************/ + +#ifndef EMOJI_PROPERTY_VALUE +#define EMOJI_PROPERTY_VALUE(val, sym, intVal) +#endif + +EMOJI_PROPERTY_VALUE(, None, 0) +EMOJI_PROPERTY_VALUE(Emoji, Emoji, 1<<0) +EMOJI_PROPERTY_VALUE(Emoji_Presentation, EmojiPresentation, 1<<1) +EMOJI_PROPERTY_VALUE(Emoji_Modifier, EmojiModifier, 1<<2) +EMOJI_PROPERTY_VALUE(Emoji_Modifier_Base, EmojiModifier_Base, 1<<3) +EMOJI_PROPERTY_VALUE(Emoji_Component, EmojiComponent, 1<<4) + +#undef EMOJI_PROPERTY_VALUE diff --git a/tools/uni2characterwidth/template.h b/tools/uni2characterwidth/template.h new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/template.h @@ -0,0 +1,184 @@ +/* + This file is part of Konsole, a terminal emulator for KDE. + + Copyright 2018 by Mariusz Glebocki + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#ifndef TEMPLATE_H +#define TEMPLATE_H + +#include +#include +#include + +// Backward compatibility +#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0) && !defined(qAsConst) +#define qAsConst(code) code +#endif + +// QVariant doesn't offer modification in place. Var does. +class Var { +public: + using Number = qint64; + using String = QString; + using Map = QMap; + using Vector = QVector; + + enum class DataType { + Invalid, + Number, + String, + Vector, + Map, + }; + + const QString dataTypeAsString() const { + switch(dataType()) { + case DataType::Invalid: return QStringLiteral("Invalid"); + case DataType::Number: return QStringLiteral("Number"); + case DataType::String: return QStringLiteral("String"); + case DataType::Vector: return QStringLiteral("Vector"); + case DataType::Map: return QStringLiteral("Map"); + default: return QStringLiteral("Unknown?"); + } + } + + Var(): num(0), _dataType(DataType::Invalid) {} + Var(const Var &other) { *this = other; } + + Var(const Number &newNum): _dataType(DataType::Number) { new(&num) auto(newNum); } + Var(const String &newStr): _dataType(DataType::String) { new(&str) auto(newStr); } + Var(const Vector &newVec): _dataType(DataType::Vector) { new(&vec) auto(newVec); } + Var(const Map &newMap): _dataType(DataType::Map) { new(&map) auto(newMap); } + + // Allow initialization without type name + Var(const char * newStr): _dataType(DataType::String) { new(&str) String(QString::fromUtf8(newStr)); } + Var(std::initializer_list newVec): _dataType(DataType::Vector) { new(&vec) Vector(newVec); } + + ~Var() { + switch(dataType()) { + case DataType::String: str.~String(); break; + case DataType::Vector: vec.~Vector(); break; + case DataType::Map: map.~Map(); break; + default: break; + } + } + + Var & operator=(const Var &other) { + _dataType = other.dataType(); + switch(other.dataType()) { + case DataType::Number: new(&num) auto(other.num); break; + case DataType::String: new(&str) auto(other.str); break; + case DataType::Vector: new(&vec) auto(other.vec); break; + case DataType::Map: new(&map) auto(other.map); break; + default: break; + } + return *this; + } + + Var & operator[](unsigned index) { + Q_ASSERT(_dataType == DataType::Vector); + return vec.data()[index]; + } + const Var & operator[](unsigned index) const { + Q_ASSERT(_dataType == DataType::Vector); + return vec.constData()[index]; + } + Var & operator[](const String &key) { + Q_ASSERT(_dataType == DataType::Map); + return map[key]; + } + const Var & operator[](const String &key) const { + Q_ASSERT(_dataType == DataType::Map); + return *map.find(key); + } + + DataType dataType() const { return _dataType; } + + union { + Number num; + String str; + Vector vec; + Map map; + }; + +private: + DataType _dataType; +}; + +class Template { +public: + Template(const QString &text); + void parse(); + QString generate(const Var &data); + + struct Element { + Element(const Element *parent = nullptr, const QString &name = QString()) + : outer() + , inner() + , name(name) + , fmt() + , line(0) + , column(0) + , isComment(false) + , children() + , parent(parent) {} + + Element(const Element &other) + : outer(other.outer) + , inner(other.inner) + , name(other.name) + , fmt(other.fmt) + , line(other.line) + , column(other.column) + , isComment(other.isComment) + , parent(other.parent) { + for(const auto &child: other.children) { + children.append(child); + } + } + + const QString findFmt(Var::DataType type) const; + QString path() const; + bool isCommand() const { return name.startsWith(QLatin1Char('!')); } + bool hasName() const { return !isCommand() && !name.isEmpty(); } + + static const QString defaultFmt(Var::DataType type); + static bool isValidFmt(const QString &fmt, Var::DataType type); + + QStringRef outer; + QStringRef inner; + QString name; + QString fmt; + uint line; + uint column; + bool isComment; + QList children; + const Element *parent; + }; +private: + + void executeCommand(Element &element, const Element &childStub, const QStringList &argv); + void parseRecursively(Element &element); + int generateRecursively(QString &result, const Element &element, const Var &data, int consumed = 0); + + QString _text; // FIXME: make it pointer (?) + Element _root; // FIXME: make it pointer +}; + +#endif diff --git a/tools/uni2characterwidth/template.cpp b/tools/uni2characterwidth/template.cpp new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/template.cpp @@ -0,0 +1,404 @@ +/* + This file is part of Konsole, a terminal emulator for KDE. + + Copyright 2018 by Mariusz Glebocki + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#include +#include +#include +#include +#include +#include +#include "template.h" + +static const QString unescape(const QStringRef &str) { + QString result; + result.reserve(str.length()); + for(int i = 0; i < str.length(); ++i) { + if(str[i] == QLatin1Char('\\') && i < str.length() - 1) + result += str[++i]; + else + result += str[i]; + } + return result; +} + +// +// Template::Element +// +const QString Template::Element::findFmt(Var::DataType type) const { + const Template::Element *element; + for(element = this; element != nullptr; element = element->parent) { + if(!element->fmt.isEmpty() && isValidFmt(element->fmt, type)) { + return element->fmt; + } + } + return defaultFmt(type); +} + +QString Template::Element::path() const { + QStringList namesList; + const Template::Element *element; + for(element = this; element != nullptr; element = element->parent) { + if(!element->hasName() && element->parent != nullptr) { + QString anonName = QStringLiteral("[anon]"); + for(int i = 0; i < element->parent->children.size(); ++i) { + if(&element->parent->children[i] == element) { + anonName = QStringLiteral("[%1]").arg(i); + break; + } + } + namesList.prepend(anonName); + } else { + namesList.prepend(element->name); + } + } + return namesList.join(QLatin1Char('.')); +} + +const QString Template::Element::defaultFmt(Var::DataType type) { + switch(type) { + case Var::DataType::Number: return QStringLiteral("%d"); + case Var::DataType::String: return QStringLiteral("%s"); + default: Q_UNREACHABLE(); + } +} + +bool Template::Element::isValidFmt(const QString &fmt, Var::DataType type) { + switch(type) { + case Var::DataType::String: return fmt.endsWith(QLatin1Char('s')); + case Var::DataType::Number: return true; // regexp in parser takes care of it + default: return false; + } +} + +// +// Template +// + +Template::Template(const QString &text): _text(text) { + _root.name = QStringLiteral("[root]"); + _root.outer = QStringRef(&_text); + _root.inner = QStringRef(&_text); + _root.parent = nullptr; + _root.line = 1; + _root.column = 1; +} + +void Template::parse() { + _root.children.clear(); + _root.outer = QStringRef(&_text); + _root.inner = QStringRef(&_text); + parseRecursively(_root); +// dbgDumpTree(_root); +} + +QString Template::generate(const Var &data) { + QString result; + result.reserve(_text.size()); + generateRecursively(result, _root, data); + return result; +} + +static inline void warn(const Template::Element &element, const QString &id, const QString &msg) { + const QString path = id.isEmpty() ? element.path() : Template::Element(&element, id).path(); + qWarning() << QStringLiteral("Warning: %1:%2: %3: %4").arg(element.line).arg(element.column).arg(path, msg); +} +static inline void warn(const Template::Element &element, const QString &msg) { + warn(element, QString(), msg); +} + +void Template::executeCommand(Element &element, const Template::Element &childStub, const QStringList &argv) { + // Insert content N times + if(argv[0] == QStringLiteral("repeat")) { + bool ok; + unsigned count = argv.value(1).toInt(&ok); + if(!ok || count < 1) { + warn(element, QStringLiteral("!") + argv[0], QStringLiteral("invalid repeat count (%1), assuming 0.").arg(argv[1])); + return; + } + + element.children.append(childStub); + Template::Element &cmdElement = element.children.last(); + if(!cmdElement.inner.isEmpty()) { + // Parse children + parseRecursively(cmdElement); + // Remember how many children was there before replication + int originalChildrenCount = cmdElement.children.size(); + // Replicate children + for(unsigned i = 1; i < count; ++i) { + for(int chId = 0; chId < originalChildrenCount; ++chId) { + cmdElement.children.append(cmdElement.children[chId]); + } + } + } + // Set printf-like format (with leading %) applied for strings and numbers + // inside the group + } else if(argv[0] == QStringLiteral("fmt")) { + static const QRegularExpression FMT_RE(QStringLiteral(R":(^%[-0 +#]?(?:[1-9][0-9]*)?\.?[0-9]*[diouxXs]$):"), + QRegularExpression::OptimizeOnFirstUsageOption); + const auto match = FMT_RE.match(argv.value(1)); + QString fmt = QStringLiteral(""); + if(!match.hasMatch()) + warn(element, QStringLiteral("!") + argv[0], QStringLiteral("invalid format (%1), assuming default").arg(argv[1])); + else + fmt = match.captured(); + + element.children.append(childStub); + Template::Element &cmdElement = element.children.last(); + cmdElement.fmt = fmt; + parseRecursively(cmdElement); + } +} + +void Template::parseRecursively(Element &element) { + static const QRegularExpression RE(QStringLiteral(R":((?'comment'«\*(([^:]*):)?.*?(?(-2):\g{-1})\*»)|):" + R":(«(?:(?'name'[-_a-zA-Z0-9]*)|(?:!(?'cmd'[-_a-zA-Z0-9]+(?: +(?:[^\\:]+|(?:\\.)+)+)?)))):" + R":((?::(?:~[ \t]*\n)?(?'inner'(?:[^«]*?|(?R))*))?(?:\n[ \t]*~)?»):"), + QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption | + QRegularExpression::OptimizeOnFirstUsageOption); + static const QRegularExpression CMD_SPLIT_RE(QStringLiteral(R":((?:"((?:(?:\\.)*|[^"]*)*)"|(?:[^\\ "]+|(?:\\.)+)+)):"), + QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption | + QRegularExpression::OptimizeOnFirstUsageOption); + static const QRegularExpression UNESCAPE_RE(QStringLiteral(R":(\\(.)):"), + QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption | + QRegularExpression::OptimizeOnFirstUsageOption); + static const QString nameGroupName = QStringLiteral("name"); + static const QString innerGroupName = QStringLiteral("inner"); + static const QString cmdGroupName = QStringLiteral("cmd"); + static const QString commentGroupName = QStringLiteral("comment"); + + int posOffset = element.outer.position(); + uint posLine = element.line; + uint posColumn = element.column; + + auto matchIter = RE.globalMatch(element.inner); + while(matchIter.hasNext()) { + auto match = matchIter.next(); + auto cmd = match.captured(cmdGroupName); + auto comment = match.captured(commentGroupName); + + const auto localOuterRef = match.capturedRef(0); + const auto localInnerRef = match.capturedRef(innerGroupName); + + auto outerRef = QStringRef(&_text, localOuterRef.position(), localOuterRef.length()); + auto innerRef = QStringRef(&_text, localInnerRef.position(), localInnerRef.length()); + + while(posOffset < outerRef.position() && posOffset < _text.size()) { + if(_text[posOffset++] == QLatin1Char('\n')) { + ++posLine; + posColumn = 1; + } else { + ++posColumn; + } + } + + if(!cmd.isEmpty()) { + QStringList cmdArgv; + auto cmdArgIter = CMD_SPLIT_RE.globalMatch(cmd); + while(cmdArgIter.hasNext()) { + auto cmdArg = cmdArgIter.next(); + cmdArgv += cmdArg.captured(cmdArg.captured(1).isEmpty() ? 0 : 1); + cmdArgv.last().replace(UNESCAPE_RE, QStringLiteral("\1")); + } + + Template::Element childStub = Template::Element(&element); + childStub.outer = outerRef; + childStub.name = QLatin1Char('!') + cmd; + childStub.inner = innerRef; + childStub.line = posLine; + childStub.column = posColumn; + executeCommand(element, childStub, cmdArgv); + } else if (!comment.isEmpty()) { + element.children.append(Element(&element)); + Template::Element &child = element.children.last(); + child.outer = outerRef; + child.name = QString(); + child.inner = QStringRef(); + child.line = posLine; + child.column = posColumn; + child.isComment = true; + } else { + element.children.append(Element(&element)); + Template::Element &child = element.children.last(); + child.outer = outerRef; + child.name = match.captured(nameGroupName); + child.inner = innerRef; + child.line = posLine; + child.column = posColumn; + if(!child.inner.isEmpty()) + parseRecursively(child); + } + } +} + +int Template::generateRecursively(QString &result, const Template::Element &element, const Var &data, int consumed) { + int consumedDataItems = consumed; + + if(!element.children.isEmpty()) { + int totalDataItems; + switch(data.dataType()) { + case Var::DataType::Number: + case Var::DataType::String: + case Var::DataType::Map: + totalDataItems = 1; + break; + case Var::DataType::Vector: + totalDataItems = data.vec.size(); + break; + case Var::DataType::Invalid: + default: + Q_UNREACHABLE(); + } + + while(consumedDataItems < totalDataItems) { + int prevChildEndPosition = element.inner.position(); + for(const auto &child: element.children) { + const int characterCountBetweenChildren = child.outer.position() - prevChildEndPosition; + if(characterCountBetweenChildren > 0) { + // Add text between previous child (or inner beginning) and this child. + result += unescape(_text.midRef(prevChildEndPosition, characterCountBetweenChildren)); + } else if(characterCountBetweenChildren < 0) { + // Repeated item; they overlap and end1 > start2 + result += unescape(element.inner.mid(prevChildEndPosition - element.inner.position())); + result += unescape(element.inner.left(child.outer.position() - element.inner.position())); + } + + switch(data.dataType()) { + case Var::DataType::Number: + case Var::DataType::String: + generateRecursively(result, child, data); + consumedDataItems = 1; // Deepest child always consumes number/string + break; + case Var::DataType::Vector: + if(!data.vec.isEmpty()) { + if(!child.hasName() && !child.isCommand() && consumedDataItems < data.vec.size()) { + consumedDataItems += generateRecursively(result, child, data[consumedDataItems]); + } else { + consumedDataItems += generateRecursively(result, child, data.vec.mid(consumedDataItems)); + } + } else { + warn(child, QStringLiteral("no more items available in parent's list.")); + } + break; + case Var::DataType::Map: + if(!child.hasName()) { + consumedDataItems = generateRecursively(result, child, data); + } else if(data.map.contains(child.name)) { + generateRecursively(result, child, data.map[child.name]); + // Always consume, repeating doesn't change anything + consumedDataItems = 1; + } else { + warn(child, QStringLiteral("missing value for the element in parent's map.")); + } + break; + default: + break; + } + prevChildEndPosition = child.outer.position() + child.outer.length(); + } + + result += unescape(element.inner.mid(prevChildEndPosition - element.inner.position(), -1)); + + if(element.isCommand()) { + break; + } + + const bool isLast = consumedDataItems >= totalDataItems; + if(!isLast) { + // Collapse empty lines between elements + int nlNum = 0; + for(int i = 0; i < element.inner.size() / 2; ++i) { + if(element.inner.at(i) == QLatin1Char('\n') && + element.inner.at(i) == element.inner.at(element.inner.size() - i - 1)) + nlNum++; + else + break; + } + if(nlNum > 0) + result.chop(nlNum); + } + } + } else if (!element.isComment) { + // Handle leaf element + switch(data.dataType()) { + case Var::DataType::Number: { + const QString fmt = element.findFmt(Var::DataType::Number); + result += QString::asprintf(qUtf8Printable(fmt), data.num); + break; + } + case Var::DataType::String: { + const QString fmt = element.findFmt(Var::DataType::String); + result += QString::asprintf(qUtf8Printable(fmt), qUtf8Printable(data.str)); + break; + } + case Var::DataType::Vector: + if(data.vec.isEmpty()) { + warn(element, QStringLiteral("got empty list.")); + } else if(data.vec.at(0).dataType() == Var::DataType::Number) { + const QString fmt = element.findFmt(Var::DataType::Number); + result += QString::asprintf(qUtf8Printable(fmt), data.num); + } else if(data.vec.at(0).dataType() == Var::DataType::String) { + const QString fmt = element.findFmt(Var::DataType::String); + result += QString::asprintf(qUtf8Printable(fmt), qUtf8Printable(data.str)); + } else { + warn(element, QStringLiteral("the list entry data type (%1) is not supported in childrenless elements."). + arg(data.vec.at(0).dataTypeAsString())); + } + break; + case Var::DataType::Map: + warn(element, QStringLiteral("map type is not supported in childrenless elements.")); + break; + case Var::DataType::Invalid: + break; + } + consumedDataItems = 1; + } + + return consumedDataItems; +} + +/* +void dbgDumpTree(const Template::Element &element) { + static int indent = 0; + QString type; + if(element.isCommand()) + type = QStringLiteral("command"); + else if(element.isComment) + type = QStringLiteral("comment"); + else if(element.hasName() && element.inner.isEmpty()) + type = QStringLiteral("empty named"); + else if(element.hasName()) + type = QStringLiteral("named"); + else if(element.inner.isEmpty()) + type = QStringLiteral("empty anonymous"); + else + type = QStringLiteral("anonymous"); + + qDebug().noquote() << QStringLiteral("%1[%2] \"%3\" %4:%5") + .arg(QStringLiteral("· ").repeated(indent), type, element.name) + .arg(element.line) + .arg(element.column); + indent++; + for(const auto &child: element.children) { + dbgDumpTree(child); + } + indent--; +} +*/ diff --git a/tools/uni2characterwidth/template.example b/tools/uni2characterwidth/template.example new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/template.example @@ -0,0 +1,77 @@ +«*COMMENT:---------------------------------------------------------------------- + +Tags: + +«*anything:comment where everything but closing sequence is allowed:anything*» + +«NAME:any content, including other tags. \: have to be escaped. It is processed +using data passed from code() function under NAME key. It should contain other +tags, without them this text will be replaced with passed data or removed.» + +«NAME» - like before, used when data should replace it, so content is + unnecessary + +EXAMPLE: +data: Map{ "exampleA", Map{ { "Number", 42 }, { "String", "hello" } } } +template: «exampleA:number\: «Number», string\: «String»» +result: number: 42, string: hello + +«» - empty anonymous element. Used in named elements which receive lists. + The element will be replaced with list item, and duplicated if + +«:anonymous container. It should contain some elements which receive data. +The element will disappear when child element will not receive any value. +Useful to add suffixes/prefixes to data» + +EXAMPLE: +data: Map{ "exampleB", Vector{ 1, 2, 3, 4, 5, 6, 7 } } +template: «exampleB:«:[«»] »» +result: [1] [2] [3] [4] [5] [6] [7] + +data: Map{ "exampleC", Vector{ "a", "b", "c" } } +template: «exampleC:«:first = «»»«:, second = «»»«:, third = «»»«:, fourth = «»»» +result: first = a, second = b, third = c + +«!fmt "XXX":a wrapper which sets printf-like format XXX for numbers and +strings inside it. Starts with %.» + +«!repeat N:repeats contents inside N times.» + +EXAMPLE: +data: Map{ "exampleD", Vector{ 1, 2, 3, 4, 10, 11, 12, 13 } } +template: «exampleD:«!fmt "%#.2x":«!repeat 3:«» »«»; »» +result: 0x01 0x02 0x03 0x04; 0x0a 0x0b 0x0c 0x0d; + +D: «exampleD:«!fmt "%#.2x":«!repeat 3:«» »«»; »» +----------------------------------------------------------------------:COMMENT*» +For available data see code() function. Below are usage examples + +Warning about generated file - putting "this is a generated file" text in a +template file could be misleading. +«gen-file-warning» + + +Command used to generate the file: +«cmdline» + + +Direct LUT - widths of the first 256 code points in direct access array: +{«!fmt "% d":«direct-lut: + «!repeat 32:«:«»,»» +»»} + + +Arrays with code point ranges for every width: +«ranges-luts:«: +«name» = {«!fmt "%#.6x":«ranges: + «!repeat 8:«:{«first»,«last»},»» +»»} +Number of elements in the array: «size» + +»» +List of array names, sizes, and widths: +{«ranges-lut-list: + «:{«!fmt "% d":«width»», «!fmt "%-16s":«name»», «size»},» +»} +Number of elements in the array: «ranges-lut-list-size»; + diff --git a/tools/uni2characterwidth/uni2characterwidth.cpp b/tools/uni2characterwidth/uni2characterwidth.cpp new file mode 100644 --- /dev/null +++ b/tools/uni2characterwidth/uni2characterwidth.cpp @@ -0,0 +1,1011 @@ +/* + This file is part of Konsole, a terminal emulator for KDE. + + Copyright 2018 by Mariusz Glebocki + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "template.h" + +#include + +// Backward compatibility +#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0) +#define qAsConst(code) code +#endif + + + +static constexpr unsigned int CODE_POINTS_NUM = 0x110000; +static constexpr unsigned int LAST_CODE_POINT = CODE_POINTS_NUM - 1; + +struct UcdEntry { + struct { uint first; uint last; } cp; + QStringList fields; +}; + +class UcdParserBase { +public: + ~UcdParserBase() { + _source->close(); + } + + bool hasNext() { + bool hadNext = _hasNext; + if(!_nextFetched) { + _hasNext = fetchNext(); + _nextFetched = true; + } + return hadNext; + } + +protected: + UcdParserBase(QIODevice *source, UcdEntry *entry) + : _source(source) + , _nextFetched(false) + , _hasNext(true) + , _lineNo(0) + , _entry(entry) + { + Q_ASSERT(_source); + Q_ASSERT(_entry); + } + + bool fetchNext() { + Q_ASSERT(_source->isOpen()); + if(!_source->isOpen()) + return false; + + static const QRegularExpression ENTRY_RE = QRegularExpression(QStringLiteral( + // Match 1: "cp1" - first CP / "cp2" (optional) - last CP + R"#((?:^(?[[:xdigit:]]+)(?:\.\.(?[[:xdigit:]]+))?[ \t]*;)#" + // Match 1: "field0" - first data field" + // "udRangeInd" (UnicodeData.txt only) - if present, the line is either first or last line of a range + R"#([ \t]*(?[^#;\n]*?(?:, (?First|Last)>)?)[ \t]*(?:;|(?:\#.*)?$))|)#" + // Match 2..n: "field" - n-th field + R"#((?:\G(?<=;)[ \t]*(?[^#;\n]*?)[ \t]*(?:;|(?:#.*)?$)))#"), + QRegularExpression::OptimizeOnFirstUsageOption + ); + static const QRegularExpression UD_RANGE_IND_RE(QStringLiteral(", (First|Last)")); + static const QRegularExpression COMMENT_RE(QStringLiteral("^[ \t]*(#.*)?$")); + + QString line; + bool ok; + _entry->fields.clear(); + while(!_source->atEnd()) { + line = QString::fromUtf8(_source->readLine()); + _lineNo++; + auto mit = ENTRY_RE.globalMatch(line); + if(!mit.hasNext()) { + // Do not complain about comments and empty lines + if(!COMMENT_RE.match(line).hasMatch()) + qDebug() << QStringLiteral("Line %1: does not match - skipping").arg(_lineNo); + continue; + } + + auto match = mit.next(); + _entry->cp.first = match.captured(QStringLiteral("cp1")).toUInt(&ok, 16); + if(!ok) { + qDebug() << QStringLiteral("Line %d Invalid cp1 - skipping").arg(_lineNo); + continue; + } + _entry->cp.last = match.captured(QStringLiteral("cp2")).toUInt(&ok, 16); + if(!ok) { + _entry->cp.last = _entry->cp.first; + } + QString field0 = match.captured(QStringLiteral("field0")); + if(field0.isNull()) { + qDebug() << QStringLiteral("Line %d: Missing field0 - skipping").arg(_lineNo); + continue; + } + if(!match.captured(QStringLiteral("udRangeInd")).isNull()) { + if(match.captured(QStringLiteral("udRangeInd")) == QStringLiteral("First")) { + // Fetch next valid line, as it pairs with the current one to form a range + QRegularExpressionMatch nlMatch; + int firstLineNo = _lineNo; + while(!_source->atEnd() && !nlMatch.hasMatch()) { + line = QString::fromUtf8(_source->readLine()); + _lineNo++; + nlMatch = ENTRY_RE.match(line); + if(!nlMatch.hasMatch()) { + qDebug() << QStringLiteral("Line %d: does not match - skipping").arg(_lineNo); + } + } + if(nlMatch.hasMatch()) { + _entry->cp.last = nlMatch.captured(QStringLiteral("cp1")).toUInt(&ok, 16); + if(!ok) { + qDebug() << QStringLiteral("Line %1-%2: Missing or invalid second cp1 (\"Last\" entry) - skipping") + .arg(firstLineNo).arg(_lineNo); + continue; + } + } + } + field0.remove(UD_RANGE_IND_RE); + } + _entry->fields.append(field0); + + while(mit.hasNext()) { + _entry->fields.append(mit.next().captured(QStringLiteral("field"))); + } + + return !_source->atEnd(); + } + return false; + } + + QIODevice *_source; + bool _nextFetched; + bool _hasNext; + +private: + int _lineNo; + UcdEntry *_entry; +}; + +template +class UcdParser: public UcdParserBase { +public: + static_assert(std::is_base_of::value, "'EntryType' has to be derived from UcdParser::Entry"); + + UcdParser(QIODevice *source): UcdParserBase(source, &_typedEntry) {} + + inline const EntryType & next() { + if(!_nextFetched) + fetchNext(); + _nextFetched = false; + return _typedEntry; + } + +private: + EntryType _typedEntry; +}; + +class KIODevice: public QIODevice { +public: + enum Error { + NoError, + UnknownError, + TimeoutError, + UnknownHostError, + MalformedUrlError, + NotFoundError, + }; + + KIODevice(const QUrl &url) + : _url(url) + , _job(nullptr) + , _error(NoError) {} + + ~KIODevice() { + close(); + } + + bool open() { + if(_job) + return false; + + _job = KIO::storedGet(_url); + QObject::connect(_job, &KIO::StoredTransferJob::result, + _job, [&](KJob *) { + if(_job->isErrorPage()) + _eventLoop.exit(KIO::ERR_DOES_NOT_EXIST); + else if(_job->error() != KJob::NoError) + _eventLoop.exit(_job->error()); + else + _data = _job->data(); + + _eventLoop.exit(KJob::NoError); + }); + + _eventLoop.exec(); + switch(_job->error()) { + case KJob::NoError: + _error = NoError; + setErrorString(QStringLiteral("")); + QIODevice::open(QIODevice::ReadOnly | QIODevice::Unbuffered); + break; + case KJob::KilledJobError: _error = TimeoutError; break; + case KIO::ERR_UNKNOWN_HOST: _error = UnknownHostError; break; + case KIO::ERR_DOES_NOT_EXIST: _error = NotFoundError; break; + case KIO::ERR_MALFORMED_URL: _error = MalformedUrlError; break; + default: _error = UnknownError; break; + } + if(_error != NoError) { + setErrorString(QStringLiteral("KIO: ") + _job->errorString()); + delete _job; + _job = nullptr; + _data.clear(); + } + return _error == NoError; + } + bool open(OpenMode mode) override { + Q_ASSERT(mode == QIODevice::ReadOnly); + return open(); + } + void close() override { + if(_job) { + delete _job; + _job = nullptr; + _error = NoError; + setErrorString(QStringLiteral("")); + _data.clear(); + QIODevice::close(); + } + } + + qint64 size() const override { + return _data.size(); + } + + int error() const { return _error; } + void unsetError() { _error = NoError; } + +protected: + qint64 writeData(const char *, qint64) override { return -1; } + qint64 readData(char *data, qint64 maxSize) override { + Q_UNUSED(maxSize); + Q_ASSERT(_job); + Q_ASSERT(_job->error() == NoError); + Q_ASSERT(data != nullptr); + if(maxSize == 0 || pos() >= _data.length()) { + return 0; + } else if(pos() < _data.length()) { + qint64 bytesToCopy = qMin(maxSize, _data.length() - pos()); + memcpy(data, _data.data() + pos(), bytesToCopy); + return bytesToCopy; + } else { + return -1; + } + } + +private: + QUrl _url; + KIO::StoredTransferJob *_job; + Error _error; + QEventLoop _eventLoop; + QByteArray _data; +}; + + + +struct CategoryProperty { + enum Flag: uint32_t { + Invalid = 0, + #define CATEGORY_PROPERTY_VALUE(val, sym, intVal) sym = intVal, + #include "properties.h" + }; + enum Group: uint32_t { + #define CATEGORY_PROPERTY_GROUP(val, sym, intVal) sym = intVal, + #include "properties.h" + }; + + CategoryProperty(uint32_t value = Unassigned): _value(value) {} + CategoryProperty(const QString &string): _value(fromString(string)) {} + operator uint32_t &() { return _value; } + operator const uint32_t &() const { return _value; } + bool isValid() const { return _value != Invalid; } + +private: + static uint32_t fromString(const QString &string) { + static const QMap map = { + #define CATEGORY_PROPERTY_VALUE(val, sym, intVal) { QStringLiteral(#val), sym }, + #include "properties.h" + }; + return map.contains(string) ? map[string] : uint8_t(Invalid); + } + uint32_t _value; +}; + +struct EastAsianWidthProperty { + enum Value: uint8_t { + Invalid = 0x80, + #define EAST_ASIAN_WIDTH_PROPERTY_VALUE(val, sym, intVal) sym = intVal, + #include "properties.h" + }; + + EastAsianWidthProperty(uint8_t value = Neutral): _value(value) {} + EastAsianWidthProperty(const QString &string): _value(fromString(string)) {} + operator uint8_t &() { return _value; } + operator const uint8_t &() const { return _value; } + bool isValid() const { return _value != Invalid; } + +private: + static uint8_t fromString(const QString &string) { + static const QMap map = { + #define EAST_ASIAN_WIDTH_PROPERTY_VALUE(val, sym, intVal) { QStringLiteral(#val), Value::sym }, + #include "properties.h" + }; + return map.contains(string) ? map[string] : Invalid; + } + uint8_t _value; +}; + +struct EmojiProperty { + enum Flag: uint8_t { + Invalid = 0x80, + #define EMOJI_PROPERTY_VALUE(val, sym, intVal) sym = intVal, + #include "properties.h" + }; + + EmojiProperty(uint8_t value = None): _value(value) {} + EmojiProperty(const QString &string): _value(fromString(string)) {} + operator uint8_t &() { return _value; } + operator const uint8_t &() const { return _value; } + bool isValid() const { return !(_value & Invalid); } + +private: + static uint8_t fromString(const QString &string) { + static const QMap map = { + #define EMOJI_PROPERTY_VALUE(val, sym, intVal) { QStringLiteral(#val), sym }, + #include "properties.h" + }; + return map.contains(string) ? map[string] : uint8_t(Invalid); + } + uint8_t _value; +}; + + + +struct CharacterWidth { + enum Width: int8_t { + Invalid = SCHAR_MIN, + _VALID_START = -3, + Ambiguous = -2, + NonPrintable = -1, + // 0 + // 1 + Unassigned = 1, + // 2 + _VALID_END = 3, + }; + + CharacterWidth(const CharacterWidth &other): _width(other._width) {} + CharacterWidth(int8_t width = Invalid): _width(width) {} + CharacterWidth & operator =(const CharacterWidth &other) { _width = other._width; return *this; } + int operator =(const int8_t width) { _width = width; return _width; } + int width() const { return _width; } + operator int() const { return width(); } + + const QString toString() const { + switch(_width) { + case Ambiguous: return QStringLiteral("Ambiguous"); + case NonPrintable: return QStringLiteral("NonPrintable"); + case 0: return QStringLiteral("0"); + case 1: return QStringLiteral("1"); + case 2: return QStringLiteral("2"); + default: + case Invalid: return QStringLiteral("Invalid"); + } + } + + bool isValid() const { return (_width > _VALID_START && _width < _VALID_END); }; + +private: + int8_t _width; +}; + + + +struct CharacterProperties { + CategoryProperty category; + EastAsianWidthProperty eastAsianWidth; + EmojiProperty emoji; + CharacterWidth customWidth; + // For debug purposes in "details" output generator + uint8_t widthFromPropsRule; +}; + + + +struct UnicodeDataEntry: public UcdEntry { + enum FieldId { + NameId = 0, + CategoryId = 1, + }; + CategoryProperty category() const { return CategoryProperty(this->fields.value(CategoryId)); } +}; + +struct EastAsianWidthEntry: public UcdEntry { + enum FieldId { + WidthId = 0, + }; + EastAsianWidthProperty eastAsianWidth() const { return EastAsianWidthProperty(this->fields.value(WidthId)); } +}; + +struct EmojiDataEntry: public UcdEntry { + enum FieldId { + EmojiId = 0, + }; + EmojiProperty emoji() const { return EmojiProperty(this->fields.value(EmojiId)); } +}; + +struct GenericWidthEntry: public UcdEntry { + enum FieldId { + WidthId = 0, + }; + CharacterWidth width() const { + bool ok; + CharacterWidth w = this->fields.value(WidthId).toInt(&ok, 10); + return (ok && w.isValid()) ? w : CharacterWidth::Invalid; + } +}; + +struct WidthsRange { + struct { uint first; uint last; } cp; + CharacterWidth width; +}; + +QVector rangesFromWidths(const QVector &widths, QPair ucsRange = {0, CODE_POINTS_NUM}) { + QVector ranges; + + if(ucsRange.second >= CODE_POINTS_NUM) + ucsRange.second = widths.size() - 1; + + uint first = ucsRange.first; + for(uint cp = first + 1; cp <= uint(ucsRange.second); ++cp) { + if(widths[first] != widths[cp]) { + ranges.append({{first, cp-1}, widths[cp-1]}); + first = cp; + } + } + ranges.append({{first, uint(ucsRange.second)}, widths[ucsRange.second]}); + + return ranges; +} + +// Real ranges look like this (each continuous letter sequence is a range): +// +// D D D D D D D D 8 ranges +// C C C C C C CC C CC 9 ranges +// BBB BBB B B BBB BBBBBB 6 ranges +// A A A A 4 ranges +// ∑: 27 ranges +// +// To reduce total ranges count, the holes in groups can be filled with ranges +// from groups above them: +// +// D D D D D D D D 8 ranges +// CCC C CCCCC CCCCCCC 4 ranges +// BBBBBBB BBBBBBB BBBBBBBBBBBBBBBB 3 ranges +// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 1 ranges +// ∑: 16 ranges +// +// First range is always without change. Last range (A) can be dropped +// (it always contains everything). Search should be done in order: D, C, B (A). +// For simplicity the funtion returns all ranges, including first and last. +QMap>> mergedRangesFromWidths(const QVector &widths, const QVector widthsSortOrder, + QPair ucsRange = {0, CODE_POINTS_NUM}) { + if(ucsRange.second >= CODE_POINTS_NUM) + ucsRange.second = widths.size() - 1; + QVector ranges = rangesFromWidths(widths, ucsRange); + QMap>> mergedRanges; + + int cmwi; // Currently Merged Width Index + int sri = -1; // Start Range Index (for current width) + int cri; // Currrent Range Index + + // First width ranges are without change. Last one has one range spanning everything, so we can skip this + for(cmwi = 1; cmwi < widthsSortOrder.size() - 1; ++cmwi) { + const CharacterWidth &cmw = widthsSortOrder[cmwi]; // Currently Merged Width + for(cri = 0; cri < ranges.size(); ++cri) { + WidthsRange &cr = ranges[cri]; // Current Range + if(cr.width == cmw) { + // Range is suitable for merge + if(sri < 0) { + // First one, just remember it + sri = cri; + } else { + // Merge + ranges[sri].cp.last = cr.cp.last; + cr.width = CharacterWidth::Invalid; + } + } else { + // Current range has another width - can we continue merging? + if(sri >= 0) { + const int crwi = widthsSortOrder.indexOf(cr.width); // Current Range Width Index + if(!(crwi < cmwi && crwi >= 0)) { + // current range is not above currently merged width - stop merging + sri = -1; + } + } + } + } + } + + for(const auto &range: qAsConst(ranges)) { + if(range.width.isValid() && range.width != widthsSortOrder.last()) + mergedRanges[range.width].append({range.cp.first, range.cp.last}); + } + mergedRanges[widthsSortOrder.last()].append({ucsRange.first, ucsRange.second}); + + return mergedRanges; +} + +namespace generators { + +using GeneratorFunc = bool (*)(QTextStream &, const QVector &, + const QVector &, const QMap &); + +bool code(QTextStream &out, const QVector &props, const QVector &widths, + const QMap &args) { + static constexpr int DIRECT_LUT_SIZE = 256; + + Q_UNUSED(props); + QTextStream eout(stderr, QIODevice::WriteOnly); + + if(args.value(QStringLiteral("param")).isEmpty()) { + eout << QStringLiteral("Template file not specified.") << endl << endl; + return false; + } + QFile templateFile(args.value(QStringLiteral("param"))); + if(!templateFile.open(QIODevice::ReadOnly)) { + eout << QStringLiteral("Could not open file ") << templateFile.fileName() << ": " << templateFile.errorString(); + exit(1); + } + + const QString templateText = QString::fromUtf8(templateFile.readAll()); + templateFile.close(); + + Var::Map data = { + {QStringLiteral("gen-file-warning"), QStringLiteral("THIS IS A GENERATED FILE. DO NOT EDIT.")}, + {QStringLiteral("cmdline"), args.value(QStringLiteral("cmdline"))}, + {QStringLiteral("direct-lut"), Var::Vector(DIRECT_LUT_SIZE)}, + {QStringLiteral("direct-lut-size"), DIRECT_LUT_SIZE}, + {QStringLiteral("ranges-luts"), Var::Vector()}, + {QStringLiteral("ranges-lut-list"), Var::Vector()}, + {QStringLiteral("ranges-lut-list-size"), 0}, + }; + + // Fill direct-lut with widths of 0x00-0xFF + for(unsigned i = 0; i < DIRECT_LUT_SIZE; ++i) { + Q_ASSERT(widths[i].isValid()); + data[QStringLiteral("direct-lut")].vec[i] = int(widths[i]); + } + + static const QVector widthsSortOrder = {CharacterWidth::NonPrintable, 2, CharacterWidth::Ambiguous, 0, 1}; + const QMap>> mergedRanges + = mergedRangesFromWidths(widths, widthsSortOrder, {DIRECT_LUT_SIZE, CODE_POINTS_NUM}); + + // Find last non-empty ranges lut + int lastWidthId = 0; + for(int wi = widthsSortOrder.size() - 1; wi > 0; --wi) { + if(mergedRanges.contains(widthsSortOrder[wi])) { + lastWidthId = wi; + break; + } + } + // Create ranges-luts for all widths except last non-empty one and empty ones + for(int wi = 0; lastWidthId != 0 && wi < lastWidthId; ++wi) { + const CharacterWidth width = widthsSortOrder[wi]; + auto currentMergedRangesIt = mergedRanges.find(width); + if(currentMergedRangesIt == mergedRanges.end() || currentMergedRangesIt.value().isEmpty()) + continue; + const int size = mergedRanges[width].size(); + const QString name = QString(QStringLiteral("LUT_%1")).arg(width.toString().toUpper()); + data[QStringLiteral("ranges-luts")].vec.append(Var::Map { + {QStringLiteral("name"), name}, + {QStringLiteral("ranges"), Var::Vector()}, + {QStringLiteral("size"), size}, + }); + data[QStringLiteral("ranges-lut-list")].vec.append(Var::Map { + {QStringLiteral("width"), int(width)}, + {QStringLiteral("name"), name}, + {QStringLiteral("size"), size}, + }); + auto ¤tLut = data[QStringLiteral("ranges-luts")].vec.last()[QStringLiteral("ranges")].vec; + for(const auto &range: *currentMergedRangesIt) { + Q_ASSERT(range.first <= LAST_CODE_POINT); + Q_ASSERT(range.second <= LAST_CODE_POINT); + currentLut.append(Var(Var::Map {{QStringLiteral("first"), range.first}, {QStringLiteral("last"), range.second}})); + } + } + data[QStringLiteral("ranges-lut-list")].vec.append(Var::Map { + {QStringLiteral("width"), widthsSortOrder[lastWidthId].width()}, + {QStringLiteral("name"), QStringLiteral("nullptr")}, + {QStringLiteral("size"), 1}, + }); + data[QStringLiteral("ranges-lut-list-size")] = mergedRanges.size(); + + Template t(templateText); + t.parse(); + out << t.generate(data); + + return true; +} + +bool list(QTextStream &out, const QVector &props, const QVector &widths, + const QMap &args) { + Q_UNUSED(props); + + out << QStringLiteral("# generated with: ") << args.value(QStringLiteral("cmdline")) << QStringLiteral("\n"); + for(uint cp = 1; cp <= LAST_CODE_POINT; ++cp) { + out << QString::asprintf("%06X ; %2d\n", cp, int(widths[cp])); + } + + return true; +} + +bool ranges(QTextStream &out, const QVector &props, const QVector &widths, + const QMap &args) { + Q_UNUSED(props); + const auto ranges = rangesFromWidths(widths); + + out << QStringLiteral("# generated with: ") << args.value(QStringLiteral("cmdline")) << QStringLiteral("\n"); + for(const WidthsRange &range: ranges) { + if(range.cp.first != range.cp.last) + out << QString::asprintf("%06X..%06X ; %2d\n", range.cp.first, range.cp.last, int(range.width)); + else + out << QString::asprintf("%06X ; %2d\n", range.cp.first, int(range.width)); + } + + return true; +} + +bool compactRanges(QTextStream &out, const QVector &props, const QVector &widths, + const QMap &args) { + Q_UNUSED(props); + static const QVector widthsSortOrder = {CharacterWidth::NonPrintable, 2, CharacterWidth::Ambiguous, 0, 1}; + const auto mergedRanges = mergedRangesFromWidths(widths, widthsSortOrder); + + out << QStringLiteral("# generated with: ") << args.value(QStringLiteral("cmdline")) << QStringLiteral("\n"); + for(const int width: qAsConst(widthsSortOrder)) { + const auto currentMergedRangesIt = mergedRanges.find(width); + if(currentMergedRangesIt == mergedRanges.end() || currentMergedRangesIt.value().isEmpty()) + continue; + for(const auto &range: currentMergedRangesIt.value()) { + if(range.first != range.second) + out << QString::asprintf("%06X..%06X ; %2d\n", range.first, range.second, int(width)); + else + out << QString::asprintf("%06X ; %2d\n", range.first, int(width)); + } + } + + return true; +} + +bool details(QTextStream &out, const QVector &props, const QVector &widths, + const QMap &args) { + out.setFieldAlignment(QTextStream::AlignLeft); + + out << QStringLiteral("# generated with: ") << args.value(QStringLiteral("cmdline")) << QStringLiteral("\n"); + out << QString::asprintf("#%-5s ; %-4s ; %-8s ; %-3s ; %-2s ; %-4s ; %-4s\n", + "CP", "Wdth", "Cat", "EAW", "EM", "CstW", "Rule"); + QMap widthStats; + for(uint cp = 0; cp <= LAST_CODE_POINT; ++cp) { + out << QString::asprintf("%06X ; %4d ; %08X ; %02X ; %02X ; %4d ; %d\n", cp, + int8_t(widths[cp]), uint32_t(props[cp].category), uint8_t(props[cp].eastAsianWidth), + uint8_t(props[cp].emoji), int8_t(props[cp].customWidth), props[cp].widthFromPropsRule); + if(!widthStats.contains(widths[cp])) + widthStats.insert(widths[cp], 0); + widthStats[widths[cp]]++; + } + QMap rangesStats; + const auto ranges = rangesFromWidths(widths); + for(const auto &range: ranges) { + if(!rangesStats.contains(range.width)) + rangesStats.insert(range.width, 0); + rangesStats[range.width]++; + } + out << QStringLiteral("# STATS") << endl; + out << QStringLiteral("#") << endl; + out << QStringLiteral("# Characters count for each width:") << endl; + for(auto wi = widthStats.constBegin(); wi != widthStats.constEnd(); ++wi) { + out << QString::asprintf("# %2d: %7d\n", int(wi.key()), widthStats[wi.key()]); + } + out << QStringLiteral("#") << endl; + out << QStringLiteral("# Ranges count for each width:") << endl; + int howmany = 0; + for(auto wi = rangesStats.constBegin(); wi != rangesStats.constEnd(); ++wi) { + if(howmany >= 20) break; + howmany++; + out << QString::asprintf("# %2d: %7d\n", int(wi.key()), rangesStats[wi.key()]); + } + + return true; +} +} // namespace generators + + + +template +static void processInputFiles(QVector &props, const QStringList &files, const QString &fileTypeName, + void (*cb)(CharacterProperties &prop, const EntryType &entry)) { + static const QRegularExpression PROTOCOL_RE(QStringLiteral(R"#(^[a-z]+://)#"), QRegularExpression::OptimizeOnFirstUsageOption); + for(const QString &fileName: files) { + qInfo().noquote() << QStringLiteral("Parsing as %1: %2").arg(fileTypeName).arg(fileName); + QSharedPointer source = nullptr; + if(PROTOCOL_RE.match(fileName).hasMatch()) { + source.reset(new KIODevice(QUrl(fileName))); + } else { + source.reset(new QFile(fileName)); + } + + if(!source->open(QIODevice::ReadOnly)) { + qCritical() << QStringLiteral("Could not open %1: %2").arg(fileName).arg(source->errorString()); + exit(1); + } + UcdParser p(source.data()); + while(p.hasNext()) { + const auto &e = p.next(); + for(uint cp = e.cp.first; cp <= e.cp.last; ++cp) { + cb(props[cp], e); + } + } + } +} + +static const QString escapeCmdline(const QStringList &args) { + static QString cmdline = QString(); + if(!cmdline.isEmpty()) + return cmdline; + + QTextStream stream(&cmdline, QIODevice::WriteOnly); + + // basename for command name + stream << QFileInfo(args[0]).baseName(); + for(auto it = args.begin() + 1; it != args.end(); ++it) { + if(!it->startsWith(QLatin1Char('-'))) + stream << QStringLiteral(" \"") << QString(*it).replace(QRegularExpression(QStringLiteral(R"(["`$\\])")), QStringLiteral(R"(\\\1)")) << '"'; + else + stream << ' ' << *it; + } + stream.flush(); + return cmdline; +} + +enum ConvertOptions { + AmbiguousWidthOpt = 0, + EmojiOpt = 1, +}; + +// Character width assignment +// +// Rules (from highest to lowest priority): +// +// * Local overlay +// * (not implemented) Character unique properties described in The Unicode Standard, Version 10.0 +// * Unicode category Cc, Cs: -1 +// * Emoji: 2 +// * Unicode category Mn, Me, Cf: 0 +// * East Asian Width W, F: 2 +// * East Asian Width H, N, Na: 1 +// * East Asian Width A: (varies) +// * Unassigned/Undefined/Private Use: 1 +// +// The list is loosely based on character width implementations in Vim 8.1 +// and glibc 2.27. There are a few cases which could look better +// (decomposed Hangul, emoji with modifiers, etc) with different widths, +// but interactive terminal programs (at least vim, zsh, everything based +// on glibc's wcwidth) would see their width as it is implemented now. +static inline CharacterWidth widthFromProps(const CharacterProperties &props, uint cp, const QMap &convertOpts) { + CharacterWidth cw; + auto &widthFromPropsRule = const_cast(props.widthFromPropsRule); + if(props.customWidth.isValid()) { + widthFromPropsRule = 1; + cw = props.customWidth; + + } else if((CategoryProperty::Control | CategoryProperty::Surrogate) & props.category) { + widthFromPropsRule = 2; + cw = CharacterWidth::NonPrintable; + + } else if(convertOpts[EmojiOpt] & props.emoji && !(EmojiProperty::EmojiComponent & props.emoji)) { + widthFromPropsRule = 3; + cw = 2; + + } else if((CategoryProperty::NonspacingMark | CategoryProperty::EnclosingMark | CategoryProperty::Format) & props.category) { + widthFromPropsRule = 4; + cw = 0; + + } else if((EastAsianWidthProperty::Wide | EastAsianWidthProperty::Fullwidth) & props.eastAsianWidth) { + widthFromPropsRule = 5; + cw = 2; + + } else if((EastAsianWidthProperty::Halfwidth | EastAsianWidthProperty::Neutral | EastAsianWidthProperty::Narrow) & props.eastAsianWidth) { + widthFromPropsRule = 6; + cw = 1; + + } else if((CategoryProperty::Unassigned | CategoryProperty::PrivateUse) & props.category) { + widthFromPropsRule = 7; + cw = CharacterWidth::Unassigned; + + } else if((EastAsianWidthProperty::Ambiguous) & props.eastAsianWidth) { + widthFromPropsRule = 8; + cw = convertOpts[AmbiguousWidthOpt]; + + } else if(!props.category.isValid()) { + widthFromPropsRule = 9; + qWarning() << QStringLiteral("Code point U+%1 has invalid category - this should not happen. Assuming \"unassigned\"") + .arg(cp, 4, 16, QLatin1Char('0')); + cw = CharacterWidth::Unassigned; + + } else { + widthFromPropsRule = 10; + qWarning() << QStringLiteral("Code point U+%1 not classified - this should not happen. Assuming non-printable character") + .arg(cp, 4, 16, QLatin1Char('0')); + cw = CharacterWidth::NonPrintable; + } + + return cw; +} + +int main(int argc, char *argv[]) { + static const QMap GENERATOR_FUNCS_MAP = { + {QStringLiteral("code"), generators::code}, + {QStringLiteral("compact-ranges"), generators::compactRanges}, + {QStringLiteral("ranges"), generators::ranges}, + {QStringLiteral("list"), generators::list}, + {QStringLiteral("details"), generators::details}, + {QStringLiteral("dummy"), [](QTextStream &, const QVector &, const QVector &, + const QMap &)->bool {return true;}}, + }; + qSetMessagePattern(QStringLiteral("%{message}")); + + QCoreApplication app(argc, argv); + QCommandLineParser parser; + parser.setApplicationDescription( + QStringLiteral("\nUCD files to characters widths converter.\n") + ); + parser.addHelpOption(); + parser.addOptions({ + {{QStringLiteral("U"), QStringLiteral("unicode-data")}, + QStringLiteral("Path or URL to UnicodeData.txt."), + QStringLiteral("URL|file")}, + {{QStringLiteral("A"), QStringLiteral("east-asian-width")}, + QStringLiteral("Path or URL to EastAsianWidth.txt."), + QStringLiteral("URL|file")}, + {{QStringLiteral("E"), QStringLiteral("emoji-data")}, + QStringLiteral("Path or URL to emoji-data.txt."), + QStringLiteral("URL|file")}, + {{QStringLiteral("W"), QStringLiteral("generic-width")}, + QStringLiteral("Path or URL to generic file with width data. Accepts output from compact-ranges, ranges, list and details generator."), + QStringLiteral("URL|file")}, + + {QStringLiteral("ambiguous-width"), + QStringLiteral("Ambiguous characters width."), + QStringLiteral("separate|1|2"), QString(QStringLiteral("%1")).arg(CharacterWidth::Ambiguous)}, + {QStringLiteral("emoji"), + QStringLiteral("Which emoji emoji subset is treated as emoji."), + QStringLiteral("all|presentation"), QStringLiteral("presentation")}, + + {{QStringLiteral("g"), QStringLiteral("generator")}, + QStringLiteral("Output generator (use \"-\" to list available generators). The code generator requires path to a template file."), + QStringLiteral("generator[:template]"), QStringLiteral("details")}, + }); + parser.addPositionalArgument(QStringLiteral("output"), QStringLiteral("Output file (leave empty for stdout).")); + parser.process(app); + + const QStringList unicodeDataFiles = parser.values(QStringLiteral("unicode-data")); + const QStringList eastAsianWidthFiles = parser.values(QStringLiteral("east-asian-width")); + const QStringList emojiDataFiles = parser.values(QStringLiteral("emoji-data")); + const QStringList genericWidthFiles = parser.values(QStringLiteral("generic-width")); + const QString ambiguousWidthStr = parser.value(QStringLiteral("ambiguous-width")); + const QString emojiStr = parser.value(QStringLiteral("emoji")); + const QString generator = parser.value(QStringLiteral("generator")); + const QString outputFileName = parser.positionalArguments().value(0); + + QTextStream eout(stderr, QIODevice::WriteOnly); + if(unicodeDataFiles.isEmpty() && eastAsianWidthFiles.isEmpty() && emojiDataFiles.isEmpty() && genericWidthFiles.isEmpty()) { + eout << QStringLiteral("Input files not specified.") << endl << endl; + parser.showHelp(1); + } + + static QMap convertOpts = { + {AmbiguousWidthOpt, CharacterWidth::Ambiguous}, + {EmojiOpt, EmojiProperty::EmojiPresentation}, + }; + + if(emojiStr == QStringLiteral("presentation")) + convertOpts[EmojiOpt] = EmojiProperty::EmojiPresentation; + else if(emojiStr == QStringLiteral("all")) + convertOpts[EmojiOpt] = EmojiProperty::Emoji; + else { + convertOpts[EmojiOpt] = EmojiProperty::EmojiPresentation; + qWarning() << QStringLiteral("invalid emoji option value: %1. Assuming \"presentation\".").arg(emojiStr); + } + + if(ambiguousWidthStr == QStringLiteral("separate")) + convertOpts[AmbiguousWidthOpt] = CharacterWidth::Ambiguous; + else if(ambiguousWidthStr == QStringLiteral("1")) + convertOpts[AmbiguousWidthOpt] = 1; + else if(ambiguousWidthStr == QStringLiteral("2")) + convertOpts[AmbiguousWidthOpt] = 2; + else { + convertOpts[AmbiguousWidthOpt] = CharacterWidth::Ambiguous; + qWarning() << QStringLiteral("Invalid ambiguous-width option value: %1. Assuming \"separate\".").arg(emojiStr); + } + + const int sepPos = generator.indexOf(QLatin1Char(':')); + const auto generatorName = generator.left(sepPos); + const auto generatorParam = sepPos >= 0 ? generator.mid(sepPos + 1) : QString(); + + if(!GENERATOR_FUNCS_MAP.contains(generatorName)) { + int status = 0; + if(generatorName != QStringLiteral("-")) { + status = 1; + eout << QStringLiteral("Invalid output generator. Available generators:") << endl; + } + + for(auto it = GENERATOR_FUNCS_MAP.constBegin(); it != GENERATOR_FUNCS_MAP.constEnd(); ++it) { + eout << it.key() << endl; + } + exit(status); + } + auto generatorFunc = GENERATOR_FUNCS_MAP[generatorName]; + + QFile outFile; + if(!outputFileName.isEmpty()) { + outFile.setFileName(outputFileName); + if(!outFile.open(QIODevice::WriteOnly)) { + eout << QStringLiteral("Could not open file ") << outputFileName << QStringLiteral(": ") << outFile.errorString() << endl; + exit(1); + } + } else { + outFile.open(stdout, QIODevice::WriteOnly); + } + QTextStream out(&outFile); + + QVector props(CODE_POINTS_NUM); + + processInputFiles( + props, unicodeDataFiles, QStringLiteral("UnicodeData.txt"), + [](CharacterProperties &prop, const UnicodeDataEntry &entry) { prop.category = entry.category(); }); + + processInputFiles( + props, eastAsianWidthFiles, QStringLiteral("EastAsianWidth.txt"), + [](CharacterProperties &prop, const EastAsianWidthEntry &entry) { prop.eastAsianWidth = entry.eastAsianWidth(); }); + + processInputFiles( + props, emojiDataFiles, QStringLiteral("emoji-data.txt"), + [](CharacterProperties &prop, const EmojiDataEntry &entry) { prop.emoji |= entry.emoji(); }); + + processInputFiles( + props, genericWidthFiles, QStringLiteral("generic width data"), + [](CharacterProperties &prop, const GenericWidthEntry &entry) { prop.customWidth = entry.width(); }); + + qInfo() << "Generating character width data"; + QVector widths(CODE_POINTS_NUM); + widths[0] = 0; // NULL character always has width 0 + for(uint cp = 1; cp <= LAST_CODE_POINT; ++cp) { + widths[cp] = widthFromProps(props[cp], cp, convertOpts); + } + + const QMap generatorArgs = { + {QStringLiteral("cmdline"), escapeCmdline(app.arguments())}, + {QStringLiteral("param"), generatorParam}, + {QStringLiteral("output"), outputFileName.isEmpty() ? QStringLiteral("") : outputFileName}, + }; + + qInfo() << "Generating output"; + if(!generatorFunc(out, props, widths, generatorArgs)) { + parser.showHelp(1); + } + + return 0; +}