Skip to content

Commit

Permalink
Revert "Revert "custom typeface""
Browse files Browse the repository at this point in the history
Fix: implement onComputeBounds() and generateFontMetrics()

This reverts commit 0066ade.

Change-Id: Idb59336a3d201bb97e494ee0e0bb189e0a7186f1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/288536
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
  • Loading branch information
reed-at-google authored and Skia Commit-Bot committed May 7, 2020
1 parent 8650d33 commit 61642b3
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 0 deletions.
89 changes: 89 additions & 0 deletions gm/userfont.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/utils/SkCustomTypeface.h"
#include "tools/Resources.h"

static sk_sp<SkTypeface> make_tf() {
SkCustomTypefaceBuilder builder(128);
SkFont font;
font.setSize(1.0f);
font.setHinting(SkFontHinting::kNone);

// Steal the first 128 chars from the default font
for (SkGlyphID index = 0; index <= 127; ++index) {
SkGlyphID glyph = font.unicharToGlyph(index);

SkScalar width;
font.getWidths(&glyph, 1, &width);
SkPath path;
font.getPath(glyph, &path);

// we use the charcode to be our glyph index, since we have no cmap table
builder.setGlyph(index, width, path);
}

return builder.detach();
}

#include "include/core/SkTextBlob.h"

class UserFontGM : public skiagm::GM {
sk_sp<SkTypeface> fTF;
sk_sp<SkTextBlob> fBlob;

SkPath fPath;
public:
UserFontGM() {}

void onOnceBeforeDraw() override {
fTF = make_tf();

SkFont font(fTF);
font.setSize(100);
font.setEdging(SkFont::Edging::kAntiAlias);

std::vector<SkGlyphID> array;
auto expand8to16 = [&](const char str[]) {
for (int i = 0; str[i]; ++i) {
array.push_back(str[i]);
}
};

expand8to16("User Typeface");
fBlob = SkTextBlob::MakeFromText(array.data(), array.size() * sizeof(SkGlyphID),
font, SkTextEncoding::kGlyphID);

}

bool runAsBench() const override { return true; }

SkString onShortName() override { return SkString("user_typeface"); }

SkISize onISize() override { return {512, 512}; }

void onDraw(SkCanvas* canvas) override {
SkScalar x = 20,
y = 250;

SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
canvas->drawRect(fBlob->bounds().makeOffset(x, y), paint);

paint.setStyle(SkPaint::kFill_Style);
paint.setColor(SK_ColorRED);
canvas->drawTextBlob(fBlob, x, y, paint);
}
};
DEF_GM(return new UserFontGM;)
1 change: 1 addition & 0 deletions gn/gm.gni
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ gm_sources = [
"$_gm/trickycubicstrokes.cpp",
"$_gm/typeface.cpp",
"$_gm/unpremul.cpp",
"$_gm/userfont.cpp",
"$_gm/variedtext.cpp",
"$_gm/verifiers/gmverifier.cpp",
"$_gm/vertices.cpp",
Expand Down
2 changes: 2 additions & 0 deletions gn/utils.gni
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ skia_utils_public = [
"$_include/utils/SkBase64.h",
"$_include/utils/SkCamera.h",
"$_include/utils/SkCanvasStateUtils.h",
"$_include/utils/SkCustomTypeface.h",
"$_include/utils/SkEventTracer.h",
"$_include/utils/SkInterpolator.h",
"$_include/utils/SkNWayCanvas.h",
Expand Down Expand Up @@ -40,6 +41,7 @@ skia_utils_sources = [
"$_src/utils/SkCharToGlyphCache.h",
"$_src/utils/SkClipStackUtils.cpp",
"$_src/utils/SkClipStackUtils.h",
"$_src/utils/SkCustomTypeface.cpp",
"$_src/utils/SkDashPath.cpp",
"$_src/utils/SkDashPathPriv.h",
"$_src/utils/SkEventTracer.cpp",
Expand Down
42 changes: 42 additions & 0 deletions include/utils/SkCustomTypeface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#ifndef SkCustomTypeface_DEFINED
#define SkCustomTypeface_DEFINED

#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPicture.h"
#include "include/core/SkTypeface.h"

#include <vector>

class SkStream;

class SkCustomTypefaceBuilder {
public:
SkCustomTypefaceBuilder(int numGlyphs);

void setGlyph(SkGlyphID, float advance, const SkPath&);
void setGlyph(SkGlyphID, float advance, const SkPath&, const SkPaint&);
void setGlyph(SkGlyphID, float advance, sk_sp<SkImage>, float scale);
void setGlyph(SkGlyphID, float advance, sk_sp<SkPicture>);

sk_sp<SkTypeface> detach();

private:
int fGlyphCount;
std::vector<SkPath> fPaths;
std::vector<float> fAdvances;

static sk_sp<SkTypeface> Deserialize(SkStream*);

friend class SkTypeface;
};

#endif
1 change: 1 addition & 0 deletions src/core/SkGlyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class SkGlyph {
friend class SkStrikeServer;
friend class SkTestScalerContext;
friend class SkTestSVGScalerContext;
friend class SkUserScalerContext;
friend class TestSVGTypeface;
friend class TestTypeface;

Expand Down
7 changes: 7 additions & 0 deletions src/core/SkTypeface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "include/core/SkTypeface.h"
#include "include/private/SkMutex.h"
#include "include/private/SkOnce.h"
#include "include/utils/SkCustomTypeface.h"
#include "src/core/SkAdvancedTypefaceMetrics.h"
#include "src/core/SkEndian.h"
#include "src/core/SkFontDescriptor.h"
Expand Down Expand Up @@ -161,6 +162,12 @@ sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
}

sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) {
if (data->hasStream()) {
if (auto tf = SkCustomTypefaceBuilder::Deserialize(data->getStream())) {
return tf;
}
}

return SkFontMgr::RefDefault()->makeFromFontData(std::move(data));
}

Expand Down
Loading

0 comments on commit 61642b3

Please sign in to comment.