File size: 960 Bytes
ad88834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import struct
from dataclasses import dataclass

Point = tuple[int, int]


@dataclass(frozen=True)
class Stroke:
    points: tuple[Point, ...]
    width: int = 6

    def __post_init__(self) -> None:
        if not 1 <= self.width <= 48 or not 1 <= len(self.points) <= 48:
            raise ValueError("笔画宽度或坐标数量超出 SDK 限制")
        if any(not 0 <= coordinate <= 411 for point in self.points for coordinate in point):
            raise ValueError("图案坐标超出屏幕范围")


def encode_vector(strokes: tuple[Stroke, ...]) -> str:
    if not 1 <= len(strokes) <= 12 or sum(len(stroke.points) for stroke in strokes) > 192:
        raise ValueError("图案超出 SDK 的笔画或坐标总量限制")
    data = bytearray((1, len(strokes)))
    for stroke in strokes:
        data.extend((stroke.width, len(stroke.points)))
        for x, y in stroke.points:
            data.extend(struct.pack(">HH", x, y))
    return data.hex()