How to get the font script type from the font - harfbuzz

I am using harfbuzz to shape regional font and this is my current code.
HBShaper2(const string& fontFile, FreeTypeLib* lib, int fontSize);
FreeTypeLib lib;
HBShaper2 hindiShaper("C:\\Windows\\Fonts\\mangal.ttf", &lib, (int)fontSize);
typedef struct {
std::string data;
std::string language;
hb_script_t script;
hb_direction_t direction;
const char* c_data() { return data.c_str(); };
} HBText2;
HBText2 hbt5 = {
stdstrTxt.c_str(),
"hi",
HB_SCRIPT_DEVANAGARI, // How to get this value of the font
HB_DIRECTION_LTR
};
Currently i am manually setting the hb_script_t value in the code.
How can i get this value of the font ?

Related

Thingsboard Arduino Client Library issues

So I've been working on modifying the thingsboard arduino library to support device provisioning. I've duplicated/renamed/modified a handful of functions in the library to support pointing to the correct topics, and added the supporting code in my main application to make use of them. However, I am now getting a compiler error in a section of the library code I did not touch. I'm hoping someone can point out what I goofed up, as it appears like it should be a fairly simple fix if I can find it.
This is the bit of code giving me the error-
inline Telemetry(const char *key, T val)
:m_type(TYPE_INT), m_key(key), m_value() { m_value.integer = val; }
Here is the error -
In file included from src\main.cpp:12:0:
.pio\libdeps\win\ThingsBoard\src/ThingsBoard.h: In instantiation of 'Telemetry::Telemetry(const char*, T) [with T = String; <template-parameter-1-2> = ArduinoJson6172_91::enable_if<false, void>]':
src\main.cpp:514:33: required from here
.pio\libdeps\win\ThingsBoard\src/ThingsBoard.h:46:64: error: cannot convert 'String' to 'int' in assignment
:m_type(TYPE_INT), m_key(key), m_value() { m_value.integer = val; }
Here's the bulk of what I added to my main code to handle device registration.
RPC_Response processDeviceRegistration(const RPC_Data &data)
{
Serial.println("Received the device registration response");
if(data["status"]=="SUCCESS"){
String credential = data["credentialsValue"];
EEPROM.write(10, sizeof(credential));
EEPROM.writeString(20, (String)credential);
dr.DR_Unsubscribe();
return RPC_Response("registration response", true);
}
else{
Serial.println("Device registration failed");
return RPC_Response("registration response", false);
}
}
const size_t callbacks_size = 2;
RPC_Callback callbacks[callbacks_size] = {
{ "device_registration", processDeviceRegistration }
};
void reinitialize()
{
if (!dr.RPC_Subscribe(callbacks, 1)) {
Serial.println("Failed to subscribe for RPC");
return;
}
dr.loop();
String IMEI = modem.getIMEI();
const int data_items = 3;
Telemetry data[data_items] = {
Telemetry("deviceName", IMEI),
Telemetry("provisionDeviceKey", provision_device_key),
Telemetry("provisionDeviceSecret", provision_device_secret),
};
if (!dr.sendDR(data, data_items)){
Serial.println("Device registration send failed");
}
If it would be helpful, I can fork and upload the full library code and post a link to my github.
----- Edit -----
Upon further investigation, it appears the template for the Telemetry class is detecting the IMEI variable as an integer rather than a string and trying to convert it for some reason. I have no idea why, or how to fix it. Any suggestions would be greatly appreciated.
Well then, helps if I take a few minutes to actually understand the code I didn't write that is giving me grief. There was no handler in the Telemetry constructor for a character array or string type variable. Added that and now everything is working :)
Full Telemetry constructor below -
inline Telemetry()
:m_type(TYPE_NONE), m_key(NULL), m_value() { }
// Constructs telemetry record from integer value.
// EnableIf trick is required to overcome ambiguous float/integer conversion
template<
typename T,
typename = ARDUINOJSON_NAMESPACE::enable_if<ARDUINOJSON_NAMESPACE::is_integral<T>::value>
>
inline Telemetry(const char *key, T val)
:m_type(TYPE_INT), m_key(key), m_value() { m_value.integer = val; }
// Constructs telemetry record from boolean value.
inline Telemetry(const char *key, bool val)
:m_type(TYPE_BOOL), m_key(key), m_value() { m_value.boolean = val; }
// Constructs telemetry record from float value.
inline Telemetry(const char *key, float val)
:m_type(TYPE_REAL), m_key(key), m_value() { m_value.real = val; }
// Constructs telemetry record from string value.
inline Telemetry(const char *key, const char *val)
:m_type(TYPE_STR), m_key(key), m_value() { m_value.str = val; }
// Constructs telemetry record from string value.
inline Telemetry(const char *key, char *val)
:m_type(TYPE_STR), m_key(key), m_value() { m_value.str = val; }

Qt Example with HexStringValidator fails

I am using the following code from this example: https://doc.qt.io/qt-5/qtserialbus-can-example.html
enum {
MaxPayload = 8,
MaxPayloadFd = 64
};
HexStringValidator::HexStringValidator(QObject *parent) :
QValidator(parent),
m_maxLength(MaxPayload)
{
}
HexStringValidator::HexStringValidator(QObject *parent, uint maxLength) :
QValidator(parent),
m_maxLength(maxLength)
{
}
class HexStringValidator : public QValidator
{
Q_OBJECT
public:
explicit HexStringValidator(QObject *parent = nullptr);
explicit HexStringValidator(QObject *parent, uint maxLength);
QValidator::State validate(QString &input, int &pos) const;
void setMaxLength(int maxLength);
private:
uint m_maxLength = 0;
};
The problem is in this function:
I have a lineedit with this validator and call it with a valid Hex String.
ui->lineEditCANCommand->setValidator(new HexStringValidator(this, 4));
ui->lineEditCANCommand->setText("000003e9");
QValidator::State HexStringValidator::validate(QString &input, int &pos) const
{
const int maxSize = 2 * static_cast<int>(m_maxLength);
const QChar space = QLatin1Char(' ');
QString data = input;
data.remove(space);
if (data.isEmpty())
return Intermediate;
// limit maximum size and forbid trailing spaces
if ((data.size() > maxSize) || (data.size() == maxSize && input.endsWith(space)))
return Invalid;
// check if all input is valid
const QRegularExpression re(QStringLiteral("^[[:xdigit:]]*$"));
if (!re.match(data).hasMatch())
return Invalid;
// insert a space after every two hex nibbles
const QRegularExpression insertSpace(QStringLiteral("(?:[[:xdigit:]]{2} )*[[:xdigit:]]{3}"));
if (insertSpace.match(input).hasMatch()) {
input.insert(input.size() - 1, space);
pos = input.size();
}
return Acceptable;
}
The function is supposed to change this to "00 00 03 e9"
It however runs endless and creates
000003e 9
000003e 9
000003e 9
000003e 9
What is going wrong? Note, this code is from Qt and not mine.
In Qt6 problem should be gone.
There was related to this problem issue and two following commits:
Can example: Fix crash in payload hex editor,
CAN Example: Properly format payload hex string
It is possible to test since Qt 6.2.0.
First, I changed insertSpace regular expression to threeDigits:
QStringLiteral("[[:xdigit:]]{3}")
and insert space if 3 digits were added sequentially:
input.insert(match.capturedEnd() - 1, space);
That solves crash problem (infinite recursion).
Then I added auxilliary condition isEvenHex and formatter formatHexData with oneDigitAndSpace and threeDigits regular expressions that removes all extra spaces and add space after every two hex nibbles. I call them respectively on text change and sendButton click:
connect(m_ui->payloadEdit, &QLineEdit::textChanged, frameIdOrPayloadChanged);
connect(m_ui->sendButton, &QPushButton::clicked, [this]() {
...
m_ui->payloadEdit->setText(formatHexData(data));
...
}
Trying to solve your task I added:
QValidator::State HexStringValidator::validate(QString &input, int &pos) const
{
...
QString data = input;
const QRegularExpression twoSpaces(QStringLiteral("([\\s]{2})"));
if (twoSpaces.match(data).hasMatch())
return Invalid;
data.remove(space);
...
But there is one problem: if you are editing string in payload editor you may remove digits and two spaces can occure. The validator won't allow you remove characters between spaces. As a compromise I would introduce
threeSpaces(QStringLiteral("([\\s]{3})"));
validator instead of twoSpaces. With that one you may enter two spaces sequentially but still can edit string. I may ask QtSerialBus developers to include that change too.

QSystemTrayIcon notification message with custom icon

QSystemTrayIcon has a function :
void showMessage(const QString &title, const QString &msg,
MessageIcon icon = Information, int msecs = 10000);
is there a way to change it to custom icon , for example like this -
void showIconMessage(const QString &title, const QString &msg,
QIcon icon = QIcon(), int msecs = 10000);
without modifying the Qt sources
I know that showMessage (d is instance of QSystemTrayIconPrivate and is called with Q_D(QSystemTrayIcon) macro)
void QSystemTrayIcon::showMessage(const QString& title, const QString& msg,
QSystemTrayIcon::MessageIcon icon, int msecs)
{
Q_D(QSystemTrayIcon);
if (d->visible)
d->showMessage_sys(title, msg, icon, msecs);
}
calls showMessage_sys from QSystemTrayIconPrivate where in turn all the magic with icon happens:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}
Now, it seems, that I need to re-implement these two functions in two classes and i'm ready to go, but.. It seems that QSystemTrayIcon is closely tied to QSystemTrayIconPrivate. Instance of QSystemTrayIconPrivate is created only in QSystemTrayIcon constructor (which I can't really change if I plan to create classes that inherit both QSystemTrayIcon and QSystemTrayIconPrivate and re-implement showMessage functions):
QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
}
QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
setIcon(icon);
}
So is there anything I am missing? Or is there another way to simply show notification message with custom icon?
What you could try (not sure if it will work for system tray) is do the same as described in this answer and override the SP_MessageBoxWarning / SP_MessageBoxCritical / SP_MessageBoxInformation icons, but as I said I'm not sure if the system tray just uses a downscaled version of the message box icons or if the system tray icons are separate. In the case of the latter, I guess you will have to patch QT sources, maybe add a new item to the QSystemTrayIcon and patch the switch to call some function provided by you to return the needed icon. Something like:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
case QSystemTrayIcon::Custom:
// Call a function that will fetch the needed icon and assign it to notificationIcon
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}

Qt tr() translation with static members and namespaces

I'm working on translation our Qt GUI project.
*.ts file is generated successfully.
I filled *.ts file with translations using Qt Linguist.
But at runtime, translation with namespaces and static members does not work.
Other translations (when tr() method calls in class, that are inherited from QObject) works ok.
I have the following code (translation does not work):
Example with namespaces:
// example with namespaces
// declaration in header
namespace Error
{
namespace RadionetworkInput
{
QString alreadyInUse = QT_TR_NOOP("already in use");
char requestFailed[] = QT_TR_NOOP("request failed");
}
}
Usage in cpp:
// usage in cpp code0
QString error0 = Error::RadionetworkInput::alreadyInUse;
QString error1 = tr(Error::RadionetworkInput::requestFailed);
Example with static members:
// example with static members
// declaration in header
namespace Error
{
class RadionetworkInput
{
public:
static const QString alreadyInUse;
static const char requestFailed[];
}
QString Error::RadionetworkInput::alreadyInUse = QT_TR_NOOP("already in use");
char Error::RadionetworkInput::requestFailed[] = QT_TR_NOOP("request failed");
}
Usage in cpp code
// usage in cpp code
QString error0 = Error::RadionetworkInput::alreadyInUse;
QString error1 = tr(Error::RadionetworkInput::requestFailed);
Example, with working translation:
class ViewNetwork : public QObject
{
Q_OBJECT
public:
explicit ViewNetwork(QString name = tr("New Radionetwork"));
};
Usage in code:
ViewNetwork::ViewNetwork(QString name)
{
QString dummy = name;
}
Using QObject::tr() instead QT_TR_NOOP() macro does not help.
Problem is your understanding how it works. With static members variables they are initialized before main function starts.
This means two thing:
they are initialized before QApplication object is created and before yuo load translation files! (I'm surprised that this didn't lead to a crash)
value is calculated only once (not updated if translation changes)!
What you should do? Just change static variable to functions:
// example with namespaces
// declaration in header
namespace Error
{
namespace RadionetworkInput
{
QString alreadyInUse() { return QT_TR_NOOP("already in use"); }
QString requestFailed() { return QT_TR_NOOP("request failed"); }
}
}
and
// example with static members
// declaration in header
namespace Error
{
class RadionetworkInput
{
public:
static QString alreadyInUse();
static QString requestFailed();
}
QString Error::RadionetworkInput::alreadyInUse() { return QT_TR_NOOP("already in use"); }
QString Error::RadionetworkInput::requestFailed() { return QT_TR_NOOP("request failed"); }
}
Returning translation as char[] is pointless, co I've corrected that also (I don't know why you did it).

Qt 4.8 - QFileIconProvider, Getting icon for non-existent file (based on extension)

I'm currently trying to get the icon based on a file extension, but it seems like QFileIconProvider will only return an icon if it can actually read an existing file. Is there any way I can get a QIcon based off of a file extension? One alternative would be to write a temporary file with the desired extension, but that is very inefficient so I'm looking for a way around.
Any help would be appreciated!
Here's my solution for Windows:
iconprovider.h:
class IconProvider
{
public:
static IconProvider * instance();
static QIcon fileIcon(const QString &filename);
static QIcon dirIcon();
private:
IconProvider() {}
private:
static IconProvider *self;
QPixmapCache iconCache;
QFileIconProvider iconProvider;
};
iconprovider.cpp:
IconProvider *IconProvider::self = 0;
IconProvider *IconProvider::instance()
{
if(!self)
self = new IconProvider();
return self;
}
QIcon IconProvider::fileIcon(const QString &filename)
{
QFileInfo fileInfo(filename);
QPixmap pixmap;
#ifdef Q_OS_WIN32
if (fileInfo.suffix().isEmpty() || fileInfo.suffix() == "exe" && fileInfo.exists())
{
return instance()->iconProvider.icon(fileInfo);
}
if (!instance()->iconCache.find(fileInfo.suffix(), &pixmap))
{
// Support for nonexistent file type icons, will reimplement it as custom icon provider later
/* We don't use the variable, but by storing it statically, we
* ensure CoInitialize is only called once. */
static HRESULT comInit = CoInitialize(NULL);
Q_UNUSED(comInit);
SHFILEINFO shFileInfo;
unsigned long val = 0;
val = SHGetFileInfo((const wchar_t *)("foo." + fileInfo.suffix()).utf16(), 0, &shFileInfo,
sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
// Even if GetFileInfo returns a valid result, hIcon can be empty in some cases
if (val && shFileInfo.hIcon)
{
pixmap = QPixmap::fromWinHICON(shFileInfo.hIcon);
if (!pixmap.isNull())
{
instance()->iconCache.insert(fileInfo.suffix(), pixmap);
}
DestroyIcon(shFileInfo.hIcon);
}
else
{
// TODO: Return default icon if nothing else found
}
}
#else
// Default icon for Linux and Mac OS X for now
return instance()->iconProvider.icon(fileInfo);
#endif
return QIcon(pixmap);
}
QIcon IconProvider::dirIcon()
{
return instance()->iconProvider.icon(QFileIconProvider::Folder);
}

Resources