Flutter Impeller
text_frame_skia.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <vector>
8 
9 #include "flutter/display_list/geometry/dl_path.h"
10 #include "flutter/fml/logging.h"
11 #include "fml/status.h"
15 #include "third_party/skia/include/core/SkFont.h"
16 #include "third_party/skia/include/core/SkFontMetrics.h"
17 #include "third_party/skia/include/core/SkPaint.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "third_party/skia/modules/skparagraph/include/Paragraph.h" // nogncheck
20 #include "third_party/skia/src/core/SkStrikeSpec.h" // nogncheck
21 #include "third_party/skia/src/core/SkTextBlobPriv.h" // nogncheck
22 
23 namespace impeller {
24 
25 static Font ToFont(const SkTextBlobRunIterator& run, AxisAlignment alignment) {
26  auto& font = run.font();
27  auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
28 
29  SkFontMetrics sk_metrics;
30  font.getMetrics(&sk_metrics);
31 
32  Font::Metrics metrics;
33  metrics.point_size = font.getSize();
34  metrics.embolden = font.isEmbolden();
35  metrics.skewX = font.getSkewX();
36  metrics.scaleX = font.getScaleX();
37 
38  return Font{std::move(typeface), metrics, alignment};
39 }
40 
41 static Rect ToRect(const SkRect& rect) {
42  return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
43 }
44 
45 std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
46  const sk_sp<SkTextBlob>& blob) {
47  bool has_color = false;
48  std::vector<TextRun> runs;
49  for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
50  SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(run.font());
51  SkBulkGlyphMetricsAndPaths paths{strikeSpec};
52  SkSpan<const SkGlyph*> glyphs =
53  paths.glyphs(SkSpan(run.glyphs(), run.glyphCount()));
54 
55  for (const auto& glyph : glyphs) {
56  has_color |= glyph->isColor();
57  }
58 
60  if (run.font().isSubpixel() && run.font().isBaselineSnap() && !has_color) {
61  alignment = AxisAlignment::kX;
62  }
63 
64  switch (run.positioning()) {
65  case SkTextBlobRunIterator::kFull_Positioning: {
66  std::vector<TextRun::GlyphPosition> positions;
67  positions.reserve(run.glyphCount());
68  for (auto i = 0u; i < run.glyphCount(); i++) {
69  // kFull_Positioning has two scalars per glyph.
70  const SkPoint* glyph_points = run.points();
71  const SkPoint* point = glyph_points + i;
73  glyphs[i]->isColor() ? Glyph::Type::kBitmap : Glyph::Type::kPath;
74  positions.emplace_back(TextRun::GlyphPosition{
75  Glyph{glyphs[i]->getGlyphID(), type}, Point{
76  point->x(),
77  point->y(),
78  }});
79  }
80  TextRun text_run(ToFont(run, alignment), positions);
81  runs.emplace_back(text_run);
82  break;
83  }
84  default:
85  FML_DLOG(ERROR) << "Unimplemented.";
86  continue;
87  }
88  }
89  return std::make_shared<TextFrame>(
90  runs, ToRect(blob->bounds()), has_color,
91  [blob]() -> fml::StatusOr<flutter::DlPath> {
92  SkPath path = skia::textlayout::Paragraph::GetPath(blob.get());
93  if (path.isEmpty()) {
94  return fml::Status(fml::StatusCode::kCancelled, "No path available");
95  }
96  SkPath transformed = path.makeTransform(
97  SkMatrix::Translate(blob->bounds().left(), blob->bounds().top()));
98  return flutter::DlPath(transformed);
99  });
100 }
101 
102 } // namespace impeller
GLenum type
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
static Font ToFont(const SkTextBlobRunIterator &run, AxisAlignment alignment)
static Rect ToRect(const SkRect &rect)
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:44
The glyph index in the typeface.
Definition: glyph.h:16
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129