craft-native 0.0.141 → 0.0.142

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.
@@ -13,6 +13,7 @@ import android.graphics.BitmapFactory
13
13
  import android.graphics.Canvas
14
14
  import android.graphics.Color
15
15
  import android.graphics.DashPathEffect
16
+ import android.graphics.Matrix
16
17
  import android.graphics.Paint
17
18
  import android.graphics.Path
18
19
  import android.graphics.Typeface
@@ -217,16 +218,36 @@ private class CraftNativeSvgView(context: Context) : View(context) {
217
218
  setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec))
218
219
  }
219
220
 
221
+ /** viewBox units to the view's pixels, set by each draw. */
222
+ private val viewTransform = Matrix()
223
+ private val inverseTransform = Matrix()
224
+
220
225
  override fun onDraw(canvas: Canvas) {
221
226
  super.onDraw(canvas)
222
227
  val box = viewBox() ?: return
223
228
  if (box.first <= 0 || box.second <= 0) return
224
- val scale = min(width.toFloat() / box.first, height.toFloat() / box.second)
225
- if (!scale.isFinite() || scale <= 0f) return
229
+ // Centred at one scale (SVG's default, `xMidYMid meet`), or each axis
230
+ // stretched to fill (`preserveAspectRatio="none"`).
231
+ val stretch = drawing.optJSONObject("props")?.optString("preserveAspectRatio")?.trim() == "none"
232
+ var sx = width.toFloat() / box.first
233
+ var sy = height.toFloat() / box.second
234
+ var dx = 0f
235
+ var dy = 0f
236
+ if (!stretch) {
237
+ val scale = min(sx, sy)
238
+ sx = scale
239
+ sy = scale
240
+ dx = (width - box.first * scale) / 2f
241
+ dy = (height - box.second * scale) / 2f
242
+ }
243
+ if (!sx.isFinite() || !sy.isFinite() || sx <= 0f || sy <= 0f) return
244
+ viewTransform.reset()
245
+ viewTransform.postTranslate(-box.third, -box.fourth)
246
+ viewTransform.postScale(sx, sy)
247
+ viewTransform.postTranslate(dx, dy)
248
+ viewTransform.invert(inverseTransform)
226
249
  canvas.save()
227
- canvas.translate((width - box.first * scale) / 2f, (height - box.second * scale) / 2f)
228
- canvas.scale(scale, scale)
229
- canvas.translate(-box.third, -box.fourth)
250
+ canvas.concat(viewTransform)
230
251
  drawNodes(canvas, drawing.optJSONArray("children") ?: JSONArray(), SvgPaint().merge(drawing))
231
252
  canvas.restore()
232
253
  }
@@ -310,7 +331,17 @@ private class CraftNativeSvgView(context: Context) : View(context) {
310
331
  }
311
332
  }
312
333
  }
313
- canvas.drawPath(path, stroke)
334
+ // `vector-effect="non-scaling-stroke"`: the stroke is in the view's
335
+ // pixels, so a stretched chart's line keeps its width.
336
+ if (props.optString("vectorEffect") == "non-scaling-stroke") {
337
+ val line = Path(path).apply { transform(viewTransform) }
338
+ canvas.save()
339
+ canvas.concat(inverseTransform)
340
+ canvas.drawPath(line, stroke)
341
+ canvas.restore()
342
+ } else {
343
+ canvas.drawPath(path, stroke)
344
+ }
314
345
  }
315
346
  }
316
347
 
@@ -443,7 +474,8 @@ private class CraftNativeFlexLayout(context: Context) : LinearLayout(context) {
443
474
  }.toFloatArray()
444
475
  val sharedLineIds = CraftNative.flexLines(availableMain.toFloat(), gap.toFloat(), wrap, basis, margins)
445
476
  val lineIds = sharedLineIds?.takeIf { ids ->
446
- ids.size >= pending.size && ids.firstOrNull() == 0 && ids.zipWithNext().all { (left, right) -> right >= left }
477
+ ids.size >= pending.size && ids.firstOrNull() == 0 &&
478
+ ids.indices.drop(1).all { index -> ids[index] >= ids[index - 1] }
447
479
  }
448
480
  var line = Line()
449
481
  pending.forEachIndexed { index, entry ->
package/dist/cli.js CHANGED
@@ -3303,7 +3303,7 @@ function pinCraftNativeDependency(packagePath, craftVersion) {
3303
3303
  `);
3304
3304
  }
3305
3305
  // package.json
3306
- var version = "0.0.141";
3306
+ var version = "0.0.142";
3307
3307
 
3308
3308
  // bin/cli.ts
3309
3309
  var spawnedFrom = process6.env[CRAFT_CLI_SPAWN_MARKER];
package/dist/index.cjs CHANGED
@@ -310,7 +310,7 @@ function craftBinaryNotFoundMessage(triedPath) {
310
310
  `);
311
311
  }
312
312
  // package.json
313
- var version = "0.0.141";
313
+ var version = "0.0.142";
314
314
 
315
315
  // src/sdk-version.ts
316
316
  var SDK_VERSION = version;
package/dist/index.js CHANGED
@@ -58,7 +58,7 @@ function craftBinaryNotFoundMessage(triedPath) {
58
58
  `);
59
59
  }
60
60
  // package.json
61
- var version = "0.0.141";
61
+ var version = "0.0.142";
62
62
 
63
63
  // src/sdk-version.ts
64
64
  var SDK_VERSION = version;
@@ -1789,14 +1789,36 @@ private final class CraftNativeSvgView: UIView {
1789
1789
  }
1790
1790
  }
1791
1791
 
1792
+ /// viewBox units to the view's points, set by each draw.
1793
+ private var viewTransform = CGAffineTransform.identity
1794
+
1795
+ /// viewBox to view: centred at one scale (SVG's default, `xMidYMid meet`),
1796
+ /// or each axis stretched to fill (`preserveAspectRatio="none"`), as a
1797
+ /// chart a page sizes to its width and a fixed height asks for.
1798
+ static func transform(box: CGRect, bounds: CGSize, preserveAspectRatio: String?) -> CGAffineTransform? {
1799
+ var sx = bounds.width / box.width
1800
+ var sy = bounds.height / box.height
1801
+ var dx: CGFloat = 0
1802
+ var dy: CGFloat = 0
1803
+ if preserveAspectRatio?.trimmingCharacters(in: .whitespaces) != "none" {
1804
+ let scale = min(sx, sy)
1805
+ sx = scale
1806
+ sy = scale
1807
+ dx = (bounds.width - box.width * scale) / 2
1808
+ dy = (bounds.height - box.height * scale) / 2
1809
+ }
1810
+ guard sx.isFinite, sy.isFinite, sx > 0, sy > 0 else { return nil }
1811
+ return CGAffineTransform(translationX: dx, y: dy).scaledBy(x: sx, y: sy).translatedBy(x: -box.minX, y: -box.minY)
1812
+ }
1813
+
1792
1814
  override func draw(_ rect: CGRect) {
1793
1815
  guard let context = UIGraphicsGetCurrentContext() else { return }
1816
+ viewTransform = .identity
1794
1817
  if let box = viewBox {
1795
- let scale = min(bounds.width / box.width, bounds.height / box.height)
1796
- guard scale.isFinite, scale > 0 else { return }
1797
- context.translateBy(x: (bounds.width - box.width * scale) / 2, y: (bounds.height - box.height * scale) / 2)
1798
- context.scaleBy(x: scale, y: scale)
1799
- context.translateBy(x: -box.minX, y: -box.minY)
1818
+ let props = drawing["props"] as? [String: Any] ?? [:]
1819
+ guard let transform = CraftNativeSvgView.transform(box: box, bounds: bounds.size, preserveAspectRatio: props["preserveAspectRatio"] as? String) else { return }
1820
+ viewTransform = transform
1821
+ context.concatenate(transform)
1800
1822
  }
1801
1823
  draw(children: drawing["children"] as? [Any] ?? [], paint: Paint().merged(drawing), in: context)
1802
1824
  }
@@ -1820,12 +1842,25 @@ private final class CraftNativeSvgView: UIView {
1820
1842
  path.fill()
1821
1843
  }
1822
1844
  if let stroke = own.resolve(own.stroke), own.strokeWidth > 0 {
1845
+ // `vector-effect="non-scaling-stroke"`: the stroke is in the
1846
+ // view's points, so a stretched chart's line keeps its width
1847
+ // and a round cap stays a round dot.
1848
+ let nonScaling = (shape["props"] as? [String: Any])?["vectorEffect"] as? String == "non-scaling-stroke"
1849
+ let line: UIBezierPath
1850
+ if nonScaling, let copy = path.copy() as? UIBezierPath {
1851
+ copy.apply(viewTransform)
1852
+ context.concatenate(viewTransform.inverted())
1853
+ line = copy
1854
+ }
1855
+ else {
1856
+ line = path
1857
+ }
1823
1858
  stroke.setStroke()
1824
- path.lineWidth = own.strokeWidth
1825
- path.lineCapStyle = own.linecap == "round" ? .round : own.linecap == "square" ? .square : .butt
1826
- path.lineJoinStyle = own.linejoin == "round" ? .round : own.linejoin == "bevel" ? .bevel : .miter
1827
- if let dashes = own.dashes, !dashes.isEmpty { path.setLineDash(dashes, count: dashes.count, phase: 0) }
1828
- path.stroke()
1859
+ line.lineWidth = own.strokeWidth
1860
+ line.lineCapStyle = own.linecap == "round" ? .round : own.linecap == "square" ? .square : .butt
1861
+ line.lineJoinStyle = own.linejoin == "round" ? .round : own.linejoin == "bevel" ? .bevel : .miter
1862
+ if let dashes = own.dashes, !dashes.isEmpty { line.setLineDash(dashes, count: dashes.count, phase: 0) }
1863
+ line.stroke()
1829
1864
  }
1830
1865
  context.restoreGState()
1831
1866
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "craft-native",
3
- "version": "0.0.141",
3
+ "version": "0.0.142",
4
4
  "type": "module",
5
5
  "description": "Build desktop apps with web languages - TypeScript SDK for Craft",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",