feat: add Konsole recipe source and patches

This commit is contained in:
2026-05-07 07:54:52 +01:00
parent 171a96c6af
commit ab85eb7b3d
632 changed files with 713138 additions and 3 deletions
@@ -0,0 +1,25 @@
set(konsolecharacters_SRCS
CharacterWidth.cpp
Hangul.cpp
LineBlockCharacters.cpp
ExtendedCharTable.cpp
)
ecm_qt_declare_logging_category(
konsolecharacters_SRCS
HEADER charactersdebug.h
IDENTIFIER CharactersDebug
CATEGORY_NAME org.kde.konsole.characters
DESCRIPTION "Konsole - characters"
EXPORT KONSOLE
)
add_library(konsolecharacters OBJECT ${konsolecharacters_SRCS})
# Needed to link this static lib to shared libs
set_target_properties(konsolecharacters PROPERTIES POSITION_INDEPENDENT_CODE ON)
generate_export_header(konsolecharacters BASE_NAME konsolecharacters)
target_include_directories(konsolecharacters
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(konsolecharacters Qt::Gui)
@@ -0,0 +1,444 @@
/*
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef CHARACTER_H
#define CHARACTER_H
// Konsole
#include "CharacterColor.h"
#include "CharacterWidth.h"
#include "ExtendedCharTable.h"
#include "Hangul.h"
#include "LineBlockCharacters.h"
// Qt
#include <QVector>
/* clang-format off */
const int LINE_WRAPPED = (1 << 0);
const int LINE_DOUBLEWIDTH = (1 << 1);
const int LINE_DOUBLEHEIGHT_TOP = (1 << 2);
const int LINE_DOUBLEHEIGHT_BOTTOM = (1 << 3);
const int LINE_PROMPT_START = (1 << 4);
const int LINE_INPUT_START = (1 << 5);
const int LINE_OUTPUT_START = (1 << 6);
/* clang-format on */
namespace Konsole
{
#pragma pack(1)
class LineProperty
{
public:
explicit constexpr LineProperty(quint16 f = 0, uint l = 0, uint c = 0)
: flags({f})
, length(l)
, counter(c)
{
}
union {
quint16 all;
struct {
uint wrapped : 1;
uint doublewidth : 1;
uint doubleheight_top : 1;
uint doubleheight_bottom : 1;
uint prompt_start : 1;
uint output_start : 1;
uint input_start : 1;
uint output : 1;
uint error : 1;
} f;
} flags;
qint16 length;
quint16 counter;
bool operator!=(const LineProperty &rhs) const
{
return (flags.all != rhs.flags.all);
}
void resetStarts()
{
flags.all &= ~(LINE_PROMPT_START | LINE_INPUT_START | LINE_OUTPUT_START);
}
quint16 getStarts() const
{
return flags.all & (LINE_PROMPT_START | LINE_INPUT_START | LINE_OUTPUT_START);
}
void setStarts(quint16 starts)
{
flags.all = (flags.all & ~(LINE_PROMPT_START | LINE_INPUT_START | LINE_OUTPUT_START)) | starts;
}
};
typedef union {
quint16 all;
struct {
uint bold : 1;
uint blink : 1;
uint transparent : 1;
uint reverse : 1;
uint italic : 1;
uint cursor : 1;
uint extended : 1;
uint faint : 1;
uint strikeout : 1;
uint conceal : 1;
uint overline : 1;
uint selected : 1;
uint underline : 4;
} f;
} RenditionFlagsC;
typedef quint16 RenditionFlags;
#pragma pack()
typedef quint16 ExtraFlags;
/* clang-format off */
const RenditionFlags DEFAULT_RENDITION = 0;
const RenditionFlags RE_BOLD = (1 << 0);
const RenditionFlags RE_BLINK = (1 << 1);
const RenditionFlags RE_TRANSPARENT = (1 << 2);
const RenditionFlags RE_REVERSE = (1 << 3); // Screen only
const RenditionFlags RE_ITALIC = (1 << 4);
const RenditionFlags RE_CURSOR = (1 << 5);
const RenditionFlags RE_EXTENDED_CHAR = (1 << 6);
const RenditionFlags RE_FAINT = (1 << 7);
const RenditionFlags RE_STRIKEOUT = (1 << 8);
const RenditionFlags RE_CONCEAL = (1 << 9);
const RenditionFlags RE_OVERLINE = (1 << 10);
const RenditionFlags RE_SELECTED = (1 << 11);
const RenditionFlags RE_UNDERLINE_MASK = (15 << 12);
const RenditionFlags RE_UNDERLINE_NONE = 0;
const RenditionFlags RE_UNDERLINE = 1;
const RenditionFlags RE_UNDERLINE_DOUBLE= 2;
const RenditionFlags RE_UNDERLINE_CURL = 3;
const RenditionFlags RE_UNDERLINE_DOT = 4;
const RenditionFlags RE_UNDERLINE_DASH = 5;
const RenditionFlags RE_UNDERLINE_BIT = (1 << 12);
// Masks of flags that matter for drawing what is below/above the text
const RenditionFlags RE_MASK_UNDER = RE_TRANSPARENT | RE_REVERSE | RE_CURSOR | RE_SELECTED;
const RenditionFlags RE_MASK_ABOVE = RE_TRANSPARENT | RE_REVERSE | RE_CURSOR | RE_SELECTED | RE_STRIKEOUT | RE_CONCEAL | RE_OVERLINE | RE_UNDERLINE_MASK;
// flags that affect how the text is drawn
// without RE_REVERSE, since the foreground color is calculated
const RenditionFlags RE_TEXTDRAWING = RE_BOLD | RE_BLINK | RE_TRANSPARENT | RE_ITALIC | RE_CURSOR | RE_FAINT | RE_SELECTED;
const ExtraFlags EF_UNREAL = 0;
const ExtraFlags EF_REAL = (1 << 0);
const ExtraFlags EF_REPL = (3 << 1);
const ExtraFlags EF_REPL_NONE = (0 << 1);
const ExtraFlags EF_REPL_PROMPT = (1 << 1);
const ExtraFlags EF_REPL_INPUT = (2 << 1);
const ExtraFlags EF_REPL_OUTPUT = (3 << 1);
const ExtraFlags EF_UNDERLINE_COLOR = (15 << 3);
const ExtraFlags EF_UNDERLINE_COLOR_1 = (1 << 3);
const ExtraFlags EF_EMOJI_REPRESENTATION = (1 << 7);
const ExtraFlags EF_ASCII_WORD = (1 << 8);
const ExtraFlags EF_BRAHMIC_WORD = (1 << 9);
#define SetULColor(f, m) (((f) & ~EF_UNDERLINE_COLOR) | ((m) * EF_UNDERLINE_COLOR_1))
#define setRepl(f, m) (((f) & ~EF_REPL) | ((m) * EF_REPL_PROMPT))
/* clang-format on */
/**
* A single character in the terminal which consists of a unicode character
* value, foreground and background colors and a set of rendition attributes
* which specify how it should be drawn.
*/
class Character
{
public:
/**
* Constructs a new character.
*
* @param _c The unicode character value of this character.
* @param _f The foreground color used to draw the character.
* @param _b The color used to draw the character's background.
* @param _r A set of rendition flags which specify how this character
* is to be drawn.
* @param _flags Extra flags describing the character. not directly related to its rendition
*/
explicit constexpr Character(uint _c = ' ',
CharacterColor _f = CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR),
CharacterColor _b = CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR),
RenditionFlags _r = DEFAULT_RENDITION,
ExtraFlags _flags = EF_REAL)
: character(_c)
, rendition({_r})
, foregroundColor(_f)
, backgroundColor(_b)
, flags(_flags)
{
}
/** The unicode character value for this character.
*
* if RE_EXTENDED_CHAR is set, character is a hash code which can be used to
* look up the unicode character sequence in the ExtendedCharTable used to
* create the sequence.
*/
char32_t character;
/** A combination of RENDITION flags which specify options for drawing the character. */
RenditionFlagsC rendition;
/** The foreground color used to draw this character. */
CharacterColor foregroundColor;
/** The color used to draw this character's background. */
CharacterColor backgroundColor;
/** Flags which are not specific to rendition
* Indicate whether this character really exists, or exists simply as place holder.
* REPL mode
* Character type (script, etc.)
*/
ExtraFlags flags;
/**
* returns true if the format (color, rendition flag) of the compared characters is equal
*/
constexpr bool equalsFormat(const Character &other) const;
/**
* Compares two characters and returns true if they have the same unicode character value,
* rendition and colors.
*/
friend constexpr bool operator==(const Character &a, const Character &b);
/**
* Compares two characters and returns true if they have different unicode character values,
* renditions or colors.
*/
friend constexpr bool operator!=(const Character &a, const Character &b);
constexpr bool isSpace() const
{
if (rendition.f.extended) {
return false;
} else {
return QChar::isSpace(character);
}
}
int width(bool ignoreWcWidth = false) const
{
return width(character, ignoreWcWidth);
}
int repl() const
{
return flags & EF_REPL;
}
static constexpr int emojiPresentation1Start = 8986;
static constexpr int emojiPresentation1End = 11093;
static constexpr int emojiPresentation2Start = 126980;
static constexpr int emojiPresentation2End = 129782;
/* clang-format off */
static constexpr uint64_t emojiPresentation1Bits[] = {
0x3, 0x0, 0x0, 0x2478000, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0xc00001800000000, 0x3ffc00000000000, 0x200002000000000, 0x4100c1800030080, 0x308090b010000,
0x2e14000000004000, 0x3800000000000000, 0x2000400000, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x840000000000006
};
static constexpr uint64_t emojiPresentation2Bits[] = {
0x1, 0x0, 0x0, 0x800, 0x0, 0x0, 0x7fe400, 0x2ffffffc00000000,
0x77c80000400000, 0x3000, 0x0, 0xf000000000000000,
0xfffbfe001fffffff, 0xfdffffffffffffff, 0xfffffffff000ffff, 0xfff11ffff000f87f,
0xd7ffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xf9ffffffffffffff,
0x3ffffffffffffff, 0x40000ffffff780, 0x100060000, 0xff80000000000000,
0xffffffffffffffff, 0xf000000000000fff, 0xffffffffffffffff, 0x1ff01800e0e7103, 0x0, 0x0, 0x0, 0x10fff0000000,
0x0, 0x0, 0x0, 0x0, 0xff7fffffffffff00, 0xfffffffffffffffb, 0xffffffffffffffff, 0xfffffffffffffff,
0x0, 0xf1f1f00000000000, 0xf07ff1fffffff007, 0x7f00ff03ff003
};
/* clang-format on */
static bool emojiPresentation(uint ucs4)
{
if (ucs4 >= emojiPresentation1Start && ucs4 <= emojiPresentation1End) {
return (emojiPresentation1Bits[(ucs4 - emojiPresentation1Start) / 64] & (uint64_t(1) << ((ucs4 - emojiPresentation1Start) % 64))) != 0;
} else if (ucs4 >= emojiPresentation2Start && ucs4 <= emojiPresentation2End) {
return (emojiPresentation2Bits[(ucs4 - emojiPresentation2Start) / 64] & (uint64_t(1) << ((ucs4 - emojiPresentation2Start) % 64))) != 0;
}
return false;
}
static constexpr int emoji1Start = 8252;
static constexpr int emoji1End = 12953;
static constexpr int emoji2Start = 126980;
static constexpr int emoji2End = 129782;
/* clang-format off */
static constexpr uint64_t emoji1Bits[] = {
0x2001, 0x0, 0x0, 0x2000004000000000, 0x0, 0x60003f000000, 0x0, 0x0,
0x0, 0x0, 0x0, 0x1000c0000000, 0x0, 0x0, 0x70ffe00000080000, 0x0,
0x0, 0x0, 0x40, 0x0, 0x0, 0x400c00000000000, 0x8000000000000010, 0x700c44d2132401f7,
0x8000169800fff050, 0x30c831afc0000c, 0x7bf0600001ac1306, 0x1801022054bf242, 0x1800b850900, 0x1000200e000000, 0x8, 0x0,
0x0, 0x0, 0x0, 0x300000000000000, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x180000e00, 0x2100000, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10000000000000,
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x28000000
};
static constexpr uint64_t emoji2Bits[] = {
0x1, 0x0, 0x0, 0x800, 0x0, 0xc00300000000000, 0x7fe400, 0x6ffffffc00000000,
0x7fc80000400000, 0x3000, 0x0, 0xf000000000000000,
0xffffffff3fffffff, 0xffffffffffffffff, 0xfffffffffcecffff, 0xfffb9fffffffffff,
0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xfbffffffffffffff,
0x3ffffffffffffff, 0x7f980ffffff7e0, 0xc1006013000613c8, 0xffc08810a700e001,
0xffffffffffffffff, 0xf000000000000fff, 0xffffffffffffffff, 0x1ff91a3fe0e7f83, 0x0, 0x0, 0x0, 0x10fff0000000,
0x0, 0x0, 0x0, 0x0, 0xff7fffffffffff00, 0xfffffffffffffffb, 0xffffffffffffffff, 0xfffffffffffffff,
0x0, 0xf1f1f00000000000, 0xf07ff1fffffff007, 0x7f00ff03ff003
};
/* clang-format on */
static bool emoji(uint ucs4)
{
if (ucs4 >= emoji1Start && ucs4 <= emoji1End) {
return (emoji1Bits[(ucs4 - emoji1Start) / 64] & (uint64_t(1) << ((ucs4 - emoji1Start) % 64))) != 0;
} else if (ucs4 >= emoji2Start && ucs4 <= emoji2End) {
return (emoji2Bits[(ucs4 - emoji2Start) / 64] & (uint64_t(1) << ((ucs4 - emoji2Start) % 64))) != 0;
}
return false;
}
static int width(uint ucs4, bool ignoreWcWidth = false)
{
// ASCII
if (ucs4 >= 0x20 && ucs4 < 0x7f)
return 1;
if (ucs4 >= 0xA0 && ucs4 <= 0xFF)
return 1;
// NULL
if (ucs4 == 0)
return 0;
// Control chars
if ((ucs4 > 0x0 && ucs4 < 0x20) || (ucs4 >= 0x7F && ucs4 < 0xA0))
return -1;
if (ignoreWcWidth && 0x04DC0 <= ucs4 && ucs4 <= 0x04DFF) {
// Yijing Hexagram Symbols have wcwidth 2, but unicode width 1
return 1;
}
return characterWidth(ucs4);
}
static int stringWidth(const char32_t *ucs4Str, int len, bool ignoreWcWidth = false)
{
int w = 0;
Hangul::SyllablePos hangulSyllablePos = Hangul::NotInSyllable;
for (int i = 0; i < len; ++i) {
const uint c = ucs4Str[i];
if (!Hangul::isHangul(c)) {
w += width(c, ignoreWcWidth);
hangulSyllablePos = Hangul::NotInSyllable;
} else {
w += Hangul::width(c, width(c, ignoreWcWidth), hangulSyllablePos);
}
}
return w;
}
inline static int stringWidth(const QString &str, bool ignoreWcWidth = false)
{
const auto ucs4Str = str.toStdU32String();
return stringWidth(ucs4Str.data(), ucs4Str.size(), ignoreWcWidth);
}
inline uint baseCodePoint() const
{
if (rendition.f.extended) {
ushort extendedCharLength = 0;
const char32_t *chars = ExtendedCharTable::instance.lookupExtendedChar(character, extendedCharLength);
// FIXME: Coverity-Dereferencing chars, which is known to be nullptr
return chars[0];
}
return character;
}
inline bool isSameScript(Character lhs) const
{
const QChar::Script script = QChar::script(lhs.baseCodePoint());
const QChar::Script currentScript = QChar::script(baseCodePoint());
if (currentScript == QChar::Script_Common || script == QChar::Script_Common || currentScript == QChar::Script_Inherited
|| script == QChar::Script_Inherited) {
return true;
}
return currentScript == script;
}
inline bool hasSameColors(Character lhs) const
{
return lhs.foregroundColor == foregroundColor && lhs.backgroundColor == backgroundColor;
}
inline bool hasSameRendition(Character lhs) const
{
return (lhs.rendition.all & ~RE_EXTENDED_CHAR) == (rendition.all & ~RE_EXTENDED_CHAR) && lhs.flags == flags;
}
inline bool hasSameLineDrawStatus(Character lhs) const
{
const bool lineDraw = LineBlockCharacters::canDraw(character);
return LineBlockCharacters::canDraw(lhs.character) == lineDraw;
}
inline bool hasSameBrailleStatus(Character lhs) const
{
const bool braille = LineBlockCharacters::isBraille(character);
return LineBlockCharacters::isBraille(lhs.character) == braille;
}
inline bool hasSameAttributes(Character lhs) const
{
return hasSameColors(lhs) && hasSameRendition(lhs) && hasSameLineDrawStatus(lhs) && isSameScript(lhs) && hasSameBrailleStatus(lhs);
}
inline bool notSameAttributesText(Character lhs) const
{
// Only compare attributes used for drawing text
return (lhs.rendition.all & RE_TEXTDRAWING) != (rendition.all & RE_TEXTDRAWING) || lhs.foregroundColor != foregroundColor;
}
inline bool isRightHalfOfDoubleWide() const
{
return character == 0;
}
inline void setRightHalfOfDoubleWide()
{
character = 0;
}
};
constexpr bool operator==(const Character &a, const Character &b)
{
return a.character == b.character && a.equalsFormat(b);
}
constexpr bool operator!=(const Character &a, const Character &b)
{
return !operator==(a, b);
}
constexpr bool Character::equalsFormat(const Character &other) const
{
return backgroundColor == other.backgroundColor && foregroundColor == other.foregroundColor && rendition.all == other.rendition.all;
}
}
Q_DECLARE_TYPEINFO(Konsole::Character, Q_MOVABLE_TYPE);
#endif // CHARACTER_H
@@ -0,0 +1,270 @@
/*
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef CHARACTERCOLOR_H
#define CHARACTERCOLOR_H
// Qt
#include <QColor>
namespace Konsole
{
// Attributed Character Representations ///////////////////////////////
// Colors
#define BASE_COLORS (2 + 8)
#define INTENSITIES 3
#define TABLE_COLORS (INTENSITIES * BASE_COLORS)
enum ColorTableIndex {
ColorFgIndex,
ColorBgIndex,
Color0Index,
Color1Index,
Color2Index,
Color3Index,
Color4Index,
Color5Index,
Color6Index,
Color7Index,
ColorFgIntenseIndex,
ColorBgIntenseIndex,
Color0IntenseIndex,
Color1IntenseIndex,
Color2IntenseIndex,
Color3IntenseIndex,
Color4IntenseIndex,
Color5IntenseIndex,
Color6IntenseIndex,
Color7IntenseIndex,
ColorFgFaintIndex,
ColorBgFaintIndex,
Color0FaintIndex,
Color1FaintIndex,
Color2FaintIndex,
Color3FaintIndex,
Color4FaintIndex,
Color5FaintIndex,
Color6FaintIndex,
Color7FaintIndex,
};
#define DEFAULT_FORE_COLOR 0
#define DEFAULT_BACK_COLOR 1
/* CharacterColor is a union of the various color spaces.
Assignment is as follows:
Type - Space - Values
0 - Undefined - u: 0, v:0 w:0
1 - Default - u: 0..1 v:intense w:0
2 - System - u: 0..7 v:intense w:0
3 - Index(256) - u: 16..255 v:0 w:0
4 - RGB - u: 0..255 v:0..256 w:0..256
``intense'' is either 0 (normal), 1 (intensive), or 2 (faint)
Default color space has two separate colors, namely
default foreground and default background color.
*/
#define COLOR_SPACE_UNDEFINED 0
#define COLOR_SPACE_DEFAULT 1
#define COLOR_SPACE_SYSTEM 2
#define COLOR_SPACE_256 3
#define COLOR_SPACE_RGB 4
/**
* Describes the color of a single character in the terminal.
*/
class CharacterColor
{
friend class Character;
public:
/** Constructs a new CharacterColor whose color and color space are undefined. */
constexpr CharacterColor()
: _colorSpace(COLOR_SPACE_UNDEFINED)
, _u(0)
, _v(0)
, _w(0)
{
}
/**
* Constructs a new CharacterColor using the specified @p colorSpace and with
* color value @p co
*
* The meaning of @p co depends on the @p colorSpace used.
*
* TODO : Document how @p co relates to @p colorSpace
*
* TODO : Add documentation about available color spaces.
*/
constexpr CharacterColor(quint8 colorSpace, int co)
: _colorSpace(colorSpace)
, _u(0)
, _v(0)
, _w(0)
{
switch (colorSpace) {
case COLOR_SPACE_DEFAULT:
_u = co & 1;
break;
case COLOR_SPACE_SYSTEM:
_u = co & 7;
_v = (co >> 3) & 3;
break;
case COLOR_SPACE_256:
_u = co & 255;
break;
case COLOR_SPACE_RGB:
_u = co >> 16;
_v = co >> 8;
_w = co;
break;
default:
_colorSpace = COLOR_SPACE_UNDEFINED;
}
}
constexpr quint8 colorSpace() const
{
return _colorSpace;
}
constexpr void termColor(int *u, int *v, int *w)
{
*u = _u;
*v = _v;
*w = _w;
}
/**
* Returns true if this character color entry is valid.
*/
constexpr bool isValid() const
{
return _colorSpace != COLOR_SPACE_UNDEFINED;
}
/**
* Set this color as an intensive system color.
*
* This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
* color spaces.
*/
void setIntensive();
/**
* Set this color as a faint system color.
*
* This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
* color spaces.
*/
void setFaint();
/**
* Returns the color within the specified color @p base
*
* The @p base is only used if this color is one of the 16 system colors, otherwise
* it is ignored.
*/
constexpr QColor color(const QColor *base) const;
/**
* Compares two colors and returns true if they represent the same color value and
* use the same color space.
*/
friend constexpr bool operator==(const CharacterColor a, const CharacterColor b)
{
return std::tie(a._colorSpace, a._u, a._v, a._w) == std::tie(b._colorSpace, b._u, b._v, b._w);
}
/**
* Compares two colors and returns true if they represent different color values
* or use different color spaces.
*/
friend constexpr bool operator!=(const CharacterColor a, const CharacterColor b)
{
return !operator==(a, b);
}
private:
quint8 _colorSpace;
// bytes storing the character color
quint8 _u;
quint8 _v;
quint8 _w;
};
constexpr QColor color256(quint8 u, const QColor *base)
{
// 0.. 16: system colors
if (u < 8) {
return base[u + 2];
}
u -= 8;
if (u < 8) {
return base[u + 2 + BASE_COLORS];
}
u -= 8;
// 16..231: 6x6x6 rgb color cube
if (u < 216) {
return QColor(((u / 36) % 6) ? (40 * ((u / 36) % 6) + 55) : 0,
((u / 6) % 6) ? (40 * ((u / 6) % 6) + 55) : 0,
((u / 1) % 6) ? (40 * ((u / 1) % 6) + 55) : 0);
}
u -= 216;
// 232..255: gray, leaving out black and white
int gray = u * 10 + 8;
return QColor(gray, gray, gray);
}
constexpr QColor CharacterColor::color(const QColor *base) const
{
switch (_colorSpace) {
case COLOR_SPACE_DEFAULT:
return base[_u + 0 + (_v * BASE_COLORS)];
case COLOR_SPACE_SYSTEM:
return base[_u + 2 + (_v * BASE_COLORS)];
case COLOR_SPACE_256:
return color256(_u, base);
case COLOR_SPACE_RGB:
return QColor(_u, _v, _w);
case COLOR_SPACE_UNDEFINED:
return QColor();
}
Q_ASSERT(false); // invalid color space
return QColor();
}
inline void CharacterColor::setIntensive()
{
if (_colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT) {
_v = 1;
}
}
inline void CharacterColor::setFaint()
{
if (_colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT) {
_v = 2;
}
}
}
#endif // CHARACTERCOLOR_H
@@ -0,0 +1,155 @@
/*
SPDX-FileCopyrightText: 2018 Mariusz Glebocki <mglb@arccos-1.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
/* clang-format off */
//
// THIS IS A GENERATED FILE. DO NOT EDIT.
//
// CharacterWidth.cpp file is automatically generated - do not edit it.
//
// To build uni2characterwidth binary, add
// -DKONSOLE_BUILD_UNI2CHARACTERWIDTH=TRUE to cmake-options
//
// To change anything here, edit CharacterWidth.src.cpp and regenerate the file
// using following command:
//
// uni2characterwidth -U "https://unicode.org/Public/15.0.0/ucd/UnicodeData.txt" -A "https://unicode.org/Public/15.0.0/ucd/EastAsianWidth.txt" -E "https://unicode.org/Public/15.0.0/ucd/emoji/emoji-data.txt" -W "tools/uni2characterwidth/overrides.txt" --ambiguous-width=1 --emoji=presentation -g "code:src/characters/CharacterWidth.src.cpp" "src/characters/CharacterWidth.cpp"
//
#include "CharacterWidth.h"
struct Range {
uint first, last;
};
/* clang-format on */
struct RangeLut {
int8_t width;
const Range *const lut;
int size;
};
enum {
InvalidWidth = INT8_MIN,
};
/* clang-format off */
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,0x0031e3},{0x0031f0,0x00321e},{0x003220,0x003247},
{0x003250,0x00a48c},{0x00a490,0x00a4c6},{0x00a960,0x00a97c},{0x00ac00,0x00d7a3},{0x00f900,0x00faff},{0x00fe10,0x00fe19},{0x00fe30,0x00fe52},{0x00fe54,0x00fe66},
{0x00fe68,0x00fe6b},{0x00ff01,0x00ff60},{0x00ffe0,0x00ffe6},{0x016fe0,0x016fe3},{0x016ff0,0x016ff1},{0x017000,0x0187f7},{0x018800,0x018cd5},{0x018d00,0x018d08},
{0x01aff0,0x01aff3},{0x01aff5,0x01affb},{0x01affd,0x01affe},{0x01b000,0x01b122},{0x01b132,0x01b132},{0x01b150,0x01b152},{0x01b155,0x01b155},{0x01b164,0x01b167},
{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},{0x01f6d5,0x01f6d7},
{0x01f6dc,0x01f6df},{0x01f6eb,0x01f6ec},{0x01f6f4,0x01f6fc},{0x01f7e0,0x01f7eb},{0x01f7f0,0x01f7f0},{0x01f90c,0x01f93a},{0x01f93c,0x01f945},{0x01f947,0x01f9ff},
{0x01fa70,0x01fa7c},{0x01fa80,0x01fa88},{0x01fa90,0x01fabd},{0x01fabf,0x01fac5},{0x01face,0x01fadb},{0x01fae0,0x01fae8},{0x01faf0,0x01faf8},{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},{0x000890,0x000891},{0x000898,0x00089f},{0x0008ca,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},{0x000b55,0x000b56},{0x000b62,0x000b63},
{0x000b82,0x000b82},{0x000bc0,0x000bc0},{0x000bcd,0x000bcd},{0x000c00,0x000c00},{0x000c04,0x000c04},{0x000c3c,0x000c3c},{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},{0x000d81,0x000d81},{0x000dca,0x000dca},
{0x000dd2,0x000dd4},{0x000dd6,0x000dd6},{0x000e31,0x000e31},{0x000e34,0x000e3a},{0x000e47,0x000e4e},{0x000eb1,0x000eb1},{0x000eb4,0x000ebc},{0x000ec8,0x000ece},
{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,0x0011ff},{0x00135d,0x00135f},{0x001712,0x001714},
{0x001732,0x001733},{0x001752,0x001753},{0x001772,0x001773},{0x0017b4,0x0017b5},{0x0017b7,0x0017bd},{0x0017c6,0x0017c6},{0x0017c9,0x0017d3},{0x0017dd,0x0017dd},
{0x00180b,0x00180f},{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,0x001ace},{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,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},{0x00a82c,0x00a82c},{0x00a8c4,0x00a8c5},{0x00a8e0,0x00a8f1},{0x00a8ff,0x00a8ff},{0x00a926,0x00a92d},{0x00a947,0x00a951},
{0x00a980,0x00a982},{0x00a9b3,0x00a9b3},{0x00a9b6,0x00a9b9},{0x00a9bc,0x00a9bd},{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},{0x00d7b0,0x00d7c6},{0x00d7cb,0x00d7fb},{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},{0x010eab,0x010eac},{0x010efd,0x010eff},
{0x010f46,0x010f50},{0x010f82,0x010f85},{0x011001,0x011001},{0x011038,0x011046},{0x011070,0x011070},{0x011073,0x011074},{0x01107f,0x011081},{0x0110b3,0x0110b6},
{0x0110b9,0x0110ba},{0x0110bd,0x0110bd},{0x0110c2,0x0110c2},{0x0110cd,0x0110cd},{0x011100,0x011102},{0x011127,0x01112b},{0x01112d,0x011134},{0x011173,0x011173},
{0x011180,0x011181},{0x0111b6,0x0111be},{0x0111c9,0x0111cc},{0x0111cf,0x0111cf},{0x01122f,0x011231},{0x011234,0x011234},{0x011236,0x011237},{0x01123e,0x01123e},
{0x011241,0x011241},{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},
{0x01193b,0x01193c},{0x01193e,0x01193e},{0x011943,0x011943},{0x0119d4,0x0119d7},{0x0119da,0x0119db},{0x0119e0,0x0119e0},{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},{0x011f00,0x011f01},{0x011f36,0x011f3a},
{0x011f40,0x011f40},{0x011f42,0x011f42},{0x013430,0x013440},{0x013447,0x013455},{0x016af0,0x016af4},{0x016b30,0x016b36},{0x016f4f,0x016f4f},{0x016f8f,0x016f92},
{0x016fe4,0x016fe4},{0x01bc9d,0x01bc9e},{0x01bca0,0x01bca3},{0x01cf00,0x01cf2d},{0x01cf30,0x01cf46},{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},{0x01e08f,0x01e08f},{0x01e130,0x01e136},{0x01e2ae,0x01e2ae},
{0x01e2ec,0x01e2ef},{0x01e4ec,0x01e4ef},{0x01e8d0,0x01e8d6},{0x01e944,0x01e94a},{0x0e0001,0x0e0001},{0x0e0020,0x0e007f},{0x0e0100,0x0e01ef},
};
static constexpr const RangeLut RANGE_LUT_LIST[] = {
{-1, LUT_NONPRINTABLE, 1},
{ 2, LUT_2 , 121},
{ 0, LUT_0 , 359},
{ 1, nullptr , 1},
};
static constexpr const int RANGE_LUT_LIST_SIZE = 4;
/* clang-format on */
int 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;
}
@@ -0,0 +1,8 @@
#ifndef CHARACTERWIDTH_H
#define CHARACTERWIDTH_H
#include <QtGlobal>
int characterWidth(uint ucs4);
#endif
@@ -0,0 +1,92 @@
/*
SPDX-FileCopyrightText: 2018 Mariusz Glebocki <mglb@arccos-1.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
/* clang-format off */
«*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 build uni2characterwidth binary, add
// -DKONSOLE_BUILD_UNI2CHARACTERWIDTH=TRUE to cmake-options
//
// To change anything here, edit CharacterWidth.src.cpp and regenerate the file
// using following command:
//
// «cmdline»
//
#include "CharacterWidth.h"
struct Range {
uint first, last;
};
/* clang-format on */
struct RangeLut {
int8_t width;
const Range *const lut;
int size;
};
enum {
InvalidWidth = INT8_MIN,
};
/* clang-format off */
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»;
/* clang-format on */
int 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;
}
@@ -0,0 +1,121 @@
/*
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
// Own
#include "ExtendedCharTable.h"
#include "charactersdebug.h"
using namespace Konsole;
ExtendedCharTable::ExtendedCharTable() = default;
ExtendedCharTable::~ExtendedCharTable()
{
// free all allocated character buffers
QHashIterator<uint, char32_t *> iter(_extendedCharTable);
while (iter.hasNext()) {
iter.next();
delete[] iter.value();
}
}
// global instance
ExtendedCharTable ExtendedCharTable::instance;
char32_t ExtendedCharTable::createExtendedChar(const char32_t *unicodePoints, ushort length, const pExtendedChars extendedChars)
{
// look for this sequence of points in the table
uint hash = extendedCharHash(unicodePoints, length);
const uint initialHash = hash;
bool triedCleaningSolution = false;
// check existing entry for match
while (_extendedCharTable.contains(hash) && hash != 0) { // 0 has a special meaning for chars so we don't use it
if (extendedCharMatch(hash, unicodePoints, length)) {
// this sequence already has an entry in the table,
// return its hash
return hash;
}
// if hash is already used by another, different sequence of unicode character
// points then try next hash
hash++;
if (hash == initialHash) {
if (!triedCleaningSolution) {
triedCleaningSolution = true;
// All the hashes are full, go to all Screens and try to free any
// This is slow but should happen very rarely
QSet<uint> usedExtendedChars = extendedChars();
QHash<uint, char32_t *>::iterator it = _extendedCharTable.begin();
QHash<uint, char32_t *>::iterator itEnd = _extendedCharTable.end();
while (it != itEnd) {
if (usedExtendedChars.contains(it.key())) {
++it;
} else {
it = _extendedCharTable.erase(it);
}
}
} else {
qCDebug(CharactersDebug) << "Using all the extended char hashes, going to miss this extended character";
return 0;
}
}
}
// add the new sequence to the table and
// return that index
auto buffer = new char32_t[length + 1];
buffer[0] = length;
std::copy_n(unicodePoints, length, &buffer[1]);
_extendedCharTable.insert(hash, buffer);
return hash;
}
char32_t *ExtendedCharTable::lookupExtendedChar(uint hash, ushort &length) const
{
// look up index in table and if found, set the length
// argument and return a pointer to the character sequence
char32_t *buffer = _extendedCharTable[hash];
if (buffer != nullptr) {
length = ushort(buffer[0]);
return buffer + 1;
}
length = 0;
return nullptr;
}
uint ExtendedCharTable::extendedCharHash(const char32_t *unicodePoints, ushort length) const
{
uint hash = 0;
for (ushort i = 0; i < length; i++) {
hash = 31 * hash + unicodePoints[i];
}
return hash;
}
bool ExtendedCharTable::extendedCharMatch(uint hash, const char32_t *unicodePoints, ushort length) const
{
char32_t *entry = _extendedCharTable[hash];
// compare given length with stored sequence length ( given as the first ushort in the
// stored buffer )
if (entry == nullptr || entry[0] != length) {
return false;
}
// if the lengths match, each character must be checked. the stored buffer starts at
// entry[1]
for (int i = 0; i < length; i++) {
if (entry[i + 1] != unicodePoints[i]) {
return false;
}
}
return true;
}
@@ -0,0 +1,74 @@
/*
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef EXTENDEDCHARTABLE_H
#define EXTENDEDCHARTABLE_H
// Qt
#include <QHash>
#include <QSet>
#include <functional>
namespace Konsole
{
/**
* A table which stores sequences of unicode characters, referenced
* by hash keys. The hash key itself is the same size as a unicode
* character ( char32_t ) so that it can occupy the same space in
* a structure.
*/
class ExtendedCharTable
{
public:
typedef std::function<QSet<uint>()> pExtendedChars;
/** Constructs a new character table. */
ExtendedCharTable();
~ExtendedCharTable();
/**
* Adds a sequences of unicode characters to the table and returns
* a hash code which can be used later to look up the sequence
* using lookupExtendedChar()
*
* If the same sequence already exists in the table, the hash
* of the existing sequence will be returned.
*
* @param unicodePoints An array of unicode character points
* @param length Length of @p unicodePoints
*/
char32_t createExtendedChar(const char32_t *unicodePoints, ushort length, const pExtendedChars extendedChars);
/**
* Looks up and returns a pointer to a sequence of unicode characters
* which was added to the table using createExtendedChar().
*
* @param hash The hash key returned by createExtendedChar()
* @param length This variable is set to the length of the
* character sequence.
*
* @return A unicode character sequence of size @p length.
*/
char32_t *lookupExtendedChar(uint hash, ushort &length) const;
/** The global ExtendedCharTable instance. */
static ExtendedCharTable instance;
private:
// calculates the hash key of a sequence of unicode points of size 'length'
uint extendedCharHash(const char32_t *unicodePoints, ushort length) const;
// tests whether the entry in the table specified by 'hash' matches the
// character sequence 'unicodePoints' of size 'length'
bool extendedCharMatch(uint hash, const char32_t *unicodePoints, ushort length) const;
// internal, maps hash keys to character sequence buffers. The first uint
// in each value is the length of the buffer, followed by the uints in the buffer
// themselves.
QHash<uint, char32_t *> _extendedCharTable;
};
}
#endif // end of EXTENDEDCHARTABLE_H
@@ -0,0 +1,154 @@
/*
SPDX-FileCopyrightText: 2022 Luis Javier Merino Morán <ninjalj@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
// Own
#include "Hangul.h"
// Qt
// Konsole
#include "Character.h"
namespace Konsole
{
int Hangul::width(uint c, int widthFromTable, enum Hangul::SyllablePos &syllablePos)
{
int w = 0;
switch (syllablePos) {
case NotInSyllable:
if (isLeadingJamo(c)) {
// new Hangul syllable
syllablePos = AtLeadingJamo;
w += widthFromTable;
} else if (isLvSyllable(c)) {
// new Hangul syllable
syllablePos = AtVowelJamo;
w += widthFromTable;
} else if (isLvtSyllable(c)) {
// new Hangul syllable
syllablePos = AtTrailingJamo;
w += widthFromTable;
} else if (isVowelJamo(c)) {
// invalid here, isolated Jamo
w += 2;
} else if (isTrailingJamo(c)) {
// invalid here, isolated Jamo
w += 2;
}
break;
case AtLeadingJamo:
if (isLeadingJamo(c)) {
// conjoin
} else if (isLvSyllable(c)) {
// new Hangul syllable
syllablePos = AtVowelJamo;
w += widthFromTable;
} else if (isLvtSyllable(c)) {
// new Hangul syllable
syllablePos = AtTrailingJamo;
w += widthFromTable;
} else if (isVowelJamo(c)) {
syllablePos = AtVowelJamo;
// conjoin
} else if (isTrailingJamo(c)) {
// invalid here, isolated Jamo
syllablePos = NotInSyllable;
w += 2;
}
break;
case AtVowelJamo:
if (isLeadingJamo(c)) {
// new Hangul syllable
syllablePos = AtLeadingJamo;
w += widthFromTable;
} else if (isLvSyllable(c)) {
// new Hangul syllable
syllablePos = AtVowelJamo;
w += widthFromTable;
} else if (isLvtSyllable(c)) {
// new Hangul syllable
syllablePos = AtTrailingJamo;
w += widthFromTable;
} else if (isVowelJamo(c)) {
// conjoin
} else if (isTrailingJamo(c)) {
syllablePos = AtTrailingJamo;
// conjoin
}
break;
case AtTrailingJamo:
if (isLeadingJamo(c)) {
// new Hangul syllable
syllablePos = AtLeadingJamo;
w += widthFromTable;
} else if (isLvSyllable(c)) {
// new Hangul syllable
syllablePos = AtVowelJamo;
w += widthFromTable;
} else if (isLvtSyllable(c)) {
// new Hangul syllable
syllablePos = AtTrailingJamo;
w += widthFromTable;
} else if (isVowelJamo(c)) {
// invalid here, isolated Jamo
syllablePos = NotInSyllable;
w += 2;
} else if (isTrailingJamo(c)) {
// conjoin
}
}
return w;
}
void Hangul::updateHangulSyllablePos(Hangul::SyllablePos &syllablePos, uint c)
{
if (!isHangul(c)) {
syllablePos = NotInSyllable;
}
(void)Hangul::width(c, 0, syllablePos);
}
bool Hangul::validSyllableContinuation(Hangul::SyllablePos syllablePos, uint c)
{
SyllableType type = jamoType(c);
switch (syllablePos) {
case AtLeadingJamo:
return type != Trailing_Jamo && type != Not_Applicable;
case AtVowelJamo:
return type == Trailing_Jamo || type == Vowel_Jamo;
case AtTrailingJamo:
return type == Trailing_Jamo;
default:
return false;
}
}
bool Hangul::combinesWith(Character prevChar, uint c)
{
Hangul::SyllablePos syllablePos = Hangul::NotInSyllable;
if (prevChar.rendition.f.extended == 0) {
const uint prev = prevChar.character;
updateHangulSyllablePos(syllablePos, prev);
} else {
ushort extendedCharLength;
const char32_t *oldChars = ExtendedCharTable::instance.lookupExtendedChar(prevChar.character, extendedCharLength);
if (oldChars == nullptr) {
return false;
} else {
for (int i = 0; i < extendedCharLength; i++) {
updateHangulSyllablePos(syllablePos, oldChars[i]);
}
}
}
return validSyllableContinuation(syllablePos, c);
}
}
@@ -0,0 +1,90 @@
/*
SPDX-FileCopyrightText: 2022 Luis Javier Merino Morán <ninjalj@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef HANGUL_H
#define HANGUL_H
#include <QtGlobal>
namespace Konsole
{
class Character;
class Hangul
{
public:
// See HangulSyllableType.txt from the Unicode data distribution
enum SyllableType {
Not_Applicable,
Leading_Jamo,
Vowel_Jamo,
Trailing_Jamo,
LV_Syllable,
LVT_Syllable,
};
enum SyllablePos {
NotInSyllable,
AtLeadingJamo,
AtVowelJamo,
AtTrailingJamo,
};
static int width(uint c, int widthFromTable, enum SyllablePos &syllablePos);
static bool combinesWith(Character prev, uint c);
static bool isHangul(const uint c)
{
return (c >= 0x1100 && c <= 0x11ff) || (c >= 0xa960 && c <= 0xa97f) || (c >= 0xd7b0 && c <= 0xd7ff) || (c >= 0xac00 && c <= 0xd7a3);
}
private:
static bool isLeadingJamo(const uint c)
{
return (c >= 0x1100 && c <= 0x115f) || (c >= 0xa960 && c <= 0xa97f);
}
static bool isVowelJamo(const uint c)
{
return (c >= 0x1160 && c <= 0x11a7) || (c >= 0xd7b0 && c <= 0xd7c6);
}
static bool isTrailingJamo(const uint c)
{
return (c >= 0x11a8 && c <= 0x11ff) || (c >= 0xd7cb && c <= 0xd7ff);
}
static bool isLvSyllable(const uint c)
{
return (c >= 0xac00 && c <= 0xd7a3) && (c % 0x1c == 0);
}
static bool isLvtSyllable(const uint c)
{
return (c >= 0xac00 && c <= 0xd7a3) && (c % 0x1c != 0);
}
static SyllableType jamoType(const uint c)
{
// clang-format off
if (isLeadingJamo(c)) return Leading_Jamo;
if (isVowelJamo(c)) return Vowel_Jamo;
if (isTrailingJamo(c)) return Trailing_Jamo;
if (isLvSyllable(c)) return LV_Syllable;
if (isLvtSyllable(c)) return LVT_Syllable;
return Not_Applicable;
// clang-format on
}
static void updateHangulSyllablePos(Hangul::SyllablePos &syllablePos, uint c);
static bool validSyllableContinuation(Hangul::SyllablePos syllablePos, uint c);
};
}
#endif // HANGUL_H
@@ -0,0 +1,872 @@
/*
SPDX-FileCopyrightText: 2018-2019 Mariusz Glebocki <mglb@arccos-1.net>
SPDX-FileCopyrightText: 2018 Martin T. H. Sandsmark <martin.sandsmark@kde.org>
SPDX-FileCopyrightText: 2015-2018 Kurt Hindenburg <kurt.hindenburg@gmail.com>
SPDX-FileCopyrightText: 2005 Maksim Orlovich <maksim@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
// Own
#include "LineBlockCharacters.h"
// Qt
#include <QPainterPath>
namespace Konsole
{
namespace LineBlockCharacters
{
enum LineType { LtNone = 0, LtDouble = 1, LtLight = 2, LtHeavy = 3 };
// PackedLineTypes is an 8-bit number representing types of 4 line character's lines. Each line is
// represented by 2 bits. Lines order, starting from MSB: top, right, bottom, left.
static inline constexpr quint8 makePackedLineTypes(LineType top, LineType right, LineType bottom, LineType left)
{
return (int(top) & 3) << 6 | (int(right) & 3) << 4 | (int(bottom) & 3) << 2 | (int(left) & 3);
}
/* clang-format off */
static constexpr const quint8 PackedLineTypesLut[] = {
// top right bottom left
makePackedLineTypes(LtNone , LtLight , LtNone , LtLight ), /* U+2500 ─ */
makePackedLineTypes(LtNone , LtHeavy , LtNone , LtHeavy ), /* U+2501 ━ */
makePackedLineTypes(LtLight , LtNone , LtLight , LtNone ), /* U+2502 │ */
makePackedLineTypes(LtHeavy , LtNone , LtHeavy , LtNone ), /* U+2503 ┃ */
0, 0, 0, 0, 0, 0, 0, 0, /* U+2504-0x250b */
makePackedLineTypes(LtNone , LtLight , LtLight , LtNone ), /* U+250C ┌ */
makePackedLineTypes(LtNone , LtHeavy , LtLight , LtNone ), /* U+250D ┍ */
makePackedLineTypes(LtNone , LtLight , LtHeavy , LtNone ), /* U+250E ┎ */
makePackedLineTypes(LtNone , LtHeavy , LtHeavy , LtNone ), /* U+250F ┏ */
makePackedLineTypes(LtNone , LtNone , LtLight , LtLight ), /* U+2510 ┐ */
makePackedLineTypes(LtNone , LtNone , LtLight , LtHeavy ), /* U+2511 ┑ */
makePackedLineTypes(LtNone , LtNone , LtHeavy , LtLight ), /* U+2512 ┒ */
makePackedLineTypes(LtNone , LtNone , LtHeavy , LtHeavy ), /* U+2513 ┓ */
makePackedLineTypes(LtLight , LtLight , LtNone , LtNone ), /* U+2514 └ */
makePackedLineTypes(LtLight , LtHeavy , LtNone , LtNone ), /* U+2515 ┕ */
makePackedLineTypes(LtHeavy , LtLight , LtNone , LtNone ), /* U+2516 ┖ */
makePackedLineTypes(LtHeavy , LtHeavy , LtNone , LtNone ), /* U+2517 ┗ */
makePackedLineTypes(LtLight , LtNone , LtNone , LtLight ), /* U+2518 ┘ */
makePackedLineTypes(LtLight , LtNone , LtNone , LtHeavy ), /* U+2519 ┙ */
makePackedLineTypes(LtHeavy , LtNone , LtNone , LtLight ), /* U+251A ┚ */
makePackedLineTypes(LtHeavy , LtNone , LtNone , LtHeavy ), /* U+251B ┛ */
makePackedLineTypes(LtLight , LtLight , LtLight , LtNone ), /* U+251C ├ */
makePackedLineTypes(LtLight , LtHeavy , LtLight , LtNone ), /* U+251D ┝ */
makePackedLineTypes(LtHeavy , LtLight , LtLight , LtNone ), /* U+251E ┞ */
makePackedLineTypes(LtLight , LtLight , LtHeavy , LtNone ), /* U+251F ┟ */
makePackedLineTypes(LtHeavy , LtLight , LtHeavy , LtNone ), /* U+2520 ┠ */
makePackedLineTypes(LtHeavy , LtHeavy , LtLight , LtNone ), /* U+2521 ┡ */
makePackedLineTypes(LtLight , LtHeavy , LtHeavy , LtNone ), /* U+2522 ┢ */
makePackedLineTypes(LtHeavy , LtHeavy , LtHeavy , LtNone ), /* U+2523 ┣ */
makePackedLineTypes(LtLight , LtNone , LtLight , LtLight ), /* U+2524 ┤ */
makePackedLineTypes(LtLight , LtNone , LtLight , LtHeavy ), /* U+2525 ┥ */
makePackedLineTypes(LtHeavy , LtNone , LtLight , LtLight ), /* U+2526 ┦ */
makePackedLineTypes(LtLight , LtNone , LtHeavy , LtLight ), /* U+2527 ┧ */
makePackedLineTypes(LtHeavy , LtNone , LtHeavy , LtLight ), /* U+2528 ┨ */
makePackedLineTypes(LtHeavy , LtNone , LtLight , LtHeavy ), /* U+2529 ┩ */
makePackedLineTypes(LtLight , LtNone , LtHeavy , LtHeavy ), /* U+252A ┪ */
makePackedLineTypes(LtHeavy , LtNone , LtHeavy , LtHeavy ), /* U+252B ┫ */
makePackedLineTypes(LtNone , LtLight , LtLight , LtLight ), /* U+252C ┬ */
makePackedLineTypes(LtNone , LtLight , LtLight , LtHeavy ), /* U+252D ┭ */
makePackedLineTypes(LtNone , LtHeavy , LtLight , LtLight ), /* U+252E ┮ */
makePackedLineTypes(LtNone , LtHeavy , LtLight , LtHeavy ), /* U+252F ┯ */
makePackedLineTypes(LtNone , LtLight , LtHeavy , LtLight ), /* U+2530 ┰ */
makePackedLineTypes(LtNone , LtLight , LtHeavy , LtHeavy ), /* U+2531 ┱ */
makePackedLineTypes(LtNone , LtHeavy , LtHeavy , LtLight ), /* U+2532 ┲ */
makePackedLineTypes(LtNone , LtHeavy , LtHeavy , LtHeavy ), /* U+2533 ┳ */
makePackedLineTypes(LtLight , LtLight , LtNone , LtLight ), /* U+2534 ┴ */
makePackedLineTypes(LtLight , LtLight , LtNone , LtHeavy ), /* U+2535 ┵ */
makePackedLineTypes(LtLight , LtHeavy , LtNone , LtLight ), /* U+2536 ┶ */
makePackedLineTypes(LtLight , LtHeavy , LtNone , LtHeavy ), /* U+2537 ┷ */
makePackedLineTypes(LtHeavy , LtLight , LtNone , LtLight ), /* U+2538 ┸ */
makePackedLineTypes(LtHeavy , LtLight , LtNone , LtHeavy ), /* U+2539 ┹ */
makePackedLineTypes(LtHeavy , LtHeavy , LtNone , LtLight ), /* U+253A ┺ */
makePackedLineTypes(LtHeavy , LtHeavy , LtNone , LtHeavy ), /* U+253B ┻ */
makePackedLineTypes(LtLight , LtLight , LtLight , LtLight ), /* U+253C ┼ */
makePackedLineTypes(LtLight , LtLight , LtLight , LtHeavy ), /* U+253D ┽ */
makePackedLineTypes(LtLight , LtHeavy , LtLight , LtLight ), /* U+253E ┾ */
makePackedLineTypes(LtLight , LtHeavy , LtLight , LtHeavy ), /* U+253F ┿ */
makePackedLineTypes(LtHeavy , LtLight , LtLight , LtLight ), /* U+2540 ╀ */
makePackedLineTypes(LtLight , LtLight , LtHeavy , LtLight ), /* U+2541 ╁ */
makePackedLineTypes(LtHeavy , LtLight , LtHeavy , LtLight ), /* U+2542 ╂ */
makePackedLineTypes(LtHeavy , LtLight , LtLight , LtHeavy ), /* U+2543 ╃ */
makePackedLineTypes(LtHeavy , LtHeavy , LtLight , LtLight ), /* U+2544 ╄ */
makePackedLineTypes(LtLight , LtLight , LtHeavy , LtHeavy ), /* U+2545 ╅ */
makePackedLineTypes(LtLight , LtHeavy , LtHeavy , LtLight ), /* U+2546 ╆ */
makePackedLineTypes(LtHeavy , LtHeavy , LtLight , LtHeavy ), /* U+2547 ╇ */
makePackedLineTypes(LtLight , LtHeavy , LtHeavy , LtHeavy ), /* U+2548 ╈ */
makePackedLineTypes(LtHeavy , LtLight , LtHeavy , LtHeavy ), /* U+2549 ╉ */
makePackedLineTypes(LtHeavy , LtHeavy , LtHeavy , LtLight ), /* U+254A ╊ */
makePackedLineTypes(LtHeavy , LtHeavy , LtHeavy , LtHeavy ), /* U+254B ╋ */
0, 0, 0, 0, /* U+254C - U+254F */
makePackedLineTypes(LtNone , LtDouble, LtNone , LtDouble), /* U+2550 ═ */
makePackedLineTypes(LtDouble, LtNone , LtDouble, LtNone ), /* U+2551 ║ */
makePackedLineTypes(LtNone , LtDouble, LtLight , LtNone ), /* U+2552 ╒ */
makePackedLineTypes(LtNone , LtLight , LtDouble, LtNone ), /* U+2553 ╓ */
makePackedLineTypes(LtNone , LtDouble, LtDouble, LtNone ), /* U+2554 ╔ */
makePackedLineTypes(LtNone , LtNone , LtLight , LtDouble), /* U+2555 ╕ */
makePackedLineTypes(LtNone , LtNone , LtDouble, LtLight ), /* U+2556 ╖ */
makePackedLineTypes(LtNone , LtNone , LtDouble, LtDouble), /* U+2557 ╗ */
makePackedLineTypes(LtLight , LtDouble, LtNone , LtNone ), /* U+2558 ╘ */
makePackedLineTypes(LtDouble, LtLight , LtNone , LtNone ), /* U+2559 ╙ */
makePackedLineTypes(LtDouble, LtDouble, LtNone , LtNone ), /* U+255A ╚ */
makePackedLineTypes(LtLight , LtNone , LtNone , LtDouble), /* U+255B ╛ */
makePackedLineTypes(LtDouble, LtNone , LtNone , LtLight ), /* U+255C ╜ */
makePackedLineTypes(LtDouble, LtNone , LtNone , LtDouble), /* U+255D ╝ */
makePackedLineTypes(LtLight , LtDouble, LtLight , LtNone ), /* U+255E ╞ */
makePackedLineTypes(LtDouble, LtLight , LtDouble, LtNone ), /* U+255F ╟ */
makePackedLineTypes(LtDouble, LtDouble, LtDouble, LtNone ), /* U+2560 ╠ */
makePackedLineTypes(LtLight , LtNone , LtLight , LtDouble), /* U+2561 ╡ */
makePackedLineTypes(LtDouble, LtNone , LtDouble, LtLight ), /* U+2562 ╢ */
makePackedLineTypes(LtDouble, LtNone , LtDouble, LtDouble), /* U+2563 ╣ */
makePackedLineTypes(LtNone , LtDouble, LtLight , LtDouble), /* U+2564 ╤ */
makePackedLineTypes(LtNone , LtLight , LtDouble, LtLight ), /* U+2565 ╥ */
makePackedLineTypes(LtNone , LtDouble, LtDouble, LtDouble), /* U+2566 ╦ */
makePackedLineTypes(LtLight , LtDouble, LtNone , LtDouble), /* U+2567 ╧ */
makePackedLineTypes(LtDouble, LtLight , LtNone , LtLight ), /* U+2568 ╨ */
makePackedLineTypes(LtDouble, LtDouble, LtNone , LtDouble), /* U+2569 ╩ */
makePackedLineTypes(LtLight , LtDouble, LtLight , LtDouble), /* U+256A ╪ */
makePackedLineTypes(LtDouble, LtLight , LtDouble, LtLight ), /* U+256B ╫ */
makePackedLineTypes(LtDouble, LtDouble, LtDouble, LtDouble), /* U+256C ╬ */
0, 0, 0, 0, 0, 0, 0, /* U+256D - U+2573 */
makePackedLineTypes(LtNone , LtNone , LtNone , LtLight ), /* U+2574 ╴ */
makePackedLineTypes(LtLight , LtNone , LtNone , LtNone ), /* U+2575 ╵ */
makePackedLineTypes(LtNone , LtLight , LtNone , LtNone ), /* U+2576 ╶ */
makePackedLineTypes(LtNone , LtNone , LtLight , LtNone ), /* U+2577 ╷ */
makePackedLineTypes(LtNone , LtNone , LtNone , LtHeavy ), /* U+2578 ╸ */
makePackedLineTypes(LtHeavy , LtNone , LtNone , LtNone ), /* U+2579 ╹ */
makePackedLineTypes(LtNone , LtHeavy , LtNone , LtNone ), /* U+257A ╺ */
makePackedLineTypes(LtNone , LtNone , LtHeavy , LtNone ), /* U+257B ╻ */
makePackedLineTypes(LtNone , LtHeavy , LtNone , LtLight ), /* U+257C ╼ */
makePackedLineTypes(LtLight , LtNone , LtHeavy , LtNone ), /* U+257D ╽ */
makePackedLineTypes(LtNone , LtLight , LtNone , LtHeavy ), /* U+257E ╾ */
makePackedLineTypes(LtHeavy , LtNone , LtLight , LtNone ), /* U+257F ╿ */
};
/* clang-format on */
// Bitwise rotate left
template<typename T>
inline static T rotateBitsLeft(T value, quint8 amount)
{
static_assert(std::is_unsigned<T>(), "T must be unsigned type");
Q_ASSERT(amount < sizeof(value) * 8);
return value << amount | value >> (sizeof(value) * 8 - amount);
}
inline static const QPen pen(const QPainter &paint, uint lineWidth)
{
return QPen(paint.pen().brush(), lineWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
}
static inline uint lineWidth(uint fontWidth, bool heavy, bool bold)
{
static const qreal LightWidthToFontWidthRatio = 1.0 / 6.5;
static const qreal HeavyHalfExtraToLightRatio = 1.0 / 3.0;
static const qreal BoldCoefficient = 1.5;
// ▄▄▄▄▄▄▄ } heavyHalfExtraWidth ⎫
// ██████████████ } lightWidth ⎬ heavyWidth
// ▀▀▀▀▀▀▀ ⎭
// light heavy
const qreal baseWidth = fontWidth * LightWidthToFontWidthRatio;
const qreal boldCoeff = bold ? BoldCoefficient : 1.0;
// Unless font size is too small, make bold lines at least 1px wider than regular lines
const qreal minWidth = bold && fontWidth >= 7 ? baseWidth + 1.0 : 1.0;
const uint lightWidth = qRound(qMax(baseWidth * boldCoeff, minWidth));
const uint heavyHalfExtraWidth = qRound(qMax(lightWidth * HeavyHalfExtraToLightRatio, 1.0));
return heavy ? lightWidth + 2 * heavyHalfExtraWidth : lightWidth;
}
// Draws characters composed of straight solid lines
static bool drawBasicLineCharacter(QPainter &paint, int x, int y, int w, int h, uchar code, bool bold)
{
quint8 packedLineTypes = code >= sizeof(PackedLineTypesLut) ? 0 : PackedLineTypesLut[code];
if (packedLineTypes == 0) {
return false;
}
const uint lightLineWidth = lineWidth(w, false, bold);
const uint heavyLineWidth = lineWidth(w, true, bold);
// Distance from double line's parallel axis to each line's parallel axis
const uint doubleLinesDistance = lightLineWidth;
const QPen lightPen = pen(paint, lightLineWidth);
const QPen heavyPen = pen(paint, heavyLineWidth);
static constexpr const unsigned LinesNum = 4;
// Pixel aligned center point
const QPointF center = {
x + int(w / 2) + 0.5 * (lightLineWidth % 2),
y + int(h / 2) + 0.5 * (lightLineWidth % 2),
};
// Lines starting points, on the cell edges
const QPointF origin[] = {
QPointF(center.x(), y),
QPointF(x + w, center.y()),
QPointF(center.x(), y + h),
QPointF(x, center.y()),
};
// Unit vectors with directions from center to the line's origin point
static const QPointF dir[] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
const auto removeLineType = [&packedLineTypes](quint8 lineId) -> void {
lineId = LinesNum - 1 - lineId % LinesNum;
packedLineTypes &= ~(3 << (2 * lineId));
};
const auto getLineType = [&packedLineTypes](quint8 lineId) -> LineType {
lineId = LinesNum - 1 - lineId % LinesNum;
return LineType(packedLineTypes >> 2 * lineId & 3);
};
QPainterPath lightPath; // PainterPath for light lines (Painter Path Light)
QPainterPath heavyPath; // PainterPath for heavy lines (Painter Path Heavy)
// Returns ppl or pph depending on line type
const auto pathForLine = [&](quint8 lineId) -> QPainterPath & {
Q_ASSERT(getLineType(lineId) != LtNone);
return getLineType(lineId) == LtHeavy ? heavyPath : lightPath;
};
// Process all single up-down/left-right lines for every character that has them. Doing it here
// reduces amount of combinations below.
// Fully draws: ╋ ╂ ┃ ┿ ┼ │ ━ ─
for (unsigned int topIndex = 0; topIndex < LinesNum / 2; topIndex++) {
unsigned iB = (topIndex + 2) % LinesNum;
const bool isSingleLine = (getLineType(topIndex) == LtLight || getLineType(topIndex) == LtHeavy);
if (isSingleLine && getLineType(topIndex) == getLineType(iB)) {
pathForLine(topIndex).moveTo(origin[topIndex]);
pathForLine(topIndex).lineTo(origin[iB]);
removeLineType(topIndex);
removeLineType(iB);
}
}
// Find base rotation of a character and map rotated line indices to the original rotation's
// indices. The base rotation is defined as the one with largest packedLineTypes value. This way
// we can use the same code for drawing 4 possible character rotations (see switch() below)
uint topIndex = 0; // index of an original top line in a base rotation
quint8 basePackedLineTypes = packedLineTypes;
for (uint i = 0; i < LinesNum; i++) {
const quint8 rotatedPackedLineTypes = rotateBitsLeft(packedLineTypes, i * 2);
if (rotatedPackedLineTypes > basePackedLineTypes) {
topIndex = i;
basePackedLineTypes = rotatedPackedLineTypes;
}
}
uint rightIndex = (topIndex + 1) % LinesNum;
uint bottomIndex = (topIndex + 2) % LinesNum;
uint leftIndex = (topIndex + 3) % LinesNum;
// Common paths
const auto drawDoubleUpRightShorterLine = [&](quint8 top, quint8 right) { // ╚
lightPath.moveTo(origin[top] + dir[right] * doubleLinesDistance);
lightPath.lineTo(center + (dir[right] + dir[top]) * doubleLinesDistance);
lightPath.lineTo(origin[right] + dir[top] * doubleLinesDistance);
};
const auto drawUpRight = [&](quint8 top, quint8 right) { // └┗
pathForLine(top).moveTo(origin[top]);
pathForLine(top).lineTo(center);
pathForLine(top).lineTo(origin[right]);
};
switch (basePackedLineTypes) {
case makePackedLineTypes(LtHeavy, LtNone, LtLight, LtNone): // ╿ ; ╼ ╽ ╾ ╊ ╇ ╉ ╈ ╀ ┾ ╁ ┽
lightPath.moveTo(origin[bottomIndex]);
lightPath.lineTo(center + dir[topIndex] * lightLineWidth / 2.0);
Q_FALLTHROUGH();
case makePackedLineTypes(LtHeavy, LtNone, LtNone, LtNone): // ╹ ; ╺ ╻ ╸ ┻ ┣ ┳ ┫ ┸ ┝ ┰ ┥
case makePackedLineTypes(LtLight, LtNone, LtNone, LtNone): // ╵ ; ╶ ╷ ╴ ┷ ┠ ┯ ┨ ┴ ├ ┬ ┤
pathForLine(topIndex).moveTo(origin[topIndex]);
pathForLine(topIndex).lineTo(center);
break;
case makePackedLineTypes(LtHeavy, LtHeavy, LtLight, LtLight): // ╄ ; ╃ ╆ ╅
drawUpRight(bottomIndex, leftIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtHeavy, LtHeavy, LtNone, LtNone): // ┗ ; ┛ ┏ ┓
case makePackedLineTypes(LtLight, LtLight, LtNone, LtNone): // └ ; ┘ ┌ ┐
drawUpRight(topIndex, rightIndex);
break;
case makePackedLineTypes(LtHeavy, LtLight, LtNone, LtNone): // ┖ ; ┙ ┍ ┒
qSwap(leftIndex, rightIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtHeavy, LtNone, LtNone, LtLight): // ┚ ; ┕ ┎ ┑
lightPath.moveTo(origin[leftIndex]);
lightPath.lineTo(center);
heavyPath.moveTo(origin[topIndex]);
heavyPath.lineTo(center + dir[bottomIndex] * lightLineWidth / 2.0);
break;
case makePackedLineTypes(LtLight, LtDouble, LtNone, LtNone): // ╘ ; ╜ ╓ ╕
qSwap(leftIndex, rightIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtLight, LtNone, LtNone, LtDouble): // ╛ ; ╙ ╒ ╖
lightPath.moveTo(origin[topIndex]);
lightPath.lineTo(center + dir[bottomIndex] * doubleLinesDistance);
lightPath.lineTo(origin[leftIndex] + dir[bottomIndex] * doubleLinesDistance);
lightPath.moveTo(origin[leftIndex] - dir[bottomIndex] * doubleLinesDistance);
lightPath.lineTo(center - dir[bottomIndex] * doubleLinesDistance);
break;
case makePackedLineTypes(LtHeavy, LtHeavy, LtLight, LtNone): // ┡ ; ┹ ┪ ┲
qSwap(leftIndex, bottomIndex);
qSwap(rightIndex, topIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtHeavy, LtHeavy, LtNone, LtLight): // ┺ ; ┩ ┢ ┱
drawUpRight(topIndex, rightIndex);
lightPath.moveTo(origin[leftIndex]);
lightPath.lineTo(center);
break;
case makePackedLineTypes(LtHeavy, LtLight, LtLight, LtNone): // ┞ ; ┵ ┧ ┮
qSwap(leftIndex, rightIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtHeavy, LtNone, LtLight, LtLight): // ┦ ; ┶ ┟ ┭
heavyPath.moveTo(origin[topIndex]);
heavyPath.lineTo(center + dir[bottomIndex] * lightLineWidth / 2.0);
drawUpRight(bottomIndex, leftIndex);
break;
case makePackedLineTypes(LtLight, LtDouble, LtNone, LtDouble): // ╧ ; ╟ ╢ ╤
lightPath.moveTo(origin[topIndex]);
lightPath.lineTo(center - dir[bottomIndex] * doubleLinesDistance);
qSwap(leftIndex, bottomIndex);
qSwap(rightIndex, topIndex);
Q_FALLTHROUGH();
case makePackedLineTypes(LtDouble, LtNone, LtDouble, LtNone): // ║ ; ╫ ═ ╪
lightPath.moveTo(origin[topIndex] + dir[leftIndex] * doubleLinesDistance);
lightPath.lineTo(origin[bottomIndex] + dir[leftIndex] * doubleLinesDistance);
lightPath.moveTo(origin[topIndex] + dir[rightIndex] * doubleLinesDistance);
lightPath.lineTo(origin[bottomIndex] + dir[rightIndex] * doubleLinesDistance);
break;
case makePackedLineTypes(LtDouble, LtNone, LtNone, LtNone): // ╨ ; ╞ ╥ ╡
lightPath.moveTo(origin[topIndex] + dir[leftIndex] * doubleLinesDistance);
lightPath.lineTo(center + dir[leftIndex] * doubleLinesDistance);
lightPath.moveTo(origin[topIndex] + dir[rightIndex] * doubleLinesDistance);
lightPath.lineTo(center + dir[rightIndex] * doubleLinesDistance);
break;
case makePackedLineTypes(LtDouble, LtDouble, LtDouble, LtDouble): // ╬
drawDoubleUpRightShorterLine(topIndex, rightIndex);
drawDoubleUpRightShorterLine(bottomIndex, rightIndex);
drawDoubleUpRightShorterLine(topIndex, leftIndex);
drawDoubleUpRightShorterLine(bottomIndex, leftIndex);
break;
case makePackedLineTypes(LtDouble, LtDouble, LtDouble, LtNone): // ╠ ; ╩ ╣ ╦
lightPath.moveTo(origin[topIndex] + dir[leftIndex] * doubleLinesDistance);
lightPath.lineTo(origin[bottomIndex] + dir[leftIndex] * doubleLinesDistance);
drawDoubleUpRightShorterLine(topIndex, rightIndex);
drawDoubleUpRightShorterLine(bottomIndex, rightIndex);
break;
case makePackedLineTypes(LtDouble, LtDouble, LtNone, LtNone): // ╚ ; ╝ ╔ ╗
lightPath.moveTo(origin[topIndex] + dir[leftIndex] * doubleLinesDistance);
lightPath.lineTo(center + (dir[leftIndex] + dir[bottomIndex]) * doubleLinesDistance);
lightPath.lineTo(origin[rightIndex] + dir[bottomIndex] * doubleLinesDistance);
drawDoubleUpRightShorterLine(topIndex, rightIndex);
break;
}
// Draw paths
if (!lightPath.isEmpty()) {
paint.strokePath(lightPath, lightPen);
}
if (!heavyPath.isEmpty()) {
paint.strokePath(heavyPath, heavyPen);
}
return true;
}
static inline bool drawDashedLineCharacter(QPainter &paint, int x, int y, int w, int h, uchar code, bool bold)
{
if (!((0x04 <= code && code <= 0x0B) || (0x4C <= code && code <= 0x4F))) {
return false;
}
const uint lightLineWidth = lineWidth(w, false, bold);
const uint heavyLineWidth = lineWidth(w, true, bold);
const auto lightPen = pen(paint, lightLineWidth);
const auto heavyPen = pen(paint, heavyLineWidth);
// Pixel aligned center point
const QPointF center = {
int(x + w / 2.0) + 0.5 * (lightLineWidth % 2),
int(y + h / 2.0) + 0.5 * (lightLineWidth % 2),
};
const qreal halfGapH = qMax(w / 20.0, 0.5);
const qreal halfGapV = qMax(h / 26.0, 0.5);
// For some reason vertical double dash has bigger gap
const qreal halfGapDDV = qMax(h / 14.0, 0.5);
static const int LinesNumMax = 4;
enum Orientation { Horizontal, Vertical };
struct {
int linesNum;
Orientation orientation;
QPen pen;
qreal halfGap;
} lineProps;
/* clang-format off */
switch (code) {
case 0x4C: lineProps = {2, Horizontal, lightPen, halfGapH }; break; // ╌
case 0x4D: lineProps = {2, Horizontal, heavyPen, halfGapH }; break; // ╍
case 0x4E: lineProps = {2, Vertical , lightPen, halfGapDDV}; break; // ╎
case 0x4F: lineProps = {2, Vertical , heavyPen, halfGapDDV}; break; // ╏
case 0x04: lineProps = {3, Horizontal, lightPen, halfGapH }; break; // ┄
case 0x05: lineProps = {3, Horizontal, heavyPen, halfGapH }; break; // ┅
case 0x06: lineProps = {3, Vertical , lightPen, halfGapV }; break; // ┆
case 0x07: lineProps = {3, Vertical , heavyPen, halfGapV }; break; // ┇
case 0x08: lineProps = {4, Horizontal, lightPen, halfGapH }; break; // ┈
case 0x09: lineProps = {4, Horizontal, heavyPen, halfGapH }; break; // ┉
case 0x0A: lineProps = {4, Vertical , lightPen, halfGapV }; break; // ┊
case 0x0B: lineProps = {4, Vertical , heavyPen, halfGapV }; break; // ┋
}
/* clang-format on */
Q_ASSERT(lineProps.linesNum <= LinesNumMax);
const int size = (lineProps.orientation == Horizontal ? w : h);
const int pos = (lineProps.orientation == Horizontal ? x : y);
QLineF lines[LinesNumMax];
for (int i = 0; i < lineProps.linesNum; i++) {
const qreal start = pos + qreal(size * (i)) / lineProps.linesNum;
const qreal end = pos + qreal(size * (i + 1)) / lineProps.linesNum;
if (lineProps.orientation == Horizontal) {
lines[i] = QLineF{start + lineProps.halfGap, center.y(), end - lineProps.halfGap, center.y()};
} else {
lines[i] = QLineF{center.x(), start + lineProps.halfGap, center.x(), end - lineProps.halfGap};
}
}
const auto origPen = paint.pen();
paint.setPen(lineProps.pen);
paint.drawLines(lines, lineProps.linesNum);
paint.setPen(origPen);
return true;
}
static inline bool drawRoundedCornerLineCharacter(QPainter &paint, int x, int y, int w, int h, uchar code, bool bold)
{
if (!(0x6D <= code && code <= 0x70)) {
return false;
}
const uint lightLineWidth = lineWidth(w, false, bold);
const auto lightPen = pen(paint, lightLineWidth);
// Pixel aligned center point
const QPointF center = {
int(x + w / 2.0) + 0.5 * (lightLineWidth % 2),
int(y + h / 2.0) + 0.5 * (lightLineWidth % 2),
};
const int r = w * 3 / 8;
const int d = 2 * r;
QPainterPath path;
// lineTo() between moveTo and arcTo is redundant - arcTo() draws line
// to the arc's starting point
switch (code) {
case 0x6D: // BOX DRAWINGS LIGHT ARC DOWN AND RIGHT
path.moveTo(center.x(), y + h);
path.arcTo(center.x(), center.y(), d, d, 180, -90);
path.lineTo(x + w, center.y());
break;
case 0x6E: // BOX DRAWINGS LIGHT ARC DOWN AND LEFT
path.moveTo(center.x(), y + h);
path.arcTo(center.x() - d, center.y(), d, d, 0, 90);
path.lineTo(x, center.y());
break;
case 0x6F: // BOX DRAWINGS LIGHT ARC UP AND LEFT
path.moveTo(center.x(), y);
path.arcTo(center.x() - d, center.y() - d, d, d, 0, -90);
path.lineTo(x, center.y());
break;
case 0x70: // BOX DRAWINGS LIGHT ARC UP AND RIGHT
path.moveTo(center.x(), y);
path.arcTo(center.x(), center.y() - d, d, d, 180, 90);
path.lineTo(x + w, center.y());
break;
}
paint.strokePath(path, lightPen);
return true;
}
static inline bool drawDiagonalLineCharacter(QPainter &paint, int x, int y, int w, int h, uchar code, bool bold)
{
if (!(0x71 <= code && code <= 0x73)) {
return false;
}
const uint lightLineWidth = lineWidth(w, false, bold);
const auto lightPen = pen(paint, lightLineWidth);
const QLineF lines[] = {
QLineF(x + w, y, x, y + h), // '/'
QLineF(x, y, x + w, y + h), // '\'
};
const auto origPen = paint.pen();
paint.setPen(lightPen);
switch (code) {
case 0x71: // BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT
paint.drawLine(lines[0]);
break;
case 0x72: // BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT
paint.drawLine(lines[1]);
break;
case 0x73: // BOX DRAWINGS LIGHT DIAGONAL CROSS
paint.drawLines(lines, 2);
break;
}
paint.setPen(origPen);
return true;
}
static inline bool drawBlockCharacter(QPainter &paint, int x, int y, int w, int h, uchar code, bool bold)
{
Q_UNUSED(bold)
const QColor color = paint.pen().color();
// Center point
const QPointF center = {
x + w / 2.0,
y + h / 2.0,
};
// Default rect fills entire cell
QRectF rect(x, y, w, h);
// LOWER ONE EIGHTH BLOCK to LEFT ONE EIGHTH BLOCK
if (code >= 0x81 && code <= 0x8f) {
if (code < 0x88) { // Horizontal
const qreal height = h * (0x88 - code) / 8.0;
rect.setY(y + height);
rect.setHeight(h - height);
} else if (code > 0x88) { // Vertical
const qreal width = w * (0x90 - code) / 8.0;
rect.setWidth(width);
}
paint.fillRect(rect, color);
return true;
}
// Combinations of quarter squares
// LEFT ONE EIGHTH BLOCK to QUADRANT UPPER RIGHT AND LOWER LEFT AND LOWER RIGHT
if (code >= 0x96 && code <= 0x9F) {
const QRectF upperLeft(x, y, w / 2.0, h / 2.0);
const QRectF upperRight(center.x(), y, w / 2.0, h / 2.0);
const QRectF lowerLeft(x, center.y(), w / 2.0, h / 2.0);
const QRectF lowerRight(center.x(), center.y(), w / 2.0, h / 2.0);
QPainterPath path;
switch (code) {
case 0x96: // ▖
path.addRect(lowerLeft);
break;
case 0x97: // ▗
path.addRect(lowerRight);
break;
case 0x98: // ▘
path.addRect(upperLeft);
break;
case 0x99: // ▙
path.addRect(upperLeft);
path.addRect(lowerLeft);
path.addRect(lowerRight);
break;
case 0x9a: // ▚
path.addRect(upperLeft);
path.addRect(lowerRight);
break;
case 0x9b: // ▛
path.addRect(upperLeft);
path.addRect(upperRight);
path.addRect(lowerLeft);
break;
case 0x9c: // ▜
path.addRect(upperLeft);
path.addRect(upperRight);
path.addRect(lowerRight);
break;
case 0x9d: // ▝
path.addRect(upperRight);
break;
case 0x9e: // ▞
path.addRect(upperRight);
path.addRect(lowerLeft);
break;
case 0x9f: // ▟
path.addRect(upperRight);
path.addRect(lowerLeft);
path.addRect(lowerRight);
break;
}
paint.fillPath(path, color);
return true;
}
QBrush lightShade;
QBrush mediumShade;
QBrush darkShade;
if (paint.testRenderHint(QPainter::Antialiasing)) {
lightShade = QColor(color.red(), color.green(), color.blue(), 64);
mediumShade = QColor(color.red(), color.green(), color.blue(), 128);
darkShade = QColor(color.red(), color.green(), color.blue(), 192);
} else {
lightShade = QBrush(color, Qt::Dense6Pattern);
mediumShade = QBrush(color, Qt::Dense4Pattern);
darkShade = QBrush(color, Qt::Dense2Pattern);
}
// And the random stuff
switch (code) {
case 0x80: // Top half block
rect.setHeight(h / 2.0);
paint.fillRect(rect, color);
return true;
case 0x90: // Right half block
rect.setX(center.x());
paint.fillRect(rect, color);
return true;
case 0x94: // Top one eighth block
rect.setHeight(h / 8.0);
paint.fillRect(rect, color);
return true;
case 0x95: // Right one eighth block
rect.setX(x + 7 * w / 8.0);
paint.fillRect(rect, color);
return true;
case 0x91: // Light shade
paint.fillRect(rect, lightShade);
return true;
case 0x92: // Medium shade
paint.fillRect(rect, mediumShade);
return true;
case 0x93: // Dark shade
paint.fillRect(rect, darkShade);
return true;
default:
return false;
}
}
static inline bool drawLegacyCharacter(QPainter &paint, int x, int y, int w, int h, uint code, bool bold)
{
Q_UNUSED(bold)
QPainterPath path;
const QColor color = paint.pen().color();
if (code >= 0x13c && code <= 0x16f) {
// Smooth mosaic terminal graphic characters
QVector<QPointF> points(13);
for (int i = 0; i < 12; i++) {
qreal px = x + (i >> 2) * w / 2.0;
qreal py = y + (3 - (i & 3)) * h / 3.0;
points[i] = QPointF(px, py);
}
points[12] = QPointF(x + w / 2.0, y + h / 2.0);
QList<std::string> chars = {
"014", "018", "024", "028", "034", "027;8", "02;8", "017;8", // 0x1fb3c - 0x1fb43
"01;8", "07;8", "01:8", "498", "098", "4:8", "0:8", "4;8", // 44 - 4b
"037:8", "03:8", "03798", "0398", "0378", "0298", "13;84", "13;8", // 4c - 53
"23;84", "23;8", "3;84", "237", "23;", "137", "13;", "037", // 54 - 5b
"13;:", "03;94", "03;9", "03;:4", // 5c-5f
"03;:", "03;4", "7;:", "3;:", "7;9", "3;9", "7;8", "23;9", // 60-67
"0<3;8", "03<;8", "03;<8", "03;8<", "03<", "3;<", "<;8", "0<8" // 68-6f
};
std::string str = chars[code - 0x13c];
QVector<QPointF> vec;
for (uint i = 0; i < str.length(); i++) {
vec.append(points[str[i] - '0']);
}
path.addPolygon(QPolygonF(vec));
paint.fillPath(path, color);
return true;
}
if (code >= 0x170 && code <= 0x175) {
// Vertical eighths
path.addRect(QRectF(x + (code - 0x16f) * w / 8.0, y, w / 8.0, h));
paint.fillPath(path, color);
return true;
}
if (code >= 0x176 && code <= 0x17b) {
// Horizontal eighths
path.addRect(QRectF(x, y + (code - 0x175) * h / 8.0, w, h / 8.0));
paint.fillPath(path, color);
return true;
}
if (code >= 0x17c && code <= 0x17f) {
// Corner eighths
qreal y1 = y;
qreal y2 = y;
if (code == 0x17c || code == 0x17f) {
y1 += 7 * h / 8.0;
} else {
y2 += h / 8.0;
}
qreal x1 = x;
if (code > 0x17d) {
x1 += 7 * w / 8.0;
}
path.addRect(QRectF(x, y1, w, h / 8.0));
path.addRect(QRectF(x1, y2, w / 8.0, 7 * h / 8.0));
paint.fillPath(path, color);
return true;
}
if (code == 0x180) {
// Horizontal eighths 18
path.addRect(QRectF(x, y, w, h / 8.0));
path.addRect(QRectF(x, y + 7 * h / 8.0, w, h / 8.0));
paint.fillPath(path, color);
return true;
}
if (code == 0x181) {
// Horizontal eighths 1358
path.addRect(QRectF(x, y, w, h / 8.0));
path.addRect(QRectF(x, y + 2 * h / 8.0, w, h / 8.0));
path.addRect(QRectF(x, y + 4 * h / 8.0, w, h / 8.0));
path.addRect(QRectF(x, y + 7 * h / 8.0, w, h / 8.0));
paint.fillPath(path, color);
return true;
}
if (code >= 0x182 && code <= 0x186) {
// Horizontal upper 2,3,5,6,7 eighths
int hs[5] = {2, 3, 5, 6, 7};
path.addRect(QRectF(x, y, w, hs[code - 0x182] * h / 8.0));
paint.fillPath(path, color);
return true;
}
if (code >= 0x187 && code <= 0x18b) {
// Vertical right 2,3,5,6,7 eighths
int hs[5] = {2, 3, 5, 6, 7};
path.addRect(QRectF(x + (8 - hs[code - 0x187]) * w / 8.0, y, hs[code - 0x187] * w / 8.0, h));
paint.fillPath(path, color);
return true;
}
if (code >= 0x128) {
code += 1;
}
if (code >= 0x114) {
code += 1;
}
code += 1;
// Center point
const QPointF center = {
x + w / 2.0,
y + h / 2.0,
};
// Default rect fills entire cell
QRectF rect(x, y, w, h);
if (code <= 0x13f) {
const QRectF upperLeft(x, y, w / 2.0, h / 3.0);
const QRectF upperRight(center.x(), y, w / 2.0, h / 3.0);
const QRectF midLeft(x, y + h / 3.0, w / 2.0, h / 3.0);
const QRectF midRight(center.x(), y + h / 3.0, w / 2.0, h / 3.0);
const QRectF lowerLeft(x, y + 2.0 * h / 3.0, w / 2.0, h / 3.0);
const QRectF lowerRight(center.x(), y + 2.0 * h / 3.0, w / 2.0, h / 3.0);
if (code & 0x01) {
path.addRect(upperLeft);
}
if (code & 0x02) {
path.addRect(upperRight);
}
if (code & 0x04) {
path.addRect(midLeft);
}
if (code & 0x08) {
path.addRect(midRight);
}
if (code & 0x10) {
path.addRect(lowerLeft);
}
if (code & 0x20) {
path.addRect(lowerRight);
}
paint.fillPath(path, color);
return true;
}
return false;
}
static inline bool drawBrailleCharacter(QPainter &paint, int x, int y, int w, int h, uint code, bool bold)
{
Q_UNUSED(bold)
QPainterPath path;
const QColor color = paint.pen().color();
const QRectF rects[8] = {QRectF(x, y, w / 2.0, h / 4.0),
QRectF(x, y + h / 4.0, w / 2.0, h / 4.0),
QRectF(x, y + h / 2.0, w / 2.0, h / 4.0),
QRectF(x + w / 2.0, y, w / 2.0, h / 4.0),
QRectF(x + w / 2.0, y + h / 4.0, w / 2.0, h / 4.0),
QRectF(x + w / 2.0, y + h / 2.0, w / 2.0, h / 4.0),
QRectF(x, y + 3.0 * h / 4.0, w / 2.0, h / 4.0),
QRectF(x + w / 2.0, y + 3.0 * h / 4.0, w / 2.0, h / 4.0)};
for (int i = 0; i < 8; i++) {
if (code & (0x01 << i)) {
path.addRect(rects[i]);
}
}
paint.fillPath(path, color);
return true;
}
void draw(QPainter &paint, const QRect &cellRect, const uint &chr, bool bold)
{
static const ushort FirstBoxDrawingCharacterCodePoint = 0x2500;
static const ushort FirstBrailleCodePoint = 0x2800;
static const uint FirstLegacyCharacterCodePoint = 0x1fb00;
uint code;
int x = cellRect.x();
int y = cellRect.y();
int w = cellRect.width();
int h = cellRect.height();
if (chr >= FirstLegacyCharacterCodePoint) {
drawLegacyCharacter(paint, x, y, w, h, chr - FirstLegacyCharacterCodePoint + 0x100, bold);
return;
} else if (chr >= FirstBrailleCodePoint) {
drawBrailleCharacter(paint, x, y, w, h, chr - FirstBrailleCodePoint, bold);
return;
} else {
code = chr - FirstBoxDrawingCharacterCodePoint;
}
// Each function below returns true when it has drawn the character, false otherwise.
drawBasicLineCharacter(paint, x, y, w, h, code, bold) || drawDashedLineCharacter(paint, x, y, w, h, code, bold)
|| drawRoundedCornerLineCharacter(paint, x, y, w, h, code, bold) || drawDiagonalLineCharacter(paint, x, y, w, h, code, bold)
|| drawBlockCharacter(paint, x, y, w, h, code, bold);
}
} // namespace LineBlockCharacters
} // namespace Konsole
@@ -0,0 +1,58 @@
/*
SPDX-FileCopyrightText: 2019 Mariusz Glebocki <mglb@arccos-1.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef LINEBLOCKCHARACTERS_H
#define LINEBLOCKCHARACTERS_H
// Qt
#include <QPainter>
namespace Konsole
{
/**
* Helper functions for drawing characters from "Box Drawing" and "Block Elements" Unicode blocks.
*/
namespace LineBlockCharacters
{
/**
* Returns true if the character can be drawn by draw() function.
*
* @param ucs4cp Character to test's UCS4 code point
*/
inline static bool canDraw(uint ucs4cp)
{
return (0x2500 <= ucs4cp && ucs4cp <= 0x259F) || (0x2800 <= ucs4cp && ucs4cp <= 0x28FF);
}
inline static bool isBraille(uint ucs4cp)
{
return (0x2800 <= ucs4cp && ucs4cp <= 0x28ff);
}
/**
* Returns true if the character is a Legacy Computing Symbol and can be drawn by draw() function.
*
* @param ucs4cp Character to test's UCS4 code point
*/
inline static bool isLegacyComputingSymbol(uint ucs4cp)
{
return (0x1fb00 <= ucs4cp && ucs4cp <= 0x1fb8b);
}
/**
* Draws character.
*
* @param paint QPainter to draw on
* @param cellRect Rectangle to draw in
* @param chr Character to be drawn
* @param bold Whether the character should be boldface
*/
void draw(QPainter &paint, const QRect &cellRect, const uint &chr, bool bold);
} // namespace LineBlockCharacters
} // namespace Konsole
#endif // LINEBLOCKCHARACTERS_H