-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHersheyFont.java
730 lines (617 loc) · 19.2 KB
/
HersheyFont.java
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
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
135
136
137
138
139
140
141
142
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
302
303
304
305
306
307
308
309
310
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*****************************************************************************/
//
// Copyright (c) James P. Buzbee 1996
// House Blend Software
//
// jbuzbee@nyx.net
// Version 1.1 Dec 11 1996
//
// Permission to use, copy, modify, and distribute this software
// for any use is hereby granted provided
// this notice is kept intact within the source file
//
// Very loosly based on code with authors listed as :
// Alan Richardson, Pete Holzmann, James Hurt
/*****************************************************************************/
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.URL;
/******************************************************************************/
public class HersheyFont
{
public final static int HORIZONTAL_CENTER = 0;
public final static int HORIZONTAL_LEFT = 1;
public final static int HORIZONTAL_RIGHT = 2;
public final static int HORIZONTAL_NORMAL = 1;
public final static int VERTICAL_TOP = 0;
public final static int VERTICAL_HALF = 1;
public final static int VERTICAL_CAP = 2;
public final static int VERTICAL_BOTTOM = 3;
public final static int VERTICAL_NORMAL = 3;
private final static int MAX_CHARACTERS = 256;
private final static int MAX_POINTS = 400;
protected final static int X = 0;
protected final static int Y = 1;
private float hersheyWidth = 1;
private float hersheyHeight = 1;
private int hersheyLineWidth = 1;
private int hersheyHorizontalAlignment = HORIZONTAL_NORMAL;
private int herhseyVerticalAlignment = VERTICAL_NORMAL;
private double hersheyTheta = 0;
private boolean hersheyItalics = false;
private float hersheyItalicSlant = 0.75f;
private String copyright = "Copyright (c) James P. Buzbee Mar 30, 1996";
protected String name;
protected char characterVectors[][][] =
new char[ MAX_CHARACTERS ][2][MAX_POINTS ];
protected int numberOfPoints[] = new int [MAX_CHARACTERS ];
protected int characterMinX[];
protected int characterMaxX[];
protected int characterSetMinY;
protected int characterSetMaxY;
protected int charactersInSet;
/*****************************************************************************/
private int getInt( InputStream file, int n ) throws IOException
{
char[] buf;
int c;
int j = 0;
buf = new char[n];
// for the specified number of characters
for (int i = 0; i < n; i++)
{
c = file.read();
// get character and discard spare newlines
while ( ( c == '\n' ) || ( c == '\r' ) )
{
c = file.read();
}
// if we hit end of file
if (c == -1 )
{
// return an error
return (c);
}
// if this is not a blank
if ( ( char ) c != ' ' )
{
// save the character
buf[j++] = ( char ) c;
}
}
// return the decimal equivilent of the string
return (Integer.parseInt( String.copyValueOf( buf,0,j ) ) );
}
/******************************************************************************/
HersheyFont( String fontName )
{
name = fontName;
try
{
// open the font file
InputStream fontStream = new FileInputStream( fontName );
// load the font file
LoadHersheyFont( fontName, fontStream );
// close the font file
fontStream.close();
}
catch ( FileNotFoundException e )
{
System.out.println(e);
}
catch ( IOException e )
{
System.out.println(e);
}
return;
}
/******************************************************************************/
HersheyFont( URL base, String fontName )
{
name = fontName;
try
{
// open the font file
InputStream fontStream= new URL(base, fontName ).openStream();
// load the font file
LoadHersheyFont( fontName, fontStream );
// close the font file
fontStream.close();
}
catch ( FileNotFoundException e )
{
System.out.println(e);
}
catch ( IOException e )
{
System.out.println(e);
}
/*
int k = ( int ) 'W' - ( int ) ' ';
replaceCharacter( 'i', numberOfPoints[k], characterVectors[k] );
if ( fontName.indexOf("scri") >= 0 )
{
writeHersheyFont();
}
*/
return;
}
/******************************************************************************/
private void LoadHersheyFont(String fontname, InputStream fontStream )
{
int character,
n;
int c;
int xadjust = fontAdjustment( fontname );
try
{
// loop through the characters in the file ...
character = 0;
// while we have not processed all of the characters
while ( true )
{
// if we cannot read the next field
if ( getInt(fontStream, 5) < 1)
{
// we are done, set the font specification for num chars
charactersInSet = character;
// break the read loop
break;
}
else
{
// get the number of vertices in this character
n = getInt(fontStream, 3);
// save it
numberOfPoints[character] = n;
// read in the vertice coordinates ...
for (int i = 0; i < n; i++)
{
// if we are at the end of the line
if ((i == 32) || (i == 68) || (i == 104) || (i == 140))
{
// skip the carriage return
fontStream.read();
}
// get the next character
c = fontStream.read();
// if this is a return ( we have a DOS style file )
if ( c == '\n' )
{
// throw it away and get another
c = fontStream.read();
}
// get the x coordinate
characterVectors[character][X][i] = ( char ) c;
// read the y coordinate
characterVectors[character][Y][i] = ( char ) fontStream.read();
}
// skip the carriage return
fontStream.read();
// increment the character counter
character++;
}
}
// determine the size of each character ...
characterMinX = new int[ charactersInSet ];
characterMaxX = new int[ charactersInSet ];
// initialize ...
characterSetMinY = 1000;
characterSetMaxY = -1000;
// loop through each character ( except the space character )
for (int j = 1; j < charactersInSet; j++)
{
// calculate the size
calculateCharacterSize( j, xadjust );
}
// handle the space character - if the 'a' character is defined
if ( ( ( int ) 'a' - ( int ) ' ' ) <= charactersInSet )
{
// make the space character the same size as the 'a'
characterMinX[0] = characterMinX[ ( int ) 'a' - ( int ) ' ' ];
characterMaxX[0] = characterMaxX[ ( int ) 'a' - ( int ) ' ' ];
}
else
{
// make the space char the same size as the last char
characterMinX[0] = characterMinX[ charactersInSet -1 ];
characterMaxX[0] = characterMaxX[ charactersInSet -1 ];
}
}
catch ( IOException e )
{
System.out.println(e);
}
return;
}
/*************************************************************************************/
protected void calculateCharacterSize( int j, int xadj )
{
characterMinX[j] = 1000;
characterMaxX[j] = -1000;
// for all the vertices in the character
for (int i = 1; i < numberOfPoints[j]; i++)
{
// if this is not a "skip"
if (characterVectors[j][X][i] != ' ')
{
// if this is less than our current minimum
if (characterVectors[j][X][i] < characterMinX[j] )
{
// save it
characterMinX[j] = characterVectors[j][X][i];
}
// if this is greater than our current maximum
if ( characterVectors[j][X][i] > characterMaxX[j] )
{
// save it
characterMaxX[j] = characterVectors[j][X][i];
}
// if this is less than our current minimum
if (characterVectors[j][Y][i] < characterSetMinY )
{
// save it
characterSetMinY = characterVectors[j][Y][i];
}
// if this is greater than our current maximum
if (characterVectors[j][Y][i] > characterSetMaxY )
{
// save it
characterSetMaxY = characterVectors[j][Y][i];
}
}
}
characterMinX[j] -= xadj;
characterMaxX[j] += xadj;
}
/******************************************************************************/
public void drawString( String text, int x, int y, Graphics g )
{
drawText( text,
x,
y,
hersheyWidth,
hersheyHeight,
hersheyHorizontalAlignment,
herhseyVerticalAlignment,
hersheyTheta,
true,
g
);
return;
}
/******************************************************************************/
public void setRotation( double theta )
{
hersheyTheta = theta;
return;
}
/******************************************************************************/
public void setWidth( float width )
{
hersheyWidth = width;
return;
}
/******************************************************************************/
public void setHeight( float height )
{
hersheyHeight = height;
return;
}
/******************************************************************************/
public void setVerticalAlignment( int alignment )
{
herhseyVerticalAlignment = alignment;
return;
}
/******************************************************************************/
public void setHorizontalAlignment( int alignment )
{
hersheyHorizontalAlignment = alignment;
return;
}
/******************************************************************************/
public void setItalics ( boolean flag )
{
hersheyItalics = flag;
return;
}
/******************************************************************************/
public void setItalicsSlant ( float slant )
{
hersheyItalicSlant = slant;
return;
}
/******************************************************************************/
public void setLineWidth ( int width )
{
hersheyLineWidth = width;
return;
}
/******************************************************************************/
public String getName ( )
{
return( name );
}
/******************************************************************************/
protected int
drawText(String text,
int xc,
int yc,
float width,
float height,
int Horizontal_Alignment,
int Vertical_Alignment,
double theta,
boolean Draw,
Graphics g
)
{
int i;
int character;
int len;
int rotpx = 0,
rotpy = 0;
int xp,
yp;
boolean rotate = false;
float scale;
float cosTheta = 0,
sinTheta = 0;
float verticalOffsetFactor = 0;
// set the flag to true if the angle is not 0.0
rotate = ( theta != 0.0 ) ? true : false ;
// if we are to do a rotation
if ( rotate )
{
// set up the rotation variables
theta = -Math.PI / 180.0 * theta;
cosTheta = ( float ) Math.cos(theta);
sinTheta = ( float ) Math.sin(theta);
// set the position to do all rotations about
rotpx = xc;
rotpy = yc;
}
// starting position
xp = xc;
yp = yc;
switch ( Vertical_Alignment )
{
case VERTICAL_TOP :
verticalOffsetFactor = 0;
break;
case VERTICAL_HALF :
verticalOffsetFactor = 0.5f;
break;
case VERTICAL_NORMAL : // also VERTICAL_BOTTOM
verticalOffsetFactor = 1;
break;
case VERTICAL_CAP :
verticalOffsetFactor = 0.25f;
break;
}
// move the y position based on the vertical alignment
yp = yp - (int) ( verticalOffsetFactor *
( height * (float) (characterSetMaxY - characterSetMinY)));
// if we have a non-standard horizontal alignment
if ( (Horizontal_Alignment != HORIZONTAL_LEFT ) &&
(Horizontal_Alignment != HORIZONTAL_NORMAL )
)
{
// find the length of the string in pixels ...
len = 0;
for (int j = 0; j < text.length(); j++)
{
// the character's number in the array ...
character = ( int ) text.charAt( j ) - ( int ) ' ';
len += (characterMaxX[character] - characterMinX[character]) * width;
}
// if we are center aligned
if (Horizontal_Alignment == HORIZONTAL_CENTER )
{
// move the starting point half to the left
xp -= len / 2;
}
else
{
// alignment is right, move the start all the way to the left
xp -= len;
}
}
// if we are not going to actually draw the string
if ( ! Draw )
{
// set up to initialize the bounding rectangle
}
// loop through each character in the string ...
for (int j = 0; j < text.length(); j++)
{
// the character's number in the array ...
character = ( int ) text.charAt( j ) - ( int ) ' ';
// render this character
drawCharacter(
xp,
yp,
rotpx,
rotpy,
width,
height,
rotate,
sinTheta,
cosTheta,
Draw,
characterVectors[character],
numberOfPoints[character],
characterMinX[character],
characterSetMinY ,
hersheyItalics,
hersheyItalicSlant,
g
);
// advance the starting coordinate
xp += (int) ((float) (characterMaxX[character] -
characterMinX[character]
) * width
);
} // end for each character
return(0);
}
/******************************************************************************/
protected void drawFontLine( int x1,
int y1,
int x2,
int y2,
int width,
Graphics g
)
{
// if the width is greater than one
if ( width > 1 )
{
Polygon filledPolygon = new Polygon();
int offset = width/2;
// this does not generate a true "wide line" but it seems to
// look OK for font lines
filledPolygon.addPoint( x1 - offset, y1 + offset );
filledPolygon.addPoint( x1 + offset, y1 - offset );
filledPolygon.addPoint( x2 + offset, y2 - offset );
filledPolygon.addPoint( x2 - offset, y2 + offset );
// draw a polygon
g.fillPolygon(filledPolygon);
}
else
{
// draw a line
g.drawLine( x1,y1, x2,y2 );
}
return;
}
/******************************************************************************/
protected int fontAdjustment( String fontname )
{
int xadjust = 0;
// if we do not have a script type font
if ( fontname.indexOf("scri") < 0 )
{
// if we have a gothic font
if ( fontname.indexOf("goth") >= 0 )
{
xadjust = 2;
}
else
{
xadjust = 3;
}
}
return( xadjust );
}
/******************************************************************************/
protected void drawCharacter(
int xp,
int yp,
int rotpx,
int rotpy,
float width,
float height,
boolean rotate,
float sinTheta,
float cosTheta,
boolean Draw,
char Vectors[][],
int numberOfPoints,
int minX,
int characterSetMinY,
boolean Italics,
float slant,
Graphics g
)
{
float xd,
yd,
xd2,
yd2;
int oldx = 0,
oldy = 0,
x,
y,
i;
boolean skip = true;
int thisCharacterMinX;
int thisCharacterMaxX;
float finalSlant = height * ( -slant );
//System.out.print("" + Vectors[X][0] + Vectors[Y][0] );
// loop through each vertex in the character
for (i = 1; i < numberOfPoints; i++)
{
//System.out.print("" + Vectors[X][i] + Vectors[Y][i] );
// if this is a "skip"
if ( Vectors[X][i] == ( int ) ' ')
{
// set the skip flag
skip = true ;
}
else
{
// calculate italics offset if necessary
x = ( int ) ( ( Italics )
? ( ( Vectors[Y][i] -
characterSetMinY
) * finalSlant
)
: 0
) +
// add italics offset to the "normal" point transformation
// xp + (int) ((float) (Vectors[X][i] -
// minX ) * width );
transformX( xp, Vectors[X][i], minX, width );
// calculate the y coordinate
//y = yp + (int) ((float) (Vectors[Y][i] -
// characterSetMinY ) * height);
y = transformY( yp, Vectors[Y][i], characterSetMinY, height );
// if we are doing a rotation
if ( rotate )
{
// apply the rotation matrix ...
// transform the coordinate to the rotation center point
xd = ( float ) (x - rotpx);
yd = ( float ) (y - rotpy);
// rotate
xd2 = xd * cosTheta - yd * sinTheta;
yd2 = xd * sinTheta + yd * cosTheta;
// transform back
x = (int) (xd2 + 0.5) + rotpx;
y = (int) (yd2 + 0.5) + rotpy;
}
if ( ! skip )
{
// if we are to draw the string
if ( Draw )
{
drawFontLine( oldx, oldy, x,y, hersheyLineWidth, g );
}
else
{
// we just want the bounding box of the string
// TBD
}
} // end if not skip
skip = false;
oldx = x;
oldy = y;
} // end if skip
} // end for each vertex in the character
//System.out.println("");
}
/******************************************************************************/
protected int transformX( int xoffset, int px, int minx, float mag )
{
return( ( int ) ( xoffset + ( px - minx ) * mag ) );
}
/******************************************************************************/
protected int transformY( int yoffset, int py, int miny, float mag )
{
return( ( int ) ( yoffset + ( py - miny ) * mag ) );
}
}
/******************************************************************************/