|
| 1 | +package com.aspose.imaging.examples.ModifyingImages; |
| 2 | + |
| 3 | +import com.aspose.imaging.*; |
| 4 | +import com.aspose.imaging.brushes.SolidBrush; |
| 5 | +import com.aspose.imaging.examples.Logger; |
| 6 | +import com.aspose.imaging.examples.Path; |
| 7 | +import com.aspose.imaging.examples.Utils; |
| 8 | +import com.aspose.imaging.imageoptions.PngOptions; |
| 9 | +import com.aspose.imaging.sources.FileCreateSource; |
| 10 | + |
| 11 | +public class PixelPerfectTextAlignment |
| 12 | +{ |
| 13 | + public static void main(String[] args) |
| 14 | + { |
| 15 | + Logger.startExample(); |
| 16 | + String baseFolder = Utils.getSharedDataDir() + "CDR"; |
| 17 | + String[] alignments = {"Left", "Center", "Right"}; |
| 18 | + for (String alignment : alignments) |
| 19 | + { |
| 20 | + drawString(baseFolder, alignment); |
| 21 | + } |
| 22 | + Logger.endExample(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void drawString(String baseFolder, String align) |
| 26 | + { |
| 27 | + String fileName = "output_" + align + ".png"; |
| 28 | + String outputFileName = Path.combine(baseFolder, fileName); |
| 29 | + String[] fontNames = |
| 30 | + { |
| 31 | + "Arial", "Times New Roman", |
| 32 | + "Bookman Old Style", "Calibri", "Comic Sans MS", |
| 33 | + "Courier New", "Microsoft Sans Serif", "Tahoma", |
| 34 | + "Verdana", "Proxima Nova Rg" |
| 35 | + }; |
| 36 | + |
| 37 | + float[] fontSizes = new float[]{10f, 22f, 50f, 100f}; |
| 38 | + int width = 3000; |
| 39 | + int height = 3500; |
| 40 | + |
| 41 | + //Create an instance of PngOptions and set its various properties |
| 42 | + try (PngOptions pngOptions = new PngOptions()) |
| 43 | + { |
| 44 | + //Set the Source for PngOptions |
| 45 | + pngOptions.setSource(new FileCreateSource(outputFileName)); |
| 46 | + |
| 47 | + //Create an instance of Image |
| 48 | + try (Image image = Image.create(pngOptions, width, height)) |
| 49 | + { |
| 50 | + //Create and initialize an instance of Graphics class |
| 51 | + Graphics graphics = new Graphics(image); |
| 52 | + // To speed up the drawing operations |
| 53 | + graphics.beginUpdate(); |
| 54 | + |
| 55 | + //Clear Graphics surface |
| 56 | + graphics.clear(Color.getWhite()); |
| 57 | + |
| 58 | + //Create a SolidBrush object and set its various properties |
| 59 | + SolidBrush brush = new SolidBrush(); |
| 60 | + brush.setColor(Color.getBlack()); |
| 61 | + float x = 10; |
| 62 | + int lineX; |
| 63 | + float y = 10; |
| 64 | + float w = width - 20; |
| 65 | + Pen pen = new Pen(Color.getRed(), 1); |
| 66 | + |
| 67 | + int alignment; |
| 68 | + switch (align) |
| 69 | + { |
| 70 | + case "Left": |
| 71 | + alignment = StringAlignment.Near; |
| 72 | + lineX = Math.round(x); |
| 73 | + break; |
| 74 | + |
| 75 | + case "Center": |
| 76 | + alignment = StringAlignment.Center; |
| 77 | + lineX = Math.round(x + w / 2f); |
| 78 | + break; |
| 79 | + |
| 80 | + case "Right": |
| 81 | + alignment = StringAlignment.Far; |
| 82 | + lineX = (int) (x + w); |
| 83 | + break; |
| 84 | + default: |
| 85 | + throw new IllegalArgumentException("Wrong alignment: " + align); |
| 86 | + } |
| 87 | + |
| 88 | + StringFormat stringFormat = new StringFormat(StringFormatFlags.ExactAlignment); |
| 89 | + stringFormat.setAlignment(alignment); |
| 90 | + for (String fontName : fontNames) |
| 91 | + { |
| 92 | + for (float fontSize : fontSizes) |
| 93 | + { |
| 94 | + Font font = new Font(fontName, fontSize); |
| 95 | + String text = String.format("This is font: %s, size:%d", fontName, (int) fontSize); |
| 96 | + SizeF s = graphics.measureString(text, font, SizeF.getEmpty(), null); |
| 97 | + graphics.drawString(text, font, brush, new RectangleF(x, y, w, s.getHeight()), stringFormat); |
| 98 | + |
| 99 | + y += s.getHeight(); |
| 100 | + } |
| 101 | + |
| 102 | + graphics.drawLine(pen, new Point((int) (x), (int) y), new Point((int) (x + w), (int) y)); |
| 103 | + } |
| 104 | + |
| 105 | + graphics.drawLine(pen, new Point(lineX, 0), new Point(lineX, (int) y)); |
| 106 | + |
| 107 | + graphics.endUpdate(); |
| 108 | + |
| 109 | + // save all changes. |
| 110 | + image.save(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Path.deleteFile(outputFileName); |
| 115 | + } |
| 116 | +} |
0 commit comments