| 1 |
% Copyright (c) 2020 Robert Ryszard Paciorek <[email protected]> |
| 2 |
% |
| 3 |
% MIT License |
| 4 |
% |
| 5 |
% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
% of this software and associated documentation files (the "Software"), to deal |
| 7 |
% in the Software without restriction, including without limitation the rights |
| 8 |
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
% copies of the Software, and to permit persons to whom the Software is |
| 10 |
% furnished to do so, subject to the following conditions: |
| 11 |
% |
| 12 |
% The above copyright notice and this permission notice shall be included in all |
| 13 |
% copies or substantial portions of the Software. |
| 14 |
% |
| 15 |
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
% SOFTWARE. |
| 22 |
|
| 23 |
% Version 1.0 (2020-07-13): first public release |
| 24 |
|
| 25 |
\NeedsTeXFormat{LaTeX2e} |
| 26 |
\ProvidesPackage{tikzPackets} |
| 27 |
|
| 28 |
\RequirePackage{tikz} |
| 29 |
|
| 30 |
\RequirePackage{xparse} |
| 31 |
|
| 32 |
% need by \packetsPrintRangeOnRight: |
| 33 |
\ifdefined\tcbsetmacrotoheightofnode\else\RequirePackage[skins]{tcolorbox}\fi |
| 34 |
\RequirePackage{pbox} |
| 35 |
|
| 36 |
% |
| 37 |
% init counters, etc |
| 38 |
% |
| 39 |
|
| 40 |
\newdimen\packetsBitWidth\packetsBitWidth=5mm |
| 41 |
\newcounter{packets@FieldCounter} |
| 42 |
|
| 43 |
\newcommand{\packetsInit}{ |
| 44 |
\let\packetsFirstNodeInLastLine\undefined |
| 45 |
\let\packets@ContinueLine\undefined |
| 46 |
\setcounter{packets@FieldCounter}{0} |
| 47 |
\newcommand{\packetsBitFont}{\tiny} |
| 48 |
\packetsBitWidth=5mm |
| 49 |
\tikzstyle{protocolsFieldBase}=[ |
| 50 |
anchor = north west, outer sep = 0pt % node position <=> position of left upper node corner |
| 51 |
] |
| 52 |
\tikzstyle{protocolsField}=[ |
| 53 |
protocolsFieldBase, |
| 54 |
draw, minimum height = 1cm, |
| 55 |
] |
| 56 |
\tikzstyle{bitScaleInfo}=[ |
| 57 |
protocolsFieldBase, inner sep = 0pt, |
| 58 |
minimum width = \packetsBitWidth, minimum height = 8pt, |
| 59 |
node font=\packetsBitFont |
| 60 |
] |
| 61 |
} |
| 62 |
|
| 63 |
% |
| 64 |
% print bits numbering |
| 65 |
% |
| 66 |
|
| 67 |
|
| 68 |
\newcommand{\packetsPrintBitNumber}[1]{ |
| 69 |
\parbox[b][5pt]{\packetsBitWidth}{\hspace{1pt}#1} |
| 70 |
} |
| 71 |
|
| 72 |
\newcommand{\packetsPrintBitScale}[1]{ |
| 73 |
\node [bitScaleInfo] (bitScaleInfo_0) {\packetsPrintBitNumber{0}}; |
| 74 |
\foreach \n[remember=\n as \lastn (initially 0)] in {1,...,#1} { |
| 75 |
\node [bitScaleInfo] (bitScaleInfo_\n) [] at (bitScaleInfo_\lastn.north east) {\packetsPrintBitNumber{\n}}; |
| 76 |
\draw[thin] (bitScaleInfo_\n.south west) |- (bitScaleInfo_\n.north west); |
| 77 |
} |
| 78 |
\draw (bitScaleInfo_0.south west) |- (bitScaleInfo_0.north west); |
| 79 |
\draw (bitScaleInfo_#1.south east) |- (bitScaleInfo_#1.north east); |
| 80 |
|
| 81 |
\def\packetsFirstNodeInLastLine{bitScaleInfo_0} |
| 82 |
\def\packetsLastNode{bitScaleInfo_#1} |
| 83 |
} |
| 84 |
|
| 85 |
\newcommand{\packetsPrintRangeOnRight}[3][\packetsLastNode]{ |
| 86 |
\tcbsetmacrotoheightofnode{\packets@NodeHeight}{#1} |
| 87 |
\node [anchor = south, outer sep = 2pt, inner sep = 0pt, rotate = -90, align=center, node font=\packetsBitFont] [] at (#1.east) {\pbox{\packets@NodeHeight}{#2-#3}}; |
| 88 |
} |
| 89 |
|
| 90 |
\newcommand{\packetsPrintRangeOnLeft}[3][\packetsFirstNodeInLastLine]{ |
| 91 |
\node [anchor = east, outer sep = 2pt, inner sep = 0pt, align=center, node font=\packetsBitFont] [] at (#1.west) {#2}; |
| 92 |
} |
| 93 |
|
| 94 |
% |
| 95 |
% add protocol fields |
| 96 |
% |
| 97 |
%\newcommand{\packetsPutField}[3][protocolsField]{ |
| 98 |
\NewDocumentCommand{\packetsPutField}{ O{protocolsField} m o m } { |
| 99 |
% if provide custom node name, use it |
| 100 |
\IfValueTF {#3} { |
| 101 |
\edef\packets@NewNodeName{#3} |
| 102 |
}{ |
| 103 |
% otherwise get new node name and step counter |
| 104 |
\edef\packets@NewNodeName{packetsFieldNode_\arabic{packets@FieldCounter}} |
| 105 |
\stepcounter{packets@FieldCounter} |
| 106 |
} |
| 107 |
|
| 108 |
% if continue line |
| 109 |
\ifdefined\packets@ContinueLine |
| 110 |
\node[#1, minimum width=#2\packetsBitWidth] (\packets@NewNodeName) [] at (\packetsLastNode.north east) {#4}; |
| 111 |
% if start new line |
| 112 |
\else |
| 113 |
% if not first line |
| 114 |
\ifdefined\packetsFirstNodeInLastLine |
| 115 |
\node[#1, minimum width=#2\packetsBitWidth] (\packets@NewNodeName) [] at (\packetsFirstNodeInLastLine.south west) {#4}; |
| 116 |
\else |
| 117 |
\node[#1, minimum width=#2\packetsBitWidth] (\packets@NewNodeName) {#4}; |
| 118 |
\fi |
| 119 |
\let\packetsFirstNodeInLastLine\packets@NewNodeName |
| 120 |
\def\packets@ContinueLine{true} |
| 121 |
\fi |
| 122 |
|
| 123 |
% remember new node name as \packetsLastNode |
| 124 |
\let\packetsLastNode\packets@NewNodeName |
| 125 |
} |
| 126 |
|
| 127 |
% |
| 128 |
% finish line of fields |
| 129 |
% |
| 130 |
|
| 131 |
\newcommand{\packetsNextLine}{ |
| 132 |
\let\packets@ContinueLine\undefined |
| 133 |
} |
| 134 |
|
| 135 |
\newcommand{\packetsEndLine}[2]{ |
| 136 |
\packetsPrintRangeOnLeft{#1}{#2} |
| 137 |
\packetsNextLine |
| 138 |
} |