#!/usr/bin/python
# -*- coding: utf-8 -*-
 
"""
GIMPで矢印を作成するPython-fuスクリプト。
Fillがoffならパスを作成。
・フィルタ -> Misc -> Make Arrow py に登録。
・Python-fuスクリプトなので、
ユーザデータフォルダ/.gimp-2.6/plug-ins/ 以下に入れる。
(~/scripts/ ではない)
"""
 
import math
from gimpfu import *
 
 
def my_get_control_point(list):
    """ 座標値のリストをパス制御点のリストにして返す。
    GIMPのパスは、1ヶ所につき、
    ・制御開始点 x0,y0
    ・アンカー x1,y1
    ・制御終了点 x2,y2
    の3つの座標=6個の値が必要らしいので、
    x,y を x,y,x,y,x,y に増量してリストに登録する。
    今回は直線でパスを作るので、3つの座標は同値。 """
 
    rlist = []
    for pos in list:
        (x, y) = pos
        rlist.extend([
            x,
            y,
            x,
            y,
            x,
            y,
            ])
    return rlist
 
 
def my_get_allow_cplist(
    x,
    y,
    ll,
    lw,
    tl,
    tw,
    tstyle,
    ):
    """ 矢印の座標値リストを作る。"""
 
    l = int(ll) / 2
    w = int(lw) / 2
    u = int(tw) / 2
    x0 = x
    x1 = x + u
    x2 = x + w
    x3 = x - w
    x4 = x - u
    y0 = y - l
    y1 = y0 + int(tl)
    y2 = y + l
 
    # 座標値リストを作る。
    # x,y は、タプル(括弧で挟む)でまとめておく。
 
    list = []
    if tstyle == 0:
 
        # 普通の形をした矢印
 
        list = [
            (x0, y0),
            (x1, y1),
            (x2, y1),
            (x2, y2),
            (x3, y2),
            (x3, y1),
            (x4, y1),
            ]
    elif tstyle == 1:
 
        # 末端が尖ってる形をした矢印
 
        list = [
            (x0, y0),
            (x1, y1),
            (x2, y1),
            (x0, y2),
            (x3, y1),
            (x4, y1),
            ]
    elif tstyle == 2:
 
        # 仮
 
        list = [
            (x0, y0),
            (x1, y1),
            (x2, y1),
            (x0, y2),
            (x3, y1),
            (x4, y1),
            ]
    else:
 
        # 仮
 
        list = [
            (x0, y0),
            (x1, y1),
            (x2, y1),
            (x0, y2),
            (x3, y1),
            (x4, y1),
            ]
 
    # GIMP用のパス制御点リストにして返す
 
    return my_get_control_point(list)
 
 
def python_fu_make_arrow_path(
    image,
    drawable,
    ll,
    lw,
    tl,
    tw,
    angle,
    fill_enable,
    fill_color,
    tstyle,
    ):
    """ 矢印パスを作成する。 """
 
    # 画像の中心座標を取得しておく
 
    imgw = int(image.width)
    imgh = int(image.height)
    cx = imgw / 2
    cy = imgh / 2
 
    image.undo_group_start()  # undoできるようにしておく?
 
    vectors = pdb.gimp_vectors_new(image, 'arrowpath')  # パス新規作成
    pdb.gimp_image_add_vectors(image, vectors, -1)  # パスを画像に追加
 
    # 矢印形状の座標値リストを作成
 
    pnts = my_get_allow_cplist(
        cx,
        cy,
        ll,
        lw,
        tl,
        tw,
        tstyle,
        )
 
    # パスとしてストロークを追加
 
    stroke_id = pdb.gimp_vectors_stroke_new_from_points(vectors, 0,
            len(pnts), pnts, TRUE)
 
    # パスを回転
 
    pdb.gimp_vectors_stroke_rotate(vectors, stroke_id, cx, cy, angle)
 
    # パスを表示
 
    pdb.gimp_vectors_set_visible(vectors, TRUE)
 
    # 塗り潰しするよう指定されてたら、パスから選択範囲を作って塗る。
    # (その後、パスは削除してしまう)
 
    if fill_enable:
 
        # 前景色をバックアップ
 
        fg_color = pdb.gimp_context_get_foreground()
 
        # 前景色変更
 
        pdb.gimp_context_set_foreground(fill_color)
 
        # 選択範囲解除
 
        pdb.gimp_selection_none(image)
 
        # 新規レイヤー作成
 
        layer = gimp.Layer(
            image,
            'arrow',
            imgw,
            imgh,
            RGBA_IMAGE,
            100,
            NORMAL_MODE,
            )
 
        # 新規レイヤーを画像に追加
 
        image.add_layer(layer, -1)
 
        # レイヤーをアクティブに
 
        pdb.gimp_image_set_active_layer(image, layer)
 
        # 念のためレイヤー内容をクリア
 
        pdb.gimp_edit_clear(layer)
 
        # パスから選択範囲作成
 
        pdb.gimp_vectors_to_selection(
            vectors,
            CHANNEL_OP_REPLACE,
            TRUE,
            FALSE,
            0,
            0,
            )
 
        # 前景色で塗り潰し
 
        drawable = pdb.gimp_image_active_drawable(image)
        pdb.gimp_edit_fill(drawable, FOREGROUND_FILL)
 
        # 選択範囲解除
 
        pdb.gimp_selection_none(image)
 
        # パス削除
 
        pdb.gimp_image_remove_vectors(image, vectors)
 
        # 前景色を戻す
 
        pdb.gimp_context_set_foreground(fg_color)
 
    image.undo_group_end()
    return
 
 
# メニューのどこに登録するか指定
 
register(
    'python-fu-make-arrow-path',
    'Make Arrow Path by Python',
    'make Arrow Path by Python',
    'nanashisan (:-<',
    'nanashisan (:-<',
    '2010-06',
    '<Image>/Filters/Misc/Make Arrow py',
    'RGB*',
    [
#         (PF_VALUE, 'll', 'Line length', 256),
#         (PF_VALUE, 'lw', 'Line width', 24),
#         (PF_VALUE, 'tl', 'triangle length', 64),
#         (PF_VALUE, 'tw', 'triangle width', 96),
#         (PF_ADJUSTMENT, 'angle', 'Angle (0-360)', 0, (0, 360, 1)),
#         (PF_TOGGLE, 'fill_enable', 'Fill', False),
#         (PF_COLOR, 'fill_color', 'Fill color', (255, 0, 0)),
#         (PF_ADJUSTMENT, 'tstyle', 'Style (0 or 1)', 0, (0, 3, 1)),
 
        (PF_VALUE, 'll', '矢印線の長さ(px)', 256),
        (PF_VALUE, 'lw', '矢印線の太さ(px)', 24),
        (PF_VALUE, 'tl', '矢先の長さ(px)', 64),
        (PF_VALUE, 'tw', '矢先の幅(px)', 96),
        (PF_ADJUSTMENT, 'angle', '角度 (0-360度、上が0度で時計回り)', 0, (0, 360, 1)),
        (PF_TOGGLE, 'fill_enable', '塗り潰す?', False),
        (PF_COLOR, 'fill_color', '塗り潰し色', (255, 0, 0)),
        (PF_ADJUSTMENT, 'tstyle', '矢印の形 (0 or 1)', 0, (0, 3, 1)),
        ],
    [],
    python_fu_make_arrow_path,
    )
 
main()
 
最終更新:2012年08月04日 07:12