QFont HtmlCssUtils
::decodeCssFontString (const QString
& cssFontStr
) {
//-----------------------------------------------------------------------
// This function creates a QFont from the provided CSS Font specification
// string. This supports most of the QFont attributes settable in the
// Qt 4.8 and Qt 5.3 QFontDialog.
//
// (1) Font Family
// (2) Font Weight (just bold or not)
// (3) Font Style (possibly Italic or Oblique)
// (4) Font Size (in either pixels or points)
// (5) Decorations (possibly Underline or Strikeout)
//
// Not supported (defaulted): Writing System (e.g. Latin).
//
// See the corresponding encode function, above.
// QString encodeCssFont (const QFont&)
//-----------------------------------------------------------------------
const int fieldCnt = fields.count();
for (int inx = 0; inx < fieldCnt; ++inx)
{
const QString field
= fields
[inx
] .
trimmed();
if (field.isEmpty()) continue;
//----------------------------
if (keyAndValue.count() != 2) continue;
//-------------------------------------
const QString key
= keyAndValue
[0] .
trimmed() .
toLower();
const QString val
= keyAndValue
[1] .
trimmed();
const QString valLower
= val .
toLower();
// *************************
// *** (1) Font Family ***
// *************************
if (key .contains ("font-family"))
{
if (!famList.isEmpty())
{
// Use only the first family in a comma separated list
QString fam
= famList
[0] .
trimmed();
// Remove leading and trailing double quotes
static const QChar DBL_QUOT
('"');
if (fam .startsWith (DBL_QUOT))
fam = fam .mid (1) .trimmed();
if (fam .endsWith (DBL_QUOT))
{ fam .chop (1); fam = fam.trimmed(); }
if (!fam.isEmpty())
retFont .setFamily (fam);
}
}
// ********************************
// *** (2) Font Weight (Bold) ***
// ********************************
else if (key .contains ("font-weight"))
{
if (valLower .contains ("bold"))
retFont .setBold (true);
}
// ********************************************
// *** (3) Font Style (Italic or Oblique) ***
// ********************************************
else if (key .contains ("font-style"))
{
if (valLower .contains ("italic"))
retFont .
setStyle (QFont::StyleItalic);
else if (valLower .contains ("oblique"))
retFont .
setStyle (QFont::StyleOblique);
}
// ************************************************
// *** (4) Font Size: either Pixels or Points ***
// ************************************************
else if (key .contains ("font-size"))
{
bool numOk (false);
numPart.chop (2);
const double num = numPart.toDouble (&numOk);
if (numOk && (num > 0.0))
{
if (valLower.endsWith ("px"))
retFont .setPixelSize (int (num));
else if (valLower.endsWith ("pt"))
retFont .setPointSizeF (num);
}
}
// ***********************************************
// *** (5) Decorations: Underline, Strikeout ***
// ***********************************************
else if (key .contains ("text-decoration"))
{
if (valLower.contains ("underline"))
retFont .setUnderline (true);
if (valLower.contains ("line-through"))
retFont .setStrikeOut (true);
}
} // end for (int inx = 0; inx < fieldCnt; ++inx)
return retFont;
}
Bookmarks