蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

验证 Paint 文本度量、Canvas 图元/弧形几何、Path 贝塞尔曲线与 60 刻度表盘变换栈

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-drawable-tool.py
explore-tutorial-drawable-tool.py173 行
"""Offline verification lab for Android Ch 110: tutorial-drawable-tool."""
import json
import math

VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"


def case_paint_configuration_and_text_metrics():
    paint = {
        "antiAlias": True,
        "style": "FILL",
        "textSize_px": 36.0,
        "strokeWidth_px": 5.0,
        "strokeCap": "ROUND",
        "strokeJoin": "MITER",
    }
    # Simulate breakText with monospace 18px per char against maxWidth=100px
    text = "AndroidCanvas"
    char_w = 18.0
    max_w = 100.0
    fit_count = min(len(text), int(max_w // char_w))
    ascent, descent = -30.5, 8.5
    line_height = round(descent - ascent, 2)
    actual = {
        "paint_style": paint["style"],
        "textSize_px": paint["textSize_px"],
        "breakText_fit_count": fit_count,
        "measured_width_px": fit_count * char_w,
        "font_line_height_px": line_height,
    }
    expected = {
        "paint_style": "FILL",
        "textSize_px": 36.0,
        "breakText_fit_count": 5,
        "measured_width_px": 90.0,
        "font_line_height_px": 39.0,
    }
    return {
        "name": "paint_configuration_and_text_metrics",
        "summary": "Verify Paint configuration properties, baseline ascent/descent height, and breakText fit",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_canvas_primitives_and_arc_geometry():
    # Circle (200,200,r=100), Rect (0,0,200,100), Arc in RectF(0,0,100,100) r=50 sweep=90deg
    r = 50.0
    sector_area = round(0.25 * math.pi * r * r, 3)  # useCenter=True
    chord_segment_area = round(sector_area - 0.5 * r * r, 3)  # useCenter=False
    round_rect_bounds = [10, 10, 210, 110]
    oval_bounds = [0, 0, 200, 300]
    actual = {
        "circle_bbox": [100, 100, 300, 300],
        "rect_area": 200 * 100,
        "arc_useCenter_true_area": sector_area,
        "arc_useCenter_false_area": chord_segment_area,
        "round_rect_wh": [round_rect_bounds[2] - round_rect_bounds[0], round_rect_bounds[3] - round_rect_bounds[1]],
        "oval_wh": [oval_bounds[2] - oval_bounds[0], oval_bounds[3] - oval_bounds[1]],
    }
    expected = {
        "circle_bbox": [100, 100, 300, 300],
        "rect_area": 20000,
        "arc_useCenter_true_area": 1963.495,
        "arc_useCenter_false_area": 713.495,
        "round_rect_wh": [200, 100],
        "oval_wh": [200, 300],
    }
    return {
        "name": "canvas_primitives_and_arc_geometry",
        "summary": "Verify Canvas drawCircle/drawRect/drawRoundRect/drawOval and drawArc useCenter geometry",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_path_polygon_bezier_and_text_on_path():
    poly = [(10, 10), (100, 50), (200, 40), (300, 20), (200, 10), (100, 70), (50, 40)]
    xs = [p[0] for p in poly]
    ys = [p[1] for p in poly]
    # Cubic Bezier from P0=(100,500), P1=(100,500), P2=(300,100), P3=(600,500) at t=0.5
    p0, p1, p2, p3 = (100, 500), (100, 500), (300, 100), (600, 500)
    t = 0.5
    bx = (1 - t) ** 3 * p0[0] + 3 * (1 - t) ** 2 * t * p1[0] + 3 * (1 - t) * t ** 2 * p2[0] + t ** 3 * p3[0]
    by = (1 - t) ** 3 * p0[1] + 3 * (1 - t) ** 2 * t * p1[1] + 3 * (1 - t) * t ** 2 * p2[1] + t ** 3 * p3[1]
    # Text path diagonal (50,50)->(300,300)
    diag_len = round(math.hypot(300 - 50, 300 - 50), 3)
    actual = {
        "polygon_vertices": len(poly),
        "polygon_bbox": [min(xs), min(ys), max(xs), max(ys)],
        "cubic_midpoint": [bx, by],
        "text_path_diag_length": diag_len,
    }
    expected = {
        "polygon_vertices": 7,
        "polygon_bbox": [10, 10, 300, 70],
        "cubic_midpoint": [237.5, 350.0],
        "text_path_diag_length": 353.553,
    }
    return {
        "name": "path_polygon_bezier_and_text_on_path",
        "summary": "Verify Path polygon bounding box, cubicTo Bezier midpoint, and drawTextOnPath length",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_canvas_save_restore_stack_and_dial_ticks():
    stack = [(0.0, 0.0, 0.0)]  # (tx, ty, deg)
    # Translate to center (150, 200)
    stack[-1] = (150.0, 200.0, 0.0)
    # save(), translate(-75, -75), restore()
    stack.append(stack[-1])
    tx, ty, deg = stack[-1]
    stack[-1] = (tx - 75.0, ty - 75.0, deg)
    saved_state = stack[-1]
    stack.pop()
    restored_state = stack[-1]
    # Restore underflow check
    underflow_raises = False
    try:
        stack.pop()
        if not stack:
            raise RuntimeError("Canvas restore underflow")
    except RuntimeError:
        underflow_raises = True

    count = 60
    step_deg = 360 / count
    major_ticks = [i for i in range(count) if i % 5 == 0]
    actual = {
        "saved_inner_origin": list(saved_state[:2]),
        "restored_origin": list(restored_state[:2]),
        "underflow_detected": underflow_raises,
        "dial_tick_count": count,
        "dial_step_degrees": step_deg,
        "major_tick_count": len(major_ticks),
        "major_labels": [i // 5 + 1 for i in major_ticks],
    }
    expected = {
        "saved_inner_origin": [75.0, 125.0],
        "restored_origin": [150.0, 200.0],
        "underflow_detected": True,
        "dial_tick_count": 60,
        "dial_step_degrees": 6.0,
        "major_tick_count": 12,
        "major_labels": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    }
    return {
        "name": "canvas_save_restore_stack_and_dial_ticks",
        "summary": "Verify Canvas save/restore transform stack, underflow guard, and 60-tick dial rotation math",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_paint_configuration_and_text_metrics(),
        case_canvas_primitives_and_arc_geometry(),
        case_path_polygon_bezier_and_text_on_path(),
        case_canvas_save_restore_stack_and_dial_ticks(),
    ]
    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-drawable-tool-lab/explore-tutorial-drawable-tool.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial-drawable-tool.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Android

课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip

下载课程关联工程包:Android

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1871.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习