From 79275a978913b71c477538e89172ace29cfe1fd7 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Tue, 18 Apr 2000 21:04:21 +0000 Subject: [PATCH] Changed to use nsILineBreaker instead of IsAsciiSpace, bug 27062, r=akkana. git-svn-id: svn://10.0.0.236/trunk@66304 18797224-902f-48f8-a5cc-f745e15eee43 --- .../htmlparser/src/nsHTMLToTXTSinkStream.cpp | 69 ++++++++++++++++--- .../htmlparser/src/nsHTMLToTXTSinkStream.h | 2 + .../htmlparser/src/nsHTMLToTXTSinkStream.cpp | 69 ++++++++++++++++--- .../htmlparser/src/nsHTMLToTXTSinkStream.h | 2 + 4 files changed, 122 insertions(+), 20 deletions(-) diff --git a/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.cpp b/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.cpp index 624050ab65c..40d87a06bca 100644 --- a/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.cpp +++ b/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.cpp @@ -46,10 +46,13 @@ #include "nsICharsetAlias.h" #include "nsIServiceManager.h" #include "nsICharsetConverterManager.h" +#include "nsILineBreakerFactory.h" +#include "nsLWBrkCIID.h" #include "nsIOutputStream.h" #include "nsFileStream.h" static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); +static NS_DEFINE_CID(kLWBrkCID, NS_LWBRK_CID); const PRInt32 gTabSize=4; const PRInt32 gOLNumberWidth = 3; @@ -174,6 +177,7 @@ nsHTMLToTXTSinkStream::nsHTMLToTXTSinkStream() mBufferLength = 0; mBuffer = nsnull; mUnicodeEncoder = nsnull; + mLineBreaker = nsnull; mWrapColumn = 72; // XXX magic number, we expect someone to reset this // Flow @@ -206,6 +210,7 @@ nsHTMLToTXTSinkStream::~nsHTMLToTXTSinkStream() delete[] mTagStack; delete[] mOLStack; NS_IF_RELEASE(mUnicodeEncoder); + NS_IF_RELEASE(mLineBreaker); } /** @@ -223,7 +228,22 @@ nsHTMLToTXTSinkStream::Initialize(nsIOutputStream* aOutStream, mString = aOutString; mFlags = aFlags; - return NS_OK; + nsILineBreakerFactory *lf; + nsresult result = NS_OK; + + result = nsServiceManager::GetService(kLWBrkCID, + NS_GET_IID(nsILineBreakerFactory), + (nsISupports **)&lf); + if (NS_SUCCEEDED(result)) { + nsAutoString lbarg(""); + result = lf->GetBreaker(lbarg, &mLineBreaker); + if(NS_FAILED(result)) { + mLineBreaker = nsnull; + } + result = nsServiceManager::ReleaseService(kLWBrkCID, lf); + } + + return result; } /** @@ -977,26 +997,55 @@ nsHTMLToTXTSinkStream::AddToLine(const PRUnichar * aLineFragment, PRInt32 aLineF // of letters too long. while(linelength+prefixwidth > mWrapColumn+4) { // Must wrap. Let's find a good place to do that. - PRInt32 goodSpace = mWrapColumn-prefixwidth; - while (goodSpace >= 0 && - !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { - goodSpace--; + PRInt32 goodSpace = mWrapColumn-prefixwidth+1; + nsresult result = NS_OK; + PRBool oNeedMoreText; + if (nsnull != mLineBreaker) { + result = mLineBreaker->Prev(mCurrentLine.GetUnicode(), mCurrentLine.Length(), goodSpace, + (PRUint32 *) &goodSpace, &oNeedMoreText); + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) + --goodSpace; // adjust the position since line breaker returns a position next to space + } + // fallback if the line breaker is unavailable or failed + if (nsnull == mLineBreaker || NS_FAILED(result)) { + goodSpace = mWrapColumn-prefixwidth; + while (goodSpace >= 0 && + !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { + goodSpace--; + } } nsAutoString restOfLine; if(goodSpace<0) { // If we don't found a good place to break, accept long line and // try to find another place to break - goodSpace=mWrapColumn-prefixwidth; - while (goodSpace < linelength && - !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { - goodSpace++; + goodSpace=mWrapColumn-prefixwidth+1; + result = NS_OK; + if (nsnull != mLineBreaker) { + result = mLineBreaker->Next(mCurrentLine.GetUnicode(), mCurrentLine.Length(), goodSpace, + (PRUint32 *) &goodSpace, &oNeedMoreText); + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) + --goodSpace; // adjust the position since line breaker returns a position next to space + } + // fallback if the line breaker is unavailable or failed + if (nsnull == mLineBreaker || NS_FAILED(result)) { + goodSpace=mWrapColumn-prefixwidth; + while (goodSpace < linelength && + !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { + goodSpace++; + } } } if(goodSpace < linelength && goodSpace > 0) { // Found a place to break - mCurrentLine.Right(restOfLine, linelength-goodSpace-1); + + // -1 (trim a char at the break position) + // only if the line break was a space. + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) + mCurrentLine.Right(restOfLine, linelength-goodSpace-1); + else + mCurrentLine.Right(restOfLine, linelength-goodSpace); mCurrentLine.Cut(goodSpace, linelength-goodSpace); EndLine(PR_TRUE); mCurrentLine.Truncate(); diff --git a/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.h b/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.h index 767b3f5cb64..49c5e05560e 100644 --- a/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/mozilla/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -54,6 +54,7 @@ class nsIUnicodeEncoder; +class nsILineBreaker; class nsIOutputStream; class nsIHTMLToTXTSinkStream : public nsIHTMLContentSink { @@ -195,6 +196,7 @@ protected: nsIUnicodeEncoder* mUnicodeEncoder; nsString mCharsetOverride; + nsILineBreaker* mLineBreaker; }; inline nsresult diff --git a/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp b/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp index 624050ab65c..40d87a06bca 100644 --- a/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp +++ b/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp @@ -46,10 +46,13 @@ #include "nsICharsetAlias.h" #include "nsIServiceManager.h" #include "nsICharsetConverterManager.h" +#include "nsILineBreakerFactory.h" +#include "nsLWBrkCIID.h" #include "nsIOutputStream.h" #include "nsFileStream.h" static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); +static NS_DEFINE_CID(kLWBrkCID, NS_LWBRK_CID); const PRInt32 gTabSize=4; const PRInt32 gOLNumberWidth = 3; @@ -174,6 +177,7 @@ nsHTMLToTXTSinkStream::nsHTMLToTXTSinkStream() mBufferLength = 0; mBuffer = nsnull; mUnicodeEncoder = nsnull; + mLineBreaker = nsnull; mWrapColumn = 72; // XXX magic number, we expect someone to reset this // Flow @@ -206,6 +210,7 @@ nsHTMLToTXTSinkStream::~nsHTMLToTXTSinkStream() delete[] mTagStack; delete[] mOLStack; NS_IF_RELEASE(mUnicodeEncoder); + NS_IF_RELEASE(mLineBreaker); } /** @@ -223,7 +228,22 @@ nsHTMLToTXTSinkStream::Initialize(nsIOutputStream* aOutStream, mString = aOutString; mFlags = aFlags; - return NS_OK; + nsILineBreakerFactory *lf; + nsresult result = NS_OK; + + result = nsServiceManager::GetService(kLWBrkCID, + NS_GET_IID(nsILineBreakerFactory), + (nsISupports **)&lf); + if (NS_SUCCEEDED(result)) { + nsAutoString lbarg(""); + result = lf->GetBreaker(lbarg, &mLineBreaker); + if(NS_FAILED(result)) { + mLineBreaker = nsnull; + } + result = nsServiceManager::ReleaseService(kLWBrkCID, lf); + } + + return result; } /** @@ -977,26 +997,55 @@ nsHTMLToTXTSinkStream::AddToLine(const PRUnichar * aLineFragment, PRInt32 aLineF // of letters too long. while(linelength+prefixwidth > mWrapColumn+4) { // Must wrap. Let's find a good place to do that. - PRInt32 goodSpace = mWrapColumn-prefixwidth; - while (goodSpace >= 0 && - !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { - goodSpace--; + PRInt32 goodSpace = mWrapColumn-prefixwidth+1; + nsresult result = NS_OK; + PRBool oNeedMoreText; + if (nsnull != mLineBreaker) { + result = mLineBreaker->Prev(mCurrentLine.GetUnicode(), mCurrentLine.Length(), goodSpace, + (PRUint32 *) &goodSpace, &oNeedMoreText); + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) + --goodSpace; // adjust the position since line breaker returns a position next to space + } + // fallback if the line breaker is unavailable or failed + if (nsnull == mLineBreaker || NS_FAILED(result)) { + goodSpace = mWrapColumn-prefixwidth; + while (goodSpace >= 0 && + !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { + goodSpace--; + } } nsAutoString restOfLine; if(goodSpace<0) { // If we don't found a good place to break, accept long line and // try to find another place to break - goodSpace=mWrapColumn-prefixwidth; - while (goodSpace < linelength && - !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { - goodSpace++; + goodSpace=mWrapColumn-prefixwidth+1; + result = NS_OK; + if (nsnull != mLineBreaker) { + result = mLineBreaker->Next(mCurrentLine.GetUnicode(), mCurrentLine.Length(), goodSpace, + (PRUint32 *) &goodSpace, &oNeedMoreText); + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) + --goodSpace; // adjust the position since line breaker returns a position next to space + } + // fallback if the line breaker is unavailable or failed + if (nsnull == mLineBreaker || NS_FAILED(result)) { + goodSpace=mWrapColumn-prefixwidth; + while (goodSpace < linelength && + !nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) { + goodSpace++; + } } } if(goodSpace < linelength && goodSpace > 0) { // Found a place to break - mCurrentLine.Right(restOfLine, linelength-goodSpace-1); + + // -1 (trim a char at the break position) + // only if the line break was a space. + if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) + mCurrentLine.Right(restOfLine, linelength-goodSpace-1); + else + mCurrentLine.Right(restOfLine, linelength-goodSpace); mCurrentLine.Cut(goodSpace, linelength-goodSpace); EndLine(PR_TRUE); mCurrentLine.Truncate(); diff --git a/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.h b/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.h index 767b3f5cb64..49c5e05560e 100644 --- a/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/mozilla/parser/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -54,6 +54,7 @@ class nsIUnicodeEncoder; +class nsILineBreaker; class nsIOutputStream; class nsIHTMLToTXTSinkStream : public nsIHTMLContentSink { @@ -195,6 +196,7 @@ protected: nsIUnicodeEncoder* mUnicodeEncoder; nsString mCharsetOverride; + nsILineBreaker* mLineBreaker; }; inline nsresult