# 図形

- Human documentation: [https://docs.univer.ai/ja-JP/guides/docs/features/shape](https://docs.univer.ai/ja-JP/guides/docs/features/shape)

- Agent Markdown: [https://docs.univer.ai/ja-JP/guides/docs/features/shape.md](https://docs.univer.ai/ja-JP/guides/docs/features/shape.md)

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.1.0`

- Source: [docs/features/shape.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/features/shape.ja-JP.mdx)

---

#### Package metadata

```json
{
  "mobile": true,
  "plugins": [
    {
      "client": "@univerjs-pro/engine-formula"
    },
    {
      "client": "@univerjs-pro/docs-shape"
    },
    {
      "client": "@univerjs-pro/docs-shape-ui",
      "locale": "@univerjs-pro/docs-shape-ui/locale/ja-JP",
      "style": "@univerjs-pro/docs-shape-ui/lib/index.css"
    }
  ],
  "license": true,
  "server": false
}
```

図形機能を使用すると、ドキュメント内に図形や描画を挿入して編集できます。基本的な幾何学的図形、矢印、フローチャート要素などが含まれます。

## はじめに

Univer Docs では、図形を使用して以下ができます：

* **幾何学的図形を挿入**：長方形、円、矢印などを追加します。
* **外観をカスタマイズ**：塗りつぶし色、枠線、テキストを調整します。
* **自由に配置**：図形をドキュメント内の任意の場所に配置します。

## プラグインモード

### インストール

#### npm

```bash
npm install @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-ui
```

#### pnpm

```bash
pnpm add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-ui
```

#### yarn

```bash
yarn add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-ui
```

#### bun

```bash
bun add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-ui
```

> [!WARN]
> 図形には `@univerjs-pro/engine-formula` の `UniverProFormulaEnginePlugin` も必要です。基本の初期化コードで、既存の `UniverFormulaEnginePlugin` のインポートと登録をこのプラグインに置き換えてください。両方の数式エンジンを登録すると、図形の挿入時に依存関係が未登録というエラーが発生する場合があります。

### 使用法

```typescript
import { UniverDocsShapePlugin } from '@univerjs-pro/docs-shape' // [!code ++]
import { UniverDocsShapeUIPlugin } from '@univerjs-pro/docs-shape-ui' // [!code ++]
import DocsShapeUIJaJP from '@univerjs-pro/docs-shape-ui/locale/ja-JP' // [!code ++]
import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula' // [!code ++]
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'

import '@univerjs-pro/docs-shape-ui/lib/index.css' // [!code ++]

const univer = new Univer({
  locale: LocaleType.JA_JP,
  locales: {
    [LocaleType.JA_JP]: mergeLocales(
      DocsShapeUIJaJP, // [!code ++]
    ),
  },
})

univer.registerPlugin(UniverProFormulaEnginePlugin) // [!code ++]
univer.registerPlugin(UniverDocsShapePlugin) // [!code ++]
univer.registerPlugin(UniverDocsShapeUIPlugin) // [!code ++]
```

Univer の商用ライセンスをお持ちの場合は、[クライアントでのライセンス使用](https://docs.univer.ai/ja-JP/server/license.md#in-plugin-mode)を参照してください。

## モバイル対応

まず[モバイルエディター](https://docs.univer.ai/ja-JP/guides/docs/getting-started/installation.md#mobile-support)を設定します。このページのプラグインモードの初期化コードで、以下のデスクトップ登録を同じパッケージのモバイル版に置き換え、**文書を開く前に**完了してください。モデル、スタイル、言語ファイル、Facade は維持し、両方を同時に登録しないでください。

| パッケージ                         | デスクトップの登録                 | モバイルの登録                         |
| ----------------------------- | ------------------------- | ------------------------------- |
| `@univerjs-pro/docs-shape-ui` | `UniverDocsShapeUIPlugin` | `UniverDocsShapeMobileUIPlugin` |

## 図形のテキストを編集する

以下または前の例で `insertShape()` が返す図形を使用します。`getText()` はライブの [FShapeText](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md) オブジェクトを返します。`getPlainText()` でテキストを読み取り、`setText()` で全文を置き換え、フォント、色、配置を設定できます。

```ts
import '@univerjs-pro/docs-shape/facade'

const document = univerAPI.getActiveDocument()
const paragraph = document?.getParagraphs()[0]
if (!document || !paragraph) throw new Error('No active document paragraph')
const shape = document.insertShape({
  shapeType: univerAPI.Enum.ShapeTypeEnum.Rect,
  placement: {
    wrappingStyle: univerAPI.Enum.TextWrappingStyle.INLINE,
    anchor: { paragraphId: paragraph.getId(), segmentId: paragraph.getSegmentId() },
  },
})
if (!shape) throw new Error('Shape is not available')
const shapeText = shape.getText()
console.log(shapeText.getPlainText())
shapeText
  .setText('Quarterly review')
  .setFontSize(18)
  .setColor('#2563eb')
  .setBold(true)
  .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER)
  .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)
```

`setText()` は既存のテキストを置き換えます。複数の書式を使う場合は `setRichText()` を使用してください。既存のリッチテキストを編集するには `getRichText()?.copy()` でビルダーを取得し、編集後に `setRichText()` に渡します。ビルダーの変更だけでは反映されません。[文字スタイル](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md#settextstyle)と[テキストボックスの余白・レイアウト](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md#settextboxoptions)も参照してください。

```ts
const richText = univerAPI.newRichText()
  .text('Quarterly ')
  .bold('review')
shapeText.setRichText(richText)
```

## 図形の書式コピー

元の図形を選択し、書式コピーを有効にして対象の図形を選びます。塗りつぶし、線、効果、対応する文字書式をコピーし、対象の ID、形状、文字内容、数式の関連付けは保持します。書式のクリアは挿入時の既定値に戻します。SmartArt は対象外です。対応する図形 UI プラグインが自動登録します。
