    001package org.vishia.gral.base;
    002
    003import java.util.Map;
    004import java.util.TreeMap;
    005
    006import org.vishia.bridgeC.IllegalArgumentExceptionJc;
    007import org.vishia.gral.ifc.GralColor;
    008import org.vishia.gral.ifc.GralFont;
    009
    010public class GralGridProperties
    011{
    012  /**Version, history and license.
    013   * <ul>
    014   * <li>2015-10-24 Hartmut chg: Size C is now 10 pixel per grid unit, size D is the older size C.
    015   * <li>2015-10-11 Hartmut chg: The text font should be lesser because g, q, y are not proper able to read.
    016   *   TODO think about finer steps between A..C, but don't change existing sizes because some graphics are tuned with them.
    017   * <li>2010-00-00 Hartmut created. The idea of a grid layout with different sizes as basic.
    018   * </ul>
    019   * <b>Copyright/Copyleft</b>:
    020   * For this source the LGPL Lesser General Public License,
    021   * published by the Free Software Foundation is valid.
    022   * It means:
    023   * <ol>
    024   * <li> You can use this source without any restriction for any desired purpose.
    025   * <li> You can redistribute copies of this source to everybody.
    026   * <li> Every user of this source, also the user of redistribute copies
    027   *    with or without payment, must accept this license for further using.
    028   * <li> But the LPGL is not appropriate for a whole software product,
    029   *    if this source is only a part of them. It means, the user
    030   *    must publish this part of source,
    031   *    but don't need to publish the whole source of the own product.
    032   * <li> You can study and modify (improve) this source
    033   *    for own using or for redistribution, but you have to license the
    034   *    modified sources likewise under this LGPL Lesser General Public License.
    035   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    036   * </ol>
    037   * If you are indent to use this sources without publishing its usage, you can get
    038   * a second license subscribing a special contract with the author.
    039   *
    040   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    041   *
    042   *
    043   */
    044  public final static int version = 0x20120303;
    045
    046
    047  /**This are the font sizes for some heights of fields in the given sizes of presentation.
    048   * First index is the height of field, second is the size of presentation.
    049   */
    050  protected final static int[][] stdTextFontSize =
    051  { {5, 5, 6, 7, 7, 8, 9}  ////1, 1.1, 1.2
    052  , {6, 6, 7, 8, 9, 9,10}  // 1 1/3,
    053  , {6, 7, 8, 9, 9,10,12}  //1.5
    054  , {7, 7, 8, 10,10,12,14}  //1 2/3
    055  , {7, 8, 9, 11,12,14,18}  //2
    056  , {8, 9, 10,11,12,14,18}  //2 1/3
    057  , {9,10, 11, 11,12,14,18}  //2.5, 2 2/3
    058  , {9,11, 11, 12, 12,14,18}  //3
    059  , {10,11,11,12, 12,14,18}  //3.5
    060  , {10,11,11,12, 12,14,18}  //>=4
    061  };
    062
    063  protected final static int[] smallPromptFontSize = stdTextFontSize[0];
    064  protected final static int[] stdInputFontSize =    stdTextFontSize[4];
    065  protected final static int[] stdButtonFontSize =   stdTextFontSize[7];
    066
    067  /** Number of pixel for fractional part and for the grid size.
    068   * The array is organized in a 2-dimensional array <code>[frac][size]</code>.
    069   * <ul>
    070   * <li>The first column contains 0 for frac = 0.
    071   * <li>The last column: <code>pixelFrac[size] [10]</code> contains the pixel size of the grid.
    072   * <li>The fractional part is given in even numbers (1, 3, 5, 7, 9) to divide in part of 1/6:
    073   *     1 = 1/6, 3 = 1/3, 5 = 1/2, 7 = 2/3, 9 = 5/6. It is decimal 0.166, 0.333, 0.5, 0.666, 0.833.
    074   * <li>The fractional part is given in odd numbers (2, 4, 6, 8) to divide in part of 1/5 or decimal 2/10:
    075   *     2 = 1/5 = 0.2, 4 = 2/5 = 0.4, 6 = 3/5 = 0.6, 8 = 4/5 = 0.8.
    076   * </ul>
    077   * Typical usage for grid units and associated pixel units in size C:
    078   * <table>
    079   * <tr><th>Type                      </td><td>units</td><td>pixel</td></tr>
    080   * <tr><td>Label normal text size    </td><td>2   </td><td>16   </td></tr>
    081   * <tr><td>Button normal text size   </td><td>3   </td><td>24   </td></tr>
    082   * <tr><td>TextField                 </td><td>2   </td><td>16   </td></tr>
    083   * <tr><td>Large InputField          </td><td>4   </td><td>32   </td></tr>
    084   * </table>
    085   * Number of Grid Units for full display sizes. An application which needs about 40 lines of text or text fields
    086   * needs 80 grid unit in vertical, it is about 80x120. It runs in a small window with 640 x 480 pixel on size A only.
    087   * In full display mode on a standard display it can use the size C or D.
    088   * <table>
    089   * <tr><th>size        </td><td>A        </td><td>B      </td><td>C      </td><td>D      </td><td>E      </td><td>F      </td></td><td>G     </td></tr>
    090   * <tr><td>pixel/grid  </td><td> 6       </td><td>  7      </td><td>  8    </td><td>  9    </td><td>  10   </td><td>12  </td><td>15  </td></tr>
    091   * <tr><td>640 x 480   </td><td><b>106 x  80</b></td><td>91  x  68</td><td> 80 x  60</td><td> todo </td><td> todo </td><td> todo </td></tr>
    092   * <tr><td>800 x 600   </td><td>133 x 100</td><td><b>114 x  85</b></td><td>100 x  75</td><td> todo </td><td> todo </td><td> todo </td></tr>
    093   * <tr><td>1024 x 768  </td><td>170 x 128</td><td>146 x 109</td><td><b>128 x  96</b></td><td>113 x 85</td><td>102 x 76</td><td>85 x 64</td><td>68 x 51</td></tr>
    094   * <tr><td>1200 x 800  </td><td>200 x 133</td><td>171 x 114</td><td>150 x 100</td><td><b>133 x 88</b></td><td>120 x 80</td><td>100 x 66</td><td>80 x 53</td></tr>
    095   * <tr><td>1680 x 1024 </td><td>280 x 170</td><td>240 x 146</td><td>210 x 128</td><td>186 x 113</td><td>168 x 102</td><td>140 x 85</td><td>112 x 68</td></tr>
    096   * </table>
    097   * <table>
    098   * <tr><th>Type                       </td><td>unit</td><td>pixel</td></tr>
    099   * <tr><td>JLabel normal text size    </td><td>2   </td><td>0+6   </td></tr>
    100   * <tr><td>JButton normal text size   </td><td>3   </td><td>26   </td></tr>
    101   * <tr><td>InputField                 </td><td>4   </td><td>34   </td></tr>
    102   * </table>
    103   */
    104  protected final static int[][] pixelFrac =
    105 //    1/6   1/3   1/2   2/3   5/6   Divisions of 2, 3 and 6
    106 //       1/5   2/5   3/5   4/5      Divisions of 5
    107 //     1  2  3  4  5  6  7  8  9   0   the number given
    108  { {0, 1, 1, 2, 2, 3, 4, 4, 5, 5,  6 }  //pixel size A
    109  , {0, 1, 1, 2, 3, 3, 4, 5, 6, 6,  7 }
    110  , {0, 1, 2, 3, 3, 4, 5, 6, 6, 7,  8 }
    111  , {0, 1, 2, 3, 4, 4, 5, 6, 7, 8,  9 }
    112  , {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    113  , {0, 1, 2, 4, 5, 6, 7, 8,10,11, 12 }
    114  , {0, 2, 3, 5, 6, 7, 9,10,12,13, 15 }
    115  };
    116
    117  //protected final static int[][] yPixelFrac = xPixelFrac;
    118
    119  protected final int xPixelUnit;
    120
    121  //Map<Integer, GralColor> colors = new TreeMap<Integer, GralColor>();
    122
    123  /**A common background color for all widgets which are paint at the background. */
    124  public GralColor colorBackground_;
    125
    126  /**Up to 10 font sizes for following hight of lettes:
    127   * <table>
    128   * <tr><td>..1.2</td><td>..1.4</td><td>..1.6</td><td>..1.8</td><td>..2.0</td><td>..2.4</td><td>..2.8</td><td>..3.1</td><td>..3.9</td><td>3.9...</td></tr>
    129   * <tr><td>  0  </td><td>  1  </td><td>  2  </td><td>  3  </td><td>  4  </td><td>  5  </td><td>  6  </td><td>  7  </td><td>  8  </td><td>  9  </td><td></tr>
    130   * </table>
    131   * This table will be filled depending of the Grid size, see {@link #stdTextFontSize}.
    132   *
    133   */
    134  public final GralFont[] textFont = new GralFont[10];
    135
    136
    137  public final GralFont[] fontMonospacedSansSerif = new GralFont[10];
    138
    139  /**The size of this propety set.*/
    140  protected final int size;
    141
    142        public GralGridProperties(char sizeC)
    143        {
    144                int size = (sizeC - 'A');
    145          if(size <0 || size >= stdInputFontSize.length) throw new IllegalArgumentException("parameter size should be 1.." + stdInputFontSize.length);
    146        this.size = size;
    147    this.xPixelUnit = pixelFrac[size][10];
    148    colorBackground_ = color(0xeeeeee);
    149    this.textFont[0] = new GralFont("Arial", stdTextFontSize[0][size], 'n');
    150    this.textFont[1] = new GralFont("Arial", stdTextFontSize[1][size], 'n');
    151    this.textFont[2] = new GralFont("Arial", stdTextFontSize[2][size], 'n');
    152    this.textFont[3] = new GralFont("Arial", stdTextFontSize[3][size], 'n');
    153    this.textFont[4] = new GralFont("Arial", stdTextFontSize[4][size], 'n');
    154    this.textFont[5] = new GralFont("Arial", stdTextFontSize[5][size], 'n');
    155    this.textFont[6] = new GralFont("Arial", stdTextFontSize[6][size], 'n');
    156    this.textFont[7] = new GralFont("Arial", stdTextFontSize[7][size], 'n');
    157    this.textFont[8] = new GralFont("Arial", stdTextFontSize[8][size], 'n');
    158    this.textFont[9] = new GralFont("Arial", stdTextFontSize[9][size], 'n');
    159    String sMonospaced = GralFont.fontMonospacedSansSerif;
    160    this.fontMonospacedSansSerif[0] = new GralFont(sMonospaced, stdTextFontSize[0][size], 'n');
    161    this.fontMonospacedSansSerif[1] = new GralFont(sMonospaced, stdTextFontSize[1][size], 'n');
    162    this.fontMonospacedSansSerif[2] = new GralFont(sMonospaced, stdTextFontSize[2][size], 'n');
    163    this.fontMonospacedSansSerif[3] = new GralFont(sMonospaced, stdTextFontSize[3][size], 'n');
    164    this.fontMonospacedSansSerif[4] = new GralFont(sMonospaced, stdTextFontSize[4][size], 'n');
    165    this.fontMonospacedSansSerif[5] = new GralFont(sMonospaced, stdTextFontSize[5][size], 'n');
    166    this.fontMonospacedSansSerif[6] = new GralFont(sMonospaced, stdTextFontSize[6][size], 'n');
    167    this.fontMonospacedSansSerif[7] = new GralFont(sMonospaced, stdTextFontSize[7][size], 'n');
    168    this.fontMonospacedSansSerif[8] = new GralFont(sMonospaced, stdTextFontSize[8][size], 'n');
    169    this.fontMonospacedSansSerif[9] = new GralFont(sMonospaced, stdTextFontSize[9][size], 'n');
    170
    171        }
    172
    173
    174        public int getColorValue(String sColorName)
    175        { //Integer colorValue =
    176          GralColor color = GralColor.getColor(sColorName);
    177          if(color == null) return 0x606060;
    178          else return color.getColorValue();
    179        }
    180
    181
    182  public GralFont getTextFont(float size)
    183  {
    184    if(size <=1.2f) return textFont[0];  //1, 1.1, 1.2
    185    if(size <=1.4f) return textFont[1];  // 1 1/3,
    186    if(size <=1.6f) return textFont[2];  //1.5
    187    if(size <=1.8f) return textFont[3];  //1 2/3
    188    if(size <=2.0f) return textFont[4];  //2
    189    if(size <=2.4f) return textFont[5];   //2 1/3
    190    if(size <=2.8f) return textFont[6];   //2.5, 2 2/3
    191    if(size <=3.1f) return textFont[7];   //3
    192    if(size <=3.9f) return textFont[8];   //3.5
    193    return textFont[9];                   //>=4
    194
    195  }
    196
    197  /**The type:
    198   * <ul>
    199   * <li>m: monospaced small
    200   * </ul>
    201   * The style: not supported yet. TODO i, b for italic, bold,...
    202   *
    203   * @param fontHeight height of the text line in GralPos. 2.0f is standard.
    204   * @param type
    205   * @param style
    206   * @return
    207   */
    208  public GralFont getTextFont(float fontHeight, char type, char style)
    209  {
    210    int ifontSize;
    211    if(fontHeight <=1.2f) ifontSize = 0;  //1, 1.1, 1.2
    212    else if(fontHeight <=1.4f) ifontSize = 1;  // 1 1/3,
    213    else if(fontHeight <=1.6f) ifontSize = 2;  //1.5
    214    else if(fontHeight <=1.8f) ifontSize = 3;  //1 2/3
    215    else if(fontHeight <=2.0f) ifontSize = 4;  //2
    216    else if(fontHeight <=2.4f) ifontSize = 5;   //2 1/3
    217    else if(fontHeight <=2.8f) ifontSize = 6;   //2.5, 2 2/3
    218    else if(fontHeight <=3.1f) ifontSize = 7;   //3
    219    else if(fontHeight <=3.9f) ifontSize = 8;   //3.5
    220    else ifontSize = 9;                   //>=4
    221    final GralFont font;
    222    switch(type){
    223      case 'm': font = fontMonospacedSansSerif[ifontSize]; break;
    224      default: font = textFont[ifontSize];
    225    }
    226    return font;
    227  }
    228
    229
    230
    231
    232  /**Returns a color with given numeric color value.
    233   * The color instance is taken from a pool if the color is used already.
    234   * Elsewhere it is created newly and put into the pool.
    235   * @param colorValue red, green and blue
    236   * @return An instance of color
    237   */
    238  public GralColor color(int colorValue){
    239    GralColor color;
    240    if(colorValue >=0 && colorValue < 0x1000000){
    241      color = GralColor.getColor(colorValue);
    242    } else {
    243      throw new IllegalArgumentExceptionJc("color value fault", colorValue);
    244    }
    245    return color;
    246  }
    247
    248
    249
    250
    251  public int yPixelFrac(int frac){ return pixelFrac[size][frac]; }
    252
    253
    254  /**Gets the number of pixel for one unit of x-direction. It is approximately the half width of the letter 'm'
    255   * for the standard input field font. To support visibility of a dedicated number of chars, you should use
    256   * 2 * nrofChars units for a text field. In opposite, the Constructor for the javax.swing.TextField(int)
    257   * sets the size of the text-field to the given number of 'm'-width. Here you should use factor 2.
    258   *
    259   * NOTE: A possibility to derive the xPixelUnit() from the font wasn't found. The cohesion between the font-widht for 'm'
    260   * and the returned number of pixels is tested manually. It may be different if another font is used.
    261   * @return Number of pixel for 1 unit in x-direction.
    262   */
    263  public int xPixelUnit(){ return pixelFrac[size][10]; }
    264
    265  public int yPixelUnit(){ return pixelFrac[size][10]; }
    266
    267  public int xPixelFrac(int frac){ return pixelFrac[size][frac]; }
    268
    269
    270
    271
    272}