Use binary search from nsTextFrame to find the length of the header/footer strings

Bug 139384  r=dcone sr=attinasi
This commit is contained in:
rods%netscape.com 2002-04-26 11:30:33 +00:00
parent 0816cae86b
commit 6db330b5a8
6 changed files with 142 additions and 30 deletions

View File

@ -50,6 +50,7 @@
#include "nsIPrintPreviewContext.h"
#include "nsIPrintContext.h"
#include "nsPageContentFrame.h"
#include "nsTextFrame.h" // for function BinarySearchForPosition
#include "nsIView.h" // view flags for clipping
#include "nsCSSRendering.h"
@ -518,24 +519,26 @@ nsPageFrame::DrawHeaderFooter(nsIRenderingContext& aRenderingContext,
if (aStr.Length() > 0 &&
((aHeaderFooter == eHeader && aHeight < mMargin.top) ||
(aHeaderFooter == eFooter && aHeight < mMargin.bottom))) {
// measure the width of the text
nsAutoString str;
ProcessSpecialCodes(aStr, str);
PRInt32 width;
aRenderingContext.GetWidth(str, width);
PRBool addEllipse = PR_FALSE;
PRInt32 indx;
PRInt32 textWidth = 0;
const PRUnichar* text = str.get();
// trim the text and add the elipses if it won't fit
while (width >= contentWidth && str.Length() > 1) {
str.SetLength(str.Length()-1);
aRenderingContext.GetWidth(str, width);
addEllipse = PR_TRUE;
PRInt32 len = (PRInt32)str.Length();
if (len == 0) {
return; // bail is empty string
}
if (addEllipse && str.Length() > 3) {
str.SetLength(str.Length()-3);
str.Append(NS_LITERAL_STRING("..."));
aRenderingContext.GetWidth(str, width);
// find how much text fits, the "position" is the size of the avilable area
if (BinarySearchForPosition(&aRenderingContext, text, 0, 0, 0, len,
PRInt32(contentWidth), indx, textWidth)) {
if (indx < len-1 && len > 3) {
str.SetLength(indx-3);
str.Append(NS_LITERAL_STRING("..."));
}
} else {
return; // bail if couldn't find the correct length
}
// cacl the x and y positions of the text

View File

@ -3306,6 +3306,8 @@ nsTextFrame::PaintAsciiText(nsIPresContext* aPresContext,
}
//---------------------------------------------------
// Also defined for external use in nsTextFrame.h
//
// Uses a binary search for find where the cursor falls in the line of text
// It also keeps track of the part of the string that has already been measured
// so it doesn't have to keep measuring the same text over and over
@ -3316,9 +3318,9 @@ nsTextFrame::PaintAsciiText(nsIPresContext* aPresContext,
//
// aTextWidth returns the (in twips) the length of the text that falls before the cursor
// aIndex contains the index of the text where the cursor falls
static PRBool
PRBool
BinarySearchForPosition(nsIRenderingContext* acx,
PRUnichar* aText,
const PRUnichar* aText,
PRInt32 aBaseWidth,
PRInt32 aBaseInx,
PRInt32 aStartInx,

View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsTextFrame_h___
#define nsTextFrame_h___
extern PRBool
BinarySearchForPosition(nsIRenderingContext* acx,
const PRUnichar* aText,
PRInt32 aBaseWidth,
PRInt32 aBaseInx,
PRInt32 aStartInx,
PRInt32 aEndInx,
PRInt32 aCursorPos,
PRInt32& aIndex,
PRInt32& aTextWidth);
#endif /* nsTextFrame_h___ */

View File

@ -50,6 +50,7 @@
#include "nsIPrintPreviewContext.h"
#include "nsIPrintContext.h"
#include "nsPageContentFrame.h"
#include "nsTextFrame.h" // for function BinarySearchForPosition
#include "nsIView.h" // view flags for clipping
#include "nsCSSRendering.h"
@ -518,24 +519,26 @@ nsPageFrame::DrawHeaderFooter(nsIRenderingContext& aRenderingContext,
if (aStr.Length() > 0 &&
((aHeaderFooter == eHeader && aHeight < mMargin.top) ||
(aHeaderFooter == eFooter && aHeight < mMargin.bottom))) {
// measure the width of the text
nsAutoString str;
ProcessSpecialCodes(aStr, str);
PRInt32 width;
aRenderingContext.GetWidth(str, width);
PRBool addEllipse = PR_FALSE;
PRInt32 indx;
PRInt32 textWidth = 0;
const PRUnichar* text = str.get();
// trim the text and add the elipses if it won't fit
while (width >= contentWidth && str.Length() > 1) {
str.SetLength(str.Length()-1);
aRenderingContext.GetWidth(str, width);
addEllipse = PR_TRUE;
PRInt32 len = (PRInt32)str.Length();
if (len == 0) {
return; // bail is empty string
}
if (addEllipse && str.Length() > 3) {
str.SetLength(str.Length()-3);
str.Append(NS_LITERAL_STRING("..."));
aRenderingContext.GetWidth(str, width);
// find how much text fits, the "position" is the size of the avilable area
if (BinarySearchForPosition(&aRenderingContext, text, 0, 0, 0, len,
PRInt32(contentWidth), indx, textWidth)) {
if (indx < len-1 && len > 3) {
str.SetLength(indx-3);
str.Append(NS_LITERAL_STRING("..."));
}
} else {
return; // bail if couldn't find the correct length
}
// cacl the x and y positions of the text

View File

@ -3306,6 +3306,8 @@ nsTextFrame::PaintAsciiText(nsIPresContext* aPresContext,
}
//---------------------------------------------------
// Also defined for external use in nsTextFrame.h
//
// Uses a binary search for find where the cursor falls in the line of text
// It also keeps track of the part of the string that has already been measured
// so it doesn't have to keep measuring the same text over and over
@ -3316,9 +3318,9 @@ nsTextFrame::PaintAsciiText(nsIPresContext* aPresContext,
//
// aTextWidth returns the (in twips) the length of the text that falls before the cursor
// aIndex contains the index of the text where the cursor falls
static PRBool
PRBool
BinarySearchForPosition(nsIRenderingContext* acx,
PRUnichar* aText,
const PRUnichar* aText,
PRInt32 aBaseWidth,
PRInt32 aBaseInx,
PRInt32 aStartInx,

View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsTextFrame_h___
#define nsTextFrame_h___
extern PRBool
BinarySearchForPosition(nsIRenderingContext* acx,
const PRUnichar* aText,
PRInt32 aBaseWidth,
PRInt32 aBaseInx,
PRInt32 aStartInx,
PRInt32 aEndInx,
PRInt32 aCursorPos,
PRInt32& aIndex,
PRInt32& aTextWidth);
#endif /* nsTextFrame_h___ */