/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : Reformat.h                             */
/*                                                 */
/* Purpose: Functions to handle page reformatting. */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 03-Dec-96: Created.                    */
/*          16-Apr-97: First merge with T.Cheal's  */
/*                     table code...               */
/*          22-May-97: Amazingly, *still* trying   */
/*                     to get this to work.        */
/*          18-Jun-97: Hpmh; works, but very slow. */
/*                     Will need to rewrite at     */
/*                     some stage; for the moment, */
/*                     moved a few bits over to    */
/*                     Tables.c as they fitted in  */
/*                     better over there.          */
/***************************************************/

#include "HTMLLib.h" /* From the HTML library; the definitions below rely on this */

/* Line break position definitions */

#define LINEBREAK    (P | BR | LI)      /* Tags that force linebreaks at the  */
                                        /* point where they are encountered   */

#define LINEBREAKSW  (PRE | BLOCKQUOTE) /* Tags that force linebreaks when    */
                                        /* they are encountered in the source */
                                        /* whilst the effect is turned on     */

#define LINEBREAKON  (DT | ADDRESS)     /* Tags that force a line break at    */
                                        /* the point when they turn on        */

#define LINEBREAKOFF (H_MASK | ADDRESS) /* Tags that force a line break at    */
                                        /* the point when they turn off       */

/* Other tag classification masks */

#define VISUALDATA   (HR | IMG | INPUT) /* Does the tag contain displayable data, despite */
                                        /* having no text inside it? If the tag's style   */
                                        /* word contains the bit(s) defined here, it is   */
                                        /* deemed to contain such data.                   */

/* Formatting definitions */

#define Reformat_AsWideAsPossible_OS           0x8000000  /* OS unit 'very very wide' value */
#define Reformat_AsWideAsPossible_MP           0x40000000 /* Millipoint 'very wide' value   */
#define Reformat_UnrealisticallyHighLineNumber 0x40000000 /* For the deferred reformatter */

/* Reformatter flags */

#define Reformatter_KeepGoingToEnd  (1u<<1) /* Tell reformatter to keep going until it runs out of data, regardless of how long it takes */
#define Reformatter_ReturnMinimum   (1u<<2) /* Tell reformatter to return the minimum width that the stream could have fitted in         */
#define Reformatter_FindingWidest   (1u<<3) /* Tell reformatter we're trying to find the greatest width the page could occupy            */
#define Reformatter_FindingSmallest (1u<<4) /* Tell reformatter we're trying to find the smallest width the page could occupy            */
#define Reformatter_FromStreamStart (1u<<5) /* Tell reformatter to ignore last_token fields in browser_data and use streambase instead   */
#define Reformatter_Virtual         (1u<<6) /* Tell reformatter not to actually allocate lines/chunks (for finding widths of things)     */

/* The CENTRED macro returns 1 when passed an HStream pointer if that HStream */
/* has the Style flag saying it is centred, or if the general alignment field */
/* says that the tag should be centred; else it returns 0.                    */

#define CENTRED(x)   (((x->style & CENTER) || ((x->type & TYPE_ALIGN_MASK) == TYPE_CENTRE)) != 0)

/* Local structures */

typedef struct reformat_width_data
{
  browser_data  * b;
  reformat_cell * d;
  HStream       * tp;     /* Pointer to the token this structure represents           */
  char          * data;   /* Pointer to the token's data                              */
  int             offset; /* The offset into the data to start at                     */
  int             maxwid; /* The width to try and fit the token into                  */
                         /*                                                          */
  int             bytes;  /* (Returned) The number of bytes that will (might) fit     */
  int             width;  /* (Returned) The actual width of the given number of bytes */

} reformat_width_data;

/* Function prototypes */

int                   reformat_formatting           (browser_data * b);
_kernel_oserror     * reformat_stop                 (browser_data * b);

int                   reformat_newline              (HStream * current, HStream * last, int offset);

_kernel_oserror     * reformat_shift_vertically     (browser_data * b, int start, int end, int y_shift);
_kernel_oserror     * reformat_format_from          (browser_data * b, int lastline, int immediate, int image);
int                   reformat_format_timer         (int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle);
void                  reformat_stop_pending         (browser_data * b);

_kernel_oserror     * reformat_get_image_size       (browser_data * b, HStream * tp, BBox * box);
_kernel_oserror     * reformat_get_object_size      (browser_data * b, HStream * tp, BBox * box);
void                  reformat_get_placeholder_size (browser_data * b, HStream * tp, const char * text, BBox * box);
int                   reformat_bullet_width         (int bullet);
int                   reformat_bullet_height        (int bullet);
int                   reformat_y_offset             (browser_data * b);

_kernel_oserror     * reformat_check_extent         (browser_data * b);
int                   reformat_return_extent        (browser_data * b, reformat_cell * cell);
_kernel_oserror     * reformat_set_extent           (browser_data * b, int y_extent);

void                  reformat_reformatter          (browser_data * b);
int                   reformat_format_cell          (int toplevel, browser_data * b, HStream * streambase, table_stream * table, reformat_cell * cellarray, int ColWidth, int Row, int Column);
void                  reformat_find_cell_limits     (int toplevel, browser_data * b, HStream * streambase, table_stream * table, reformat_cell * cellarray, int Row, int Column, int * retmin, int * retmax);

void                  reformat_change_text          (browser_data * b, HStream * tp);