Skip to content

Commit

Permalink
feat: add font manager with switching capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Aug 11, 2024
1 parent 095dc8d commit 836a958
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 7 deletions.
25 changes: 23 additions & 2 deletions build/Build.LibSkia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ partial class Build
throw new IOException("BUILD.gn of skia changed, cannot patch files");
}

var sourcesEnd = buildFileSource.IndexOf("defines = []", sourcesStart, StringComparison.Ordinal);
var sourcesEnd = buildFileSource.IndexOf("libs = []", sourcesStart, StringComparison.Ordinal);
if (sourcesEnd == -1)
{
throw new IOException("BUILD.gn of skia changed, cannot patch files");
Expand Down Expand Up @@ -153,13 +153,16 @@ partial class Build
newSources += " sources += [\n";
newSources += " \"include/ports/SkFontMgr_indirect.h\",\n";
newSources += " \"include/ports/SkRemotableFontMgr.h\",\n";
newSources += " \"src/fonts/SkFontMgr_indirect.cpp\",\n";
newSources += " \"src/ports/SkFontMgr_win_dw.cpp\",\n";
newSources += " \"src/ports/SkScalerContext_win_dw.cpp\",\n";
newSources += " \"src/ports/SkScalerContext_win_dw.h\",\n";
newSources += " \"src/ports/SkTypeface_win_dw.cpp\",\n";
newSources += " \"src/ports/SkTypeface_win_dw.h\",\n";
newSources += " ]\n";
newSources += " }\n";
newSources += " if (is_android) {\n";
newSources += " deps += [ \"//third_party/expat\" ]\n";
newSources += " sources += [\n";
newSources += " \"src/ports/SkFontMgr_android.cpp\",\n";
newSources += " \"src/ports/SkFontMgr_android_parser.cpp\",\n";
Expand Down Expand Up @@ -205,8 +208,26 @@ partial class Build
var emptyFactoryIndex = buildFileSource.IndexOf(emptyFactoryFile);
if(emptyFactoryIndex >= 0)
{
var newSources = " sources = [\n";
newSources += " \"../../wrapper/src/SkFontMgr_alphaskia_factory.cpp\",\n";
newSources += " \"../../wrapper/src/SkFontMgr_alphaskia.cpp\",\n";
newSources += " ]\n";
newSources += " defines = []";
newSources += " if (is_win) {\n";
newSources += " defines += [ \"ALPHASKIA_FONTMGR_WINDOWS\" ]\n";
newSources += " }\n";
newSources += " if (is_android) {\n";
newSources += " defines += [ \"ALPHASKIA_FONTMGR_ANDROID\" ]\n";
newSources += " }\n";
newSources += " if (is_mac) {\n";
newSources += " defines += [ \"ALPHASKIA_FONTMGR_MAC\" ]\n";
newSources += " }\n";
newSources += " if (is_ios) {\n";
newSources += " defines += [ \"ALPHASKIA_FONTMGR_IOS\" ]\n";
newSources += " }\n";

buildFileSource = buildFileSource[..emptyFactoryIndex]
+ " sources = [ \"../../wrapper/src/SkFontMgr_alphaskia_factory.cpp\" ] "
+ newSources
+ buildFileSource[(emptyFactoryIndex + emptyFactoryFile.Length)..];
}

Expand Down
33 changes: 33 additions & 0 deletions wrapper/include/SkFontMgr_alphaskia.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "../../externals/skia/include/core/SkFontMgr.h"

class SkFontMgr_AlphaSkia : public SkFontMgr
{
public:
explicit SkFontMgr_AlphaSkia();

void switch_to_operating_system_fonts();
void switch_to_freetype_fonts();

protected:
int onCountFamilies() const override;
void onGetFamilyName(int index, SkString *familyName) const override;
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override;
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override;
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[],
const SkFontStyle &fontStyle) const override;
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle &,
const char *bcp47[], int bcp47Count,
SkUnichar character) const override;
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments &) const override;
sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override;

private:
sk_sp<SkFontMgr> currentFontMgr_;
sk_sp<SkFontMgr> freeTypeFontMgr_;
sk_sp<SkFontMgr> operatingSystemTypeFontMgr_;
};
2 changes: 2 additions & 0 deletions wrapper/include/alphaskia.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
extern "C"
{
AS_API int32_t alphaskia_get_color_type();
AS_API void alphaskia_switch_to_freetype_fonts();
AS_API void alphaskia_switch_to_operating_system_fonts();

typedef AS_API void *alphaskia_data_t;
AS_API alphaskia_data_t alphaskia_data_new_copy(const uint8_t *data, uint64_t length);
Expand Down
108 changes: 108 additions & 0 deletions wrapper/src/SkFontMgr_alphaskia.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "../include/SkFontMgr_alphaskia.h"

#include "../../externals/skia/include/core/SkSpan.h"
#include "../../externals/skia/include/core/SkFontStyle.h"
#include "../../externals/skia/include/core/SkTypeface.h"
#include "../../externals/skia/include/core/SkStream.h"
#include "../../externals/skia/include/ports/SkFontMgr_data.h"

#if defined(ALPHASKIA_FONTMGR_WINDOWS)
#include "../../externals/skia/include/ports/SkTypeface_win.h"
#define CREATE_OPERATING_SYSTEM_FONTMGR SkFontMgr_New_DirectWrite()
#elif defined(ALPHASKIA_FONTMGR_ANDROID)
#include "../../externals/skia/include/ports/SkFontMgr_android.h"
#define CREATE_OPERATING_SYSTEM_FONTMGR SkFontMgr_New_Android(nullptr)
#elif defined(ALPHASKIA_FONTMGR_MAC)
#include "../../externals/skia/include/ports/SkFontMgr_mac_ct.h"
#define CREATE_OPERATING_SYSTEM_FONTMGR SkFontMgr_New_CoreText(nullptr)
#elif defined(ALPHASKIA_FONTMGR_IOS)
#include "../../externals/skia/include/ports/SkFontMgr_mac_ct.h"
#define CREATE_OPERATING_SYSTEM_FONTMGR SkFontMgr_New_CoreText(nullptr)
#else
#error "Unsupported operating system - extend font mgr"
#endif

SkFontMgr_AlphaSkia::SkFontMgr_AlphaSkia()
{
operatingSystemTypeFontMgr_ = CREATE_OPERATING_SYSTEM_FONTMGR;
currentFontMgr_ = operatingSystemTypeFontMgr_;
}

void SkFontMgr_AlphaSkia::switch_to_operating_system_fonts()
{
currentFontMgr_ = operatingSystemTypeFontMgr_;
}

void SkFontMgr_AlphaSkia::switch_to_freetype_fonts()
{
if (!freeTypeFontMgr_)
{
freeTypeFontMgr_ = SkFontMgr_New_Custom_Data(SkSpan<sk_sp<SkData>>(nullptr, 0));
}
currentFontMgr_ = freeTypeFontMgr_;
}

int SkFontMgr_AlphaSkia::onCountFamilies() const
{
return currentFontMgr_->countFamilies();
}

void SkFontMgr_AlphaSkia::onGetFamilyName(int index, SkString *familyName) const
{
currentFontMgr_->getFamilyName(index, familyName);
}
sk_sp<SkFontStyleSet> SkFontMgr_AlphaSkia::onCreateStyleSet(int index) const
{
return currentFontMgr_->createStyleSet(index);
}

sk_sp<SkFontStyleSet> SkFontMgr_AlphaSkia::onMatchFamily(const char familyName[]) const
{
return currentFontMgr_->matchFamily(familyName);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMatchFamilyStyle(const char familyName[],
const SkFontStyle &fontStyle) const
{
return currentFontMgr_->matchFamilyStyle(familyName, fontStyle);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle &style,
const char *bcp47[], int bcp47Count,
SkUnichar character) const
{
return currentFontMgr_->matchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMakeFromData(sk_sp<SkData> data, int ttcIndex) const
{
return currentFontMgr_->makeFromData(data, ttcIndex);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, int ttcIndex) const
{
if (stream == nullptr)
{
return nullptr;
}
return currentFontMgr_->makeFromStream(std::move(stream), ttcIndex);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, const SkFontArguments &args) const
{
if (stream == nullptr)
{
return nullptr;
}
return currentFontMgr_->makeFromStream(std::move(stream), args);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onMakeFromFile(const char path[], int ttcIndex) const
{
return currentFontMgr_->makeFromFile(path, ttcIndex);
}

sk_sp<SkTypeface> SkFontMgr_AlphaSkia::onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const
{
return currentFontMgr_->legacyMakeTypeface(familyName, style);
}
9 changes: 4 additions & 5 deletions wrapper/src/SkFontMgr_alphaskia_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../../externals/skia/include/core/SkFontMgr.h"
#include "../../externals/skia/include/ports/SkTypeface_win.h"
#include "../include/SkFontMgr_alphaskia.h"

sk_sp<SkFontMgr> SkFontMgr::Factory() {
// TODO: functions for dynamic changing
return SkFontMgr_New_DirectWrite();
sk_sp<SkFontMgr> SkFontMgr::Factory()
{
return sk_make_sp<SkFontMgr_AlphaSkia>();
}
23 changes: 23 additions & 0 deletions wrapper/src/alphaskia_canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "../include/AlphaSkiaCanvas.h"
#include "../include/SkFontMgr_alphaskia.h"

#include "../../externals/skia/include/core/SkFontMgr.h"

// C - API
extern "C"
Expand All @@ -15,6 +18,26 @@ extern "C"
delete internal;
}

AS_API void alphaskia_switch_to_freetype_fonts()
{
sk_sp<SkFontMgr> fm = SkFontMgr::RefDefault();
SkFontMgr_AlphaSkia *afm = dynamic_cast<SkFontMgr_AlphaSkia *>(fm.get());
if (afm != nullptr)
{
afm->switch_to_freetype_fonts();
}
}

AS_API void alphaskia_switch_to_operating_system_fonts()
{
sk_sp<SkFontMgr> fm = SkFontMgr::RefDefault();
SkFontMgr_AlphaSkia *afm = dynamic_cast<SkFontMgr_AlphaSkia *>(fm.get());
if (afm != nullptr)
{
afm->switch_to_operating_system_fonts();
}
}

AS_API int32_t alphaskia_get_color_type()
{
return kN32_SkColorType;
Expand Down

0 comments on commit 836a958

Please sign in to comment.