473,378 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

text justification

Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
Thanks,
Gaetano

Oct 23 '07 #1
10 3935
ga***********@yahoo.it wrote:
Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
Thanks,
Have you tried to find the source code of nroff?
Oct 23 '07 #2
In article <11*********************@t8g2000prg.googlegroups.c om>,
<ga***********@yahoo.itwrote:
>Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
Thanks,
Gaetano
I don't understand why you need to justify fixed fonts. As is the
standard advice in this ng when such questions arise, if you don't like
them, just don't use them.

Oct 23 '07 #3
In article <11*********************@t8g2000prg.googlegroups.c om>,
<ga***********@yahoo.itwrote:
>can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
Well, the code for nroff should be easy enough to find (look for
groff, the most widely-used implementation).

But I recommend not justifying with fixed width fonts. It's much
harder to read.

If you really want to do it, you fill the line with as many words as
will fit, then distribute the required number of extra spaces in the
gaps. You might use a version of Bressenham's algorithm (try Google)
for this.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 23 '07 #4
In article <ff**********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
>In article <11*********************@t8g2000prg.googlegroups.c om>,
<ga***********@yahoo.itwrote:
>>can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?

Well, the code for nroff should be easy enough to find (look for
groff, the most widely-used implementation).

But I recommend not justifying with fixed width fonts. It's much
harder to read.

If you really want to do it, you fill the line with as many words as
will fit, then distribute the required number of extra spaces in the
gaps. You might use a version of Bressenham's algorithm (try Google)
for this.
Another thing to look at is "par".

Oct 23 '07 #5
On Oct 23, 11:33 am, gaetanoort...@yahoo.it wrote:
Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
Thanks,
Gaetano
here's a routine to get you started. This doesn't do justifications,
but it will start you on the way.
This shouldn't violate too many std C rules. 8^)

/*
** START DOC
**
** wordwrap()
**
** Ed Prochak
** 1 June 1995
**
** The wordwrap function splits the input string at whitespace
** which is at the wrap position specified or less (ie left).
** The string is truncated at the wrap point if no whitespace
** is found within wrap margin (wrap_mgn) characters.
** (Sorry, it does not hyphenate!)
** If the wrap margin value is zero, this routine will simply
** truncate the string at the wrap point. The function returns
** a pointer value which indicates the location within the input
** string just after the last character that was copied to the
** output string. This allows the caller to set up a loop to
** repeatedly call this function to split a string multiple times.
**
**
** Parameters :-
** instring - pointer to the current position for the
** input text string
** wrap_pnt - the wrap point specifies the last position
** in the output field. It is the length of the
** output string minus one.
** wrap_mgn - the wrap margin specifies the backup limit
** in searching for whitespace for spliting the
** line. If no whitespace is found within this many
** characters, the string is merely truncated at
** the wrap point.
** outstring - output string is a copy of the input from
** position 0 to the wrap_pnt (or less)
**
** Return Value :-
** the function returns a pointer to the place in the input
** string which immediately follows the last character moved
** to the output string. This allows repeated calls to split a
** large string multiple times.
** Errors :-
** A null pointer value is returned in case of an error.
** Possible errors include:
** a null pointer value for either instring or outstring,
** instring and outstring pointing to the same location,
** a wrap margin that may back up too far,
** a wrap margin or wrap point that is negative,
** or zero value for the wrap point.
**
** Example :-
**
** Suppose the requirement is to print a column 14 character wide
** and allows words longer than 3 charaters to be split. Then
** given an input string:
**
** ls =
** "12345678901234 long string with tabs ., and more_such_junk"
**
** the following code:
**
** char part[15]
** char *s=&ls
** do {
** s = wordwrap( s, 14, 3, part )
** printf("*%14s*\n",part)
** }
** while ( *s!='\0' ) {
**
** will print:
**
** *12345678901234*
** * long string*
** * with tabs .,*
** * and more_such*
** *_junk*
**
** ----------------------------------------------------------
** The diagram below should illustrate the wrap margin value.
**
** this places the wrap point here!
** ^ ^ ^
** | | |
** | wrap_mgn=4 --+ +--- wrap_pnt=32
** |
** +----wrap_mgn=20
**
** ----------------------------------------------------------
**
** Assumptions :-
** == the output string is assumed to be at least as long as
** wrap_pnt+1 where the extra position will be filled with
** the string terminator.
** == whitespace characters are assumed to include all
** characters with binary values less than or equal to
** the blank ' ' character.
**
** END DOC
*/

#ifndef NULL
#define NULL ((char *)0)
#endif

/**********/
char *wordwrap(
char *instring,
int wrap_pnt,
int wrap_mgn,
char *outstring )
{
int i;
char *in;
char *out;

/* PERFORM SANITY CHECKS - MAKE SURE WE DON'T PARSE GARBAGE */

if( instring == outstring ) return (NULL);
if( NULL == instring ) return (NULL);
if( NULL == outstring ) return (NULL);
if( wrap_pnt < wrap_mgn ) return (NULL);
if( wrap_pnt < 1 ) return (NULL);
if( wrap_mgn < 0 ) return (NULL);
/* START WITH A SIMPLEMINDED COPY */

in = instring;
out = outstring;
for ( i=0; i<wrap_pnt; i++ )
{
if ( '\0'==*in ) break;
*out = *in;
out++; in++;
}
*out='\0';
/* DID WE REACH THE END OF INPUT BEFORE THE FILLING THE OUTPUT? */
/* IF YES, JUST RETURN THE END OF STRING LOCATION. */

if ( '\0'==*in ) return (in);

/* THE OUTPUT BUFFER IS FULL, BACK UP TO FIND THE SPLIT POINT */

for ( i=0; i<wrap_mgn; i++, in-- )
{
/* IF WHITESPACE FOUND THEN MAKE THE BREAK */

if ( *in <= ' ' ) break;

}

/* IF NO WHITESPACE TO SPLIT ON, THEN TRUNCATE */

if ( i==wrap_mgn )
{
i=0 ;
in=in+wrap_mgn;
}
out=out-i; /* BACKUP TO THE WHITESPACE */
*out='\0'; /* OVERWRITE THE WHITESPACE CHAR. WITH TERMINATOR */

return(in);
}
/************************************************** ****************/

Oct 23 '07 #6

Thanks

Oct 23 '07 #7
ga***********@yahoo.it wrote:
>
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?
The following simple program (a modification of my generalized
filter source) may be useful:

/* ----- justify.c -----
Filter text file, right justifying by inserting
spaces between words. Words are anything separated
by blanks, tabs, newlines, formfeeds, bell, etc.

The single (optional) parameter is the output line
length, and defaults to 65. Execution without any
input redirections causes a help message.

This is a quick and dirty utility.
Released to public domain 2003-01-28 by:
<mailto:cb********@maineline.net>
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define RHDEFAULT 65
#define RHMIN 20

static int rhcol; /* right hand column limit */
static int ragged; /* No rh justification, 0 init */

/* ------------------- */

/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp)));
} /* akeyboard */

/* ------------------- */

static void help(char *phrase1, char *phrase2)
{
if (phrase1) fprintf(stderr, "%s", phrase1);
if (phrase2) fprintf(stderr, "%s", phrase2);
fprintf(stderr, "\n"
"Usage: justify [rightmargin] <infile >outfile\n"
" The default rightmargin is 65\n"
" and values less than 20 are rejected\n"
"\n"
"A large value of rightmargin will effectively\n"
"convert all paragraphs into single lines\n"
"\n"
"A negative rightmargin causes ragged right\n"
"\n"
"A blank line delimits paragraphs\n");
} /* help */

/* ------------------- */

static int initialize(int argc, char *argv[])
{
long rightcol;
char *err;

if (akeyboard(stdin) || (argc 2)) {
help(NULL, NULL);
return 0;
}
rhcol = RHDEFAULT;
if (2 == argc) {
rightcol = strtol(argv[1], &err, 10);
if (rightcol < 0) {
rightcol = -rightcol;
ragged = 1;
}
if ((err == argv[1]) || (rightcol < RHMIN)) {
help("Bad argument: ", argv[1]);
return 0;
}
else rhcol = rightcol;
}
return 1;
} /* initialize */

/* ------------------- */

static void cleanup(void)
{
} /* cleanup */

/* ------------------- */

/* ================================== */
/* Routines for text input and output */
/* ================================== */

static void skipblanks(FILE *f)
{
int ch;

while ( (' ' == (ch = getc(f))) || ('\t' == ch) ||
('\v' == ch) || ('\f' == ch) || ('\a' == ch) )
continue;
ungetc(ch, f);
} /* skipblanks */

/* ------------------- */

/* The file is assumed to hold no control chars */
/* other than \n \t \v \a and \f. A blank line */
/* marks a paragraph ending word */
static int nextword(FILE *f, char *buffer, int max)
{
int i, ch;

skipblanks(f);
if (EOF == (ch = getc(f))) return 0;

/* Detect paragraph endings as \n\n */
if ('\n' == ch) {
skipblanks(f); ch = getc(f);
if ('\n' == ch) { /* paragraph ending */
buffer[0] = buffer[1] = ch; /* wd = "\n\n" */
buffer[2] = '\0';
/* now we have to absorb any more blank lines */
do {
skipblanks(f); ch = getc(f);
} while ('\n' == ch);
ungetc(ch, f);
return 1;
}
}
/* now ch holds the first non-blank. Use all printable */
if (EOF == ch) return 0;
if (!isgraph(ch)) {
fprintf(stderr, "'%c', 0x%x WARN: Invalid character\n",
ch, (unsigned)ch);
}

i = 0;
do {
buffer[i++] = ch;
if (i >= max) { /* truncate over long words */
i--;
break; /* leaving ch for next word */
}
ch = getc(f);
} while (isgraph(ch));

ungetc(ch, f); /* save for next word, may be \n */
buffer[i] = '\0'; /* terminate string */
return 1;
} /* nextword */

/* ------------------- */

static void justify(char *ln, int wdgaps, int xtra, FILE *out)
{
int insert, i;
static int oddln = 0; /* for rt left blank insertion */
char ch;

#ifdef DEBUG
fprintf(out, "%2d %2d ", wdgaps, xtra);
#endif
insert = 0; oddln = !oddln;
if (wdgaps)
while (xtra wdgaps) {
insert++; xtra -= wdgaps;
}
while ((ch = *ln++)) {
putc(ch, out);
if (' ' == ch) {
if (xtra) {
xtra--;
putc(' ', out);
}
for (i = insert; i; i--) putc(' ', out);
}
}
putc('\n', out);
} /* justify */

/* ------------------- */

static int filter(FILE *in, FILE *out)
{
char *buf;
char *ln;
int wdcount, lnlgh, wdlgh;
char *eop = "\n\n"; /* end of paragraph */
int done, endpar;

if (!(buf = malloc(rhcol+1))) exit(EXIT_FAILURE);
if (!(ln = malloc(rhcol+1))) exit(EXIT_FAILURE);

done = !nextword(in, buf, rhcol + 1);
endpar = !strcmp(buf, eop);

while (!endpar && !done) {
/* form paragraph */
wdlgh = strlen(buf);
wdcount = 0;
*ln = '\0'; lnlgh = 0;

while ((((lnlgh + wdlgh) < rhcol) || !lnlgh)
&& !done && !endpar) {
/* form a line */
if (lnlgh) ln[lnlgh++] = ' ';
strcpy(ln + lnlgh, buf);
lnlgh += wdlgh;
wdcount++;

done = !nextword(in, buf, rhcol + 1);
endpar = !strcmp(buf, eop);
wdlgh = strlen(buf);
}

/* dump the line, wdcount words */
if (endpar || done) lnlgh = rhcol;
if (ragged) fprintf(out, "%s\n", ln);
else justify(ln, wdcount-1, rhcol-lnlgh, out);

if (endpar) {
fputc('\n', out);
done = !nextword(in, buf, rhcol + 1);
endpar = !strcmp(buf, eop);
}
}
return 0;
} /* filter */

/* ------------------- */

int main(int argc, char *argv[])
{
if (!initialize(argc, argv)) return EXIT_FAILURE;
else {
(void)filter(stdin, stdout);
cleanup();
}
return 0;
} /* main */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 23 '07 #8
Ed Prochak <ed*******@gmail.comwrites:
On Oct 23, 11:33 am, gaetanoort...@yahoo.it wrote:
>Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?

here's a routine to get you started. This doesn't do justifications,
but it will start you on the way.
This shouldn't violate too many std C rules. 8^)
[...]
#ifndef NULL
#define NULL ((char *)0)
#endif
[...]

Why do you do this rather than just ``#include <stddef.h>'' (or one of
the other standard headers that define NULL)?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 28 '07 #9
Keith Thompson <ks***@mib.orgwrites:
Ed Prochak <ed*******@gmail.comwrites:
>On Oct 23, 11:33 am, gaetanoort...@yahoo.it wrote:
>>Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?

here's a routine to get you started. This doesn't do justifications,
but it will start you on the way.
This shouldn't violate too many std C rules. 8^)
[...]
>#ifndef NULL
#define NULL ((char *)0)
#endif
[...]

Why do you do this rather than just ``#include <stddef.h>'' (or one of
the other standard headers that define NULL)?
And as I realized just after I posted this, ``((char *)0)'' isn't even
a valid definition for NULL.

The language defines it for you. Just use it. And if you want to
define something else (say, something that's specifically of type
char*), give it a different name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 28 '07 #10
clearly correct.

Sometimes the ISO soln is the best one.

--
South Jordan pkwy
435 -838-7760
President
wa**@zaxfuuq.net
Westates Companies
Merrill Jensen Consulting
1108 W. South Jordan pkwy
42
wa**@zaxfuuq.net
Merrill Jensen Consulting
1108 W. South Jordan pkwy
435 -838-7760
President
wa**@zaxfuuq.net
Westates Companies
1108 W. South Jordan pkwy
435 -838-7760
President
wa**@zaxfuuq.net

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
Keith Thompson <ks***@mib.orgwrites:
>Ed Prochak <ed*******@gmail.comwrites:
>>On Oct 23, 11:33 am, gaetanoort...@yahoo.it wrote:
Hello,
can anyone point me to some piece of code that do text
justification of fixed fonts like those that nroff do on manpages?

here's a routine to get you started. This doesn't do justifications,
but it will start you on the way.
This shouldn't violate too many std C rules. 8^)
[...]
>>#ifndef NULL
#define NULL ((char *)0)
#endif
[...]

Why do you do this rather than just ``#include <stddef.h>'' (or one of
the other standard headers that define NULL)?

And as I realized just after I posted this, ``((char *)0)'' isn't even
a valid definition for NULL.

The language defines it for you. Just use it. And if you want to
define something else (say, something that's specifically of type
char*), give it a different name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Oct 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Owen Jacobson | last post by:
We all know that text-align: justify; is supposed to justify the block's text across the box. I have a friend, however, who wants to take that further. A visual example, because it's 2:30 AM and...
2
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to input data from strings to a memo field. I would like to know how to check first to see if there is text there currently and if so...
1
by: Alex | last post by:
I have looked at the code for text justification on lebans.com and this seems to work very well. Except my report is a subreport and I cannot get the code to work on a subreport. Does anyone know...
1
by: Alex | last post by:
The JustiDirect text justification on Stephen LeBans site works well but I cannot get it to work on a sub report. Could anyone tell me if this is possible. Thanks. Alex
4
by: devine | last post by:
Hi All, I am VERY new to Javascript. I have been provided with some code, which will enable me to hide/show a text area and change a submit button dependant on a check box. <!DOCTYPE html...
29
by: Michael Bulatovich | last post by:
Is there a way to use CSS to format "plain" text in an html document. By plain I mean text which is not contained by <por <h#tags. Is there no way to control how this stuff is rendered? tia
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
0
by: Wieland | last post by:
I have a rich text box that I'm using to edit rtf documents that need to be full justified the SelectionAlignment only has right, left and center. Does any one know how to get full justification...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.