Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/SpanShapeRenderer.java
41159 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pipe;2627import sun.java2d.SunGraphics2D;28import sun.java2d.SurfaceData;29import java.awt.Rectangle;30import java.awt.Shape;31import java.awt.BasicStroke;32import java.awt.geom.PathIterator;33import java.awt.geom.AffineTransform;34import java.awt.geom.Rectangle2D;35import sun.awt.SunHints;3637/**38* This class is used to convert raw geometry into a span iterator39* object using a simple flattening polygon scan converter.40* The iterator can be passed on to special SpanFiller loops to41* perform the actual rendering.42*/43public abstract class SpanShapeRenderer implements ShapeDrawPipe {4445public static class Composite extends SpanShapeRenderer {46CompositePipe comppipe;4748public Composite(CompositePipe pipe) {49comppipe = pipe;50}5152public Object startSequence(SunGraphics2D sg, Shape s,53Rectangle devR, int[] bbox) {54return comppipe.startSequence(sg, s, devR, bbox);55}5657public void renderBox(Object ctx, int x, int y, int w, int h) {58comppipe.renderPathTile(ctx, null, 0, w, x, y, w, h);59}6061public void endSequence(Object ctx) {62comppipe.endSequence(ctx);63}64}6566public static class Simple extends SpanShapeRenderer67implements LoopBasedPipe68{69public Object startSequence(SunGraphics2D sg, Shape s,70Rectangle devR, int[] bbox) {71return sg;72}7374public void renderBox(Object ctx, int x, int y, int w, int h) {75SunGraphics2D sg2d = (SunGraphics2D) ctx;76SurfaceData sd = sg2d.getSurfaceData();77sg2d.loops.fillRectLoop.FillRect(sg2d, sd, x, y, w, h);78}7980public void endSequence(Object ctx) {81}82}8384public void draw(SunGraphics2D sg, Shape s) {85if (sg.stroke instanceof BasicStroke) {86ShapeSpanIterator sr = LoopPipe.getStrokeSpans(sg, s);87try {88renderSpans(sg, sg.getCompClip(), s, sr);89} finally {90sr.dispose();91}92} else {93fill(sg, sg.stroke.createStrokedShape(s));94}95}9697public static final int NON_RECTILINEAR_TRANSFORM_MASK =98(AffineTransform.TYPE_GENERAL_TRANSFORM |99AffineTransform.TYPE_GENERAL_ROTATION);100101public void fill(SunGraphics2D sg, Shape s) {102if (s instanceof Rectangle2D &&103(sg.transform.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)104{105renderRect(sg, (Rectangle2D) s);106return;107}108109Region clipRegion = sg.getCompClip();110ShapeSpanIterator sr = LoopPipe.getFillSSI(sg);111try {112sr.setOutputArea(clipRegion);113sr.appendPath(s.getPathIterator(sg.transform));114renderSpans(sg, clipRegion, s, sr);115} finally {116sr.dispose();117}118}119120public abstract Object startSequence(SunGraphics2D sg, Shape s,121Rectangle devR, int[] bbox);122123public abstract void renderBox(Object ctx, int x, int y, int w, int h);124125public abstract void endSequence(Object ctx);126127public void renderRect(SunGraphics2D sg, Rectangle2D r) {128double[] corners = {129r.getX(), r.getY(), r.getWidth(), r.getHeight(),130};131corners[2] += corners[0];132corners[3] += corners[1];133if (corners[2] <= corners[0] || corners[3] <= corners[1]) {134return;135}136sg.transform.transform(corners, 0, corners, 0, 2);137if (corners[2] < corners[0]) {138double t = corners[2];139corners[2] = corners[0];140corners[0] = t;141}142if (corners[3] < corners[1]) {143double t = corners[3];144corners[3] = corners[1];145corners[1] = t;146}147int[] abox = {148(int) corners[0],149(int) corners[1],150(int) corners[2],151(int) corners[3],152};153Rectangle devR = new Rectangle(abox[0], abox[1],154abox[2] - abox[0],155abox[3] - abox[1]);156Region clipRegion = sg.getCompClip();157clipRegion.clipBoxToBounds(abox);158if (abox[0] >= abox[2] || abox[1] >= abox[3]) {159return;160}161Object context = startSequence(sg, r, devR, abox);162if (clipRegion.isRectangular()) {163renderBox(context, abox[0], abox[1],164abox[2] - abox[0],165abox[3] - abox[1]);166} else {167SpanIterator sr = clipRegion.getSpanIterator(abox);168while (sr.nextSpan(abox)) {169renderBox(context, abox[0], abox[1],170abox[2] - abox[0],171abox[3] - abox[1]);172}173}174endSequence(context);175}176177public void renderSpans(SunGraphics2D sg, Region clipRegion, Shape s,178ShapeSpanIterator sr)179{180Object context = null;181int[] abox = new int[4];182try {183sr.getPathBox(abox);184Rectangle devR = new Rectangle(abox[0], abox[1],185abox[2] - abox[0],186abox[3] - abox[1]);187clipRegion.clipBoxToBounds(abox);188if (abox[0] >= abox[2] || abox[1] >= abox[3]) {189return;190}191sr.intersectClipBox(abox[0], abox[1], abox[2], abox[3]);192context = startSequence(sg, s, devR, abox);193194spanClipLoop(context, sr, clipRegion, abox);195196} finally {197if (context != null) {198endSequence(context);199}200}201}202203public void spanClipLoop(Object ctx, SpanIterator sr,204Region r, int[] abox) {205if (!r.isRectangular()) {206sr = r.filter(sr);207}208while (sr.nextSpan(abox)) {209int x = abox[0];210int y = abox[1];211renderBox(ctx, x, y, abox[2] - x, abox[3] - y);212}213}214}215216217