kumiori 0.2.0 → 0.4.0
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.
- package/CHANGELOG.en.md +201 -2
- package/CHANGELOG.md +202 -3
- package/NOTICE +2 -0
- package/README.en.md +58 -18
- package/README.md +58 -18
- package/THIRD_PARTY_NOTICES.md +364 -26
- package/dist-browser/NOTICE +2 -0
- package/dist-browser/THIRD_PARTY_NOTICES.md +364 -26
- package/dist-browser/chunks/{UTIF-DJ2yEO_P.js → UTIF-Br536cfi.js} +4 -4
- package/dist-browser/chunks/ccitt-DuYpUkox.js +1640 -0
- package/dist-browser/chunks/cgm-Blvzv6Sp.js +2103 -0
- package/dist-browser/chunks/{el-monoton-Bh-cSidd.js → el-monoton-vcesHl--.js} +4 -4
- package/dist-browser/chunks/{en-gb-DLCXyvjY.js → en-gb-DAIgCw5-.js} +4 -4
- package/dist-browser/chunks/{en-us-BBLcTX9N.js → en-us-BzUSGdOw.js} +4 -4
- package/dist-browser/chunks/eps-BFOW93N0.js +5237 -0
- package/dist-browser/chunks/epsInfo-D7b8OFlX.js +565 -0
- package/dist-browser/chunks/{export-contract-DDGE_g3K.js → export-contract-J-x2aLjv.js} +3 -3
- package/dist-browser/chunks/{la-D46x91kc.js → la-DRKqnbhl.js} +4 -4
- package/dist-browser/chunks/load-ftVKpJYM.js +16686 -0
- package/dist-browser/chunks/matrix-DxjvN-4S.js +48 -0
- package/dist-browser/chunks/metafile-CdWE2Vpy.js +1867 -0
- package/dist-browser/chunks/{pako-CeDDB8tF.js → pako-BwlM3ygd.js} +6 -6
- package/dist-browser/chunks/pixels-BvYSA32R.js +714 -0
- package/dist-browser/chunks/{rolldown-runtime-BXCUS5Bd.js → rolldown-runtime-BV67vctc.js} +2 -2
- package/dist-browser/chunks/{ru-CkmoNumx.js → ru-C-K8QVC6.js} +4 -4
- package/dist-browser/chunks/sniff-CqYWlxhA.js +8779 -0
- package/dist-browser/chunks/strokeOutline-yLvGVz8Y.js +1107 -0
- package/dist-browser/chunks/{uk-DIZr4Urv.js → uk-DsJ1E5t7.js} +4 -4
- package/dist-browser/kumiori.js +41493 -52444
- package/dist-cli/NOTICE +2 -0
- package/dist-cli/THIRD_PARTY_NOTICES.md +364 -26
- package/dist-cli/assets/cgm-DNhYr9YE.js +3239 -0
- package/dist-cli/assets/drawioStencil-FTgn0IEy.js +991 -0
- package/dist-cli/assets/eps-L8amQ51W.js +7496 -0
- package/dist-cli/assets/load-O0H7f7JT.js +39125 -0
- package/dist-cli/assets/metafile-xjD5AdL9.js +2522 -0
- package/dist-cli/cli.js +51358 -47290
- package/dist-lib/NOTICE +2 -0
- package/dist-lib/THIRD_PARTY_NOTICES.md +364 -26
- package/dist-lib/{api-CQ3aOtc5.js → api-DG-iGARW.js} +38568 -37973
- package/dist-lib/cgm-CAP3l6Ps.js +2103 -0
- package/dist-lib/eps-BVpP3M6Z.js +5236 -0
- package/dist-lib/epsInfo-D7b8OFlX.js +565 -0
- package/dist-lib/index.js +11 -12
- package/dist-lib/load-WPgVW8G9.js +15328 -0
- package/dist-lib/metafile-DIpYelkX.js +1867 -0
- package/dist-lib/node.js +4 -5
- package/dist-lib/pixels-BvYSA32R.js +714 -0
- package/dist-lib/types/api.d.ts +61 -2
- package/dist-lib/types/areaResources.d.ts +9 -1
- package/dist-lib/types/areaTree.d.ts +3 -1
- package/dist-lib/types/assets.d.ts +78 -3
- package/dist-lib/types/base64.d.ts +7 -0
- package/dist-lib/types/ccitt.d.ts +1 -1
- package/dist-lib/types/cgm.d.ts +2 -2
- package/dist-lib/types/color.d.ts +13 -4
- package/dist-lib/types/colorGlyphPdf.d.ts +11 -1
- package/dist-lib/types/compat.d.ts +23 -2
- package/dist-lib/types/cssCascade.d.ts +35 -1
- package/dist-lib/types/cssSelector.d.ts +80 -2
- package/dist-lib/types/cssText.d.ts +23 -0
- package/dist-lib/types/documentResources.d.ts +2 -1
- package/dist-lib/types/drawio/base64.d.ts +6 -0
- package/dist-lib/types/drawio/drawio.d.ts +137 -0
- package/dist-lib/types/drawio/drawioCanvas.d.ts +156 -0
- package/dist-lib/types/drawio/drawioEdges.d.ts +89 -0
- package/dist-lib/types/drawio/drawioJs/archimate.d.ts +33 -0
- package/dist-lib/types/drawio/drawioJs/arrows.d.ts +37 -0
- package/dist-lib/types/drawio/drawioJs/aws3d.d.ts +18 -0
- package/dist-lib/types/drawio/drawioJs/basic.d.ts +29 -0
- package/dist-lib/types/drawio/drawioJs/bpmn.d.ts +35 -0
- package/dist-lib/types/drawio/drawioJs/edges.d.ts +19 -0
- package/dist-lib/types/drawio/drawioJs/electrical.d.ts +29 -0
- package/dist-lib/types/drawio/drawioJs/misc.d.ts +34 -0
- package/dist-lib/types/drawio/drawioJs/mockup.d.ts +27 -0
- package/dist-lib/types/drawio/drawioJs/mockupIos.d.ts +2 -0
- package/dist-lib/types/drawio/drawioJs/ui.d.ts +34 -0
- package/dist-lib/types/drawio/drawioShapes.d.ts +51 -0
- package/dist-lib/types/drawio/drawioShapesA.d.ts +23 -0
- package/dist-lib/types/drawio/drawioShapesB.d.ts +26 -0
- package/dist-lib/types/drawio/drawioSketch.d.ts +50 -0
- package/dist-lib/types/drawio/drawioStencil.d.ts +159 -0
- package/dist-lib/types/drawio/drawioTapered.d.ts +67 -0
- package/dist-lib/types/drawio/drawioText.d.ts +27 -0
- package/dist-lib/types/drawio/drawioXml.d.ts +40 -0
- package/dist-lib/types/drawio/index.d.ts +37 -0
- package/dist-lib/types/drawio/inflate.d.ts +12 -0
- package/dist-lib/types/drawio/load.d.ts +35 -0
- package/dist-lib/types/drawio/messages.d.ts +12 -0
- package/dist-lib/types/drawio/options.d.ts +44 -0
- package/dist-lib/types/drawio/prefixSums.d.ts +9 -0
- package/dist-lib/types/drawio/sniff.d.ts +16 -0
- package/dist-lib/types/eps.d.ts +2 -28
- package/dist-lib/types/epsInfo.d.ts +36 -0
- package/dist-lib/types/foContext.d.ts +19 -0
- package/dist-lib/types/foExpr.d.ts +7 -0
- package/dist-lib/types/fontMapping.d.ts +14 -0
- package/dist-lib/types/fontSanity.d.ts +15 -0
- package/dist-lib/types/footnotes.d.ts +17 -1
- package/dist-lib/types/geometry.d.ts +5 -0
- package/dist-lib/types/gridTracks.d.ts +51 -0
- package/dist-lib/types/hebrewMarks.d.ts +11 -3
- package/dist-lib/types/htmlImport.d.ts +84 -15
- package/dist-lib/types/htmlPaged.d.ts +20 -2
- package/dist-lib/types/htmlParserStack.d.ts +26 -0
- package/dist-lib/types/htmlPrepare.d.ts +14 -3
- package/dist-lib/types/imageFormats.d.ts +138 -0
- package/dist-lib/types/imageInfo.d.ts +6 -1
- package/dist-lib/types/imageLimits.d.ts +4 -4
- package/dist-lib/types/index.d.ts +9 -3
- package/dist-lib/types/inflateFast.d.ts +12 -0
- package/dist-lib/types/inlineLayout.d.ts +34 -1
- package/dist-lib/types/jis.d.ts +19 -0
- package/dist-lib/types/layoutEngine.d.ts +191 -6
- package/dist-lib/types/linearText.d.ts +69 -0
- package/dist-lib/types/lnode.d.ts +36 -0
- package/dist-lib/types/pageFlow.d.ts +34 -0
- package/dist-lib/types/paint.d.ts +3 -1
- package/dist-lib/types/pdfCheck.d.ts +36 -0
- package/dist-lib/types/pdfFlatten.d.ts +165 -0
- package/dist-lib/types/pdfLibPatches.d.ts +29 -5
- package/dist-lib/types/pdfRenderer.d.ts +61 -12
- package/dist-lib/types/pdfStandard.d.ts +88 -0
- package/dist-lib/types/pdfxVerify.d.ts +4 -0
- package/dist-lib/types/pixels.d.ts +21 -3
- package/dist-lib/types/psFilters.d.ts +4 -14
- package/dist-lib/types/psShading.d.ts +1 -24
- package/dist-lib/types/resources.d.ts +2 -2
- package/dist-lib/types/sourceRef.d.ts +20 -1
- package/dist-lib/types/strokeOutline.d.ts +32 -0
- package/dist-lib/types/svg.d.ts +236 -10
- package/dist-lib/types/svgFilterRaster.d.ts +88 -0
- package/dist-lib/types/svgFilterSubregion.d.ts +46 -0
- package/dist-lib/types/tableBorders.d.ts +33 -4
- package/dist-lib/types/types.d.ts +396 -11
- package/package.json +5 -1
- package/dist-lib/api-CQ3aOtc5.js.map +0 -1
- package/dist-lib/index.js.map +0 -1
- package/dist-lib/node.js.map +0 -1
|
@@ -0,0 +1,3239 @@
|
|
|
1
|
+
/*! Kumiori (組み織り) 0.4.0 | Apache License 2.0 | Copyright 2026 Northern Lights Co.
|
|
2
|
+
* Third-party notices (第三者のソフトウェアの表示): THIRD_PARTY_NOTICES.md
|
|
3
|
+
* https://cdn.jsdelivr.net/npm/kumiori@0.4.0/THIRD_PARTY_NOTICES.md */
|
|
4
|
+
import { A as msg, H as metafileSvgBudget, O as encodePngStored, U as base64, V as metafilePixelBudget, W as isCgm, Y as fontFamilyOf, _ as inflateBytes, i as decodeJpeg, p as decodeModifiedHuffman, v as lzwBytes } from "../cli.js";
|
|
5
|
+
// src/cgm.ts
|
|
6
|
+
/**
|
|
7
|
+
* cgm — CGM (Computer Graphics Metafile) の 2 進符号化を読み、絵 (picture) を SVG にする。
|
|
8
|
+
*
|
|
9
|
+
* 仕様: ISO/IEC 8632-1:1999 (機能仕様。要素の意味・既定値。正誤表 1:2006・2:2007 を含む) と ISO/IEC 8632-3:1999 (2 進符号化)。
|
|
10
|
+
* - 命令の見出し (第 3 部 5.4): 1 語 (16 ビット) = 類 (4 ビット)・番号 (7 ビット)・引数の長さ (5 ビット)。
|
|
11
|
+
* 長さ 31 は長い形: 次の語の下位 15 ビットが長さ、最上位ビットが「続きあり」。命令は語の境目から始まる (奇数長は 1 octet 埋める)
|
|
12
|
+
* - 数値の形 (第 3 部 6 章): 符号付き・符号無し整数 (8/16/24/32 ビット)、固定小数点 (整数部 SI + 小数部 UI / 2^n)、IEEE 754 浮動小数点。
|
|
13
|
+
* すべて上位バイトが先 (big-endian)
|
|
14
|
+
* - 引数の型 (第 3 部 7 章 表 1): VDC は VDC TYPE で整数か実数、CO は COLOUR SELECTION MODE で色番号か直接色、
|
|
15
|
+
* SS は幅の指定の流儀 (absolute なら VDC、ほかは実数)、文字列は長さ (0..254、255 なら次の 16 ビット) と文字
|
|
16
|
+
* - 要素の番号と引数 (第 3 部 8 章 表 3〜表 12)、既定値 (第 1 部 8 章・第 3 部 9 章)
|
|
17
|
+
* 座標: VDC EXTENT の第 1 の角が見る人にとっての左下、第 2 の角が右上 (第 1 部 6.4.4)。正の角度は +x から +y へ (同)。
|
|
18
|
+
* 表示装置しだいと定められている値 (背景色・前景色・scaled の線の太さの基準など) は、このファイルの定数で決める。
|
|
19
|
+
* 登録簿 (ISO International Register of Graphical Items) の要素: ESCAPE 22 (Transparent Cell Colour)・45 (Alpha Transparency)、
|
|
20
|
+
* 色の型 6・7・8 (RGB-alpha・sRGB・sRGB-alpha)、TILE の圧縮の形 7・8・9 (baseline JPEG・LZW・PNG の deflate) を読む。
|
|
21
|
+
* 描かない要素: GENERALIZED DRAWING PRIMITIVE (中身は登録簿で決まるが、登録された GDP が無い)、POLYSYMBOL (記号の表はメタファイルの外のもので、
|
|
22
|
+
* 登録された表も無い)、ほかの ESCAPE。どれも警告に出す。LINE CAP の triangle と dash cap は SVG に無いので、butt で描く
|
|
23
|
+
*/
|
|
24
|
+
var PX_PER_MM = 96 / 25.4;
|
|
25
|
+
/** 縮尺が abstract のときの絵の幅 (px)。imageInfo の cgmInfo と同じ */
|
|
26
|
+
var ABSTRACT_WIDTH = 400;
|
|
27
|
+
/** 表示装置しだいの値 (第 1 部 8 章 "device-dependent"): 背景色と前景色 */
|
|
28
|
+
var DEVICE_BACKGROUND = [
|
|
29
|
+
1,
|
|
30
|
+
1,
|
|
31
|
+
1
|
|
32
|
+
];
|
|
33
|
+
var DEVICE_FOREGROUND = [
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0
|
|
37
|
+
];
|
|
38
|
+
/**
|
|
39
|
+
* 表示装置しだいの値 (第 1 部 6.7.5 'scaled'): 線・辺の太さの基準 (倍率 1 の太さ。px)。
|
|
40
|
+
* Kumiori では 0.192pt とする。
|
|
41
|
+
* scaled・mm の太さは表示装置の上の太さなので、vector-effect="non-scaling-stroke" を付けて、絵を拡大縮小しても変えない
|
|
42
|
+
*/
|
|
43
|
+
var NOMINAL_LINE_WIDTH = .192 / .75;
|
|
44
|
+
/** scaled の印の大きさの基準 (px)。Kumiori では 1.34pt とする (大きさ 1・5・10・20 の × が 1.34・6.7・13.4・26.8pt) */
|
|
45
|
+
var NOMINAL_MARKER_SIZE = 1.34 / .75;
|
|
46
|
+
/**
|
|
47
|
+
* 文字の高さ (CHARACTER HEIGHT。基線から CAPLINE まで。第 1 部 7.7.15) から SVG の font-size を出す比。
|
|
48
|
+
* 書体の大文字の高さは書体ごとに違い、ここでは書体を読まないので、仮に 0.72 em とする (仮定)
|
|
49
|
+
*/
|
|
50
|
+
var CAP_HEIGHT_EM = .72;
|
|
51
|
+
/**
|
|
52
|
+
* INHERITANCE FILTER の選び方の番号 (第 3 部 表 11 の 2 番) → 状態の項目 (第 1 部 7.10.2 の表 6・表 7)。ASF は 'asf0'〜'asf17'。
|
|
53
|
+
* 切り抜きの方法 (clipping mode)・補助の色・透明・選び出しの番号・記号の属性は、描き方に関わらないので空
|
|
54
|
+
*/
|
|
55
|
+
var INHERIT = (() => {
|
|
56
|
+
const line = [
|
|
57
|
+
"lineBundle",
|
|
58
|
+
"lineType",
|
|
59
|
+
"lineWidth",
|
|
60
|
+
"lineColour",
|
|
61
|
+
"lineCap",
|
|
62
|
+
"lineJoin",
|
|
63
|
+
"lineCont",
|
|
64
|
+
"lineOffset"
|
|
65
|
+
];
|
|
66
|
+
const marker = [
|
|
67
|
+
"markerBundle",
|
|
68
|
+
"markerType",
|
|
69
|
+
"markerSize",
|
|
70
|
+
"markerColour"
|
|
71
|
+
];
|
|
72
|
+
const textPres = [
|
|
73
|
+
"textBundle",
|
|
74
|
+
"textFont",
|
|
75
|
+
"textPrecision",
|
|
76
|
+
"charExpansion",
|
|
77
|
+
"charSpacing",
|
|
78
|
+
"textColour",
|
|
79
|
+
"charHeight",
|
|
80
|
+
"scores",
|
|
81
|
+
"restrictedType"
|
|
82
|
+
];
|
|
83
|
+
const textPlace = [
|
|
84
|
+
"charOrientation",
|
|
85
|
+
"textPath",
|
|
86
|
+
"textAlign"
|
|
87
|
+
];
|
|
88
|
+
const fill = [
|
|
89
|
+
"fillBundle",
|
|
90
|
+
"interiorStyle",
|
|
91
|
+
"fillColour",
|
|
92
|
+
"hatchIndex",
|
|
93
|
+
"patternIndex",
|
|
94
|
+
"interp"
|
|
95
|
+
];
|
|
96
|
+
const edge = [
|
|
97
|
+
"edgeBundle",
|
|
98
|
+
"edgeType",
|
|
99
|
+
"edgeWidth",
|
|
100
|
+
"edgeColour",
|
|
101
|
+
"edgeVisible",
|
|
102
|
+
"edgeCap",
|
|
103
|
+
"edgeJoin",
|
|
104
|
+
"edgeCont",
|
|
105
|
+
"edgeOffset"
|
|
106
|
+
];
|
|
107
|
+
const pattern = ["fillRef", "patternSize"];
|
|
108
|
+
const output = ["mitreLimit"];
|
|
109
|
+
const asf = (a, b) => Array.from({ length: b - a + 1 }, (_, k) => `asf${a + k}`);
|
|
110
|
+
const all = [
|
|
111
|
+
...line,
|
|
112
|
+
...marker,
|
|
113
|
+
...textPres,
|
|
114
|
+
...textPlace,
|
|
115
|
+
...fill,
|
|
116
|
+
...edge,
|
|
117
|
+
...pattern,
|
|
118
|
+
...output
|
|
119
|
+
];
|
|
120
|
+
const single = [
|
|
121
|
+
"lineBundle",
|
|
122
|
+
"lineType",
|
|
123
|
+
"lineWidth",
|
|
124
|
+
"lineColour",
|
|
125
|
+
"",
|
|
126
|
+
"markerBundle",
|
|
127
|
+
"markerType",
|
|
128
|
+
"markerSize",
|
|
129
|
+
"markerColour",
|
|
130
|
+
"",
|
|
131
|
+
"textBundle",
|
|
132
|
+
"textFont",
|
|
133
|
+
"textPrecision",
|
|
134
|
+
"charExpansion",
|
|
135
|
+
"charSpacing",
|
|
136
|
+
"textColour",
|
|
137
|
+
"charHeight",
|
|
138
|
+
"charOrientation",
|
|
139
|
+
"textPath",
|
|
140
|
+
"textAlign",
|
|
141
|
+
"fillBundle",
|
|
142
|
+
"interiorStyle",
|
|
143
|
+
"fillColour",
|
|
144
|
+
"hatchIndex",
|
|
145
|
+
"patternIndex",
|
|
146
|
+
"edgeBundle",
|
|
147
|
+
"edgeType",
|
|
148
|
+
"edgeWidth",
|
|
149
|
+
"edgeColour",
|
|
150
|
+
"edgeVisible",
|
|
151
|
+
"",
|
|
152
|
+
"fillRef",
|
|
153
|
+
"patternSize",
|
|
154
|
+
"",
|
|
155
|
+
""
|
|
156
|
+
];
|
|
157
|
+
const m = {};
|
|
158
|
+
single.forEach((k, i) => m[i] = k ? [k] : []);
|
|
159
|
+
Object.assign(m, {
|
|
160
|
+
35: line,
|
|
161
|
+
36: marker,
|
|
162
|
+
37: textPres,
|
|
163
|
+
38: textPlace,
|
|
164
|
+
39: fill,
|
|
165
|
+
40: edge,
|
|
166
|
+
41: pattern,
|
|
167
|
+
42: output,
|
|
168
|
+
43: [],
|
|
169
|
+
44: all,
|
|
170
|
+
45: [...all, ...asf(0, 17)]
|
|
171
|
+
});
|
|
172
|
+
for (let k = 0; k < 18; k++) m[46 + k] = [`asf${k}`];
|
|
173
|
+
Object.assign(m, {
|
|
174
|
+
64: asf(0, 2),
|
|
175
|
+
65: asf(3, 5),
|
|
176
|
+
66: asf(6, 10),
|
|
177
|
+
67: asf(11, 14),
|
|
178
|
+
68: asf(15, 17),
|
|
179
|
+
69: asf(0, 17)
|
|
180
|
+
});
|
|
181
|
+
[
|
|
182
|
+
"mitreLimit",
|
|
183
|
+
"lineCap",
|
|
184
|
+
"lineJoin",
|
|
185
|
+
"lineCont",
|
|
186
|
+
"lineOffset",
|
|
187
|
+
"scores",
|
|
188
|
+
"restrictedType",
|
|
189
|
+
"interp",
|
|
190
|
+
"edgeCap",
|
|
191
|
+
"edgeJoin",
|
|
192
|
+
"edgeCont",
|
|
193
|
+
"edgeOffset"
|
|
194
|
+
].forEach((k, i) => m[70 + i] = [k]);
|
|
195
|
+
for (let k = 82; k <= 86; k++) m[k] = [];
|
|
196
|
+
return m;
|
|
197
|
+
})();
|
|
198
|
+
/** 色の型の直接色の成分の数 (4 CMYK・6 RGB-alpha・8 sRGB-alpha は 4、ほかは 3) */
|
|
199
|
+
var compsOf = (model) => model === 4 || model === 6 || model === 8 ? 4 : 3;
|
|
200
|
+
/** 色の型が RGB の仲間か (1 RGB・6 RGB-alpha・7 sRGB・8 sRGB-alpha。COLOUR VALUE EXTENT は最小と最大の直接色) */
|
|
201
|
+
var rgbLike = (model) => model === 1 || model === 6 || model === 7 || model === 8;
|
|
202
|
+
var fixed32 = {
|
|
203
|
+
float: false,
|
|
204
|
+
a: 16,
|
|
205
|
+
b: 16
|
|
206
|
+
};
|
|
207
|
+
var float32 = {
|
|
208
|
+
float: true,
|
|
209
|
+
a: 9,
|
|
210
|
+
b: 23
|
|
211
|
+
};
|
|
212
|
+
/** 第 3 部 9 章の符号化の既定値と、第 1 部 8 章の既定値 */
|
|
213
|
+
function initialState() {
|
|
214
|
+
return {
|
|
215
|
+
vdcType: 0,
|
|
216
|
+
intPrec: 16,
|
|
217
|
+
realForm: { ...fixed32 },
|
|
218
|
+
indexPrec: 16,
|
|
219
|
+
colourPrec: 8,
|
|
220
|
+
colourIndexPrec: 8,
|
|
221
|
+
namePrec: 16,
|
|
222
|
+
vdcIntPrec: 16,
|
|
223
|
+
vdcRealForm: { ...fixed32 },
|
|
224
|
+
colourModel: 1,
|
|
225
|
+
cvMin: [
|
|
226
|
+
0,
|
|
227
|
+
0,
|
|
228
|
+
0,
|
|
229
|
+
0
|
|
230
|
+
],
|
|
231
|
+
cvMax: [
|
|
232
|
+
255,
|
|
233
|
+
255,
|
|
234
|
+
255,
|
|
235
|
+
255
|
|
236
|
+
],
|
|
237
|
+
cvScale: [
|
|
238
|
+
0,
|
|
239
|
+
0,
|
|
240
|
+
0,
|
|
241
|
+
0,
|
|
242
|
+
0,
|
|
243
|
+
0
|
|
244
|
+
],
|
|
245
|
+
white: void 0,
|
|
246
|
+
scalingMetric: void 0,
|
|
247
|
+
colourSelection: 0,
|
|
248
|
+
lineWidthMode: 1,
|
|
249
|
+
markerSizeMode: 1,
|
|
250
|
+
edgeWidthMode: 1,
|
|
251
|
+
vdcExtent: void 0,
|
|
252
|
+
background: void 0,
|
|
253
|
+
clip: void 0,
|
|
254
|
+
clipOn: true,
|
|
255
|
+
lineType: 1,
|
|
256
|
+
lineWidth: [
|
|
257
|
+
NaN,
|
|
258
|
+
1,
|
|
259
|
+
.001,
|
|
260
|
+
.35
|
|
261
|
+
],
|
|
262
|
+
lineColour: { index: 1 },
|
|
263
|
+
lineCap: 1,
|
|
264
|
+
lineJoin: 1,
|
|
265
|
+
markerType: 3,
|
|
266
|
+
markerSize: [
|
|
267
|
+
NaN,
|
|
268
|
+
1,
|
|
269
|
+
.01,
|
|
270
|
+
2.5
|
|
271
|
+
],
|
|
272
|
+
markerColour: { index: 1 },
|
|
273
|
+
textFont: 1,
|
|
274
|
+
charExpansion: 1,
|
|
275
|
+
charSpacing: 0,
|
|
276
|
+
textColour: { index: 1 },
|
|
277
|
+
charHeight: void 0,
|
|
278
|
+
charOrientation: void 0,
|
|
279
|
+
textPath: 0,
|
|
280
|
+
textAlign: [
|
|
281
|
+
0,
|
|
282
|
+
0,
|
|
283
|
+
0,
|
|
284
|
+
0
|
|
285
|
+
],
|
|
286
|
+
restrictedType: 1,
|
|
287
|
+
interiorStyle: 0,
|
|
288
|
+
interiorMode: 0,
|
|
289
|
+
fillRef: void 0,
|
|
290
|
+
interp: void 0,
|
|
291
|
+
fillColour: { index: 1 },
|
|
292
|
+
hatchIndex: 1,
|
|
293
|
+
edgeType: 1,
|
|
294
|
+
edgeWidth: [
|
|
295
|
+
NaN,
|
|
296
|
+
1,
|
|
297
|
+
.001,
|
|
298
|
+
.35
|
|
299
|
+
],
|
|
300
|
+
edgeColour: { index: 1 },
|
|
301
|
+
edgeVisible: false,
|
|
302
|
+
edgeCap: 1,
|
|
303
|
+
edgeJoin: 1,
|
|
304
|
+
colourTable: /* @__PURE__ */ new Map(),
|
|
305
|
+
fonts: [],
|
|
306
|
+
lineTypeDefs: /* @__PURE__ */ new Map(),
|
|
307
|
+
hatchDefs: /* @__PURE__ */ new Map(),
|
|
308
|
+
patternIndex: 1,
|
|
309
|
+
patternTable: /* @__PURE__ */ new Map(),
|
|
310
|
+
patternSize: void 0,
|
|
311
|
+
geoPatterns: /* @__PURE__ */ new Map(),
|
|
312
|
+
transparentCell: void 0,
|
|
313
|
+
colourTableAlpha: /* @__PURE__ */ new Map(),
|
|
314
|
+
alpha: 1,
|
|
315
|
+
mitreLimit: 32767,
|
|
316
|
+
lineCont: 1,
|
|
317
|
+
edgeCont: 1,
|
|
318
|
+
lineOffset: 0,
|
|
319
|
+
edgeOffset: 0,
|
|
320
|
+
scores: [],
|
|
321
|
+
textPrecision: 0,
|
|
322
|
+
charSets: [],
|
|
323
|
+
codingAnnouncer: 0,
|
|
324
|
+
charSetIndex: 1,
|
|
325
|
+
altCharSetIndex: 1,
|
|
326
|
+
textPathMode: 0,
|
|
327
|
+
textPathD: void 0,
|
|
328
|
+
bundles: {
|
|
329
|
+
line: /* @__PURE__ */ new Map(),
|
|
330
|
+
marker: /* @__PURE__ */ new Map(),
|
|
331
|
+
text: /* @__PURE__ */ new Map(),
|
|
332
|
+
fill: /* @__PURE__ */ new Map(),
|
|
333
|
+
edge: /* @__PURE__ */ new Map()
|
|
334
|
+
},
|
|
335
|
+
lineBundle: 1,
|
|
336
|
+
markerBundle: 1,
|
|
337
|
+
textBundle: 1,
|
|
338
|
+
fillBundle: 1,
|
|
339
|
+
edgeBundle: 1,
|
|
340
|
+
asf: new Array(18).fill(false),
|
|
341
|
+
inherit: []
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function cloneState(s) {
|
|
345
|
+
return {
|
|
346
|
+
...s,
|
|
347
|
+
realForm: { ...s.realForm },
|
|
348
|
+
vdcRealForm: { ...s.vdcRealForm },
|
|
349
|
+
cvMin: [...s.cvMin],
|
|
350
|
+
cvMax: [...s.cvMax],
|
|
351
|
+
cvScale: [...s.cvScale],
|
|
352
|
+
lineWidth: [...s.lineWidth],
|
|
353
|
+
markerSize: [...s.markerSize],
|
|
354
|
+
edgeWidth: [...s.edgeWidth],
|
|
355
|
+
textAlign: [...s.textAlign],
|
|
356
|
+
colourTable: new Map(s.colourTable),
|
|
357
|
+
colourTableAlpha: new Map(s.colourTableAlpha),
|
|
358
|
+
fonts: [...s.fonts],
|
|
359
|
+
lineTypeDefs: new Map(s.lineTypeDefs),
|
|
360
|
+
hatchDefs: new Map(s.hatchDefs),
|
|
361
|
+
patternTable: new Map(s.patternTable),
|
|
362
|
+
geoPatterns: new Map(s.geoPatterns),
|
|
363
|
+
scores: [...s.scores],
|
|
364
|
+
charSets: [...s.charSets],
|
|
365
|
+
bundles: {
|
|
366
|
+
line: new Map(s.bundles.line),
|
|
367
|
+
marker: new Map(s.bundles.marker),
|
|
368
|
+
text: new Map(s.bundles.text),
|
|
369
|
+
fill: new Map(s.bundles.fill),
|
|
370
|
+
edge: new Map(s.bundles.edge)
|
|
371
|
+
},
|
|
372
|
+
asf: [...s.asf],
|
|
373
|
+
inherit: [...s.inherit]
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/** 引数を読む (第 3 部 6・7 章)。1 つの要素の引数 (分割された長い形はつないだもの) を、ビット単位で読む */
|
|
377
|
+
var Params = class {
|
|
378
|
+
d;
|
|
379
|
+
s;
|
|
380
|
+
bit = 0;
|
|
381
|
+
constructor(d, s) {
|
|
382
|
+
this.d = d;
|
|
383
|
+
this.s = s;
|
|
384
|
+
}
|
|
385
|
+
get left() {
|
|
386
|
+
return this.d.length * 8 - this.bit;
|
|
387
|
+
}
|
|
388
|
+
get pos() {
|
|
389
|
+
return this.bit >> 3;
|
|
390
|
+
}
|
|
391
|
+
/** バイトの位置へ移る */
|
|
392
|
+
seekByte(n) {
|
|
393
|
+
this.bit = n * 8;
|
|
394
|
+
}
|
|
395
|
+
/** 次の語 (16 ビット) の境目へ進む (CELL ARRAY の行の始め) */
|
|
396
|
+
alignWord() {
|
|
397
|
+
this.bit = Math.ceil(this.bit / 16) * 16;
|
|
398
|
+
}
|
|
399
|
+
/** 残りのバイト (次のバイトの境目から。BITONAL TILE・TILE の BS) */
|
|
400
|
+
rest() {
|
|
401
|
+
return this.d.subarray(Math.ceil(this.bit / 8));
|
|
402
|
+
}
|
|
403
|
+
bits(n) {
|
|
404
|
+
let v = 0;
|
|
405
|
+
if ((this.bit & 7) === 0 && (n & 7) === 0) {
|
|
406
|
+
let o = this.bit >> 3;
|
|
407
|
+
for (let k = 0; k < n; k += 8) v = v * 256 + (this.d[o++] ?? 0);
|
|
408
|
+
this.bit += n;
|
|
409
|
+
return v;
|
|
410
|
+
}
|
|
411
|
+
for (let k = 0; k < n; k++) {
|
|
412
|
+
const byte = this.d[this.bit >> 3] ?? 0;
|
|
413
|
+
v = v * 2 + (byte >> 7 - (this.bit & 7) & 1);
|
|
414
|
+
this.bit++;
|
|
415
|
+
}
|
|
416
|
+
return v;
|
|
417
|
+
}
|
|
418
|
+
uint(prec) {
|
|
419
|
+
return this.bits(prec);
|
|
420
|
+
}
|
|
421
|
+
sint(prec) {
|
|
422
|
+
const u = this.bits(prec);
|
|
423
|
+
return u >= 2 ** (prec - 1) ? u - 2 ** prec : u;
|
|
424
|
+
}
|
|
425
|
+
real(f) {
|
|
426
|
+
if (!f.float) return this.sint(f.a) + this.uint(f.b) / 2 ** f.b;
|
|
427
|
+
const bytes = new Uint8Array(f.a + f.b > 32 ? 8 : 4);
|
|
428
|
+
for (let k = 0; k < bytes.length; k++) bytes[k] = this.bits(8);
|
|
429
|
+
const v = new DataView(bytes.buffer);
|
|
430
|
+
return bytes.length === 8 ? v.getFloat64(0, false) : v.getFloat32(0, false);
|
|
431
|
+
}
|
|
432
|
+
/** 浮動小数点で書かれる実数 (SCALING MODE の倍率など。第 3 部 表 1 の注 10) */
|
|
433
|
+
floatReal() {
|
|
434
|
+
return this.real(this.s.realForm.float ? this.s.realForm : float32);
|
|
435
|
+
}
|
|
436
|
+
I() {
|
|
437
|
+
return this.sint(this.s.intPrec);
|
|
438
|
+
}
|
|
439
|
+
R() {
|
|
440
|
+
return this.real(this.s.realForm);
|
|
441
|
+
}
|
|
442
|
+
IX() {
|
|
443
|
+
return this.sint(this.s.indexPrec);
|
|
444
|
+
}
|
|
445
|
+
E() {
|
|
446
|
+
return this.sint(16);
|
|
447
|
+
}
|
|
448
|
+
N() {
|
|
449
|
+
return this.sint(this.s.namePrec);
|
|
450
|
+
}
|
|
451
|
+
VDC() {
|
|
452
|
+
return this.s.vdcType === 0 ? this.sint(this.s.vdcIntPrec) : this.real(this.s.vdcRealForm);
|
|
453
|
+
}
|
|
454
|
+
P() {
|
|
455
|
+
return [this.VDC(), this.VDC()];
|
|
456
|
+
}
|
|
457
|
+
CI(prec = this.s.colourIndexPrec) {
|
|
458
|
+
return this.uint(prec);
|
|
459
|
+
}
|
|
460
|
+
CD(prec = this.s.colourPrec) {
|
|
461
|
+
const n = compsOf(this.s.colourModel);
|
|
462
|
+
const out = [];
|
|
463
|
+
for (let k = 0; k < n; k++) out.push(this.uint(prec));
|
|
464
|
+
return out;
|
|
465
|
+
}
|
|
466
|
+
CO() {
|
|
467
|
+
return this.s.colourSelection === 0 ? { index: this.CI() } : directColour(this.s, this.CD());
|
|
468
|
+
}
|
|
469
|
+
/** SS: 流儀が absolute なら VDC、ほかは実数 */
|
|
470
|
+
SS(mode) {
|
|
471
|
+
return mode === 0 ? this.VDC() : this.R();
|
|
472
|
+
}
|
|
473
|
+
/** 構造のある記録 SDR (第 3 部 表 1 の注 17): 文字列と同じ形で包んだ、ほかの型の値の並び。中身のバイトを返す */
|
|
474
|
+
SDR() {
|
|
475
|
+
return Uint8Array.from(this.S(), (c) => c.charCodeAt(0));
|
|
476
|
+
}
|
|
477
|
+
/** 文字列 (第 3 部 表 1 の注 6) */
|
|
478
|
+
S() {
|
|
479
|
+
let out = "";
|
|
480
|
+
let n = this.uint(8);
|
|
481
|
+
let more = false;
|
|
482
|
+
if (n === 255) {
|
|
483
|
+
const w = this.uint(16);
|
|
484
|
+
more = (w & 32768) !== 0;
|
|
485
|
+
n = w & 32767;
|
|
486
|
+
}
|
|
487
|
+
for (;;) {
|
|
488
|
+
for (let k = 0; k < n && this.left >= 8; k++) out += String.fromCharCode(this.uint(8));
|
|
489
|
+
if (!more || this.left < 16) break;
|
|
490
|
+
const w = this.uint(16);
|
|
491
|
+
more = (w & 32768) !== 0;
|
|
492
|
+
n = w & 32767;
|
|
493
|
+
}
|
|
494
|
+
return out;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
/** D65 の白 (XYZ。Y = 1) */
|
|
498
|
+
var D65 = [
|
|
499
|
+
.95047,
|
|
500
|
+
1,
|
|
501
|
+
1.08883
|
|
502
|
+
];
|
|
503
|
+
/** XYZ (基準の白 w) を sRGB (0..1) にする。白が D65 でなければ、XYZ の比で D65 へ寄せる (簡単な順応) */
|
|
504
|
+
function xyzToRgb(x, y, z, w) {
|
|
505
|
+
const X = x * D65[0] / w[0];
|
|
506
|
+
const Y = y * D65[1] / w[1];
|
|
507
|
+
const Z = z * D65[2] / w[2];
|
|
508
|
+
const lin = [
|
|
509
|
+
3.2404542 * X - 1.5371385 * Y - .4985314 * Z,
|
|
510
|
+
-.969266 * X + 1.8760108 * Y + .041556 * Z,
|
|
511
|
+
.0556434 * X - .2040259 * Y + 1.0572252 * Z
|
|
512
|
+
];
|
|
513
|
+
const g = (c) => {
|
|
514
|
+
const v = Math.min(1, Math.max(0, c));
|
|
515
|
+
return v <= .0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - .055;
|
|
516
|
+
};
|
|
517
|
+
return [
|
|
518
|
+
g(lin[0]),
|
|
519
|
+
g(lin[1]),
|
|
520
|
+
g(lin[2])
|
|
521
|
+
];
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* 直接色の不透明度 (0..1)。登録簿の色の型 6 (RGB-alpha)・8 (sRGB-alpha) の 4 つ目の成分 (COLOUR VALUE EXTENT で正規化。
|
|
525
|
+
* 最小が透明、最大が不透明。成分は不透明度を掛けていない値とする)。ほかの色の型は 1
|
|
526
|
+
*/
|
|
527
|
+
function toAlpha(s, v) {
|
|
528
|
+
if (s.colourModel !== 6 && s.colourModel !== 8) return 1;
|
|
529
|
+
const lo = s.cvMin[3] ?? 0;
|
|
530
|
+
const hi = s.cvMax[3] ?? 255;
|
|
531
|
+
return hi === lo ? 1 : Math.min(1, Math.max(0, ((v[3] ?? hi) - lo) / (hi - lo)));
|
|
532
|
+
}
|
|
533
|
+
/** 直接色 (不透明度が 1 未満なら a も持つ) */
|
|
534
|
+
function directColour(s, v) {
|
|
535
|
+
const a = toAlpha(s, v);
|
|
536
|
+
return a < 1 ? {
|
|
537
|
+
rgb: toRgb(s, v),
|
|
538
|
+
a
|
|
539
|
+
} : { rgb: toRgb(s, v) };
|
|
540
|
+
}
|
|
541
|
+
/** 直接色を 0..1 の RGB にする (COLOUR VALUE EXTENT で正規化。CMYK・CIELAB・CIELUV は RGB に直す。第 1 部 7.3.10・7.3.19)。
|
|
542
|
+
* 登録簿の色の型 6・7・8 (RGB-alpha・sRGB・sRGB-alpha) の色の成分は RGB と同じに読む (sRGB は出力の色と同じ) */
|
|
543
|
+
function toRgb(s, v) {
|
|
544
|
+
if (s.colourModel === 2 || s.colourModel === 3 || s.colourModel === 5) {
|
|
545
|
+
const c = [
|
|
546
|
+
0,
|
|
547
|
+
1,
|
|
548
|
+
2
|
|
549
|
+
].map((k) => (s.cvScale[2 * k] ?? 0) * (v[k] ?? 0) + (s.cvScale[2 * k + 1] ?? 0));
|
|
550
|
+
const w = s.white ?? D65;
|
|
551
|
+
if (s.colourModel === 5) return [
|
|
552
|
+
0,
|
|
553
|
+
1,
|
|
554
|
+
2
|
|
555
|
+
].map((k) => Math.min(1, Math.max(0, c[k])));
|
|
556
|
+
const [L] = c;
|
|
557
|
+
const fy = (L + 16) / 116;
|
|
558
|
+
const Y = L > 8 ? fy ** 3 : L / 903.3;
|
|
559
|
+
if (s.colourModel === 2) {
|
|
560
|
+
const f = (t) => t > 6 / 29 ? t ** 3 : 3 * (6 / 29) ** 2 * (t - 4 / 29);
|
|
561
|
+
return xyzToRgb(w[0] * f(fy + c[1] / 500), w[1] * Y, w[2] * f(fy - c[2] / 200), w);
|
|
562
|
+
}
|
|
563
|
+
const den = w[0] + 15 * w[1] + 3 * w[2];
|
|
564
|
+
const un = 4 * w[0] / den;
|
|
565
|
+
const vn = 9 * w[1] / den;
|
|
566
|
+
if (!(L > 0)) return [
|
|
567
|
+
0,
|
|
568
|
+
0,
|
|
569
|
+
0
|
|
570
|
+
];
|
|
571
|
+
const u = c[1] / (13 * L) + un;
|
|
572
|
+
const vv = c[2] / (13 * L) + vn;
|
|
573
|
+
if (!(vv > 0)) return [
|
|
574
|
+
0,
|
|
575
|
+
0,
|
|
576
|
+
0
|
|
577
|
+
];
|
|
578
|
+
const YY = w[1] * Y;
|
|
579
|
+
return xyzToRgb(YY * 9 * u / (4 * vv), YY, YY * (12 - 3 * u - 20 * vv) / (4 * vv), w);
|
|
580
|
+
}
|
|
581
|
+
const n = (k) => {
|
|
582
|
+
const lo = s.cvMin[k] ?? 0;
|
|
583
|
+
const hi = s.cvMax[k] ?? 255;
|
|
584
|
+
return hi === lo ? 0 : Math.min(1, Math.max(0, (v[k] - lo) / (hi - lo)));
|
|
585
|
+
};
|
|
586
|
+
if (s.colourModel === 4) {
|
|
587
|
+
const k = n(3);
|
|
588
|
+
return [
|
|
589
|
+
(1 - n(0)) * (1 - k),
|
|
590
|
+
(1 - n(1)) * (1 - k),
|
|
591
|
+
(1 - n(2)) * (1 - k)
|
|
592
|
+
];
|
|
593
|
+
}
|
|
594
|
+
return [
|
|
595
|
+
n(0),
|
|
596
|
+
n(1),
|
|
597
|
+
n(2)
|
|
598
|
+
];
|
|
599
|
+
}
|
|
600
|
+
/** 文字の並びを、名前の符号で読む (使えない符号なら undefined) */
|
|
601
|
+
function decodeWith(label, bytes) {
|
|
602
|
+
try {
|
|
603
|
+
return new TextDecoder(label, { fatal: false }).decode(Uint8Array.from(bytes));
|
|
604
|
+
} catch {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
/** 96 字の G 集合の指示の列の終わり (ISO 2375 の登録簿) → ISO/IEC 8859 の符号の名前 */
|
|
609
|
+
var G96 = {
|
|
610
|
+
A: "iso-8859-1",
|
|
611
|
+
B: "iso-8859-2",
|
|
612
|
+
C: "iso-8859-3",
|
|
613
|
+
D: "iso-8859-4",
|
|
614
|
+
L: "iso-8859-5",
|
|
615
|
+
G: "iso-8859-6",
|
|
616
|
+
F: "iso-8859-7",
|
|
617
|
+
H: "iso-8859-8",
|
|
618
|
+
M: "iso-8859-9",
|
|
619
|
+
V: "iso-8859-10",
|
|
620
|
+
T: "windows-874",
|
|
621
|
+
Y: "iso-8859-13",
|
|
622
|
+
"_": "iso-8859-14",
|
|
623
|
+
b: "iso-8859-15",
|
|
624
|
+
f: "iso-8859-16"
|
|
625
|
+
};
|
|
626
|
+
/** 1 つの G 集合の符号 (7 ビットの値の並び) を読む */
|
|
627
|
+
function decodeSet(cs, codes) {
|
|
628
|
+
const tail = cs.tail;
|
|
629
|
+
if (cs.type === 0) {
|
|
630
|
+
if (tail === "I") return codes.map((c) => c >= 33 && c <= 95 ? String.fromCharCode(65377 + c - 33) : String.fromCharCode(c)).join("");
|
|
631
|
+
if (tail === "J") return codes.map((c) => c === 92 ? "¥" : c === 126 ? "‾" : String.fromCharCode(c)).join("");
|
|
632
|
+
if (tail === "A") return codes.map((c) => c === 35 ? "£" : String.fromCharCode(c)).join("");
|
|
633
|
+
return codes.map((c) => String.fromCharCode(c)).join("");
|
|
634
|
+
}
|
|
635
|
+
if (cs.type === 1) {
|
|
636
|
+
const hi = codes.map((c) => c | 128);
|
|
637
|
+
return decodeWith(G96[tail] ?? "iso-8859-1", hi) ?? hi.map((c) => String.fromCharCode(c)).join("");
|
|
638
|
+
}
|
|
639
|
+
if (cs.type === 2 || cs.type === 3) {
|
|
640
|
+
const hi = codes.map((c) => c | 128);
|
|
641
|
+
let out;
|
|
642
|
+
if (tail === "@" || tail === "B" || tail === "O" || tail === "Q") out = decodeWith("euc-jp", hi);
|
|
643
|
+
else if (tail === "A") out = decodeWith("gb18030", hi);
|
|
644
|
+
else if (tail === "C") out = decodeWith("euc-kr", hi);
|
|
645
|
+
else if (tail === "D") {
|
|
646
|
+
const b = [];
|
|
647
|
+
for (let k = 0; k + 1 < hi.length; k += 2) b.push(143, hi[k], hi[k + 1]);
|
|
648
|
+
out = decodeWith("euc-jp", b);
|
|
649
|
+
}
|
|
650
|
+
return out ?? "�".repeat(codes.length >> 1);
|
|
651
|
+
}
|
|
652
|
+
return codes.map((c) => String.fromCharCode(c)).join("");
|
|
653
|
+
}
|
|
654
|
+
/** 完全な符号 (ESC 2/5 の後の列。G は UTF-8、/@・/C・/E は UCS-2、/J・/K・/L は UTF-16、/A・/D・/F は UCS-4) で読む。読めない列なら undefined */
|
|
655
|
+
function decodeComplete(tail, bytes) {
|
|
656
|
+
if ([
|
|
657
|
+
"G",
|
|
658
|
+
"/G",
|
|
659
|
+
"/H",
|
|
660
|
+
"/I"
|
|
661
|
+
].includes(tail)) return decodeWith("utf-8", bytes);
|
|
662
|
+
if ([
|
|
663
|
+
"/@",
|
|
664
|
+
"/C",
|
|
665
|
+
"/E",
|
|
666
|
+
"/J",
|
|
667
|
+
"/K",
|
|
668
|
+
"/L"
|
|
669
|
+
].includes(tail)) return decodeWith("utf-16be", bytes);
|
|
670
|
+
if ([
|
|
671
|
+
"/A",
|
|
672
|
+
"/D",
|
|
673
|
+
"/F"
|
|
674
|
+
].includes(tail)) {
|
|
675
|
+
let out = "";
|
|
676
|
+
for (let k = 0; k + 3 < bytes.length; k += 4) {
|
|
677
|
+
const cp = (bytes[k] << 24 | bytes[k + 1] << 16 | bytes[k + 2] << 8 | bytes[k + 3]) >>> 0;
|
|
678
|
+
out += cp <= 1114111 ? String.fromCodePoint(cp) : "�";
|
|
679
|
+
}
|
|
680
|
+
return out;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* 字の文字列の符号を読む (第 1 部 6.3.4・7.3.14・7.3.15・7.7.19・7.7.20)。CHARACTER SET INDEX の集合を G0 (0x21〜0x7E)、
|
|
685
|
+
* ALTERNATE CHARACTER SET INDEX の集合を G1 (8 ビットなら 0xA1〜0xFE、7 ビットなら SO の後) に置く。
|
|
686
|
+
* 拡張の符号 (CHARACTER CODING ANNOUNCER が extended) では、文字列の中の ISO 2022 の指示 (ESC ( F・ESC ) F・ESC - F・ESC $ F・ESC $ ( F・ESC $ ) F・ESC % F) も読む。
|
|
687
|
+
* CHARACTER SET LIST が無ければ、1 番は ISO 646 (ASCII) とし、8 ビットの右半分は Latin-1 とする (これまでどおり)
|
|
688
|
+
*/
|
|
689
|
+
function decodeText(st, raw) {
|
|
690
|
+
const bytes = Array.from(raw, (c) => c.charCodeAt(0) & 255);
|
|
691
|
+
const set = (k) => st.charSets[k - 1];
|
|
692
|
+
let g0 = set(st.charSetIndex);
|
|
693
|
+
let g1 = set(st.altCharSetIndex);
|
|
694
|
+
if (!st.charSets.length && !bytes.includes(27)) return raw;
|
|
695
|
+
if (g0?.type === 4) return decodeComplete(g0.tail, bytes) ?? raw;
|
|
696
|
+
const extended = st.codingAnnouncer >= 2;
|
|
697
|
+
if (st.codingAnnouncer === 1 && (g0?.type === 1 || g0?.type === 3)) {
|
|
698
|
+
const gr = g0;
|
|
699
|
+
g0 = g1 && (g1.type === 0 || g1.type === 2) ? g1 : void 0;
|
|
700
|
+
g1 = gr;
|
|
701
|
+
}
|
|
702
|
+
const ascii = {
|
|
703
|
+
type: 0,
|
|
704
|
+
tail: "B"
|
|
705
|
+
};
|
|
706
|
+
let out = "";
|
|
707
|
+
let shifted = false;
|
|
708
|
+
let run = [];
|
|
709
|
+
let runSet;
|
|
710
|
+
let latin1 = false;
|
|
711
|
+
const flush = () => {
|
|
712
|
+
if (!run.length) return;
|
|
713
|
+
out += latin1 ? run.map((c) => String.fromCharCode(c | 128)).join("") : decodeSet(runSet ?? ascii, run);
|
|
714
|
+
run = [];
|
|
715
|
+
};
|
|
716
|
+
const push = (cs, c, l1 = false) => {
|
|
717
|
+
if (cs !== runSet || l1 !== latin1) flush();
|
|
718
|
+
runSet = cs;
|
|
719
|
+
latin1 = l1;
|
|
720
|
+
run.push(c);
|
|
721
|
+
};
|
|
722
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
723
|
+
const b = bytes[i];
|
|
724
|
+
if (b === 27 && extended) {
|
|
725
|
+
let j = i + 1;
|
|
726
|
+
const mid = [];
|
|
727
|
+
while (j < bytes.length && bytes[j] >= 32 && bytes[j] <= 47) mid.push(bytes[j++]);
|
|
728
|
+
if (j >= bytes.length) break;
|
|
729
|
+
const f = String.fromCharCode(bytes[j]);
|
|
730
|
+
const m = String.fromCharCode(...mid);
|
|
731
|
+
let cs;
|
|
732
|
+
let toG1 = false;
|
|
733
|
+
if (m === "(" || m === ")") cs = {
|
|
734
|
+
type: 0,
|
|
735
|
+
tail: f
|
|
736
|
+
}, toG1 = m === ")";
|
|
737
|
+
else if (m === "-" || m === "." || m === "/") cs = {
|
|
738
|
+
type: 1,
|
|
739
|
+
tail: f
|
|
740
|
+
}, toG1 = true;
|
|
741
|
+
else if (m === "$" || m === "$(" || m === "$)") cs = {
|
|
742
|
+
type: 2,
|
|
743
|
+
tail: f
|
|
744
|
+
}, toG1 = m === "$)";
|
|
745
|
+
else if (m === "%" || m === "%/") {
|
|
746
|
+
flush();
|
|
747
|
+
const rest = bytes.slice(j + 1);
|
|
748
|
+
out += decodeComplete(m.slice(1) + f, rest) ?? String.fromCharCode(...rest);
|
|
749
|
+
return out;
|
|
750
|
+
}
|
|
751
|
+
if (cs) {
|
|
752
|
+
flush();
|
|
753
|
+
if (toG1) g1 = cs;
|
|
754
|
+
else g0 = cs;
|
|
755
|
+
}
|
|
756
|
+
i = j;
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
if (b === 14 && extended) {
|
|
760
|
+
shifted = true;
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
if (b === 15 && extended) {
|
|
764
|
+
shifted = false;
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
if (b < 32 || b === 127) {
|
|
768
|
+
push(ascii, b);
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (b < 128) {
|
|
772
|
+
const cs = shifted ? g1 : g0;
|
|
773
|
+
if (b === 32 && cs?.type !== 1 && cs?.type !== 3) push(ascii, b);
|
|
774
|
+
else push(cs, b);
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
if (b < 160) {
|
|
778
|
+
push(ascii, 63);
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
if (g1 && g1 !== g0) push(g1, b & 127);
|
|
782
|
+
else if (g1 && g1.type !== 0) push(g1, b & 127);
|
|
783
|
+
else push(void 0, b & 127, true);
|
|
784
|
+
}
|
|
785
|
+
flush();
|
|
786
|
+
return out;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* 構造のある記録 (SDR。第 1 部 附属書 C.2.2、第 3 部 表 1 の注 17) の項目を読む: (型の番号 IX、数 I、その型の値) の並び。
|
|
790
|
+
* 型は 1 SDR・2 CI・3 CD・4 N・5 E・6 I・8〜10 IF8・IF16・IF32・11 IX・12 R・13 S・14 SF・15 VC・16 VDC・17 CCO・18 UI8・19 UI32・22 UI16。
|
|
791
|
+
* BS (20)・CL (21) と知らない型が来たら、そこで止める
|
|
792
|
+
*/
|
|
793
|
+
function sdrMembers(p, st) {
|
|
794
|
+
const out = [];
|
|
795
|
+
while (p.left >= st.indexPrec + st.intPrec) {
|
|
796
|
+
const type = p.IX();
|
|
797
|
+
const n = p.I();
|
|
798
|
+
if (n < 0 || n > 1e5) break;
|
|
799
|
+
const f = {
|
|
800
|
+
1: () => p.SDR(),
|
|
801
|
+
2: () => p.CI(),
|
|
802
|
+
3: () => p.CD(),
|
|
803
|
+
4: () => p.N(),
|
|
804
|
+
5: () => p.E(),
|
|
805
|
+
6: () => p.I(),
|
|
806
|
+
8: () => p.sint(8),
|
|
807
|
+
9: () => p.sint(16),
|
|
808
|
+
10: () => p.sint(32),
|
|
809
|
+
11: () => p.IX(),
|
|
810
|
+
12: () => p.R(),
|
|
811
|
+
13: () => p.S(),
|
|
812
|
+
14: () => p.S(),
|
|
813
|
+
15: () => p.VDC(),
|
|
814
|
+
16: () => p.VDC(),
|
|
815
|
+
17: () => p.uint(st.colourPrec),
|
|
816
|
+
18: () => p.uint(8),
|
|
817
|
+
19: () => p.uint(32),
|
|
818
|
+
22: () => p.uint(16)
|
|
819
|
+
}[type];
|
|
820
|
+
if (!f) break;
|
|
821
|
+
const values = [];
|
|
822
|
+
for (let k = 0; k < n && p.left > 0; k++) values.push(f());
|
|
823
|
+
out.push({
|
|
824
|
+
type,
|
|
825
|
+
values
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
return out;
|
|
829
|
+
}
|
|
830
|
+
/** PNG のフィルタ (0 なし・1 Sub・2 Up・3 Average・4 Paeth。PNG の仕様 §9) を戻す。各行の頭に 1 バイトのフィルタの番号がある */
|
|
831
|
+
function unfilterPng(d, rowBytes, bpp, rows) {
|
|
832
|
+
const out = new Uint8Array(rowBytes * rows);
|
|
833
|
+
for (let j = 0; j < rows; j++) {
|
|
834
|
+
const f = d[j * (rowBytes + 1)];
|
|
835
|
+
const src = j * (rowBytes + 1) + 1;
|
|
836
|
+
const o = j * rowBytes;
|
|
837
|
+
for (let i = 0; i < rowBytes; i++) {
|
|
838
|
+
const x = d[src + i] ?? 0;
|
|
839
|
+
const a = i >= bpp ? out[o + i - bpp] : 0;
|
|
840
|
+
const b = j > 0 ? out[o - rowBytes + i] : 0;
|
|
841
|
+
const c = i >= bpp && j > 0 ? out[o - rowBytes + i - bpp] : 0;
|
|
842
|
+
let v = x;
|
|
843
|
+
if (f === 1) v = x + a;
|
|
844
|
+
else if (f === 2) v = x + b;
|
|
845
|
+
else if (f === 3) v = x + (a + b >> 1);
|
|
846
|
+
else if (f === 4) {
|
|
847
|
+
const pp = a + b - c;
|
|
848
|
+
const pa = Math.abs(pp - a);
|
|
849
|
+
const pb = Math.abs(pp - b);
|
|
850
|
+
const pc = Math.abs(pp - c);
|
|
851
|
+
v = x + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c);
|
|
852
|
+
}
|
|
853
|
+
out[o + i] = v & 255;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return out;
|
|
857
|
+
}
|
|
858
|
+
/** 命令を順に取り出す (第 3 部 5.4)。壊れていれば途中まで */
|
|
859
|
+
function* elements(b) {
|
|
860
|
+
let p = 0;
|
|
861
|
+
while (p + 2 <= b.length) {
|
|
862
|
+
const h = b[p] << 8 | b[p + 1];
|
|
863
|
+
p += 2;
|
|
864
|
+
const cls = h >> 12;
|
|
865
|
+
const id = h >> 5 & 127;
|
|
866
|
+
let len = h & 31;
|
|
867
|
+
if (len !== 31) {
|
|
868
|
+
if (p + len > b.length) return;
|
|
869
|
+
yield {
|
|
870
|
+
cls,
|
|
871
|
+
id,
|
|
872
|
+
data: b.subarray(p, p + len)
|
|
873
|
+
};
|
|
874
|
+
p += len + (len & 1);
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const parts = [];
|
|
878
|
+
for (;;) {
|
|
879
|
+
if (p + 2 > b.length) return;
|
|
880
|
+
const w = b[p] << 8 | b[p + 1];
|
|
881
|
+
p += 2;
|
|
882
|
+
len = w & 32767;
|
|
883
|
+
if (p + len > b.length) return;
|
|
884
|
+
parts.push(b.subarray(p, p + len));
|
|
885
|
+
p += len + (len & 1);
|
|
886
|
+
if (!(w & 32768)) break;
|
|
887
|
+
}
|
|
888
|
+
if (parts.length === 1) yield {
|
|
889
|
+
cls,
|
|
890
|
+
id,
|
|
891
|
+
data: parts[0]
|
|
892
|
+
};
|
|
893
|
+
else {
|
|
894
|
+
const all = new Uint8Array(parts.reduce((a, x) => a + x.length, 0));
|
|
895
|
+
let o = 0;
|
|
896
|
+
for (const x of parts) {
|
|
897
|
+
all.set(x, o);
|
|
898
|
+
o += x.length;
|
|
899
|
+
}
|
|
900
|
+
yield {
|
|
901
|
+
cls,
|
|
902
|
+
id,
|
|
903
|
+
data: all
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* 最初の絵の大きさ (px) を、絵の本体を読まずに求める (imageInfo の cgmInfo 用)。cgmToSvg の大きさと同じになるように、
|
|
910
|
+
* 記述子を cgmToSvg と同じ読み方で読む: 精度 (INTEGER PRECISION・REAL PRECISION・VDC TYPE・VDC INTEGER PRECISION・VDC REAL PRECISION。
|
|
911
|
+
* 規格に無い値は前の値のまま)、SCALING MODE の倍率 (浮動小数点で書く。第 3 部 表 1 の注 10)、METAFILE DEFAULTS REPLACEMENT
|
|
912
|
+
* (中の要素で既定値を置き換える)、BEGIN PICTURE で既定値に戻すこと。VDC EXTENT が無ければ、既定の範囲 (第 1 部 8 章) を使う。
|
|
913
|
+
* 絵の本体 (BEGIN PICTURE BODY) が無いファイルは、VDC EXTENT が書かれていればその大きさ。大きさが決まらなければ undefined
|
|
914
|
+
*/
|
|
915
|
+
function cgmPictureSize(bytes) {
|
|
916
|
+
if (!isCgm(bytes)) return void 0;
|
|
917
|
+
let defaults = initialState();
|
|
918
|
+
let s = cloneState(defaults);
|
|
919
|
+
let inPicture = false;
|
|
920
|
+
const prec = (v, ok, cur) => ok.includes(v) ? v : cur;
|
|
921
|
+
const realForm = (float, a, b, cur) => (float ? a === 9 && b === 23 || a === 12 && b === 52 : a === 16 && b === 16 || a === 32 && b === 32) ? {
|
|
922
|
+
float,
|
|
923
|
+
a,
|
|
924
|
+
b
|
|
925
|
+
} : cur;
|
|
926
|
+
const P8 = [
|
|
927
|
+
8,
|
|
928
|
+
16,
|
|
929
|
+
24,
|
|
930
|
+
32
|
|
931
|
+
];
|
|
932
|
+
const size = (st, needExtent) => {
|
|
933
|
+
if (needExtent && !st.vdcExtent) return void 0;
|
|
934
|
+
const ext = st.vdcExtent ?? (st.vdcType === 0 ? [[0, 0], [32767, 32767]] : [[0, 0], [1, 1]]);
|
|
935
|
+
const w = Math.abs(ext[1][0] - ext[0][0]);
|
|
936
|
+
const h = Math.abs(ext[1][1] - ext[0][1]);
|
|
937
|
+
if (!(w > 0) || !(h > 0)) return void 0;
|
|
938
|
+
if (st.scalingMetric && st.scalingMetric > 0) return {
|
|
939
|
+
width: w * st.scalingMetric * PX_PER_MM,
|
|
940
|
+
height: h * st.scalingMetric * PX_PER_MM
|
|
941
|
+
};
|
|
942
|
+
return {
|
|
943
|
+
width: ABSTRACT_WIDTH,
|
|
944
|
+
height: ABSTRACT_WIDTH * h / w
|
|
945
|
+
};
|
|
946
|
+
};
|
|
947
|
+
/** 大きさに関わる要素だけを読む (cgmToSvg の process と同じ読み方) */
|
|
948
|
+
const apply = (el, st, inMdr) => {
|
|
949
|
+
const p = new Params(el.data, st);
|
|
950
|
+
const { cls, id } = el;
|
|
951
|
+
if (cls === 1) {
|
|
952
|
+
if (id === 3) st.vdcType = p.E() === 1 ? 1 : 0;
|
|
953
|
+
else if (id === 4) st.intPrec = prec(p.I(), P8, st.intPrec);
|
|
954
|
+
else if (id === 5) {
|
|
955
|
+
const f = p.E();
|
|
956
|
+
const a = p.I();
|
|
957
|
+
const b = p.I();
|
|
958
|
+
st.realForm = realForm(f === 0, a, b, st.realForm);
|
|
959
|
+
} else if (id === 12 && !inMdr) {
|
|
960
|
+
for (const sub of elements(el.data)) apply(sub, defaults, true);
|
|
961
|
+
s = cloneState(defaults);
|
|
962
|
+
}
|
|
963
|
+
} else if (cls === 2) {
|
|
964
|
+
if (id === 1) {
|
|
965
|
+
const m = p.E();
|
|
966
|
+
const f = p.floatReal();
|
|
967
|
+
st.scalingMetric = m === 1 ? f : void 0;
|
|
968
|
+
} else if (id === 6) st.vdcExtent = [p.P(), p.P()];
|
|
969
|
+
} else if (cls === 3) {
|
|
970
|
+
if (id === 1) st.vdcIntPrec = prec(p.I(), [
|
|
971
|
+
16,
|
|
972
|
+
24,
|
|
973
|
+
32
|
|
974
|
+
], st.vdcIntPrec);
|
|
975
|
+
else if (id === 2) {
|
|
976
|
+
const f = p.E();
|
|
977
|
+
const a = p.I();
|
|
978
|
+
const b = p.I();
|
|
979
|
+
st.vdcRealForm = realForm(f === 0, a, b, st.vdcRealForm);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
for (const el of elements(bytes)) {
|
|
984
|
+
if (el.cls === 0) {
|
|
985
|
+
if (el.id === 3) {
|
|
986
|
+
s = cloneState(defaults);
|
|
987
|
+
inPicture = true;
|
|
988
|
+
} else if (el.id === 4) return size(s, false);
|
|
989
|
+
else if (el.id === 5 || el.id === 2) break;
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
992
|
+
apply(el, inPicture ? s : defaults, false);
|
|
993
|
+
if (!inPicture && el.cls === 1) s = cloneState(defaults);
|
|
994
|
+
}
|
|
995
|
+
return size(s, true);
|
|
996
|
+
}
|
|
997
|
+
var esc = (t) => t.replace(/[&<>"]/g, (c) => c === "&" ? "&" : c === "<" ? "<" : c === ">" ? ">" : """).replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, "");
|
|
998
|
+
var num = (v) => Math.abs(v) < 1e-9 ? "0" : String(Math.round(v * 1e3) / 1e3);
|
|
999
|
+
var hex = (c) => "#" + c.map((v) => Math.round(Math.min(1, Math.max(0, v)) * 255).toString(16).padStart(2, "0")).join("");
|
|
1000
|
+
/** 要素の名前 (警告用。第 3 部 附属書 C の名前の一部) */
|
|
1001
|
+
function elementName(cls, id) {
|
|
1002
|
+
return `${cls},${id}`;
|
|
1003
|
+
}
|
|
1004
|
+
/** CGM の最初の絵を SVG にする。CGM でなければ undefined */
|
|
1005
|
+
function cgmToSvg(bytes) {
|
|
1006
|
+
if (!isCgm(bytes)) return void 0;
|
|
1007
|
+
let defaults = initialState();
|
|
1008
|
+
let s = cloneState(defaults);
|
|
1009
|
+
const warnings = /* @__PURE__ */ new Set();
|
|
1010
|
+
const warn = (m) => warnings.add(m);
|
|
1011
|
+
let inPicture = false;
|
|
1012
|
+
/** BEGIN PICTURE BODY の後 (背景色は BEGIN PICTURE BODY で決まり、絵の本体の色番号 0 の定義では変わらない。第 1 部 7.7.34) */
|
|
1013
|
+
let inBody = false;
|
|
1014
|
+
let pictureDone = false;
|
|
1015
|
+
let body = [];
|
|
1016
|
+
let count = 0;
|
|
1017
|
+
let W = ABSTRACT_WIDTH;
|
|
1018
|
+
let H = ABSTRACT_WIDTH;
|
|
1019
|
+
let ext = [[0, 0], [32767, 32767]];
|
|
1020
|
+
let pendingText;
|
|
1021
|
+
let clipSeq = 0;
|
|
1022
|
+
let clipId;
|
|
1023
|
+
const defs = [];
|
|
1024
|
+
const defaultExtent = (st) => st.vdcExtent ?? (st.vdcType === 0 ? [[0, 0], [32767, 32767]] : [[0, 0], [1, 1]]);
|
|
1025
|
+
const X = (x) => (x - ext[0][0]) / (ext[1][0] - ext[0][0]) * W;
|
|
1026
|
+
const Y = (y) => (ext[1][1] - y) / (ext[1][1] - ext[0][1]) * H;
|
|
1027
|
+
const map = (p) => [X(p[0]), Y(p[1])];
|
|
1028
|
+
/** VDC の向き (ベクトル) を SVG の向きにする */
|
|
1029
|
+
const mapVec = (v) => [v[0] / (ext[1][0] - ext[0][0]) * W, -v[1] / (ext[1][1] - ext[0][1]) * H];
|
|
1030
|
+
/** VDC の長さ (向きによらない大きさ) を px にする (縦横の倍率の平均) */
|
|
1031
|
+
const vdcLen = (v) => Math.abs(v) * (W / Math.abs(ext[1][0] - ext[0][0]) + H / Math.abs(ext[1][1] - ext[0][1])) / 2;
|
|
1032
|
+
/** 既定の VDC EXTENT の長い辺 (幅・大きさの既定値の基準。第 1 部 8 章) */
|
|
1033
|
+
const longSide = () => {
|
|
1034
|
+
const e = defaultExtent(defaults);
|
|
1035
|
+
return Math.max(Math.abs(e[1][0] - e[0][0]), Math.abs(e[1][1] - e[0][1]));
|
|
1036
|
+
};
|
|
1037
|
+
/** 幅・大きさを px にする (第 1 部 6.7.5) */
|
|
1038
|
+
const sizePx = (mode, v, nominal, defAbs) => {
|
|
1039
|
+
if (mode === 0) return vdcLen(Number.isNaN(v) ? defAbs * longSide() : v);
|
|
1040
|
+
if (mode === 1) return v * nominal;
|
|
1041
|
+
if (mode === 2) return v * W;
|
|
1042
|
+
return v * PX_PER_MM;
|
|
1043
|
+
};
|
|
1044
|
+
const lineWidthPx = () => sizePx(s.lineWidthMode, s.lineWidth[s.lineWidthMode], NOMINAL_LINE_WIDTH, .001);
|
|
1045
|
+
const edgeWidthPx = () => sizePx(s.edgeWidthMode, s.edgeWidth[s.edgeWidthMode], NOMINAL_LINE_WIDTH, .001);
|
|
1046
|
+
const colourOf = (c) => {
|
|
1047
|
+
if ("rgb" in c) return c.rgb;
|
|
1048
|
+
if (s.colourSelection === 1) return DEVICE_FOREGROUND;
|
|
1049
|
+
const t = s.colourTable.get(c.index);
|
|
1050
|
+
if (t) return t;
|
|
1051
|
+
return c.index === 0 ? s.background ?? DEVICE_BACKGROUND : DEVICE_FOREGROUND;
|
|
1052
|
+
};
|
|
1053
|
+
/** 色の不透明度: 直接色の a か色表の不透明度 (色の型 6・8) に、ESCAPE 45 の不透明度を掛けたもの */
|
|
1054
|
+
const alphaOf = (c) => {
|
|
1055
|
+
return ("rgb" in c ? c.a ?? 1 : s.colourSelection === 0 ? s.colourTableAlpha.get(c.index) ?? 1 : 1) * s.alpha;
|
|
1056
|
+
};
|
|
1057
|
+
/** 塗り・線の色の属性 (不透明度が 1 未満なら fill-opacity・stroke-opacity も) */
|
|
1058
|
+
const paint = (kind, c) => {
|
|
1059
|
+
const a = alphaOf(c);
|
|
1060
|
+
return `${kind}="${hex(colourOf(c))}"${a < 1 ? ` ${kind}-opacity="${num(a)}"` : ""}`;
|
|
1061
|
+
};
|
|
1062
|
+
/**
|
|
1063
|
+
* 束ね表 (第 1 部 6.7.1・7.7.35): ASPECT SOURCE FLAGS が 'bundled' の見た目は、束ねの番号 (LINE BUNDLE INDEX など) の表の値を使う。
|
|
1064
|
+
* 表に無い番号の中身は処理系しだい: 個別の値 (その時の LINE TYPE など) を使う。図形を描く間だけ、状態を置き換える
|
|
1065
|
+
*/
|
|
1066
|
+
const resolveBundles = (st) => {
|
|
1067
|
+
const a = st.asf;
|
|
1068
|
+
if (!a.some(Boolean)) return st;
|
|
1069
|
+
const r = {
|
|
1070
|
+
...st,
|
|
1071
|
+
lineWidth: [...st.lineWidth],
|
|
1072
|
+
markerSize: [...st.markerSize],
|
|
1073
|
+
edgeWidth: [...st.edgeWidth]
|
|
1074
|
+
};
|
|
1075
|
+
const get = (m, idx, what) => {
|
|
1076
|
+
const b = m.get(idx);
|
|
1077
|
+
if (!b) warn(msg(`${what}の束ね表の ${idx} 番が定められていないので、個別の値で描きます`, `bundle ${idx} of the ${what} bundle table is not defined; the individual values are used`));
|
|
1078
|
+
return b;
|
|
1079
|
+
};
|
|
1080
|
+
if (a[0] || a[1] || a[2]) {
|
|
1081
|
+
const b = get(st.bundles.line, st.lineBundle, msg("線", "line"));
|
|
1082
|
+
if (b) {
|
|
1083
|
+
if (a[0]) r.lineType = b.type;
|
|
1084
|
+
if (a[1]) {
|
|
1085
|
+
r.lineWidthMode = b.mode;
|
|
1086
|
+
r.lineWidth[b.mode] = b.width;
|
|
1087
|
+
}
|
|
1088
|
+
if (a[2]) r.lineColour = b.colour;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
if (a[3] || a[4] || a[5]) {
|
|
1092
|
+
const b = get(st.bundles.marker, st.markerBundle, msg("印", "marker"));
|
|
1093
|
+
if (b) {
|
|
1094
|
+
if (a[3]) r.markerType = b.type;
|
|
1095
|
+
if (a[4]) {
|
|
1096
|
+
r.markerSizeMode = b.mode;
|
|
1097
|
+
r.markerSize[b.mode] = b.size;
|
|
1098
|
+
}
|
|
1099
|
+
if (a[5]) r.markerColour = b.colour;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
if (a[6] || a[7] || a[8] || a[9] || a[10]) {
|
|
1103
|
+
const b = get(st.bundles.text, st.textBundle, msg("字", "text"));
|
|
1104
|
+
if (b) {
|
|
1105
|
+
if (a[6]) r.textFont = b.font;
|
|
1106
|
+
if (a[7]) r.textPrecision = b.precision;
|
|
1107
|
+
if (a[8]) r.charExpansion = b.expansion;
|
|
1108
|
+
if (a[9]) r.charSpacing = b.spacing;
|
|
1109
|
+
if (a[10]) r.textColour = b.colour;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
if (a[11] || a[12] || a[13] || a[14]) {
|
|
1113
|
+
const b = get(st.bundles.fill, st.fillBundle, msg("塗り", "fill"));
|
|
1114
|
+
if (b) {
|
|
1115
|
+
if (a[11]) r.interiorStyle = b.style;
|
|
1116
|
+
if (a[12]) r.fillColour = b.colour;
|
|
1117
|
+
if (a[13]) r.hatchIndex = b.hatch;
|
|
1118
|
+
if (a[14]) r.patternIndex = b.pattern;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
if (a[15] || a[16] || a[17]) {
|
|
1122
|
+
const b = get(st.bundles.edge, st.edgeBundle, msg("辺", "edge"));
|
|
1123
|
+
if (b) {
|
|
1124
|
+
if (a[15]) r.edgeType = b.type;
|
|
1125
|
+
if (a[16]) {
|
|
1126
|
+
r.edgeWidthMode = b.mode;
|
|
1127
|
+
r.edgeWidth[b.mode] = b.width;
|
|
1128
|
+
}
|
|
1129
|
+
if (a[17]) r.edgeColour = b.colour;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return r;
|
|
1133
|
+
};
|
|
1134
|
+
const withBundles = (f) => {
|
|
1135
|
+
const saved = s;
|
|
1136
|
+
s = resolveBundles(s);
|
|
1137
|
+
if (s === saved) return f();
|
|
1138
|
+
try {
|
|
1139
|
+
f();
|
|
1140
|
+
} finally {
|
|
1141
|
+
s = saved;
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
/** 線種 (1 実線・2 破線・3 点線・4 一点鎖線・5 二点鎖線。第 1 部 7.7.2)。線と隙間の長さは処理系しだいなので、線の太さの倍数で決める */
|
|
1145
|
+
const dashOf = (type, w) => {
|
|
1146
|
+
const u = Math.max(w, 1);
|
|
1147
|
+
const def = s.lineTypeDefs.get(type);
|
|
1148
|
+
if (def) {
|
|
1149
|
+
const sum = def.dashes.reduce((a, v) => a + v, 0);
|
|
1150
|
+
if (def.dashes.length < 2 || !(sum > 0)) return void 0;
|
|
1151
|
+
const cycle = def.mode === 0 ? vdcLen(def.repeat) : def.mode === 1 ? def.repeat * u : def.mode === 2 ? def.repeat * W : def.repeat * PX_PER_MM;
|
|
1152
|
+
const list = def.dashes.map((v, k) => k % 2 === 0 && v === 0 ? Math.max(w, .5) : v / sum * cycle);
|
|
1153
|
+
if (list.length % 2) list.push(0);
|
|
1154
|
+
return list.map(num).join(" ");
|
|
1155
|
+
}
|
|
1156
|
+
const p = {
|
|
1157
|
+
2: [6, 3],
|
|
1158
|
+
3: [1, 3],
|
|
1159
|
+
4: [
|
|
1160
|
+
6,
|
|
1161
|
+
3,
|
|
1162
|
+
1,
|
|
1163
|
+
3
|
|
1164
|
+
],
|
|
1165
|
+
5: [
|
|
1166
|
+
6,
|
|
1167
|
+
3,
|
|
1168
|
+
1,
|
|
1169
|
+
3,
|
|
1170
|
+
1,
|
|
1171
|
+
3
|
|
1172
|
+
]
|
|
1173
|
+
}[type];
|
|
1174
|
+
return p ? p.map((v) => num(v * u)).join(" ") : void 0;
|
|
1175
|
+
};
|
|
1176
|
+
const capOf = (c) => c === 3 ? "round" : c === 4 ? "square" : "butt";
|
|
1177
|
+
const joinOf = (j) => j === 3 ? "round" : j === 4 ? "bevel" : "miter";
|
|
1178
|
+
/**
|
|
1179
|
+
* fixed: 太さが表示装置の上の大きさ (scaled = 基準の太さの倍数、mm。第 1 部 6.7.5)。絵を拡大縮小しても太さを変えない
|
|
1180
|
+
* (vector-effect="non-scaling-stroke"。拡大した絵の線も、本来の太さのまま)
|
|
1181
|
+
*/
|
|
1182
|
+
const strokeAttrs = (colour, w, type, cap, join, fixed = false, offset = 0, dashOverride) => {
|
|
1183
|
+
const d = dashOverride ?? dashOf(type, w);
|
|
1184
|
+
const cycle = d ? d.split(" ").reduce((a, v) => a + Number(v), 0) : 0;
|
|
1185
|
+
const off = d && offset > 0 && cycle > 0 ? ` stroke-dashoffset="${num(offset % 1 * cycle)}"` : "";
|
|
1186
|
+
const ml = join === 2 || join === 1 && s.mitreLimit !== 32767 ? ` stroke-miterlimit="${num(Math.max(1, s.mitreLimit))}"` : "";
|
|
1187
|
+
return `fill="none" ${paint("stroke", colour)} stroke-width="${num(w)}" stroke-linecap="${capOf(cap)}" stroke-linejoin="${joinOf(join)}"${ml}${d ? ` stroke-dasharray="${d}"` : ""}${off}${fixed ? " vector-effect=\"non-scaling-stroke\"" : ""}`;
|
|
1188
|
+
};
|
|
1189
|
+
const fixedMode = (m) => m === 1 || m === 3;
|
|
1190
|
+
/** 経路を、命令ごとの区間 (始めの点と命令) に分ける (M・L・C・Q・Z の絶対座標だけ) */
|
|
1191
|
+
const pathParts = (d) => {
|
|
1192
|
+
const toks = d.match(/[MLCQZ]|-?\d+(?:\.\d+)?(?:e[-+]?\d+)?/g) ?? [];
|
|
1193
|
+
const out = [];
|
|
1194
|
+
let cur = ["0", "0"];
|
|
1195
|
+
let start = cur;
|
|
1196
|
+
for (let i = 0; i < toks.length;) {
|
|
1197
|
+
const c = toks[i++];
|
|
1198
|
+
const n = c === "C" ? 6 : c === "Q" ? 4 : c === "Z" ? 0 : 2;
|
|
1199
|
+
const a = toks.slice(i, i + n);
|
|
1200
|
+
i += n;
|
|
1201
|
+
if (c === "M") {
|
|
1202
|
+
cur = start = a;
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
if (c === "Z") {
|
|
1206
|
+
out.push({
|
|
1207
|
+
from: cur,
|
|
1208
|
+
cmd: "L",
|
|
1209
|
+
args: start
|
|
1210
|
+
});
|
|
1211
|
+
cur = start;
|
|
1212
|
+
continue;
|
|
1213
|
+
}
|
|
1214
|
+
out.push({
|
|
1215
|
+
from: cur,
|
|
1216
|
+
cmd: c,
|
|
1217
|
+
args: a
|
|
1218
|
+
});
|
|
1219
|
+
cur = a.slice(-2);
|
|
1220
|
+
}
|
|
1221
|
+
return out;
|
|
1222
|
+
};
|
|
1223
|
+
/** 区間の長さ (曲線は 16 の折れ線で測る) */
|
|
1224
|
+
const partLength = (q) => {
|
|
1225
|
+
const v = [...q.from, ...q.args].map(Number);
|
|
1226
|
+
const pt = (t) => {
|
|
1227
|
+
const u = 1 - t;
|
|
1228
|
+
if (q.cmd === "C") return [u * u * u * v[0] + 3 * u * u * t * v[2] + 3 * u * t * t * v[4] + t * t * t * v[6], u * u * u * v[1] + 3 * u * u * t * v[3] + 3 * u * t * t * v[5] + t * t * t * v[7]];
|
|
1229
|
+
if (q.cmd === "Q") return [u * u * v[0] + 2 * u * t * v[2] + t * t * v[4], u * u * v[1] + 2 * u * t * v[3] + t * t * v[5]];
|
|
1230
|
+
return [v[0] + (v[2] - v[0]) * t, v[1] + (v[3] - v[1]) * t];
|
|
1231
|
+
};
|
|
1232
|
+
const n = q.cmd === "L" ? 1 : 16;
|
|
1233
|
+
let len = 0;
|
|
1234
|
+
for (let k = 1; k <= n; k++) {
|
|
1235
|
+
const a = pt((k - 1) / n);
|
|
1236
|
+
const b = pt(k / n);
|
|
1237
|
+
len += Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
1238
|
+
}
|
|
1239
|
+
return len;
|
|
1240
|
+
};
|
|
1241
|
+
/** LINE TYPE CONTINUATION の restart: 頂点ごとに線種を始め直す (区間ごとに新しい部分経路にする。SVG の破線は部分経路ごとに始まる) */
|
|
1242
|
+
const restartAtVertices = (d) => pathParts(d).map((q) => `M${q.from[0]} ${q.from[1]}${q.cmd}${q.args.join(" ")}`).join("");
|
|
1243
|
+
/**
|
|
1244
|
+
* LINE TYPE CONTINUATION の adaptive continue: 頂点 (と始めと終わりの点) を必ず描く。区間ごとに、線種を伸び縮みさせて、
|
|
1245
|
+
* 区間の長さが「周期の整数倍 + 最初の線の長さ」になるようにする (区間の始めと終わりが線の上に来る)
|
|
1246
|
+
*/
|
|
1247
|
+
const adaptiveDash = (d, dash, vertices) => {
|
|
1248
|
+
const list = dash.split(" ").map(Number);
|
|
1249
|
+
const cycle = list.reduce((a, v) => a + v, 0);
|
|
1250
|
+
const d0 = list[0] ?? 0;
|
|
1251
|
+
const parts = pathParts(d);
|
|
1252
|
+
return (vertices ? parts.map((q) => [q]) : [parts]).filter((g) => g.length).map((g) => {
|
|
1253
|
+
const len = g.reduce((a, q) => a + partLength(q), 0);
|
|
1254
|
+
const n = Math.max(0, Math.round((len - d0) / cycle));
|
|
1255
|
+
const k = n * cycle + d0 > 0 ? len / (n * cycle + d0) : 1;
|
|
1256
|
+
return [`M${g[0].from[0]} ${g[0].from[1]}` + g.map((q) => `${q.cmd}${q.args.join(" ")}`).join(""), list.map((v) => num(v * k)).join(" ")];
|
|
1257
|
+
});
|
|
1258
|
+
};
|
|
1259
|
+
/** 線・辺の経路を描く要素 (LINE / EDGE TYPE CONTINUATION を掛ける)。vertices: 経路の点が要素の頂点か (円弧などを折れ線にした点でなく) */
|
|
1260
|
+
const strokeEl = (d, edge, vertices = true) => {
|
|
1261
|
+
const w = edge ? edgeWidthPx() : lineWidthPx();
|
|
1262
|
+
const type = edge ? s.edgeType : s.lineType;
|
|
1263
|
+
const dash = dashOf(type, w);
|
|
1264
|
+
const cont = edge ? s.edgeCont : s.lineCont;
|
|
1265
|
+
const attrs = (dd, offset = edge ? s.edgeOffset : s.lineOffset) => edge ? strokeAttrs(s.edgeColour, w, type, s.edgeCap, s.edgeJoin, fixedMode(s.edgeWidthMode), offset, dd) : strokeAttrs(s.lineColour, w, type, s.lineCap, s.lineJoin, fixedMode(s.lineWidthMode), offset, dd);
|
|
1266
|
+
if (dash && cont === 3 && vertices) return `<path d="${restartAtVertices(d)}" ${attrs()}${clipAttr()}/>`;
|
|
1267
|
+
if (dash && cont === 4) return adaptiveDash(d, dash, vertices).map(([sd, dd]) => `<path d="${sd}" ${attrs(dd, 0)}${clipAttr()}/>`).join("");
|
|
1268
|
+
return `<path d="${d}" ${attrs()}${clipAttr()}/>`;
|
|
1269
|
+
};
|
|
1270
|
+
/** セグメントを写している間は、要素ごとの切り抜きを付けない (写した全体に、写した時の切り抜きを付ける) */
|
|
1271
|
+
let replayDepth = 0;
|
|
1272
|
+
let replayBudget = 2e5;
|
|
1273
|
+
const clipAttr = () => clipId && !replayDepth ? ` clip-path="url(#${clipId})"` : "";
|
|
1274
|
+
const pathOf = (pts, close) => pts.map((p, k) => `${k ? "L" : "M"}${num(X(p[0]))} ${num(Y(p[1]))}`).join("") + (close ? "Z" : "");
|
|
1275
|
+
/**
|
|
1276
|
+
* 出力の SVG の長さの勘定 (imageLimits.ts の metafileSvgBudget。画像の data: の中身と模様の中身は数えない: 画素の合計の上限で縛ってある)。
|
|
1277
|
+
* 上限を超えたら、警告を出して、その後は何も描かない (overflow)。COPY SEGMENT (24 バイト) は、点の多いセグメントを写すたびに全部を書くので、
|
|
1278
|
+
* 写した要素の数だけを数える上限 (replayBudget) では、33KB の CGM で SVG が数千万字 (18 秒・1.2GB) になった。
|
|
1279
|
+
* セグメントを写して描いた分は、写すたびに (emit で) 数えるので、写した量がそのまま勘定に入る
|
|
1280
|
+
*/
|
|
1281
|
+
const svgBudget = metafileSvgBudget(bytes.length);
|
|
1282
|
+
let svgChars = 0;
|
|
1283
|
+
let overflow = false;
|
|
1284
|
+
const room = (n) => {
|
|
1285
|
+
if (overflow) return false;
|
|
1286
|
+
if (svgChars + n > svgBudget) {
|
|
1287
|
+
overflow = true;
|
|
1288
|
+
warn(msg(`CGM を SVG にした長さが上限 (${bytes.length} バイトのファイルでは ${svgBudget} 字) を超えるので、途中までを描きます`, `the SVG made from the CGM exceeds the length limit (${svgBudget} characters for a file of ${bytes.length} bytes); only the first part is drawn`));
|
|
1289
|
+
return false;
|
|
1290
|
+
}
|
|
1291
|
+
svgChars += n;
|
|
1292
|
+
return true;
|
|
1293
|
+
};
|
|
1294
|
+
/** 要素を出す。uncounted は、el のうち勘定に入れない字数 (画像の中身、写したセグメントの中身 (写した時に数えてある)) */
|
|
1295
|
+
const emit = (el, uncounted = 0) => {
|
|
1296
|
+
if (!room(el.length - uncounted)) return;
|
|
1297
|
+
body.push(el);
|
|
1298
|
+
count++;
|
|
1299
|
+
};
|
|
1300
|
+
/** 定義 (defs) を足す。長さは勘定に入れるが、捨てはしない (引く側の要素が先に出ていることがあるので)。上限を超えた後は足さない */
|
|
1301
|
+
const addDef = (el, uncounted = 0) => {
|
|
1302
|
+
if (overflow) return;
|
|
1303
|
+
room(el.length - uncounted);
|
|
1304
|
+
defs.push(el);
|
|
1305
|
+
};
|
|
1306
|
+
/**
|
|
1307
|
+
* 閉じた図形 (BEGIN FIGURE 〜 END FIGURE。第 1 部 6.6.11)。中の線・塗りの要素は、その場では描かず、領域の境界 (regions) と
|
|
1308
|
+
* 辺 (edges: 辺の属性で描く部分) に集め、END FIGURE で、内側の塗り方に従って全体を 1 つの塗りとして描く
|
|
1309
|
+
*/
|
|
1310
|
+
let fig = null;
|
|
1311
|
+
/** CONNECTING EDGE (第 1 部 7.6.21): 前の線の要素の終わりの点から、次の点へ、辺を足す。閉じた図形の中で待っている間の始めの点 */
|
|
1312
|
+
let connectFrom;
|
|
1313
|
+
/** 閉じた図形の中の、前の線の要素の終わりの点と、いまの領域の始めの点 (px) */
|
|
1314
|
+
let figLast;
|
|
1315
|
+
let figStart;
|
|
1316
|
+
/**
|
|
1317
|
+
* セグメント (第 1 部 6.10): BEGIN SEGMENT 〜 END SEGMENT の要素を、始めの時の状態とともに覚えておき、COPY SEGMENT・geometric pattern で
|
|
1318
|
+
* 同じ要素をもう一度読んで描く (属性は定義した時のもの。INHERITANCE FILTER の既定 'segment')。
|
|
1319
|
+
* 絵の本体で定義したセグメントはその場でも描く。メタファイル記述子 (大域のセグメント)・絵の記述子で定義したものは、描かずに覚えるだけ
|
|
1320
|
+
*/
|
|
1321
|
+
const segments = /* @__PURE__ */ new Map();
|
|
1322
|
+
let rec;
|
|
1323
|
+
/** 2 行 3 列の行列 (a11 a12 a21 a22 a13 a23。x' = a11·x + a12·y + a13、y' = a21·x + a22·y + a23。第 3 部 表 11) を VDC の写しにする */
|
|
1324
|
+
const xfOf = (m) => (q) => [m[0] * q[0] + m[1] * q[1] + m[4], m[2] * q[0] + m[3] * q[1] + m[5]];
|
|
1325
|
+
const fmtM = (m) => m.map((v) => String(Math.round(v * 1e4) / 1e4)).join(" ");
|
|
1326
|
+
/** セグメントの要素を読み直し、描いた SVG を返す (状態は元に戻す)。入れ子は 8 段まで (自分を写すセグメントで止まらなくならないように) */
|
|
1327
|
+
const replaySegment = (id, inheritFrom) => {
|
|
1328
|
+
const seg = segments.get(id);
|
|
1329
|
+
if (!seg) {
|
|
1330
|
+
warn(msg(`セグメント ${id} は定義されていません`, `segment ${id} is not defined`));
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
if (replayDepth >= 8 || replayBudget <= 0) {
|
|
1334
|
+
warn(msg("COPY SEGMENT の入れ子が深すぎるか多すぎるので、途中から描きません", "COPY SEGMENT is nested too deeply or used too many times; the rest is not drawn"));
|
|
1335
|
+
return "";
|
|
1336
|
+
}
|
|
1337
|
+
if (overflow) return "";
|
|
1338
|
+
const saved = {
|
|
1339
|
+
s,
|
|
1340
|
+
body,
|
|
1341
|
+
fig,
|
|
1342
|
+
pendingText,
|
|
1343
|
+
compound
|
|
1344
|
+
};
|
|
1345
|
+
s = cloneState(seg.snap);
|
|
1346
|
+
const keys = inheritFrom?.inherit ?? [];
|
|
1347
|
+
const inheritNow = () => {
|
|
1348
|
+
if (!inheritFrom) return;
|
|
1349
|
+
const dst = s;
|
|
1350
|
+
const src = inheritFrom;
|
|
1351
|
+
for (const k of keys) if (k.startsWith("asf")) s.asf[Number(k.slice(3))] = inheritFrom.asf[Number(k.slice(3))];
|
|
1352
|
+
else dst[k] = Array.isArray(src[k]) ? [...src[k]] : src[k];
|
|
1353
|
+
};
|
|
1354
|
+
inheritNow();
|
|
1355
|
+
body = [];
|
|
1356
|
+
fig = null;
|
|
1357
|
+
compound = null;
|
|
1358
|
+
replayDepth++;
|
|
1359
|
+
try {
|
|
1360
|
+
for (const e of seg.els) {
|
|
1361
|
+
if (--replayBudget < 0) break;
|
|
1362
|
+
process(e, s, false);
|
|
1363
|
+
if (keys.length && (e.cls === 5 || e.cls === 3)) inheritNow();
|
|
1364
|
+
}
|
|
1365
|
+
return body.join("");
|
|
1366
|
+
} finally {
|
|
1367
|
+
replayDepth--;
|
|
1368
|
+
({s, body, fig, pendingText, compound} = saved);
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
/**
|
|
1372
|
+
* タイルの並び (BEGIN TILE ARRAY 〜 END TILE ARRAY。第 1 部 7.2.16・6.6.5.2): タイル (TILE・BITONAL TILE) を、セルの進む向きに並べ、
|
|
1373
|
+
* 行の進む向きに折り返して 1 枚の画素にし、END TILE ARRAY で、中身のある範囲 (画像のずれと大きさ) を描く
|
|
1374
|
+
*/
|
|
1375
|
+
let tiles;
|
|
1376
|
+
/** 開いている領域を閉じる (閉じる点へ戻る暗黙の境界は、Z で表す) */
|
|
1377
|
+
const closeRegion = () => {
|
|
1378
|
+
if (fig?.cur) fig.regions.push(fig.cur + "Z");
|
|
1379
|
+
if (fig) fig.cur = "";
|
|
1380
|
+
connectFrom = void 0;
|
|
1381
|
+
figLast = void 0;
|
|
1382
|
+
};
|
|
1383
|
+
/**
|
|
1384
|
+
* 複合線・複合の文字の経路 (BEGIN COMPOUND LINE 〜 END COMPOUND LINE・BEGIN COMPOUND TEXT PATH 〜 END。第 1 部 6.6.1.2・6.7.8):
|
|
1385
|
+
* 中の線の要素をつないで 1 本の経路にする (続いていない要素の間は直線でつなぐ)。複合線は END で 1 本の線として描き、文字の経路は描かずに覚える
|
|
1386
|
+
*/
|
|
1387
|
+
let compound = null;
|
|
1388
|
+
/** 線の要素を描く。閉じた図形の中では、領域の境界に足す (前の要素の終わりから、この要素の始めへ、暗黙の境界でつなぐ)。vertices は strokeEl と同じ */
|
|
1389
|
+
const line = (d, vertices = true) => {
|
|
1390
|
+
if (compound) {
|
|
1391
|
+
compound.d += compound.d ? d.replace(/^M/, "L") : d;
|
|
1392
|
+
compound.vertices &&= vertices;
|
|
1393
|
+
return;
|
|
1394
|
+
}
|
|
1395
|
+
if (!fig) {
|
|
1396
|
+
emit(strokeEl(d, false, vertices));
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1399
|
+
const ends = pathEnds(d);
|
|
1400
|
+
if (ends) connectEdge(ends[0]);
|
|
1401
|
+
const joined = d.replace(/M/g, "L");
|
|
1402
|
+
if (!fig.cur && ends) figStart = ends[0];
|
|
1403
|
+
fig.cur += fig.cur ? joined : "M" + joined.slice(1);
|
|
1404
|
+
if (ends) figLast = ends[1];
|
|
1405
|
+
if (s.edgeVisible) fig.edges.push(strokeEl(d, true, vertices));
|
|
1406
|
+
};
|
|
1407
|
+
/** 経路 (M・L・C・Q の絶対座標だけ) の最初と最後の点 */
|
|
1408
|
+
const pathEnds = (d) => {
|
|
1409
|
+
const n = (d.match(/-?\d+(?:\.\d+)?(?:e-?\d+)?/g) ?? []).map(Number);
|
|
1410
|
+
return n.length >= 2 ? [[n[0], n[1]], [n[n.length - 2], n[n.length - 1]]] : void 0;
|
|
1411
|
+
};
|
|
1412
|
+
/** CONNECTING EDGE の辺を足す (待っていれば。to は次の点 px) */
|
|
1413
|
+
const connectEdge = (to) => {
|
|
1414
|
+
if (!connectFrom || !to || !fig) return;
|
|
1415
|
+
if (s.edgeVisible) fig.edges.push(strokeEl(`M${num(connectFrom[0])} ${num(connectFrom[1])}L${num(to[0])} ${num(to[1])}`, true));
|
|
1416
|
+
connectFrom = void 0;
|
|
1417
|
+
};
|
|
1418
|
+
/** 塗りの領域を描く: 内側 (INTERIOR STYLE) と辺 (EDGE VISIBILITY)。d は SVG の経路。edges は辺として描く経路 (省略時は d、null なら辺を描かない) */
|
|
1419
|
+
const filled = (d, edges, vertices = true) => {
|
|
1420
|
+
if (fig) {
|
|
1421
|
+
closeRegion();
|
|
1422
|
+
fig.regions.push(d);
|
|
1423
|
+
if (s.edgeVisible && edges !== null && (edges ?? d)) fig.edges.push(strokeEl(edges ?? d, true, vertices));
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
const st = s.interiorStyle;
|
|
1427
|
+
if (st === 1) emit(`<path d="${d}" ${paint("fill", s.fillColour)} fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1428
|
+
else if (st === 0) emit(`<path d="${d}" ${strokeAttrs(s.fillColour, NOMINAL_LINE_WIDTH, 1, 1, 1, true)}${clipAttr()}/>`);
|
|
1429
|
+
else if (st === 3) for (const id of hatchPatterns(s.hatchIndex, colourOf(s.fillColour))) emit(`<path d="${d}" fill="url(#${id})" fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1430
|
+
else if (st === 4) {} else if (st === 2 || st === 5) {
|
|
1431
|
+
const id = st === 2 ? tablePattern() : geometricPattern();
|
|
1432
|
+
if (id) emit(`<path d="${d}" fill="url(#${id})" fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1433
|
+
else {
|
|
1434
|
+
warn(msg(`内側の塗り方 ${st === 2 ? "pattern" : "geometric pattern"} の模様が定められていないので、塗りの色で塗ります`, `the pattern for interior style ${st === 2 ? "pattern" : "geometric pattern"} is not defined; the fill color is used`));
|
|
1435
|
+
emit(`<path d="${d}" ${paint("fill", s.fillColour)} fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1436
|
+
}
|
|
1437
|
+
} else if (st === 6) {
|
|
1438
|
+
const g = s.interp ?? defaultInterp();
|
|
1439
|
+
if (g.style === 3) triangleFill(d, g);
|
|
1440
|
+
else emit(`<path d="${d}" fill="url(#${gradient(g)})" fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1441
|
+
} else {
|
|
1442
|
+
warn(msg(`内側の塗り方 ${st} は塗りの色で塗ります`, `interior style ${st} is filled with the fill color`));
|
|
1443
|
+
emit(`<path d="${d}" ${paint("fill", s.fillColour)} fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1444
|
+
}
|
|
1445
|
+
if (s.edgeVisible && edges !== null && (edges ?? d)) emit(strokeEl(edges ?? d, true, vertices));
|
|
1446
|
+
};
|
|
1447
|
+
let gradSeq = 0;
|
|
1448
|
+
/**
|
|
1449
|
+
* 色の補間 (INTERPOLATED INTERIOR。第 1 部 7.7.43・6.7.4.3.2) を SVG のグラデーションにする。
|
|
1450
|
+
* 平行: 塗りの基準点 F から基準の点 F + v へ向かう線 L 上で、段の区切り (F からの距離 |v|×S_i) に色を置き、間を線形に補間する。L に垂直な線の上は同じ色、外側は端の色
|
|
1451
|
+
* 楕円: F を中心、(F からの) 2 つの共役半径の楕円を基準に、同心の楕円の上は同じ色
|
|
1452
|
+
* 三角形は triangleFill (画素で塗る)
|
|
1453
|
+
*/
|
|
1454
|
+
const gradient = (g) => {
|
|
1455
|
+
const F = fillRefPoint();
|
|
1456
|
+
const g1 = interiorVec(g.geo[0] ?? 0, g.geo[1] ?? 0, s.interiorMode);
|
|
1457
|
+
const g2 = interiorVec(g.geo[2] ?? 0, g.geo[3] ?? 0, s.interiorMode);
|
|
1458
|
+
const id = `cgmg${gradSeq++}`;
|
|
1459
|
+
const cols = g.colours.map((c) => hex(colourOf(c)));
|
|
1460
|
+
const stops = [0, ...g.stages];
|
|
1461
|
+
const last = stops[stops.length - 1] || 1;
|
|
1462
|
+
const stopTags = stops.map((v, k) => `<stop offset="${num(v / last)}" stop-color="${cols[Math.min(k, cols.length - 1)] ?? "#000000"}"/>`).join("");
|
|
1463
|
+
if (g.style === 1) {
|
|
1464
|
+
const a = map(F);
|
|
1465
|
+
const b = map([F[0] + g1[0] * last, F[1] + g1[1] * last]);
|
|
1466
|
+
addDef(`<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${num(a[0])}" y1="${num(a[1])}" x2="${num(b[0])}" y2="${num(b[1])}">${stopTags}</linearGradient>`);
|
|
1467
|
+
} else {
|
|
1468
|
+
const c = map(F);
|
|
1469
|
+
const u = mapVec(g1);
|
|
1470
|
+
const v = mapVec(g2);
|
|
1471
|
+
addDef(`<radialGradient id="${id}" gradientUnits="userSpaceOnUse" cx="0" cy="0" r="${num(last)}" gradientTransform="matrix(${[
|
|
1472
|
+
u[0],
|
|
1473
|
+
u[1],
|
|
1474
|
+
v[0],
|
|
1475
|
+
v[1],
|
|
1476
|
+
c[0],
|
|
1477
|
+
c[1]
|
|
1478
|
+
].map(num).join(" ")})">${stopTags}</radialGradient>`);
|
|
1479
|
+
}
|
|
1480
|
+
return id;
|
|
1481
|
+
};
|
|
1482
|
+
/** INTERPOLATED INTERIOR の既定値 (第 1 部 8 章): 平行、基準は既定の VDC EXTENT、段は 1 つ (1.0)、色は背景色 (indexed なら 0 番) */
|
|
1483
|
+
const defaultInterp = () => {
|
|
1484
|
+
const e = defaultExtent(defaults);
|
|
1485
|
+
const bgc = s.colourSelection === 1 ? { rgb: s.background ?? DEVICE_BACKGROUND } : { index: 0 };
|
|
1486
|
+
return {
|
|
1487
|
+
style: 1,
|
|
1488
|
+
geo: [e[1][0] - e[0][0], e[1][1] - e[0][1]],
|
|
1489
|
+
stages: [1],
|
|
1490
|
+
colours: [bgc, bgc]
|
|
1491
|
+
};
|
|
1492
|
+
};
|
|
1493
|
+
/** 塗りの基準点 (FILL REFERENCE POINT。既定は既定の VDC EXTENT の左下) */
|
|
1494
|
+
const fillRefPoint = () => {
|
|
1495
|
+
const e = defaultExtent(defaults);
|
|
1496
|
+
return s.fillRef ?? [Math.min(e[0][0], e[1][0]), Math.min(e[0][1], e[1][1])];
|
|
1497
|
+
};
|
|
1498
|
+
/**
|
|
1499
|
+
* 内側の大きさの指定 (INTERIOR STYLE SPECIFICATION MODE。第 1 部 6.7.5) の向き (x, y) を VDC の向きにする。
|
|
1500
|
+
* absolute は VDC のまま。ほかは表示装置の上の大きさ (x は右、y は上) なので、px にしてから VDC に戻す
|
|
1501
|
+
*/
|
|
1502
|
+
const interiorVec = (x, y, mode) => {
|
|
1503
|
+
if (mode === 0) return [x, y];
|
|
1504
|
+
const k = mode === 1 ? NOMINAL_LINE_WIDTH * 4 : mode === 2 ? W : PX_PER_MM;
|
|
1505
|
+
const sx = W / (ext[1][0] - ext[0][0]);
|
|
1506
|
+
const sy = H / (ext[1][1] - ext[0][1]);
|
|
1507
|
+
return [x * k / sx, y * k / sy];
|
|
1508
|
+
};
|
|
1509
|
+
/** VDC の 1 次の写し (x' = a·x + b·y + e、y' = c·x + d·y + f) を、SVG の px の行列 (matrix(a b c d e f)) にする */
|
|
1510
|
+
const svgMatrix = (f) => {
|
|
1511
|
+
const inv = (q) => [ext[0][0] + q[0] / W * (ext[1][0] - ext[0][0]), ext[1][1] - q[1] / H * (ext[1][1] - ext[0][1])];
|
|
1512
|
+
const o = map(f(inv([0, 0])));
|
|
1513
|
+
const ex = map(f(inv([1, 0])));
|
|
1514
|
+
const ey = map(f(inv([0, 1])));
|
|
1515
|
+
return [
|
|
1516
|
+
ex[0] - o[0],
|
|
1517
|
+
ex[1] - o[1],
|
|
1518
|
+
ey[0] - o[0],
|
|
1519
|
+
ey[1] - o[1],
|
|
1520
|
+
o[0],
|
|
1521
|
+
o[1]
|
|
1522
|
+
];
|
|
1523
|
+
};
|
|
1524
|
+
/** 画素 (RGBA) を、単位の正方形を行列 m で写した位置に置く <image> (PNG)。cells ならセルの境目をぼかさない (image-rendering: pixelated) */
|
|
1525
|
+
const imageEl = (rgba, w, h, m, cells = true) => `<image x="0" y="0" width="1" height="1" preserveAspectRatio="none"${cells ? " style=\"image-rendering:pixelated\"" : ""} transform="matrix(${m.map((v) => String(Math.round(v * 1e4) / 1e4)).join(" ")})" href="data:image/png;base64,${base64(encodePngStored({
|
|
1526
|
+
width: w,
|
|
1527
|
+
height: h,
|
|
1528
|
+
rgba
|
|
1529
|
+
}))}"/>`;
|
|
1530
|
+
/**
|
|
1531
|
+
* この文書で画素にできる残りの数 (imageLimits.ts の metafilePixelBudget: 合計の上限と、ファイルの大きさに応じた上限の小さい方)。
|
|
1532
|
+
* 足りなければ警告して false (その画像は描かない)
|
|
1533
|
+
*/
|
|
1534
|
+
const pixelBudget = metafilePixelBudget(bytes.length);
|
|
1535
|
+
let pixelsLeft = pixelBudget;
|
|
1536
|
+
const takePixels = (n, what) => {
|
|
1537
|
+
if (n > pixelsLeft) {
|
|
1538
|
+
warn(msg(`${what} は、文書の画像の画素の合計が上限 (${bytes.length} バイトのファイルでは ${pixelBudget} 画素) を超えるので描きません`, `${what} is not drawn because the total pixels of the images in the document exceed the limit (${pixelBudget} pixels for a file of ${bytes.length} bytes)`));
|
|
1539
|
+
return false;
|
|
1540
|
+
}
|
|
1541
|
+
pixelsLeft -= n;
|
|
1542
|
+
return true;
|
|
1543
|
+
};
|
|
1544
|
+
let patSeq = 0;
|
|
1545
|
+
/** PATTERN TABLE の定義の通し番号 (模様の中身を見分ける鍵に使う) */
|
|
1546
|
+
let patternTableSeq = 0;
|
|
1547
|
+
/**
|
|
1548
|
+
* PATTERN TABLE の模様で塗るたびに調べる「表の中の違う色の数」の合計の上限 (2,000 万。1 つ約 50 ナノ秒で、約 1 秒)。
|
|
1549
|
+
* 正当な模様の色は多くて数百 (色番号 8 ビットの表で 256) なので、数万回塗っても届かない
|
|
1550
|
+
*/
|
|
1551
|
+
let patternWork = 2e7;
|
|
1552
|
+
/** geometric pattern のセグメントを写した結果の控え (セグメントの番号 → 写した SVG と、模様の中身の id)。セグメントを定義し直したら捨てる */
|
|
1553
|
+
const geoFrags = /* @__PURE__ */ new Map();
|
|
1554
|
+
/**
|
|
1555
|
+
* 模様の定義を、使うたびに書き出さない。中身 (セルの矩形・画像、geometric pattern のセグメントの図形) は、同じ中身ごとに 1 つの
|
|
1556
|
+
* pattern に書き (patternBodies: 中身 → id)、使う所は、それを href で引いて置き方 (位置・大きさ・patternTransform) だけを書いた pattern にする
|
|
1557
|
+
* (SVG 1.1 §13.3 の xlink:href・SVG 2 §14.3.4 の href: 引いた pattern の中身を受け継ぐ)。同じ中身・同じ置き方なら同じ pattern を使う
|
|
1558
|
+
* (patternUses: 中身の id と置き方 → id)。前は使うたびに中身ごと書いたので、14KB の CGM で SVG が 2.9 億字 (<rect> 410 万個) になった
|
|
1559
|
+
*/
|
|
1560
|
+
const patternBodies = /* @__PURE__ */ new Map();
|
|
1561
|
+
const patternUses = /* @__PURE__ */ new Map();
|
|
1562
|
+
/**
|
|
1563
|
+
* 中身の pattern の id。key は中身を見分ける鍵 (中身の文字列そのもの、または中身を決める値)。新しい中身なら content() で作って定義を書き、
|
|
1564
|
+
* cost を画素の合計に数える (足りなければ undefined)
|
|
1565
|
+
*/
|
|
1566
|
+
const patternBody = (key, content, cost, what) => {
|
|
1567
|
+
let id = patternBodies.get(key);
|
|
1568
|
+
if (id) return id;
|
|
1569
|
+
if (!takePixels(cost, what)) return void 0;
|
|
1570
|
+
id = `cgmp${patSeq++}`;
|
|
1571
|
+
patternBodies.set(key, id);
|
|
1572
|
+
const inner = content();
|
|
1573
|
+
addDef(`<pattern id="${id}" patternUnits="userSpaceOnUse">${inner}</pattern>`, inner.length);
|
|
1574
|
+
return id;
|
|
1575
|
+
};
|
|
1576
|
+
/** 中身の pattern (body) を、置き方 (place: pattern の属性) で敷く pattern の id */
|
|
1577
|
+
const patternUse = (body, place) => {
|
|
1578
|
+
const key = `${body}|${place}`;
|
|
1579
|
+
let id = patternUses.get(key);
|
|
1580
|
+
if (id) return id;
|
|
1581
|
+
id = `cgmp${patSeq++}`;
|
|
1582
|
+
patternUses.set(key, id);
|
|
1583
|
+
addDef(`<pattern id="${id}" href="#${body}" ${place}/>`);
|
|
1584
|
+
return id;
|
|
1585
|
+
};
|
|
1586
|
+
/**
|
|
1587
|
+
* 模様のセルを矩形で書くときの、1 セルの重み (画素の合計に数える数)。<rect> 1 つは約 70 字で、画素を PNG (圧縮しない形) の base64 で書くときの
|
|
1588
|
+
* 1 画素 (約 5.3 字) の 13 倍ほどなので、16 と数える (画素の合計の上限が、SVG の長さの上限としても同じ程度に効くように)
|
|
1589
|
+
*/
|
|
1590
|
+
const RECT_CELL_WEIGHT = 16;
|
|
1591
|
+
/**
|
|
1592
|
+
* PATTERN TABLE の模様 (第 1 部 7.7.32・7.7.33): 塗りの基準点 F と、PATTERN SIZE の高さの向き h・幅の向き w の平行四辺形 (模様の箱) を
|
|
1593
|
+
* nx × ny のセルに分ける。色の (1, ny) が F のセル、第 1 の添字は w の向き、第 2 の添字は h の向きに減る (1 行目が箱の h の側)。箱を繰り返して敷く
|
|
1594
|
+
*/
|
|
1595
|
+
const tablePattern = () => {
|
|
1596
|
+
const t = s.patternTable.get(s.patternIndex) ?? (s.patternIndex === 1 ? {
|
|
1597
|
+
nx: 1,
|
|
1598
|
+
ny: 1,
|
|
1599
|
+
uid: 0,
|
|
1600
|
+
palette: [s.colourSelection === 1 ? { rgb: DEVICE_FOREGROUND } : { index: 1 }],
|
|
1601
|
+
cells: /* @__PURE__ */ new Uint32Array(1)
|
|
1602
|
+
} : void 0);
|
|
1603
|
+
if (!t || t.nx < 1 || t.ny < 1 || t.nx * t.ny > 1e8) return void 0;
|
|
1604
|
+
const box = patternBox();
|
|
1605
|
+
if (!box) return void 0;
|
|
1606
|
+
const cells = t.nx * t.ny;
|
|
1607
|
+
const rects = cells <= 4096;
|
|
1608
|
+
patternWork -= t.palette.length;
|
|
1609
|
+
if (patternWork < 0) {
|
|
1610
|
+
warn(msg("PATTERN TABLE の模様の色を調べる手間が上限を超えたので、その後の模様の塗りは描きません", "checking the colors of PATTERN TABLE patterns exceeded the work limit; later pattern fills are not drawn"));
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1613
|
+
const pal = t.palette.map(colourOf);
|
|
1614
|
+
let h1 = 2166136261;
|
|
1615
|
+
let h2 = 2538058380;
|
|
1616
|
+
for (const c of pal) for (let j = 0; j < 3; j++) {
|
|
1617
|
+
const v = Math.round(Math.min(1, Math.max(0, c[j])) * 255);
|
|
1618
|
+
h1 = Math.imul(h1 ^ v, 16777619);
|
|
1619
|
+
h2 = Math.imul((h2 ^ v) + 2654435769, 1540483477);
|
|
1620
|
+
}
|
|
1621
|
+
const key = `t${t.uid}:${t.nx}x${t.ny}:${(h1 >>> 0).toString(16)}:${(h2 >>> 0).toString(16)}`;
|
|
1622
|
+
const [F, wv, hv] = box;
|
|
1623
|
+
const o = map([F[0] + hv[0], F[1] + hv[1]]);
|
|
1624
|
+
const a = mapVec(wv);
|
|
1625
|
+
const c = mapVec([-hv[0], -hv[1]]);
|
|
1626
|
+
const content = () => {
|
|
1627
|
+
if (rects) return Array.from(t.cells, (n, k) => `<rect x="${num(k % t.nx / t.nx)}" y="${num(Math.floor(k / t.nx) / t.ny)}" width="${num(1 / t.nx + 1e-4)}" height="${num(1 / t.ny + 1e-4)}" fill="${hex(pal[n])}"/>`).join("");
|
|
1628
|
+
const rgba = new Uint8Array(cells * 4);
|
|
1629
|
+
const bytes4 = pal.map((c) => [
|
|
1630
|
+
Math.round(c[0] * 255),
|
|
1631
|
+
Math.round(c[1] * 255),
|
|
1632
|
+
Math.round(c[2] * 255)
|
|
1633
|
+
]);
|
|
1634
|
+
for (let k = 0; k < cells; k++) {
|
|
1635
|
+
const c = bytes4[t.cells[k]];
|
|
1636
|
+
rgba[k * 4] = c[0];
|
|
1637
|
+
rgba[k * 4 + 1] = c[1];
|
|
1638
|
+
rgba[k * 4 + 2] = c[2];
|
|
1639
|
+
rgba[k * 4 + 3] = 255;
|
|
1640
|
+
}
|
|
1641
|
+
return `<image x="0" y="0" width="1" height="1" preserveAspectRatio="none" style="image-rendering:pixelated" href="data:image/png;base64,${base64(encodePngStored({
|
|
1642
|
+
width: t.nx,
|
|
1643
|
+
height: t.ny,
|
|
1644
|
+
rgba
|
|
1645
|
+
}))}"/>`;
|
|
1646
|
+
};
|
|
1647
|
+
const body = patternBody(key, content, rects ? cells * RECT_CELL_WEIGHT : cells, msg("PATTERN TABLE の模様", "a PATTERN TABLE pattern"));
|
|
1648
|
+
if (!body) return void 0;
|
|
1649
|
+
return patternUse(body, `patternUnits="userSpaceOnUse" x="0" y="0" width="1" height="1" patternTransform="matrix(${[
|
|
1650
|
+
a[0],
|
|
1651
|
+
a[1],
|
|
1652
|
+
c[0],
|
|
1653
|
+
c[1],
|
|
1654
|
+
o[0],
|
|
1655
|
+
o[1]
|
|
1656
|
+
].map((v) => String(Math.round(v * 1e4) / 1e4)).join(" ")})"`);
|
|
1657
|
+
};
|
|
1658
|
+
/** 模様の箱 (第 1 部 7.7.33): 基準点 F・幅の向き w・高さの向き h (VDC)。既定は (0, dy, dx, 0): 既定の VDC EXTENT の高さと幅 (absolute のとき) */
|
|
1659
|
+
const patternBox = () => {
|
|
1660
|
+
const e = defaultExtent(defaults);
|
|
1661
|
+
const ps = s.patternSize ?? (s.interiorMode === 0 ? [
|
|
1662
|
+
0,
|
|
1663
|
+
Math.abs(e[1][1] - e[0][1]),
|
|
1664
|
+
Math.abs(e[1][0] - e[0][0]),
|
|
1665
|
+
0
|
|
1666
|
+
] : [
|
|
1667
|
+
0,
|
|
1668
|
+
1,
|
|
1669
|
+
1,
|
|
1670
|
+
0
|
|
1671
|
+
]);
|
|
1672
|
+
const hv = interiorVec(ps[0], ps[1], s.interiorMode);
|
|
1673
|
+
const wv = interiorVec(ps[2], ps[3], s.interiorMode);
|
|
1674
|
+
if (Math.abs(wv[0] * hv[1] - wv[1] * hv[0]) < 1e-12) return void 0;
|
|
1675
|
+
return [
|
|
1676
|
+
fillRefPoint(),
|
|
1677
|
+
wv,
|
|
1678
|
+
hv
|
|
1679
|
+
];
|
|
1680
|
+
};
|
|
1681
|
+
/**
|
|
1682
|
+
* geometric pattern (第 1 部 7.4.19): GEOMETRIC PATTERN DEFINITION のセグメントを、範囲 (2 点の矩形) で切り抜いて、模様の箱に写して敷く。
|
|
1683
|
+
* 範囲の左下が F、x の向きが幅の向き w、y の向きが高さの向き h に写る
|
|
1684
|
+
*/
|
|
1685
|
+
const geometricPattern = () => {
|
|
1686
|
+
const gp = s.geoPatterns.get(s.patternIndex);
|
|
1687
|
+
const box = patternBox();
|
|
1688
|
+
if (!gp || !box) return void 0;
|
|
1689
|
+
let geo = geoFrags.get(gp.seg);
|
|
1690
|
+
if (!geo) {
|
|
1691
|
+
const f = replaySegment(gp.seg);
|
|
1692
|
+
if (f === void 0) return void 0;
|
|
1693
|
+
geo = { frag: f };
|
|
1694
|
+
if (!overflow && replayBudget > 0) geoFrags.set(gp.seg, geo);
|
|
1695
|
+
}
|
|
1696
|
+
const frag = geo.frag;
|
|
1697
|
+
const [F, wv, hv] = box;
|
|
1698
|
+
const x0 = Math.min(gp.ext[0][0], gp.ext[1][0]);
|
|
1699
|
+
const x1 = Math.max(gp.ext[0][0], gp.ext[1][0]);
|
|
1700
|
+
const y0 = Math.min(gp.ext[0][1], gp.ext[1][1]);
|
|
1701
|
+
const y1 = Math.max(gp.ext[0][1], gp.ext[1][1]);
|
|
1702
|
+
if (!(x1 > x0 && y1 > y0)) return void 0;
|
|
1703
|
+
const B = (q) => {
|
|
1704
|
+
const u = (q[0] - x0) / (x1 - x0);
|
|
1705
|
+
const v = (q[1] - y0) / (y1 - y0);
|
|
1706
|
+
return [F[0] + u * wv[0] + v * hv[0], F[1] + u * wv[1] + v * hv[1]];
|
|
1707
|
+
};
|
|
1708
|
+
const a = map([x0, y1]);
|
|
1709
|
+
const b = map([x1, y0]);
|
|
1710
|
+
const m = svgMatrix(B);
|
|
1711
|
+
const body = geo.body ?? (geo.body = patternBody(`g${frag}`, () => frag, 0, msg("GEOMETRIC PATTERN の模様", "a GEOMETRIC PATTERN pattern")));
|
|
1712
|
+
if (!body) return void 0;
|
|
1713
|
+
return patternUse(body, `patternUnits="userSpaceOnUse" x="${num(Math.min(a[0], b[0]))}" y="${num(Math.min(a[1], b[1]))}" width="${num(Math.abs(b[0] - a[0]))}" height="${num(Math.abs(b[1] - a[1]))}" patternTransform="matrix(${m.map((v) => String(Math.round(v * 1e4) / 1e4)).join(" ")})"`);
|
|
1714
|
+
};
|
|
1715
|
+
/**
|
|
1716
|
+
* 三角形の色の補間 (第 1 部 7.7.43・6.7.4.3.2): 塗りの基準点 F と、F からの 2 つのずれの点の 3 つの角に色を置き、その間を 1 次で補間する
|
|
1717
|
+
* (平面の上の 1 次の関数なので、三角形の外もそのまま延ばす)。SVG のグラデーションでは表せないので、図形の外接矩形を画素で塗り、図形で切り抜く
|
|
1718
|
+
*/
|
|
1719
|
+
const triangleFill = (d, g) => {
|
|
1720
|
+
const F = fillRefPoint();
|
|
1721
|
+
const v1 = interiorVec(g.geo[0] ?? 0, g.geo[1] ?? 0, s.interiorMode);
|
|
1722
|
+
const v2 = interiorVec(g.geo[2] ?? 0, g.geo[3] ?? 0, s.interiorMode);
|
|
1723
|
+
const P = [
|
|
1724
|
+
map(F),
|
|
1725
|
+
map([F[0] + v1[0], F[1] + v1[1]]),
|
|
1726
|
+
map([F[0] + v2[0], F[1] + v2[1]])
|
|
1727
|
+
];
|
|
1728
|
+
const C = [
|
|
1729
|
+
0,
|
|
1730
|
+
1,
|
|
1731
|
+
2
|
|
1732
|
+
].map((k) => colourOf(g.colours[k] ?? { index: 1 }));
|
|
1733
|
+
const det = (P[1][0] - P[0][0]) * (P[2][1] - P[0][1]) - (P[2][0] - P[0][0]) * (P[1][1] - P[0][1]);
|
|
1734
|
+
const nums = (d.match(/-?\d+(?:\.\d+)?(?:e-?\d+)?/g) ?? []).map(Number);
|
|
1735
|
+
if (Math.abs(det) < 1e-9 || nums.length < 2) {
|
|
1736
|
+
emit(`<path d="${d}" fill="${hex(C[0])}" fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1737
|
+
return;
|
|
1738
|
+
}
|
|
1739
|
+
let [x0, y0, x1, y1] = [
|
|
1740
|
+
Infinity,
|
|
1741
|
+
Infinity,
|
|
1742
|
+
-Infinity,
|
|
1743
|
+
-Infinity
|
|
1744
|
+
];
|
|
1745
|
+
for (let k = 0; k + 1 < nums.length; k += 2) {
|
|
1746
|
+
x0 = Math.min(x0, nums[k]);
|
|
1747
|
+
x1 = Math.max(x1, nums[k]);
|
|
1748
|
+
y0 = Math.min(y0, nums[k + 1]);
|
|
1749
|
+
y1 = Math.max(y1, nums[k + 1]);
|
|
1750
|
+
}
|
|
1751
|
+
const bw = Math.max(x1 - x0, 1e-6);
|
|
1752
|
+
const bh = Math.max(y1 - y0, 1e-6);
|
|
1753
|
+
const n = 128;
|
|
1754
|
+
const iw = Math.max(1, Math.round(bw >= bh ? n : n * bw / bh));
|
|
1755
|
+
const ih = Math.max(1, Math.round(bh >= bw ? n : n * bh / bw));
|
|
1756
|
+
if (!takePixels(iw * ih, msg("INTERPOLATED INTERIOR の色の変化", "an INTERPOLATED INTERIOR color gradient"))) {
|
|
1757
|
+
emit(`<path d="${d}" fill="${hex(C[0])}" fill-rule="evenodd" stroke="none"${clipAttr()}/>`);
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
const rgba = new Uint8Array(iw * ih * 4);
|
|
1761
|
+
for (let j = 0; j < ih; j++) for (let i = 0; i < iw; i++) {
|
|
1762
|
+
const x = x0 + (i + .5) / iw * bw;
|
|
1763
|
+
const y = y0 + (j + .5) / ih * bh;
|
|
1764
|
+
const l1 = ((x - P[0][0]) * (P[2][1] - P[0][1]) - (P[2][0] - P[0][0]) * (y - P[0][1])) / det;
|
|
1765
|
+
const l2 = ((P[1][0] - P[0][0]) * (y - P[0][1]) - (x - P[0][0]) * (P[1][1] - P[0][1])) / det;
|
|
1766
|
+
const l0 = 1 - l1 - l2;
|
|
1767
|
+
const o = (j * iw + i) * 4;
|
|
1768
|
+
for (let k = 0; k < 3; k++) rgba[o + k] = Math.round(Math.min(1, Math.max(0, l0 * C[0][k] + l1 * C[1][k] + l2 * C[2][k])) * 255);
|
|
1769
|
+
rgba[o + 3] = 255;
|
|
1770
|
+
}
|
|
1771
|
+
const cid = `cgmc${clipSeq++}`;
|
|
1772
|
+
addDef(`<clipPath id="${cid}"><path d="${d}" clip-rule="evenodd"/></clipPath>`);
|
|
1773
|
+
const img = imageEl(rgba, iw, ih, [
|
|
1774
|
+
bw,
|
|
1775
|
+
0,
|
|
1776
|
+
0,
|
|
1777
|
+
bh,
|
|
1778
|
+
x0,
|
|
1779
|
+
y0
|
|
1780
|
+
], false);
|
|
1781
|
+
emit(`<g${clipAttr()}><g clip-path="url(#${cid})">${img}</g></g>`, img.length);
|
|
1782
|
+
};
|
|
1783
|
+
const hatchIds = /* @__PURE__ */ new Map();
|
|
1784
|
+
/**
|
|
1785
|
+
* HATCH STYLE DEFINITION で定めた模様の線 (第 1 部 7.4.18): 向きのベクトルに平行な線を、間隔の並び (和が 1 周期の長さになる比) で引く。
|
|
1786
|
+
* 最初の線の中心は塗りの基準点。線の太さは間隔の 1 単位 (1 周期の長さ ÷ 間隔の和)。色は塗りの色。交差 (crosshatch) は 2 つ目の向きの線も引く
|
|
1787
|
+
*/
|
|
1788
|
+
const hatchPatterns = (idx, c) => {
|
|
1789
|
+
const def = s.hatchDefs.get(idx);
|
|
1790
|
+
if (!def) return [hatchPattern(idx, c)];
|
|
1791
|
+
const sum = def.gaps.reduce((a, v) => a + v, 0);
|
|
1792
|
+
if (!(sum > 0) || !def.gaps.length) return [hatchPattern(1, c)];
|
|
1793
|
+
const dutyPx = def.mode === 0 ? vdcLen(def.duty) : def.mode === 1 ? def.duty * NOMINAL_LINE_WIDTH * 24 : def.mode === 2 ? def.duty * W : def.duty * PX_PER_MM;
|
|
1794
|
+
if (!(dutyPx > 0)) return [hatchPattern(1, c)];
|
|
1795
|
+
const unit = dutyPx / sum;
|
|
1796
|
+
const F = map(fillRefPoint());
|
|
1797
|
+
const out = [];
|
|
1798
|
+
for (const k of def.cross ? [0, 1] : [0]) {
|
|
1799
|
+
const v = def.mode === 0 ? mapVec([def.dirs[2 * k], def.dirs[2 * k + 1]]) : [def.dirs[2 * k], -def.dirs[2 * k + 1]];
|
|
1800
|
+
if (!(Math.hypot(v[0], v[1]) > 0)) continue;
|
|
1801
|
+
const ang = Math.atan2(v[1], v[0]) * 180 / Math.PI;
|
|
1802
|
+
let y = 0;
|
|
1803
|
+
const lines = [];
|
|
1804
|
+
def.gaps.forEach((gap, i) => {
|
|
1805
|
+
const dash = dashOf(def.types[i] ?? 1, unit);
|
|
1806
|
+
for (const yy of [y, y + dutyPx]) lines.push(`<line x1="-1" y1="${num(yy)}" x2="${num(dutyPx + 1)}" y2="${num(yy)}" stroke="${hex(c)}" stroke-width="${num(unit)}"${dash ? ` stroke-dasharray="${dash}"` : ""}/>`);
|
|
1807
|
+
y += gap * unit;
|
|
1808
|
+
});
|
|
1809
|
+
const id = `cgmh${hatchIds.size + out.length}x${patSeq++}`;
|
|
1810
|
+
addDef(`<pattern id="${id}" patternUnits="userSpaceOnUse" x="0" y="0" width="${num(dutyPx)}" height="${num(dutyPx)}" patternTransform="translate(${num(F[0])} ${num(F[1])}) rotate(${num(ang)})">${lines.join("")}</pattern>`);
|
|
1811
|
+
out.push(id);
|
|
1812
|
+
}
|
|
1813
|
+
return out.length ? out : [hatchPattern(1, c)];
|
|
1814
|
+
};
|
|
1815
|
+
/** 模様の線 (HATCH INDEX 1〜6。第 3 部 表 8 の 24 番)。線の間隔と太さは処理系しだい */
|
|
1816
|
+
const hatchPattern = (idx, c) => {
|
|
1817
|
+
const key = `${idx}:${hex(c)}`;
|
|
1818
|
+
let id = hatchIds.get(key);
|
|
1819
|
+
if (id) return id;
|
|
1820
|
+
id = `cgmh${hatchIds.size}`;
|
|
1821
|
+
hatchIds.set(key, id);
|
|
1822
|
+
const g = 6;
|
|
1823
|
+
const line = (x1, y1, x2, y2) => `<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" stroke="${hex(c)}" stroke-width="0.6"/>`;
|
|
1824
|
+
const parts = [];
|
|
1825
|
+
if (idx === 1 || idx === 5) parts.push(line(0, g / 2, g, g / 2));
|
|
1826
|
+
if (idx === 2 || idx === 5) parts.push(line(g / 2, 0, g / 2, g));
|
|
1827
|
+
if (idx === 3 || idx === 6) parts.push(line(0, g, g, 0));
|
|
1828
|
+
if (idx === 4 || idx === 6) parts.push(line(0, 0, g, g));
|
|
1829
|
+
if (!parts.length) parts.push(line(0, g / 2, g, g / 2));
|
|
1830
|
+
addDef(`<pattern id="${id}" patternUnits="userSpaceOnUse" width="${g}" height="${g}">${parts.join("")}</pattern>`);
|
|
1831
|
+
return id;
|
|
1832
|
+
};
|
|
1833
|
+
/** 円・楕円の弧の点列 (VDC)。中心 c、t の範囲 [t0, t1]、点 f(t) */
|
|
1834
|
+
const arcPoints = (f, t0, t1) => {
|
|
1835
|
+
const n = Math.max(8, Math.ceil(Math.abs(t1 - t0) / (2 * Math.PI) * 128));
|
|
1836
|
+
const out = [];
|
|
1837
|
+
for (let k = 0; k <= n; k++) out.push(f(t0 + (t1 - t0) * k / n));
|
|
1838
|
+
return out;
|
|
1839
|
+
};
|
|
1840
|
+
/** 始めの角度から正の向きに終わりの角度まで (同じ向きなら一周。第 1 部 附属書 D の推奨) */
|
|
1841
|
+
const sweep = (a0, a1) => {
|
|
1842
|
+
let d = a1 - a0;
|
|
1843
|
+
while (d <= 1e-12) d += 2 * Math.PI;
|
|
1844
|
+
return a0 + d;
|
|
1845
|
+
};
|
|
1846
|
+
let textPathSeq = 0;
|
|
1847
|
+
/** 文字列を描く (TEXT・RESTRICTED TEXT)。box は RESTRICTED TEXT の幅と高さ (VDC) */
|
|
1848
|
+
const drawText = (pos, text, box) => {
|
|
1849
|
+
let str = decodeText(s, text);
|
|
1850
|
+
if (!str) return;
|
|
1851
|
+
const e = defaultExtent(defaults);
|
|
1852
|
+
const dy = Math.abs(e[1][1] - e[0][1]);
|
|
1853
|
+
let h = s.charHeight ?? longSide() / 100;
|
|
1854
|
+
const rt = box ? s.restrictedType : 1;
|
|
1855
|
+
/** 書体の上端から下端まで (BOTTOMLINE〜TOPLINE) を箱の高さに合わせる種類 */
|
|
1856
|
+
const all = rt === 3 || rt === 5;
|
|
1857
|
+
if (box && box[1] > 0) {
|
|
1858
|
+
if (rt === 2 || rt === 4) h = box[1];
|
|
1859
|
+
else if (all) h = box[1] / 1.5;
|
|
1860
|
+
else if (rt !== 6 && h > box[1]) h = box[1];
|
|
1861
|
+
}
|
|
1862
|
+
const [ux, uy, bx, by] = s.charOrientation ?? [
|
|
1863
|
+
0,
|
|
1864
|
+
dy,
|
|
1865
|
+
dy,
|
|
1866
|
+
0
|
|
1867
|
+
];
|
|
1868
|
+
const ul = Math.hypot(ux, uy) || 1;
|
|
1869
|
+
const path = box && s.textPath >= 2 ? 0 : s.textPath;
|
|
1870
|
+
if (box && s.textPath >= 2) warn(msg("RESTRICTED TEXT の TEXT PATH (up・down) は right で描きます", "RESTRICTED TEXT with TEXT PATH up or down is drawn as right"));
|
|
1871
|
+
const B = mapVec([bx / ul * s.charExpansion, by / ul * s.charExpansion]);
|
|
1872
|
+
const U = mapVec([ux / ul, uy / ul]);
|
|
1873
|
+
const P = map(pos);
|
|
1874
|
+
const fs = h / CAP_HEIGHT_EM;
|
|
1875
|
+
const [ha, va, ch, cv] = s.textAlign;
|
|
1876
|
+
if (s.textPathMode === 1 || s.textPathMode === 2) {
|
|
1877
|
+
drawTextOnPath(str, h, U, ha, !!box);
|
|
1878
|
+
return;
|
|
1879
|
+
}
|
|
1880
|
+
if (path >= 2) {
|
|
1881
|
+
if (s.scores.length) warn(msg("TEXT PATH が up・down の文字の TEXT SCORE TYPE は描きません", "TEXT SCORE TYPE of text with TEXT PATH up or down is not drawn"));
|
|
1882
|
+
drawVerticalText(str, h, path === 3, B, U, map(pos), fs, ha, va, cv);
|
|
1883
|
+
return;
|
|
1884
|
+
}
|
|
1885
|
+
if (path === 1) str = [...str].reverse().join("");
|
|
1886
|
+
const hh = path === 1 && ha === 0 ? 3 : ha;
|
|
1887
|
+
const anchor = hh === 2 ? "middle" : hh === 3 ? "end" : "start";
|
|
1888
|
+
let up = 0;
|
|
1889
|
+
if (va === 1) up = -h * 1.2;
|
|
1890
|
+
else if (va === 2) up = -h;
|
|
1891
|
+
else if (va === 3) up = -h / 2;
|
|
1892
|
+
else if (va === 5) up = h * .3;
|
|
1893
|
+
else if (va === 6) up = h * .3 - h * 1.5 * cv;
|
|
1894
|
+
let along = 0;
|
|
1895
|
+
let fit = "";
|
|
1896
|
+
let anchorAttr = anchor !== "start" ? ` text-anchor="${anchor}"` : "";
|
|
1897
|
+
if (box) {
|
|
1898
|
+
const [bw, bh] = box;
|
|
1899
|
+
up = (va === 1 || va === 2 ? -bh : va === 3 ? -bh / 2 : va === 6 ? -bh * cv : 0) + (all || va === 5 ? h * .3 : 0);
|
|
1900
|
+
const kx = Math.hypot(bx, by) / ul * s.charExpansion || 1;
|
|
1901
|
+
along = (ha === 2 ? -bw / 2 : ha === 3 ? -bw : ha === 4 ? -bw * ch : 0) / kx;
|
|
1902
|
+
anchorAttr = "";
|
|
1903
|
+
const len = num(bw / kx);
|
|
1904
|
+
fit = rt === 2 || rt === 3 ? ` textLength="${len}" lengthAdjust="spacingAndGlyphs"` : rt === 4 || rt === 5 ? ` data-max-length="${len}" data-fit="uniform"` : rt === 6 ? ` textLength="${len}" lengthAdjust="spacing"` : ` data-max-length="${len}"`;
|
|
1905
|
+
}
|
|
1906
|
+
const m = [
|
|
1907
|
+
B[0],
|
|
1908
|
+
B[1],
|
|
1909
|
+
-U[0],
|
|
1910
|
+
-U[1],
|
|
1911
|
+
P[0] + B[0] * along + U[0] * up,
|
|
1912
|
+
P[1] + B[1] * along + U[1] * up
|
|
1913
|
+
];
|
|
1914
|
+
const attrs = textAttrs(fs);
|
|
1915
|
+
const spacing = s.charSpacing ? ` letter-spacing="${num(s.charSpacing * h)}"` : "";
|
|
1916
|
+
const el = `<text transform="matrix(${m.map(num).join(" ")})" x="0" y="0" ${attrs}${anchorAttr}${spacing}${fit}${scoreAttrs(path === 1)} xml:space="preserve">${esc(str)}</text>`;
|
|
1917
|
+
emit(clipId && !replayDepth ? `<g${clipAttr()}>${el}</g>` : el);
|
|
1918
|
+
};
|
|
1919
|
+
/**
|
|
1920
|
+
* TEXT SCORE TYPE (第 1 部 7.7.41) を SVG の text-decoration と data-kendot にする。right score は字の進む向きの右側
|
|
1921
|
+
* (right の path なら字の下 = 下線、left の path なら字の上)、left score はその逆、through score は字の中ほど、kendot は right score の側の点
|
|
1922
|
+
*/
|
|
1923
|
+
const scoreAttrs = (reversed) => {
|
|
1924
|
+
if (!s.scores.length) return "";
|
|
1925
|
+
const on = (t) => s.scores.includes(t);
|
|
1926
|
+
const deco = [
|
|
1927
|
+
on(reversed ? 2 : 1) && "underline",
|
|
1928
|
+
on(reversed ? 1 : 2) && "overline",
|
|
1929
|
+
on(3) && "line-through"
|
|
1930
|
+
].filter(Boolean);
|
|
1931
|
+
return `${deco.length ? ` text-decoration="${deco.join(" ")}"` : ""}${on(4) ? ` data-kendot="${reversed ? "over" : "true"}"` : ""}`;
|
|
1932
|
+
};
|
|
1933
|
+
/**
|
|
1934
|
+
* GENERALIZED TEXT PATH MODE (第 1 部 6.5.5・6.7.8) が non-tangential・axis-tangential の文字列: 最後の複合の文字の経路 (無ければ既定:
|
|
1935
|
+
* 既定の VDC EXTENT の下の辺) に沿って、字の位置を弧の長さで置く (SVG の textPath)。axis-tangential は字を経路の接線の向きに回し、
|
|
1936
|
+
* non-tangential は回さない。経路の終わりを超える字は、終わりの接線の向きの直線の上に置く。横の揃えは経路の始め・真ん中・終わり
|
|
1937
|
+
*/
|
|
1938
|
+
const drawTextOnPath = (str, h, U, ha, boxed) => {
|
|
1939
|
+
if (boxed) warn(msg("GENERALIZED TEXT PATH MODE の RESTRICTED TEXT は、箱に合わせずに経路に沿って描きます", "RESTRICTED TEXT in GENERALIZED TEXT PATH MODE is drawn along the path without fitting it to the box"));
|
|
1940
|
+
const e = defaultExtent(defaults);
|
|
1941
|
+
const d = s.textPathD ?? pathOf([[Math.min(e[0][0], e[1][0]), Math.min(e[0][1], e[1][1])], [Math.max(e[0][0], e[1][0]), Math.min(e[0][1], e[1][1])]], false);
|
|
1942
|
+
const id = `cgmtp${textPathSeq++}`;
|
|
1943
|
+
addDef(`<path id="${id}" d="${d}"/>`);
|
|
1944
|
+
const k = Math.hypot(U[0], U[1]) || 1;
|
|
1945
|
+
const fs = h / CAP_HEIGHT_EM * k;
|
|
1946
|
+
const font = fontFamilyOf(s.fonts[s.textFont - 1] ?? "Helvetica");
|
|
1947
|
+
const anchor = ha === 2 ? " text-anchor=\"middle\"" : ha === 3 ? " text-anchor=\"end\"" : "";
|
|
1948
|
+
const offset = ha === 2 ? "50%" : ha === 3 ? "100%" : "0";
|
|
1949
|
+
const spacing = s.charSpacing ? ` letter-spacing="${num(s.charSpacing * h * k)}"` : "";
|
|
1950
|
+
const el = `<text font-family="${font.family}"${font.bold ? " font-weight=\"bold\"" : ""}${font.italic ? " font-style=\"italic\"" : ""} font-size="${num(fs)}" ${paint("fill", s.textColour)}${anchor}${spacing} xml:space="preserve"><textPath href="#${id}" startOffset="${offset}" data-extend="true"${s.textPathMode === 1 ? " data-rotate=\"none\"" : ""}>${esc(str)}</textPath></text>`;
|
|
1951
|
+
if (s.scores.length) warn(msg("GENERALIZED TEXT PATH MODE の文字の TEXT SCORE TYPE は描きません", "TEXT SCORE TYPE of text in GENERALIZED TEXT PATH MODE is not drawn"));
|
|
1952
|
+
emit(clipId && !replayDepth ? `<g${clipAttr()}>${el}</g>` : el);
|
|
1953
|
+
};
|
|
1954
|
+
/** 文字の属性 (書体・大きさ・色) */
|
|
1955
|
+
const textAttrs = (fs) => {
|
|
1956
|
+
const font = fontFamilyOf(s.fonts[s.textFont - 1] ?? "Helvetica");
|
|
1957
|
+
return `font-family="${font.family}"${font.bold ? " font-weight=\"bold\"" : ""}${font.italic ? " font-style=\"italic\"" : ""} font-size="${num(fs)}" ${paint("fill", s.textColour)}`;
|
|
1958
|
+
};
|
|
1959
|
+
/**
|
|
1960
|
+
* TEXT PATH が up・down の文字列: 字を 1 字ずつ、上の向き (down はその逆) に、字の送り (書体の大きさ + CHARACTER SPACING × 文字の高さ) で積む。
|
|
1961
|
+
* 横の揃えは各字に (normal は centre)、縦の揃えは字の列全体に掛ける (normal は up なら base = 最初の字の基線、down なら top = 最初の字の上端)
|
|
1962
|
+
*/
|
|
1963
|
+
const drawVerticalText = (str, h, down, B, U, P, fs, ha, va, cv) => {
|
|
1964
|
+
const chars = [...str];
|
|
1965
|
+
const n = chars.length;
|
|
1966
|
+
const adv = fs + s.charSpacing * h;
|
|
1967
|
+
const ys = chars.map((_, k) => (down ? -k : k) * adv);
|
|
1968
|
+
const last = ys[n - 1];
|
|
1969
|
+
const top = down ? h * 1.2 : last + h * 1.2;
|
|
1970
|
+
const cap = down ? h : last + h;
|
|
1971
|
+
const base = down ? last : 0;
|
|
1972
|
+
const bottom = (down ? last : 0) - h * .3;
|
|
1973
|
+
const v = va === 0 ? down ? 1 : 4 : va;
|
|
1974
|
+
const ref = v === 1 ? top : v === 2 ? cap : v === 3 ? (base + cap) / 2 : v === 5 ? bottom : v === 6 ? bottom + (top - bottom) * cv : base;
|
|
1975
|
+
const anchor = ha === 1 ? "start" : ha === 3 ? "end" : "middle";
|
|
1976
|
+
const m = [
|
|
1977
|
+
B[0],
|
|
1978
|
+
B[1],
|
|
1979
|
+
-U[0],
|
|
1980
|
+
-U[1],
|
|
1981
|
+
P[0],
|
|
1982
|
+
P[1]
|
|
1983
|
+
];
|
|
1984
|
+
const parts = chars.map((c, k) => `<text transform="matrix(${m.map(num).join(" ")})" x="0" y="${num(-(ys[k] - ref))}" ${textAttrs(fs)} text-anchor="${anchor}" xml:space="preserve">${esc(c)}</text>`);
|
|
1985
|
+
emit(`<g${clipAttr()}>${parts.join("")}</g>`);
|
|
1986
|
+
};
|
|
1987
|
+
const prec = (v, ok, cur, what) => {
|
|
1988
|
+
if (ok.includes(v)) return v;
|
|
1989
|
+
warn(msg(`${what} ${v} は規格に無い値なので、${cur} のまま読みます`, `${what} ${v} is not a value of the standard; ${cur} is kept`));
|
|
1990
|
+
return cur;
|
|
1991
|
+
};
|
|
1992
|
+
const realForm = (float, a, b, cur, what) => {
|
|
1993
|
+
if (float ? a === 9 && b === 23 || a === 12 && b === 52 : a === 16 && b === 16 || a === 32 && b === 32) return {
|
|
1994
|
+
float,
|
|
1995
|
+
a,
|
|
1996
|
+
b
|
|
1997
|
+
};
|
|
1998
|
+
warn(msg(`${what} (${a}, ${b}) は規格に無い値なので、前の値のまま読みます`, `${what} (${a}, ${b}) is not a value of the standard; the previous value is kept`));
|
|
1999
|
+
return cur;
|
|
2000
|
+
};
|
|
2001
|
+
const P8 = [
|
|
2002
|
+
8,
|
|
2003
|
+
16,
|
|
2004
|
+
24,
|
|
2005
|
+
32
|
|
2006
|
+
];
|
|
2007
|
+
const process = (el, st, inMdr) => {
|
|
2008
|
+
const p = new Params(el.data, st);
|
|
2009
|
+
const { cls, id } = el;
|
|
2010
|
+
const remaining = () => p.left > 0;
|
|
2011
|
+
switch (cls) {
|
|
2012
|
+
case 0:
|
|
2013
|
+
if (id === 3 && !inMdr) {
|
|
2014
|
+
if (pictureDone) return "stop";
|
|
2015
|
+
s = cloneState(defaults);
|
|
2016
|
+
inPicture = true;
|
|
2017
|
+
inBody = false;
|
|
2018
|
+
body = [];
|
|
2019
|
+
} else if (id === 4 && !inMdr) {
|
|
2020
|
+
ext = defaultExtent(s);
|
|
2021
|
+
const w = Math.abs(ext[1][0] - ext[0][0]) || 1;
|
|
2022
|
+
const h = Math.abs(ext[1][1] - ext[0][1]) || 1;
|
|
2023
|
+
if (s.scalingMetric && s.scalingMetric > 0) {
|
|
2024
|
+
W = w * s.scalingMetric * PX_PER_MM;
|
|
2025
|
+
H = h * s.scalingMetric * PX_PER_MM;
|
|
2026
|
+
} else {
|
|
2027
|
+
W = ABSTRACT_WIDTH;
|
|
2028
|
+
H = ABSTRACT_WIDTH * h / w;
|
|
2029
|
+
}
|
|
2030
|
+
inBody = true;
|
|
2031
|
+
const bg = s.background;
|
|
2032
|
+
if (bg) body.push(`<rect x="0" y="0" width="${num(W)}" height="${num(H)}" fill="${hex(bg)}"/>`);
|
|
2033
|
+
updateClip();
|
|
2034
|
+
} else if (id === 5 && !inMdr) {
|
|
2035
|
+
pictureDone = true;
|
|
2036
|
+
return "stop";
|
|
2037
|
+
} else if (id === 8 && inPicture) fig = {
|
|
2038
|
+
regions: [],
|
|
2039
|
+
cur: "",
|
|
2040
|
+
edges: []
|
|
2041
|
+
};
|
|
2042
|
+
else if (id === 9 && fig) {
|
|
2043
|
+
connectEdge(figStart);
|
|
2044
|
+
closeRegion();
|
|
2045
|
+
const f = fig;
|
|
2046
|
+
fig = null;
|
|
2047
|
+
if (f.regions.length) withBundles(() => filled(f.regions.join(""), null));
|
|
2048
|
+
for (const e of f.edges) emit(e);
|
|
2049
|
+
} else if (id === 6 && !inMdr && !replayDepth) rec = {
|
|
2050
|
+
id: p.N(),
|
|
2051
|
+
els: [],
|
|
2052
|
+
snap: cloneState(st),
|
|
2053
|
+
bodyStart: body.length,
|
|
2054
|
+
shown: inBody
|
|
2055
|
+
};
|
|
2056
|
+
else if (id === 7 && rec && !replayDepth) {
|
|
2057
|
+
segments.set(rec.id, {
|
|
2058
|
+
els: rec.els,
|
|
2059
|
+
snap: rec.snap,
|
|
2060
|
+
xf: rec.xf
|
|
2061
|
+
});
|
|
2062
|
+
geoFrags.clear();
|
|
2063
|
+
if (rec.shown && rec.xf) {
|
|
2064
|
+
const part = body.splice(rec.bodyStart);
|
|
2065
|
+
body.push(`<g transform="matrix(${fmtM(svgMatrix(xfOf(rec.xf)))})">${part.join("")}</g>`);
|
|
2066
|
+
}
|
|
2067
|
+
rec = void 0;
|
|
2068
|
+
} else if ((id === 15 || id === 17) && inBody) compound = {
|
|
2069
|
+
text: id === 17,
|
|
2070
|
+
d: "",
|
|
2071
|
+
vertices: true
|
|
2072
|
+
};
|
|
2073
|
+
else if ((id === 16 || id === 18) && compound) {
|
|
2074
|
+
const c = compound;
|
|
2075
|
+
compound = null;
|
|
2076
|
+
if (c.text) st.textPathD = c.d || void 0;
|
|
2077
|
+
else if (c.d) withBundles(() => line(c.d, c.vertices));
|
|
2078
|
+
} else if (id === 19 && inBody) beginTileArray(p);
|
|
2079
|
+
else if (id === 20 && tiles) endTileArray();
|
|
2080
|
+
return;
|
|
2081
|
+
case 1:
|
|
2082
|
+
if (id === 3) st.vdcType = p.E() === 1 ? 1 : 0;
|
|
2083
|
+
else if (id === 4) st.intPrec = prec(p.I(), P8, st.intPrec, "INTEGER PRECISION");
|
|
2084
|
+
else if (id === 5) {
|
|
2085
|
+
const f = p.E();
|
|
2086
|
+
const a = p.I();
|
|
2087
|
+
const b = p.I();
|
|
2088
|
+
st.realForm = realForm(f === 0, a, b, st.realForm, "REAL PRECISION");
|
|
2089
|
+
} else if (id === 6) st.indexPrec = prec(p.I(), P8, st.indexPrec, "INDEX PRECISION");
|
|
2090
|
+
else if (id === 7) st.colourPrec = prec(p.I(), P8, st.colourPrec, "COLOUR PRECISION");
|
|
2091
|
+
else if (id === 8) st.colourIndexPrec = prec(p.I(), P8, st.colourIndexPrec, "COLOUR INDEX PRECISION");
|
|
2092
|
+
else if (id === 10) {
|
|
2093
|
+
if (rgbLike(st.colourModel) || st.colourModel === 4) {
|
|
2094
|
+
st.cvMin = p.CD();
|
|
2095
|
+
st.cvMax = p.CD();
|
|
2096
|
+
} else if (st.colourModel === 2 || st.colourModel === 3 || st.colourModel === 5) st.cvScale = [
|
|
2097
|
+
0,
|
|
2098
|
+
1,
|
|
2099
|
+
2,
|
|
2100
|
+
3,
|
|
2101
|
+
4,
|
|
2102
|
+
5
|
|
2103
|
+
].map(() => p.R());
|
|
2104
|
+
} else if (id === 14) {
|
|
2105
|
+
st.charSets = [];
|
|
2106
|
+
while (p.left >= 24) st.charSets.push({
|
|
2107
|
+
type: p.E(),
|
|
2108
|
+
tail: p.S()
|
|
2109
|
+
});
|
|
2110
|
+
} else if (id === 15) st.codingAnnouncer = p.E();
|
|
2111
|
+
else if (id === 20) {
|
|
2112
|
+
const sel = p.IX();
|
|
2113
|
+
const w = [
|
|
2114
|
+
p.R(),
|
|
2115
|
+
p.R(),
|
|
2116
|
+
p.R()
|
|
2117
|
+
];
|
|
2118
|
+
if (sel >= 2 && w[1] > 0) st.white = [
|
|
2119
|
+
w[0] / w[1],
|
|
2120
|
+
1,
|
|
2121
|
+
w[2] / w[1]
|
|
2122
|
+
];
|
|
2123
|
+
if (sel >= 3) warn(msg("COLOUR CALIBRATION の行列と表は読まず、基準の白だけを使います", "the matrices and tables of COLOUR CALIBRATION are not read; only the reference white is used"));
|
|
2124
|
+
} else if (id === 12) {
|
|
2125
|
+
if (inMdr) return;
|
|
2126
|
+
for (const sub of elements(el.data)) process(sub, defaults, true);
|
|
2127
|
+
s = cloneState(defaults);
|
|
2128
|
+
} else if (id === 13) {
|
|
2129
|
+
st.fonts = [];
|
|
2130
|
+
while (remaining()) st.fonts.push(p.S());
|
|
2131
|
+
} else if (id === 16) st.namePrec = prec(p.I(), P8, st.namePrec, "NAME PRECISION");
|
|
2132
|
+
else if (id === 19) st.colourModel = p.IX();
|
|
2133
|
+
return;
|
|
2134
|
+
case 2:
|
|
2135
|
+
if (id === 1) {
|
|
2136
|
+
const m = p.E();
|
|
2137
|
+
const f = p.floatReal();
|
|
2138
|
+
st.scalingMetric = m === 1 ? f : void 0;
|
|
2139
|
+
} else if (id === 2) st.colourSelection = p.E() === 1 ? 1 : 0;
|
|
2140
|
+
else if (id === 3) st.lineWidthMode = p.E();
|
|
2141
|
+
else if (id === 4) st.markerSizeMode = p.E();
|
|
2142
|
+
else if (id === 5) st.edgeWidthMode = p.E();
|
|
2143
|
+
else if (id === 6) st.vdcExtent = [p.P(), p.P()];
|
|
2144
|
+
else if (id === 7) {
|
|
2145
|
+
st.background = toRgb(st, p.CD());
|
|
2146
|
+
if (st.colourSelection === 0) st.colourTable.set(0, st.background);
|
|
2147
|
+
} else if (id === 16) st.interiorMode = p.E();
|
|
2148
|
+
else if (id === 17) {
|
|
2149
|
+
const idx = p.IX();
|
|
2150
|
+
const repeat = p.SS(st.lineWidthMode);
|
|
2151
|
+
const dashes = [];
|
|
2152
|
+
while (p.left >= st.intPrec) dashes.push(Math.max(0, p.I()));
|
|
2153
|
+
if (idx < 0 && dashes.length) st.lineTypeDefs.set(idx, {
|
|
2154
|
+
repeat,
|
|
2155
|
+
mode: st.lineWidthMode,
|
|
2156
|
+
dashes
|
|
2157
|
+
});
|
|
2158
|
+
} else if (id === 18) {
|
|
2159
|
+
const idx = p.IX();
|
|
2160
|
+
const cross = p.E() === 1;
|
|
2161
|
+
const dirs = [
|
|
2162
|
+
0,
|
|
2163
|
+
1,
|
|
2164
|
+
2,
|
|
2165
|
+
3
|
|
2166
|
+
].map(() => p.SS(st.interiorMode));
|
|
2167
|
+
const duty = p.SS(st.interiorMode);
|
|
2168
|
+
const n = Math.max(0, Math.min(p.I(), 1e4));
|
|
2169
|
+
const gaps = [];
|
|
2170
|
+
for (let k = 0; k < n && p.left > 0; k++) gaps.push(Math.max(0, p.I()));
|
|
2171
|
+
const types = [];
|
|
2172
|
+
for (let k = 0; k < n && p.left > 0; k++) types.push(p.IX());
|
|
2173
|
+
if (idx < 0) st.hatchDefs.set(idx, {
|
|
2174
|
+
cross,
|
|
2175
|
+
dirs,
|
|
2176
|
+
duty,
|
|
2177
|
+
gaps,
|
|
2178
|
+
types,
|
|
2179
|
+
mode: st.interiorMode
|
|
2180
|
+
});
|
|
2181
|
+
} else if (id === 11 || id === 15) {
|
|
2182
|
+
const idx = p.IX();
|
|
2183
|
+
const type = p.IX();
|
|
2184
|
+
const mode = id === 11 ? st.lineWidthMode : st.edgeWidthMode;
|
|
2185
|
+
const width = p.SS(mode);
|
|
2186
|
+
const colour = p.CO();
|
|
2187
|
+
(id === 11 ? st.bundles.line : st.bundles.edge).set(idx, {
|
|
2188
|
+
type,
|
|
2189
|
+
width,
|
|
2190
|
+
mode,
|
|
2191
|
+
colour
|
|
2192
|
+
});
|
|
2193
|
+
} else if (id === 12) {
|
|
2194
|
+
const idx = p.IX();
|
|
2195
|
+
const type = p.IX();
|
|
2196
|
+
const mode = st.markerSizeMode;
|
|
2197
|
+
const size = p.SS(mode);
|
|
2198
|
+
st.bundles.marker.set(idx, {
|
|
2199
|
+
type,
|
|
2200
|
+
size,
|
|
2201
|
+
mode,
|
|
2202
|
+
colour: p.CO()
|
|
2203
|
+
});
|
|
2204
|
+
} else if (id === 13) {
|
|
2205
|
+
const idx = p.IX();
|
|
2206
|
+
const font = p.IX();
|
|
2207
|
+
const precision = p.E();
|
|
2208
|
+
const spacing = p.R();
|
|
2209
|
+
const expansion = p.R();
|
|
2210
|
+
st.bundles.text.set(idx, {
|
|
2211
|
+
font,
|
|
2212
|
+
precision,
|
|
2213
|
+
spacing,
|
|
2214
|
+
expansion,
|
|
2215
|
+
colour: p.CO()
|
|
2216
|
+
});
|
|
2217
|
+
} else if (id === 14) {
|
|
2218
|
+
const idx = p.IX();
|
|
2219
|
+
const style = p.E();
|
|
2220
|
+
const colour = p.CO();
|
|
2221
|
+
const hatch = p.IX();
|
|
2222
|
+
st.bundles.fill.set(idx, {
|
|
2223
|
+
style,
|
|
2224
|
+
colour,
|
|
2225
|
+
hatch,
|
|
2226
|
+
pattern: p.IX()
|
|
2227
|
+
});
|
|
2228
|
+
} else if (id === 19) {
|
|
2229
|
+
const idx = p.IX();
|
|
2230
|
+
const seg = p.N();
|
|
2231
|
+
st.geoPatterns.set(idx, {
|
|
2232
|
+
seg,
|
|
2233
|
+
ext: [p.P(), p.P()]
|
|
2234
|
+
});
|
|
2235
|
+
}
|
|
2236
|
+
return;
|
|
2237
|
+
case 3:
|
|
2238
|
+
if (id === 10) {
|
|
2239
|
+
connectEdge(figStart);
|
|
2240
|
+
closeRegion();
|
|
2241
|
+
return;
|
|
2242
|
+
}
|
|
2243
|
+
if (id === 1) st.vdcIntPrec = prec(p.I(), [
|
|
2244
|
+
16,
|
|
2245
|
+
24,
|
|
2246
|
+
32
|
|
2247
|
+
], st.vdcIntPrec, "VDC INTEGER PRECISION");
|
|
2248
|
+
else if (id === 2) {
|
|
2249
|
+
const f = p.E();
|
|
2250
|
+
const a = p.I();
|
|
2251
|
+
const b = p.I();
|
|
2252
|
+
st.vdcRealForm = realForm(f === 0, a, b, st.vdcRealForm, "VDC REAL PRECISION");
|
|
2253
|
+
} else if (id === 5) {
|
|
2254
|
+
st.clip = [p.P(), p.P()];
|
|
2255
|
+
if (!inMdr) updateClip();
|
|
2256
|
+
} else if (id === 6) {
|
|
2257
|
+
st.clipOn = p.E() === 1;
|
|
2258
|
+
if (!inMdr) updateClip();
|
|
2259
|
+
} else if (id === 18) st.textPathMode = p.E();
|
|
2260
|
+
else if (id === 19) st.mitreLimit = p.R();
|
|
2261
|
+
else if (id === 20) {
|
|
2262
|
+
const on = p.E() === 1;
|
|
2263
|
+
const c = p.CO();
|
|
2264
|
+
st.transparentCell = on ? c : void 0;
|
|
2265
|
+
}
|
|
2266
|
+
return;
|
|
2267
|
+
case 4:
|
|
2268
|
+
if (inMdr || !inPicture || !inBody) return;
|
|
2269
|
+
if (tiles && id !== 28 && id !== 29) return;
|
|
2270
|
+
withBundles(() => drawPrimitive(id, p));
|
|
2271
|
+
return;
|
|
2272
|
+
case 5:
|
|
2273
|
+
attribute(id, p, st);
|
|
2274
|
+
return;
|
|
2275
|
+
case 6: {
|
|
2276
|
+
if (id !== 1) return;
|
|
2277
|
+
const fid = p.I();
|
|
2278
|
+
const rec = sdrMembers(new Params(p.SDR(), st), st);
|
|
2279
|
+
const first = (types) => rec.find((m) => types.includes(m.type) && m.values.length);
|
|
2280
|
+
if (fid === 22) {
|
|
2281
|
+
const flag = first([
|
|
2282
|
+
5,
|
|
2283
|
+
6,
|
|
2284
|
+
11
|
|
2285
|
+
]);
|
|
2286
|
+
const on = flag ? flag.values[0] !== 0 : true;
|
|
2287
|
+
const ci = first([2]);
|
|
2288
|
+
const cd = first([3]);
|
|
2289
|
+
const cco = rec.find((m) => m.type === 17 && m.values.length >= 3);
|
|
2290
|
+
const c = ci ? { index: ci.values[0] } : cd ? directColour(st, cd.values[0]) : cco ? directColour(st, cco.values) : void 0;
|
|
2291
|
+
if (!on) st.transparentCell = void 0;
|
|
2292
|
+
else if (c) st.transparentCell = c;
|
|
2293
|
+
else warn(msg("ESCAPE 22 (Transparent Cell Colour) の色が読めません", "cannot read the color of ESCAPE 22 (Transparent Cell Colour)"));
|
|
2294
|
+
} else if (fid === 45) {
|
|
2295
|
+
const r = first([12]);
|
|
2296
|
+
if (r) st.alpha = Math.min(1, Math.max(0, r.values[0]));
|
|
2297
|
+
else warn(msg("ESCAPE 45 (Alpha Transparency) の値が読めません", "cannot read the value of ESCAPE 45 (Alpha Transparency)"));
|
|
2298
|
+
} else warn(msg(`ESCAPE ${fid} は、登録簿に無いか Kumiori の知らない機能なので読み飛ばします`, `ESCAPE ${fid} is skipped (not in the register or not known to Kumiori)`));
|
|
2299
|
+
return;
|
|
2300
|
+
}
|
|
2301
|
+
case 8:
|
|
2302
|
+
if (id === 1) {
|
|
2303
|
+
if (inMdr || !inBody) return;
|
|
2304
|
+
const sid = p.N();
|
|
2305
|
+
const m = [
|
|
2306
|
+
p.R(),
|
|
2307
|
+
p.R(),
|
|
2308
|
+
p.R(),
|
|
2309
|
+
p.R(),
|
|
2310
|
+
p.VDC(),
|
|
2311
|
+
p.VDC()
|
|
2312
|
+
];
|
|
2313
|
+
const applySeg = p.left >= 16 && p.E() === 1;
|
|
2314
|
+
const frag = replaySegment(sid, st);
|
|
2315
|
+
if (frag === void 0) return;
|
|
2316
|
+
const segXf = segments.get(sid)?.xf;
|
|
2317
|
+
const f = applySeg && segXf ? (q) => xfOf(m)(xfOf(segXf)(q)) : xfOf(m);
|
|
2318
|
+
emit(`<g${clipAttr()}><g transform="matrix(${fmtM(svgMatrix(f))})">${frag}</g></g>`, frag.length);
|
|
2319
|
+
} else if (id === 2) {
|
|
2320
|
+
const vals = [];
|
|
2321
|
+
while (p.left >= 16) vals.push(p.E());
|
|
2322
|
+
const setting = vals.pop();
|
|
2323
|
+
const keys = new Set(vals.flatMap((v) => INHERIT[v] ?? []));
|
|
2324
|
+
const rest = st.inherit.filter((k) => !keys.has(k));
|
|
2325
|
+
st.inherit = setting === 0 ? [...rest, ...keys] : rest;
|
|
2326
|
+
} else if (id === 4 && rec && !replayDepth) {
|
|
2327
|
+
p.N();
|
|
2328
|
+
rec.xf = [
|
|
2329
|
+
p.R(),
|
|
2330
|
+
p.R(),
|
|
2331
|
+
p.R(),
|
|
2332
|
+
p.R(),
|
|
2333
|
+
p.VDC(),
|
|
2334
|
+
p.VDC()
|
|
2335
|
+
];
|
|
2336
|
+
}
|
|
2337
|
+
return;
|
|
2338
|
+
default: return;
|
|
2339
|
+
}
|
|
2340
|
+
};
|
|
2341
|
+
const updateClip = () => {
|
|
2342
|
+
if (!s.clipOn) {
|
|
2343
|
+
clipId = void 0;
|
|
2344
|
+
return;
|
|
2345
|
+
}
|
|
2346
|
+
const r = s.clip ?? defaultExtent(defaults);
|
|
2347
|
+
const a = map(r[0]);
|
|
2348
|
+
const b = map(r[1]);
|
|
2349
|
+
const x = Math.min(a[0], b[0]);
|
|
2350
|
+
const y = Math.min(a[1], b[1]);
|
|
2351
|
+
clipId = `cgmc${clipSeq++}`;
|
|
2352
|
+
addDef(`<clipPath id="${clipId}"><rect x="${num(x)}" y="${num(y)}" width="${num(Math.abs(b[0] - a[0]))}" height="${num(Math.abs(b[1] - a[1]))}"/></clipPath>`);
|
|
2353
|
+
};
|
|
2354
|
+
const attribute = (id, p, st) => {
|
|
2355
|
+
switch (id) {
|
|
2356
|
+
case 2:
|
|
2357
|
+
st.lineType = p.IX();
|
|
2358
|
+
break;
|
|
2359
|
+
case 3:
|
|
2360
|
+
st.lineWidth[st.lineWidthMode] = p.SS(st.lineWidthMode);
|
|
2361
|
+
break;
|
|
2362
|
+
case 4:
|
|
2363
|
+
st.lineColour = p.CO();
|
|
2364
|
+
break;
|
|
2365
|
+
case 6:
|
|
2366
|
+
st.markerType = p.IX();
|
|
2367
|
+
break;
|
|
2368
|
+
case 7:
|
|
2369
|
+
st.markerSize[st.markerSizeMode] = p.SS(st.markerSizeMode);
|
|
2370
|
+
break;
|
|
2371
|
+
case 8:
|
|
2372
|
+
st.markerColour = p.CO();
|
|
2373
|
+
break;
|
|
2374
|
+
case 10:
|
|
2375
|
+
st.textFont = p.IX();
|
|
2376
|
+
break;
|
|
2377
|
+
case 12:
|
|
2378
|
+
st.charExpansion = p.R();
|
|
2379
|
+
break;
|
|
2380
|
+
case 13:
|
|
2381
|
+
st.charSpacing = p.R();
|
|
2382
|
+
break;
|
|
2383
|
+
case 14:
|
|
2384
|
+
st.textColour = p.CO();
|
|
2385
|
+
break;
|
|
2386
|
+
case 15:
|
|
2387
|
+
st.charHeight = p.VDC();
|
|
2388
|
+
break;
|
|
2389
|
+
case 16:
|
|
2390
|
+
st.charOrientation = [
|
|
2391
|
+
p.VDC(),
|
|
2392
|
+
p.VDC(),
|
|
2393
|
+
p.VDC(),
|
|
2394
|
+
p.VDC()
|
|
2395
|
+
];
|
|
2396
|
+
break;
|
|
2397
|
+
case 17:
|
|
2398
|
+
st.textPath = p.E();
|
|
2399
|
+
break;
|
|
2400
|
+
case 18:
|
|
2401
|
+
st.textAlign = [
|
|
2402
|
+
p.E(),
|
|
2403
|
+
p.E(),
|
|
2404
|
+
p.R(),
|
|
2405
|
+
p.R()
|
|
2406
|
+
];
|
|
2407
|
+
break;
|
|
2408
|
+
case 22:
|
|
2409
|
+
st.interiorStyle = p.E();
|
|
2410
|
+
break;
|
|
2411
|
+
case 31:
|
|
2412
|
+
st.fillRef = p.P();
|
|
2413
|
+
break;
|
|
2414
|
+
case 43: {
|
|
2415
|
+
const style = p.IX();
|
|
2416
|
+
const nGeo = style === 1 ? 2 : 4;
|
|
2417
|
+
const geo = [];
|
|
2418
|
+
for (let k = 0; k < nGeo; k++) geo.push(p.SS(st.interiorMode));
|
|
2419
|
+
const m = p.I();
|
|
2420
|
+
const stages = [];
|
|
2421
|
+
for (let k = 0; k < m && p.left > 0; k++) stages.push(p.R());
|
|
2422
|
+
const nc = style === 3 ? 3 : m + 1;
|
|
2423
|
+
const colours = [];
|
|
2424
|
+
for (let k = 0; k < nc && p.left > 0; k++) colours.push(p.CO());
|
|
2425
|
+
st.interp = {
|
|
2426
|
+
style,
|
|
2427
|
+
geo,
|
|
2428
|
+
stages,
|
|
2429
|
+
colours
|
|
2430
|
+
};
|
|
2431
|
+
break;
|
|
2432
|
+
}
|
|
2433
|
+
case 23:
|
|
2434
|
+
st.fillColour = p.CO();
|
|
2435
|
+
break;
|
|
2436
|
+
case 25:
|
|
2437
|
+
st.patternIndex = p.IX();
|
|
2438
|
+
break;
|
|
2439
|
+
case 32: {
|
|
2440
|
+
const idx = p.IX();
|
|
2441
|
+
const nx = p.I();
|
|
2442
|
+
const ny = p.I();
|
|
2443
|
+
const prec = p.I();
|
|
2444
|
+
if (!(nx > 0 && ny > 0) || nx * ny > 1e8 || ![
|
|
2445
|
+
0,
|
|
2446
|
+
1,
|
|
2447
|
+
2,
|
|
2448
|
+
4,
|
|
2449
|
+
8,
|
|
2450
|
+
16,
|
|
2451
|
+
24,
|
|
2452
|
+
32
|
|
2453
|
+
].includes(prec)) break;
|
|
2454
|
+
const indexed = st.colourSelection === 0;
|
|
2455
|
+
const cellBits = indexed ? prec || st.colourIndexPrec : (prec || st.colourPrec) * compsOf(st.colourModel);
|
|
2456
|
+
if (nx * ny > 65536 && !(cellBits > 0 && nx * ny <= Math.floor(Math.max(0, p.left) / cellBits))) {
|
|
2457
|
+
warn(msg("PATTERN TABLE の色の数が、セルの数に足りないので読みません", "PATTERN TABLE has fewer colors than cells and is not read"));
|
|
2458
|
+
break;
|
|
2459
|
+
}
|
|
2460
|
+
const palette = [];
|
|
2461
|
+
const seen = /* @__PURE__ */ new Map();
|
|
2462
|
+
const cells = new Uint32Array(nx * ny);
|
|
2463
|
+
const slot = (key, make) => {
|
|
2464
|
+
let n = seen.get(key);
|
|
2465
|
+
if (n === void 0) {
|
|
2466
|
+
n = palette.length;
|
|
2467
|
+
seen.set(key, n);
|
|
2468
|
+
palette.push(make());
|
|
2469
|
+
}
|
|
2470
|
+
return n;
|
|
2471
|
+
};
|
|
2472
|
+
let k = 0;
|
|
2473
|
+
for (; k < nx * ny && p.left > 0; k++) if (indexed) {
|
|
2474
|
+
const ci = p.CI(prec || st.colourIndexPrec);
|
|
2475
|
+
cells[k] = slot(ci, () => ({ index: ci }));
|
|
2476
|
+
} else {
|
|
2477
|
+
const cd = p.CD(prec || st.colourPrec);
|
|
2478
|
+
cells[k] = slot(cd.join(","), () => directColour(st, cd));
|
|
2479
|
+
}
|
|
2480
|
+
if (k < nx * ny) cells.fill(slot(-1, () => ({ index: 1 })), k);
|
|
2481
|
+
st.patternTable.set(idx, {
|
|
2482
|
+
nx,
|
|
2483
|
+
ny,
|
|
2484
|
+
uid: ++patternTableSeq,
|
|
2485
|
+
palette,
|
|
2486
|
+
cells
|
|
2487
|
+
});
|
|
2488
|
+
break;
|
|
2489
|
+
}
|
|
2490
|
+
case 33:
|
|
2491
|
+
st.patternSize = [
|
|
2492
|
+
0,
|
|
2493
|
+
1,
|
|
2494
|
+
2,
|
|
2495
|
+
3
|
|
2496
|
+
].map(() => p.SS(st.interiorMode));
|
|
2497
|
+
break;
|
|
2498
|
+
case 24:
|
|
2499
|
+
st.hatchIndex = p.IX();
|
|
2500
|
+
break;
|
|
2501
|
+
case 27:
|
|
2502
|
+
st.edgeType = p.IX();
|
|
2503
|
+
break;
|
|
2504
|
+
case 28:
|
|
2505
|
+
st.edgeWidth[st.edgeWidthMode] = p.SS(st.edgeWidthMode);
|
|
2506
|
+
break;
|
|
2507
|
+
case 29:
|
|
2508
|
+
st.edgeColour = p.CO();
|
|
2509
|
+
break;
|
|
2510
|
+
case 30:
|
|
2511
|
+
st.edgeVisible = p.E() === 1;
|
|
2512
|
+
break;
|
|
2513
|
+
case 34: {
|
|
2514
|
+
let i = p.CI();
|
|
2515
|
+
const n = compsOf(st.colourModel);
|
|
2516
|
+
while (p.left >= n * st.colourPrec) {
|
|
2517
|
+
const v = p.CD();
|
|
2518
|
+
const c = toRgb(st, v);
|
|
2519
|
+
if (i === 0 && !inBody) st.background = c;
|
|
2520
|
+
const a = toAlpha(st, v);
|
|
2521
|
+
if (a < 1) st.colourTableAlpha.set(i, a);
|
|
2522
|
+
else st.colourTableAlpha.delete(i);
|
|
2523
|
+
st.colourTable.set(i++, c);
|
|
2524
|
+
}
|
|
2525
|
+
break;
|
|
2526
|
+
}
|
|
2527
|
+
case 37:
|
|
2528
|
+
st.lineCap = p.IX();
|
|
2529
|
+
p.IX();
|
|
2530
|
+
break;
|
|
2531
|
+
case 38:
|
|
2532
|
+
st.lineJoin = p.IX();
|
|
2533
|
+
break;
|
|
2534
|
+
case 42:
|
|
2535
|
+
st.restrictedType = p.IX();
|
|
2536
|
+
break;
|
|
2537
|
+
case 1:
|
|
2538
|
+
st.lineBundle = p.IX();
|
|
2539
|
+
break;
|
|
2540
|
+
case 5:
|
|
2541
|
+
st.markerBundle = p.IX();
|
|
2542
|
+
break;
|
|
2543
|
+
case 9:
|
|
2544
|
+
st.textBundle = p.IX();
|
|
2545
|
+
break;
|
|
2546
|
+
case 11:
|
|
2547
|
+
st.textPrecision = p.E();
|
|
2548
|
+
break;
|
|
2549
|
+
case 19:
|
|
2550
|
+
st.charSetIndex = p.IX();
|
|
2551
|
+
break;
|
|
2552
|
+
case 20:
|
|
2553
|
+
st.altCharSetIndex = p.IX();
|
|
2554
|
+
break;
|
|
2555
|
+
case 21:
|
|
2556
|
+
st.fillBundle = p.IX();
|
|
2557
|
+
break;
|
|
2558
|
+
case 26:
|
|
2559
|
+
st.edgeBundle = p.IX();
|
|
2560
|
+
break;
|
|
2561
|
+
case 35:
|
|
2562
|
+
while (p.left >= 32) {
|
|
2563
|
+
const t = p.E();
|
|
2564
|
+
const v = p.E();
|
|
2565
|
+
if (t >= 0 && t < 18) st.asf[t] = v === 1;
|
|
2566
|
+
}
|
|
2567
|
+
break;
|
|
2568
|
+
case 39:
|
|
2569
|
+
st.lineCont = p.IX();
|
|
2570
|
+
break;
|
|
2571
|
+
case 40:
|
|
2572
|
+
st.lineOffset = Math.min(1, Math.max(0, p.R()));
|
|
2573
|
+
break;
|
|
2574
|
+
case 41:
|
|
2575
|
+
st.scores = [];
|
|
2576
|
+
while (p.left >= st.indexPrec + 16) {
|
|
2577
|
+
const t = p.IX();
|
|
2578
|
+
if (p.E() === 1 && !st.scores.includes(t)) st.scores.push(t);
|
|
2579
|
+
}
|
|
2580
|
+
break;
|
|
2581
|
+
case 46:
|
|
2582
|
+
st.edgeCont = p.IX();
|
|
2583
|
+
break;
|
|
2584
|
+
case 47:
|
|
2585
|
+
st.edgeOffset = Math.min(1, Math.max(0, p.R()));
|
|
2586
|
+
break;
|
|
2587
|
+
case 44:
|
|
2588
|
+
st.edgeCap = p.IX();
|
|
2589
|
+
p.IX();
|
|
2590
|
+
break;
|
|
2591
|
+
case 45:
|
|
2592
|
+
st.edgeJoin = p.IX();
|
|
2593
|
+
break;
|
|
2594
|
+
default: if (id !== 36 && !(id >= 48 && id <= 51)) warn(msg(`属性 ${elementName(5, id)} は読み飛ばします`, `attribute ${elementName(5, id)} is skipped`));
|
|
2595
|
+
}
|
|
2596
|
+
};
|
|
2597
|
+
const drawPrimitive = (id, p) => {
|
|
2598
|
+
const pts = () => {
|
|
2599
|
+
const out = [];
|
|
2600
|
+
const per = 2 * (s.vdcType === 0 ? s.vdcIntPrec : s.vdcRealForm.a + s.vdcRealForm.b);
|
|
2601
|
+
while (p.left >= per) out.push(p.P());
|
|
2602
|
+
return out;
|
|
2603
|
+
};
|
|
2604
|
+
switch (id) {
|
|
2605
|
+
case 1: {
|
|
2606
|
+
const v = pts();
|
|
2607
|
+
if (v.length >= 2) line(pathOf(v, false));
|
|
2608
|
+
break;
|
|
2609
|
+
}
|
|
2610
|
+
case 2: {
|
|
2611
|
+
const v = pts();
|
|
2612
|
+
let d = "";
|
|
2613
|
+
for (let k = 0; k + 1 < v.length; k += 2) d += pathOf([v[k], v[k + 1]], false);
|
|
2614
|
+
if (d) line(d);
|
|
2615
|
+
break;
|
|
2616
|
+
}
|
|
2617
|
+
case 3: {
|
|
2618
|
+
const v = pts();
|
|
2619
|
+
const size = sizePx(s.markerSizeMode, s.markerSize[s.markerSizeMode], NOMINAL_MARKER_SIZE, .01);
|
|
2620
|
+
const r = size / 2;
|
|
2621
|
+
const c = hex(colourOf(s.markerColour));
|
|
2622
|
+
const ma = alphaOf(s.markerColour);
|
|
2623
|
+
const fo = ma < 1 ? ` fill-opacity="${num(ma)}"` : "";
|
|
2624
|
+
const so = ma < 1 ? ` stroke-opacity="${num(ma)}"` : "";
|
|
2625
|
+
const fixed = fixedMode(s.markerSizeMode);
|
|
2626
|
+
const ve = fixed ? " vector-effect=\"non-scaling-size non-scaling-stroke\"" : "";
|
|
2627
|
+
const sw = fixed ? Math.min(size * .075, .5 / .75) : 1;
|
|
2628
|
+
for (const q of v) {
|
|
2629
|
+
const [px, py] = map(q);
|
|
2630
|
+
const [x, y] = [0, 0];
|
|
2631
|
+
const tr = ` transform="translate(${num(px)} ${num(py)})"`;
|
|
2632
|
+
const t = s.markerType;
|
|
2633
|
+
const wrap = (el) => emit(clipId ? `<g${clipAttr()}>${el}</g>` : el);
|
|
2634
|
+
if (t === 1) wrap(`<circle cx="${num(x)}" cy="${num(y)}" r="${num(Math.max(r / 4, fixed ? .25 : .5))}" fill="${c}"${fo}${tr}${ve}/>`);
|
|
2635
|
+
else if (t === 4) wrap(`<circle cx="${num(x)}" cy="${num(y)}" r="${num(r)}" fill="none" stroke="${c}"${so} stroke-width="${num(sw)}"${tr}${ve}/>`);
|
|
2636
|
+
else {
|
|
2637
|
+
let d = "";
|
|
2638
|
+
if (t === 2 || t === 3) d += `M${num(x - r)} ${num(y)}H${num(x + r)}M${num(x)} ${num(y - r)}V${num(y + r)}`;
|
|
2639
|
+
if (t === 3 || t === 5) d += `M${num(x - r)} ${num(y - r)}L${num(x + r)} ${num(y + r)}M${num(x - r)} ${num(y + r)}L${num(x + r)} ${num(y - r)}`;
|
|
2640
|
+
if (d) wrap(`<path d="${d}" fill="none" stroke="${c}"${so} stroke-width="${num(sw)}"${tr}${ve}/>`);
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
break;
|
|
2644
|
+
}
|
|
2645
|
+
case 4:
|
|
2646
|
+
case 5: {
|
|
2647
|
+
const box = id === 5 ? [p.VDC(), p.VDC()] : void 0;
|
|
2648
|
+
const pos = p.P();
|
|
2649
|
+
const final = p.E() === 1;
|
|
2650
|
+
const str = p.S();
|
|
2651
|
+
pendingText = {
|
|
2652
|
+
pos,
|
|
2653
|
+
box,
|
|
2654
|
+
str
|
|
2655
|
+
};
|
|
2656
|
+
if (final) {
|
|
2657
|
+
drawText(pos, str, box);
|
|
2658
|
+
pendingText = void 0;
|
|
2659
|
+
}
|
|
2660
|
+
break;
|
|
2661
|
+
}
|
|
2662
|
+
case 6: {
|
|
2663
|
+
const final = p.E() === 1;
|
|
2664
|
+
const str = p.S();
|
|
2665
|
+
if (pendingText) {
|
|
2666
|
+
pendingText.str += str;
|
|
2667
|
+
if (final) {
|
|
2668
|
+
drawText(pendingText.pos, pendingText.str, pendingText.box);
|
|
2669
|
+
pendingText = void 0;
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
break;
|
|
2673
|
+
}
|
|
2674
|
+
case 7: {
|
|
2675
|
+
const v = pts();
|
|
2676
|
+
if (v.length >= 2) filled(pathOf(v, true));
|
|
2677
|
+
break;
|
|
2678
|
+
}
|
|
2679
|
+
case 8: {
|
|
2680
|
+
const per = 2 * (s.vdcType === 0 ? s.vdcIntPrec : s.vdcRealForm.a + s.vdcRealForm.b) + 16;
|
|
2681
|
+
const v = [];
|
|
2682
|
+
while (p.left >= per) v.push({
|
|
2683
|
+
p: p.P(),
|
|
2684
|
+
f: p.E()
|
|
2685
|
+
});
|
|
2686
|
+
let d = "";
|
|
2687
|
+
let edges = "";
|
|
2688
|
+
let start = 0;
|
|
2689
|
+
for (let k = 0; k < v.length; k++) {
|
|
2690
|
+
const last = k === v.length - 1;
|
|
2691
|
+
let f = v[k].f;
|
|
2692
|
+
if (last && f < 2) f += 2;
|
|
2693
|
+
const a = v[k].p;
|
|
2694
|
+
const close = f >= 2;
|
|
2695
|
+
const b = close ? v[start].p : v[k + 1]?.p;
|
|
2696
|
+
d += (k === start ? `M${num(X(a[0]))} ${num(Y(a[1]))}` : `L${num(X(a[0]))} ${num(Y(a[1]))}`) + (close ? "Z" : "");
|
|
2697
|
+
if (b && (f === 1 || f === 3)) edges += pathOf([a, b], false);
|
|
2698
|
+
if (close) start = k + 1;
|
|
2699
|
+
}
|
|
2700
|
+
if (d) filled(d, edges);
|
|
2701
|
+
break;
|
|
2702
|
+
}
|
|
2703
|
+
case 11: {
|
|
2704
|
+
const [a, b] = [p.P(), p.P()];
|
|
2705
|
+
filled(pathOf([
|
|
2706
|
+
a,
|
|
2707
|
+
[b[0], a[1]],
|
|
2708
|
+
b,
|
|
2709
|
+
[a[0], b[1]]
|
|
2710
|
+
], true));
|
|
2711
|
+
break;
|
|
2712
|
+
}
|
|
2713
|
+
case 12: {
|
|
2714
|
+
const c = p.P();
|
|
2715
|
+
const r = p.VDC();
|
|
2716
|
+
const v = arcPoints((t) => [c[0] + r * Math.cos(t), c[1] + r * Math.sin(t)], 0, 2 * Math.PI);
|
|
2717
|
+
filled(pathOf(v, true), void 0, false);
|
|
2718
|
+
break;
|
|
2719
|
+
}
|
|
2720
|
+
case 13:
|
|
2721
|
+
case 14: {
|
|
2722
|
+
const [a, m, b] = [
|
|
2723
|
+
p.P(),
|
|
2724
|
+
p.P(),
|
|
2725
|
+
p.P()
|
|
2726
|
+
];
|
|
2727
|
+
const closure = id === 14 ? p.E() : -1;
|
|
2728
|
+
const v = arc3(a, m, b);
|
|
2729
|
+
if (id === 13) line(pathOf(v.pts, false), false);
|
|
2730
|
+
else filled(pathOf(closure === 0 && v.centre ? [v.centre, ...v.pts] : v.pts, true), void 0, false);
|
|
2731
|
+
break;
|
|
2732
|
+
}
|
|
2733
|
+
case 15:
|
|
2734
|
+
case 16:
|
|
2735
|
+
case 20: {
|
|
2736
|
+
const c = p.P();
|
|
2737
|
+
const s0 = [p.VDC(), p.VDC()];
|
|
2738
|
+
const s1 = [p.VDC(), p.VDC()];
|
|
2739
|
+
const r = p.VDC();
|
|
2740
|
+
const closure = id === 16 ? p.E() : -1;
|
|
2741
|
+
let a0 = Math.atan2(s0[1], s0[0]);
|
|
2742
|
+
let a1 = Math.atan2(s1[1], s1[0]);
|
|
2743
|
+
if (id === 20) [a0, a1] = [-a0, -a1];
|
|
2744
|
+
const t1 = sweep(a0, a1);
|
|
2745
|
+
const f = (t) => id === 20 ? [c[0] + r * Math.cos(-t), c[1] + r * Math.sin(-t)] : [c[0] + r * Math.cos(t), c[1] + r * Math.sin(t)];
|
|
2746
|
+
const v = arcPoints(f, a0, t1);
|
|
2747
|
+
if (id !== 16) line(pathOf(v, false), false);
|
|
2748
|
+
else filled(pathOf(closure === 0 ? [c, ...v] : v, true), void 0, false);
|
|
2749
|
+
break;
|
|
2750
|
+
}
|
|
2751
|
+
case 17:
|
|
2752
|
+
case 18:
|
|
2753
|
+
case 19: {
|
|
2754
|
+
const c = p.P();
|
|
2755
|
+
const p1 = p.P();
|
|
2756
|
+
const p2 = p.P();
|
|
2757
|
+
const a = [p1[0] - c[0], p1[1] - c[1]];
|
|
2758
|
+
const b = [p2[0] - c[0], p2[1] - c[1]];
|
|
2759
|
+
const f = (t) => [c[0] + a[0] * Math.cos(t) + b[0] * Math.sin(t), c[1] + a[1] * Math.cos(t) + b[1] * Math.sin(t)];
|
|
2760
|
+
if (id === 17) {
|
|
2761
|
+
filled(pathOf(arcPoints(f, 0, 2 * Math.PI), true), void 0, false);
|
|
2762
|
+
break;
|
|
2763
|
+
}
|
|
2764
|
+
const s0 = [p.VDC(), p.VDC()];
|
|
2765
|
+
const s1 = [p.VDC(), p.VDC()];
|
|
2766
|
+
const closure = id === 19 ? p.E() : -1;
|
|
2767
|
+
const det = a[0] * b[1] - a[1] * b[0];
|
|
2768
|
+
if (Math.abs(det) < 1e-12) break;
|
|
2769
|
+
const tOf = (v) => Math.atan2((-a[1] * v[0] + a[0] * v[1]) / det, (b[1] * v[0] - b[0] * v[1]) / det);
|
|
2770
|
+
const t0 = tOf(s0);
|
|
2771
|
+
const t1 = sweep(t0, tOf(s1));
|
|
2772
|
+
const v = arcPoints(f, t0, t1);
|
|
2773
|
+
if (id === 18) line(pathOf(v, false), false);
|
|
2774
|
+
else filled(pathOf(closure === 0 ? [c, ...v] : v, true), void 0, false);
|
|
2775
|
+
break;
|
|
2776
|
+
}
|
|
2777
|
+
case 26: {
|
|
2778
|
+
const ci = p.IX();
|
|
2779
|
+
const v = pts();
|
|
2780
|
+
let d = "";
|
|
2781
|
+
const P = (q) => `${num(X(q[0]))} ${num(Y(q[1]))}`;
|
|
2782
|
+
if (ci === 1) for (let k = 0; k + 3 < v.length; k += 4) d += `M${P(v[k])}C${P(v[k + 1])} ${P(v[k + 2])} ${P(v[k + 3])}`;
|
|
2783
|
+
else if (v.length >= 4) {
|
|
2784
|
+
d = `M${P(v[0])}`;
|
|
2785
|
+
for (let k = 1; k + 2 < v.length; k += 3) d += `C${P(v[k])} ${P(v[k + 1])} ${P(v[k + 2])}`;
|
|
2786
|
+
}
|
|
2787
|
+
if (d) line(d);
|
|
2788
|
+
break;
|
|
2789
|
+
}
|
|
2790
|
+
case 9:
|
|
2791
|
+
cellArray(p);
|
|
2792
|
+
break;
|
|
2793
|
+
case 10:
|
|
2794
|
+
warn(msg(`GENERALIZED DRAWING PRIMITIVE (GDP ${p.I()}) は描きません (登録された GDP がありません)`, `GENERALIZED DRAWING PRIMITIVE (GDP ${p.I()}) is not drawn (no registered GDP)`));
|
|
2795
|
+
break;
|
|
2796
|
+
case 21:
|
|
2797
|
+
if (fig) connectFrom = figLast;
|
|
2798
|
+
break;
|
|
2799
|
+
case 22: {
|
|
2800
|
+
const c = p.P();
|
|
2801
|
+
const a = sub(p.P(), c);
|
|
2802
|
+
const b = sub(p.P(), c);
|
|
2803
|
+
const s0 = [p.VDC(), p.VDC()];
|
|
2804
|
+
const s1 = [p.VDC(), p.VDC()];
|
|
2805
|
+
const det = a[0] * b[1] - a[1] * b[0];
|
|
2806
|
+
if (Math.abs(det) < 1e-12) break;
|
|
2807
|
+
const coef = (v) => [(v[0] * b[1] - v[1] * b[0]) / det, (a[0] * v[1] - a[1] * v[0]) / det];
|
|
2808
|
+
const [al0, be0] = coef(s0);
|
|
2809
|
+
const [al1, be1] = coef(s1);
|
|
2810
|
+
const branch = Math.sign(al0);
|
|
2811
|
+
if (!(Math.abs(al0) > Math.abs(be0)) || !(Math.abs(al1) > Math.abs(be1)) || Math.sign(al1) !== branch) {
|
|
2812
|
+
warn(msg("HYPERBOLIC ARC の向きが双曲線と交わらない (または両方の枝にまたがる) ので描きません", "HYPERBOLIC ARC is not drawn because its directions do not meet the hyperbola (or span both branches)"));
|
|
2813
|
+
break;
|
|
2814
|
+
}
|
|
2815
|
+
const t0 = Math.atanh(be0 / Math.abs(al0));
|
|
2816
|
+
const t1 = Math.atanh(be1 / Math.abs(al1));
|
|
2817
|
+
const n = 64;
|
|
2818
|
+
const v = [];
|
|
2819
|
+
for (let k = 0; k <= n; k++) {
|
|
2820
|
+
const t = t0 + (t1 - t0) * k / n;
|
|
2821
|
+
v.push([c[0] + branch * a[0] * Math.cosh(t) + b[0] * Math.sinh(t), c[1] + branch * a[1] * Math.cosh(t) + b[1] * Math.sinh(t)]);
|
|
2822
|
+
}
|
|
2823
|
+
line(pathOf(v, false), false);
|
|
2824
|
+
break;
|
|
2825
|
+
}
|
|
2826
|
+
case 23: {
|
|
2827
|
+
const c = p.P();
|
|
2828
|
+
const a = p.P();
|
|
2829
|
+
const b = p.P();
|
|
2830
|
+
line(`M${num(X(a[0]))} ${num(Y(a[1]))}Q${num(X(c[0]))} ${num(Y(c[1]))} ${num(X(b[0]))} ${num(Y(b[1]))}`);
|
|
2831
|
+
break;
|
|
2832
|
+
}
|
|
2833
|
+
case 24:
|
|
2834
|
+
case 25: {
|
|
2835
|
+
const k = p.I();
|
|
2836
|
+
const n = p.I();
|
|
2837
|
+
if (!(k >= 1 && k <= 64 && n >= k && n <= 1e5)) {
|
|
2838
|
+
warn(msg(`${id === 25 ? "NON-UNIFORM RATIONAL B-SPLINE" : "NON-UNIFORM B-SPLINE"} の次数か制御点の数が読めないので描きません`, `${id === 25 ? "NON-UNIFORM RATIONAL B-SPLINE" : "NON-UNIFORM B-SPLINE"} is not drawn because its order or number of control points cannot be read`));
|
|
2839
|
+
break;
|
|
2840
|
+
}
|
|
2841
|
+
const ctrl = [];
|
|
2842
|
+
for (let i = 0; i < n && p.left > 0; i++) ctrl.push(p.P());
|
|
2843
|
+
const knots = [];
|
|
2844
|
+
for (let i = 0; i < n + k && p.left > 0; i++) knots.push(p.R());
|
|
2845
|
+
const tmin = p.R();
|
|
2846
|
+
const tmax = p.R();
|
|
2847
|
+
const wts = [];
|
|
2848
|
+
if (id === 25) for (let i = 0; i < n && p.left > 0; i++) wts.push(p.R());
|
|
2849
|
+
if (ctrl.length !== n || knots.length !== n + k || id === 25 && wts.length !== n) {
|
|
2850
|
+
warn(msg(`${id === 25 ? "NON-UNIFORM RATIONAL B-SPLINE" : "NON-UNIFORM B-SPLINE"} の制御点・節点が足りないので描きません`, `${id === 25 ? "NON-UNIFORM RATIONAL B-SPLINE" : "NON-UNIFORM B-SPLINE"} is not drawn because it lacks control points or knots`));
|
|
2851
|
+
break;
|
|
2852
|
+
}
|
|
2853
|
+
const v = bspline(k, ctrl, knots, Math.max(tmin, knots[k - 1]), Math.min(tmax, knots[n]), id === 25 ? wts : void 0);
|
|
2854
|
+
if (v.length >= 2) line(pathOf(v, false), false);
|
|
2855
|
+
break;
|
|
2856
|
+
}
|
|
2857
|
+
case 27:
|
|
2858
|
+
warn(msg("POLYSYMBOL は描きません (記号の表はメタファイルの外のもので、登録された表もありません)", "POLYSYMBOL is not drawn (the symbol tables are outside the metafile and none is registered)"));
|
|
2859
|
+
break;
|
|
2860
|
+
case 28:
|
|
2861
|
+
case 29:
|
|
2862
|
+
if (tiles) tile(id === 28, p);
|
|
2863
|
+
break;
|
|
2864
|
+
default: warn(msg(`図形 ${elementName(4, id)} は描きません`, `graphical primitive ${elementName(4, id)} is not drawn`));
|
|
2865
|
+
}
|
|
2866
|
+
};
|
|
2867
|
+
const sub = (a, b) => [a[0] - b[0], a[1] - b[1]];
|
|
2868
|
+
/** B スプラインの点列 (de Boor の算法。重みがあれば有理: 同次座標で計算する) */
|
|
2869
|
+
const bspline = (k, P, T, t0, t1, w) => {
|
|
2870
|
+
const n = P.length;
|
|
2871
|
+
if (!(t1 >= t0)) return [];
|
|
2872
|
+
const at = (t) => {
|
|
2873
|
+
let lo = k - 1;
|
|
2874
|
+
let hi = n - 1;
|
|
2875
|
+
while (lo < hi) {
|
|
2876
|
+
const mid = lo + hi + 1 >> 1;
|
|
2877
|
+
if (T[mid] <= t) lo = mid;
|
|
2878
|
+
else hi = mid - 1;
|
|
2879
|
+
}
|
|
2880
|
+
let i = lo;
|
|
2881
|
+
while (i > k - 1 && T[i] === T[i + 1]) i--;
|
|
2882
|
+
const d = [];
|
|
2883
|
+
for (let j = 0; j < k; j++) {
|
|
2884
|
+
const q = P[i - k + 1 + j];
|
|
2885
|
+
const wt = w ? w[i - k + 1 + j] : 1;
|
|
2886
|
+
if (!q) return void 0;
|
|
2887
|
+
d.push([
|
|
2888
|
+
q[0] * wt,
|
|
2889
|
+
q[1] * wt,
|
|
2890
|
+
wt
|
|
2891
|
+
]);
|
|
2892
|
+
}
|
|
2893
|
+
for (let r = 1; r < k; r++) for (let j = k - 1; j >= r; j--) {
|
|
2894
|
+
const ii = i - k + 1 + j;
|
|
2895
|
+
const den = T[ii + k - r] - T[ii];
|
|
2896
|
+
const a = den ? (t - T[ii]) / den : 0;
|
|
2897
|
+
d[j] = [
|
|
2898
|
+
(1 - a) * d[j - 1][0] + a * d[j][0],
|
|
2899
|
+
(1 - a) * d[j - 1][1] + a * d[j][1],
|
|
2900
|
+
(1 - a) * d[j - 1][2] + a * d[j][2]
|
|
2901
|
+
];
|
|
2902
|
+
}
|
|
2903
|
+
const [x, y, h] = d[k - 1];
|
|
2904
|
+
return h ? [x / h, y / h] : void 0;
|
|
2905
|
+
};
|
|
2906
|
+
const spans = new Set(T.filter((v) => v >= t0 && v <= t1)).size + 1;
|
|
2907
|
+
const m = Math.min(4e3, Math.max(16, spans * 16));
|
|
2908
|
+
const out = [];
|
|
2909
|
+
for (let j = 0; j <= m; j++) {
|
|
2910
|
+
const q = at(t0 + (t1 - t0) * j / m);
|
|
2911
|
+
if (q) out.push(q);
|
|
2912
|
+
}
|
|
2913
|
+
return out;
|
|
2914
|
+
};
|
|
2915
|
+
/** セルの色の並びを読む (CELL ARRAY・PATTERN TABLE): 精度 0 は COLOUR (INDEX) PRECISION (第 3 部 表 7 の 9 番) */
|
|
2916
|
+
const readCell = (p, prec) => s.colourSelection === 0 ? { index: p.CI(prec || s.colourIndexPrec) } : directColour(s, p.CD(prec || s.colourPrec));
|
|
2917
|
+
const sameColour = (a, b) => !!b && ("index" in a ? "index" in b && a.index === b.index : "rgb" in b && a.rgb.every((v, k) => Math.abs(v - b.rgb[k]) < 1e-6));
|
|
2918
|
+
/**
|
|
2919
|
+
* セルの色を、画素の 4 バイト (R・G・B・不透明度) を 1 つの数に詰めた値にする (前の putCell が書いた値と同じ)。透明のセルの色なら -1。
|
|
2920
|
+
* セルごとに色のオブジェクトを作らずに書くために使う (CELL ARRAY・タイルは、セルの数だけ色を読む)
|
|
2921
|
+
*/
|
|
2922
|
+
const packCell = (c) => {
|
|
2923
|
+
if (sameColour(c, s.transparentCell)) return -1;
|
|
2924
|
+
const v = colourOf(c);
|
|
2925
|
+
const b = (x) => Math.round(x * 255) & 255;
|
|
2926
|
+
return b(v[0]) + b(v[1]) * 256 + b(v[2]) * 65536 + b(alphaOf(c)) * 16777216;
|
|
2927
|
+
};
|
|
2928
|
+
const putPacked = (rgba, k, v) => {
|
|
2929
|
+
if (v < 0) return;
|
|
2930
|
+
rgba[k * 4] = v & 255;
|
|
2931
|
+
rgba[k * 4 + 1] = v >>> 8 & 255;
|
|
2932
|
+
rgba[k * 4 + 2] = v >>> 16 & 255;
|
|
2933
|
+
rgba[k * 4 + 3] = Math.floor(v / 16777216);
|
|
2934
|
+
};
|
|
2935
|
+
/** 色番号 → 詰めた値 (1 つの要素を読む間は色の表が変わらないので、番号ごとに 1 度だけ色にする) を返す関数 */
|
|
2936
|
+
const indexPacker = () => {
|
|
2937
|
+
const memo = /* @__PURE__ */ new Map();
|
|
2938
|
+
return (index) => {
|
|
2939
|
+
let v = memo.get(index);
|
|
2940
|
+
if (v === void 0) memo.set(index, v = packCell({ index }));
|
|
2941
|
+
return v;
|
|
2942
|
+
};
|
|
2943
|
+
};
|
|
2944
|
+
/**
|
|
2945
|
+
* CELL ARRAY (第 1 部 7.6.9、第 3 部 表 7 の 9 番と表 1 の注 11): 角 P・Q・R、nx・ny、局所の色の精度、表し方 (0 連なり・1 詰めた並び)、色の並び。
|
|
2946
|
+
* 行は P から R へ、行の進む向きは R から Q へ。(1, 1) のセルが P の角。各行は語の境目から始まる。連なりは (数 I、色) の並び
|
|
2947
|
+
*/
|
|
2948
|
+
const cellArray = (p) => {
|
|
2949
|
+
const P0 = p.P();
|
|
2950
|
+
const Q0 = p.P();
|
|
2951
|
+
const R0 = p.P();
|
|
2952
|
+
const nx = p.I();
|
|
2953
|
+
const ny = p.I();
|
|
2954
|
+
const prec = p.I();
|
|
2955
|
+
const mode = p.E();
|
|
2956
|
+
if (!(nx > 0 && ny > 0) || nx * ny > 1e8 || ![
|
|
2957
|
+
0,
|
|
2958
|
+
1,
|
|
2959
|
+
2,
|
|
2960
|
+
4,
|
|
2961
|
+
8,
|
|
2962
|
+
16,
|
|
2963
|
+
24,
|
|
2964
|
+
32
|
|
2965
|
+
].includes(prec)) {
|
|
2966
|
+
warn(msg("CELL ARRAY の大きさか精度が読めないので描きません", "CELL ARRAY is not drawn because its size or precision cannot be read"));
|
|
2967
|
+
return;
|
|
2968
|
+
}
|
|
2969
|
+
const cellBits = (prec || (s.colourSelection === 0 ? s.colourIndexPrec : s.colourPrec)) * (s.colourSelection === 0 ? 1 : compsOf(s.colourModel));
|
|
2970
|
+
const rowBits = mode === 1 ? nx * cellBits : s.intPrec + cellBits;
|
|
2971
|
+
const rows = rowBits > 0 ? Math.min(ny, Math.ceil(Math.max(0, p.left) / rowBits)) : ny;
|
|
2972
|
+
if (!rows || !takePixels(nx * rows, "CELL ARRAY")) return;
|
|
2973
|
+
const rgba = new Uint8Array(nx * rows * 4);
|
|
2974
|
+
const indexed = s.colourSelection === 0;
|
|
2975
|
+
const pack = indexPacker();
|
|
2976
|
+
const read = () => indexed ? pack(p.CI(prec || s.colourIndexPrec)) : packCell(readCell(p, prec));
|
|
2977
|
+
for (let j = 0; j < rows && p.left > 0; j++) {
|
|
2978
|
+
p.alignWord();
|
|
2979
|
+
if (mode === 1) for (let i = 0; i < nx && p.left > 0; i++) putPacked(rgba, j * nx + i, read());
|
|
2980
|
+
else {
|
|
2981
|
+
let i = 0;
|
|
2982
|
+
while (i < nx && p.left > 0) {
|
|
2983
|
+
const cnt = p.I();
|
|
2984
|
+
const c = read();
|
|
2985
|
+
for (let r = 0; r < cnt && i < nx; r++, i++) putPacked(rgba, j * nx + i, c);
|
|
2986
|
+
if (cnt <= 0) break;
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2990
|
+
const a = mapVec(sub(R0, P0));
|
|
2991
|
+
const c = mapVec(sub(Q0, R0)).map((v) => v * rows / ny);
|
|
2992
|
+
const o = map(P0);
|
|
2993
|
+
const img = imageEl(rgba, nx, rows, [
|
|
2994
|
+
a[0],
|
|
2995
|
+
a[1],
|
|
2996
|
+
c[0],
|
|
2997
|
+
c[1],
|
|
2998
|
+
o[0],
|
|
2999
|
+
o[1]
|
|
3000
|
+
]);
|
|
3001
|
+
emit(clipId && !replayDepth ? `<g${clipAttr()}>${img}</g>` : img, img.length);
|
|
3002
|
+
};
|
|
3003
|
+
/** BEGIN TILE ARRAY (第 3 部 表 3 の 19 番) */
|
|
3004
|
+
const beginTileArray = (p) => {
|
|
3005
|
+
const pos = p.P();
|
|
3006
|
+
const pathDir = p.E();
|
|
3007
|
+
const lineDir = p.E();
|
|
3008
|
+
const [ntp, ntl, ctp, ctl] = [
|
|
3009
|
+
p.I(),
|
|
3010
|
+
p.I(),
|
|
3011
|
+
p.I(),
|
|
3012
|
+
p.I()
|
|
3013
|
+
];
|
|
3014
|
+
const [csp, csl] = [p.R(), p.R()];
|
|
3015
|
+
const [offp, offl, ncp, ncl] = [
|
|
3016
|
+
p.I(),
|
|
3017
|
+
p.I(),
|
|
3018
|
+
p.I(),
|
|
3019
|
+
p.I()
|
|
3020
|
+
];
|
|
3021
|
+
const w = ntp * ctp;
|
|
3022
|
+
const h = ntl * ctl;
|
|
3023
|
+
if (!(ntp > 0 && ntl > 0 && ctp > 0 && ctl > 0) || w * h > 1e8 || !(csp > 0 && csl > 0)) {
|
|
3024
|
+
warn(msg("BEGIN TILE ARRAY の大きさが読めないので、タイルの並びを描きません", "the size of BEGIN TILE ARRAY cannot be read; the tile array is not drawn"));
|
|
3025
|
+
return;
|
|
3026
|
+
}
|
|
3027
|
+
if (!takePixels(w * h, msg("タイルの並び", "the tile array"))) return;
|
|
3028
|
+
tiles = {
|
|
3029
|
+
pos,
|
|
3030
|
+
pathDir,
|
|
3031
|
+
lineDir,
|
|
3032
|
+
ntp,
|
|
3033
|
+
ntl,
|
|
3034
|
+
ctp,
|
|
3035
|
+
ctl,
|
|
3036
|
+
csp,
|
|
3037
|
+
csl,
|
|
3038
|
+
offp,
|
|
3039
|
+
offl,
|
|
3040
|
+
ncp,
|
|
3041
|
+
ncl,
|
|
3042
|
+
rgba: new Uint8Array(w * h * 4),
|
|
3043
|
+
next: 0
|
|
3044
|
+
};
|
|
3045
|
+
};
|
|
3046
|
+
/**
|
|
3047
|
+
* TILE・BITONAL TILE (第 1 部 7.6.28・7.6.29、第 3 部 表 7 の 28・29 番): 圧縮の形 (0 全部背景・1 全部前景・2 T6・3 T4 1 次元・4 T4 2 次元・
|
|
3048
|
+
* 5 圧縮なし・6 連なり。登録簿の 7 baseline JPEG・8 LZW・9 PNG の圧縮 (deflate))、行の埋め (n > 0 なら各行を n の倍数のセルに埋めてから圧縮)、色 (BITONAL は背景・前景の 2 色)、SDR、BS
|
|
3049
|
+
*/
|
|
3050
|
+
const tile = (bitonal, p) => {
|
|
3051
|
+
const t = tiles;
|
|
3052
|
+
const kind = p.IX();
|
|
3053
|
+
const pad = p.I();
|
|
3054
|
+
let bg;
|
|
3055
|
+
let fg;
|
|
3056
|
+
let prec = 1;
|
|
3057
|
+
if (bitonal) {
|
|
3058
|
+
bg = p.CO();
|
|
3059
|
+
fg = p.CO();
|
|
3060
|
+
} else {
|
|
3061
|
+
prec = p.I();
|
|
3062
|
+
bg = s.colourSelection === 0 ? { index: 0 } : { rgb: s.background ?? DEVICE_BACKGROUND };
|
|
3063
|
+
fg = s.colourSelection === 0 ? { index: 1 } : { rgb: DEVICE_FOREGROUND };
|
|
3064
|
+
}
|
|
3065
|
+
const sdr = new Params(p.SDR(), s);
|
|
3066
|
+
const bs = p.rest();
|
|
3067
|
+
const idx = t.next++;
|
|
3068
|
+
if (idx >= t.ntp * t.ntl) return;
|
|
3069
|
+
const rowLen = pad > 0 ? Math.ceil(t.ctp / pad) * pad : t.ctp;
|
|
3070
|
+
if (!(rowLen * t.ctl <= 1e8) || !takePixels((rowLen - t.ctp) * t.ctl, msg("TILE (行の埋め)", "TILE (row padding)"))) return;
|
|
3071
|
+
const effPrec = bitonal ? 1 : prec || (s.colourSelection === 0 ? s.colourIndexPrec : s.colourPrec);
|
|
3072
|
+
const comps = s.colourSelection === 0 ? 1 : compsOf(s.colourModel);
|
|
3073
|
+
const indexed = s.colourSelection === 0;
|
|
3074
|
+
const bgP = packCell(bg);
|
|
3075
|
+
const fgP = packCell(fg);
|
|
3076
|
+
const pack = indexPacker();
|
|
3077
|
+
/** r から色を 1 つ読み、詰めた値にする */
|
|
3078
|
+
const packedFromBits = (r) => {
|
|
3079
|
+
if (bitonal) return r.uint(1) ? fgP : bgP;
|
|
3080
|
+
return indexed ? pack(r.CI(effPrec)) : packCell(directColour(s, r.CD(effPrec)));
|
|
3081
|
+
};
|
|
3082
|
+
const tx = idx % t.ntp * t.ctp;
|
|
3083
|
+
const ty = Math.floor(idx / t.ntp) * t.ctl;
|
|
3084
|
+
const W0 = t.ntp * t.ctp;
|
|
3085
|
+
/** タイルの中のセル (j 行・i 列) の、並びの画素の番号 */
|
|
3086
|
+
const at = (j, i) => (ty + j) * W0 + tx + i;
|
|
3087
|
+
if (kind === 0 || kind === 1) {
|
|
3088
|
+
const v = kind === 0 ? bgP : fgP;
|
|
3089
|
+
if (v >= 0) for (let j = 0; j < t.ctl; j++) for (let i = 0; i < t.ctp; i++) putPacked(t.rgba, at(j, i), v);
|
|
3090
|
+
} else if (kind === 5 || kind === 6) {
|
|
3091
|
+
const r = new Params(bs, s);
|
|
3092
|
+
if (kind === 5) for (let j = 0; j < t.ctl; j++) for (let i = 0; i < rowLen; i++) {
|
|
3093
|
+
if (r.left < effPrec * comps) break;
|
|
3094
|
+
const c = packedFromBits(r);
|
|
3095
|
+
if (i < t.ctp) putPacked(t.rgba, at(j, i), c);
|
|
3096
|
+
}
|
|
3097
|
+
else {
|
|
3098
|
+
sdr.I();
|
|
3099
|
+
sdr.I();
|
|
3100
|
+
const n = sdr.left > 0 ? sdr.I() : 8;
|
|
3101
|
+
let k = 0;
|
|
3102
|
+
const total = rowLen * t.ctl;
|
|
3103
|
+
while (k < total && r.left >= n + effPrec * comps && n > 0 && n <= 32) {
|
|
3104
|
+
const cnt = r.uint(n);
|
|
3105
|
+
const c = packedFromBits(r);
|
|
3106
|
+
for (let q = 0; q < cnt && k < total; q++, k++) {
|
|
3107
|
+
const i = k % rowLen;
|
|
3108
|
+
if (i < t.ctp) putPacked(t.rgba, at(Math.floor(k / rowLen), i), c);
|
|
3109
|
+
}
|
|
3110
|
+
if (cnt <= 0) break;
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
} else if (kind >= 2 && kind <= 4) {
|
|
3114
|
+
const bits = decodeModifiedHuffman(bs, {
|
|
3115
|
+
width: rowLen,
|
|
3116
|
+
rows: t.ctl,
|
|
3117
|
+
compression: kind === 2 ? 4 : 3,
|
|
3118
|
+
twoDimensional: kind === 4
|
|
3119
|
+
});
|
|
3120
|
+
if (!bits) warn(msg("TILE の T.4 / T.6 の符号が読めません", "cannot read the T.4 / T.6 code of TILE"));
|
|
3121
|
+
else for (let j = 0; j < t.ctl; j++) for (let i = 0; i < t.ctp; i++) putPacked(t.rgba, at(j, i), bits[j * rowLen + i] ? fgP : bgP);
|
|
3122
|
+
} else if (kind === 7 && !bitonal) {
|
|
3123
|
+
const px = decodeJpeg(bs);
|
|
3124
|
+
if (!px) {
|
|
3125
|
+
warn(msg("TILE の JPEG が読めません", "cannot read the JPEG of TILE"));
|
|
3126
|
+
return;
|
|
3127
|
+
}
|
|
3128
|
+
const alpha = Math.round(s.alpha * 255);
|
|
3129
|
+
const tc = s.transparentCell && "rgb" in s.transparentCell ? s.transparentCell.rgb : void 0;
|
|
3130
|
+
const mono = px.components !== 3;
|
|
3131
|
+
for (let j = 0; j < Math.min(t.ctl, px.height); j++) for (let i = 0; i < Math.min(t.ctp, px.width); i++) {
|
|
3132
|
+
const o = (j * px.width + i) * px.components;
|
|
3133
|
+
const v0 = px.data[o];
|
|
3134
|
+
const v1 = mono ? v0 : px.data[o + 1];
|
|
3135
|
+
const v2 = mono ? v0 : px.data[o + 2];
|
|
3136
|
+
if (tc && Math.abs(v0 / 255 - tc[0]) < 1e-6 && Math.abs(v1 / 255 - tc[1]) < 1e-6 && Math.abs(v2 / 255 - tc[2]) < 1e-6) continue;
|
|
3137
|
+
const k = at(j, i) * 4;
|
|
3138
|
+
t.rgba[k] = v0;
|
|
3139
|
+
t.rgba[k + 1] = v1;
|
|
3140
|
+
t.rgba[k + 2] = v2;
|
|
3141
|
+
t.rgba[k + 3] = alpha;
|
|
3142
|
+
}
|
|
3143
|
+
} else if (kind === 8 || kind === 9) {
|
|
3144
|
+
const bitsPerCell = effPrec * comps;
|
|
3145
|
+
const rowBytes = Math.ceil(rowLen * bitsPerCell / 8);
|
|
3146
|
+
const limit = (rowBytes + 1) * t.ctl + 16;
|
|
3147
|
+
let data = kind === 8 ? lzwBytes(bs, limit) : inflateBytes(bs, limit);
|
|
3148
|
+
if (!data.length) {
|
|
3149
|
+
warn(msg(`TILE の ${kind === 8 ? "LZW" : "PNG (deflate)"} の圧縮が読めません`, `cannot read the ${kind === 8 ? "LZW" : "PNG (deflate)"} compression of TILE`));
|
|
3150
|
+
return;
|
|
3151
|
+
}
|
|
3152
|
+
let filtered = kind === 9 && data.length >= (rowBytes + 1) * t.ctl;
|
|
3153
|
+
for (let j = 0; filtered && j < t.ctl; j++) if (data[j * (rowBytes + 1)] > 4) filtered = false;
|
|
3154
|
+
if (filtered) data = unfilterPng(data, rowBytes, Math.max(1, Math.ceil(bitsPerCell / 8)), t.ctl);
|
|
3155
|
+
const r = new Params(data, s);
|
|
3156
|
+
for (let j = 0; j < t.ctl; j++) {
|
|
3157
|
+
if (filtered) r.seekByte(j * rowBytes);
|
|
3158
|
+
for (let i = 0; i < rowLen; i++) {
|
|
3159
|
+
if (r.left < bitsPerCell) break;
|
|
3160
|
+
const c = packedFromBits(r);
|
|
3161
|
+
if (i < t.ctp) putPacked(t.rgba, at(j, i), c);
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
} else {
|
|
3165
|
+
warn(msg(`TILE の圧縮の形 ${kind} は、登録簿に無いか Kumiori の知らない形なので読みません`, `TILE compression type ${kind} is not read (not in the register or not known to Kumiori)`));
|
|
3166
|
+
return;
|
|
3167
|
+
}
|
|
3168
|
+
};
|
|
3169
|
+
/**
|
|
3170
|
+
* END TILE ARRAY: 中身のある範囲 (画像のずれ・画像のセルの数) を描く。位置は中身のある最初のセルの角。セルの進む向きは VDC の x 軸から
|
|
3171
|
+
* 0・90・180・270 度、行の進む向きはそこから 90・270 度。セルの大きさは「VDC の 1 単位あたりのセルの数」(第 1 部 7.2.16) なので、
|
|
3172
|
+
* 1 つのセルの長さは、その逆数 (VDC)
|
|
3173
|
+
*/
|
|
3174
|
+
const endTileArray = () => {
|
|
3175
|
+
const t = tiles;
|
|
3176
|
+
tiles = void 0;
|
|
3177
|
+
const W0 = t.ntp * t.ctp;
|
|
3178
|
+
const H0 = t.ntl * t.ctl;
|
|
3179
|
+
const iw = Math.max(0, Math.min(t.ncp, W0 - t.offp));
|
|
3180
|
+
const ih = Math.max(0, Math.min(t.ncl, H0 - t.offl));
|
|
3181
|
+
if (!iw || !ih || t.offp < 0 || t.offl < 0) return;
|
|
3182
|
+
const rgba = new Uint8Array(iw * ih * 4);
|
|
3183
|
+
for (let j = 0; j < ih; j++) rgba.set(t.rgba.subarray(((t.offl + j) * W0 + t.offp) * 4, ((t.offl + j) * W0 + t.offp + iw) * 4), j * iw * 4);
|
|
3184
|
+
const ang = ([
|
|
3185
|
+
0,
|
|
3186
|
+
90,
|
|
3187
|
+
180,
|
|
3188
|
+
270
|
|
3189
|
+
][t.pathDir] ?? 0) * (Math.PI / 180);
|
|
3190
|
+
const lang = ang + (t.lineDir === 1 ? 270 : 90) * Math.PI / 180;
|
|
3191
|
+
const dp = [Math.cos(ang) * iw / t.csp, Math.sin(ang) * iw / t.csp];
|
|
3192
|
+
const dl = [Math.cos(lang) * ih / t.csl, Math.sin(lang) * ih / t.csl];
|
|
3193
|
+
const a = mapVec(dp);
|
|
3194
|
+
const c = mapVec(dl);
|
|
3195
|
+
const o = map(t.pos);
|
|
3196
|
+
const el = imageEl(rgba, iw, ih, [
|
|
3197
|
+
a[0],
|
|
3198
|
+
a[1],
|
|
3199
|
+
c[0],
|
|
3200
|
+
c[1],
|
|
3201
|
+
o[0],
|
|
3202
|
+
o[1]
|
|
3203
|
+
]);
|
|
3204
|
+
emit(clipId && !replayDepth ? `<g${clipAttr()}>${el}</g>` : el, el.length);
|
|
3205
|
+
};
|
|
3206
|
+
/** 3 点を通る円弧 (第 1 部 7.6.13)。3 点が一直線なら折れ線 (附属書 D.4.5.4) */
|
|
3207
|
+
const arc3 = (a, m, b) => {
|
|
3208
|
+
const d = 2 * (a[0] * (m[1] - b[1]) + m[0] * (b[1] - a[1]) + b[0] * (a[1] - m[1]));
|
|
3209
|
+
if (Math.abs(d) < 1e-12) return { pts: [a, b] };
|
|
3210
|
+
const sq = (q) => q[0] * q[0] + q[1] * q[1];
|
|
3211
|
+
const cx = (sq(a) * (m[1] - b[1]) + sq(m) * (b[1] - a[1]) + sq(b) * (a[1] - m[1])) / d;
|
|
3212
|
+
const cy = (sq(a) * (b[0] - m[0]) + sq(m) * (a[0] - b[0]) + sq(b) * (m[0] - a[0])) / d;
|
|
3213
|
+
const r = Math.hypot(a[0] - cx, a[1] - cy);
|
|
3214
|
+
const ang = (q) => Math.atan2(q[1] - cy, q[0] - cx);
|
|
3215
|
+
const a0 = ang(a);
|
|
3216
|
+
const am = ang(m);
|
|
3217
|
+
const a1 = ang(b);
|
|
3218
|
+
const pos = sweep(a0, a1);
|
|
3219
|
+
const t1 = sweep(a0, am) <= pos ? pos : pos - 2 * Math.PI;
|
|
3220
|
+
return {
|
|
3221
|
+
pts: arcPoints((t) => [cx + r * Math.cos(t), cy + r * Math.sin(t)], a0, t1),
|
|
3222
|
+
centre: [cx, cy]
|
|
3223
|
+
};
|
|
3224
|
+
};
|
|
3225
|
+
for (const el of elements(bytes)) {
|
|
3226
|
+
if (rec && !(el.cls === 0 && el.id === 7) && !(el.cls === 8 && el.id >= 4)) rec.els.push(el);
|
|
3227
|
+
if (process(el, inPicture ? s : defaults, false) === "stop") break;
|
|
3228
|
+
if (!inPicture && el.cls === 1) s = cloneState(defaults);
|
|
3229
|
+
}
|
|
3230
|
+
if (!count && !body.length) return void 0;
|
|
3231
|
+
return {
|
|
3232
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="${num(W)}" height="${num(H)}" viewBox="0 0 ${num(W)} ${num(H)}">${defs.length ? `<defs>${defs.join("")}</defs>` : ""}${body.join("")}</svg>`,
|
|
3233
|
+
width: W,
|
|
3234
|
+
height: H,
|
|
3235
|
+
count,
|
|
3236
|
+
warnings: [...warnings]
|
|
3237
|
+
};
|
|
3238
|
+
}
|
|
3239
|
+
export { cgmPictureSize, cgmToSvg };
|