Apply font variations when drawing text with Skia

Set variations on the Skia font since now we allow the user to set font
variations, and they can be different from the variations the font was
created with. Previously we only supported named instances and these got
their variations set by the platform font API.

Change-Id: I2cda0409273a8ae00f40dacb1deb166af65ebbe5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200673
Reviewed-by: Khaled Hosny <khaled@libreoffice.org>
Tested-by: Jenkins
This commit is contained in:
Khaled Hosny
2026-03-02 21:43:58 +01:00
committed by Khaled Hosny
parent c992dc2c53
commit ece6f72de3
6 changed files with 37 additions and 3 deletions
+1
View File
@@ -37,6 +37,7 @@ $(eval $(call gb_CppunitTest_use_libraries,vcl_skia, \
$(eval $(call gb_CppunitTest_use_externals,vcl_skia, \
boost_headers \
$(if $(filter SKIA,$(BUILD_TYPE)),skia) \
harfbuzz \
))
$(eval $(call gb_CppunitTest_use_sdk_api,vcl_skia))
+5
View File
@@ -27,12 +27,14 @@
#include <driverblocklist.hxx>
#include <vcl/bitmap.hxx>
#include <vcl/salgtype.hxx>
#include <font/LogicalFontInstance.hxx>
#include <test/GraphicsRenderTests.hxx>
#include <premac.h>
#include <SkRegion.h>
#include <SkSurface.h>
#include <SkTypeface.h>
#if defined __GNUC__ && !defined __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
@@ -138,6 +140,9 @@ VCL_DLLPUBLIC const SkSurfaceProps* surfaceProps();
// Set pixel geometry to be used by SkSurfaceProps.
VCL_DLLPUBLIC void setPixelGeometry(SkPixelGeometry pixelGeometry);
VCL_DLLPUBLIC sk_sp<SkTypeface> applyVariations(const sk_sp<SkTypeface>& skTypeface,
const LogicalFontInstance& font);
inline bool isUnitTestRunning(const char* name = nullptr)
{
if (name == nullptr)
+25
View File
@@ -57,6 +57,8 @@ bool isAlphaMaskBlendingEnabled() { return false; }
#include <SkRuntimeEffect.h>
#include <SkStream.h>
#include <SkTileMode.h>
#include <SkTypeface.h>
#include <SkFontArguments.h>
#include <skia_compiler.hxx>
#include <skia_opts.hxx>
#if defined(MACOSX)
@@ -909,6 +911,29 @@ void dump(const sk_sp<SkImage>& image, const char* file)
ostream.write(static_cast<const char*>(data->data()), data->size());
}
sk_sp<SkTypeface> applyVariations(const sk_sp<SkTypeface>& skTypeface,
const LogicalFontInstance& font)
{
const auto& variations = font.GetVariations();
if (variations.empty() || !skTypeface)
return skTypeface;
std::vector<SkFontArguments::VariationPosition::Coordinate> skCoords;
skCoords.reserve(variations.size());
for (const auto& var : variations)
skCoords.push_back({ var.tag, var.value });
SkFontArguments fontArgs;
SkFontArguments::VariationPosition varPosition
= { skCoords.data(), static_cast<int>(skCoords.size()) };
fontArgs.setVariationDesignPosition(varPosition);
sk_sp<SkTypeface> variationFace = skTypeface->makeClone(fontArgs);
if (variationFace)
return variationFace;
return skTypeface;
}
#ifdef DBG_UTIL
void prefillSurface(const sk_sp<SkSurface>& surface)
{
+2 -1
View File
@@ -381,7 +381,8 @@ void AquaSkiaSalGraphicsImpl::drawTextLayout(const GenericSalLayout& rLayout)
}
}
sk_sp<SkTypeface> typeface = SkMakeTypefaceFromCTFont(rFont.GetCTFont());
sk_sp<SkTypeface> typeface
= SkiaHelper::applyVariations(SkMakeTypefaceFromCTFont(rFont.GetCTFont()), rFont);
SkFont font(typeface);
font.setSize(nHeight);
// font.setScaleX(rFont.mfFontStretch); TODO
+1
View File
@@ -268,6 +268,7 @@ bool WinSkiaSalGraphicsImpl::DrawTextLayout(const GenericSalLayout& rLayout)
if (!typeface)
return false;
}
typeface = SkiaHelper::applyVariations(typeface, rWinFont);
// Cache the typeface.
rWinFont.SetSkiaTypeface(typeface, dwrite);
}
+3 -2
View File
@@ -64,8 +64,9 @@ void SkiaTextRender::DrawTextLayout(const GenericSalLayout& rLayout, const SalGr
fontManager
= SkFontMgr_New_FontConfig(FcConfigReference(nullptr), SkFontScanner_Make_FreeType());
}
sk_sp<SkTypeface> typeface
= SkFontMgr_createTypefaceFromFcPattern(fontManager, rFont.GetFontOptions()->GetPattern());
sk_sp<SkTypeface> typeface = SkiaHelper::applyVariations(
SkFontMgr_createTypefaceFromFcPattern(fontManager, rFont.GetFontOptions()->GetPattern()),
rInstance);
SkFont font(typeface);
font.setSize(nHeight);
font.setScaleX(nWidth / nHeight);