summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsDrawRectangle.java
blob: ea0f1c281068a78ea3371b0553a2efce5ef17342 (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
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023-2024 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 android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;

import android.util.Log;

public final class EmacsDrawRectangle
{
  private static final Xfermode srcInAlu;

  static
  {
    srcInAlu = new PorterDuffXfermode (Mode.SRC_IN);
  };

  public static void
  perform (EmacsDrawable drawable, EmacsGC gc,
	   int x, int y, int width, int height)
  {
    Paint maskPaint, paint;
    Canvas maskCanvas;
    Bitmap maskBitmap;
    Rect maskRect, dstRect;
    Canvas canvas;
    Bitmap clipBitmap;

    /* TODO implement stippling for this request.  */
    if (gc.fill_style == EmacsGC.GC_FILL_OPAQUE_STIPPLED
	/* And GC_INVERT also.  */
	|| gc.fill_style == EmacsGC.GC_INVERT)
      return;

    canvas = drawable.lockCanvas (gc);

    if (canvas == null)
      return;

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

    /* This graphics request, in contrast to X, does not presently
       respect the GC's line style.  */

    if (gc.clip_mask == null)
      /* Use canvas.drawRect with a RectF.  That seems to reliably
	 get PostScript behavior.  */
      canvas.drawRect (new RectF (x + 0.5f, y + 0.5f,
				  x + width + 0.5f,
				  y + height + 0.5f),
		       paint);
    else
      {
	/* Drawing with a clip mask involves calculating the
	   intersection of the clip mask with the dst rect, and
	   extrapolating the corresponding part of the src rect.  */
	clipBitmap = gc.clip_mask.bitmap;
	dstRect = new Rect (x, y, x + width, y + height);
	maskRect = new Rect (gc.clip_x_origin,
			     gc.clip_y_origin,
			     (gc.clip_x_origin
			      + clipBitmap.getWidth ()),
			     (gc.clip_y_origin
			      + clipBitmap.getHeight ()));

	if (!maskRect.setIntersect (dstRect, maskRect))
	  /* There is no intersection between the clip mask and the
	     dest rect.  */
	  return;

	/* Finally, create a temporary bitmap that is the size of
	   maskRect.  */

	maskBitmap
	  = Bitmap.createBitmap (maskRect.width (), maskRect.height (),
				 Bitmap.Config.ARGB_8888);

	/* Draw the mask onto the maskBitmap.  */
	maskCanvas = new Canvas (maskBitmap);
	maskRect.offset (-gc.clip_x_origin,
			 -gc.clip_y_origin);
	maskCanvas.drawBitmap (gc.clip_mask.bitmap,
			       maskRect, new Rect (0, 0,
						   maskRect.width (),
						   maskRect.height ()),
			       paint);
	maskRect.offset (gc.clip_x_origin,
			 gc.clip_y_origin);

	/* Set the transfer mode to SRC_IN to preserve only the parts
	   of the source that overlap with the mask.  */
	maskPaint = new Paint ();
	maskPaint.setXfermode (srcInAlu);
	maskPaint.setStyle (Paint.Style.STROKE);

	/* Draw the source.  */
	maskCanvas.drawRect (maskRect, maskPaint);

	/* Finally, draw the mask bitmap to the destination.  */
	paint.setXfermode (null);
	canvas.drawBitmap (maskBitmap, null, maskRect, paint);

	/* Recycle this unused bitmap.  */
	maskBitmap.recycle ();
      }

    drawable.damageRect (x, y, x + width + 1, y + height + 1);
  }
}