summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsSdk7FontDriver.java
blob: 33d6ee34fa4e40e2adc1c56d683b6a8245b7844e (plain)
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
/* Font backend for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

package org.gnu.emacs;

import java.io.File;

import java.util.LinkedList;
import java.util.List;

import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Canvas;

import android.util.Log;



/* EmacsSdk7FontDriver implements a fallback font driver under
   Android.  This font driver is enabled when the SFNT font driver (in
   sfntfont-android.c) proves incapable of locating any fonts, which
   has hitherto not been observed in practice.

   This font driver does not supply each font installed on the system,
   in lieu of which it provides a list of fonts for each conceivable
   style and sub-type of the system's own Typefaces, which arises from
   Android's absence of suitable APIs for loading individual font
   files.  */

public class EmacsSdk7FontDriver extends EmacsFontDriver
{
  private static final String TOFU_STRING = "\uDB3F\uDFFD";
  private static final String EM_STRING   = "m";
  private static final String TAG	  = "EmacsSdk7FontDriver";

  protected static final class Sdk7Typeface
  {
    /* The typeface and paint.  */
    public Typeface typeface;
    public Paint typefacePaint;
    public String familyName;
    public int slant, width, weight, spacing;

    public
    Sdk7Typeface (String familyName, Typeface typeface)
    {
      String style, testString;
      int index, measured, i;
      float[] widths;

      /* Initialize the font style fields and create a paint object
	 linked with that typeface.  */

      slant = NORMAL;
      weight = REGULAR;
      width = UNSPECIFIED;
      spacing = PROPORTIONAL;

      this.typeface = typeface;
      this.familyName = familyName;

      typefacePaint = new Paint ();
      typefacePaint.setAntiAlias (true);
      typefacePaint.setTypeface (typeface);
    }

    @Override
    public String
    toString ()
    {
      return ("Sdk7Typeface ("
	      + String.valueOf (familyName) + ", "
	      + String.valueOf (slant) + ", "
	      + String.valueOf (width) + ", "
	      + String.valueOf (weight) + ", "
	      + String.valueOf (spacing) + ")");
    }
  };

  protected static final class Sdk7FontEntity extends FontEntity
  {
    /* The typeface.  */
    public Sdk7Typeface typeface;

    @SuppressWarnings ("deprecation")
    public
    Sdk7FontEntity (Sdk7Typeface typeface)
    {
      foundry = "Google";
      family = typeface.familyName;
      adstyle = null;
      weight = typeface.weight;
      slant = typeface.slant;
      spacing = typeface.spacing;
      width = typeface.width;
      dpi = Math.round (EmacsService.SERVICE.metrics.scaledDensity * 160f);

      this.typeface = typeface;
    }
  };

  protected final class Sdk7FontObject extends FontObject
  {
    /* The typeface.  */
    public Sdk7Typeface typeface;

    @SuppressWarnings ("deprecation")
    public
    Sdk7FontObject (Sdk7Typeface typeface, int pixelSize)
    {
      float totalWidth;
      String testWidth, testString;

      this.typeface = typeface;
      this.pixelSize = pixelSize;

      family = typeface.familyName;
      adstyle = null;
      weight = typeface.weight;
      slant = typeface.slant;
      spacing = typeface.spacing;
      width = typeface.width;
      dpi = Math.round (EmacsService.SERVICE.metrics.scaledDensity * 160f);

      /* Compute the ascent and descent.  */
      typeface.typefacePaint.setTextSize (pixelSize);
      ascent
	= Math.round (-typeface.typefacePaint.ascent ());
      descent
	= Math.round (typeface.typefacePaint.descent ());

      /* Compute the average width.  */
      testString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      totalWidth = typeface.typefacePaint.measureText (testString);

      if (totalWidth > 0)
	avgwidth = Math.round (totalWidth
			       / testString.length ());

      /* Android doesn't expose the font average width and height
	 information, so this will have to do.  */
      minWidth = maxWidth = avgwidth;

      /* This is different from avgwidth in the font spec! */
      averageWidth = avgwidth;

      /* Set the space width.  */
      totalWidth = typeface.typefacePaint.measureText (" ");
      spaceWidth = Math.round (totalWidth);

      /* Set the height and default ascent.  */
      height = ascent + descent;
      defaultAscent = ascent;
    }
  };

  private String[] fontFamilyList;
  private Sdk7Typeface[] typefaceList;
  private Sdk7Typeface fallbackTypeface;

  public
  EmacsSdk7FontDriver ()
  {
    int i;
    Typeface typeface;

    typefaceList = new Sdk7Typeface[5];

    /* Initialize the default monospace and Sans Serif typefaces.
       Initialize the same typeface with various distinct styles.  */
    fallbackTypeface = new Sdk7Typeface ("Sans Serif",
					 Typeface.DEFAULT);
    typefaceList[1] = fallbackTypeface;

    fallbackTypeface = new Sdk7Typeface ("Sans Serif",
					 Typeface.create (Typeface.DEFAULT,
							  Typeface.BOLD));
    fallbackTypeface.weight = BOLD;
    typefaceList[2] = fallbackTypeface;

    fallbackTypeface = new Sdk7Typeface ("Sans Serif",
					 Typeface.create (Typeface.DEFAULT,
							  Typeface.ITALIC));
    fallbackTypeface.slant = ITALIC;
    typefaceList[3] = fallbackTypeface;

    fallbackTypeface
      = new Sdk7Typeface ("Sans Serif",
			  Typeface.create (Typeface.DEFAULT,
					   Typeface.BOLD_ITALIC));
    fallbackTypeface.weight = BOLD;
    fallbackTypeface.slant = ITALIC;
    typefaceList[4] = fallbackTypeface;

    fallbackTypeface = new Sdk7Typeface ("Monospace",
					 Typeface.MONOSPACE);
    fallbackTypeface.spacing = MONO;
    typefaceList[0] = fallbackTypeface;

    fontFamilyList = new String[] { "Monospace", "Sans Serif", };
  }

  private boolean
  checkMatch (Sdk7Typeface typeface, FontSpec fontSpec)
  {
    if (fontSpec.family != null
	&& !fontSpec.family.equals (typeface.familyName))
      return false;

    if (fontSpec.slant != null
	&& !fontSpec.weight.equals (typeface.weight))
      return false;

    if (fontSpec.spacing != null
	&& !fontSpec.spacing.equals (typeface.spacing))
      return false;

    if (fontSpec.weight != null
	&& !fontSpec.weight.equals (typeface.weight))
      return false;

    if (fontSpec.width != null
	&& !fontSpec.width.equals (typeface.width))
      return false;

    return true;
  }

  @Override
  public FontEntity[]
  list (FontSpec fontSpec)
  {
    LinkedList<FontEntity> list;
    int i;

    list = new LinkedList<FontEntity> ();

    for (i = 0; i < typefaceList.length; ++i)
      {
	if (checkMatch (typefaceList[i], fontSpec))
	  list.add (new Sdk7FontEntity (typefaceList[i]));
      }

    return list.toArray (new FontEntity[0]);
  }

  @Override
  public FontEntity
  match (FontSpec fontSpec)
  {
    FontEntity[] entities;
    int i;

    entities = this.list (fontSpec);

    if (entities.length == 0)
      return new Sdk7FontEntity (fallbackTypeface);

    return entities[0];
  }

  @Override
  public String[]
  listFamilies ()
  {
    return fontFamilyList;
  }

  @Override
  public FontObject
  openFont (FontEntity fontEntity, int pixelSize)
  {
    return new Sdk7FontObject (((Sdk7FontEntity) fontEntity).typeface,
			       pixelSize);
  }

  @Override
  public int
  hasChar (FontSpec font, int charCode)
  {
    float missingGlyphWidth, width;
    Rect rect1, rect2;
    Paint paint;
    Sdk7FontObject fontObject;

    /* Ignore characters outside the BMP.  */

    if (charCode > 65535)
      return 0;

    if (font instanceof Sdk7FontObject)
      {
	fontObject = (Sdk7FontObject) font;
	paint = fontObject.typeface.typefacePaint;
      }
    else
      paint = ((Sdk7FontEntity) font).typeface.typefacePaint;

    paint.setTextSize (10);

    if (Character.isWhitespace ((char) charCode))
      return 1;

    missingGlyphWidth = paint.measureText (TOFU_STRING);
    width = paint.measureText ("" + charCode);

    if (width == 0f)
      return 0;

    if (width != missingGlyphWidth)
      return 1;

    rect1 = new Rect ();
    rect2 = new Rect ();

    paint.getTextBounds (TOFU_STRING, 0, TOFU_STRING.length (),
			 rect1);
    paint.getTextBounds ("" + (char) charCode, 0, 1, rect2);
    return rect1.equals (rect2) ? 0 : 1;
  }

  private void
  textExtents1 (Sdk7FontObject font, int code, FontMetrics metrics,
		Paint paint, Rect bounds)
  {
    char[] text;

    text = new char[1];
    text[0] = (char) code;

    paint.getTextBounds (text, 0, 1, bounds);

    /* bounds is the bounding box of the glyph corresponding to CODE.
       Translate these into XCharStruct values.

       The origin is at 0, 0, and lbearing is the distance counting
       rightwards from the origin to the left most pixel in the glyph
       raster.  rbearing is the distance between the origin and the
       rightmost pixel in the glyph raster.  ascent is the distance
       counting upwards between the the topmost pixel in the glyph
       raster.  descent is the distance (once again counting
       downwards) between the origin and the bottommost pixel in the
       glyph raster.

       width is the distance between the origin and the origin of any
       character to the right.  */

    metrics.lbearing = (short) bounds.left;
    metrics.rbearing = (short) bounds.right;
    metrics.ascent = (short) -bounds.top;
    metrics.descent = (short) bounds.bottom;
    metrics.width = (short) paint.measureText ("" + text[0]);
  }

  @Override
  public void
  textExtents (FontObject font, int code[], FontMetrics fontMetrics)
  {
    int i;
    Paint paintCache;
    Rect boundsCache;
    Sdk7FontObject fontObject;
    char[] text;
    float width;

    fontObject = (Sdk7FontObject) font;
    paintCache = fontObject.typeface.typefacePaint;
    paintCache.setTextSize (fontObject.pixelSize);
    boundsCache = new Rect ();

    if (code.length == 0)
      {
	fontMetrics.lbearing = 0;
	fontMetrics.rbearing = 0;
	fontMetrics.ascent = 0;
	fontMetrics.descent = 0;
	fontMetrics.width = 0;
      }
    else if (code.length == 1)
      textExtents1 ((Sdk7FontObject) font, code[0], fontMetrics,
		    paintCache, boundsCache);
    else
      {
	text = new char[code.length];

	for (i = 0; i < code.length; ++i)
	  text[i] = (char) code[i];

	paintCache.getTextBounds (text, 0, code.length,
				  boundsCache);
	width = paintCache.measureText (text, 0, code.length);

	fontMetrics.lbearing = (short) boundsCache.left;
	fontMetrics.rbearing = (short) boundsCache.right;
	fontMetrics.ascent = (short) -boundsCache.top;
	fontMetrics.descent = (short) boundsCache.bottom;
	fontMetrics.width = (short) Math.round (width);
      }
  }

  @Override
  public int
  encodeChar (FontObject fontObject, int charCode)
  {
    if (charCode > 65535)
      return FONT_INVALID_CODE;

    return charCode;
  }

  @Override
  public int
  draw (FontObject fontObject, EmacsGC gc, EmacsDrawable drawable,
	int[] chars, int x, int y, int backgroundWidth,
	boolean withBackground)
  {
    Rect backgroundRect, bounds;
    Sdk7FontObject sdk7FontObject;
    int i;
    Canvas canvas;
    Paint paint;
    char[] array;

    sdk7FontObject = (Sdk7FontObject) fontObject;

    backgroundRect = new Rect ();
    backgroundRect.top = y - sdk7FontObject.ascent;
    backgroundRect.left = x;
    backgroundRect.right = x + backgroundWidth;
    backgroundRect.bottom = y + sdk7FontObject.descent;

    canvas = drawable.lockCanvas (gc);

    if (canvas == null)
      return 0;

    paint = gc.gcPaint;
    paint.setStyle (Paint.Style.FILL);

    if (withBackground)
      {
	paint.setColor (gc.background | 0xff000000);
	canvas.drawRect (backgroundRect, paint);
	paint.setColor (gc.foreground | 0xff000000);
      }

    paint.setTextSize (sdk7FontObject.pixelSize);
    paint.setTypeface (sdk7FontObject.typeface.typeface);
    paint.setAntiAlias (true);

    /* Android applies kerning to non-monospaced fonts by default,
       which brings the dimensions of strings drawn via `drawText' out
       of agreement with measurements previously provided to redisplay
       by textExtents.  To avert such disaster, draw each character
       individually, advancing the origin point by hand.  */

    bounds = new Rect ();
    array = new char[1];

    for (i = 0; i < chars.length; ++i)
      {
	/* Retrieve the text bounds for this character so as to
	   compute the damage rectangle.  */
	array[0] = (char) chars[i];
	paint.getTextBounds (array, 0, 1, bounds);
	bounds.offset (x, y);
	backgroundRect.union (bounds);

	/* Draw this character.  */
	canvas.drawText (array, 0, 1, x, y, paint);

	/* Advance the origin point by that much.  */
	x += paint.measureText ("" + array[0]);
      }

    drawable.damageRect (backgroundRect);
    paint.setAntiAlias (false);
    return 1;
  }
};