"""Offline Android graphics verifier for Ch 123: Paint Enums, FontMetrics & ShadowLayer."""
from dataclasses import dataclass
import json
import math
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
@dataclass
class ShadowState:
radius: float = 0.0
dx: float = 0.0
dy: float = 0.0
color: int = 0
@property
def active(self) -> bool:
return self.radius > 0.0 and ((self.color >> 24) & 0xFF) > 0
def clear(self) -> None:
self.radius = 0.0
self.dx = 0.0
self.dy = 0.0
self.color = 0
def text_x_span(anchor_x: float, text_width: float, align: str) -> list[float]:
if align == "LEFT":
return [anchor_x, anchor_x + text_width]
if align == "CENTER":
return [anchor_x - text_width / 2.0, anchor_x + text_width / 2.0]
if align == "RIGHT":
return [anchor_x - text_width, anchor_x]
raise ValueError(align)
def cap_extension(stroke_width: float, cap: str) -> float:
if cap == "BUTT":
return 0.0
if cap in ("ROUND", "SQUARE"):
return stroke_width / 2.0
raise ValueError(cap)
def miter_falls_back_to_bevel(angle_deg: float, miter_limit: float) -> bool:
ratio = 1.0 / math.sin(math.radians(angle_deg / 2.0))
return ratio > miter_limit
def main() -> None:
# Case 1: Paint.Style / Cap / Join / Align nativeInt enums matching Android Paint.java
c1_actual = {
"Style": {"FILL": 0, "STROKE": 1, "FILL_AND_STROKE": 2},
"Cap": {"BUTT": 0, "ROUND": 1, "SQUARE": 2},
"Join": {"MITER": 0, "ROUND": 1, "BEVEL": 2},
"Align": {"LEFT": 0, "CENTER": 1, "RIGHT": 2},
}
c1_expected = {
"Style": {"FILL": 0, "STROKE": 1, "FILL_AND_STROKE": 2},
"Cap": {"BUTT": 0, "ROUND": 1, "SQUARE": 2},
"Join": {"MITER": 0, "ROUND": 1, "BEVEL": 2},
"Align": {"LEFT": 0, "CENTER": 1, "RIGHT": 2},
}
# Case 2: Stroke Cap endpoint extension & Join miter limit (default 4.0)
c2_actual = {
"cap_ext_w20": {cap: cap_extension(20.0, cap) for cap in ("BUTT", "ROUND", "SQUARE")},
"miter_fallback_deg20_limit4": miter_falls_back_to_bevel(20.0, 4.0),
"miter_fallback_deg60_limit4": miter_falls_back_to_bevel(60.0, 4.0),
}
c2_expected = {
"cap_ext_w20": {"BUTT": 0.0, "ROUND": 10.0, "SQUARE": 10.0},
"miter_fallback_deg20_limit4": True,
"miter_fallback_deg60_limit4": False,
}
# Case 3: Paint.Align horizontal bounds around x=400 (text_width=120) & FontMetricsInt baseline centering
fm = {"top": -64, "ascent": -56, "descent": 15, "bottom": 17, "leading": 0}
center_y = 200.0
baseline_for_center = center_y - (fm["ascent"] + fm["descent"]) / 2.0
c3_actual = {
"align_spans_x400_w120": {a: text_x_span(400.0, 120.0, a) for a in ("LEFT", "CENTER", "RIGHT")},
"metrics_ordered": fm["top"] <= fm["ascent"] < 0 < fm["descent"] <= fm["bottom"],
"line_height_top_to_bottom": fm["bottom"] - fm["top"],
"vertical_center_baseline_at_y200": baseline_for_center,
}
c3_expected = {
"align_spans_x400_w120": {
"LEFT": [400.0, 520.0],
"CENTER": [340.0, 460.0],
"RIGHT": [280.0, 400.0],
},
"metrics_ordered": True,
"line_height_top_to_bottom": 81,
"vertical_center_baseline_at_y200": 220.5,
}
# Case 4: setShadowLayer(5, 0, 0, Color.BLACK) & clearShadowLayer()
sh = ShadowState(radius=5.0, dx=0.0, dy=0.0, color=0xFF000000)
active_before = sh.active
shadow_offset_box = [400.0 + sh.dx - sh.radius, 400.0 + sh.dy - sh.radius, 400.0 + sh.dx + sh.radius]
sh.clear()
c4_actual = {
"active_after_set": active_before,
"blur_radius": 5.0,
"shadow_halo_sample": shadow_offset_box,
"active_after_clear": sh.active,
}
c4_expected = {
"active_after_set": True,
"blur_radius": 5.0,
"shadow_halo_sample": [395.0, 395.0, 405.0],
"active_after_clear": False,
}
cases = [
{
"name": "paint_enum_native_values",
"summary": "核验 Paint.Style、Paint.Cap、Paint.Join 与 Paint.Align 四组枚举的 nativeInt 映射值",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "stroke_cap_extension_and_miter_limit",
"summary": "核验 BUTT/ROUND/SQUARE 端点延展量及 setStrokeMiter(4.0) 在锐角 20°/60° 下的倒角回退判定",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "text_align_and_fontmetrics_baseline",
"summary": "核验 Paint.Align 相对基准点 x 的真实对齐区间及 FontMetricsInt 五值顺序与垂直居中基线求解",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "shadow_layer_set_and_clear",
"summary": "核验 setShadowLayer(5, 0, 0, Color.BLACK) 的激活条件、模糊半径与 clearShadowLayer() 清除行为",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-shadowlayer-lab/explore-tutorial-shadowlayer.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-shadowlayer.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1944.zip