2018-06-15 02:42:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ScaledFontFreeType.h"
|
|
|
|
#include "UnscaledFontFreeType.h"
|
|
|
|
#include "NativeFontResourceFreeType.h"
|
|
|
|
#include "Logging.h"
|
2019-10-31 16:39:12 +03:00
|
|
|
#include "mozilla/StaticPrefs_gfx.h"
|
2018-06-15 02:42:56 +03:00
|
|
|
#include "mozilla/webrender/WebRenderTypes.h"
|
|
|
|
|
2021-07-30 02:29:44 +03:00
|
|
|
#include "skia/include/ports/SkTypeface_cairo.h"
|
2018-06-15 02:42:56 +03:00
|
|
|
|
|
|
|
#include FT_MULTIPLE_MASTERS_H
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
ScaledFontFreeType::ScaledFontFreeType(
|
2019-10-02 00:56:30 +03:00
|
|
|
RefPtr<SharedFTFace>&& aFace, const RefPtr<UnscaledFont>& aUnscaledFont,
|
|
|
|
Float aSize, bool aApplySyntheticBold)
|
2019-09-16 20:15:10 +03:00
|
|
|
: ScaledFontBase(aUnscaledFont, aSize),
|
|
|
|
mFace(std::move(aFace)),
|
2019-10-02 00:56:30 +03:00
|
|
|
mApplySyntheticBold(aApplySyntheticBold) {}
|
2018-06-15 02:42:56 +03:00
|
|
|
|
2019-10-02 17:04:45 +03:00
|
|
|
bool ScaledFontFreeType::UseSubpixelPosition() const {
|
2019-10-31 16:39:12 +03:00
|
|
|
return !MOZ_UNLIKELY(
|
|
|
|
StaticPrefs::
|
|
|
|
gfx_text_subpixel_position_force_disabled_AtStartup()) &&
|
|
|
|
FT_IS_SCALABLE(mFace->GetFace());
|
2019-10-02 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 21:58:21 +03:00
|
|
|
SkTypeface* ScaledFontFreeType::CreateSkTypeface() {
|
2019-09-16 20:12:38 +03:00
|
|
|
return SkCreateTypefaceFromCairoFTFont(mFace->GetFace(), mFace.get());
|
2019-09-16 20:03:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScaledFontFreeType::SetupSkFontDrawOptions(SkFont& aFont) {
|
2019-10-02 17:04:45 +03:00
|
|
|
aFont.setSubpixel(UseSubpixelPosition());
|
2019-09-16 20:03:04 +03:00
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
if (mApplySyntheticBold) {
|
|
|
|
aFont.setEmbolden(true);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
aFont.setEmbeddedBitmaps(true);
|
2018-06-15 02:42:56 +03:00
|
|
|
}
|
|
|
|
|
2022-05-27 19:07:15 +03:00
|
|
|
bool ScaledFontFreeType::MayUseBitmaps() {
|
|
|
|
return !FT_IS_SCALABLE(mFace->GetFace());
|
|
|
|
}
|
|
|
|
|
2019-10-02 00:56:30 +03:00
|
|
|
cairo_font_face_t* ScaledFontFreeType::CreateCairoFontFace(
|
|
|
|
cairo_font_options_t* aFontOptions) {
|
|
|
|
cairo_font_options_set_hint_metrics(aFontOptions, CAIRO_HINT_METRICS_OFF);
|
|
|
|
|
|
|
|
int loadFlags = FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING;
|
|
|
|
if (mFace->GetFace()->face_flags & FT_FACE_FLAG_TRICKY) {
|
|
|
|
loadFlags &= ~FT_LOAD_NO_AUTOHINT;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int synthFlags = 0;
|
|
|
|
if (mApplySyntheticBold) {
|
|
|
|
synthFlags |= CAIRO_FT_SYNTHESIZE_BOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cairo_ft_font_face_create_for_ft_face(mFace->GetFace(), loadFlags,
|
|
|
|
synthFlags, mFace.get());
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:42:56 +03:00
|
|
|
bool ScaledFontFreeType::GetFontInstanceData(FontInstanceDataOutput aCb,
|
|
|
|
void* aBaton) {
|
|
|
|
std::vector<FontVariation> variations;
|
|
|
|
if (HasVariationSettings()) {
|
2019-09-16 20:03:04 +03:00
|
|
|
UnscaledFontFreeType::GetVariationSettingsFromFace(&variations,
|
|
|
|
mFace->GetFace());
|
2018-06-15 02:42:56 +03:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
InstanceData instance(this);
|
|
|
|
aCb(reinterpret_cast<uint8_t*>(&instance), sizeof(instance),
|
|
|
|
variations.data(), variations.size(), aBaton);
|
2018-06-15 02:42:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScaledFontFreeType::GetWRFontInstanceOptions(
|
|
|
|
Maybe<wr::FontInstanceOptions>* aOutOptions,
|
|
|
|
Maybe<wr::FontInstancePlatformOptions>* aOutPlatformOptions,
|
|
|
|
std::vector<FontVariation>* aOutVariations) {
|
|
|
|
wr::FontInstanceOptions options;
|
|
|
|
options.render_mode = wr::FontRenderMode::Alpha;
|
2019-03-07 02:01:11 +03:00
|
|
|
options.flags = wr::FontInstanceFlags{0};
|
2019-10-02 17:04:45 +03:00
|
|
|
if (UseSubpixelPosition()) {
|
2019-12-09 00:09:37 +03:00
|
|
|
options.flags |= wr::FontInstanceFlags::SUBPIXEL_POSITION;
|
2019-10-02 17:04:45 +03:00
|
|
|
}
|
2019-12-09 00:09:37 +03:00
|
|
|
options.flags |= wr::FontInstanceFlags::EMBEDDED_BITMAPS;
|
2020-03-09 17:16:17 +03:00
|
|
|
options.bg_color = wr::ToColorU(DeviceColor());
|
2018-07-04 17:56:40 +03:00
|
|
|
options.synthetic_italics =
|
|
|
|
wr::DegreesToSyntheticItalics(GetSyntheticObliqueAngle());
|
2018-06-15 02:42:56 +03:00
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
if (mApplySyntheticBold) {
|
2019-12-09 00:09:37 +03:00
|
|
|
options.flags |= wr::FontInstanceFlags::SYNTHETIC_BOLD;
|
2019-09-16 20:15:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 02:42:56 +03:00
|
|
|
wr::FontInstancePlatformOptions platformOptions;
|
|
|
|
platformOptions.lcd_filter = wr::FontLCDFilter::None;
|
|
|
|
platformOptions.hinting = wr::FontHinting::None;
|
|
|
|
|
|
|
|
*aOutOptions = Some(options);
|
|
|
|
*aOutPlatformOptions = Some(platformOptions);
|
|
|
|
|
|
|
|
if (HasVariationSettings()) {
|
2019-09-16 20:03:04 +03:00
|
|
|
UnscaledFontFreeType::GetVariationSettingsFromFace(aOutVariations,
|
|
|
|
mFace->GetFace());
|
2018-06-15 02:42:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
ScaledFontFreeType::InstanceData::InstanceData(
|
|
|
|
const wr::FontInstanceOptions* aOptions,
|
|
|
|
const wr::FontInstancePlatformOptions* aPlatformOptions)
|
|
|
|
: mApplySyntheticBold(false) {
|
|
|
|
if (aOptions) {
|
2019-12-09 00:09:37 +03:00
|
|
|
if (aOptions->flags & wr::FontInstanceFlags::SYNTHETIC_BOLD) {
|
2019-09-16 20:15:10 +03:00
|
|
|
mApplySyntheticBold = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:42:56 +03:00
|
|
|
already_AddRefed<ScaledFont> UnscaledFontFreeType::CreateScaledFont(
|
|
|
|
Float aGlyphSize, const uint8_t* aInstanceData,
|
|
|
|
uint32_t aInstanceDataLength, const FontVariation* aVariations,
|
|
|
|
uint32_t aNumVariations) {
|
2019-09-16 20:15:10 +03:00
|
|
|
if (aInstanceDataLength < sizeof(ScaledFontFreeType::InstanceData)) {
|
|
|
|
gfxWarning() << "FreeType scaled font instance data is truncated.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const ScaledFontFreeType::InstanceData& instanceData =
|
|
|
|
*reinterpret_cast<const ScaledFontFreeType::InstanceData*>(aInstanceData);
|
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
RefPtr<SharedFTFace> face(InitFace());
|
2018-06-15 02:42:56 +03:00
|
|
|
if (!face) {
|
|
|
|
gfxWarning() << "Attempted to deserialize FreeType scaled font without "
|
|
|
|
"FreeType face";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
if (aNumVariations > 0 && face->GetData()) {
|
|
|
|
if (RefPtr<SharedFTFace> varFace = face->GetData()->CloneFace()) {
|
2018-06-15 02:42:56 +03:00
|
|
|
face = varFace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
// Only apply variations if we have an explicitly cloned face.
|
|
|
|
if (aNumVariations > 0 && face != GetFace()) {
|
|
|
|
ApplyVariationsToFace(aVariations, aNumVariations, face->GetFace());
|
|
|
|
}
|
2018-06-15 02:42:56 +03:00
|
|
|
|
2019-10-02 00:56:30 +03:00
|
|
|
RefPtr<ScaledFontFreeType> scaledFont = new ScaledFontFreeType(
|
|
|
|
std::move(face), this, aGlyphSize, instanceData.mApplySyntheticBold);
|
2018-06-15 02:42:56 +03:00
|
|
|
|
2019-09-16 10:44:20 +03:00
|
|
|
return scaledFont.forget();
|
2019-09-15 06:01:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
already_AddRefed<ScaledFont> UnscaledFontFreeType::CreateScaledFontFromWRFont(
|
|
|
|
Float aGlyphSize, const wr::FontInstanceOptions* aOptions,
|
|
|
|
const wr::FontInstancePlatformOptions* aPlatformOptions,
|
|
|
|
const FontVariation* aVariations, uint32_t aNumVariations) {
|
|
|
|
ScaledFontFreeType::InstanceData instanceData(aOptions, aPlatformOptions);
|
|
|
|
return CreateScaledFont(aGlyphSize, reinterpret_cast<uint8_t*>(&instanceData),
|
|
|
|
sizeof(instanceData), aVariations, aNumVariations);
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:42:56 +03:00
|
|
|
bool ScaledFontFreeType::HasVariationSettings() {
|
|
|
|
// Check if the FT face has been cloned.
|
2019-09-16 20:03:04 +03:00
|
|
|
return mFace &&
|
|
|
|
mFace->GetFace()->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS &&
|
2018-06-15 02:42:56 +03:00
|
|
|
mFace !=
|
|
|
|
static_cast<UnscaledFontFreeType*>(mUnscaledFont.get())->GetFace();
|
|
|
|
}
|
|
|
|
|
2020-03-26 19:11:00 +03:00
|
|
|
already_AddRefed<UnscaledFont> UnscaledFontFreeType::CreateFromFontDescriptor(
|
|
|
|
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) {
|
|
|
|
if (aDataLength == 0) {
|
|
|
|
gfxWarning() << "FreeType font descriptor is truncated.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const char* path = reinterpret_cast<const char*>(aData);
|
|
|
|
RefPtr<UnscaledFont> unscaledFont =
|
|
|
|
new UnscaledFontFreeType(std::string(path, aDataLength), aIndex);
|
|
|
|
return unscaledFont.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:42:56 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|