-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8650d33
commit 61642b3
Showing
7 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.