45 inherited_opacity_ = opacity;
57 force_text_color_ =
value;
61 const Matrix entity_offset_transform =
63 return frame_->GetBounds().TransformBounds(entity_offset_transform);
68 const std::optional<StrokeParameters>& stroke) {
69 if (frame_->HasColor()) {
74 properties_.
stroke = stroke;
81 const Scalar epsilon = 0.005f;
82 if (std::abs(
x - 1.f) < epsilon) {
85 if (std::abs(
x + 1.f) < epsilon) {
94 VS::PerVertexData* vtx_contents,
95 const Matrix& entity_transform,
96 const std::shared_ptr<TextFrame>& frame,
98 const Matrix& screen_transform,
99 std::optional<GlyphProperties> glyph_properties,
100 const std::shared_ptr<GlyphAtlas>& atlas) {
108 constexpr std::array<Point, 4> unit_points = {
Point{0, 0},
Point{1, 0},
111 Matrix entity_offset_transform =
114 ISize atlas_size = atlas->GetTexture()->GetSize();
116 Matrix basis_transform = entity_offset_transform.
Basis();
118 VS::PerVertexData vtx;
121 const Matrix frame_transform =
132 unscaled_basis.
m[0] = AttractToOne(unscaled_basis.
m[0]);
133 unscaled_basis.
m[5] = AttractToOne(unscaled_basis.
m[5]);
136 Point screen_offset = (entity_offset_transform *
Point(0, 0));
138 for (
const TextRun& run : frame->GetRuns()) {
139 const Font& font = run.GetFont();
140 const ScaledFont scaled_font{.
font = font, .scale = rounded_scale};
141 const FontGlyphAtlas* font_atlas = atlas->GetFontGlyphAtlas(scaled_font);
156 Point subpixel_adjustment(0.5, 0.5);
161 subpixel_adjustment.
x = 0.125;
164 subpixel_adjustment.
y = 0.125;
167 subpixel_adjustment.
x = 0.125;
168 subpixel_adjustment.
y = 0.125;
173 run.GetGlyphPositions()) {
201 for (
const Point& point : unit_points) {
203 if (is_translation_scale) {
204 Point unrounded_glyph_position =
207 (basis_transform * glyph_position.position);
209 Point screen_glyph_position =
210 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
213 (screen_glyph_position +
219 position = entity_offset_transform *
220 (glyph_position.position + scaled_bounds.
GetLeftTop() +
221 point * scaled_bounds.
GetSize());
223 vtx.uv = uv_origin + (uv_size * point);
224 vtx.position = position;
225 vtx_contents[i++] = vtx;
240 const std::shared_ptr<GlyphAtlas>& atlas =
244 if (!atlas || !atlas->IsValid()) {
256 VS::FrameInfo frame_info;
265 FS::FragInfo frag_info;
266 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
274 if (is_translation_scale) {
284 constexpr
Scalar kMinScaleForRatio = 0.001f;
285 constexpr
Scalar kAnisotropicScaleThreshold = 1.15f;
288 const Scalar ratio = (sx > sy) ? sx / std::max(sy, kMinScaleForRatio)
289 : sy / std::max(sx, kMinScaleForRatio);
290 if (ratio > kAnisotropicScaleThreshold) {
311 FS::BindGlyphAtlasSampler(
314 renderer.
GetContext()->GetSamplerLibrary()->GetSampler(
320 size_t glyph_count = 0;
321 for (
const auto& run : frame_->GetRuns()) {
322 glyph_count += run.GetGlyphPositions().size();
324 size_t vertex_count = glyph_count * 4;
325 size_t index_count = glyph_count * 6;
328 vertex_count *
sizeof(VS::PerVertexData),
alignof(VS::PerVertexData),
330 VS::PerVertexData* vtx_contents =
331 reinterpret_cast<VS::PerVertexData*
>(data);
337 GetGlyphProperties(),
341 index_count *
sizeof(uint16_t),
alignof(uint16_t), [&](uint8_t* data) {
342 uint16_t* indices =
reinterpret_cast<uint16_t*
>(data);
344 for (
auto i = 0u; i < glyph_count; i++) {
346 indices[j++] = base + 0;
347 indices[j++] = base + 1;
348 indices[j++] = base + 2;
349 indices[j++] = base + 1;
350 indices[j++] = base + 2;
351 indices[j++] = base + 3;
359 return pass.
Draw().ok();
362 std::optional<GlyphProperties> TextContents::GetGlyphProperties()
const {
363 return (properties_.
stroke || frame_->HasColor())
364 ? std::optional<GlyphProperties>(properties_)
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
std::shared_ptr< Context > GetContext() const
Matrix GetShaderTransform(const RenderPass &pass) const
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
float GetShaderClipDepth() const
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Describes a typeface along with any modifications to its intrinsic properties.
AxisAlignment GetAxisAlignment() const
Type
Describes how the glyphs are represented in the texture.
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Render passes encode render commands directed as one specific render target into an underlying comman...
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
virtual bool SetIndexBuffer(BufferView index_buffer, IndexType index_type)
Specify an index buffer to use for this command. To unset the index buffer, pass IndexType::kNone to ...
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
virtual fml::Status Draw()
Record the currently pending command.
virtual void SetElementCount(size_t count)
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
VertexShader_ VertexShader
FragmentShader_ FragmentShader
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
void SetForceTextColor(bool value)
Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.
void SetTextProperties(Color color, const std::optional< StrokeParameters > &stroke)
Must be set after text frame.
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const Matrix &entity_transform, const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &screen_transform, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
Computes the vertex data for the render operation from a collection of data drawn from the DrawTextFr...
void SetPosition(Point position)
void SetScreenTransform(const Matrix &transform)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
void SetTextFrame(const std::shared_ptr< TextFrame > &frame)
void SetColor(Color color)
static Rational RoundScaledFontSize(Scalar scale)
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Represents a collection of positioned glyphs from a specific font.
LinePipeline::FragmentShader FS
@ kBase
The texture is sampled as if it only had a single mipmap level.
Point SizeToPoint(Size size)
constexpr Vector4 ToVector(Color color)
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr bool IsTransparent() const
constexpr Color WithAlpha(Scalar new_alpha) const
constexpr Color Premultiply() const
Rect atlas_bounds
The bounds of the glyph within the glyph atlas texture.
Rect glyph_bounds
The glyph bounds within the local coordinate system.
std::optional< StrokeParameters > stroke
A 4x4 matrix using column-major storage.
static constexpr Matrix MakeTranslation(const Vector3 &t)
constexpr Vector3 GetBasisY() const
constexpr bool IsTranslationScaleOnly() const
Returns true if the matrix has a scale-only basis and is non-projective. Note that an identity matrix...
constexpr Matrix Basis() const
The Matrix without its w components (without translation).
static constexpr Matrix MakeScale(const Vector3 &s)
constexpr Vector3 GetBasisX() const
Scalar GetMaxBasisLengthXY() const
Return the maximum scale applied specifically to either the X axis or Y axis unit vectors (the bases)...
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.
static constexpr TPoint Round(const TPoint< U > &other)
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
constexpr TRect Scale(Type scale) const
constexpr TPoint< T > GetLeftTop() const