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

Copyright (C) 2023-2026 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.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;

import android.util.Log;

public final class EmacsFillRectangle
{
  /* Color filter that inverts colors from the source.  */
  private static final ColorFilter invertFilter;

  static
  {
    invertFilter = new ColorMatrixColorFilter (new float[] {
	-1f, 0f, 0f, 0f, 255f,
	0f, -1f, 0f, 0f, 255f,
	0f, 0f, -1f, 0f, 255f,
	0f, 0f, 0f, 1f, 0f,
      });
  };

  public static void
  perform (EmacsDrawable drawable, EmacsGC gc,
	   int x, int y, int width, int height)
  {
    Paint paint;
    Rect rect;
    Canvas canvas;
    Bitmap invertBitmap;

    canvas = drawable.lockCanvas (gc);

    /* Clip masks are not respected or implemented when specified with
       this request.  */
    if (canvas == null || gc.clip_mask != null)
      return;

    rect = new Rect (x, y, x + width, y + height);

    if (gc.function != EmacsGC.GC_INVERT)
      {
	paint = gc.gcPaint;
	paint.setStyle (Paint.Style.FILL);

	if (gc.fill_style != EmacsGC.GC_FILL_OPAQUE_STIPPLED)
	  canvas.drawRect (rect, paint);
	else
	  gc.blitOpaqueStipple (canvas, rect);
      }
    else
      {
	paint = new Paint ();

	/* Simply invert the destination, which is only implemented for
	   this request.  As Android doesn't permit copying a bitmap to
	   itself, a copy of the source must be procured beforehand.  */
	invertBitmap = Bitmap.createBitmap (drawable.getBitmap (),
					    x, y, width, height);
        paint.setColorFilter (invertFilter);
	canvas.drawBitmap (invertBitmap, null, rect, paint);
        paint.setColorFilter (null);
	invertBitmap.recycle ();
      }

    drawable.damageRect (rect);
  }
};