-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement GlyphFace rendering capabilities #371
Changes from 16 commits
194d822
a49135f
ace00c1
4032de2
c225220
fa33273
bd287e5
847a778
9ed1938
b36088b
10ed5ea
cc2ddb2
fcecbfb
ea7f358
797b3f3
7ed665c
9333349
f5d1bf1
413e3d8
09d3c05
f5f46a2
7de4b86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making tgfx available. | ||
// | ||
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// unless required by applicable law or agreed to in writing, software distributed under the | ||
// license is distributed on an "as is" basis, without warranties or conditions of any kind, | ||
// either express or implied. see the license for the specific language governing permissions | ||
// and limitations under the license. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include "tgfx/core/Image.h" | ||
#include "tgfx/core/Path.h" | ||
#include "tgfx/core/Typeface.h" | ||
|
||
namespace tgfx { | ||
/** | ||
* GlyphFace is a render-only font that contains only the necessary information to render glyphs. It can be implemented externally to render glyphs from a custom font or used as a wrapper around a Font object. | ||
*/ | ||
class GlyphFace { | ||
public: | ||
GlyphFace() = default; | ||
virtual ~GlyphFace() = default; | ||
|
||
/** | ||
* Returns true if the font has color glyphs, for example, color emojis. | ||
*/ | ||
virtual bool hasColor() const = 0; | ||
|
||
/** | ||
* Returns true if the font has outline glyphs, meaning it can generate paths. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 更新注释里的 the font 为 the glyph face |
||
*/ | ||
virtual bool hasOutlines() const = 0; | ||
|
||
/** | ||
* Returns a new GlyphFace with the same attributes of this font, but with the specified scale. | ||
*/ | ||
virtual std::shared_ptr<GlyphFace> makeScaled(float scale) = 0; | ||
|
||
/** | ||
* Creates a path corresponding to glyph outline. If glyph has an outline, copies outline to path | ||
* and returns true. If glyph is described by a bitmap, returns false and ignores path parameter. | ||
*/ | ||
virtual bool getPath(GlyphID glyphID, Path* path) const = 0; | ||
|
||
/** | ||
* Creates an Image capturing the content of the specified glyph. The returned matrix should apply | ||
* to the glyph image when drawing. Please note that the fauxBold is not supported for this | ||
* method. | ||
*/ | ||
virtual std::shared_ptr<Image> getImage(GlyphID glyphID, Matrix* matrix) const = 0; | ||
|
||
/** | ||
* Returns the bounding box of the specified glyph. | ||
*/ | ||
virtual Rect getBounds(GlyphID glyphID) const = 0; | ||
|
||
/** | ||
* Returns the font object represented by this GlyphFace. | ||
* If the GlyphFace is not a font object, returns false and sets the font pointer to null. | ||
* If the GlyphFace is a font object, returns true and sets the font pointer to the font object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修改注释: Checks if the GlyphFace is backed by a Font object. If so, sets the font pointer to the backing Font object and returns true. Otherwise, returns false and leaves the font pointer unchanged. |
||
*/ | ||
virtual bool asFont(Font* font) const = 0; | ||
}; | ||
} // namespace tgfx |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,14 @@ | |
#pragma once | ||
|
||
#include "tgfx/core/Font.h" | ||
#include "tgfx/core/GlyphFace.h" | ||
|
||
namespace tgfx { | ||
/** | ||
* GlyphRun represents a sequence of glyphs from a single font, along with their positions. | ||
*/ | ||
struct GlyphRun { | ||
class GlyphRun { | ||
public: | ||
/** | ||
* Constructs an empty GlyphRun. | ||
*/ | ||
|
@@ -33,14 +35,18 @@ struct GlyphRun { | |
/** | ||
* Constructs a GlyphRun using a font, a list of glyph IDs, and their positions. | ||
*/ | ||
GlyphRun(Font font, std::vector<GlyphID> glyphIDs, std::vector<Point> positions) | ||
: font(font), glyphs(glyphIDs), positions(positions) { | ||
} | ||
GlyphRun(Font font, std::vector<GlyphID> glyphIDs, std::vector<Point> positions); | ||
|
||
/** | ||
* Returns the font used to render the glyphs in this run. | ||
* Constructs a GlyphRun using a GlyphFace, a list of glyph IDs, and their positions. | ||
*/ | ||
Font font = {}; | ||
GlyphRun(std::shared_ptr<GlyphFace> glyphFace, std::vector<GlyphID> glyphIDs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要删除原本的Font构造函数,提供两个版本,和Canvas上一样。 |
||
std::vector<Point> positions); | ||
|
||
/** | ||
* Returns the GlyphFace used to render the glyphs in this run. | ||
*/ | ||
std::shared_ptr<GlyphFace> glyphFace = nullptr; | ||
|
||
/** | ||
* Returns the sequence of glyph IDs in this run. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making tgfx available. | ||
// | ||
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// unless required by applicable law or agreed to in writing, software distributed under the | ||
// license is distributed on an "as is" basis, without warranties or conditions of any kind, | ||
// either express or implied. see the license for the specific language governing permissions | ||
// and limitations under the license. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "core/FontGlyphFace.h" | ||
#include "utils/MathExtra.h" | ||
|
||
namespace tgfx { | ||
std::shared_ptr<FontGlyphFace> FontGlyphFace::Make(const Font& font) { | ||
if (font.getTypeface() == nullptr) { | ||
return nullptr; | ||
} | ||
return std::shared_ptr<FontGlyphFace>(new FontGlyphFace(font)); | ||
} | ||
|
||
bool FontGlyphFace::hasColor() const { | ||
return _font.hasColor(); | ||
} | ||
|
||
bool FontGlyphFace::hasOutlines() const { | ||
return _font.hasOutlines(); | ||
} | ||
|
||
std::shared_ptr<GlyphFace> FontGlyphFace::makeScaled(float scale) { | ||
if (FloatNearlyZero(scale)) { | ||
return nullptr; | ||
} | ||
auto size = _font.getSize() * scale; | ||
return FontGlyphFace::Make(_font.makeWithSize(size)); | ||
} | ||
|
||
bool FontGlyphFace::getPath(GlyphID glyphID, Path* path) const { | ||
return _font.getPath(glyphID, path); | ||
} | ||
|
||
std::shared_ptr<Image> FontGlyphFace::getImage(GlyphID glyphID, Matrix* matrix) const { | ||
return _font.getImage(glyphID, matrix); | ||
} | ||
|
||
Rect FontGlyphFace::getBounds(GlyphID glyphID) const { | ||
return _font.getBounds(glyphID); | ||
} | ||
|
||
bool FontGlyphFace::asFont(Font* font) const { | ||
if (font == nullptr) { | ||
return false; | ||
} | ||
*font = _font; | ||
return true; | ||
} | ||
} // namespace tgfx |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making tgfx available. | ||
// | ||
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// unless required by applicable law or agreed to in writing, software distributed under the | ||
// license is distributed on an "as is" basis, without warranties or conditions of any kind, | ||
// either express or implied. see the license for the specific language governing permissions | ||
// and limitations under the license. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
#include "tgfx/core/Font.h" | ||
#include "tgfx/core/GlyphFace.h" | ||
|
||
namespace tgfx { | ||
class FontGlyphFace final : public GlyphFace { | ||
public: | ||
static std::shared_ptr<FontGlyphFace> Make(const Font& font); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个方法重命名为Wrap,移动到父类GlyphFace上作为公开API,之前GlyphRun的修改可以还原回去。 |
||
|
||
bool hasColor() const override; | ||
|
||
bool hasOutlines() const override; | ||
|
||
std::shared_ptr<GlyphFace> makeScaled(float scale) override; | ||
|
||
bool getPath(GlyphID glyphID, Path* path) const override; | ||
|
||
std::shared_ptr<Image> getImage(GlyphID glyphID, Matrix* matrix) const override; | ||
|
||
Rect getBounds(GlyphID glyphID) const override; | ||
|
||
bool asFont(Font* font) const override; | ||
|
||
private: | ||
explicit FontGlyphFace(const Font& font) : _font(font) { | ||
} | ||
|
||
Font _font = {}; | ||
}; | ||
} // namespace tgfx |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making tgfx available. | ||
// | ||
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// unless required by applicable law or agreed to in writing, software distributed under the | ||
// license is distributed on an "as is" basis, without warranties or conditions of any kind, | ||
// either express or implied. see the license for the specific language governing permissions | ||
// and limitations under the license. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
#include "tgfx/core/GlyphRun.h" | ||
#include "core/FontGlyphFace.h" | ||
|
||
namespace tgfx { | ||
GlyphRun::GlyphRun(Font font, std::vector<GlyphID> glyphIDs, std::vector<Point> positions) | ||
: GlyphRun(FontGlyphFace::Make(font), glyphIDs, positions) { | ||
} | ||
|
||
GlyphRun::GlyphRun(std::shared_ptr<GlyphFace> glyphFace, std::vector<GlyphID> glyphIDs, | ||
std::vector<Point> positions) | ||
: glyphFace(glyphFace), glyphs(glyphIDs), positions(positions) { | ||
} | ||
|
||
} // namespace tgfx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
忘记换行了