Commit 5263feef authored by Wolthera van Hövell's avatar Wolthera van Hövell 🔤 Committed by Wolthera van Hövell
Browse files

Improve font style selection, enabling the proper styles to be selected.

Apparantly Qt will synthesize the bold/italic if you don't set it on creation.
This patch also tries to fix the handling of stretch in the same way, however
it seems that all QFonts return 'anystretch' for the stretch, which makes me
suspect there's a qt bug there. In any case, the changes ensure that at the
least a coherent value is set (0 doesn't mean much).

BUG:425312


(cherry picked from commit 556d6a10)
parent 210f8c85
Pipeline #99053 skipped with stage
......@@ -231,9 +231,15 @@ void KoSvgTextProperties::parseSvgTextAttribute(const SvgLoadingContext &context
newStretch = it != fontStretches.rend() ? *it : fontStretches.front();
} else {
auto it = std::find(fontStretchNames.begin(), fontStretchNames.end(), value);
if (it != fontStretchNames.end()) {
newStretch = fontStretches[it - fontStretchNames.begin()];
// try to read numerical stretch value
bool ok = false;
newStretch = value.toInt(&ok, 10);
if (!ok) {
auto it = std::find(fontStretchNames.begin(), fontStretchNames.end(), value);
if (it != fontStretchNames.end()) {
newStretch = fontStretches[it - fontStretchNames.begin()];
}
}
}
......@@ -448,19 +454,20 @@ QMap<QString, QString> KoSvgTextProperties::convertToSvgTextAttributes() const
QFont KoSvgTextProperties::generateFont() const
{
QFont font;
QString fontFamily;
QStringList familiesList =
propertyOrDefault(KoSvgTextProperties::FontFamiliesId).toStringList();
if (!familiesList.isEmpty()) {
font.setFamily(familiesList.first());
fontFamily = familiesList.first();
}
font.setPointSizeF(propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal());
const QFont::Style style =
QFont::Style(propertyOrDefault(KoSvgTextProperties::FontStyleId).toInt());
QFont font(fontFamily
, propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal()
, propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt()
, style != QFont::StyleNormal);
font.setStyle(style);
font.setCapitalization(
......@@ -469,8 +476,6 @@ QFont KoSvgTextProperties::generateFont() const
font.setStretch(propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt());
font.setWeight(propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt());
using namespace KoSvgText;
TextDecorations deco =
......
......@@ -1039,8 +1039,26 @@ QString KoSvgTextShapeMarkupConverter::style(QTextCharFormat format,
}
if (propertyId == QTextCharFormat::FontStretch) {
QString valueString = QString::number(format.fontStretch(), 10);
if (format.fontStretch() == QFont::ExtraCondensed) {
valueString = "extra-condensed";
} else if (format.fontStretch() == QFont::SemiCondensed) {
valueString = "semi-condensed";
} else if (format.fontStretch() == QFont::Condensed) {
valueString = "condensed";
} else if (format.fontStretch() == QFont::AnyStretch) {
valueString = "normal";
} else if (format.fontStretch() == QFont::Expanded) {
valueString = "expanded";
} else if (format.fontStretch() == QFont::SemiExpanded) {
valueString = "semi-expanded";
} else if (format.fontStretch() == QFont::ExtraExpanded) {
valueString = "extra-expanded";
} else if (format.fontStretch() == QFont::UltraExpanded) {
valueString = "ultra-expanded";
}
c.append("font-stretch").append(":")
.append(format.properties()[propertyId].toString());
.append(valueString);
}
if (propertyId == QTextCharFormat::FontKerning) {
QString val;
......@@ -1188,7 +1206,25 @@ QVector<QTextFormat> KoSvgTextShapeMarkupConverter::stylesFromString(QStringList
}
if (property == "font-stretch") {
charFormat.setFontStretch(value.toInt());
if (value == "ultra-condensed") {
charFormat.setFontStretch(QFont::UltraCondensed);
} else if (value == "condensed") {
charFormat.setFontStretch(QFont::Condensed);
} else if (value == "semi-condensed") {
charFormat.setFontStretch(QFont::SemiCondensed);
} else if (value == "normal") {
charFormat.setFontStretch(100);
} else if (value == "semi-expanded") {
charFormat.setFontStretch(QFont::SemiExpanded);
} else if (value == "expanded") {
charFormat.setFontStretch(QFont::Expanded);
} else if (value == "extra-expanded") {
charFormat.setFontStretch(QFont::ExtraExpanded);
} else if (value == "ultra-expanded") {
charFormat.setFontStretch(QFont::UltraExpanded);
} else { // "normal"
charFormat.setFontStretch(value.toInt());
}
}
if (property == "font-weight") {
......
......@@ -1130,9 +1130,7 @@ void SvgTextEditor::setFont(const QString &fontName)
const int fontWeight = fontDatabase.weight(font.family(), font.styleName());
if (isRichTextEditorTabActive()) {
QTextCharFormat format;
format.setFontFamily(font.family());
format.setFontItalic(italic);
format.setFontWeight(fontWeight);
format.setFont(font);
QTextCursor oldCursor = setTextSelection();
m_textEditorWidget.richTextEdit->mergeCurrentCharFormat(format);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment