defineShapeRect method

void defineShapeRect(
  1. int id, {
  2. required int widthPx,
  3. required int heightPx,
  4. int fillRgb = 0x000000,
})

単色塗りの矩形シェイプをDefineShape(タグ2/バージョン1)として定義する。

Implementation

void defineShapeRect(
  int id, {
  required int widthPx,
  required int heightPx,
  int fillRgb = 0x000000,
}) {
  final widthTwips = widthPx * _twipsPerPixel;
  final heightTwips = heightPx * _twipsPerPixel;

  final bw = BitWriter();
  bw.writeUI16(id);
  _writeRect(bw, 0, widthTwips, 0, heightTwips);

  // fill styles: solid 1個
  bw.writeUI8(1);
  bw.writeUI8(0x00); // solid fill
  bw.writeUI8((fillRgb >> 16) & 0xFF);
  bw.writeUI8((fillRgb >> 8) & 0xFF);
  bw.writeUI8(fillRgb & 0xFF);

  // line styles: なし
  bw.writeUI8(0);

  // shape records: MoveTo省略(原点0,0開始)→ 矩形の辺4本 → End
  final edgeBits =
      _neededSignedBits([widthTwips, heightTwips, -widthTwips, -heightTwips])
          .clamp(2, 17);
  bw.writeUB(4, 1); // numFillBits=1(fillStyle index 1が入る)
  bw.writeUB(4, 0); // numLineBits=0

  // StyleChangeRecord: fillStyle1=1を選択
  bw.writeUB(1, 0); // isEdge=false
  bw.writeUB(5, 0x04); // flags: fillStyle1あり
  bw.writeUB(1, 1); // fillStyle1 = 1 (numFillBits=1bit)

  void edge(int dx, int dy) {
    bw.writeUB(1, 1); // isEdge
    bw.writeUB(1, 1); // isStraight
    bw.writeUB(4, edgeBits - 2);
    bw.writeUB(1, 1); // isGeneral
    bw.writeSB(edgeBits, dx);
    bw.writeSB(edgeBits, dy);
  }

  edge(widthTwips, 0);
  edge(0, heightTwips);
  edge(-widthTwips, 0);
  edge(0, -heightTwips);

  // EndShapeRecord
  bw.writeUB(1, 0);
  bw.writeUB(5, 0);
  bw.align();

  _writeTag(TagCode.defineShape, bw.toBytes());
}