@windowkit/appkit 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -0
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/calayers.node +0 -0
- package/prebuilds/darwin-x64/calayers.node +0 -0
- package/src/addon.mm +59 -2
- package/src/backend.mm +143 -13
package/README.md
CHANGED
|
@@ -417,6 +417,20 @@ positive angle turns clockwise and a positive translation moves down, with no si
|
|
|
417
417
|
change on the way in. A matrix of the wrong length or with a number that is not finite
|
|
418
418
|
is a `TypeError`, and nothing is applied.
|
|
419
419
|
|
|
420
|
+
**Contents gravity.** `setLayerProps`' `contentsGravity` is how a layer's contents sit in
|
|
421
|
+
bounds that are not their size: `resize`, Core Animation's default, stretches them;
|
|
422
|
+
`resizeAspect` and `resizeAspectFill` scale them keeping their shape; `center`, `top`,
|
|
423
|
+
`bottom`, `left`, `right`, `topLeft`, `topRight`, `bottomLeft` and `bottomRight` leave
|
|
424
|
+
them at their size, anchored there. A window or a pane whose bounds grow before a frame
|
|
425
|
+
of the new size is drawn shows its last frame by this: stretched by default — a page's
|
|
426
|
+
left column scaled a little, then drawn back at its size — or anchored, a strip of what
|
|
427
|
+
is under it showing where the new frame will be. The names are as they look, `top` being
|
|
428
|
+
the window's top. Core Animation's own are in the layer's space, where `top` is the
|
|
429
|
+
greatest y, which under a window's geometry-flipped root is the bottom of the screen, so
|
|
430
|
+
the bridge turns them over where the layer's space is flipped; `presentationValue(layer,
|
|
431
|
+
'contentsGravity')` answers Core Animation's name, `bottomLeft` for a `topLeft` here.
|
|
432
|
+
Any other name, or a value that is not one, is a `TypeError`, and nothing is applied.
|
|
433
|
+
|
|
420
434
|
**`native.transformForms()`** — `['translate', 'rotate', 'scale', 'matrix', 'matrix3d']`:
|
|
421
435
|
the forms a transform takes on its way in. A caller that hands over matrices
|
|
422
436
|
feature-detects `matrix` here; before 0.19 an object holding one was read as no transform
|
|
@@ -732,6 +746,31 @@ one tree sets its text in the same place on both. Below 1 the same split takes r
|
|
|
732
746
|
both sides and the glyphs overflow a short box evenly; `0` is a box of no height, as CSS's
|
|
733
747
|
`line-height: 0` is. An absent multiplier is 1.
|
|
734
748
|
|
|
749
|
+
## Text: justified lines
|
|
750
|
+
|
|
751
|
+
`createLayout`'s `justify` sets lines to fill `maxWidth`: what a line leaves of the width is
|
|
752
|
+
shared equally among its word separators, each space and no-break space before the white
|
|
753
|
+
space it ends on that much wider — the white space it ends on hangs and takes none. That is
|
|
754
|
+
CSS Text 3's `text-justify: auto` for the scripts that space their words, and it is how ntk
|
|
755
|
+
justifies on X11 and Wayland. It is bits:
|
|
756
|
+
|
|
757
|
+
- **`1`** — every line that goes on to another, CSS's `text-align: justify`;
|
|
758
|
+
- **`2`** — the paragraph's last line and each a forced break ends (a line feed, a carriage
|
|
759
|
+
return or a paragraph separator; a line separator, U+2028, is not one);
|
|
760
|
+
- **`3`** — every line.
|
|
761
|
+
|
|
762
|
+
A line with no separator, or no width to spare, keeps the place `align` gives it, and a line
|
|
763
|
+
an ellipsis ends is not justified. The lines are the ones the typesetter breaks at the
|
|
764
|
+
width; justifying moves no break, and a kept typesetter (`keep`, below) laid out again at
|
|
765
|
+
another width is broken and spaced again without shaping anything. A justified line is
|
|
766
|
+
`maxWidth` wide, its runs, carets (`layoutCaret`) and hit tests (`layoutIndexAt`) are where
|
|
767
|
+
the wider separators put them, and `drawLayout`, `drawLayoutGradient` and `layoutCoverage`
|
|
768
|
+
draw its glyphs there.
|
|
769
|
+
|
|
770
|
+
It is not `CTLineCreateJustifiedLine`. Once a line's spaces have taken some share of their
|
|
771
|
+
own advance, CoreText's justification spaces the letters too, where a browser widens
|
|
772
|
+
nothing but the separators; and it gives the space a line ends on no advance at all.
|
|
773
|
+
|
|
735
774
|
## Text: a layout's coverage
|
|
736
775
|
|
|
737
776
|
- **`native.layoutCoverage(layout, pad?)`** — `{ width, height, data }`: how much of each
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@windowkit/appkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Retained-mode AppKit backend for Node.js: Core Animation (CALayer) layer trees, CoreText layout and drawing, IOSurface presentation, NSMenu, and native control bezels — the macOS half of react-x11.",
|
|
5
5
|
"author": "Andrey Sidorov <andrey.sidorov@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"install": "node scripts/install.js",
|
|
44
44
|
"build": "node-gyp rebuild",
|
|
45
45
|
"demo": "node demo/demo.js",
|
|
46
|
-
"test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/text-scale.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
|
|
46
|
+
"test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/contents-gravity.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/text-justify.js && node test/text-scale.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"node-addon-api": "^8.3.0"
|
|
Binary file
|
|
Binary file
|
package/src/addon.mm
CHANGED
|
@@ -583,8 +583,49 @@ struct LayerPropsSpec {
|
|
|
583
583
|
NSString* name = nil;
|
|
584
584
|
id mask = nil; // a layer target; nil clears
|
|
585
585
|
CATransform3D transform = CATransform3DIdentity;
|
|
586
|
+
bool hasGravity = false, badGravity = false;
|
|
587
|
+
NSString* gravity = nil; // as named: the window's top is `top`
|
|
586
588
|
};
|
|
587
589
|
|
|
590
|
+
// `contentsGravity`: how a layer's contents sit in bounds that are not
|
|
591
|
+
// their size. Core Animation's default stretches them to the bounds, which
|
|
592
|
+
// is what a window shows while it waits for a frame of its new size; the
|
|
593
|
+
// others leave them at their size, anchored. Named as they look — `top` is
|
|
594
|
+
// the window's top — where Core Animation's names are the layer's own
|
|
595
|
+
// space, whose `top` is its greatest y: under the window's geometry-flipped
|
|
596
|
+
// root, the bottom of the screen (`CALGravity`).
|
|
597
|
+
static bool CALGravityKnown(NSString* name) {
|
|
598
|
+
static NSSet* known = [NSSet setWithArray:@[
|
|
599
|
+
@"resize", @"resizeAspect", @"resizeAspectFill", @"center", @"top", @"bottom",
|
|
600
|
+
@"left", @"right", @"topLeft", @"topRight", @"bottomLeft", @"bottomRight"
|
|
601
|
+
]];
|
|
602
|
+
return name && [known containsObject:name];
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// The constant for a gravity named as it looks, on the UI thread, where the
|
|
606
|
+
// layer can say whether its space is flipped.
|
|
607
|
+
static NSString* CALGravity(CALayer* L, NSString* name) {
|
|
608
|
+
bool flipped = L.contentsAreFlipped;
|
|
609
|
+
NSString* top = flipped ? kCAGravityBottom : kCAGravityTop;
|
|
610
|
+
NSString* bottom = flipped ? kCAGravityTop : kCAGravityBottom;
|
|
611
|
+
NSString* topLeft = flipped ? kCAGravityBottomLeft : kCAGravityTopLeft;
|
|
612
|
+
NSString* topRight = flipped ? kCAGravityBottomRight : kCAGravityTopRight;
|
|
613
|
+
NSString* bottomLeft = flipped ? kCAGravityTopLeft : kCAGravityBottomLeft;
|
|
614
|
+
NSString* bottomRight = flipped ? kCAGravityTopRight : kCAGravityBottomRight;
|
|
615
|
+
if ([name isEqualToString:@"top"]) return top;
|
|
616
|
+
if ([name isEqualToString:@"bottom"]) return bottom;
|
|
617
|
+
if ([name isEqualToString:@"topLeft"]) return topLeft;
|
|
618
|
+
if ([name isEqualToString:@"topRight"]) return topRight;
|
|
619
|
+
if ([name isEqualToString:@"bottomLeft"]) return bottomLeft;
|
|
620
|
+
if ([name isEqualToString:@"bottomRight"]) return bottomRight;
|
|
621
|
+
if ([name isEqualToString:@"left"]) return kCAGravityLeft;
|
|
622
|
+
if ([name isEqualToString:@"right"]) return kCAGravityRight;
|
|
623
|
+
if ([name isEqualToString:@"center"]) return kCAGravityCenter;
|
|
624
|
+
if ([name isEqualToString:@"resizeAspect"]) return kCAGravityResizeAspect;
|
|
625
|
+
if ([name isEqualToString:@"resizeAspectFill"]) return kCAGravityResizeAspectFill;
|
|
626
|
+
return kCAGravityResize;
|
|
627
|
+
}
|
|
628
|
+
|
|
588
629
|
static LayerPropsSpec ParseLayerProps(Napi::Object o) {
|
|
589
630
|
LayerPropsSpec s;
|
|
590
631
|
if (o.Has("frame")) {
|
|
@@ -648,6 +689,11 @@ static LayerPropsSpec ParseLayerProps(Napi::Object o) {
|
|
|
648
689
|
else s.transform = TransformFrom(tv);
|
|
649
690
|
}
|
|
650
691
|
if (o.Has("contents")) s.clearContents = o.Get("contents").IsNull();
|
|
692
|
+
if ((s.hasGravity = o.Has("contentsGravity"))) {
|
|
693
|
+
Napi::Value g = o.Get("contentsGravity");
|
|
694
|
+
s.gravity = g.IsString() ? ToNSString(g) : nil;
|
|
695
|
+
s.badGravity = !CALGravityKnown(s.gravity);
|
|
696
|
+
}
|
|
651
697
|
return s;
|
|
652
698
|
}
|
|
653
699
|
|
|
@@ -677,6 +723,7 @@ static void ApplyLayerProps(CALayer* L, const LayerPropsSpec& s) {
|
|
|
677
723
|
CALNoteContentsReplaced(L, nil);
|
|
678
724
|
L.contents = nil;
|
|
679
725
|
}
|
|
726
|
+
if (s.hasGravity) L.contentsGravity = CALGravity(L, s.gravity);
|
|
680
727
|
}
|
|
681
728
|
|
|
682
729
|
static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
|
|
@@ -687,6 +734,14 @@ static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
|
|
|
687
734
|
.ThrowAsJavaScriptException();
|
|
688
735
|
return info.Env().Undefined();
|
|
689
736
|
}
|
|
737
|
+
if (s.badGravity) {
|
|
738
|
+
Napi::TypeError::New(info.Env(),
|
|
739
|
+
"contentsGravity: resize, resizeAspect, resizeAspectFill, center, "
|
|
740
|
+
"top, bottom, left, right, topLeft, topRight, bottomLeft or "
|
|
741
|
+
"bottomRight")
|
|
742
|
+
.ThrowAsJavaScriptException();
|
|
743
|
+
return info.Env().Undefined();
|
|
744
|
+
}
|
|
690
745
|
CALOnLayers(^{
|
|
691
746
|
CALayer* L = CALResolve(target);
|
|
692
747
|
if (L) ApplyLayerProps(L, s);
|
|
@@ -1192,6 +1247,8 @@ static Napi::Value RemoveAllAnimations(const Napi::CallbackInfo& info) {
|
|
|
1192
1247
|
static Napi::Value JSFromCAValue(Napi::Env env, id v) {
|
|
1193
1248
|
if (!v) return env.Null();
|
|
1194
1249
|
if ([v isKindOfClass:[NSNumber class]]) return Napi::Number::New(env, [(NSNumber*)v doubleValue]);
|
|
1250
|
+
// a name — `contentsGravity` is Core Animation's own, in the layer's space
|
|
1251
|
+
if ([v isKindOfClass:[NSString class]]) return Napi::String::New(env, [(NSString*)v UTF8String]);
|
|
1195
1252
|
if ([v isKindOfClass:[NSValue class]]) {
|
|
1196
1253
|
NSValue* nv = (NSValue*)v;
|
|
1197
1254
|
const char* t = nv.objCType;
|
|
@@ -1261,8 +1318,8 @@ static Napi::Value PresentationValue(const Napi::CallbackInfo& info) {
|
|
|
1261
1318
|
v = nil;
|
|
1262
1319
|
}
|
|
1263
1320
|
}
|
|
1264
|
-
// an NSNumber, an NSValue or a
|
|
1265
|
-
// caller's thread
|
|
1321
|
+
// an NSNumber, an NSValue, a CGColor or a name: immutable, made into JS
|
|
1322
|
+
// on the caller's thread
|
|
1266
1323
|
return ^Napi::Value(Napi::Env e) { return JSFromCAValue(e, v); };
|
|
1267
1324
|
});
|
|
1268
1325
|
}
|
package/src/backend.mm
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
#include <mutex>
|
|
32
32
|
#include <string>
|
|
33
33
|
#include <unordered_map>
|
|
34
|
-
#include <
|
|
34
|
+
#include <algorithm>
|
|
35
35
|
#include <vector>
|
|
36
36
|
|
|
37
37
|
#include "channel.h"
|
|
@@ -5593,8 +5593,41 @@ struct CALLine {
|
|
|
5593
5593
|
// (BDrawGlyphsAtScale)
|
|
5594
5594
|
bool bitmapsAtScale = false;
|
|
5595
5595
|
std::vector<CALRun> runs;
|
|
5596
|
+
// A justified line (`justify`): its word separators' left edges and
|
|
5597
|
+
// widths as the typesetter set them, from the line's origin, in visual
|
|
5598
|
+
// order, and the room each was widened by. Everything to a separator's
|
|
5599
|
+
// right is that much further right, which every reader of the line adds
|
|
5600
|
+
// (JustifiedX) or takes away (UnjustifiedX).
|
|
5601
|
+
double justifyExtra = 0;
|
|
5602
|
+
std::vector<double> sepX, sepW;
|
|
5596
5603
|
};
|
|
5597
5604
|
|
|
5605
|
+
// Where a point of a justified line is, `ux` as the typesetter set it: the
|
|
5606
|
+
// room of every separator left of it added. A caret at a separator's left
|
|
5607
|
+
// edge is before it, and one at its right edge after it.
|
|
5608
|
+
static double JustifiedX(const CALLine& L, double ux) {
|
|
5609
|
+
if (L.justifyExtra <= 0) return ux;
|
|
5610
|
+
size_t k = std::lower_bound(L.sepX.begin(), L.sepX.end(), ux - 1e-6) -
|
|
5611
|
+
L.sepX.begin();
|
|
5612
|
+
return ux + L.justifyExtra * (double)k;
|
|
5613
|
+
}
|
|
5614
|
+
|
|
5615
|
+
// The inverse, for a hit test: a point inside a widened separator is in it,
|
|
5616
|
+
// at its share of the separator's own advance.
|
|
5617
|
+
static double UnjustifiedX(const CALLine& L, double x) {
|
|
5618
|
+
if (L.justifyExtra <= 0) return x;
|
|
5619
|
+
const double e = L.justifyExtra;
|
|
5620
|
+
for (size_t i = 0; i < L.sepX.size(); i++) {
|
|
5621
|
+
const double left = L.sepX[i] + e * (double)i;
|
|
5622
|
+
if (x < left) return x - e * (double)i;
|
|
5623
|
+
const double width = L.sepW[i] + e;
|
|
5624
|
+
if (x < left + width) {
|
|
5625
|
+
return L.sepX[i] + (x - left) * (width > 0 ? L.sepW[i] / width : 0);
|
|
5626
|
+
}
|
|
5627
|
+
}
|
|
5628
|
+
return x - e * (double)L.sepX.size();
|
|
5629
|
+
}
|
|
5630
|
+
|
|
5598
5631
|
struct CALLayout {
|
|
5599
5632
|
std::vector<CALLine> lines;
|
|
5600
5633
|
double width = 0, height = 0;
|
|
@@ -5778,10 +5811,67 @@ static long UnfitClusterRunsOn(CTTypesetterRef ts, NSString* text,
|
|
|
5778
5811
|
return std::max(count, end - start);
|
|
5779
5812
|
}
|
|
5780
5813
|
|
|
5814
|
+
// A line set to fill `free` more of its width (`justify`): what it leaves
|
|
5815
|
+
// shared equally among its word separators, the spaces and no-break spaces
|
|
5816
|
+
// before the white space it ends on, which hangs and takes none — CSS Text
|
|
5817
|
+
// 3's `text-justify: auto` for the scripts that space their words, as
|
|
5818
|
+
// ntk's layout sets it. Not CTLineCreateJustifiedLine: CoreText spaces the
|
|
5819
|
+
// letters too once a line's spaces have taken some share of their own
|
|
5820
|
+
// advance, where a browser widens nothing but its separators, and gives the
|
|
5821
|
+
// space a line ends on no advance at all. The line keeps the typesetter's
|
|
5822
|
+
// glyphs; where they are drawn, measured and hit is moved by the separators
|
|
5823
|
+
// left of them (JustifiedX). False where it has no separator.
|
|
5824
|
+
static bool JustifyLine(CALLine& L, NSString* text, long start, long end,
|
|
5825
|
+
double free, std::vector<long>* sepIdx) {
|
|
5826
|
+
long hang = end;
|
|
5827
|
+
while (hang > start) {
|
|
5828
|
+
unichar c = [text characterAtIndex:(NSUInteger)(hang - 1)];
|
|
5829
|
+
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != 0x2028 &&
|
|
5830
|
+
c != 0x2029)
|
|
5831
|
+
break;
|
|
5832
|
+
hang--;
|
|
5833
|
+
}
|
|
5834
|
+
std::vector<std::pair<double, double>> seps;
|
|
5835
|
+
std::vector<CGPoint> positions;
|
|
5836
|
+
CFArrayRef runs = CTLineGetGlyphRuns(L.line);
|
|
5837
|
+
for (CFIndex ri = 0; ri < CFArrayGetCount(runs); ri++) {
|
|
5838
|
+
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, ri);
|
|
5839
|
+
const CFIndex count = CTRunGetGlyphCount(run);
|
|
5840
|
+
if (count <= 0) continue;
|
|
5841
|
+
std::vector<CFIndex> indices((size_t)count);
|
|
5842
|
+
CTRunGetStringIndices(run, CFRangeMake(0, 0), indices.data());
|
|
5843
|
+
BRunPositions(run, &positions);
|
|
5844
|
+
for (CFIndex g = 0; g < count; g++) {
|
|
5845
|
+
const long at = (long)indices[(size_t)g];
|
|
5846
|
+
if (at < start || at >= hang) continue;
|
|
5847
|
+
unichar c = [text characterAtIndex:(NSUInteger)at];
|
|
5848
|
+
if (c != ' ' && c != 0xA0) continue;
|
|
5849
|
+
// one glyph for the one character: a space shaped into anything else
|
|
5850
|
+
// is no separator to widen
|
|
5851
|
+
if ((g + 1 < count && indices[(size_t)g + 1] == indices[(size_t)g]) ||
|
|
5852
|
+
(g > 0 && indices[(size_t)g - 1] == indices[(size_t)g]))
|
|
5853
|
+
continue;
|
|
5854
|
+
const double w = CTRunGetTypographicBounds(run, CFRangeMake(g, 1),
|
|
5855
|
+
nullptr, nullptr, nullptr);
|
|
5856
|
+
seps.push_back({positions[(size_t)g].x, w});
|
|
5857
|
+
sepIdx->push_back(at);
|
|
5858
|
+
}
|
|
5859
|
+
}
|
|
5860
|
+
if (seps.empty()) return false;
|
|
5861
|
+
std::sort(seps.begin(), seps.end());
|
|
5862
|
+
L.justifyExtra = free / (double)seps.size();
|
|
5863
|
+
for (const auto& sep : seps) {
|
|
5864
|
+
L.sepX.push_back(sep.first);
|
|
5865
|
+
L.sepW.push_back(sep.second);
|
|
5866
|
+
}
|
|
5867
|
+
std::sort(sepIdx->begin(), sepIdx->end());
|
|
5868
|
+
return true;
|
|
5869
|
+
}
|
|
5870
|
+
|
|
5781
5871
|
// createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
|
|
5782
5872
|
// maxWidth?, align: 0 left | 0.5 center | 1 right,
|
|
5783
5873
|
// lineHeight?, maxLines?, ellipsis?, rtl?,
|
|
5784
|
-
// keep?, typesetter?, packed? })
|
|
5874
|
+
// keep?, typesetter?, packed?, justify? })
|
|
5785
5875
|
// -> { handle, width, height,
|
|
5786
5876
|
// lines: [{x,y,width,height,baseline,descent,start,end,
|
|
5787
5877
|
// runs:[{x,width,start,end,rtl}]}],
|
|
@@ -5796,6 +5886,16 @@ static long UnfitClusterRunsOn(CTTypesetterRef ts, NSString* text,
|
|
|
5796
5886
|
// x, y, width, height, baseline, ascent, descent, start, end and how many
|
|
5797
5887
|
// runs it has — and `runData`, five a run in line order — x, width, start,
|
|
5798
5888
|
// end, and 1 when it runs right to left.
|
|
5889
|
+
//
|
|
5890
|
+
// `justify` sets lines to fill `maxWidth` at their word separators
|
|
5891
|
+
// (JustifyLine), as bits: 1 for every line that goes on to another, 2 for
|
|
5892
|
+
// the paragraph's last and each a forced break ends — a line feed, a
|
|
5893
|
+
// carriage return or a paragraph separator, and not a line separator. A
|
|
5894
|
+
// line an ellipsis ends is not justified, and one with no separator keeps
|
|
5895
|
+
// `align`'s place. Its width is then `maxWidth`, and its runs, its carets,
|
|
5896
|
+
// its hit tests and its drawing are where the wider separators put them.
|
|
5897
|
+
// The breaks are the typesetter's at the width: a kept typesetter (`keep`)
|
|
5898
|
+
// is broken and spaced again at another, and nothing is shaped.
|
|
5799
5899
|
static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
|
|
5800
5900
|
Napi::Env env = info.Env();
|
|
5801
5901
|
Napi::Object o = info[0].As<Napi::Object>();
|
|
@@ -5812,6 +5912,7 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
|
|
|
5812
5912
|
bool rtl = BBoolOr(o, "rtl", false);
|
|
5813
5913
|
bool keep = BBoolOr(o, "keep", false);
|
|
5814
5914
|
bool packed = BBoolOr(o, "packed", false);
|
|
5915
|
+
const int justify = (int)BNumOr(o, "justify", 0);
|
|
5815
5916
|
if (ellipsis && maxLines <= 0) maxLines = 1;
|
|
5816
5917
|
|
|
5817
5918
|
CALTypesetter* shaped = nullptr;
|
|
@@ -5906,13 +6007,28 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
|
|
|
5906
6007
|
L.baseline = y + halfLeading + ascent;
|
|
5907
6008
|
L.start = start;
|
|
5908
6009
|
L.end = lineEnd;
|
|
6010
|
+
bool stops = false;
|
|
5909
6011
|
if (lineEnd > start) {
|
|
5910
6012
|
unichar last = [[as string] characterAtIndex:(NSUInteger)(lineEnd - 1)];
|
|
5911
6013
|
L.hardBreak =
|
|
5912
6014
|
last == '\n' || last == '\r' || last == 0x2028 || last == 0x2029;
|
|
6015
|
+
stops = last == '\n' || last == '\r' || last == 0x2029;
|
|
6016
|
+
}
|
|
6017
|
+
// the line's place in the paragraph, for `justify`: its last, which a
|
|
6018
|
+
// `maxLines` cut leaves no line, or one a forced break ends
|
|
6019
|
+
const int role = lineEnd >= total || stops ? 2 : 1;
|
|
6020
|
+
std::vector<long> sepIdx;
|
|
6021
|
+
double spread = 0;
|
|
6022
|
+
if (bounded && (justify & role) && !elided && maxWidth - lw > 0 &&
|
|
6023
|
+
JustifyLine(L, as.string, start, lineEnd, maxWidth - lw,
|
|
6024
|
+
&sepIdx)) {
|
|
6025
|
+
spread = maxWidth - lw;
|
|
6026
|
+
L.width = maxWidth;
|
|
5913
6027
|
}
|
|
5914
6028
|
if (bounded && flush > 0) {
|
|
5915
|
-
|
|
6029
|
+
// as wide as the width now, where it was justified
|
|
6030
|
+
L.x = CTLineGetPenOffsetForFlush(line, flush, maxWidth) -
|
|
6031
|
+
flush * spread;
|
|
5916
6032
|
}
|
|
5917
6033
|
// runs, for selection bands
|
|
5918
6034
|
CFArrayRef runs = CTLineGetGlyphRuns(line);
|
|
@@ -5943,15 +6059,23 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
|
|
|
5943
6059
|
}
|
|
5944
6060
|
}
|
|
5945
6061
|
CALRun R;
|
|
5946
|
-
R.x = rx;
|
|
6062
|
+
R.x = JustifiedX(L, rx);
|
|
5947
6063
|
R.width = rwidth;
|
|
5948
6064
|
R.start = range.location;
|
|
5949
6065
|
R.end = range.location + range.length;
|
|
6066
|
+
if (!sepIdx.empty()) {
|
|
6067
|
+
// and as much wider as its separators were widened
|
|
6068
|
+
const auto from =
|
|
6069
|
+
std::lower_bound(sepIdx.begin(), sepIdx.end(), (long)R.start);
|
|
6070
|
+
const auto to =
|
|
6071
|
+
std::lower_bound(sepIdx.begin(), sepIdx.end(), (long)R.end);
|
|
6072
|
+
R.width += L.justifyExtra * (double)(to - from);
|
|
6073
|
+
}
|
|
5950
6074
|
R.rtl = (CTRunGetStatus(run) & kCTRunStatusRightToLeft) != 0;
|
|
5951
6075
|
L.runs.push_back(R);
|
|
5952
6076
|
}
|
|
5953
6077
|
layout->lines.push_back(L);
|
|
5954
|
-
layout->width = std::max(layout->width,
|
|
6078
|
+
layout->width = std::max(layout->width, L.width);
|
|
5955
6079
|
y += advance;
|
|
5956
6080
|
lineIndex++;
|
|
5957
6081
|
start = lineEnd;
|
|
@@ -6053,7 +6177,8 @@ static Napi::Value DrawLayout(const Napi::CallbackInfo& info) {
|
|
|
6053
6177
|
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1, -1));
|
|
6054
6178
|
std::vector<CGPoint> positions;
|
|
6055
6179
|
for (const CALLine& L : layout->lines) {
|
|
6056
|
-
|
|
6180
|
+
const bool justified = L.justifyExtra > 0;
|
|
6181
|
+
if (!L.bitmapsAtScale && !justified) {
|
|
6057
6182
|
CGContextSetTextPosition(ctx, x + L.x, y + L.baseline);
|
|
6058
6183
|
CTLineDraw(L.line, ctx);
|
|
6059
6184
|
continue;
|
|
@@ -6061,12 +6186,13 @@ static Napi::Value DrawLayout(const Napi::CallbackInfo& info) {
|
|
|
6061
6186
|
// a line with a scaled face's bitmap glyphs in it is drawn a run at a
|
|
6062
6187
|
// time, and a scaled run at its face's point size under a scaled CTM:
|
|
6063
6188
|
// CTRunDraw, unlike CTLineDraw, does not draw through the font matrix
|
|
6064
|
-
// either, outlines included
|
|
6189
|
+
// either, outlines included. A justified line is drawn a run at a time
|
|
6190
|
+
// too, each glyph where its line's separators move it (JustifiedX).
|
|
6065
6191
|
CFArrayRef runs = CTLineGetGlyphRuns(L.line);
|
|
6066
6192
|
for (CFIndex ri = 0; ri < CFArrayGetCount(runs); ri++) {
|
|
6067
6193
|
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, ri);
|
|
6068
6194
|
CTFontRef font = BRunFont(run);
|
|
6069
|
-
if (!font || BFontScale(font) == 1) {
|
|
6195
|
+
if (!font || (BFontScale(font) == 1 && !justified)) {
|
|
6070
6196
|
CGContextSetTextPosition(ctx, x + L.x, y + L.baseline);
|
|
6071
6197
|
CTRunDraw(run, ctx, CFRangeMake(0, 0));
|
|
6072
6198
|
continue;
|
|
@@ -6076,6 +6202,9 @@ static Napi::Value DrawLayout(const Napi::CallbackInfo& info) {
|
|
|
6076
6202
|
std::vector<CGGlyph> glyphs((size_t)count);
|
|
6077
6203
|
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs.data());
|
|
6078
6204
|
BRunPositions(run, &positions);
|
|
6205
|
+
if (justified) {
|
|
6206
|
+
for (CGPoint& p : positions) p.x = JustifiedX(L, p.x);
|
|
6207
|
+
}
|
|
6079
6208
|
CGContextSaveGState(ctx);
|
|
6080
6209
|
CGColorRef ink = (CGColorRef)CFDictionaryGetValue(
|
|
6081
6210
|
CTRunGetAttributes(run), kCTForegroundColorAttributeName);
|
|
@@ -6296,7 +6425,7 @@ static Napi::Value DrawLayoutGradient(const Napi::CallbackInfo& info) {
|
|
|
6296
6425
|
BRunPositions(run, &positions);
|
|
6297
6426
|
for (CFIndex g = 0; g < count; g++) {
|
|
6298
6427
|
CGAffineTransform t = {1, 0, 0, -1,
|
|
6299
|
-
x + L.x + positions[(size_t)g].x,
|
|
6428
|
+
x + L.x + JustifiedX(L, positions[(size_t)g].x),
|
|
6300
6429
|
y + L.baseline - positions[(size_t)g].y};
|
|
6301
6430
|
CGPathRef gp = CTFontCreatePathForGlyph(font, glyphs[(size_t)g], &t);
|
|
6302
6431
|
if (gp) {
|
|
@@ -6572,7 +6701,7 @@ static Napi::Value LayoutCoverage(const Napi::CallbackInfo& info) {
|
|
|
6572
6701
|
if (glyphs[g] == kCGFontIndexInvalid) continue;
|
|
6573
6702
|
// positions are from the line's origin, y up; outlines are y up
|
|
6574
6703
|
// from the glyph's origin, flipped here into canvas space
|
|
6575
|
-
const double ox = pad + L.x + positions[g].x;
|
|
6704
|
+
const double ox = pad + L.x + JustifiedX(L, positions[g].x);
|
|
6576
6705
|
const double oy = pad + L.baseline - positions[g].y;
|
|
6577
6706
|
const CGAffineTransform t = {1, 0, 0, -1, ox, oy};
|
|
6578
6707
|
CGPathRef outline = CTFontCreatePathForGlyph(font, glyphs[g], &t);
|
|
@@ -6667,8 +6796,8 @@ static Napi::Value LayoutIndexAt(const Napi::CallbackInfo& info) {
|
|
|
6667
6796
|
break;
|
|
6668
6797
|
}
|
|
6669
6798
|
}
|
|
6670
|
-
CFIndex idx =
|
|
6671
|
-
|
|
6799
|
+
CFIndex idx = CTLineGetStringIndexForPosition(
|
|
6800
|
+
pick->line, CGPointMake(UnjustifiedX(*pick, x - pick->x), 0));
|
|
6672
6801
|
if (idx == kCFNotFound) idx = pick->end;
|
|
6673
6802
|
// Trailing-newline aware, ntk's contract: a hit at or past the right edge
|
|
6674
6803
|
// of a hard-wrapped line answers the end of its VISIBLE content. The index
|
|
@@ -6705,7 +6834,8 @@ static Napi::Value LayoutCaret(const Napi::CallbackInfo& info) {
|
|
|
6705
6834
|
}
|
|
6706
6835
|
}
|
|
6707
6836
|
const CALLine* pick = &layout->lines[li];
|
|
6708
|
-
double x =
|
|
6837
|
+
double x =
|
|
6838
|
+
JustifiedX(*pick, CTLineGetOffsetForStringIndex(pick->line, idx, NULL));
|
|
6709
6839
|
r.Set("x", pick->x + x);
|
|
6710
6840
|
r.Set("y", pick->y);
|
|
6711
6841
|
r.Set("height", pick->height);
|