From 79585e218c40f9010601e8af442b573ece40c5ea Mon Sep 17 00:00:00 2001
From: Neil
Date: Tue, 20 Apr 2021 10:57:59 +1000
Subject: Bug [#2027]. Implement font locale SCI_SETFONTLOCALE on Win32 using
DirectWrite.
---
doc/ScintillaDoc.html | 20 ++++++++++++++++++--
doc/ScintillaHistory.html | 4 ++++
include/Scintilla.h | 2 ++
include/Scintilla.iface | 6 ++++++
src/CallTip.cxx | 7 +++++--
src/CallTip.h | 3 ++-
src/Editor.cxx | 10 ++++++++++
src/ScintillaBase.cxx | 1 +
src/ViewStyle.cxx | 15 ++++++++++++---
src/ViewStyle.h | 5 ++++-
test/simpleTests.py | 7 +++++++
11 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html
index f3b26b88f..0443c7a24 100644
--- a/doc/ScintillaDoc.html
+++ b/doc/ScintillaDoc.html
@@ -120,7 +120,7 @@
Scintilla Documentation
- Last edited 9 April 2021 NH
+ Last edited 20 April 2021 NH
Scintilla 5 has moved the lexers from Scintilla into a new
Lexilla project.
@@ -2970,6 +2970,9 @@ struct Sci_TextToFind {
SCI_STYLESETHOTSPOT(int style, bool
hotspot)
SCI_STYLEGETHOTSPOT(int style) → bool
+ SCI_SETFONTLOCALE(<unused>, const char
+ *fontName)
+ SCI_GETFONTLOCALE(<unused>, char *fontName) → int
SCI_STYLERESETDEFAULT
@@ -3298,6 +3301,15 @@ struct Sci_TextToFind {
+
SCI_SETFONTLOCALE(<unused>, const char *localeName)
+ SCI_GETFONTLOCALE(<unused>, char *localeName NUL-terminated) → int
+ These messages set the locale used for font selection with language-dependent glyphs.
+ It may, depending on platform and other circumstances influence the display of text, so setting "zh-Hant" may result in traditional
+ Chinese display and "zh-Hans" may result in simplified Chinese display.
+ It is currently only implemented for Win32 using DirectWrite where the value is passed as the localeName argument to CreateTextFormat.
+ The default value is US English "en-us".
+
+
Caret, selection, and hotspot styles
The selection is shown by changing the foreground and/or background colours. If one of these
@@ -4121,7 +4133,11 @@ struct Sci_TextToFind {
SC_TECHNOLOGY_DIRECTWRITEDC differs from
SC_TECHNOLOGY_DIRECTWRITE by using DirectWrite to draw into a GDI DC.
Since Direct2D buffers drawing, Scintilla's buffering can be turned off with
- SCI_SETBUFFEREDDRAW(0).
+ SCI_SETBUFFEREDDRAW(0).
+ When using DirectWrite, you can use
+ SCI_SETFONTLOCALE
+ to set an appropriate font locale to draw text with expected language-dependent glyphs.
+
SCI_SETFONTQUALITY(int fontQuality)
SCI_GETFONTQUALITY → int
diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html
index a63ac8fc3..5ced2dd86 100644
--- a/doc/ScintillaHistory.html
+++ b/doc/ScintillaHistory.html
@@ -582,6 +582,10 @@
On Windows, fix encoding used for text display with DirectWrite.
Bug #2246.
+
+ Implement font locale SCI_SETFONTLOCALE. Initially only for DirectWrite on Win32.
+ Bug #2027.
+
Release 5.0.1
diff --git a/include/Scintilla.h b/include/Scintilla.h
index 1ea5d3fa1..ca512ab71 100644
--- a/include/Scintilla.h
+++ b/include/Scintilla.h
@@ -103,6 +103,8 @@ typedef sptr_t (*SciFnDirect)(sptr_t ptr, unsigned int iMessage, uptr_t wParam,
#define SCI_GETNEXTTABSTOP 2677
#define SC_CP_UTF8 65001
#define SCI_SETCODEPAGE 2037
+#define SCI_SETFONTLOCALE 2760
+#define SCI_GETFONTLOCALE 2761
#define SC_IME_WINDOWED 0
#define SC_IME_INLINE 1
#define SCI_GETIMEINTERACTION 2678
diff --git a/include/Scintilla.iface b/include/Scintilla.iface
index 8cce965cc..946ba1ec8 100644
--- a/include/Scintilla.iface
+++ b/include/Scintilla.iface
@@ -282,6 +282,12 @@ val SC_CP_UTF8=65001
# The SC_CP_UTF8 value can be used to enter Unicode mode.
set void SetCodePage=2037(int codePage,)
+# Set the locale for displaying text.
+set void SetFontLocale=2760(, string localeName)
+
+# Set the locale for displaying text.
+set void GetFontLocale=2761(, stringresult localeName)
+
enu IMEInteraction=SC_IME_
val SC_IME_WINDOWED=0
val SC_IME_INLINE=1
diff --git a/src/CallTip.cxx b/src/CallTip.cxx
index 22f559ef7..f9f3ce7f7 100644
--- a/src/CallTip.cxx
+++ b/src/CallTip.cxx
@@ -272,7 +272,9 @@ void CallTip::MouseClick(Point pt) noexcept {
PRectangle CallTip::CallTipStart(Sci::Position pos, Point pt, int textHeight, const char *defn,
const char *faceName, int size,
int codePage_, int characterSet,
- int technology, const Window &wParent) {
+ int technology,
+ const char *localeName,
+ const Window &wParent) {
clickPlace = 0;
val = defn;
codePage = codePage_;
@@ -283,7 +285,8 @@ PRectangle CallTip::CallTipStart(Sci::Position pos, Point pt, int textHeight, co
inCallTipMode = true;
posStartCallTip = pos;
const XYPOSITION deviceHeight = static_cast(surfaceMeasure->DeviceHeightFont(size));
- const FontParameters fp(faceName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, SC_WEIGHT_NORMAL, false, 0, technology, characterSet);
+ const FontParameters fp(faceName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, SC_WEIGHT_NORMAL,
+ false, 0, technology, characterSet, localeName);
font = Font::Allocate(fp);
// Look for multiple lines in the text
// Only support \n here - simply means container must avoid \r!
diff --git a/src/CallTip.h b/src/CallTip.h
index 6cc89d3a5..f1358a94a 100644
--- a/src/CallTip.h
+++ b/src/CallTip.h
@@ -72,7 +72,8 @@ public:
/// Setup the calltip and return a rectangle of the area required.
PRectangle CallTipStart(Sci::Position pos, Point pt, int textHeight, const char *defn,
const char *faceName, int size, int codePage_,
- int characterSet, int technology, const Window &wParent);
+ int characterSet, int technology, const char *localeName,
+ const Window &wParent);
void CallTipCancel();
diff --git a/src/Editor.cxx b/src/Editor.cxx
index 63ed979d9..208a075aa 100644
--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -7199,6 +7199,16 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
case SCI_GETELEMENTALLOWSTRANSLUCENT:
return vs.ElementAllowsTranslucent(static_cast(wParam));
+ case SCI_SETFONTLOCALE:
+ if (lParam) {
+ vs.SetFontLocaleName(CharPtrFromSPtr(lParam));
+ InvalidateStyleRedraw();
+ }
+ break;
+
+ case SCI_GETFONTLOCALE:
+ return StringResult(lParam, vs.localeName.c_str());
+
#ifdef INCLUDE_DEPRECATED_FEATURES
case SCI_SETSTYLEBITS:
vs.EnsureStyle(0xff);
diff --git a/src/ScintillaBase.cxx b/src/ScintillaBase.cxx
index e2fb58206..72bb5f6e0 100644
--- a/src/ScintillaBase.cxx
+++ b/src/ScintillaBase.cxx
@@ -478,6 +478,7 @@ void ScintillaBase::CallTipShow(Point pt, const char *defn) {
CodePage(),
vs.styles[ctStyle].characterSet,
vs.technology,
+ vs.localeName.c_str(),
wMain);
// If the call-tip window would be out of the client
// space
diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx
index f553f4205..aa6779728 100644
--- a/src/ViewStyle.cxx
+++ b/src/ViewStyle.cxx
@@ -42,14 +42,15 @@ FontRealised::FontRealised() noexcept = default;
FontRealised::~FontRealised() {
}
-void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs) {
+void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs, const char *localeName) {
PLATFORM_ASSERT(fs.fontName);
sizeZoomed = fs.size + zoomLevel * SC_FONT_SIZE_MULTIPLIER;
if (sizeZoomed <= 2 * SC_FONT_SIZE_MULTIPLIER) // Hangs if sizeZoomed <= 1
sizeZoomed = 2 * SC_FONT_SIZE_MULTIPLIER;
const float deviceHeight = static_cast(surface.DeviceHeightFont(sizeZoomed));
- const FontParameters fp(fs.fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, fs.weight, fs.italic, fs.extraFontFlag, technology, fs.characterSet);
+ const FontParameters fp(fs.fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, fs.weight,
+ fs.italic, fs.extraFontFlag, technology, fs.characterSet, localeName);
font = Font::Allocate(fp);
ascent = static_cast(surface.Ascent(font.get()));
@@ -152,6 +153,8 @@ ViewStyle::ViewStyle(const ViewStyle &source) : markers(MARKER_MAX + 1), indicat
wrapVisualFlagsLocation = source.wrapVisualFlagsLocation;
wrapVisualStartIndent = source.wrapVisualStartIndent;
wrapIndentMode = source.wrapIndentMode;
+
+ localeName = source.localeName;
}
ViewStyle::~ViewStyle() {
@@ -286,6 +289,8 @@ void ViewStyle::Init(size_t stylesSize_) {
wrapVisualFlagsLocation = 0;
wrapVisualStartIndent = 0;
wrapIndentMode = SC_WRAPINDENT_FIXED;
+
+ localeName = localeNameDefault;
}
void ViewStyle::Refresh(Surface &surface, int tabInChars) {
@@ -307,7 +312,7 @@ void ViewStyle::Refresh(Surface &surface, int tabInChars) {
// Ask platform to allocate each unique font.
for (std::pair> &font : fonts) {
- font.second->Realise(surface, zoomLevel, technology, font.first);
+ font.second->Realise(surface, zoomLevel, technology, font.first, localeName.c_str());
}
// Set the platform font handle and measurements for each style.
@@ -400,6 +405,10 @@ void ViewStyle::SetStyleFontName(int styleIndex, const char *name) {
styles[styleIndex].fontName = fontNames.Save(name);
}
+void ViewStyle::SetFontLocaleName(const char *name) {
+ localeName = name;
+}
+
bool ViewStyle::ProtectionActive() const noexcept {
return someStylesProtected;
}
diff --git a/src/ViewStyle.h b/src/ViewStyle.h
index 7f50804c2..946bd6c60 100644
--- a/src/ViewStyle.h
+++ b/src/ViewStyle.h
@@ -37,7 +37,7 @@ public:
FontRealised &operator=(const FontRealised &) = delete;
FontRealised &operator=(FontRealised &&) = delete;
virtual ~FontRealised();
- void Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs);
+ void Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs, const char *localeName);
};
enum class IndentView {none, real, lookForward, lookBoth};
@@ -168,6 +168,8 @@ public:
int wrapVisualStartIndent;
int wrapIndentMode; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
+ std::string localeName;
+
ViewStyle();
ViewStyle(const ViewStyle &source);
ViewStyle(ViewStyle &&) = delete;
@@ -184,6 +186,7 @@ public:
void ResetDefaultStyle();
void ClearStyles();
void SetStyleFontName(int styleIndex, const char *name);
+ void SetFontLocaleName(const char *name);
bool ProtectionActive() const noexcept;
int ExternalMarginWidth() const noexcept;
int MarginFromLocation(Point pt) const noexcept;
diff --git a/test/simpleTests.py b/test/simpleTests.py
index 89e2ff69c..63efc8019 100644
--- a/test/simpleTests.py
+++ b/test/simpleTests.py
@@ -1916,6 +1916,13 @@ class TestStyleAttributes(unittest.TestCase):
self.ed.SetDefaultFoldDisplayText(0, b"...")
self.assertEquals(self.ed.GetDefaultFoldDisplayText(), b"...")
+ def testFontLocale(self):
+ initialLocale = "en-us".encode("UTF-8")
+ testLocale = "zh-Hans".encode("UTF-8")
+ self.assertEquals(self.ed.GetFontLocale(), initialLocale)
+ self.ed.FontLocale = testLocale
+ self.assertEquals(self.ed.GetFontLocale(), testLocale)
+
class TestIndices(unittest.TestCase):
def setUp(self):
self.xite = Xite.xiteFrame
--
cgit v1.2.3