| 139 | } |
| 140 | |
| 141 | void collectRanges(QTextFrame* frame, const QColor& fgcolor, const QColor& bgcolor, bool bgSet, |
| 142 | std::vector<FormatRange>& ranges) |
| 143 | { |
| 144 | for (auto it = frame->begin(); it != frame->end(); ++it) { |
| 145 | if (auto frame = it.currentFrame()) { |
| 146 | auto fmt = it.currentFrame()->frameFormat(); |
| 147 | if (auto background = backgroundColor(fmt)) { |
| 148 | collectRanges(frame, fgcolor, *background, true, ranges); |
| 149 | } else { |
| 150 | collectRanges(frame, fgcolor, bgcolor, bgSet, ranges); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | const auto block = it.currentBlock(); |
| 155 | if (!block.isValid()) { |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | for (auto jt = block.begin(); jt != block.end(); ++jt) { |
| 160 | auto fragment = jt.fragment(); |
| 161 | if (!QStringView{fragment.text()}.trimmed().empty()) { |
| 162 | auto fmt = fragment.charFormat(); |
| 163 | auto foreground = foregroundColor(fmt); |
| 164 | auto background = backgroundColor(fmt); |
| 165 | |
| 166 | if (!bgSet && !background) { |
| 167 | if (!foreground || foreground == Qt::black) { |
| 168 | fmt.setForeground(fgcolor); |
| 169 | } else if (canInvertDarkColor(*foreground)) { |
| 170 | fmt.setForeground(invertColor(*foreground)); |
| 171 | } else if (canBlendForegroundColor(*foreground)) { |
| 172 | fmt.setForeground(WidgetColorizer::blendForeground(*foreground, 1.0, fgcolor, bgcolor)); |
| 173 | } |
| 174 | } else { |
| 175 | auto bg = background.value_or(bgcolor); |
| 176 | auto fg = foreground.value_or(fgcolor); |
| 177 | if (background && canInvertBrightColor(bg)) { |
| 178 | bg = invertColor(bg); |
| 179 | fmt.setBackground(bg); |
| 180 | if (canInvertDarkColor(fg)) { |
| 181 | fmt.setForeground(invertColor(fg)); |
| 182 | } else if (canBlendForegroundColor(fg)) { |
| 183 | fmt.setForeground(WidgetColorizer::blendForeground(fg, 1.0, fgcolor, bg)); |
| 184 | } |
| 185 | } else if (isBrightBackgroundColor(bg) && canInvertBrightColor(fg)) { |
| 186 | fg = invertColor(fg); |
| 187 | fmt.setForeground(fg); |
| 188 | } |
| 189 | } |
| 190 | ranges.push_back({fragment.position(), fragment.position() + fragment.length(), fmt}); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | // see also: https://invent.kde.org/kdevelop/kdevelop/-/merge_requests/370#note_487717 |
no test coverage detected