Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/PixelToParallelogramConverter.java
41159 views
/*1* Copyright (c) 2008, 2018, 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 java.awt.Shape;28import java.awt.BasicStroke;29import java.awt.geom.Line2D;30import java.awt.geom.Rectangle2D;31import java.awt.geom.AffineTransform;32import sun.java2d.SunGraphics2D;33import sun.awt.SunHints;3435/**36* This class converts calls to the basic pixel rendering methods37* into calls to the methods on a ParallelogramPipe.38* Most calls are transformed into calls to the fill(Shape) method39* by the parent PixelToShapeConverter class, but some calls are40* transformed into calls to fill/drawParallelogram().41*/42public class PixelToParallelogramConverter extends PixelToShapeConverter43implements ShapeDrawPipe44{45ParallelogramPipe outrenderer;46double minPenSize;47double normPosition;48double normRoundingBias;49boolean adjustfill;5051/**52* @param shapepipe pipeline to forward shape calls to53* @param pgrampipe pipeline to forward parallelogram calls to54* (and drawLine calls if possible)55* @param minPenSize minimum pen size for dropout control56* @param normPosition sub-pixel location to normalize endpoints57* for STROKE_NORMALIZE cases58* @param adjustfill boolean to control whethere normalization59* constants are also applied to fill operations60* (normally true for non-AA, false for AA)61*/62public PixelToParallelogramConverter(ShapeDrawPipe shapepipe,63ParallelogramPipe pgrampipe,64double minPenSize,65double normPosition,66boolean adjustfill)67{68super(shapepipe);69outrenderer = pgrampipe;70this.minPenSize = minPenSize;71this.normPosition = normPosition;72this.normRoundingBias = 0.5 - normPosition;73this.adjustfill = adjustfill;74}7576public void drawLine(SunGraphics2D sg2d,77int x1, int y1, int x2, int y2)78{79if (!drawGeneralLine(sg2d, x1, y1, x2, y2)) {80super.drawLine(sg2d, x1, y1, x2, y2);81}82}8384public void drawRect(SunGraphics2D sg2d,85int x, int y, int w, int h)86{87if (w >= 0 && h >= 0) {88if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {89BasicStroke bs = ((BasicStroke) sg2d.stroke);90if (w > 0 && h > 0) {91if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&92bs.getDashArray() == null)93{94double lw = bs.getLineWidth();95drawRectangle(sg2d, x, y, w, h, lw);96return;97}98} else {99// Note: This calls the integer version which100// will verify that the local drawLine optimizations101// work and call super.drawLine(), if not.102drawLine(sg2d, x, y, x+w, y+h);103return;104}105}106super.drawRect(sg2d, x, y, w, h);107}108}109110public void fillRect(SunGraphics2D sg2d,111int x, int y, int w, int h)112{113if (w > 0 && h > 0) {114fillRectangle(sg2d, x, y, w, h);115}116}117118public void draw(SunGraphics2D sg2d, Shape s) {119if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {120BasicStroke bs = ((BasicStroke) sg2d.stroke);121if (s instanceof Rectangle2D) {122if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&123bs.getDashArray() == null)124{125Rectangle2D r2d = (Rectangle2D) s;126double w = r2d.getWidth();127double h = r2d.getHeight();128double x = r2d.getX();129double y = r2d.getY();130if (w >= 0 && h >= 0) {131double lw = bs.getLineWidth();132drawRectangle(sg2d, x, y, w, h, lw);133}134return;135}136} else if (s instanceof Line2D) {137Line2D l2d = (Line2D) s;138if (drawGeneralLine(sg2d,139l2d.getX1(), l2d.getY1(),140l2d.getX2(), l2d.getY2()))141{142return;143}144}145}146147outpipe.draw(sg2d, s);148}149150public void fill(SunGraphics2D sg2d, Shape s) {151if (s instanceof Rectangle2D) {152Rectangle2D r2d = (Rectangle2D) s;153double w = r2d.getWidth();154double h = r2d.getHeight();155if (w > 0 && h > 0) {156double x = r2d.getX();157double y = r2d.getY();158fillRectangle(sg2d, x, y, w, h);159}160return;161}162163outpipe.fill(sg2d, s);164}165166static double len(double x, double y) {167return ((x == 0) ? Math.abs(y)168: ((y == 0) ? Math.abs(x)169: Math.sqrt(x * x + y * y)));170}171172double normalize(double v) {173return Math.floor(v + normRoundingBias) + normPosition;174}175176public boolean drawGeneralLine(SunGraphics2D sg2d,177double ux1, double uy1,178double ux2, double uy2)179{180if (sg2d.strokeState == SunGraphics2D.STROKE_CUSTOM ||181sg2d.strokeState == SunGraphics2D.STROKE_THINDASHED)182{183return false;184}185BasicStroke bs = (BasicStroke) sg2d.stroke;186int cap = bs.getEndCap();187if (cap == BasicStroke.CAP_ROUND || bs.getDashArray() != null) {188// TODO: we could construct the GeneralPath directly189// for CAP_ROUND and save a lot of processing in that case...190// And again, we would need to deal with dropout control...191return false;192}193double lw = bs.getLineWidth();194// Save the original dx, dy in case we need it to transform195// the linewidth as a perpendicular vector below196double dx = ux2 - ux1;197double dy = uy2 - uy1;198double x1, y1, x2, y2;199switch (sg2d.transformState) {200case SunGraphics2D.TRANSFORM_GENERIC:201case SunGraphics2D.TRANSFORM_TRANSLATESCALE:202{203double[] coords = {ux1, uy1, ux2, uy2};204sg2d.transform.transform(coords, 0, coords, 0, 2);205x1 = coords[0];206y1 = coords[1];207x2 = coords[2];208y2 = coords[3];209}210break;211case SunGraphics2D.TRANSFORM_ANY_TRANSLATE:212case SunGraphics2D.TRANSFORM_INT_TRANSLATE:213{214double tx = sg2d.transform.getTranslateX();215double ty = sg2d.transform.getTranslateY();216x1 = ux1 + tx;217y1 = uy1 + ty;218x2 = ux2 + tx;219y2 = uy2 + ty;220}221break;222case SunGraphics2D.TRANSFORM_ISIDENT:223x1 = ux1;224y1 = uy1;225x2 = ux2;226y2 = uy2;227break;228default:229throw new InternalError("unknown TRANSFORM state...");230}231if (sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE) {232if (sg2d.strokeState == SunGraphics2D.STROKE_THIN &&233outrenderer instanceof PixelDrawPipe)234{235// PixelDrawPipes will add sg2d.transXY so we need to factor236// that out...237int ix1 = (int) Math.floor(x1 - sg2d.transX);238int iy1 = (int) Math.floor(y1 - sg2d.transY);239int ix2 = (int) Math.floor(x2 - sg2d.transX);240int iy2 = (int) Math.floor(y2 - sg2d.transY);241((PixelDrawPipe)outrenderer).drawLine(sg2d, ix1, iy1, ix2, iy2);242return true;243}244x1 = normalize(x1);245y1 = normalize(y1);246x2 = normalize(x2);247y2 = normalize(y2);248}249if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {250// Transform the linewidth...251// calculate the scaling factor for a unit vector252// perpendicular to the original user space line.253double len = len(dx, dy);254if (len == 0) {255dx = len = 1;256// dy = 0; already257}258// delta transform the transposed (90 degree rotated) unit vector259double[] unitvector = {dy/len, -dx/len};260sg2d.transform.deltaTransform(unitvector, 0, unitvector, 0, 1);261lw *= len(unitvector[0], unitvector[1]);262}263lw = Math.max(lw, minPenSize);264dx = x2 - x1;265dy = y2 - y1;266double len = len(dx, dy);267double udx, udy;268if (len == 0) {269if (cap == BasicStroke.CAP_BUTT) {270return true;271}272udx = lw;273udy = 0;274} else {275udx = lw * dx / len;276udy = lw * dy / len;277}278double px = x1 + udy / 2.0;279double py = y1 - udx / 2.0;280if (cap == BasicStroke.CAP_SQUARE) {281px -= udx / 2.0;282py -= udy / 2.0;283dx += udx;284dy += udy;285}286outrenderer.fillParallelogram(sg2d, ux1, uy1, ux2, uy2,287px, py, -udy, udx, dx, dy);288return true;289}290291public void fillRectangle(SunGraphics2D sg2d,292double rx, double ry,293double rw, double rh)294{295double px, py;296double dx1, dy1, dx2, dy2;297AffineTransform txform = sg2d.transform;298dx1 = txform.getScaleX();299dy1 = txform.getShearY();300dx2 = txform.getShearX();301dy2 = txform.getScaleY();302px = rx * dx1 + ry * dx2 + txform.getTranslateX();303py = rx * dy1 + ry * dy2 + txform.getTranslateY();304dx1 *= rw;305dy1 *= rw;306dx2 *= rh;307dy2 *= rh;308if (adjustfill &&309sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&310sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)311{312double newx = normalize(px);313double newy = normalize(py);314dx1 = normalize(px + dx1) - newx;315dy1 = normalize(py + dy1) - newy;316dx2 = normalize(px + dx2) - newx;317dy2 = normalize(py + dy2) - newy;318px = newx;319py = newy;320}321outrenderer.fillParallelogram(sg2d, rx, ry, rx+rw, ry+rh,322px, py, dx1, dy1, dx2, dy2);323}324325public void drawRectangle(SunGraphics2D sg2d,326double rx, double ry,327double rw, double rh,328double lw)329{330double px, py;331double dx1, dy1, dx2, dy2;332double lw1, lw2;333AffineTransform txform = sg2d.transform;334dx1 = txform.getScaleX();335dy1 = txform.getShearY();336dx2 = txform.getShearX();337dy2 = txform.getScaleY();338px = rx * dx1 + ry * dx2 + txform.getTranslateX();339py = rx * dy1 + ry * dy2 + txform.getTranslateY();340// lw along dx1,dy1 scale by transformed length of dx2,dy2 vectors341// and vice versa342lw1 = len(dx1, dy1) * lw;343lw2 = len(dx2, dy2) * lw;344dx1 *= rw;345dy1 *= rw;346dx2 *= rh;347dy2 *= rh;348if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&349sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)350{351double newx = normalize(px);352double newy = normalize(py);353dx1 = normalize(px + dx1) - newx;354dy1 = normalize(py + dy1) - newy;355dx2 = normalize(px + dx2) - newx;356dy2 = normalize(py + dy2) - newy;357px = newx;358py = newy;359}360lw1 = Math.max(lw1, minPenSize);361lw2 = Math.max(lw2, minPenSize);362double len1 = len(dx1, dy1);363double len2 = len(dx2, dy2);364if (lw1 >= len1 || lw2 >= len2) {365// The line widths are large enough to consume the366// entire hole in the middle of the parallelogram367// so we can just fill the outer parallelogram.368fillOuterParallelogram(sg2d,369rx, ry, rx+rw, ry+rh,370px, py, dx1, dy1, dx2, dy2,371len1, len2, lw1, lw2);372} else {373outrenderer.drawParallelogram(sg2d,374rx, ry, rx+rw, ry+rh,375px, py, dx1, dy1, dx2, dy2,376lw1 / len1, lw2 / len2);377}378}379380/**381* This utility function handles the case where a drawRectangle382* operation discovered that the interior hole in the rectangle383* or parallelogram has been completely filled in by the stroke384* width. It calculates the outer parallelogram of the stroke385* and issues a single fillParallelogram request to fill it.386*/387public void fillOuterParallelogram(SunGraphics2D sg2d,388double ux1, double uy1,389double ux2, double uy2,390double px, double py,391double dx1, double dy1,392double dx2, double dy2,393double len1, double len2,394double lw1, double lw2)395{396double udx1 = dx1 / len1;397double udy1 = dy1 / len1;398double udx2 = dx2 / len2;399double udy2 = dy2 / len2;400if (len1 == 0) {401// len1 is 0, replace udxy1 with perpendicular of udxy2402if (len2 == 0) {403// both are 0, use a unit Y vector for udxy2404udx2 = 0;405udy2 = 1;406}407udx1 = udy2;408udy1 = -udx2;409} else if (len2 == 0) {410// len2 is 0, replace udxy2 with perpendicular of udxy1411udx2 = udy1;412udy2 = -udx1;413}414udx1 *= lw1;415udy1 *= lw1;416udx2 *= lw2;417udy2 *= lw2;418px -= (udx1 + udx2) / 2;419py -= (udy1 + udy2) / 2;420dx1 += udx1;421dy1 += udy1;422dx2 += udx2;423dy2 += udy2;424425outrenderer.fillParallelogram(sg2d, ux1, uy1, ux2, uy2,426px, py, dx1, dy1, dx2, dy2);427}428}429430431