// Scintilla source code edit control /** @file ViewStyle.cxx ** Store information on how the document is to be viewed. **/ // Copyright 1998-2003 by Neil Hodgson // The License.txt file describes the conditions under which this software may be distributed. #include #include #include #include #include #include #include #include #include #include #include #include #include "Debugging.h" #include "Geometry.h" #include "Platform.h" #include "Scintilla.h" #include "Position.h" #include "UniqueString.h" #include "Indicator.h" #include "XPM.h" #include "LineMarker.h" #include "Style.h" #include "ViewStyle.h" using namespace Scintilla; MarginStyle::MarginStyle(int style_, int width_, int mask_) noexcept : style(style_), width(width_), mask(mask_), sensitive(false), cursor(SC_CURSORREVERSEARROW) { } FontRealised::FontRealised() noexcept = default; FontRealised::~FontRealised() = default; 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, localeName); font = Font::Allocate(fp); ascent = static_cast(surface.Ascent(font.get())); descent = static_cast(surface.Descent(font.get())); capitalHeight = surface.Ascent(font.get()) - surface.InternalLeading(font.get()); aveCharWidth = surface.AverageCharWidth(font.get()); spaceWidth = surface.WidthText(font.get(), " "); } ViewStyle::ViewStyle() : markers(MARKER_MAX + 1), indicators(INDICATOR_MAX + 1) { Init(); } // Copy constructor only called when printing copies the screen ViewStyle so it can be // modified for printing styles. ViewStyle::ViewStyle(const ViewStyle &source) : markers(MARKER_MAX + 1), indicators(INDICATOR_MAX + 1) { Init(source.styles.size()); styles = source.styles; for (size_t sty=0; sty 0) maskInLine &= ~m.mask; maskDefinedMarkers |= m.mask; } maskDrawInText = 0; for (int markBit = 0; markBit < 32; markBit++) { const int maskBit = 1U << markBit; switch (markers[markBit].markType) { case SC_MARK_EMPTY: maskInLine &= ~maskBit; break; case SC_MARK_BACKGROUND: case SC_MARK_UNDERLINE: maskInLine &= ~maskBit; maskDrawInText |= maskDefinedMarkers & maskBit; break; default: // Other marker types do not affect the masks break; } } } void ViewStyle::Init(size_t stylesSize_) { AllocStyles(stylesSize_); nextExtendedStyle = 256; fontNames.Clear(); ResetDefaultStyle(); // There are no image markers by default, so no need for calling CalcLargestMarkerHeight() largestMarkerHeight = 0; indicators[0] = Indicator(INDIC_SQUIGGLE, ColourAlpha(0, 0x7f, 0)); indicators[1] = Indicator(INDIC_TT, ColourAlpha(0, 0, 0xff)); indicators[2] = Indicator(INDIC_PLAIN, ColourAlpha(0xff, 0, 0)); technology = SC_TECHNOLOGY_DEFAULT; indicatorsDynamic = false; indicatorsSetFore = false; lineHeight = 1; lineOverlap = 0; maxAscent = 1; maxDescent = 1; aveCharWidth = 8; spaceWidth = 8; tabWidth = spaceWidth * 8; selection.colours.fore.reset(); selection.colours.back = ColourAlpha(0xc0, 0xc0, 0xc0); selection.additionalForeground = ColourAlpha(0xff, 0, 0); selection.additionalBackground = ColourAlpha(0xd7, 0xd7, 0xd7); selection.background2 = ColourAlpha(0xb0, 0xb0, 0xb0); selection.alpha = SC_ALPHA_NOALPHA; selection.additionalAlpha = SC_ALPHA_NOALPHA; selection.eolFilled = false; foldmarginColour.reset(); foldmarginHighlightColour.reset(); whitespaceColours.fore.reset(); whitespaceColours.back.reset(); controlCharSymbol = 0; /* Draw the control characters */ controlCharWidth = 0; selbar = Platform::Chrome(); selbarlight = Platform::ChromeHighlight(); styles[STYLE_LINENUMBER].fore = ColourAlpha(0, 0, 0); styles[STYLE_LINENUMBER].back = Platform::Chrome(); caret.colour = ColourAlpha(0, 0, 0); caret.additionalColour = ColourAlpha(0x7f, 0x7f, 0x7f); caret.style = CARETSTYLE_LINE; caret.width = 1; caretLine.background = ColourAlpha(0xff, 0xff, 0); caretLine.show = false; caretLine.alwaysShow = false; caretLine.alpha = SC_ALPHA_NOALPHA; caretLine.frame = 0; someStylesProtected = false; someStylesForceCase = false; hotspotColours.fore.reset(); hotspotColours.back.reset(); hotspotUnderline = true; leftMarginWidth = 1; rightMarginWidth = 1; ms.resize(SC_MAX_MARGIN + 1); ms[0] = MarginStyle(SC_MARGIN_NUMBER); ms[1] = MarginStyle(SC_MARGIN_SYMBOL, 16, ~SC_MASK_FOLDERS); ms[2] = MarginStyle(SC_MARGIN_SYMBOL); marginInside = true; CalculateMarginWidthAndMask(); textStart = marginInside ? fixedColumnWidth : leftMarginWidth; zoomLevel = 0; viewWhitespace = WhiteSpace::invisible; tabDrawMode = TabDrawMode::longArrow; whitespaceSize = 1; viewIndentationGuides = IndentView::none; viewEOL = false; extraFontFlag = 0; extraAscent = 0; extraDescent = 0; marginStyleOffset = 0; annotationVisible = ANNOTATION_HIDDEN; annotationStyleOffset = 0; eolAnnotationVisible = EOLANNOTATION_HIDDEN; eolAnnotationStyleOffset = 0; braceHighlightIndicatorSet = false; braceHighlightIndicator = 0; braceBadLightIndicatorSet = false; braceBadLightIndicator = 0; edgeState = EDGE_NONE; theEdge = EdgeProperties(0, ColourAlpha(0xc0, 0xc0, 0xc0)); marginNumberPadding = 3; ctrlCharPadding = 3; // +3 For a blank on front and rounded edge each side lastSegItalicsOffset = 2; wrap.state = WrapMode::none; wrap.visualFlags = 0; wrap.visualFlagsLocation = 0; wrap.visualStartIndent = 0; wrap.indentMode = SC_WRAPINDENT_FIXED; localeName = localeNameDefault; } void ViewStyle::Refresh(Surface &surface, int tabInChars) { fonts.clear(); selbar = Platform::Chrome(); selbarlight = Platform::ChromeHighlight(); // Apply the extra font flag which controls text drawing quality to each style. for (Style &style : styles) { style.extraFontFlag = extraFontFlag; } // Create a FontRealised object for each unique font in the styles. CreateAndAddFont(styles[STYLE_DEFAULT]); for (const Style &style : styles) { CreateAndAddFont(style); } // Ask platform to allocate each unique font. for (const std::pair> &font : fonts) { font.second->Realise(surface, zoomLevel, technology, font.first, localeName.c_str()); } // Set the platform font handle and measurements for each style. for (Style &style : styles) { const FontRealised *fr = Find(style); style.Copy(fr->font, *fr); } indicatorsDynamic = std::any_of(indicators.cbegin(), indicators.cend(), [](const Indicator &indicator) noexcept { return indicator.IsDynamic(); }); indicatorsSetFore = std::any_of(indicators.cbegin(), indicators.cend(), [](const Indicator &indicator) noexcept { return indicator.OverridesTextFore(); }); maxAscent = 1; maxDescent = 1; FindMaxAscentDescent(); maxAscent += extraAscent; maxDescent += extraDescent; lineHeight = maxAscent + maxDescent; lineOverlap = lineHeight / 10; if (lineOverlap < 2) lineOverlap = 2; if (lineOverlap > lineHeight) lineOverlap = lineHeight; someStylesProtected = std::any_of(styles.cbegin(), styles.cend(), [](const Style &style) noexcept { return style.IsProtected(); }); someStylesForceCase = std::any_of(styles.cbegin(), styles.cend(), [](const Style &style) noexcept { return style.caseForce != Style::CaseForce::mixed; }); aveCharWidth = styles[STYLE_DEFAULT].aveCharWidth; spaceWidth = styles[STYLE_DEFAULT].spaceWidth; tabWidth = spaceWidth * tabInChars; controlCharWidth = 0.0; if (controlCharSymbol >= 32) { const char cc[2] = { static_cast(controlCharSymbol), '\0' }; controlCharWidth = surface.WidthText(styles[STYLE_CONTROLCHAR].font.get(), cc); } CalculateMarginWidthAndMask(); textStart = marginInside ? fixedColumnWidth : leftMarginWidth; } void ViewStyle::ReleaseAllExtendedStyles() noexcept { nextExtendedStyle = 256; } int ViewStyle::AllocateExtendedStyles(int numberStyles) { const int startRange = nextExtendedStyle; nextExtendedStyle += numberStyles; EnsureStyle(nextExtendedStyle); for (int i=startRange; i= styles.size()) { AllocStyles(index+1); } } void ViewStyle::ResetDefaultStyle() { styles[STYLE_DEFAULT].Clear(ColourAlpha(0,0,0), ColourAlpha(0xff,0xff,0xff), Platform::DefaultFontSize() * SC_FONT_SIZE_MULTIPLIER, fontNames.Save(Platform::DefaultFont()), SC_CHARSET_DEFAULT, SC_WEIGHT_NORMAL, false, false, false, Style::CaseForce::mixed, true, true, false); } void ViewStyle::ClearStyles() { // Reset all styles to be like the default style for (size_t i=0; i= x) && (pt.x < x + ms[i].width)) margin = static_cast(i); x += ms[i].width; } return margin; } bool ViewStyle::ValidStyle(size_t styleIndex) const noexcept { return styleIndex < styles.size(); } void ViewStyle::CalcLargestMarkerHeight() noexcept { largestMarkerHeight = 0; for (const LineMarker &marker : markers) { switch (marker.markType) { case SC_MARK_PIXMAP: if (marker.pxpm && marker.pxpm->GetHeight() > largestMarkerHeight) largestMarkerHeight = marker.pxpm->GetHeight(); break; case SC_MARK_RGBAIMAGE: if (marker.image && marker.image->GetHeight() > largestMarkerHeight) largestMarkerHeight = marker.image->GetHeight(); break; default: // Only images have their own natural heights break; } } } int ViewStyle::GetFrameWidth() const noexcept { return std::clamp(caretLine.frame, 1, lineHeight / 3); } bool ViewStyle::IsLineFrameOpaque(bool caretActive, bool lineContainsCaret) const noexcept { return caretLine.frame && (caretActive || caretLine.alwaysShow) && caretLine.show && (caretLine.alpha == SC_ALPHA_NOALPHA) && lineContainsCaret; } // See if something overrides the line background colour: Either if caret is on the line // and background colour is set for that, or if a marker is defined that forces its background // colour onto the line, or if a marker is defined but has no selection margin in which to // display itself (as long as it's not an SC_MARK_EMPTY marker). These are checked in order // with the earlier taking precedence. When multiple markers cause background override, // the colour for the highest numbered one is used. std::optional ViewStyle::Background(int marksOfLine, bool caretActive, bool lineContainsCaret) const { std::optional background; if (!caretLine.frame && (caretActive || caretLine.alwaysShow) && caretLine.show && (caretLine.alpha == SC_ALPHA_NOALPHA) && lineContainsCaret) { background = caretLine.background; } if (!background && marksOfLine) { int marks = marksOfLine; for (int markBit = 0; (markBit < 32) && marks; markBit++) { if ((marks & 1) && (markers[markBit].markType == SC_MARK_BACKGROUND) && (markers[markBit].alpha == SC_ALPHA_NOALPHA)) { background = markers[markBit].back; } marks >>= 1; } } if (!background && maskInLine) { int marksMasked = marksOfLine & maskInLine; if (marksMasked) { for (int markBit = 0; (markBit < 32) && marksMasked; markBit++) { if ((marksMasked & 1) && (markers[markBit].alpha == SC_ALPHA_NOALPHA)) { background = markers[markBit].back; } marksMasked >>= 1; } } } return background; } bool ViewStyle::SelectionBackgroundDrawn() const noexcept { return selection.colours.back && ((selection.alpha == SC_ALPHA_NOALPHA) || (selection.additionalAlpha == SC_ALPHA_NOALPHA)); } bool ViewStyle::WhitespaceBackgroundDrawn() const noexcept { return (viewWhitespace != WhiteSpace::invisible) && (whitespaceColours.back); } bool ViewStyle::WhiteSpaceVisible(bool inIndent) const noexcept { return (!inIndent && viewWhitespace == WhiteSpace::visibleAfterIndent) || (inIndent && viewWhitespace == WhiteSpace::visibleOnlyInIndent) || viewWhitespace == WhiteSpace::visibleAlways; } ColourAlpha ViewStyle::WrapColour() const noexcept { return whitespaceColours.fore.value_or(styles[STYLE_DEFAULT].fore); } // Insert new edge in sorted order. void ViewStyle::AddMultiEdge(uptr_t wParam, sptr_t lParam) { const int column = static_cast(wParam); theMultiEdge.insert( std::upper_bound(theMultiEdge.begin(), theMultiEdge.end(), column, [](const EdgeProperties &a, const EdgeProperties &b) { return a.column < b.column; }), EdgeProperties(column, lParam)); } std::optional ViewStyle::ElementColour(int index) const { auto search = elementColours.find(index); if (search != elementColours.end()) { if (search->second.has_value()) { return search->second; } } return {}; } bool ViewStyle::ElementAllowsTranslucent(int index) const { return elementAllowsTranslucent.count(index) > 0; } bool ViewStyle::SetWrapState(int wrapState_) noexcept { WrapMode wrapStateWanted; switch (wrapState_) { case SC_WRAP_WORD: wrapStateWanted = WrapMode::word; break; case SC_WRAP_CHAR: wrapStateWanted = WrapMode::character; break; case SC_WRAP_WHITESPACE: wrapStateWanted = WrapMode::whitespace; break; default: wrapStateWanted = WrapMode::none; break; } const bool changed = wrap.state != wrapStateWanted; wrap.state = wrapStateWanted; return changed; } bool ViewStyle::SetWrapVisualFlags(int wrapVisualFlags_) noexcept { const bool changed = wrap.visualFlags != wrapVisualFlags_; wrap.visualFlags = wrapVisualFlags_; return changed; } bool ViewStyle::SetWrapVisualFlagsLocation(int wrapVisualFlagsLocation_) noexcept { const bool changed = wrap.visualFlagsLocation != wrapVisualFlagsLocation_; wrap.visualFlagsLocation = wrapVisualFlagsLocation_; return changed; } bool ViewStyle::SetWrapVisualStartIndent(int wrapVisualStartIndent_) noexcept { const bool changed = wrap.visualStartIndent != wrapVisualStartIndent_; wrap.visualStartIndent = wrapVisualStartIndent_; return changed; } bool ViewStyle::SetWrapIndentMode(int wrapIndentMode_) noexcept { const bool changed = wrap.indentMode != wrapIndentMode_; wrap.indentMode = wrapIndentMode_; return changed; } bool ViewStyle::IsBlockCaretStyle() const noexcept { return ((caret.style & CARETSTYLE_INS_MASK) == CARETSTYLE_BLOCK) || (caret.style & CARETSTYLE_OVERSTRIKE_BLOCK) != 0; } bool ViewStyle::IsCaretVisible() const noexcept { return caret.width > 0 && caret.style != CARETSTYLE_INVISIBLE; } bool ViewStyle::DrawCaretInsideSelection(bool inOverstrike, bool imeCaretBlockOverride) const noexcept { if (caret.style & CARETSTYLE_BLOCK_AFTER) return false; return ((caret.style & CARETSTYLE_INS_MASK) == CARETSTYLE_BLOCK) || (inOverstrike && (caret.style & CARETSTYLE_OVERSTRIKE_BLOCK) != 0) || imeCaretBlockOverride; } ViewStyle::CaretShape ViewStyle::CaretShapeForMode(bool inOverstrike) const noexcept { if (inOverstrike) { return (caret.style & CARETSTYLE_OVERSTRIKE_BLOCK) ? CaretShape::block : CaretShape::bar; } const int caretStyle = caret.style & CARETSTYLE_INS_MASK; return (caretStyle <= CARETSTYLE_BLOCK) ? static_cast(caretStyle) : CaretShape::line; } void ViewStyle::AllocStyles(size_t sizeNew) { size_t i=styles.size(); styles.resize(sizeNew); if (styles.size() > STYLE_DEFAULT) { for (; i(); } } } FontRealised *ViewStyle::Find(const FontSpecification &fs) { if (!fs.fontName) // Invalid specification so return arbitrary object return fonts.begin()->second.get(); FontMap::iterator it = fonts.find(fs); if (it != fonts.end()) { // Should always reach here since map was just set for all styles return it->second.get(); } return nullptr; } void ViewStyle::FindMaxAscentDescent() { for (const auto &font : fonts) { if (maxAscent < font.second->ascent) maxAscent = font.second->ascent; if (maxDescent < font.second->descent) maxDescent = font.second->descent; } }