From 4d1a31d6a74019c3d43de1c33e2be093ed6dfd11 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 31 Jul 2021 08:49:25 +1000 Subject: Implement StyleSetCheckMonospaced. --- src/ViewStyle.cxx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/ViewStyle.cxx') diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 33b861445..47f76d357 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -18,6 +18,7 @@ #include #include #include +#include #include "ScintillaTypes.h" @@ -64,6 +65,24 @@ void FontRealised::Realise(Surface &surface, int zoomLevel, Technology technolog capitalHeight = surface.Ascent(font.get()) - surface.InternalLeading(font.get()); aveCharWidth = surface.AverageCharWidth(font.get()); spaceWidth = surface.WidthText(font.get(), " "); + + if (fs.checkMonospaced) { + std::string allASCIIGraphic("Ayfi"); // "Ay" is normally strongly kerned and "fi" may be a ligature + for (unsigned char ch = 0x20; ch <= 0x7E; ch++) { + allASCIIGraphic.push_back(ch); + } + std::vector positions(allASCIIGraphic.length()); + surface.MeasureWidths(font.get(), allASCIIGraphic, positions.data()); + std::adjacent_difference(positions.begin(), positions.end(), positions.begin()); + const XYPOSITION maxWidth = *std::max_element(positions.begin(), positions.end()); + const XYPOSITION minWidth = *std::min_element(positions.begin(), positions.end()); + const XYPOSITION variance = maxWidth - minWidth; + const XYPOSITION scaledVariance = variance / aveCharWidth; + constexpr XYPOSITION monospaceWidthEpsilon = 0.000001; // May need tweaking if monospace fonts vary more + monospaceASCII = scaledVariance < monospaceWidthEpsilon; + } else { + monospaceASCII = false; + } } ViewStyle::ViewStyle() : markers(MarkerMax + 1), indicators(static_cast(IndicatorNumbers::Max) + 1) { -- cgit v1.2.3