MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / stringToPixels

Method stringToPixels

ij/src/main/java/ij/gui/Plot.java:3083–3185  ·  view source on GitHub ↗
(String labelStr)

Source from the content-addressed store, hash-verified

3081 //Returns null for empty or blank-only strings
3082 //Supports !!subscript!! and ^^superscript^^
3083 ByteProcessor stringToPixels(String labelStr) {
3084 Font bigFont = ip.getFont();
3085 Rectangle rect = ip.getStringBounds(labelStr);
3086 int ww = rect.width * 2;
3087 int hh = rect.height * 3;//enough space, will be cropped later
3088 int y0 = rect.height * 2;//base line
3089 if (ww <= 0 || hh <= 0) {
3090 return null;
3091 }
3092 ByteProcessor box = new ByteProcessor(ww, hh);
3093 box.setColor(Color.WHITE);
3094 //box.setColor(Color.LIGHT_GRAY); //make box visible for test
3095 box.fill();
3096 box.setColor(Color.black);
3097 box.setAntialiasedText(pp.antialiasedText);
3098 if (invertedLut) {
3099 box.invertLut();
3100 }
3101 box.setFont(bigFont);
3102
3103 FontMetrics fm = box.getFontMetrics();
3104 int ascent = fm.getAscent();
3105 int offSub = ascent / 6;
3106 int offSuper = -ascent / 2;
3107 Font smallFont = bigFont.deriveFont((float) (bigFont.getSize() * 0.7));
3108
3109 Rectangle bigBounds = box.getStringBounds(labelStr);
3110 boolean doParse = (labelStr.indexOf("^^") >= 0 || labelStr.indexOf("!!") >= 0);
3111 doParse = doParse && (labelStr.indexOf("^^^") < 0 && labelStr.indexOf("!!!") < 0);
3112 if (!doParse) {
3113 box.drawString(labelStr, 0, y0);
3114 Rectangle cropRect = new Rectangle(bigBounds);
3115 cropRect.y += y0;
3116 box.setRoi(cropRect);
3117 ImageProcessor boxI = box.crop();
3118 box = boxI.convertToByteProcessor();
3119 return box;
3120 }
3121
3122 if (labelStr.endsWith("^^") || labelStr.endsWith("!!")) {
3123 labelStr = labelStr.substring(0, labelStr.length() - 2);
3124 }
3125 if (labelStr.startsWith("^^") || labelStr.startsWith("!!")) {
3126 labelStr = " " + labelStr;
3127 }
3128
3129 box.setFont(smallFont);
3130 Rectangle smallBounds = box.getStringBounds(labelStr);
3131 box.setFont(bigFont);
3132 int upperBound = y0 + smallBounds.y + offSuper;
3133 int lowerBound = y0 + smallBounds.y + smallBounds.height + offSub;
3134
3135 int h = fm.getHeight();
3136 int len = labelStr.length();
3137 int[] tags = new int[len];
3138 int nTags = 0;
3139
3140 for (int jj = 0; jj < len - 2; jj++) {//get positions where font size changes

Callers 1

Calls 15

setColorMethod · 0.95
fillMethod · 0.95
cropMethod · 0.95
substringMethod · 0.80
startsWithMethod · 0.80
getFontMethod · 0.65
setFontMethod · 0.65
getSizeMethod · 0.65
lengthMethod · 0.65
getStringBoundsMethod · 0.45
setAntialiasedTextMethod · 0.45

Tested by

no test coverage detected