File: gtext.c

package info (click to toggle)
yorick 2.2.04%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 10,012 kB
  • sloc: ansic: 86,757; sh: 1,697; cpp: 1,309; lisp: 1,234; makefile: 1,050; fortran: 19
file content (68 lines) | stat: -rw-r--r-- 1,844 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * $Id: gtext.c,v 1.2 2009-03-27 02:12:17 dhmunro Exp $
 * Define GIST text utilities
 */
/* Copyright (c) 2005, The Regents of the University of California.
 * All rights reserved.
 * This file is part of yorick (http://yorick.sourceforge.net).
 * Read the accompanying LICENSE file for details.
 */

#include "gtext.h"

extern long strcspn(const char *, const char *);

int gtDoEscapes= 1;

/* Return t->alignH, t->alignV, guaranteed not TH_NORMAL or TV_NORMAL */
void GtGetAlignment(const GpTextAttribs *t, int *alignH, int *alignV)
{
  *alignH= t->alignH;
  *alignV= t->alignV;
  if (*alignH==TH_NORMAL) *alignH= TH_LEFT;
  if (*alignV==TV_NORMAL) *alignV= TV_BASE;
}

/* Get shape of text input to GdText, given a function Width which can
   compute the width of a simple text string (no imbedded \n).  Returns
   largest value of Width for any line, and a line count.  */
int GtTextShape(const char *text, const GpTextAttribs *t,
                WidthFunction Width, GpReal *widest)
{
  int path= t->orient;
  GpReal wdest, thisWid;
  int nLines, nChars;

  /* Count lines in this text, find widest line */
  nLines= 0;
  wdest= 0.0;
  while ((text= GtNextLine(text, &nChars, path))) {
    nLines++;
    if (Width) thisWid= Width(text, nChars, t);
    else thisWid= (GpReal)nChars;
    if (thisWid>wdest) wdest= thisWid;
    text+= nChars;
  }

  *widest= wdest;
  return nLines;
}

/* Return the next line of text--
   returns text and a count of the characters in the
   line, nChars.  If text is '\0', or '\n', returns text+1 and
   a count of the number of characters to the next '\n' or '\0'.  */
const char *GtNextLine(const char *text, int *nChars, int path)
{
  char first= text? text[0] : '\0';

  if (!first) {
    *nChars= 0;
    return 0;
  }

  if (first=='\n') text+= 1;
  *nChars= strcspn(text, "\n");

  return text;
}