Speech bubble

From Wiki
Jump to navigation Jump to search

Overview

This page describes a few ways to create speech bubbles.

LMTX

Using ConTeXt LMTX, the following code generates a dependency-free speech bubble:

\startuseMPgraphic{TextBubble}{side}
  z1 = (0, 0) ;
  z2 = (OverlayWidth, 0) ;
  z3 = (OverlayWidth, OverlayHeight) ;
  z4 = (0, OverlayHeight) ;

  offset := 1 ;
  tail := 1 ;

  if \MPvar{side} = 1:
    offset := 5 ;
    tail := -1 ;
  fi

  (offset/6)[x1,x2] = x8 + 1cm * tail = x7 + .5cm = x9 - .5cm ;
  y7 =  0cm ;
  y8 = -1cm ;
  y9 =  0cm ;

  path p ;
  p = (z1--z7--z8--z9--z2--z3--z4--cycle) cornered .25cm ;
  draw p withpen pencircle scaled 0.5 ;

  setbounds currentpicture to OverlayBox ;
\stopuseMPgraphic

% Receive text (left-facing).
\defineoverlay[TextBubbleRxOverlay][\useMPgraphic{TextBubble}{side=0}]
\defineframedtext[TextBubbleRxFrame][
  frame=off,
  background=TextBubbleRxOverlay,
  width=fit,
  offset=.75em,
  location=left,
  after={\blank[1.25cm]},
]

% Send text (right-facing).
\defineoverlay[TextBubbleTxOverlay][\useMPgraphic{TextBubble}{side=1}]
\defineframedtext[TextBubbleTxFrame][
  frame=off,
  background=TextBubbleTxOverlay,
  width=fit,
  offset=.75em,
  location=right,
  after={\blank[1.25cm]},
]

\starttext 
  \startTextBubbleRxFrame
    \input weisman
  \stopTextBubbleRxFrame

  \startTextBubbleTxFrame
    \input ward
  \stopTextBubbleTxFrame

  \input knuth
\stoptext

Tikz Module

Use the Tikz module to create « speech bubbles ». Here are two ways of doing this: the first one works under mkii and mkiv, while the second one needs the Annotation module written by Wofgang Schuster who wrapped the Tikz commands in ConTeXt macros in the code below.


\usemodule[tikz]
\usetikzlibrary[shapes.callouts]

\setupbodyfont[bookman]

\setupTEXpage[offset=1em]

\starttext

\startTEXpage
\starttikzpicture[remember picture]
  \node[ellipse callout, draw] (node:ref) {Hi there!};
\stoptikzpicture
\stopTEXpage

\startTEXpage
\starttikzpicture[remember picture]
  \node[cloud callout, draw] (node:ref) {Hallo!};
\stoptikzpicture
\stopTEXpage

\startTEXpage
\starttikzpicture
  \node[cloud callout, cloud puffs=15, aspect=2.5, cloud puff arc=120,
    shading=ball,text=white] {\bf Imagine\dots};
\stoptikzpicture
\stopTEXpage

\startTEXpage
\starttikzpicture[remember picture, note/.style={rectangle callout, fill=#1}]
  \draw [help lines] grid(3,2);
  \node [note=red!50, callout relative pointer={(0,1)}] at (3,1) {Relative position};
  \node [note=blue!50, callout absolute pointer={(0,1)}] at (1,0) {Absolute position};
  \node [note=green!50, ellipse callout, opacity=.5, overlay, callout absolute pointer={(hallo.south)}] at (1,2) {Outside};
\stoptikzpicture
\stopTEXpage

\stoptext

In the above example, you can leave out the \startTEXpage and \stopTEXpage, and use the pictures and you wish in your text.

The second example is as follows:


\usemodule[tikz,annotation]
\usetikzlibrary[shapes.callouts]

\define[2]\SpeechbubbleCommand
 {\starttikzpicture[remember picture]
  \node[rectangle callout, draw] (node:ref) {\framedtext[width=fit,frame=off]{#2}};
  \stoptikzpicture}

\defineannotation
 [speechbubble]
 [alternative=command,
      command=\SpeechbubbleCommand]

\starttext

\speechbubble{Hello!}

\startspeechbubble
\input ward
\stopspeechbubble

\stoptext

As said before the above code needs mkiv functionalities (due to the usage of the Annotation module). Indeed you can change some of Tikz commands in the above defintions, for instance change the command

\node[rectangle callout, draw]

into

\node[cloud callout, draw]

to get other bubble shapes.