Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 36a2dc9

Browse files
author
Yuncong Zhang
committed
[1.5.4] Upgrade text painter.
1 parent 016b342 commit 36a2dc9

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

Runtime/painting/notched_shapes.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,24 @@ public override Path getOuterPath(Rect host, Rect guest) {
6868
return ret;
6969
}
7070
}
71+
72+
class AutomaticNotchedShape : NotchedShape {
73+
public AutomaticNotchedShape(ShapeBorder host, ShapeBorder guest = null) {
74+
this.host = host;
75+
this.guest = guest;
76+
}
77+
78+
public readonly ShapeBorder host;
79+
public readonly ShapeBorder guest;
80+
81+
public override Path getOuterPath(Rect hostRect, Rect guestRect) {
82+
Path hostPath = this.host.getOuterPath(hostRect);
83+
if (this.guest != null && guestRect != null) {
84+
Path guestPath = this.guest.getOuterPath(guestRect);
85+
return Path.combine(PathOperation.difference, hostPath, guestPath);
86+
}
87+
88+
return hostPath;
89+
}
90+
}
7191
}

Runtime/painting/text_painter.cs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
23
using Unity.UIWidgets.foundation;
34
using Unity.UIWidgets.service;
45
using Unity.UIWidgets.ui;
@@ -7,6 +8,16 @@
78
using Rect = Unity.UIWidgets.ui.Rect;
89

910
namespace Unity.UIWidgets.painting {
11+
12+
class _CaretMetrics {
13+
public _CaretMetrics(Offset offset, float? fullHeight) {
14+
this.offset = offset;
15+
this.fullHeight = fullHeight;
16+
}
17+
18+
public Offset offset;
19+
public float? fullHeight;
20+
}
1021
public class TextPainter {
1122
TextSpan _text;
1223
TextAlign _textAlign;
@@ -224,29 +235,38 @@ public void paint(Canvas canvas, Offset offset) {
224235
}
225236

226237
public Offset getOffsetForCaret(TextPosition position, Rect caretPrototype) {
238+
this._computeCaretMetrics(position, caretPrototype);
239+
return this._caretMetrics.offset;
240+
}
241+
242+
_CaretMetrics _caretMetrics;
243+
244+
TextPosition _previousCaretPosition;
245+
Rect _previousCaretPrototype;
246+
247+
void _computeCaretMetrics(TextPosition position, Rect caretPrototype) {
227248
D.assert(!this._needsLayout);
228-
var offset = position.offset;
229-
if (offset > 0) {
230-
var prevCodeUnit = this._text.codeUnitAt(offset);
231-
if (prevCodeUnit == null) // out of upper bounds
232-
{
233-
var rectNextLine = this._paragraph.getNextLineStartRect();
234-
if (rectNextLine != null) {
235-
return new Offset(rectNextLine.start, rectNextLine.top);
236-
}
237-
}
249+
if (position == this._previousCaretPosition && caretPrototype == this._previousCaretPrototype) {
250+
return;
238251
}
239-
252+
var offset = position.offset;
253+
Rect rect;
240254
switch (position.affinity) {
241255
case TextAffinity.upstream:
242-
return this._getOffsetFromUpstream(offset, caretPrototype) ??
243-
this._getOffsetFromDownstream(offset, caretPrototype) ?? this._emptyOffset;
256+
rect = this._getRectFromUpstream(offset, caretPrototype) ??
257+
this._getRectFromDownStream(offset, caretPrototype);
258+
break;
244259
case TextAffinity.downstream:
245-
return this._getOffsetFromDownstream(offset, caretPrototype) ??
246-
this._getOffsetFromUpstream(offset, caretPrototype) ?? this._emptyOffset;
260+
rect = this._getRectFromDownStream(offset, caretPrototype) ??
261+
this._getRectFromUpstream(offset, caretPrototype);
262+
break;
263+
default:
264+
throw new UIWidgetsError("Unknown Position Affinity");
247265
}
248266

249-
return null;
267+
this._caretMetrics = new _CaretMetrics(
268+
offset: rect != null ? new Offset(rect.left, rect.top) : this._emptyOffset,
269+
fullHeight: rect != null ? (float?) (rect.bottom - rect.top) : null);
250270
}
251271

252272
public Paragraph.LineRange getLineRange(int lineNumber) {
@@ -372,7 +392,7 @@ float _applyFloatingPointHack(float layoutValue) {
372392

373393
const int _zwjUtf16 = 0x200d;
374394

375-
Offset _getOffsetFromUpstream(int offset, Rect caretPrototype) {
395+
Rect _getRectFromUpstream(int offset, Rect caretPrototype) {
376396
string flattenedText = this._text.toPlainText();
377397
var prevCodeUnit = this._text.codeUnitAt(Mathf.Max(0, offset - 1));
378398
if (prevCodeUnit == null) {
@@ -401,18 +421,20 @@ Offset _getOffsetFromUpstream(int offset, Rect caretPrototype) {
401421
TextBox box = boxes[0];
402422
const int NEWLINE_CODE_UNIT = 10;
403423
if (prevCodeUnit == NEWLINE_CODE_UNIT) {
404-
return new Offset(this._emptyOffset.dx, box.bottom);
424+
return Rect.fromLTRB(this._emptyOffset.dx, box.bottom,
425+
this._emptyOffset.dx, box.bottom + box.bottom - box.top);
405426
}
406427

407428
float caretEnd = box.end;
408429
float dx = box.direction == TextDirection.rtl ? caretEnd - caretPrototype.width : caretEnd;
409-
return new Offset(dx, box.top);
430+
return Rect.fromLTRB(Mathf.Min(dx, this.width), box.top,
431+
Mathf.Min(dx, this.width), box.bottom);
410432
}
411433

412434
return null;
413435
}
414436

415-
Offset _getOffsetFromDownstream(int offset, Rect caretPrototype) {
437+
Rect _getRectFromDownStream(int offset, Rect caretPrototype) {
416438
string flattenedText = this._text.toPlainText();
417439
var nextCodeUnit =
418440
this._text.codeUnitAt(Mathf.Min(offset, flattenedText == null ? 0 : flattenedText.Length - 1));
@@ -442,7 +464,8 @@ Offset _getOffsetFromDownstream(int offset, Rect caretPrototype) {
442464
TextBox box = boxes[boxes.Count - 1];
443465
float caretStart = box.start;
444466
float dx = box.direction == TextDirection.rtl ? caretStart - caretPrototype.width : caretStart;
445-
return new Offset(dx, box.top);
467+
return Rect.fromLTRB(Mathf.Min(dx, this.width), box.top,
468+
Mathf.Min(dx, this.width), box.bottom);
446469
}
447470

448471
return null;

Runtime/ui/painting/path.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
using Vector3 = UnityEngine.Vector3;
88

99
namespace Unity.UIWidgets.ui {
10+
public enum PathOperation {
11+
difference,
12+
intersect,
13+
union,
14+
xor,
15+
reverseDifference,
16+
}
1017
public class Path {
1118
const float _KAPPA90 = 0.5522847493f;
1219

@@ -161,6 +168,18 @@ public Rect getBounds() {
161168
return Rect.fromLTRB(this._minX, this._minY, this._maxX, this._maxY);
162169
}
163170

171+
public static Path combine(PathOperation operation, Path path1, Path path2) {
172+
D.assert(path1 != null);
173+
D.assert(path2 != null);
174+
Path path = new Path();
175+
throw new UIWidgetsError("Path._op() not implemented yet!");
176+
// if (path._op(path1, path2, (int) operation)) {
177+
// return path;
178+
// }
179+
// throw new UIWidgetsError("Path.combine() failed. This may be due an invalid path; " +
180+
// "in particular, check for NaN values.");
181+
}
182+
164183
void _appendMoveTo(float x, float y) {
165184
this._commands.Add((float) PathCommand.moveTo);
166185
this._commands.Add(x);

0 commit comments

Comments
 (0)