Format a Drop Capital

21

0

Introduction

Write a program or function that adds a drop capital to a paragraph. The program will input the text to format, the column width, and the number of lines to drop the capital. The drop capitals look like this:

Lines:        2     3      4 
Drop capital: A|    A.|    A..|
              ~'    ..|    ...|    etc.
                    ~~'    ...|
                           ~~~'

This is a related challenge.

Input

  • Input a string of printable ASCII characters (no tabs or newlines) and 2 integers greater than one.
  • One integer is the number of columns that the output should have.
  • The other is the number of lines spanned by the drop capital.
  • The text string contains words separated by single spaces.
  • Assume none of the words will be longer than the column width. That is, column width > longest word + drop capital height
  • All lines will have at least one word.
  • For this challenge, a word consists of any character other than a space.
  • Input may be in any convenient format following the rules above.

Output

  • A left-justified block of text containing a drop capital with the specified number of lines.
  • A line should contain as many words as possible without being longer than the column width.
  • There is a space between the drop capital and the rest of each line.
  • One trailing space or newline is allowed.

Examples

Lines: 2 Columns: 10 Text: The quick brown fox jumped over the lazy dog.

T| he
~' quick
brown fox
jumped
over the
lazy dog.

Lines: 3 Columns: 10 Text: (Same as above)

T.| he
..| quick
~~' brown
fox jumped
over the
lazy dog.

Lines: 4 Columns: 10 Text: (Same as above)

T..| he
...| quick
...| brown
~~~' fox
jumped
over the
lazy dog.

Lines: 2 Columns: 80 Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.

L| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
~' Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero
egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem
lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.

Lines: 3 Columns: 80 Text: (Same as above)

L.| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
..| Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
~~' consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget
libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta
lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non
tortor.

Lines: 4 Columns: 80 Text: (Same as above)

L..| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
...| lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra
...| nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam
~~~' eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim,
ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies
a non tortor.

This is and standard loopholes apply.

intrepidcoder

Posted 2015-11-04T02:20:50.217

Reputation: 2 575

3Yay, another [tag:typography] challenge! We need more of these. – ETHproductions – 2015-11-04T02:49:17.370

2What happens when a word is longer than a column width, ie The quick brown fox jumped over the unenthusiastic dog for any of the first three examples? – MickyT – 2015-11-04T19:07:24.497

@MickyT Assume none of the words will be longer than the column width. That is, column width > longest word + drop capital height – intrepidcoder – 2015-11-05T02:37:04.750

What if the capital lines are greater than the line of text? Ie. L:4,C:100,T:'Stuff' – TFeld – 2015-11-05T08:45:04.397

Also, can Lines = 1? – TFeld – 2015-11-05T09:30:21.573

@TFeld You may assume those are both invalid inputs. Both numbers are strictly greater than one and every line will have at least one word. – intrepidcoder – 2015-11-05T14:53:54.077

Answers

1

Python 2, 202 bytes

def f(l,c,t):
 l-=1;s=['.'*l+'|']*l+['~'*l+"'"]
 s[0]=t[0]+s[0][1:];t=t[1:].split();j=0
 while t:
  w=t.pop(0)
  if len(s[j]+w)>=c:j+=1
  if j>=len(s):s.append(w)
  else:s[j]+=' '+w
 return '\n'.join(s)

Call as f(Lines, Columns, Text)

f(4,100,'Stuff') gives

S..| tuff
...|
...|
~~~'

TFeld

Posted 2015-11-04T02:20:50.217

Reputation: 19 246

1

C#, 244 bytes

string F(int d,int c,string t){var w=t.Substring(1).Split(' ');t=""+t[0];for(int i=0,x=c,y=0;i<w.Length;x+=w[i++].Length+1)t+=(1>(x=x+w[i].Length>c?0:x)?"\n"+(y++<d?(y<d?"| ":"' ").PadLeft(x=d+1,y<d?'.':'~'):""):" ")+w[i];return t.Remove(1,2);}

Indentation, new lines and comments for clarity:

string F(int d,int c,string t){
    var w=t.Substring(1).Split(' ');
    t=""+t[0];
    for(int i=0,x=c,y=0;i<w.Length;x+=w[i++].Length+1)
        t+=(1>(x=x+w[i].Length>c?0:x)?"\n"+(y++<d?(y<d?"| ":"' ").PadLeft(x=d+1,y<d?'.':'~'):""):" ")+w[i];
    return t.Remove(1,2);
}

Hand-E-Food

Posted 2015-11-04T02:20:50.217

Reputation: 7 912