Krita Source Code Documentation
Loading...
Searching...
No Matches
KoCssTextUtils Class Reference

The KoCssTextUtils class. More...

#include <KoCssTextUtils.h>

Static Public Member Functions

static bool characterCanHang (QChar c, KoSvgText::HangingPunctuations hangType)
 characterCanHang The function returns whether the character qualifies for 'hanging-punctuation', using the given hang-type.
 
static bool collapseLastSpace (QChar c, KoSvgText::TextSpaceCollapse collapseMethod)
 collapseLastSpace Some versions of CSS-Text 'white-space' or 'text-space-collapse' will collapse the last spaces while others don't. This function returns whether that's the case.
 
static QVector< bool > collapseSpaces (QString *text, QMap< int, KoSvgText::TextSpaceCollapse > collapseMethods)
 collapseSpaces Some versions of CSS-Text 'white-space' or 'text-space-collapse' will collapse or transform white space characters while others don't. This function returns whether that's the case.
 
static qreal cssSelectFontStyleValue (const QVector< qreal > &values, const qreal targetValue, const qreal defaultValue, const qreal defaultValueUpper, const bool shouldNotReturnDefault)
 cssSelectFontStyleValue Select the closest font style value from the list, following the CSS Fonts selection algorithm. Note that for slant, this needs to be inverted.
 
static QString getBidiClosing (KoSvgText::UnicodeBidi bidi)
 getBidiClosing Returns the bidi closing string associated with the given Css unicode-bidi value.
 
static QString getBidiOpening (bool ltr, KoSvgText::UnicodeBidi bidi)
 getBidiOpening Get the bidi opening string associated with the given Css unicode-bidi value and direction https://www.w3.org/TR/css-writing-modes-3/#unicode-bidi
 
static bool hangLastSpace (const QChar c, KoSvgText::TextSpaceCollapse collapseMethod, KoSvgText::TextWrap wrapMethod, bool &force, bool nextCharIsHardBreak)
 hangLastSpace Some versions of CSS-Text 'white-space' or 'text-space-collapse' will hang the final space depending on the situation.
 
static bool IsCssWordSeparator (QString grapheme)
 IsCssWordSeparator CSS has a number of characters it considers word-separators, which are used in justification and for word-spacing.
 
static QVector< QPair< bool, bool > > justificationOpportunities (QString text, QString langCode)
 justificationOpportunities mark justification opportunities in the text. Opportunities are between characters, so this returns a pair of before and after. As of currently, this only implements the bare minimum for CSS-Text-3 auto justification.
 
static void removeText (QString &text, int &start, int length)
 removeText Special removal of text that takes a text, start and length and will modify these values so that...
 
static QStringList textToUnicodeGraphemeClusters (const QString &text, const QString &langCode)
 textToUnicodeGraphemes In letters like Å, the amount of unicode codpoints can be 1, but it can also be 2, one for 'A', and one for 'Combining Mark Ring Above". In some letters used by Vietnamese, such as ỗ there can be even 3. Such codepoint sequences are considered 'grapheme-clusters'. For editing text, matching fonts or capitalizing the first letter, it's wisest to do so on the grapheme clusters instead of the individual codepoints.
 
static QString transformTextCapitalize (const QString &text, QString langCode, QVector< QPair< int, int > > &positions)
 transformTextToUpperCase This function splits the text into graphemes, and then uses QLocale::toUpper for each letter following a whitespace character or CSS Wordseparator. It has a small codepath for transforming the Dutch IJ correctly, as this is more readable. Note: When building Qt without ICU, this uses platform dependant functions.
 
static QString transformTextFullSizeKana (const QString &text)
 transformTextFullSizeKana This function will take 'small' Kana (Japanese phonetic script) and transform it to their 'full-size' equivalents, following the list in the CSS-Text-3 spec.
 
static QString transformTextFullWidth (const QString &text)
 transformTextFullWidth This function will transform 'narrow' or 'halfwidth' characters to their normal counterparts, and will transform ascii characters to their 'fullwidth'/'ideographic' counterparts.
 
static QString transformTextToLowerCase (const QString &text, const QString &langCode, QVector< QPair< int, int > > &positions)
 transformTextToUpperCase convenience function that creates a QLocale and uses it's 'toLower' function. Note: When building Qt without ICU, this uses platform dependant functions.
 
static QString transformTextToUpperCase (const QString &text, const QString &langCode, QVector< QPair< int, int > > &positions)
 transformTextToUpperCase convenience function that creates a QLocale and uses it's 'toUpper' function. Note: When building Qt without ICU, this uses platform dependant functions.
 

Detailed Description

The KoCssTextUtils class.

This class keeps a number of utility functions related to CSS Text, in particular CSS-Text-3 and CSS-Text-4.

Definition at line 21 of file KoCssTextUtils.h.

Member Function Documentation

◆ characterCanHang()

bool KoCssTextUtils::characterCanHang ( QChar c,
KoSvgText::HangingPunctuations hangType )
static

characterCanHang The function returns whether the character qualifies for 'hanging-punctuation', using the given hang-type.

Parameters
cthe character to check.
hangTypehow to hang.
Returns
whether the character can hang.

Definition at line 329 of file KoCssTextUtils.cpp.

330{
331 if (hangType.testFlag(KoSvgText::HangFirst)) {
332 if (c.category() == QChar::Punctuation_InitialQuote || // Pi
333 c.category() == QChar::Punctuation_Open || // Ps
334 c.category() == QChar::Punctuation_FinalQuote || // Pf
335 c == "\u0027" || // Apostrophe
336 c == "\uFF07" || // Fullwidth Apostrophe
337 c == "\u0022" || // Quotation Mark
338 c == "\uFF02") { // Fullwidth Quotation Mark
339 return true;
340 }
341 }
342 if (hangType.testFlag(KoSvgText::HangLast)) {
343 if (c.category() == QChar::Punctuation_InitialQuote || // Pi
344 c.category() == QChar::Punctuation_FinalQuote || // Pf
345 c.category() == QChar::Punctuation_Close || // Pe
346 c == "\u0027" || // Apostrophe
347 c == "\uFF07" || // Fullwidth Apostrophe
348 c == "\u0022" || // Quotation Mark
349 c == "\uFF02") { // Fullwidth Quotation Mark
350 return true;
351 }
352 }
353 if (hangType.testFlag(KoSvgText::HangEnd)) {
354 if (c == "\u002c" || // Comma
355 c == "\u002e" || // Full Stop
356 c == "\u060c" || // Arabic Comma
357 c == "\u06d4" || // Arabic Full Stop
358 c == "\u3001" || // Ideographic Comma
359 c == "\u3002" || // Ideographic Full Stop
360 c == "\uff0c" || // Fullwidth Comma
361 c == "\uff0e" || // Fullwidth full stop
362 c == "\ufe50" || // Small comma
363 c == "\ufe51" || // Small ideographic comma
364 c == "\ufe52" || // Small full stop
365 c == "\uff61" || // Halfwidth ideographic full stop
366 c == "\uff64" // halfwidth ideographic comma
367 ) {
368 return true;
369 }
370 }
371 return false;
372}
@ HangLast
Hang closing brackets and quotes.
Definition KoSvgText.h:207
@ HangFirst
Hang opening brackets and quotes.
Definition KoSvgText.h:206
@ HangEnd
Hang stops and commas. Force/Allow is a separate boolean.
Definition KoSvgText.h:208

References KoSvgText::HangEnd, KoSvgText::HangFirst, and KoSvgText::HangLast.

◆ collapseLastSpace()

bool KoCssTextUtils::collapseLastSpace ( QChar c,
KoSvgText::TextSpaceCollapse collapseMethod )
static

collapseLastSpace Some versions of CSS-Text 'white-space' or 'text-space-collapse' will collapse the last spaces while others don't. This function returns whether that's the case.

Parameters
cthe character to check.
collapseMethodthe text-space collapse type.
Returns
whether the character should collapse if it's the last space in a line.

Definition at line 272 of file KoCssTextUtils.cpp.

273{
274 bool collapse = false;
275 if (c == QChar::LineFeed) {
276 collapse = true;
277 } else if (c.isSpace()) {
278 switch (collapseMethod) {
281 collapse = true;
282 break;
285 collapse = false;
286 break;
288 collapse = true;
289 break;
291 collapse = false;
292 break;
293 }
294 }
295 return collapse;
296}
@ Preserve
Do not collapse any space.
Definition KoSvgText.h:99
@ BreakSpaces
Same as preserve, except each white space and wordseperate is breakable.
Definition KoSvgText.h:104
@ Collapse
Collapse white space sequences into a single character.
Definition KoSvgText.h:97
@ PreserveBreaks
Definition KoSvgText.h:100
@ Discard
Discard all Spaces.
Definition KoSvgText.h:98
@ PreserveSpaces
required for 'xml:space="preserve"' emulation.
Definition KoSvgText.h:102

References KoSvgText::BreakSpaces, KoSvgText::Collapse, KoSvgText::Discard, KoSvgText::Preserve, KoSvgText::PreserveBreaks, and KoSvgText::PreserveSpaces.

◆ collapseSpaces()

QVector< bool > KoCssTextUtils::collapseSpaces ( QString * text,
QMap< int, KoSvgText::TextSpaceCollapse > collapseMethods )
static

collapseSpaces Some versions of CSS-Text 'white-space' or 'text-space-collapse' will collapse or transform white space characters while others don't. This function returns whether that's the case.

Parameters
textthe text to check against, this text will be transformed if the collapse method requires that.
collapseMethodthe white-space/text-space-collapse method, with the key indicating the position in the string at which it starts.
Returns
A vector of booleans the size of the input text that marks whether the character should be collapsed.

Definition at line 199 of file KoCssTextUtils.cpp.

200{
201
202 QString modifiedText = *text;
203 QVector<bool> collapseList(modifiedText.size());
204 collapseList.fill(false);
205 int spaceSequenceCount = 0;
206 KoSvgText::TextSpaceCollapse collapseMethod = collapseMethods.first();
207 for (int i = 0; i < modifiedText.size(); i++) {
208 bool firstOrLast = (i == 0 || i == modifiedText.size() - 1);
209 bool collapse = false;
210 if (collapseMethods.contains(i)) {
211 // If a whitespace:collapse sequence is inside a pre-wrap sequence, the first space needs to be left alone.
212 if (collapseMethods.value(i) != collapseMethod) spaceSequenceCount = 0;
213 collapseMethod = collapseMethods.value(i);
214 }
215 const QChar c = modifiedText.at(i);
216 if (c == QChar::LineFeed || c == QChar::Tabulation) {
217 if (collapseMethod == KoSvgText::Collapse ||
218 collapseMethod == KoSvgText::PreserveSpaces) {
219 modifiedText[i] = QChar::Space;
220 spaceSequenceCount += 1;
221 collapseList[i] = spaceSequenceCount > 1 || firstOrLast? true: false;
222 continue;
223 }
224 }
225 if (c.isSpace()) {
226 bool isSegmentBreak = c == QChar::LineFeed;
227 bool isTab = c == QChar::Tabulation;
228 spaceSequenceCount += 1;
229 if (spaceSequenceCount > 1 || firstOrLast) {
230 switch (collapseMethod) {
233 collapse = true;
234 break;
238 collapse = false;
239 break;
241 collapse = !isSegmentBreak;
242 if (isTab) {
243 modifiedText[i] = QChar::Space;
244 }
245 break;
246 }
247 }
248 } else {
249 spaceSequenceCount = 0;
250 }
251 collapseList[i] = collapse;
252 }
253 // go backward to ensure any dangling space characters are marked as collapsed.
254 for (int i = modifiedText.size()-1; i>=0; i--) {
255 if (collapseMethods.contains(i)) {
256 int pos = collapseMethods.keys().indexOf(i);
257 collapseMethod = collapseMethods.value(collapseMethods.keys().value(pos-1, 0), KoSvgText::Collapse);
258 if (collapseMethod != KoSvgText::Collapse) break;
259 }
260 if (modifiedText.at(i).isSpace()) {
261 if (collapseMethod == KoSvgText::Collapse) {
262 collapseList[i] = true;
263 }
264 } else {
265 break;
266 }
267 }
268 *text = modifiedText;
269 return collapseList;
270}
TextSpaceCollapse
Definition KoSvgText.h:96

References KoSvgText::BreakSpaces, KoSvgText::Collapse, KoSvgText::Discard, KoSvgText::Preserve, KoSvgText::PreserveBreaks, and KoSvgText::PreserveSpaces.

◆ cssSelectFontStyleValue()

qreal KoCssTextUtils::cssSelectFontStyleValue ( const QVector< qreal > & values,
const qreal targetValue,
const qreal defaultValue,
const qreal defaultValueUpper,
const bool shouldNotReturnDefault )
static

cssSelectFontStyleValue Select the closest font style value from the list, following the CSS Fonts selection algorithm. Note that for slant, this needs to be inverted.

Parameters
values– values list to search in.
targetValue– the target value to search for.
defaultValue– the (lower-end) default value for the given axis.
defaultValueUpper– the upper-end of the default value, for example, for weight this is 100 larger than default, for the others it is exactly the same.
shouldNotReturnDefault– used for the slants, as they need to fall back on one another.
Returns
closest value on this list.

Definition at line 592 of file KoCssTextUtils.cpp.

593{
594 if(values.isEmpty()) {
595 return targetValue;
596 }
597 // follow the CSS Fonts selection mechanism.
598 // See https://drafts.csswg.org/css-fonts-4/#font-style-matching
599 QVector<qreal> sortedValues = values;
600 std::sort(sortedValues.begin(), sortedValues.end());
601 qreal selectedValue = defaultValue;
602 auto upper = std::lower_bound(sortedValues.begin(), sortedValues.end(), targetValue);
603 if (upper == sortedValues.end()) {
604 upper--;
605 }
606 auto lower = upper;
607 if (lower != sortedValues.begin() && *lower > targetValue) {
608 lower--;
609 }
610
611 // ... Which wants to select the lower possible selection when the value is below the default.
612 if (targetValue < defaultValue) {
613 selectedValue = *lower;
614 // ... the higher closest value when the value is higher than the default (upper bound)
615 } else if (targetValue > defaultValueUpper) {
616 selectedValue = *upper;
617 } else {
618 // ... and if the value is between the lower and upper default bounds, first higher (within bounds)
619 // then lower, then higher.
620 if (*upper <= defaultValueUpper && *lower != targetValue) {
621 selectedValue = *upper;
622 } else {
623 selectedValue = *lower;
624 }
625 }
626 if (qFuzzyCompare(selectedValue, defaultValue) && shouldNotReturnDefault) {
627 return targetValue;
628 }
629 return selectedValue;
630}
static bool qFuzzyCompare(half p1, half p2)

References qFuzzyCompare().

◆ getBidiClosing()

QString KoCssTextUtils::getBidiClosing ( KoSvgText::UnicodeBidi bidi)
static

getBidiClosing Returns the bidi closing string associated with the given Css unicode-bidi value.

Parameters
bidi– the unicode-bidi value
Returns
string with bidi closing marks.

Definition at line 488 of file KoCssTextUtils.cpp.

489{
490 using namespace KoSvgText;
491
492 QString result;
493
494 if (bidi == BidiEmbed || bidi == BidiOverride) {
495 result = BIDI_CONTROL_PDF;
496 } else if (bidi == BidiIsolate || bidi == BidiPlainText) {
497 result = BIDI_CONTROL_PDI;
498 } else if (bidi == BidiIsolateOverride) {
500 }
501
502 return result;
503}
const QString BIDI_CONTROL_PDF
const QString UNICODE_BIDI_ISOLATE_OVERRIDE_END
const QString BIDI_CONTROL_PDI

References BIDI_CONTROL_PDF, BIDI_CONTROL_PDI, and UNICODE_BIDI_ISOLATE_OVERRIDE_END.

◆ getBidiOpening()

QString KoCssTextUtils::getBidiOpening ( bool ltr,
KoSvgText::UnicodeBidi bidi )
static

getBidiOpening Get the bidi opening string associated with the given Css unicode-bidi value and direction https://www.w3.org/TR/css-writing-modes-3/#unicode-bidi

Parameters
ltr– whether the direction is left-to-right
bidi– the unicodee-bidi value.
Returns
string with bidi opening marks.

Definition at line 453 of file KoCssTextUtils.cpp.

454{
455 using namespace KoSvgText;
456
457 QString result;
458
459 if (ltr) {
460 if (bidi == BidiEmbed) {
461 result = BIDI_CONTROL_LRE;
462 } else if (bidi == BidiOverride) {
463 result = BIDI_CONTROL_LRO;
464 } else if (bidi == BidiIsolate) {
465 result = BIDI_CONTROL_LRI;
466 } else if (bidi == BidiIsolateOverride) {
468 } else if (bidi == BidiPlainText) {
469 result = BIDI_CONTROL_FSI;
470 }
471 } else {
472 if (bidi == BidiEmbed) {
473 result = BIDI_CONTROL_RLE;
474 } else if (bidi == BidiOverride) {
475 result = BIDI_CONTROL_RLO;
476 } else if (bidi == BidiIsolate) {
477 result = BIDI_CONTROL_RLI;
478 } else if (bidi == BidiIsolateOverride) {
480 } else if (bidi == BidiPlainText) {
481 result = BIDI_CONTROL_FSI;
482 }
483 }
484
485 return result;
486}
const QString BIDI_CONTROL_RLE
const QString BIDI_CONTROL_LRE
const QString UNICODE_BIDI_ISOLATE_OVERRIDE_RL_START
const QString BIDI_CONTROL_RLO
const QString BIDI_CONTROL_LRI
const QString BIDI_CONTROL_LRO
const QString BIDI_CONTROL_FSI
const QString BIDI_CONTROL_RLI
const QString UNICODE_BIDI_ISOLATE_OVERRIDE_LR_START

References BIDI_CONTROL_FSI, BIDI_CONTROL_LRE, BIDI_CONTROL_LRI, BIDI_CONTROL_LRO, BIDI_CONTROL_RLE, BIDI_CONTROL_RLI, BIDI_CONTROL_RLO, UNICODE_BIDI_ISOLATE_OVERRIDE_LR_START, and UNICODE_BIDI_ISOLATE_OVERRIDE_RL_START.

◆ hangLastSpace()

bool KoCssTextUtils::hangLastSpace ( const QChar c,
KoSvgText::TextSpaceCollapse collapseMethod,
KoSvgText::TextWrap wrapMethod,
bool & force,
bool nextCharIsHardBreak )
static

hangLastSpace Some versions of CSS-Text 'white-space' or 'text-space-collapse' will hang the final space depending on the situation.

Parameters
cthe character in question.
collapseMethodthe collapse method
wrapMethodthe wrap method.
forcewhether said hang is a forced hang or not.
nextCharIsHardBreakwhether the next char is a line break.
Returns

Definition at line 298 of file KoCssTextUtils.cpp.

303{
304 if (c.isSpace()) {
305 if (collapseMethod == KoSvgText::Collapse || collapseMethod == KoSvgText::PreserveBreaks) {
306 // [css-text-3] white-space is set to normal, nowrap, or pre-line; or
307 // [css-text-4] white-space-collapse is collapse or preserve-breaks:
308 // hang unconditionally.
309 force = true;
310 return true;
311 } else if (collapseMethod == KoSvgText::Preserve && wrapMethod != KoSvgText::NoWrap) {
312 // [css-text-3] white-space is set to pre-wrap; or
313 // [css-text-4] white-space-collapse is preserve and text-wrap is not nowrap:
314 // hang unconditionally, unless followed by a force line break,
315 // in which case conditionally hang.
316
317 if (nextCharIsHardBreak) {
318 force = false;
319 } else {
320 force = true;
321 }
322 return true;
323 }
324 }
325
326 return false;
327}
@ NoWrap
Do not do any text wrapping.
Definition KoSvgText.h:112

References KoSvgText::Collapse, KoSvgText::NoWrap, KoSvgText::Preserve, and KoSvgText::PreserveBreaks.

◆ IsCssWordSeparator()

bool KoCssTextUtils::IsCssWordSeparator ( QString grapheme)
static

IsCssWordSeparator CSS has a number of characters it considers word-separators, which are used in justification and for word-spacing.

Parameters
graphemea grapheme to check. Using graphemes here, because some of the word-separators are not in the unicode basic plane.
Returns
true if it is a word-separator

Definition at line 374 of file KoCssTextUtils.cpp.

375{
376 return (grapheme == "\u0020" || // Space
377 grapheme == "\u00A0" || // No Break Space
378 grapheme == "\u1361" || // Ethiopic Word Space
379 grapheme == "\u10100" || // Aegean Word Sepator Line
380 grapheme == "\u10101" || // Aegean Word Sepator Dot
381 grapheme == "\u1039F");
382}

◆ justificationOpportunities()

QVector< QPair< bool, bool > > KoCssTextUtils::justificationOpportunities ( QString text,
QString langCode )
static

justificationOpportunities mark justification opportunities in the text. Opportunities are between characters, so this returns a pair of before and after. As of currently, this only implements the bare minimum for CSS-Text-3 auto justification.

Parameters
textthe text to check against.
langCodelanguage, used for the grapheme breaking.
Returns
a list of booleans for whether the current codePoint represents a justificaton opportunity.

Definition at line 424 of file KoCssTextUtils.cpp.

425{
426 QVector<QPair<bool, bool>> opportunities(text.size());
427 opportunities.fill(QPair<bool, bool>(false, false));
428 QStringList graphemes = textToUnicodeGraphemeClusters(text, langCode);
429 for (int i = 0; i < graphemes.size(); i++) {
430 QString grapheme = graphemes.at(i);
431 if (IsCssWordSeparator(grapheme) || blockScript.contains(grapheme.at(0).script())
432 || clusterScript.contains(grapheme.at(0).script())) {
433 opportunities[i] = QPair<bool, bool>(true, true);
434 }
435 }
436 return opportunities;
437}
static QVector< QChar::Script > blockScript
static QVector< QChar::Script > clusterScript
static bool IsCssWordSeparator(QString grapheme)
IsCssWordSeparator CSS has a number of characters it considers word-separators, which are used in jus...
static QStringList textToUnicodeGraphemeClusters(const QString &text, const QString &langCode)
textToUnicodeGraphemes In letters like Å, the amount of unicode codpoints can be 1,...

References blockScript, clusterScript, IsCssWordSeparator(), and textToUnicodeGraphemeClusters().

◆ removeText()

void KoCssTextUtils::removeText ( QString & text,
int & start,
int length )
static

removeText Special removal of text that takes a text, start and length and will modify these values so that...

  • Whole code points are deleted at any time, avoiding no dangling surrogates.
  • Graphemes don't end with Zero-width-joiners, as that can lead to the grapheme merging with the next.
  • Variation selectors are deleted along their base.
  • regional sequences are deleted in pairs.
    Parameters
    texttext to remove text from.
    startthe start index, will be modified.
    lengththe length.

Definition at line 532 of file KoCssTextUtils.cpp.

533{
534 int end = start+length;
535 int j = 0;
536 int v = 0;
537 int lastCharZWJ = 0;
538 int lastVS = 0;
539 int vsClusterStart = 0;
540 int regionalIndicatorCount = 0;
541 bool startFound = false;
542 bool addToEnd = true;
543 Q_FOREACH(const uint i, text.toUcs4()) {
544 v = QChar::requiresSurrogates(i)? 2: 1;
545 int index = (j+v) -1;
546 bool ZWJ = text.at(index) == ZERO_WIDTH_JOINER;
547 if (isVariationSelector(i)) {
548 lastVS += v;
549 } else {
550 lastVS = 0;
551 vsClusterStart = j;
552 }
553 if (index >= start && !startFound) {
554 startFound = true;
555 if (v > 1) {
556 start = j;
557 }
558 if (regionalIndicatorCount > 0 && regionalIndicator(i)) {
559 start -= regionalIndicatorCount;
560 regionalIndicatorCount = 0;
561 }
562 // Always delete any zero-width-joiners as well.
563 if (ZWJ && index > start) {
564 start = -1;
565 }
566 if (lastCharZWJ > 0) {
567 start -= lastCharZWJ;
568 lastCharZWJ = 0;
569 }
570 // remove any clusters too.
571 if (lastVS > 0) {
572 start = vsClusterStart;
573 }
574 }
575
576
577 if (j >= end && addToEnd) {
578 end = j;
579 addToEnd = ZWJ || isVariationSelector(i)
580 || (regionalIndicatorCount < 3 && regionalIndicator(i));
581 if (addToEnd) {
582 end += v;
583 }
584 }
585 j += v;
586 lastCharZWJ = ZWJ? lastCharZWJ + v: 0;
587 regionalIndicatorCount = regionalIndicator(i)? regionalIndicatorCount + v: 0;
588 }
589 text.remove(start, end-start);
590}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
qreal v
bool isVariationSelector(uint val)
const QChar ZERO_WIDTH_JOINER
bool regionalIndicator(uint val)
unsigned int uint

References isVariationSelector(), length(), regionalIndicator(), v, and ZERO_WIDTH_JOINER.

◆ textToUnicodeGraphemeClusters()

QStringList KoCssTextUtils::textToUnicodeGraphemeClusters ( const QString & text,
const QString & langCode )
static

textToUnicodeGraphemes In letters like Å, the amount of unicode codpoints can be 1, but it can also be 2, one for 'A', and one for 'Combining Mark Ring Above". In some letters used by Vietnamese, such as ỗ there can be even 3. Such codepoint sequences are considered 'grapheme-clusters'. For editing text, matching fonts or capitalizing the first letter, it's wisest to do so on the grapheme clusters instead of the individual codepoints.

Parameters
textthe text to break.
langCodethe language code of the text, BCP style.
Returns
a QStringList of the graphemes as separate strings.

Definition at line 384 of file KoCssTextUtils.cpp.

385{
386 QVector<char> graphemeBreaks(text.size());
387 set_graphemebreaks_utf16(text.utf16(), static_cast<size_t>(text.size()), langCode.toUtf8().data(), graphemeBreaks.data());
388 QStringList graphemes;
389 int graphemeLength = 0;
390 int lastGrapheme = 0;
391 for (int i = 0; i < text.size(); i++) {
392 graphemeLength += 1;
393 bool breakGrapheme = lastGrapheme + graphemeLength < text.size() ? graphemeBreaks[i] == GRAPHEMEBREAK_BREAK : false;
394 if (breakGrapheme) {
395 graphemes.append(text.mid(lastGrapheme, graphemeLength));
396 lastGrapheme += graphemeLength;
397 graphemeLength = 0;
398 }
399 }
400 graphemes.append(text.mid(lastGrapheme, text.size() - lastGrapheme));
401 return graphemes;
402}

◆ transformTextCapitalize()

QString KoCssTextUtils::transformTextCapitalize ( const QString & text,
QString langCode,
QVector< QPair< int, int > > & positions )
static

transformTextToUpperCase This function splits the text into graphemes, and then uses QLocale::toUpper for each letter following a whitespace character or CSS Wordseparator. It has a small codepath for transforming the Dutch IJ correctly, as this is more readable. Note: When building Qt without ICU, this uses platform dependant functions.

Parameters
textthe text to transform.
langCodethe language code in BCP format, it gets transformed to qLocale's format.
Returns
the transformed string.

While this is the only case I know of, make no mistake, "IJsbeer" (Polar bear) is much more readable than "Ijsbeer".

Definition at line 56 of file KoCssTextUtils.cpp.

57{
58 if (text.isEmpty()) return text;
59 QLocale locale(langCode);
60
61 QStringList graphemes = textToUnicodeGraphemeClusters(text, langCode);
62 QStringList oldGraphemes = graphemes;
63 bool capitalizeGrapheme = true;
64 for (int i = 0; i < graphemes.size(); i++) {
65 QString grapheme = graphemes.at(i);
66 if (grapheme.isEmpty() || IsCssWordSeparator(grapheme)) {
67 capitalizeGrapheme = true;
68
69 } else if (capitalizeGrapheme) {
70 graphemes[i] = locale.toUpper(grapheme);
71 if (i + 1 < graphemes.size()) {
74 if (locale == QLocale::Dutch && grapheme.toLower().startsWith("i") && graphemes.at(i + 1).toLower().startsWith("j")) {
75 capitalizeGrapheme = true;
76 continue;
77 }
78 }
79 capitalizeGrapheme = false;
80 }
81 }
82
83 positions = positionDifference(oldGraphemes, graphemes);
84 return graphemes.join("");
85}
QVector< QPair< int, int > > positionDifference(QStringList a, QStringList b)

References IsCssWordSeparator(), positionDifference(), and textToUnicodeGraphemeClusters().

◆ transformTextFullSizeKana()

QString KoCssTextUtils::transformTextFullSizeKana ( const QString & text)
static

transformTextFullSizeKana This function will take 'small' Kana (Japanese phonetic script) and transform it to their 'full-size' equivalents, following the list in the CSS-Text-3 spec.

Parameters
textthe text to transform.
Returns
the transformed text.

Definition at line 189 of file KoCssTextUtils.cpp.

190{
191 QString transformedText;
192 Q_FOREACH (const QChar &c, text) {
193 transformedText.append(findSmallKanaToBigKana(c, c));
194 }
195
196 return transformedText;
197}
static QChar findSmallKanaToBigKana(const QChar &value, const QChar &defaultValue)

References findSmallKanaToBigKana().

◆ transformTextFullWidth()

QString KoCssTextUtils::transformTextFullWidth ( const QString & text)
static

transformTextFullWidth This function will transform 'narrow' or 'halfwidth' characters to their normal counterparts, and will transform ascii characters to their 'fullwidth'/'ideographic' counterparts.

Parameters
textthe text to transform.
Returns
the transformed text.

Definition at line 103 of file KoCssTextUtils.cpp.

104{
105 if (text.isEmpty()) return text;
106 QString transformedText;
107 Q_FOREACH (const QChar &c, text) {
108 if (c.decompositionTag() == QChar::Narrow) {
109 transformedText.append(c.decomposition());
110 } else {
111 transformedText.append(findProportionalToFullWidth(c, c));
112 }
113 }
114
115 return transformedText;
116}
static QChar findProportionalToFullWidth(const QChar &value, const QChar &defaultValue)

References findProportionalToFullWidth().

◆ transformTextToLowerCase()

QString KoCssTextUtils::transformTextToLowerCase ( const QString & text,
const QString & langCode,
QVector< QPair< int, int > > & positions )
static

transformTextToUpperCase convenience function that creates a QLocale and uses it's 'toLower' function. Note: When building Qt without ICU, this uses platform dependant functions.

Parameters
textthe text to transform.
langCodethe language code in BCP format, it gets transformed to qLocale's format.
Returns
the transformed string.

Definition at line 47 of file KoCssTextUtils.cpp.

48{
49 if (text.isEmpty()) return text;
50 QLocale locale(langCode.split("-").join("_"));
51 QString transformedText = locale.toLower(text);
52 positions = positionDifference(textToUnicodeGraphemeClusters(text, langCode), textToUnicodeGraphemeClusters(transformedText, langCode));
53 return transformedText;
54}

References positionDifference(), and textToUnicodeGraphemeClusters().

◆ transformTextToUpperCase()

QString KoCssTextUtils::transformTextToUpperCase ( const QString & text,
const QString & langCode,
QVector< QPair< int, int > > & positions )
static

transformTextToUpperCase convenience function that creates a QLocale and uses it's 'toUpper' function. Note: When building Qt without ICU, this uses platform dependant functions.

Parameters
textthe text to transform.
langCodethe language code in BCP format, it gets transformed to qLocale's format.
Returns
the transformed string.

Definition at line 38 of file KoCssTextUtils.cpp.

39{
40 if (text.isEmpty()) return text;
41 QLocale locale(langCode.split("-").join("_"));
42 QString transformedText = locale.toUpper(text);
43 positions = positionDifference(textToUnicodeGraphemeClusters(text, langCode), textToUnicodeGraphemeClusters(transformedText, langCode));
44 return transformedText;
45}

References positionDifference(), and textToUnicodeGraphemeClusters().


The documentation for this class was generated from the following files: