summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsFillRectangle.java
blob: ca87c06c01468ac291fa206fcdeef27e988cb890 (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
/* 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.Rect;

import android.util.Log;

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

    /* TODO implement stippling.  */
    if (gc.fill_style == EmacsGC.GC_FILL_OPAQUE_STIPPLED)
      return;

    canvas = drawable.lockCanvas (gc);

    if (canvas == null)
      return;

    paint = gc.gcPaint;
    rect = new Rect (x, y, x + width, y + height);

    paint.setStyle (Paint.Style.FILL);

    if (gc.clip_mask == null)
      canvas.drawRect (rect, 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 (EmacsGC.srcInAlu);

	/* 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 (rect);
  }
}