【PDF文法・仕様】Highlight AnnotationのRectとQuadPoints

PDFのHighlight Annotationのうち、`Rect`と`QuadPoints`についてリバースエンジニアリングを試みます。

:::note
PDF文法そのものはこちらのサイトが分かりやすいです: [詳細PDF入門 ー 実装して学ぼう！PDFファイルの構造とその書き方読み方](https://itchyny.hatenablog.com/entry/2015/09/16/100000)

PDF公式仕様書はこちら: https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
:::

## 材料
### OS
Microsoft Windows 11 Home
Version	10.0.26100 Build 26100

### Chrome
Version 133.0.6943.142 (Official Build) (64-bit)

### Adobe Acrobat
Continuous Release | 2024.005.20421 | 64 bit

### 使用するPDFサンプル
Highlight Annotationを使用したミニマルサンプル（不完全データ）
```test.pdf
%PDF-1.7

1 0 obj  % entry point
<<
  /Type /Catalog
  /Pages 2 0 R
>>
endobj

2 0 obj
<<
  /Type /Pages
  /MediaBox [ 0 0 200 400 ]
  /Count 1
  /Kids [ 3 0 R ]
>>
endobj

3 0 obj
<<
  /Type /Page
  /Parent 2 0 R
  /Resources <<
    /Font <<
      /F1 4 0 R 
    >>
  >>
  /Contents 5 0 R
  /Annots [6 0 R]
>>
endobj

4 0 obj
<<
  /Type /Font
  /Subtype /Type1
  /BaseFont /Times-Roman
>>
endobj

5 0 obj  % page content
<<
  /Length 44
>>
stream
BT
70 50 TD
/F1 12 Tf
(Hello, world!) Tj
ET
endstream
endobj

6 0 obj
<<
  /Type /Annot 
  /Rect [0 0 100 100 ] 
  /Subtype /Highlight 
  /QuadPoints [69.9398 58.8418 96.5932 58.8418 69.9398 47.3526 96.5932 47.3526 ] 
  /M (D:20190101153417)  
  /Contents (aaa)  
  /F 4 
  /C [1 1 0 ] 
  /CA 1 
  /Border [0 0 1 ] 
  /P 3 0 R 
>>
endobj

xref
trailer
<<
  /Size 6
  /Root 1 0 R
>>

startxref

%%EOF
```

これに対して、[pdftk](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/)を使用して、完璧にします。（クロスリファレンス他）

`pdftk`使用コマンドライン
```
pdftk test.pdf output test_out.pdf
```

この状態でのChromeの見え方はこう。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/fe4d9830-9480-43d5-be13-1ad6886e0ca1.png)

Acrobatでの見え方はこう。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/6003f150-c96f-4869-b264-6205c82e2459.png)

## Rect

Rectをいじったところ、結論としてはハイライトの変更はない。Chromeにおいて、カーソルオーバーしたときのコメント表示の位置が変動した。

一方、AcrobatのコメントはRectではなくカーソル位置に依存（大きさは不変）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/6003f150-c96f-4869-b264-6205c82e2459.png)


### Chromeでの挙動

```
/Rect [0 0 100 100 ] 
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/263ee4b0-c6ee-4c61-bd0d-4c05080977df.png)

```
/Rect [50 0 100 100 ] 
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/37d0bd5f-ab78-4f9a-8e2f-447a7deb4830.png)

```
/Rect [0 50 100 100 ] 
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/96bbfe9c-d47e-4bbc-9e62-c5a911c85340.png)

```
/Rect [0 0 50 100 ] 
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/91fc2f73-a531-48e0-a83b-f3ea333498f2.png)

```
/Rect [0 0 100 50 ] 
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/8d1cf486-69c0-4b0c-ba0c-b833d006da1e.png)

### Acrobatの挙動

```
/Rect [0 0 100 100 ] 
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/6003f150-c96f-4869-b264-6205c82e2459.png)

```
/Rect [1 1 100 100 ] 
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/7539ce9c-906a-4515-a2a7-97738c16b1ca.png)

...法則性が分からないですな。

## QuadPoint

QuadPointsの順番は、仕様書と異なって、下記が実態になっている
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/15113a62-d49a-4e80-ab81-8623df4e7c24.png)

参考: https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints

### 台形
```
/QuadPoints [0 58.8418 96.5932 58.8418 69.9398 47.3526 96.5932 47.3526 ] 
```

Chrome
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/fa593f99-d111-4582-a088-32f8cbcb376f.png)

Acrobat
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/fc34c4b8-8053-496e-a8e6-9940ec7be21e.png)

### 伸ばす
```
/QuadPoints [0 58.8418 96.5932 58.8418 0 47.3526 96.5932 47.3526 ] 
```

Chrome
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/c4209e96-3192-488b-b75c-07944281fc1d.png)

Acrobat
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/d7d8522a-0d9c-4631-954c-2a33085269b1.png)

### 蝶つがい
```
 /QuadPoints [69.9398 0 96.5932 58.8418 69.9398 47.3526 96.5932 47.3526 ] 
```

Chrome
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/be31cfdf-9141-48a1-a869-f1a8a388fdf4.png)

Acrobat
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/52e37674-80b7-4a48-8766-c500ba6ab31d.png)

### 裏返し
```
/QuadPoints [69.9398 0 96.5932 0 69.9398 47.3526 96.5932 47.3526 ]
```

Chrome
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/785b9757-e316-4c3a-b135-a2c400d76299.png)

Acrobat
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2602537/f407d7ee-db4f-4da6-9e47-bfbd666a1ccd.png)