processing 0.5.31 → 0.5.32

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f74daae97a7e307834facbc6eb147a08dbdc731afdb738674161db47eccd04ef
4
- data.tar.gz: 41324b1b967f4e3e5c11392ecf1a36beb82f9bbd4a2bb97dbc6d297b572bbe16
3
+ metadata.gz: f96427cef5e2add31fe4baa3c6055a94bdff9d126d1ae194cd937c24066d4af4
4
+ data.tar.gz: a4a797bb6246dbb1de3450d6a802ec273553cdf4d5c4b8b2a87e82160f8a22b4
5
5
  SHA512:
6
- metadata.gz: dace99fcdae4a94a38942d40327fe0e27251e4ea0cb5ba1c1b19683c45a92690124449b61fd47980389730350e8ccda180076e29cf99678a03a2a8408d0be03e
7
- data.tar.gz: a60cd201501e210f71ef6c163e0493877e6aed90ecae82c344d6f3e62238fa6091e3d1a17755098a3b7722b56f812fa6aa4ca1aa78de02560264f2fa30c41003
6
+ metadata.gz: 8d39fcf7857c9ad1b10597811adcb23b04f96f1f78e128ff089b2cd7cd1fe4b851ba0ff235813f8946853d940383ec82568300d9a9914ce028fc9d3622d58b4a
7
+ data.tar.gz: 226f9d830533ae9f5836ad447bd84de2bb8939d8a5718d1b43c4f66dc073b776c67c7b5c5ccfa421e9271707a9511568b626a9124b6cfaf65dd61ba95fd88af5
data/ChangeLog.md CHANGED
@@ -1,6 +1,21 @@
1
1
  # processing ChangeLog
2
2
 
3
3
 
4
+ ## [v0.5.32] - 2024-01-08
5
+
6
+ - Add requestImage()
7
+ - Add texture(), textureMode(), and textureWrap()
8
+ - Add loadPixels(), updatePixels(), and pixels()
9
+ - Add curveVertex(), bezierVertex(), and quadraticVertex()
10
+ - Add beginContour() and endContour()
11
+ - Add createFont(), loadFont(), and Font.list()
12
+ - Add Shape#setFill
13
+
14
+ - vertex() can teke UV parameters
15
+ - vertex() records the fill color
16
+ - Drawing shapes with texture is affected by tin() instead of the fill()
17
+
18
+
4
19
  ## [v0.5.31] - 2023-12-09
5
20
 
6
21
  - Add Shape class
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.31
1
+ 0.5.32
@@ -7,7 +7,8 @@ module Processing
7
7
 
8
8
  # @private
9
9
  def initialize(font)
10
- @font = font
10
+ @font = font or raise ArgumentError
11
+ @cachedSizes = {}
11
12
  end
12
13
 
13
14
  # Returns bounding box.
@@ -24,8 +25,8 @@ module Processing
24
25
  # @return [TextBounds] bounding box for text
25
26
  #
26
27
  def textBounds(str, x = 0, y = 0, fontSize = nil)
27
- f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
28
- TextBounds.new x, y, x + f.width(str), y + f.height
28
+ font = getInternal__ fontSize
29
+ TextBounds.new x, y, x + font.width(str), y + font.height
29
30
  end
30
31
 
31
32
  # Returns a string containing a human-readable representation of object.
@@ -36,6 +37,32 @@ module Processing
36
37
  "#<Processing::Font: name:'#{@font.name}' size:#{@font.size}>"
37
38
  end
38
39
 
40
+ # Returns available font names
41
+ #
42
+ # @return [String] font names
43
+ #
44
+ # @see https://processing.org/reference/PFont_list_.html
45
+ #
46
+ def self.list()
47
+ Rays::Font.families.values.flatten
48
+ end
49
+
50
+ # @private
51
+ def getInternal__(size = nil)
52
+ if size
53
+ @cachedSizes[size.to_f] ||= @font.dup.tap {|font| font.size = size}
54
+ else
55
+ @font
56
+ end
57
+ end
58
+
59
+ # @private
60
+ def setSize__(size)
61
+ return if size == @font.size
62
+ @cachedSizes[@font.size] = @font
63
+ @font = getInternal__ size
64
+ end
65
+
39
66
  end# Font
40
67
 
41
68
 
@@ -38,17 +38,6 @@ module Processing
38
38
  endDraw__
39
39
  end
40
40
 
41
- # Saves image to file.
42
- #
43
- # @param filename [String] file name to save image
44
- #
45
- # @return [nil] nil
46
- #
47
- def save(filename)
48
- @image__.save filename
49
- nil
50
- end
51
-
52
41
  end# Graphics
53
42
 
54
43
 
@@ -129,6 +129,18 @@ module Processing
129
129
  # Mode for textAlign().
130
130
  BASELINE = :baseline
131
131
 
132
+ # Mode for textureMode().
133
+ IMAGE = :image
134
+
135
+ # Mode for textureMode().
136
+ NORMAL = :normal
137
+
138
+ # Mode for textureWrap().
139
+ CLAMP = :clamp
140
+
141
+ # Mode for textureWrap().
142
+ REPEAT = :repeat
143
+
132
144
  # Filter type for filter()
133
145
  THRESHOLD = :threshold
134
146
 
@@ -247,6 +259,12 @@ module Processing
247
259
  # @private
248
260
  RAD2DEG__ = 180.0 / Math::PI
249
261
 
262
+ # @private
263
+ FONT_SIZE_DEFAULT__ = 12
264
+
265
+ # @private
266
+ FONT_SIZE_MAX__ = 256
267
+
250
268
  # @private
251
269
  def init__(image, painter)
252
270
  @drawing__ = false
@@ -262,11 +280,12 @@ module Processing
262
280
  @blendMode__ = nil
263
281
  @textAlignH__ = nil
264
282
  @textAlignV__ = nil
283
+ @textFont__ = nil
265
284
  @tint__ = nil
266
285
  @filter__ = nil
286
+ @pixels__ = nil
267
287
  @matrixStack__ = []
268
288
  @styleStack__ = []
269
- @fontCache__ = {}
270
289
 
271
290
  updateCanvas__ image, painter
272
291
 
@@ -280,6 +299,9 @@ module Processing
280
299
  strokeCap ROUND
281
300
  strokeJoin MITER
282
301
  textAlign LEFT
302
+ textFont createFont(nil, nil)
303
+ textureMode IMAGE
304
+ textureWrap CLAMP
283
305
 
284
306
  fill 255
285
307
  stroke 0
@@ -313,7 +335,7 @@ module Processing
313
335
  # @return [Numeric] width
314
336
  #
315
337
  def width()
316
- @image__.width
338
+ getInternal__.width
317
339
  end
318
340
 
319
341
  # Returns the height of the graphics object.
@@ -321,7 +343,7 @@ module Processing
321
343
  # @return [Numeric] height
322
344
  #
323
345
  def height()
324
- @image__.height
346
+ getInternal__.height
325
347
  end
326
348
 
327
349
  # Returns the width of the graphics object in pixels.
@@ -443,17 +465,21 @@ module Processing
443
465
 
444
466
  # @private
445
467
  private def toRGBA__(*args)
446
- a, b, c, d = args
468
+ a, b = args
447
469
  return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
470
+ toRaysColor__(*args).to_a
471
+ end
448
472
 
473
+ # @private
474
+ def toRaysColor__(*args)
475
+ a, b, c, d = args
449
476
  rgba = case args.size
450
477
  when 1, 2 then [a, a, a, b || alphaMax__]
451
478
  when 3, 4 then [a, b, c, d || alphaMax__]
452
479
  else raise ArgumentError
453
480
  end
454
- rgba = rgba.map.with_index {|value, i| value / @colorMaxes__[i]}
455
- color = @hsbColor__ ? Rays::Color.hsv(*rgba) : Rays::Color.new(*rgba)
456
- color.to_a
481
+ rgba = rgba.map.with_index {|value, i| value / @colorMaxes__[i]}
482
+ @hsbColor__ ? Rays::Color.hsv(*rgba) : Rays::Color.new(*rgba)
457
483
  end
458
484
 
459
485
  # @private
@@ -618,6 +644,11 @@ module Processing
618
644
  nil
619
645
  end
620
646
 
647
+ # @private
648
+ def getFill__()
649
+ @painter__.fill
650
+ end
651
+
621
652
  # Disables filling.
622
653
  #
623
654
  # @return [nil] nil
@@ -723,6 +754,11 @@ module Processing
723
754
  @tint__ = nil
724
755
  end
725
756
 
757
+ # @private
758
+ def getTint__()
759
+ @tint__ ? toRGBA__(*@tint__) : 1
760
+ end
761
+
726
762
  # Limits the drawable rectangle.
727
763
  #
728
764
  # The parameters a, b, c, and d are determined by rectMode().
@@ -749,7 +785,9 @@ module Processing
749
785
  nil
750
786
  end
751
787
 
752
- # Sets font.
788
+ # Sets text font.
789
+ #
790
+ # (Passing a font name as the first parameter is deprecated)
753
791
  #
754
792
  # @overload textFont(font)
755
793
  # @overload textFont(name)
@@ -760,11 +798,18 @@ module Processing
760
798
  # @param name [String] font name
761
799
  # @param size [Numeric] font size (max 256)
762
800
  #
763
- # @return [Font] current font
801
+ # @return [nil] nil
764
802
  #
765
- def textFont(font = nil, size = nil)
766
- setFont__ font, size if font || size
767
- Font.new @painter__.font
803
+ def textFont(font, size = nil)
804
+ size = FONT_SIZE_MAX__ if size && size > FONT_SIZE_MAX__
805
+ if font.nil? || font.kind_of?(String)
806
+ font = createFont font, size
807
+ elsif size
808
+ font.setSize__ size
809
+ end
810
+ @textFont__ = font
811
+ @painter__.font = font.getInternal__
812
+ nil
768
813
  end
769
814
 
770
815
  # Sets text size.
@@ -774,8 +819,7 @@ module Processing
774
819
  # @return [nil] nil
775
820
  #
776
821
  def textSize(size)
777
- setFont__ nil, size
778
- nil
822
+ textFont @textFont__, size
779
823
  end
780
824
 
781
825
  def textWidth(str)
@@ -795,16 +839,46 @@ module Processing
795
839
  @textAlignV__ = vertical
796
840
  end
797
841
 
842
+ def texture(image)
843
+ @painter__.texture image&.getInternal__
844
+ nil
845
+ end
846
+
798
847
  # @private
799
- def setFont__(fontOrName, size)
800
- name = case fontOrName
801
- when Font then fontOrName.name
802
- else fontOrName || @painter__.font.name
803
- end
804
- size ||= @painter__.font.size
805
- size = 256 if size > 256
806
- font = @fontCache__[[name, size]] ||= Rays::Font.new name, size
807
- @painter__.font = font
848
+ def drawWithTexture__(&block)
849
+ if @painter__.texture
850
+ @painter__.push fill: getTint__, &block
851
+ else
852
+ block.call
853
+ end
854
+ end
855
+
856
+ # Sets the coordinate space for texture mapping.
857
+ #
858
+ # @param mode [IMAGE, NORMAL] image coordinate, or normalized coordinate
859
+ #
860
+ # @return [nil] nil
861
+ #
862
+ # @see https://processing.org/reference/textureMode_.html
863
+ # @see https://p5js.org/reference/#/p5/textureMode
864
+ #
865
+ def textureMode(mode)
866
+ @painter__.texcoord_mode = mode
867
+ nil
868
+ end
869
+
870
+ # Sets the texture wrapping mode.
871
+ #
872
+ # @param wrap [CLAMP, REPEAT] how texutres behave when go outside of the range
873
+ #
874
+ # @return [nil] nil
875
+ #
876
+ # @see https://processing.org/reference/textureWrap_.html
877
+ # @see https://p5js.org/reference/#/p5/textureWrap
878
+ #
879
+ def textureWrap(wrap)
880
+ @painter__.texcoord_wrap = wrap
881
+ nil
808
882
  end
809
883
 
810
884
  # Sets shader.
@@ -1156,8 +1230,7 @@ module Processing
1156
1230
  def image(img, a, b, c = nil, d = nil)
1157
1231
  assertDrawing__
1158
1232
  x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
1159
- tint = @tint__ ? toRGBA__(*@tint__) : 1
1160
- img.drawImage__ @painter__, x, y, w, h, fill: tint, stroke: :none
1233
+ img.drawImage__ @painter__, x, y, w, h, fill: getTint__, stroke: :none
1161
1234
  nil
1162
1235
  end
1163
1236
 
@@ -1182,11 +1255,13 @@ module Processing
1182
1255
  assertDrawing__
1183
1256
  return nil unless shp.isVisible
1184
1257
 
1185
- if c || d || @shapeMode__ != CORNER
1186
- x, y, w, h = toXYWH__ @shapeMode__, a, b, c || shp.width, d || shp.height
1187
- shp.draw__ @painter__, x, y, w, h
1188
- else
1189
- shp.draw__ @painter__, a, b
1258
+ drawWithTexture__ do |_|
1259
+ if c || d || @shapeMode__ != CORNER
1260
+ x, y, w, h = toXYWH__ @shapeMode__, a, b, c || shp.width, d || shp.height
1261
+ shp.draw__ @painter__, x, y, w, h
1262
+ else
1263
+ shp.draw__ @painter__, a, b
1264
+ end
1190
1265
  end
1191
1266
  nil
1192
1267
  end
@@ -1195,7 +1270,7 @@ module Processing
1195
1270
 
1196
1271
  # Begins drawing complex shapes.
1197
1272
  #
1198
- # @param mode [POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, QUAD_STRIP, TESS]
1273
+ # @param type [POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, QUAD_STRIP, TESS]
1199
1274
  #
1200
1275
  # @return [nil] nil
1201
1276
  #
@@ -1217,9 +1292,10 @@ module Processing
1217
1292
  #
1218
1293
  # @see https://processing.org/reference/beginShape_.html
1219
1294
  #
1220
- def beginShape(mode = nil)
1221
- @shapeMode__, @shapePoints__ = mode, []
1222
- nil
1295
+ def beginShape(type = nil)
1296
+ raise "beginShape() cannot be called twice" if @drawingShape__
1297
+ @drawingShape__ = createShape
1298
+ @drawingShape__.beginShape type
1223
1299
  end
1224
1300
 
1225
1301
  # Ends drawing complex shapes.
@@ -1234,25 +1310,114 @@ module Processing
1234
1310
  # @see https://processing.org/reference/endShape_.html
1235
1311
  #
1236
1312
  def endShape(mode = nil)
1237
- raise "endShape() must be called after beginShape()" unless @shapePoints__
1238
- polygon = Shape.createPolygon__ @shapeMode__, @shapePoints__, mode == CLOSE
1239
- @painter__.polygon polygon if polygon
1240
- @shapeMode__ = @shapePoints__ = nil
1313
+ s = @drawingShape__ or raise "endShape() must be called after beginShape()"
1314
+ s.endShape mode
1315
+ shape s
1316
+ @drawingShape__ = nil
1241
1317
  nil
1242
1318
  end
1243
1319
 
1320
+ # Begins drawing a hole inside shape.
1321
+ #
1322
+ # @return [nil] nil
1323
+ #
1324
+ # @example
1325
+ # beginShape
1326
+ # vertex 10, 10
1327
+ # vertex 10, 50
1328
+ # vertex 50, 50
1329
+ # vertex 90, 10
1330
+ # beginContour
1331
+ # vertex 20, 20
1332
+ # vertex 30, 20
1333
+ # vertex 30, 30
1334
+ # vertex 20, 30
1335
+ # endContour
1336
+ # endShape CLOSE
1337
+ #
1338
+ # @see https://processing.org/reference/beginContour_.html
1339
+ # @see https://p5js.org/reference/#/p5/beginContour
1340
+ #
1341
+ def beginContour()
1342
+ (@drawingShape__ or raise "beginContour() must be called after beginShape()")
1343
+ .beginContour
1344
+ end
1345
+
1346
+ # Ends drawing a hole.
1347
+ #
1348
+ # @return [nil] nil
1349
+ #
1350
+ # @see https://processing.org/reference/endContour_.html
1351
+ # @see https://p5js.org/reference/#/p5/endContour
1352
+ #
1353
+ def endContour()
1354
+ (@drawingShape__ or raise "endContour() must be called after beginShape()")
1355
+ .endContour
1356
+ end
1357
+
1244
1358
  # Append vertex for shape polygon.
1245
1359
  #
1360
+ # @overload vertex(x, y)
1361
+ # @overload vertex(x, y, u, v)
1362
+ #
1246
1363
  # @param x [Numeric] x position of vertex
1247
1364
  # @param y [Numeric] y position of vertex
1365
+ # @param u [Numeric] u texture coordinate of vertex
1366
+ # @param v [Numeric] v texture coordinate of vertex
1248
1367
  #
1249
1368
  # @return [nil] nil
1250
1369
  #
1251
1370
  # @see https://processing.org/reference/vertex_.html
1371
+ # @see https://p5js.org/reference/#/p5/vertex
1372
+ #
1373
+ def vertex(x, y, u = nil, v = nil)
1374
+ (@drawingShape__ or raise "vertex() must be called after beginShape()")
1375
+ .vertex x, y, u, v
1376
+ end
1377
+
1378
+ # Append curve vertex for shape polygon.
1379
+ #
1380
+ # @param x [Numeric] x position of vertex
1381
+ # @param y [Numeric] y position of vertex
1382
+ #
1383
+ # @return [nil] nil
1384
+ #
1385
+ # @see https://processing.org/reference/curveVertex_.html
1386
+ # @see https://p5js.org/reference/#/p5/curveVertex
1387
+ #
1388
+ def curveVertex(x, y)
1389
+ (@drawingShape__ or raise "curveVertex() must be called after beginShape()")
1390
+ .curveVertex x, y
1391
+ end
1392
+
1393
+ # Append bezier vertex for shape polygon.
1394
+ #
1395
+ # @param x [Numeric] x position of vertex
1396
+ # @param y [Numeric] y position of vertex
1252
1397
  #
1253
- def vertex(x, y)
1254
- raise "vertex() must be called after beginShape()" unless @shapePoints__
1255
- @shapePoints__ << x << y
1398
+ # @return [nil] nil
1399
+ #
1400
+ # @see https://processing.org/reference/bezierVertex_.html
1401
+ # @see https://p5js.org/reference/#/p5/bezierVertex
1402
+ #
1403
+ def bezierVertex(x2, y2, x3, y3, x4, y4)
1404
+ (@drawingShape__ or raise "bezierVertex() must be called after beginShape()")
1405
+ .bezierVertex x2, y2, x3, y3, x4, y4
1406
+ end
1407
+
1408
+ # Append quadratic vertex for shape polygon.
1409
+ #
1410
+ # @param x [Numeric] x position of vertex
1411
+ # @param y [Numeric] y position of vertex
1412
+ #
1413
+ # @return [nil] nil
1414
+ #
1415
+ # @see https://processing.org/reference/quadraticVertex_.html
1416
+ # @see https://p5js.org/reference/#/p5/quadraticVertex
1417
+ #
1418
+ def quadraticVertex(cx, cy, x3, y3)
1419
+ (@drawingShape__ or raise "quadraticVertex() must be called after beginShape()")
1420
+ .quadraticVertex cx, cy, x3, y3
1256
1421
  end
1257
1422
 
1258
1423
  # Copies image.
@@ -1296,11 +1461,34 @@ module Processing
1296
1461
  #
1297
1462
  def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
1298
1463
  assertDrawing__
1299
- tint = @tint__ ? toRGBA__(*@tint__) : 1
1300
- img ||= self
1301
- img.drawImage__(
1464
+ (img || self).drawImage__(
1302
1465
  @painter__, sx, sy, sw, sh, dx, dy, dw, dh,
1303
- fill: tint, stroke: :none, blend_mode: mode)
1466
+ fill: getTint__, stroke: :none, blend_mode: mode)
1467
+ end
1468
+
1469
+ # Loads all pixels to the 'pixels' array.
1470
+ #
1471
+ # @return [nil] nil
1472
+ #
1473
+ def loadPixels()
1474
+ @pixels__ = getInternal__.pixels
1475
+ end
1476
+
1477
+ # Update the image pixels with the 'pixels' array.
1478
+ #
1479
+ # @return [nil] nil
1480
+ #
1481
+ def updatePixels()
1482
+ return unless @pixels__
1483
+ getInternal__.pixels = @pixels__
1484
+ @pixels__ = nil
1485
+ end
1486
+
1487
+ # An array of all pixels.
1488
+ # Call loadPixels() before accessing the array.
1489
+ #
1490
+ def pixels()
1491
+ @pixels__
1304
1492
  end
1305
1493
 
1306
1494
  # Saves screen image to file.
@@ -1310,7 +1498,7 @@ module Processing
1310
1498
  # @return [nil] nil
1311
1499
  #
1312
1500
  def save(filename)
1313
- @window__.canvas_image.save filename
1501
+ getInternal__.save filename
1314
1502
  nil
1315
1503
  end
1316
1504
 
@@ -1408,6 +1596,9 @@ module Processing
1408
1596
  @painter__.clip,
1409
1597
  @painter__.blend_mode,
1410
1598
  @painter__.font,
1599
+ @painter__.texture,
1600
+ @painter__.texcoord_mode,
1601
+ @painter__.texcoord_wrap,
1411
1602
  @painter__.shader,
1412
1603
  @hsbColor__,
1413
1604
  @colorMaxes__,
@@ -1418,6 +1609,7 @@ module Processing
1418
1609
  @shapeMode__,
1419
1610
  @textAlignH__,
1420
1611
  @textAlignV__,
1612
+ @textFont__,
1421
1613
  @tint__,
1422
1614
  ]
1423
1615
  block.call if block
@@ -1440,6 +1632,9 @@ module Processing
1440
1632
  @painter__.clip,
1441
1633
  @painter__.blend_mode,
1442
1634
  @painter__.font,
1635
+ @painter__.texture,
1636
+ @painter__.texcoord_mode,
1637
+ @painter__.texcoord_wrap,
1443
1638
  @painter__.shader,
1444
1639
  @hsbColor__,
1445
1640
  @colorMaxes__,
@@ -1450,7 +1645,9 @@ module Processing
1450
1645
  @shapeMode__,
1451
1646
  @textAlignH__,
1452
1647
  @textAlignV__,
1648
+ @textFont__,
1453
1649
  @tint__ = @styleStack__.pop
1650
+ @textFont__.setSize__ @painter__.font.size
1454
1651
  nil
1455
1652
  end
1456
1653
 
@@ -1862,7 +2059,7 @@ module Processing
1862
2059
  rand (low || 0).to_f...(high || 1).to_f
1863
2060
  end
1864
2061
 
1865
- # Creates a new vector.
2062
+ # Creates a new vector object.
1866
2063
  #
1867
2064
  # @overload createVector()
1868
2065
  # @overload createVector(x, y)
@@ -1878,7 +2075,17 @@ module Processing
1878
2075
  Vector.new(*args, context: self)
1879
2076
  end
1880
2077
 
1881
- # Creates a new image.
2078
+ # Creates a new font object.
2079
+ #
2080
+ # @param name [String] font name
2081
+ # @param size [Numeric] font size (max 256)
2082
+ #
2083
+ def createFont(name, size)
2084
+ size = FONT_SIZE_MAX__ if size && size > FONT_SIZE_MAX__
2085
+ Font.new Rays::Font.new(name, size || FONT_SIZE_DEFAULT__)
2086
+ end
2087
+
2088
+ # Creates a new image object.
1882
2089
  #
1883
2090
  # @overload createImage(w, h)
1884
2091
  # @overload createImage(w, h, format)
@@ -1895,7 +2102,7 @@ module Processing
1895
2102
  Image.new Rays::Image.new(w, h, colorspace).paint {background 0, 0}
1896
2103
  end
1897
2104
 
1898
- # Creates a new shape.
2105
+ # Creates a new shape object.
1899
2106
  #
1900
2107
  # @overload createShape()
1901
2108
  # @overload createShape(LINE, x1, y1, x2, y2)
@@ -1924,7 +2131,7 @@ module Processing
1924
2131
 
1925
2132
  # @private
1926
2133
  private def createLineShape__(x1, y1, x2, y2)
1927
- Shape.new Rays::Polygon.lines(x1, y1, x2, y2), context: self
2134
+ Shape.new Rays::Polygon.line(x1, y1, x2, y2), context: self
1928
2135
  end
1929
2136
 
1930
2137
  # @private
@@ -2014,6 +2221,23 @@ module Processing
2014
2221
  Capture.new(*args)
2015
2222
  end
2016
2223
 
2224
+ # Loads font from file.
2225
+ #
2226
+ # @param filename [String] file name to load font file
2227
+ #
2228
+ # @return [Font] loaded font object
2229
+ #
2230
+ # @see https://processing.org/reference/loadFont_.html
2231
+ # @see https://p5js.org/reference/#/p5/loadFont
2232
+ #
2233
+ def loadFont(filename)
2234
+ ext = File.extname filename
2235
+ raise "unsupported font type -- '#{ext}'" unless ext =~ /^\.?(ttf|otf)$/i
2236
+
2237
+ filename = httpGet__ filename, ext if filename =~ %r|^https?://|
2238
+ Font.new Rays::Font.load p filename
2239
+ end
2240
+
2017
2241
  # Loads image.
2018
2242
  #
2019
2243
  # @param filename [String] file name to load image
@@ -2021,11 +2245,39 @@ module Processing
2021
2245
  #
2022
2246
  # @return [Image] loaded image object
2023
2247
  #
2248
+ # @see https://processing.org/reference/loadImage_.html
2249
+ # @see https://p5js.org/reference/#/p5/loadImage
2250
+ #
2024
2251
  def loadImage(filename, extension = nil)
2025
- filename = getImage__ filename, extension if filename =~ %r|^https?://|
2252
+ ext = extension || File.extname(filename)
2253
+ raise "unsupported image type -- '#{ext}'" unless ext =~ /^\.?(png|jpg|gif)$/i
2254
+
2255
+ filename = httpGet__ filename, ext if filename =~ %r|^https?://|
2026
2256
  Image.new Rays::Image.load filename
2027
2257
  end
2028
2258
 
2259
+ # Loads image on a new thread.
2260
+ # When the image is loading, its width and height will be 0.
2261
+ # If an error occurs while loading the image, its width and height wil be -1.
2262
+ #
2263
+ # @param filename [String] file name to load image
2264
+ # @param extension [String] type of image to load (ex. 'png')
2265
+ #
2266
+ # @return [Image] loading image object
2267
+ #
2268
+ # @see https://processing.org/reference/requestImage_.html
2269
+ #
2270
+ def requestImage(filename, extension = nil)
2271
+ img = Image.new nil
2272
+ Thread.new filename, extension do |fn, ext|
2273
+ loaded = loadImage(fn, ext) or raise
2274
+ img.setInternal__ loaded.getInternal__
2275
+ rescue
2276
+ img.setInternal__ nil, true
2277
+ end
2278
+ img
2279
+ end
2280
+
2029
2281
  # Loads shader file.
2030
2282
  #
2031
2283
  # @overload loadShader(fragPath)
@@ -2041,10 +2293,7 @@ module Processing
2041
2293
  end
2042
2294
 
2043
2295
  # @private
2044
- private def getImage__(uri, ext)
2045
- ext ||= File.extname uri
2046
- raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png|jpg|gif)$/i
2047
-
2296
+ private def httpGet__(uri, ext)
2048
2297
  tmpdir = tmpdir__
2049
2298
  path = tmpdir + Digest::SHA1.hexdigest(uri)
2050
2299
  path = path.sub_ext ext