FontManage 70.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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   : FontManage.c                           */
17 18 19
/*                                                 */
/* Purpose: A range of font management veneers.    */
/*                                                 */
20 21
/* Author : Merlyn Kline for Customer browser     */
/*          This source adapted by A.D.Hodgkinson  */
22 23
/*                                                 */
/* History: 05-Dec-96: Created.                    */
24 25 26
/*          14-Apr-97: Merged in a bug fix to      */
/*                     fm_get_string_width spotted */
/*                     by T.Cheal (see source for  */
27
/*                     more info).                 */
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/***************************************************/

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

#include "swis.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
#include "MiscDefs.h"
#include "Utils.h"

#include "Redraw.h"

#include "FontManage.h"

53 54
/* Conditional includes */

55
#ifdef UNIFONT
56 57
  #include "Unicode/iso10646.h"
  #include "Unicode/utf8.h"
58
  #include "Unifont.h"
59 60
#endif

61
#ifndef Font_WideFormat
62
  #define Font_WideFormat 0x400A9
63 64
#endif

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**************************************************************************************************/
/*                                                                                                */
/* Merlyn's original overview comments (corrected where necessary, due to changes in the source): */
/* ============================================================================================== */
/*                                                                                                */
/* RISC OS Font management                                                                        */
/*                                                                                                */
/* Merlyn Kline, 1995                                                                             */
/*                                                                                                */
/* This module provides a range of veneers for font management. It allows a typeface              */
/* to be defined including bold, italic and bold-italic attributes. These typefaces               */
/* are given names by which they are referred to in subsequent use. When defining a               */
/* typeface an alternative to be used can be specified for cases where the primary                */
/* selection is unavailable for some reason. If the alternative does not exist, *its*             */
/* alternative is checked. This goes on for several attempts (about 4) before giving              */
/* up after which the ordinary system font is used, scaled appropriately. This limit              */
/* on the number of attempts improves performance and means you don't have to worry too           */
/* much about defining loops (eg pairs of typefaces which specify each other as alternatives).    */
/*                                                                                                */
/* The system provides veneers for all font related functions and these use an internal           */
/* font handle. This font handle is either the RISC OS font handle when an appropriate            */
/* font was available or a negative number made up from the x and y font sizes.                   */
/*                                                                                                */
/* Note the use of the word 'typeface' to describe the four-font structure used by this           */
/* module. This is to avoid confusion with the RISC OS term 'font' underlying it.                 */
/*                                                                                                */
/* Typefaces are defined by passing strings to the font definition routine, which contain         */
/* a description of the desired font, like:                                                       */
/*                                                                                                */
/*    name=normal:italic:bold:bolditalic;alternative                                              */
/*                                                                                                */
/* where:                                                                                         */
/*                                                                                                */
/*    name         is the name you want to refer to this typeface by (case *sensitive* for speed) */
/*    normal       is the RISC OS font name to use in normal rendering                            */
/*    italic       is the RISC OS font name to use in italic rendering                            */
/*    bold         is the RISC OS font name to use in bold rendering                              */
/*    bolditalic   is the RISC OS font name to use in bold plus italic rendering                  */
/*    alternative  is the alternative typeface name to use if necessary                           */
/*                                                                                                */
/* Any of these fields may be missing except <name>. If the font names used for                   */
/* bold/italic rendering are missing, the normal one will be used instead. If                     */
/* the normal font name is missing, the alternative typface is used. If the                       */
/* alternative is missing, system font will be used when the requested fonts are                  */
/* not available.                                                                                 */
/*                                                                                                */
/* When defining typefaces for optimum performance define the one which will be used              */
/* most, last. This is because they are kept in a linked list and new ones are added to           */
/* the start of the list and so will be found more quickly.                                       */
/*                                                                                                */
/* If you define a typeface with the same name as an existing one the new definition              */
/* overwrites the old one.                                                                        */
/*                                                                                                */
/* Default font definitions are available as follows:                                             */
/*                                                                                                */
/*   system=System.Fixed:Corpus.Medium.Oblique:Corpus.Bold:Corpus.Bold.Oblique;fixed              */
/*   sans=Homerton.Medium:Homerton.Medium.Oblique:Homerton.Bold:Homerton.Bold.Oblique;serif       */
/*   serif=Trinity.Medium:Trinity.Medium.Italic:Trinity.Bold:Trinity.Bold.Italic;sans             */
/*   fixed=Corpus.Medium:Corpus.Medium.Oblique:Corpus.Bold:Corpus.Bold.Oblique                    */
/*                                                                                                */
/* Font sizes are defined in sixteenths of a point. The system font is treated as a font          */
/* with a 2:1 aspect ratio so set both xsize and ysize to the same to get normal looking text.    */
/* A size of 205 (=8*2*16*72/90 ie:                                                               */
/*                                                                                                */
/*    pixels * os_units_per_pixel * sixteenths_of_a _point_per_inch / os_units_per_inch )         */
/*                                                                                                */
/* will get system font at its normal size. This size should therefore be used for any display    */
/* of information where size is pretty arbitrary, ensuring a reasonable appearnce should system   */
/* font be used. To help, it is #defined as FM_Standard_Size in FontManage.h.                     */
/*                                                                                                */
135
/*                     ------------------------------------------------------                     */
136 137 138 139 140 141 142
/*                                                                                                */
/* (Merlyn then goes on to describe the font menu display and interpretation functions, which are */
/* more or less redundant in this source due to the Toolbox's automation).                        */
/*                                                                                                */
/* Note that some specialisation of the library for use in the browser has been necessary, so     */
/* for example references to browser_data structures would need to be removed were this version   */
/* of the library to be used elsewhere.                                                           */
143
/*                                                                                                */
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/**************************************************************************************************/

/* Statics */

static fm_typeface * fm_new_typeface  (void);
static fm_face       fm_sub_find_font (fm_typeface * f, int xsize, int ysize, int italic, int bold);

static fm_facelist * fm_list       = NULL;
static int           fm_systemfont = 0;
static int           fm_allowblend = -1;

/* Locals */

/* fm_faces allows typeface numbers to be defined internally */
/* which relate to typeface names held by the font managing  */
/* routines; these are easier to deal with than strings.     */

char * fm_faces[] = {
                      "serif",
                      "sans",
                      "fixed"
                    };

/* fm_handles records usage of font handles. For every font  */
/* font claim, an appropriate bit is examined in the         */
/* browser_data structure of the claimant (if available). If */
/* the bit is unset, it is set and the entry in fm_handles   */
/* incremented by one. For every font discard, the bit is    */
/* examined again and if set, it is cleared, and the entry   */
/* in fm_handles decremented by one. If that entry is now    */
/* zero, Font_LoseFont is called for the handle.             */

176
int fm_handles[Limits_OS_FontHandles];
177 178 179

/* fm_sizes allows quick lookup of a font size number, which */
/* otherwise would needed to be calculated on the fly (this  */
180 181
/* is slow). Initialisation is done in fm_init, and the size */
/* is looked up through fm_size.                             */
182 183 184 185 186
/*                                                           */
/* Whilst fm_find_token_font scales using this, fm_find_font */
/* doesn't. For speed, it's quicker if the caller can do the */
/* scaling, if needed, when falling fm_find_font.            */

187 188 189
int fm_sizes[Limits_FontSizes + 1]; /* (Indices 1 to Limits_FontSizes are used, with index 0 */
                                    /* filled with base size in case something goes wrong    */
                                    /* and a size of 0 is given)                             */
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

/*************************************************/
/* fm_find_typeface()                            */
/*                                               */
/* Goes through the linked list of fm_facelist   */
/* structures looking for one which is named,    */
/* returning the address of the fm_typeface      */
/* structure containing that name if it is found */
/* (or NULL if not). The name should be in lower */
/* case charaters.                               */
/*                                               */
/* Parameters: A pointer to a string holding the */
/*             name of the typeface to find, in  */
/*             lower case.                       */
/*                                               */
/* Returns:    Pointer to the fm_typeface struct */
/*             if found, else NULL.              */
/*************************************************/

209
fm_typeface * fm_find_typeface(char * name)
210 211 212 213 214 215 216 217 218 219 220 221 222
{
  fm_facelist * f;

  /* Set f to point to the start of the list */
  /* of fm_facelist structures, and go up    */
  /* the list looking for the named font.    */
  /* Return the address of the fm_typeface   */
  /* structure itself (rather than the       */
  /* fm_facelist structure that contains it) */
  /* if found, NULL if not.                  */

  f = fm_list;

223
  while (f)
224 225 226 227 228
  {
    if (!strcmp(f->face.name, name)) return &f->face;
    f = f->next;
  }

229
  return NULL;
230 231 232 233 234 235 236 237 238 239
}

/*************************************************/
/* fm_new_typeface()                             */
/*                                               */
/* Adds a new typeface to the list of typefaces. */
/* Returns a pointer to the fm_typeface struct   */
/* itself, when added, or NULL if the malloc     */
/* claim failed (so error check this externally) */
/*                                               */
240 241
/* Returns:    Pointer to the new fm_typeface    */
/*             struct, or NULL if malloc failed. */
242 243 244 245 246 247 248 249 250 251 252 253
/*************************************************/

static fm_typeface * fm_new_typeface(void)
{
  fm_facelist * f;

  /* Allocate memory for the fm_facelist struct */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("fm_new_typeface: malloc %d for 'fm_facelist' structure\n",sizeof(fm_facelist));
  #endif

254
  f = calloc(1, sizeof(fm_facelist));
255 256 257 258 259

  if (f)
  {
    #ifdef TRACE
      malloccount += sizeof(fm_facelist);
260
      if (tl & (1u<<13)) Printf("** malloccount (fm_new_typeface): \0211%d\0217\n",malloccount);
261 262
    #endif

263 264 265
    /* If the claim succeeded, link the new item into */
    /* the list, and return with the address of the   */
    /* fm_typeface structure.                         */
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

    f->next = fm_list;
    fm_list = f;

    return &f->face;
  }

  /* The malloc claim failed, so return NULL */

  return NULL;
}

/*************************************************/
/* fm_define_typeface_explicit()                 */
/*                                               */
/* Adds a new fm_facelist structure to the list  */
/* of fm_facelist structures, but also names     */
/* the new typeface and only allocates a new     */
/* list item if one of the same name wasn't      */
/* already present.                              */
/*                                               */
/* Parameters: A pointer to an fm_typeface       */
/*             structure, in which only the      */
/*             'name' field is filled in. This   */
/*             becomes the name for the new      */
/*             typeface and is used to check if  */
/*             a typeface of that name is        */
/*             already present.                  */
/*************************************************/

_kernel_oserror * fm_define_typeface_explicit(fm_typeface * f)
{
  fm_typeface * fp;

  /* Try to find a typeface of the same name in the list */

302
  fp = fm_find_typeface(f->name);
303 304 305 306 307 308 309

  /* If there isn't one, create a new typeface entry */

  if (!fp) fp = fm_new_typeface();

  /* Report an error if the new typeface entry couldn't be created */

310
  if (!fp)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  {
    erb.errnum = Utils_Error_Custom_Fatal;

    StrNCpy0(erb.errmess,
             lookup_token("FontMem:There is not enough free memory for font handling (%0).",
                          0,
                          "1"));

    return(&erb);
  }

  /* Copy whole structure over to fp */

  *fp = *f;

  return(NULL);
}

/*************************************************/
/* fm_define_typeface()                          */
/*                                               */
/* This function defines a whole new typeface in */
/* full, according to a description string the   */
/* format of which is described in Merlyn's      */
/* comments at the top of this file.             */
/*                                               */
/* Parameters: A pointer to a string containing  */
/*             the typeface description.         */
/*************************************************/

_kernel_oserror * fm_define_typeface(char * description)
{
  fm_typeface   f;
  char        * p = description;
  int           i = 0;

  /* Ensure that the fm_typeface struct has been */
  /* allocated locally, then zero the contents   */

  f.name[0] = 0;
  memset(&f,0,sizeof(f));

  /* Travel through the description string looking for */
  /* an equals sign - this separates the name of the   */
  /* typeface from the actual RISC OS fonts it uses.   */
  /* Copy the text - which is the typeface name - into */
  /* the 'name' field of the new typeface structure    */
  /* that was filled with zeros above.                 */

  while ((*p) && (*p != '='))
  {
    if (i < (sizeof(f.name) - 1)) f.name[i++] = *p++;
  }

  /* If we're not now at the end of the string, get the */
  /* four RISC OS font names from the string.           */

  if (*p)
  {
    int n;

    for (n = 0; n < 4; n++)
    {
      p++; /* Skip past the =, : or ; that we last ended on */
      i=0;

      /* Copy between the current position and the next separator */
      /* (i.e. the font name) into the relevant fontname field of */
      /* the fm_typeface structure.                               */

      while ((*p) && (*p != ':') && (*p != ';'))
      {
        if (i < sizeof(f.fontnames[n]) - 1) f.fontnames[n][i++] = *p++;
      }

      /* If we've reached the end of the string or a ';', exit now */
      /* as there are no more font names.                          */

      if ((!*p) || (*p == ';')) break;
    }

    /* If there's a semicolon, there is an alternative typeface */
    /* name defined                                             */

    if (*p == ';')
    {
      p++; /* Skip past the ; */

      /* Copy the alternative typeface name to the fm_typeface struct */

401
      strncpy(f.alternative, p, sizeof(f.alternative) - 1);
402 403 404 405 406 407 408 409
    }
  }

  /* If there's at least one font name present, */
  /* actually allocate the memory for the       */
  /* typeface (or copy over an old one with the */
  /* same name).                                */

410
  if (f.name[0]) return fm_define_typeface_explicit(&f);
411 412 413 414 415 416

  /* There are no font names - the typeface is badly defined */

  erb.errnum = Utils_Error_Custom_Fatal;

  StrNCpy0(erb.errmess,
417
           lookup_token("BadType:Internal error - Bad typeface definition in %0.",
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                        0,
                        "fm_define_typeface"));
  return &erb;
}

/*************************************************/
/* fm_define_default_typefaces()                 */
/*                                               */
/* Sets up the default typeface settings, for    */
/* system, fixed space, sans serif and serif     */
/* font 'banks'.                                 */
/*************************************************/

_kernel_oserror * fm_define_default_typefaces(void)
{
433 434
  /* These will be overridden if typefaces of the same name are read */
  /* in by load_choices later.                                       */
435

436 437 438
  RetError(fm_define_typeface("fixed=Corpus.Medium:Corpus.Medium.Oblique:Corpus.Bold:Corpus.Bold.Oblique"));
  RetError(fm_define_typeface("sans=Homerton.Medium:Homerton.Medium.Oblique:Homerton.Bold:Homerton.Bold.Oblique;serif"));
  RetError(fm_define_typeface("serif=Trinity.Medium:Trinity.Medium.Italic:Trinity.Bold:Trinity.Bold.Italic;sans"));
439

440
  return NULL;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

/*************************************************/
/* fm_claim_basic_typefaces()                    */
/*                                               */
/* Claim a few basic fonts which will probably   */
/* be needed for the duration of the application */
/* run time.                                     */
/*                                               */
/* Parameters: Base font size to claim at.       */
/*************************************************/

void fm_claim_basic_typefaces(int base_size)
{
  fm_face h;

457 458 459 460 461 462
  #define FMCIncHandles(f) {                                              \
                             if (f >= 0)                                  \
                             {                                            \
                               if (!fm_handles[(f)]) fm_handles[(f)] = 1; \
                               else fm_lose_font(NULL, (f));              \
                             }                                            \
463 464
                           }

465
  if (!choices.system_font)
466
  {
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    /* Need to be a bit careful here... Must not claim a font twice, if the */
    /* typeface is the same (e.g. System.Fixed for both fixed width normal  */
    /* and fixed width bold). The fm_handles array is an *internal* usage   */
    /* count - as far as the RISC OS Font Manager is concerned, for every   */
    /* font marked as used inside fm_handles, there's only one user in the  */
    /* browser. So if we claim something two or more times here, it'll only */
    /* get released once later on, thereby 'leaking' the font handle.       */

    h = fm_find_font(NULL, "sans",  base_size, base_size, 0, 0); FMCIncHandles(h)
    h = fm_find_font(NULL, "sans",  base_size, base_size, 0, 1); FMCIncHandles(h)
    h = fm_find_font(NULL, "sans",  base_size, base_size, 1, 0); FMCIncHandles(h)
    h = fm_find_font(NULL, "sans",  base_size, base_size, 1, 1); FMCIncHandles(h)

    h = fm_find_font(NULL, "serif", base_size, base_size, 0, 0); FMCIncHandles(h)
    h = fm_find_font(NULL, "serif", base_size, base_size, 1, 0); FMCIncHandles(h)
    h = fm_find_font(NULL, "serif", base_size, base_size, 0, 1); FMCIncHandles(h)
    h = fm_find_font(NULL, "serif", base_size, base_size, 1, 1); FMCIncHandles(h)

    h = fm_find_font(NULL, "fixed", base_size, base_size, 0, 0); FMCIncHandles(h)
    h = fm_find_font(NULL, "fixed", base_size, base_size, 0, 1); FMCIncHandles(h)
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  }
}

/*************************************************/
/* fm_sub_find_font()                            */
/*                                               */
/* When asked for a standard, italic, bold or    */
/* bold italic variant of a typeface, this will  */
/* return the font handle for the requested font */
/* or -1 if it can't be found. The routine       */
/* will try for just a standard or bold variant  */
/* if an italic or bold italic variant isn't     */
/* defined, then drop to just standard, before   */
/* giving up.                                    */
/*                                               */
/* Parameters: Pointer to the fm_typeface struct */
/*             for the typeface in question;     */
504 505
/*                                               */
/*             The X and Y sizes, in 1/16ths of  */
506
/*             a point, of the font to claim;    */
507
/*                                               */
508
/*             1 to specify italics, else 0;     */
509
/*                                               */
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
/*             1 to specify bold, else 0.        */
/*                                               */
/* Returns:    The RISC OS font manager's font   */
/*             handle for the requested font (as */
/*             an fm_face) or -1 if the font     */
/*             couldn't be found either by this  */
/*             routine, or by the font manager   */
/*             itself.                           */
/*************************************************/

static fm_face fm_sub_find_font(fm_typeface * f, int xsize, int ysize, int italic, int bold)
{
  int        n;
  fm_face    h;

  /* Set bit 0 of n if italic was specified, and bit 1 */
  /* if bold was specified.                            */

  n=(italic ? 1 : 0) | (bold ? 2 : 0);

  /* So n = 0 for standard, 1 for italic, 2 for bold */
  /* and 3 for bold italic.                          */

  /* If there is no fontname defined for that combination, */
  /* AND n with 2 - i.e., drop the italic specifier and    */
  /* just try plain / bold.                                */

  if (!f->fontnames[n][0]) n &= 2;

  /* If there is still no font name, try the standard only */

  if (!f->fontnames[n][0]) n = 0;

  /* If there is *still* no font name, we can't match this */
  /* so flag that by returning -1.                         */

  if (!f->fontnames[n][0]) return -1;

  /* If the font manager can't find the font, again flag the */
  /* problem by returning -1. Otherwise, return the font     */
  /* handle returned by the font manager.                    */

  if (_swix(Font_FindFont,
            _INR(1,5) | _OUT(0),

            f->fontnames[n],     /* Font name to find    */
            xsize,               /* x size in 16ths pt   */
            ysize,               /* y size in 16ths pt   */
            180 / wimpt_dx(),    /* Current mode's x dpi */
            180 / wimpt_dy(),    /* Current mode's y dpi */

            &h))                 /* Font handle put in h */

            return -1;

  return h;
}

/*************************************************/
/* fm_size()                                     */
/*                                               */
/* Given an abstracted font size (from 1 to 7,   */
/* with 3 being 'base font' size), returns the   */
/* actual size (in 16ths of a point) to use.     */
/*                                               */
/* Parameters: The font size, from 1 to 7.       */
/*                                               */
/* Returns:    The equivalent font size, in      */
/*             16ths of a point.                 */
/*                                               */
/* Assumes:    For speed, the parameter is not   */
/*             limit checked, so if it falls     */
/*             outside the allowed range the     */
/*             returned value will be undefined  */
/*             and probably very inappropriate   */
/*             for a font size!                  */
/*************************************************/

int fm_size(int size)
{
  return fm_sizes[size];
}

/*************************************************/
/* fm_find_font()                                */
/*                                               */
/* Returns the font handle of a requested        */
/* typeface variant, optionally recording the    */
/* usage in a given browser_data structure.      */
/*                                               */
/* Parameters: Pointer to the browser_data       */
/*             struct relevant to the font;      */
602
/*                                               */
603
/*             Pointer to the typeface name;     */
604
/*                                               */
605
/*             X and Y sizes in 16ths of a pt;   */
606
/*                                               */
607
/*             1 for italics, 0 for not italic;  */
608
/*                                               */
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
/*             1 for bold, 0 for not bold.       */
/*                                               */
/* Returns:    A RISC OS font handle for the     */
/*             requested font or the closest     */
/*             available match to it; this may   */
/*             be the system font if no good     */
/*             match could be found.             */
/*                                               */
/* Assumes:    The browser_data struct pointer   */
/*             may be NULL.                      */
/*************************************************/

fm_face fm_find_font(browser_data * b, char * name, int xsize, int ysize, int italic, int bold)
{
  fm_typeface * f;
  fm_face       s,h;
  int           i;

  /* Work out the system font 'handle' for the given point size */

  s = FM_System_Handle(xsize,ysize); /* (Macro defined in FontManage.h) */

  /* If system font has been configured locally through the */
632
  /* fm_set_system_font function, return that handle now    */
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

  if (fm_systemfont) return s;

  /* The slower alternative - try to find the named typeface */

  f = fm_find_typeface(name);

  /* If it can't be found, return the system font */

  if (!f) return s;

  /* Get the RISC OS font handle for that typeface */

  h = fm_sub_find_font(f, xsize, ysize, italic, bold);

648
  /* If fm_sub_find_font returns -1, keep trying   */
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
  /* alternative typefaces as long as there are    */
  /* some defined, to a maximum of 4 alternatives. */

  for (
        i = 0;
        i < 3 && h < 0 && f->alternative[0];
        i++
      )
  {
    f = fm_find_typeface(f->alternative);
    if (!f) return s;
    h = fm_sub_find_font(f, xsize, ysize, italic, bold);
  }

  /* If we still haven't got a RISC OS font handle */
  /* return the system font handle.                */

  if (h < 0) return s;

  /* Success, eventually! */

  /* If the font has already been claimed (it has an entry in fm_handles) */
  /* don't want to keep the second claim caused by the Font_FindFont that */
  /* gave the handle. So, lose the font - we know that the Font Manager   */
  /* will still have a usage of at least 1 for the font as long as        */
  /* fm_handles has a non-zero usage count itself. This is somewhat       */
  /* inefficient, but otherwise, for every claimed font handle, the face  */
  /* name, style, and size would have to be recorded with it, and for     */
  /* every font claim these preclaimed fonts would need to checked        */
  /* through. This would end up being quite slow.                         */

  if (b && fm_handles[h]) _swix(Font_LoseFont, _IN(0), h);

  /* Record the usage of this font */

  if (b && !(Bitfield_Is_Set(&b->handles, h)))
  {
    fm_handles[h]++;
    Bitfield_Set_Bit(&b->handles, h);
  }

  return h;
}

/*************************************************/
/* fm_lose_font()                                */
/*                                               */
/* Forgets about the usage of a given font by a  */
/* given browser, and if there are no users at   */
/* this point, tell the RISC OS Font Manager     */
/* that the font is no longer in use.            */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the font;             */
703
/*                                               */
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
/*             The RISC OS font handle for the   */
/*             font to forget.                   */
/*                                               */
/* Assumes:    The pointer to the browser_data   */
/*             struct *may* be NULL, in which    */
/*             case the RISC OS Font Manager is  */
/*             told the font isn't needed        */
/*             straight away.                    */
/*************************************************/

void fm_lose_font(browser_data * b, fm_face h)
{
  if (h >= 0)
  {
    if (b)
    {
      if (Bitfield_Is_Set(&b->handles, h))
      {
        fm_handles[h]--;

        Bitfield_Clear_Bit(&b->handles, h);
      }
726

727
      if (!fm_handles[h]) _swix(Font_LoseFont,_IN(0),h);
728
      else if (fm_handles[h] < 0) fm_handles[h] = 0;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    }
    else _swix(Font_LoseFont,_IN(0),h);
  }
}

/*************************************************/
/* fm_lose_fonts()                               */
/*                                               */
/* Forgets about the usage of all fonts used by  */
/* a given browser, and if there are no longer   */
/* any users of any of the fonts, tells the      */
/* RISC OS Font Manager that those fonts are no  */
/* longer in use.                                */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fonts.            */
/*************************************************/

void fm_lose_fonts(browser_data * b)
{
  int fh;

751
  for (fh = 0; fh < Limits_OS_FontHandles; fh++)
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
  {
    if (Bitfield_Is_Set(&b->handles, fh)) fm_lose_font(b, fh);
  }
}

/*************************************************/
/* fm_rescale_fonts()                            */
/*                                               */
/* For all currently claimed fonts, reclaim them */
/* with the current screen DPI. Useful for (say) */
/* mode changes, when the Desktop scaling may    */
/* have changed.                                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fonts to rescale. */
/*************************************************/

_kernel_oserror * fm_rescale_fonts(browser_data * b)
{
  _kernel_oserror * e;
  browser_data      temp;
  int               fh, size, xsize, ysize;
774
  char              name[Limits_FontName];
775 776 777 778 779 780 781 782 783 784 785 786 787

  /* Can't lose and reclaim fonts within the same browser, */
  /* whilst scanning through its list of claimed fonts...  */
  /* if ever a font handle higher than the one just lost   */
  /* was returned, things would go very wrong. So copy     */
  /* the list of used fonts into a temporary store and use */
  /* this for the loses and claims, then copy that back to */
  /* the actual browser_data structure when finished.      */

  temp.handles = b->handles;

  /* Now loop round all the font handles */

788
  for (fh = 0; fh < Limits_OS_FontHandles; fh++)
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
  {
    /* If this font is claimed... */

    if (Bitfield_Is_Set(&b->handles, fh))
    {
      /* How much space does the font name need? */

      e = _swix(Font_ReadDefn,
                _INR(0,1) | _IN(3) | _OUT(2),

                fh,              /* Handle */
                0,               /* Return buffer size needed for name */
                Magic_Word_FULL, /* See MiscDefs.h */

                &size);

      if (e) return e;

      /* If the font name will fit, then get the name and size details */

      if (size < sizeof(name)) /* Not '<=' as need terminator */
      {
        int h;

        /* Fill the name buffer with zeros first, to ensure the */
        /* name string is correctly terminated.                 */

        memset(name, 0, sizeof(name));

        /* Now read the font information */

        e = _swix(Font_ReadDefn,
                  _INR(0,1) | _IN(3) | _OUTR(2,3),

                  fh,
                  name,
                  Magic_Word_FULL, /* See MiscDefs.h */

                  &xsize,
                  &ysize);

        if (e) return e;

        /* Lose the old font, and reclaim the same name and size */
        /* at a new resolution.                                  */

        fm_lose_font(&temp, fh);

        e = _swix(Font_FindFont,
                  _INR(1,5) | _OUT(0),

                  name,             /* Font name to find    */
                  xsize,            /* x size in 16ths pt   */
                  ysize,            /* y size in 16ths pt   */
                  180 / wimpt_dx(), /* Current mode's x dpi */
                  180 / wimpt_dy(), /* Current mode's y dpi */

                  &h);              /* Font handle put in h */

        if (e) return e;

850 851
        /* See fm_find_font for full comments on this bit, no need */
        /* to repeat it all here.                                  */
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

        if (fm_handles[h]) _swix(Font_LoseFont, _IN(0), h);

        /* Record the usage of this font */

        fm_handles[h]++;
        Bitfield_Set_Bit(&temp.handles, h);

      /* Closure of long 'if' to see if the font name would fit */
      /* in the allocated buffer. The code above executes if it */
      /* would.                                                 */
      }

    /* Closure of long 'if' to see if the current font handle */
    /* was claimed; the code above executes if it was.        */
    }

  /* Closure of 'for' loop going found all font handles. */
  }

  /* Copy the font usage information back to the actual browser. */

  b->handles = temp.handles;

  return NULL;
}

/*************************************************/
880
/* fm_font_box()                                 */
881
/*                                               */
882 883
/* Gets a bounding box large enough to cover any */
/* character in a given font, in OS units.       */
884 885
/*                                               */
/* Parameters: RISC OS font handle of the font;  */
886
/*                                               */
887
/*             Pointer to a BBox into which the  */
888 889
/*             bounding box is placed (min X and */
/*             Y are inclusive, max X and Y are  */
890 891 892 893 894 895 896 897 898 899 900 901
/*             exclusive).                       */
/*************************************************/

_kernel_oserror * fm_font_box(fm_face h, BBox * box)
{
  if (h < 0)
  {
    /* If we're passed a system font handle, */
    /* treat that as a special case.         */

    int xs,ys;

902 903
    xs = FM_System_Size_X(h); /* (Macros defined in FontManage.h) */
    ys = FM_System_Size_Y(h);
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

    /* The code is derived from this:        */
    /*                                       */
    /* box->xmin=0;                          */
    /* box->ymin=ys * (10 * -3) / (64 * 16); */
    /*                                       */
    /* box->xmax=xs * 10 / 128;              */
    /* box->ymax=ys * (10 * 13) / (64 * 16); */

    box->xmin = 0;
    box->ymin = (ys * -30) / (1024);

    box->xmax = (xs * 10) / 128;
    box->ymax = (ys * 130) / (1024);
  }
  else
  {
    /* Otherwise, ask the Font Manager for the bounding box */

    // this probably needs to a char by char scan of the font and cache the result

    return _swix(Font_ReadInfo,
                 _IN(0) | _OUTR(1,4),

                 h,            /* Handle                    */

                 &box->xmin,   /* Minimum X of bounding box */
                 &box->ymin,   /* Minimum Y of bounding box */
                 &box->xmax,   /* Maximum X of bounding box */
                 &box->ymax);  /* Maximum Y of bounding box */
  }

  return NULL;
}

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
/*************************************************/
/* fm_char_box()                                 */
/*                                               */
/* Gets the bounding box of a char, in OS units. */
/*                                               */
/* Parameters: RISC OS font handle of the font;  */
/*                                               */
/*             Character to measure;             */
/*                                               */
/*             Pointer to a BBox into which the  */
/*             bounding box is placed (min X and */
/*             Y are inclusive, max X and Y are  */
/*             exclusive).                       */
/*************************************************/

_kernel_oserror * fm_char_box(fm_face h, char c, BBox * box)
{
  /* Very similar structure to fm_font_box, so see that for */
  /* more comments.                                         */

  if (h < 0)
  {
    int xs,ys;

    xs = FM_System_Size_X(h);
    ys = FM_System_Size_Y(h);

    box->xmin = 0;
    box->ymin = (ys * -30) / (1024);

    box->xmax = (xs * 10) / 128;
    box->ymax = (ys * 130) / (1024);
  }
  else
  {
974
    #ifdef UNIFONT
975
      _swix(Font_WideFormat, _INR(0,1), h, 12);
976
    #endif
977

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    return _swix(Font_CharBBox,
                 _INR(0,2) | _OUTR(1,4),

                 h,            /* Handle                    */
                 c,            /* Character                 */
                 FM_OSUnits,   /* Flags -> OS units, please */

                 &box->xmin,   /* Minimum X of bounding box */
                 &box->ymin,   /* Minimum Y of bounding box */
                 &box->xmax,   /* Maximum X of bounding box */
                 &box->ymax);  /* Maximum Y of bounding box */
  }

  return NULL;
}

994 995 996 997 998 999 1000
/*************************************************/
/* fm_set_font_colour()                          */
/*                                               */
/* Sets the font colour for subsequent painting. */
/*                                               */
/* Parameters: The RISC OS font handle of the    */
/*             font in question;                 */
1001
/*                                               */
1002
/*             The foreground colour;            */
1003
/*                                               */
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
/*             The background colour.            */
/*                                               */
/*             Colours are As palette entries,   */
/*             i.e. are in the form BBGGRRcc     */
/*             where cc = GCOL, else use the Red */
/*             Green and Blue bytes.             */
/*************************************************/

_kernel_oserror * fm_set_font_colour(fm_face h, int fore, int back)
{
  /* First, the system font case */

1016 1017 1018 1019
  if (h < 0)
  {
    #ifdef UNIFONT

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
      /* Need to set the background colour too */

      _swix(ColourTrans_SetGCOL,

                            _IN(0) | _INR(3,4),

                            fore,    /* Palette entry */
                            0x80,    /* Flags         */
                            0);      /* GCOL action   */

1030
    #endif
1031

1032
    return _swix(ColourTrans_SetGCOL,
1033 1034 1035 1036 1037
                          _IN(0) | _INR(3,4),

                          fore,    /* Palette entry */
                          0,       /* Flags         */
                          0);      /* GCOL action   */
1038
  }
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  else
  {
    /* Alternatively, deal with an outline font */

    return _swix(ColourTrans_SetFontColours,
                 _INR(0,3),

                 h,       /* Handle                            */
                 back,    /* Background colour palette entry   */
                 fore,    /* Foreground colour palette entry   */
                 14);     /* Maximum foreground colour offset  */
  }
}

/*************************************************/
/* fm_puts()                                     */
/*                                               */
/* Outputs a string in a given font at specified */
/* coordinates in a colour determined by calls   */
/* to fm_set_font_colour() prior to calling this */
/* function.                                     */
/*                                               */
/* Parameters: A RISC OS font handle;            */
1062
/*                                               */
1063 1064 1065 1066 1067
/*             X and Y coordinates of the bottom */
/*             left of the bounding box of the   */
/*             plotted string. These may be in   */
/*             OS units or millipoints (see      */
/*             below);                           */
1068
/*                                               */
1069
/*             Pointer to the string to output;  */
1070
/*                                               */
1071 1072
/*             1 if using OS coords, else 0;     */
/*                                               */
1073 1074 1075
/*             1 to use font blending, else 0 -  */
/*             do NOT use font blending on a     */
/*             Font Manager earlier than v3.37!  */
1076 1077
/*************************************************/

Kevin Bracey's avatar
Kevin Bracey committed
1078
_kernel_oserror * fm_puts(fm_face h, int x, int y, const char * s, int os, int blend)
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
{
  #ifdef TRACE
    if (tl & (1u<<10)) Printf("\nfm_puts: Called with handle %p\n         String '%s'\n",(void *) h, s);
  #endif

  if (h < 0)
  {
    /* System font output */

    int xs,ys;

    if (!os) convert_pair_to_os(x, y, &x, &y);

    /* Work out the pixel size of the font; note that the */
    /* Y size is twice the X size for aesthetic reasons   */

    xs = FM_System_Size_X(h) * 10 / (128 * wimpt_dx());
    ys = FM_System_Size_Y(h) * 10 / (64  * wimpt_dy());

    /* Set the size and spacing */

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
    bbc_vduq(23,
             17,
             7,
             6,
             xs & 255,
             xs >> 8,
             ys & 255,
             ys >> 8,
             0,
             0);
1110 1111 1112 1113 1114 1115 1116 1117

    /* Move to the requested (converted) coordinates. 10/64 */
    /* converts to OS units, 13/16 is height above base     */

    bbc_move(x, y + (FM_System_Size_Y(h) * (10*13) / (64*16)) - 1);

    /* Output the string */

1118 1119 1120 1121 1122
    #ifdef UNIFONT
      unifont_write0(s, x, y + (FM_System_Size_Y(h) * (10*13) / (64*16)) - 1, xs, ys);
    #else
      _swix(OS_Write0, _IN(0), s);
    #endif
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192

    /* Set the text output size back (certainly get interesting */
    /* effects from Make if you don't do this...!)              */

    bbc_vduq(23,
             17,
             7,
             6,
             16 / wimpt_dx(),
             0,
             32 / wimpt_dy(),
             0,
             0,
             0);

    return NULL;
  }
  else
  {
    unsigned int flags;

    /* Outline font output */

    if (fm_allowblend < 0)
    {
      /* If we haven't already, check the Font Manager */
      /* version - for 3.37 or later, allow background */
      /* blending (fm_allowblend will set bit 11 of    */
      /* the flags for Font_Paint), else don't allow   */
      /* blending (earlier versions of the Font        */
      /* Manager will object strongly to bit 11 being  */
      /* set).                                         */

      int version;

      if (_swix(Font_CacheAddr,_OUT(0),&version)) version = 0;

      fm_allowblend = (version >= 337) ? FM_Blend : 0;
    }

    /* Paint the string. The parameters are a pointer to the string, */
    /* flags, and x and y coordinates. The flags have the blending   */
    /* set if asked for, specify absolute coordaintes, and tell the  */
    /* Font Manager whether x and y are OS units or millipoints.     */

    #ifdef TRACE
      if (tl & (1u<<10))
      {
        unsigned char * scan = (unsigned char *) s;

        while (*scan)
        {
          if (*scan < 32 && *scan != 10 && *scan != 13) Printf("fm_puts: Warning, non-CR/LF control char %d in string\n",(unsigned int) *scan);
          scan ++;
        }

        Printf("fm_puts: Calling Font_Paint prior to exiting\n");
      }
    #endif

    if (printing) flags = 0;
    else          flags = blend ? fm_allowblend : 0;

    flags |= (
               (FM_GivenHandle) |
               (FM_Kern)        |

               (os ? FM_OSUnits : 0)
             );

1193
    #ifdef UNIFONT
1194
      _swix(Font_WideFormat, _INR(0,1), h, 12);
1195 1196
    #endif

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    return _swix(Font_Paint,
                 _INR(0,7),

                 h,
                 s,
                 flags,
                 x,
                 y,
                 NULL,NULL,0);
  }
}

/*************************************************/
/* fm_putsl()                                    */
/*                                               */
1212
/* As fm_puts above, but takes an extra          */
1213 1214 1215 1216 1217 1218 1219 1220
/* parameter after the string pointer which says */
/* how many characters of the string should be   */
/* plotted.                                      */
/*                                               */
/* This will *write a character into the string* */
/* during operation, so the string must be owned */
/* by the caller and not in ROM.                 */
/*                                               */
1221
/* Parameters: As fm_puts above, but after the   */
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
/*             pointer to the string to output,  */
/*             pass the number of characters in  */
/*             the string that will be plotted.  */
/*************************************************/

_kernel_oserror * fm_putsl(fm_face handle, int x, int y, char * s, int chars, int os, int blend)
{
  char              c;
  _kernel_oserror * e = NULL;

  /* If chars doesn't point past the end of the */
  /* string, then forcibly terminate it and     */
1234
  /* call fm_puts. Restore the character that   */
1235 1236 1237
  /* the terminator wrote over whther or not    */
  /* there is an error.                         */

1238
  if (s[chars] != '\0')
1239 1240
  {
    c        = s[chars];
1241
    s[chars] = '\0';
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

    e = fm_puts(handle,x,y,s,os,blend);

    s[chars] = c;
  }

  /* If the number of chars is longer than the */
  /* string, plot as much as there is of it.   */

  else e = fm_puts(handle,x,y,s,os,blend);

  return e;
}

/*************************************************/
/* fm_write_descriptor()                         */
/*                                               */
/* Writes the descriptor string for a typeface   */
/* into a given buffer.                          */
/*                                               */
/* Parameters: Pointer to the typeface name;     */
1263
/*                                               */
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
/*             Pointer to the buffer.            */
/*                                               */
/* Assumes:    That the buffer is big enough.    */
/*************************************************/

_kernel_oserror * fm_write_descriptor(char * name, char * buffer)
{
  fm_typeface * f;

  /* Try to find the typeface */

  f = fm_find_typeface(name);

  /* If the typeface isn't found, give an error */

  if (!f)
  {
    erb.errnum = Utils_Error_Custom_Fatal;

    StrNCpy0(erb.errmess,
1284
             lookup_token("BadTRef:Internal error - Reference to undefined typeface in %0.",
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
                          0,
                          "fm_write_descriptor"));
    return &erb;
  }

  /* Else fill in the buffer */

  sprintf(buffer,
          "%s=%s:%s:%s:%s;%s",
          f->name,
          f->fontnames[0],
          f->fontnames[1],
          f->fontnames[2],
          f->fontnames[3],
          f->alternative);

  return NULL;
}

/*************************************************/
/* fm_write_name()                               */
/*                                               */
/* Writes the RISC OS font name for a given      */
/* typeface variant to a given buffer            */
/*                                               */
1310 1311
/* Parameters: Typeface number from the fm_faces */
/*             array;                            */
1312
/*                                               */
1313
/*             Pointer to the buffer;            */
1314
/*                                               */
1315
/*             1 for italics, else 0;            */
1316
/*                                               */
1317 1318 1319 1320 1321
/*             1 for bold, else 0.               */
/*                                               */
/* Assumes:    That the buffer is big enough.    */
/*************************************************/

1322
_kernel_oserror * fm_write_name(int number, char * buffer, int italic, int bold)
1323 1324 1325
{
  fm_typeface * f;
  int           n;
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  char        * name;

  if (number < 0 || number >= sizeof(fm_faces))
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Illegal typeface number %d in fm_write_name.",
              number);

      return &erb;

    #endif

    return NULL;
  }
  else name = fm_faces[number];
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

  /* Index the correct font name using the bold and italic flags */

  n = (italic ? 1 : 0) | (bold ? 2 : 0);

  /* Try to find the typeface */

  f = fm_find_typeface(name);

  /* If the typeface isn't found, give an error */

  if (!f)
  {
    erb.errnum = Utils_Error_Custom_Fatal;

    StrNCpy0(erb.errmess,
1361
             lookup_token("BadTRef:Internal error - Reference to undefined typeface in %0.",
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
                          0,
                          "fm_write_name"));
    return &erb;
  }

  /* Else fill in the buffer */

  sprintf(buffer,"%s",f->fontnames[n]);

  return NULL;
}

/*************************************************/
/* fm_read_typeface()                            */
/*                                               */
/* Given a typeface name inside an fm_typeface   */
/* structure, the rest of the typeface details   */
/* are filled in. If the typeface name isn't     */
/* found an error will be raised.                */
/*                                               */
/* Parameters: A pointer to the fm_typeface with */
/*             the name to look up inside its    */
/*             'name' field.                     */
/*************************************************/

_kernel_oserror * fm_read_typeface(fm_typeface * face)
{
  fm_typeface * f;

  /* Try to find the named typeface */

  f = fm_find_typeface(face->name);

  /* Raise an error if it can't be found */

  if (!f)
  {
    erb.errnum = Utils_Error_Custom_Fatal;

    StrNCpy0(erb.errmess,
1402
             lookup_token("BadTRef:Internal error - Reference to undefined typeface in %0.",
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
                          0,
                          "fm_read_typeface"));
    return &erb;
  }

  /* Copy the found typeface contents across to the supplied typeface */

  *face = *f;

  return NULL;
}

/*************************************************/
/* fm_destroy_typefaces()                        */
/*                                               */
/* Destroys *all* typeface records, therefore    */
/* emptying the list of typefaces.               */
/*************************************************/

void fm_destroy_typefaces(void)
{
  fm_facelist * f, * c;

  /* Point f to the head of the list */

  f = fm_list;

  /* As long as f is non-null, there is a typeface. */
  /* Remember the typeface address and point f to   */
  /* the next item in the list; then free the       */
  /* typeface structure memory.                     */

  while(f)
  {
    c = f;
    f = f->next;

    #ifdef TRACE
      if (tl & (1u<<12)) Printf("fm_destroy_typefaces: free block %p, which held 'fm_facelist' structure\n",c);
      malloccount -= sizeof(fm_facelist);
1443
      if (tl & (1u<<13)) Printf("** malloccount (fm_destroy_typefaces): \0212%d\0217\n",malloccount);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
    #endif

    free(c);
  }

  /* Ensure that the list is flagged as empty. */

  fm_list=NULL;
}

/*************************************************/
/* fm_set_system_font()                          */
/*                                               */
/* Sets the static local flag to say that system */
/* fonts only should be used for font handling.  */
/* This will only affect any fonts claimed with  */
1460
/* e.g. fm_find_font after this function has     */
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
/* been called.                                  */
/*                                               */
/* Parameters: 1 to use system font, 0 to allow  */
/*             outline fonts.                    */
/*************************************************/

void fm_set_system_font(int systemfont)
{
  fm_systemfont = systemfont;
}

/*************************************************/
/* fm_system_font()                              */
/*                                               */
/* Returns 1 if the font library is currently    */
/* using system font for any new font finds, or  */
/* 0 if outline fonts are allowed. This is       */
/* intended for external callers only (it is     */
/* faster to use the fm_systemfont variable      */
/* directly in internal routines).               */
/*************************************************/

int fm_system_font(void)
{
  return fm_systemfont;
}

/*************************************************/
/* fm_init()                                     */
/*                                               */
/* Initialises the library for use.              */
/*                                               */
/* Parameters: 1 if the library is to only use   */
/*             the system font from the outset,  */
/*             or 0 to allow outline fonts;      */
1496
/*                                               */
1497
/*             The 'base font' size, if going    */
1498
/*             to use the fm_size function to    */
1499
/*             allow an abstracted font size     */
1500
/*             selection method (see fm_size).   */
1501 1502 1503 1504 1505 1506
/*************************************************/

void fm_init(int systemfont, int base_size)
{
  fm_destroy_typefaces();
  fm_set_system_font(systemfont);
1507 1508 1509 1510 1511 1512
  fm_init_sizes(base_size);
}

/*************************************************/
/* fm_init_sizes()                               */
/*                                               */
1513 1514 1515
/* Sets up the size array for FONT SIZE=xxx and  */
/* related attributes, based on the given base   */
/* font size.                                    */
1516
/*                                               */
1517
/* Parameters: The 'base font' size.             */
1518
/*************************************************/
1519

1520 1521
void fm_init_sizes(int base_size)
{
1522 1523
  int size, scaleby;

1524 1525
  /* Set up the size array */

1526
  fm_sizes[0] = base_size; /* Just in case 0 gets given to fm_size by accident - more likely than a large out of range value */
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539

  for (size = 1; size <= 7; size ++)
  {
    scaleby        = size - 3;
    fm_sizes[size] = (int) (base_size * pow(1.2, scaleby));
  }
}

/*************************************************/
/* fm_shutdown()                                 */
/*                                               */
/* Tells the RISC OS Font Manager that all fonts */
/* currently marked as used in fm_handles are    */
1540 1541 1542
/* not needed regardless of the internal usage   */
/* counter therein, and resets those counters to */
/* zero.                                         */
1543 1544 1545 1546 1547 1548
/*************************************************/

void fm_shutdown(void)
{
  int fh;

1549
  for (fh = 0; fh < Limits_OS_FontHandles; fh++)
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
  {
    if (fm_handles[fh])
    {
      _swix(Font_LoseFont, _IN(0), fh);
      fm_handles[fh] = 0;
    }
  }
}

/*************************************************/
/* fm_get_string_width()                         */
/*                                               */
/* Given a string, and the number of chars to    */
/* consider in that string, work out the width   */
/* of the string. A maximum width can be given,  */
/* and if it is possible to split the string     */
/* (pass the character to split on), the width   */
/* returned will reflect this consideration (but */
/* can still be greater than maxwid, if no split */
/* characters were found for far enough). The    */
/* number of bytes used to work out the final    */
/* returned width is itself returned, though any */
/* trailing spaces are included in this value    */
/* even if they aren't used to get the width.    */
/*                                               */
/* Parameters: The RISC OS font handle to use in */
/*             width calculations;               */
/*                                               */
/*             Pointer to the string to use;     */
/*                                               */
/*             Maximum width to allow if         */
1581
/*             splitting the line;               */
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
/*                                               */
/*             The maximum number of bytes of    */
/*             string to consider;               */
/*                                               */
/*             The character to split at (such   */
/*             as a space), or -1 for 'don't     */
/*             split';                           */
/*                                               */
/*             Pointer to an int, into which the */
/*             number of characters used to get  */
/*             the returned width is placed. Any */
/*             trailing spaces are skipped past  */
/*             and included in this returned     */
/*             value;                            */
/*                                               */
/*             Pointer to an int, into which the */
1598 1599
/*             calculated width (in millipoints) */
/*             is placed.                        */
1600
/*                                               */
1601
/* Assumes:    The two int pointers may be NULL. */
1602 1603
/*************************************************/

Kevin Bracey's avatar
Kevin Bracey committed
1604
_kernel_oserror * fm_get_string_width(fm_face h, const char * s, int maxwid, int maxbytes,
1605 1606
                                      int split, int * bytes, int * width)
{
1607 1608
  int retwidth = 0;
  int retbytes = 0;
1609

1610
  /* Now have to do some real work. */
1611

1612 1613 1614
  if (h >= 0)
  {
    /* The outline font case */
1615

1616 1617 1618
    _kernel_oserror * e = NULL;
    int               buffer[9];
    int               reached;
1619

1620 1621 1622
    /* For Font_ScanString, negative values of maxwid will  */
    /* produce a positive return as it copes with RTL text. */
    /* We don't want this, so skip the call if maxwid <= 0. */
1623

1624
    #ifdef UNIFONT
1625
      _swix(Font_WideFormat, _INR(0,1), h, 12);
1626 1627
    #endif

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
    if (maxwid > 0)
    {
      buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
      buffer[4] = split;

      e = _swix(Font_ScanString,
                _INR(0,5) | _IN(7) | _OUT(1) | _OUT(3),

                h,
                s,
                FM_GivenLength | FM_GivenBuffer | FM_Kern | FM_GivenHandle,
                maxwid,
                0x1000000,
                buffer,
                maxbytes,

                &reached,
                &retwidth);

      if (!e) retbytes = reached - ((int) s);

//      #ifdef TRACE
//        {
//          int wi, by;
//
//          e = _swix(Font_SetFont,_IN(0),h);
//
//          if (!e) e = _swix(Font_StringWidth,
//                            _INR(1,5) | _OUT(2) | _OUT(5),
//
//                            s,         /* String to find width of                   */
//                            maxwid,    /* Maximum width if splitting is allowed     */
//                            0x1000000, /* Maximum height if splitting is allowed    */
//                            split,     /* Character to split on, or -1 for no split */
//                            maxbytes,  /* Maximum string length                     */
//
//                            &wi,       /* Width of scanned string                   */
//                            &by);      /* Length of scanned string                  */
//
//          if (abs(wi - retwidth) > 2000 || by != retbytes)
//          {
//            Printf("fm_get_string_width: Width/Bytes mismatch in first call...\n");
//            Printf("                     Font_ScanString  -> %d, %d\n", retwidth, retbytes);
//            Printf("                     Font_StringWidth -> %d, %d\n", wi, by);
//          }
//        }
//      #endif
    }
1676 1677 1678 1679 1680 1681

    /* If there was no error, a split character was specified */
    /* but the width is zero, the Font Manager did not find a */
    /* split char before the maximum width given to it was    */
    /* exceeded. We want to be able to spill over in this     */
    /* case, so recall the Font_StringWidth in a non-         */
1682 1683 1684 1685 1686
    /* splitting mode, but only look as far as the first      */
    /* split char (if any) to allow the string to still split */
    /* up. This keeps the width as small as possible, even    */
    /* though individual words are wider than the space we    */
    /* had hoped to fit them into.                            */
1687

1688
    if (!e && !retwidth && split > 0)
1689
    {
Kevin Bracey's avatar
Kevin Bracey committed
1690
      const char * p = s;
1691

1692 1693 1694
      /* Scan only as far as the first split character.         */
      /*                                                        */
      /* Thanks to Tony Cheal for spotting this when doing      */
1695 1696 1697
      /* tables stuff - the original Customer browser sources  */
      /* recognised 'some problem' with Font_StringWidth but    */
      /* didn't then allow any sort of splitting in this part   */
1698
      /* of the code.                                           */
1699 1700 1701 1702 1703 1704

      while (*p && *(p++) != split);

      /* Now call the Font Manager without asking for a split, knowing that */
      /* the string length only runs up to the split character.             */

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
      _swix(Font_ScanString,
            _INR(0,4) | _IN(7) | _OUT(3),

            h,
            s,
            FM_GivenLength | FM_Kern | FM_GivenHandle,
            0x1000000,
            0x1000000,
            p - s,

            &retwidth);

      retbytes = p - s;

//      #ifdef TRACE
//        {
//          int wi, by;
//
//          _swix(Font_StringWidth,
//                _INR(1,5) | _OUT(2) | _OUT(5),
//
//                s,            /* String to find width of                   */
//                0x1000000,    /* Maximum width if splitting is allowed     */
//                0x1000000,    /* Maximum height if splitting is allowed    */
//                -1,           /* Character to split on, or -1 for no split */
//                p - s,        /* String length                             */
//
//                &wi,          /* Width of scanned string                   */
//                &by);         /* Length of scanned string                  */
//
//          if (abs(wi != retwidth) > 2000 || by != retbytes)
//          {
//            Printf("fm_get_string_width: Width/Bytes mismatch in second call...\n");
//            Printf("                     Font_ScanString  -> %d, %d\n", retwidth, retbytes);
//            Printf("                     Font_StringWidth -> %d, %d\n", wi, by);
//          }
//        }
//      #endif
1743 1744 1745 1746 1747 1748 1749 1750
    }

    /* Exit if there was some error */

    if (e) return e;

    /* Include any trailing spaces in the byte count */

1751
    while (retbytes < maxbytes && s[retbytes] == ' ') retbytes++;
1752 1753 1754 1755 1756 1757
  }
  else
  {
    /* The system font case. */

    int wCurr, wLastSplit, oLastSplit, oCurr;
1758

1759 1760 1761
    /* (Macro defined in FontManage.h). Important that we take */
    /* account of rounding correctly - compare with formula in */
    /* fm_puts().                                              */
1762

1763 1764
    int wChar = (FM_System_Size_X(h) * 10 / (128 * wimpt_dx())) * wimpt_dx() * 400;

1765 1766 1767
    wCurr = wLastSplit = oLastSplit = oCurr = 0;

    /* Whilst the byte counter is less than maxbytes and the width */
1768 1769
    /* calculated so far is less than maxwidth, or we haven't      */
    /* found a split character yet, go through adding              */
1770 1771 1772 1773 1774
    /* the width of a character to the width total for each char   */
    /* of the string. If a split character is met during this, set */
    /* oLastSplit and wLastSplit to the byte counter and thus far  */
    /* calculated width at that point.                             */

1775 1776 1777 1778 1779 1780 1781
    while (
            oCurr < maxbytes &&
            (
              (wCurr <= (unsigned) maxwid) ||
              (split > 0) && !oLastSplit
            )
          )
1782
    {
1783
      #ifdef UNIFONT
1784
        UCS4 ucs;
1785 1786
      #endif

1787 1788 1789 1790 1791 1792
      if ((split > 0) && (s[oCurr] == split))
      {
        oLastSplit = oCurr;
        wLastSplit = wCurr;
      }

1793
      #ifdef UNIFONT
1794
        oCurr += UTF8_to_UCS4(s + oCurr, &ucs);
1795
        wCurr += unifont_widechar(ucs) ? wChar * 2 : wChar;
1796
      #else
1797 1798
        wCurr += wChar;
        oCurr ++;
1799
      #endif
1800 1801 1802 1803 1804
    }

    /* If the current width is greater than the maximum width and there is */
    /* a split point, go back to that last split point.                    */

1805
    if ((wCurr > (unsigned) maxwid) && oLastSplit)
1806 1807 1808 1809 1810 1811 1812 1813 1814
    {
      oCurr = oLastSplit;
      wCurr = wLastSplit;
    }

    /* Include any trailing spaces in the byte count */

    while ((oCurr < maxbytes) && (s[oCurr] == ' ')) oCurr++;

1815 1816
    retbytes = oCurr;
    retwidth = wCurr;
1817 1818
  }

1819 1820 1821
  if (bytes) *bytes = retbytes;
  if (width) *width = retwidth;

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
  return NULL;
}

/*************************************************/
/* fm_token_font_info()                          */
/*                                               */
/* Looks at a token and returns information on   */
/* the best font to use with it, e.g. bold,      */
/* italic, a typeface number, etc.               */
/*                                               */
/* The returned typeface number is based on the  */
/* array of typeface names defined at the top of */
/* this file (called redraw_faces).              */
/*                                               */
/* Parameters: Pointer to the HStream struct     */
/*             (token) of interest;              */
1838
/*                                               */
1839 1840
/*             Address of an int into which the  */
/*             typeface number is placed;        */
1841
/*                                               */
1842 1843 1844
/*             Address of an int into which the  */
/*             font size to use is returned (in  */
/*             16ths of a point);                */
1845
/*                                               */
1846 1847 1848
/*             Address of an int into which 1 is */
/*             placed if italics should be used  */
/*             (else 0 is returned);             */
1849
/*                                               */
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
/*             Address of an int into which 1 is */
/*             placed if bold should be used     */
/*             (else 0 is returned).             */
/*************************************************/

void fm_token_font_info(HStream * t, int * facenum, int * size, int * italic, int * bold)
{
  int flags;

  flags = t->style;

  *facenum = 0; /* 0 = Serif */

  *bold    = *italic = 0;
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881

  /* May want to scale the size for certain tag types */

  if (ISSUP(t) || ISSUB(t)) *size = (choices.font_size * 3) / 5;
  else                      *size = choices.font_size;

  /* Scale the size according to the font size specifier */
  /* in the token                                        */

  #ifdef TRACE
    if (t->fontsize < 1 || t->fontsize > 7)
    {
      if (tl & (1u<<10)) Printf("fm_token_font_info: fontsize '%d' out of range\n",t->fontsize);
      t->fontsize = 3;
    }
  #endif

  *size += fm_size(t->fontsize) - choices.font_size;
1882 1883 1884 1885 1886

  /* Set font details according to specific tags */
  /* (or any header, with H_MASK). The macros    */
  /* defined in HTMLLib:tags.h.                  */

1887 1888 1889
  if (flags & (ITALIC | BLOCKQUOTE)) *italic  = 1;
  if (flags & (BOLD   | DT))         *bold    = 1;
  if (flags & (H_MASK | ADDRESS))    *facenum = 1; /* 1 = Sans serif */
1890

1891
  if (flags & (PRE    | TT))         *facenum = 2; /* 2 = Monospaced */
1892 1893 1894 1895

  /* Set bold, italic and size details according  */
  /* to the header level (if the token represents */
  /* header information, that is).                */
1896

1897 1898
  switch (redraw_header(flags))
  {
1899 1900 1901 1902 1903 1904
    case 1: *bold = 1, *size *= 2;              break;
    case 2: *bold = 1, *size = (*size * 8) / 5; break;
    case 3: *bold = 1, *size = (*size * 3) / 2; break;
    case 4: *bold = 1, *size = (*size * 4) / 3; break;
    case 5: *bold = 1, *size = (*size * 8) / 7; break;
    case 6: *bold = 1;                          break;
1905
  }
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
}

/*************************************************/
/* fm_find_token_font()                          */
/*                                               */
/* Returns an appropriate RISC OS font handle in */
/* which the contents of a given token should be */
/* rendered, optionally recording the usage in a */
/* given browser_data structure.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
1917 1918 1919
/*             relevant to the font (may be NULL */
/*             for a non-browser specific font   */
/*             claim);                           */
1920
/*                                               */
1921
/*             Pointer to the HStream struct     */
1922
/*             (token) to find the font for;     */
1923
/*                                               */
1924 1925
/*             1 to ignore bold and italic flags */
/*             and just find plain text, else 0. */
1926 1927
/*************************************************/

1928
fm_face fm_find_token_font(browser_data * b, HStream * t, int ignore_styles)
1929 1930 1931 1932 1933 1934 1935
{
  int facenum, size, bold, italic;

  /* Get the recommended font */

  fm_token_font_info(t, &facenum, &size, &italic, &bold);

1936 1937
  if (ignore_styles) facenum = italic = bold = 0;

1938 1939 1940 1941 1942 1943 1944
  /* Return the font handle - note that the typeface name is */
  /* passed to the font managing routines by looking up the  */
  /* typeface number in the redraw_faces array defined at    */
  /* the top of this file, and that 100% aspect ratio is     */
  /* always used due to 'size' being passed in for both the  */
  /* x and y sizes of the font.                              */

1945 1946
  if (facenum == 2) return fm_find_font(b, fm_faces[facenum], (size * choices.tt_aspect) / 100, size, italic, bold);
  else              return fm_find_font(b, fm_faces[facenum], size,                             size, italic, bold);
1947
}