Prolog "vector"

Admin User, erstellt 16. Apr. 2024
         
/**
* This file provides SVG writing predicates.
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(misc/markup)).
/*******************************************************************/
/* SVG Commands */
/*******************************************************************/
/**
* svg_begin(L):
* svg_begin(S, L):
* The predicate succeeds. As a side effect a new SVG output area with
* options in L. The binary predicate allows specifying a DOM writer.
*/
% svg_begin(+List)
svg_begin(L) :-
current_output(S),
svg_begin(S, L).
% svg_begin(+Stream, +List)
svg_begin(S, L) :-
sys_svg_opts(L, 100-100, W-H),
SW is W*500/100, SH is H*400/100,
DW is SW/12, DH is SH/12,
tag_format(S, '<svg style="width: ~gem; height: ~gem" viewBox="0 0 ~g ~g">', [DW, DH, SW, SH]).
/**
* svg_rect(X, Y, W, H, C):
* svg_rect(S, X, Y, W, H, C):
* The predicate succeeds. As a side effect a rectangle element at (X,Y)
* with dimension (W,H) and style C is added to the SVG output area. The
* septenary predicate allows specifying a DOM writer.
*/
% svg_rect(+Float, +Float, +Float, +Float, +Atom)
svg_rect(X, Y, W, H, C) :-
current_output(S),
svg_rect(S, X, Y, W, H, C).
% svg_rect(+Stream, +Float, +Float, +Float, +Float, +Atom)
svg_rect(S, X, Y, W, H, C) :-
tag_format(S, '<rect x="~g" y="~g" width="~g" height="~g" class="~a"/>', [X, Y, W, H, C]).
/**
* svg_line(X1, Y1, X2, Y2, C):
* svg_line(S, X1, Y1, X2, Y2, C):
* The predicate succeeds. As a side effect a line element from (X1,Y1)
* to (X2,Y2) with style C is added to the SVG output area. The
* septenary predicate allows specifying a DOM writer.
*/
% svg_line(+Float, +Float, +Float, +Float, +Atom)
svg_line(X1, Y1, X2, Y2, C) :-
current_output(S),
svg_line(S, X1, Y1, X2, Y2, C).
% svg_line(+Stream, +Float, +Float, +Float, +Float, +Atom)
svg_line(S, X1, Y1, X2, Y2, C) :-
tag_format(S, '<line x1="~g" y1="~g" x2="~g" y2="~g" class="~a"/>', [X1, Y1, X2, Y2, C]).
/**
* svg_text(X, Y, T, C):
* svg_text(S, X, Y, T, C):
* The predicate succeeds. As a side effect a text element at (X,Y)
* with content T and style C is added to the SVG output area. The
* pentamery predicate allows specifying a DOM writer.
*/
% svg_text(+Float, +Float, +Atom, +Atom)
svg_text(X, Y, T, C) :-
current_output(S),
svg_text(S, X, Y, T, C).
% svg_text(+Stream, +Float, +Float, +Atom, +Atom)
svg_text(S, X, Y, T, C) :-
tag_format(S, '<text x="~g" y="~g" class="~a">', [X, Y, C]),
write(S, T),
tag(S, '</text>').
/**
* svg_circle(X, Y, R, C):
* svg_circle(S, X, Y, R, C):
* The predicate succeeds. As a side effect a circle element at (X,Y)
* with radius R and style C is added to the SVG output area. The
* pentamery predicate allows specifying a DOM writer.
*/
% svg_circle(+Float, +Float, +Float, +Atom)
svg_circle(X, Y, R, C) :-
current_output(S),
svg_circle(S, X, Y, R, C).
% svg_circle(+Stream, +Float, +Float, +Float, +Atom)
svg_circle(S, X, Y, R, C) :-
tag_format(S, '<circle cx="~g" cy="~g" r="~g" class="~a"/>', [X, Y, R, C]).
/**
* svg_path(L, C):
* svg_path(S, L, C):
* The predicate succeeds. As a side effect a path element with shape L
* and style C is added to the SVG output area. The ternary predicate allows
* specifying a DOM writer.
*/
% svg_path(+List, +Atom)
svg_path(L, C) :-
current_output(S),
svg_path(S, L, C).
% svg_path(+Stream, +List, +Atom)
svg_path(S, L, C) :-
sys_shape_list(L, R),
atom_split(D, ' ', R),
tag_format(S, '<path d="~a" class="~a"/>', [D, C]).
% sys_shape_list(+List, +List)
sys_shape_list([X|L], [Y|R]) :-
sys_shape(X, Y),
sys_shape_list(L, R).
sys_shape_list([], []).
% sys_shape(+Term, -Atom)
sys_shape(X, Y) :- number(X), !,
format_atom('~g', [X], Y).
sys_shape(X, Y) :-
format_atom('~w', [X], Y).
/**
* svg_image(X, Y, W, H, U):
* svg_image(S, X, Y, W, H, U):
* The predicate succeeds. As a side effect an image element at (X,Y)
* with width W, height H and image URL U s added to the SVG output area.
* The sixternary predicate allows * specifying a DOM writer.
*/
% svg_image(+Float, +Float, +Float, +Float, +Atom)
svg_image(X, Y, W, H, U) :-
current_output(S),
svg_image(S, X, Y, W, H, U).
% svg_image(+Stream, +Float, +Float, +Float, +Float, +Atom)
svg_image(S, X, Y, W, H, U) :-
tag_format(S, '<image x="~g" y="~g" width="~g" height="~g" href="~a" preserveAspectRatio="none" pointer-events="none"/>', [X, Y, W, H, U]).
/**
* svg_end:
* svg_end(S):
* The predicate succeeds. As a side effect the SVG output
* area is closed. The unary predicate allows specifying
* a DOM writer.
*/
% svg_end
svg_end :-
current_output(S),
svg_end(S).
% svg_end(+Stream)
svg_end(S) :-
tag(S, '</svg>').
/*******************************************************************/
/* SVG Options */
/*******************************************************************/
/**
* sys_svg_opts(L, F, G):
* The predicate succeeds in G with the options L starting with defaults F.
*/
% sys_svg_opts(+List, +Pair, -Pair)
sys_svg_opts(V, _, _) :- var(V),
throw(error(instantiation_error,_)).
sys_svg_opts([X|L], I, O) :- !,
sys_svg_opt(X, I, H),
sys_svg_opts(L, H, O).
sys_svg_opts([], H, H) :- !.
sys_svg_opts(L, _, _) :-
throw(error(type_error(list,L),_)).
% sys_svg_opt(+Option, +Pair, -Pair)
sys_svg_opt(V, _, _) :- var(V),
throw(error(instantiation_error,_)).
sys_svg_opt(width(W), _-H, W-H) :- !.
sys_svg_opt(height(H), W-_, W-H) :- !.
sys_svg_opt(O, _, _) :-
throw(error(type_error(svg_option,O),_)).
/*******************************************************************/
/* Foreign Predicates */
/*******************************************************************/
% svg_view_inverse(E, I):
% defined in foreign(misc/portlib)
% svg_apply_transform(I, CX, CY, SX, SY):
% defined in foreign(misc/portlib)
:- ensure_loaded(foreign(misc/portlib)).