Skip to content

Commit

Permalink
Support locales with three letter language codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsbers committed Nov 10, 2024
1 parent 7be7963 commit ae2efc3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
30 changes: 20 additions & 10 deletions src/yfontxft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#ifdef CONFIG_XFREETYPE

#include "ascii.h"
#include "ypaint.h"
#include "yprefs.h"
#include "ystring.h"
Expand Down Expand Up @@ -109,16 +110,25 @@ YXftFont::YXftFont(mstring name, bool use_xlfd):
if (use_xlfd) {
font = XftFontOpenXlfd(xapp->display(), xapp->screen(), fname);
} else {

if (fname.find(":lang=") < 0)
{
auto lclocale = mstring(YLocale::getCheckedExplicitLocale(true));
if (lclocale) {
fname = (fname + ":lang=" + lclocale.substring(0,2) + "-"
+ lclocale.substring(3,2)).lower();
if (fname.find(":lang=") < 0) {
const char* yloc = YLocale::getCheckedLocaleName();
const char* unsc = yloc ? strchr(yloc, '_') : nullptr;
const int prefix = unsc ? int(unsc - yloc) : 0;
if (prefix == 2 || prefix == 3) {
char buf[10];
memcpy(buf, yloc, prefix);
int len = prefix;
buf[len] = '-';
len++;
buf[len] = ASCII::toLower(yloc[len]);
len++;
buf[len] = ASCII::toLower(yloc[len]);
len++;
buf[len] = '\0';
fname += ":lang=";
fname += buf;
}
}

font = XftFontOpenName(xapp->display(), xapp->screen(), fname);
}
if (font) {
Expand Down Expand Up @@ -288,7 +298,7 @@ void YXftFont::drawLimitLeft(Graphics& g, XftFont* font, int x, int y,
lo -= 1;
if (0 < ew) {
const int size = lo + 2;
asmart<wchar_t> copy(new wchar_t[size]);
wchar_t copy[size];

memcpy(copy, str, lo * sizeof(wchar_t));
copy[lo] = el;
Expand Down Expand Up @@ -321,7 +331,7 @@ void YXftFont::drawLimitRight(Graphics& g, XftFont* font, int x, int y,
}
if (0 < ew) {
const int size = lo + 2;
asmart<wchar_t> copy(new wchar_t[size]);
wchar_t copy[size];
memcpy(copy + 1, str + len - lo, lo * sizeof(wchar_t));
copy[0] = el;
copy[lo + 1] = 0;
Expand Down
21 changes: 11 additions & 10 deletions src/ylocale.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,17 @@ char* YLocale::narrowString(const wchar_t* uStr, size_t uLen, size_t& lLen) {
return dest;
}

const char *YLocale::getCheckedExplicitLocale(bool lctype)
{
auto loc = setlocale(lctype ? LC_CTYPE : LC_MESSAGES, NULL);
if (loc == NULL)
return NULL;
return (islower(*loc & 0xff)
&& islower(loc[1] & 0xff)
&& !isalpha(loc[2] & 0xff))
? loc
: NULL;
const char* YLocale::getCheckedLocaleName() {
using namespace ASCII;
const char* loc = getLocaleName();
if (loc && isLower(*loc) && isLower(loc[1])) {
int i = 2;
if (isLower(loc[i]))
i++;
if (loc[i] == '_' && isUpper(loc[i + 1]) && isUpper(loc[i + 2]))
return loc;
}
return nullptr;
}

const char *YLocale::getLocaleName() {
Expand Down
2 changes: 1 addition & 1 deletion src/ylocale.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class YLocale {
static wchar_t* wideCharString(const char* str, size_t len, size_t& out);
#endif
static char* narrowString(const wchar_t* uStr, size_t uLen, size_t& lLen);
static const char* getCheckedExplicitLocale(bool lctype=true);
static const char* getCheckedLocaleName();

private:
class YConverter* converter;
Expand Down

0 comments on commit ae2efc3

Please sign in to comment.