Home > marsbar > @mardo_5 > private > pr_spm_justify.m

pr_spm_justify

PURPOSE ^

Justify text

SYNOPSIS ^

function out = pr_spm_justify(n,varargin)

DESCRIPTION ^

 Justify text
 FORMAT out = justify(n,txt)
 out - a cell array of lines of text
 n   - line length
 txt - a text string or a cell array of text strings

 If txt is a cell array, then each element is treated
 as a paragraph and justified, otherwise the string is
 treated as a paragraph and is justified.
 Non a-z or A-Z characters at the start of a paragraph
 are used to define any indentation required (such as
 for enumeration, bullets etc.  If less than one line
 of text is returned, then no formatting is done.

 Example usage:

    out = pr_spm_justify(40,{['Statistical Parametric ',...
    'Mapping refers to the construction and ',...
    'assessment of spatially extended ',...
    'statistical process used to test hypotheses ',...
    'about [neuro]imaging data from SPECT/PET & ',...
    'fMRI. These ideas have been instantiated ',...
    'in software that is called SPM']});
    strvcat(out{:})

------------------------------------------------------------------------
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out = pr_spm_justify(n,varargin)
0002 % Justify text
0003 % FORMAT out = justify(n,txt)
0004 % out - a cell array of lines of text
0005 % n   - line length
0006 % txt - a text string or a cell array of text strings
0007 %
0008 % If txt is a cell array, then each element is treated
0009 % as a paragraph and justified, otherwise the string is
0010 % treated as a paragraph and is justified.
0011 % Non a-z or A-Z characters at the start of a paragraph
0012 % are used to define any indentation required (such as
0013 % for enumeration, bullets etc.  If less than one line
0014 % of text is returned, then no formatting is done.
0015 %
0016 % Example usage:
0017 %
0018 %    out = pr_spm_justify(40,{['Statistical Parametric ',...
0019 %    'Mapping refers to the construction and ',...
0020 %    'assessment of spatially extended ',...
0021 %    'statistical process used to test hypotheses ',...
0022 %    'about [neuro]imaging data from SPECT/PET & ',...
0023 %    'fMRI. These ideas have been instantiated ',...
0024 %    'in software that is called SPM']});
0025 %    strvcat(out{:})
0026 %
0027 %------------------------------------------------------------------------
0028 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0029 
0030 % John Ashburner
0031 % $Id: spm_justify.m 232 2005-09-15 19:02:59Z john $
0032 
0033 out = {};
0034 for i=1:nargin-1,
0035     if iscell(varargin{i}),
0036         for j=1:numel(varargin{i}),
0037             para = justify_paragraph(n,varargin{i}{j});
0038             out  = {out{:},para{:}};
0039         end;
0040     else
0041         para = justify_paragraph(n,varargin{i});
0042         out = {out{:},para{:}};
0043     end;
0044 end;
0045 
0046 function out = justify_paragraph(n,txt)
0047 if numel(txt)>1 && txt(1)=='%',
0048     txt = txt(2:end);
0049 end;
0050 %txt = regexprep(txt,'/\*([^(/\*)]*)\*/','');
0051 st1  = findstr(txt,'/*');
0052 en1  = findstr(txt,'*/');
0053 st = [];
0054 en = [];
0055 for i=1:numel(st1),
0056     en1  = en1(en1>st1(i));
0057     if ~isempty(en1),
0058         st  = [st st1(i)];
0059         en  = [en en1(1)];
0060         en1 = en1(2:end);
0061     end;
0062 end;
0063 
0064 str = [];
0065 pen = 1;
0066 for i=1:numel(st),
0067     str = [str txt(pen:st(i)-1)];
0068     pen = en(i)+2;
0069 end;
0070 str = [str txt(pen:numel(txt))];
0071 txt = str;
0072 
0073 off = find((txt'>='a' & txt'<='z') | (txt'>='A' & txt'<='Z'));
0074 off = off(off<n);
0075 if isempty(off),
0076     out{1} = txt;
0077 else
0078     off  = off(1);
0079     para = justify_para(n-off+1,txt(off:end));
0080     if numel(para)>1,
0081         out{1} = [txt(1:(off-1)) para{1}];
0082         for j=2:numel(para),
0083             out{j} = [repmat(' ',1,off-1) para{j}];
0084         end;
0085     else
0086         out{1} = txt;
0087     end;
0088 end;
0089 return;
0090 
0091 function out = justify_para(n,varargin)
0092 % Collect varargs into a single string
0093 str = varargin{1};
0094 for i=2:length(varargin),
0095     str = [str ' ' varargin{i}];
0096 end;
0097 
0098 if isempty(str), out = {''}; return; end;
0099 
0100 % Remove repeats
0101 sp  = find(str==' ');
0102 rep = sp(diff(sp)==1);
0103 str(rep) = [];
0104 if str(1)  ==' ', str(1)   = ''; end;
0105 if str(end)==' ', str(end) = ''; end;
0106 
0107 out = {};
0108 while length(str)>n,
0109 
0110     % Break the string into lines
0111     sp  = find(str==' ');
0112     brk = sp(sp<=n);
0113     if isempty(brk),
0114         if isempty(sp),
0115             brk = length(str)+1;
0116         else
0117             brk = sp(1);
0118         end;
0119     else
0120         brk = brk(end);
0121     end;
0122 
0123     % Pad the line to n characters wide
0124     current = str(1:(brk-1));
0125     % l   = length(current);
0126     % l   = n-l;
0127     sp  = find(current==' ');
0128     if ~isempty(sp),
0129 
0130         % Break into words
0131         sp    = [sp length(current)+1];
0132         words = {current(1:(sp(1)-1))};
0133         for i=1:(length(sp)-1),
0134             words = {words{:}, current((sp(i)+1):(sp(i+1)-1))};
0135         end;
0136 
0137         % Figure out how much padding on average
0138         nsp = length(sp)-1;
0139         pad = (n-(length(current)-nsp))/nsp;
0140 
0141         % Pad all spaces by the same integer amount
0142         sp  = repmat(floor(pad),1,nsp);
0143 
0144         % Pad a random selection of spaces by one
0145         pad = round((pad-floor(pad))*nsp);
0146         [unused,ind] = sort(rand(pad,1));
0147         ind = ind(1:pad);
0148         sp(ind) = sp(ind)+1;
0149 
0150         % Re-construct line from individual words
0151         current = words{1};
0152         for i=2:length(words),
0153             current = [current repmat(' ',1,sp(i-1)) words{i}];
0154         end;
0155     end;
0156 
0157     out = {out{:},current};
0158     str = str((brk+1):end);
0159 end;
0160 
0161 out = {out{:},str};

Generated on Wed 11-May-2022 15:34:44 by m2html © 2003-2019