Font Type Detector agent
@param content Text to make a detection
@param def Default return format;
@return unicode ? zawgyi
13053
function fontDetect(content, fallback_font_type, options = {}){
  if (!content) {
    if (!globalOptions.isSilentMode()) console.warn('Content must be specified on knayi.fontDetect.');
    return fallback_font_type || 'en';
  }

	if (content === '')
		return content;

	if (!mmCharacterRange.test(content))
		return fallback_font_type || 'en';

	content = content.trim().replace(/\u200B/g, '');
	fallback_font_type = fallback_font_type || 'zawgyi';

  options = globalOptions.detector(options);

  if (options.use_myanmartools) {
    var myanmartools_zg_probability = myanmartoolZawgyiDetector.getZawgyiProbability(content);

    if (myanmartools_zg_probability < options.myanmartools_zg_threshold[0]) {
  		return 'unicode';
  	} else if (myanmartools_zg_probability > options.myanmartools_zg_threshold[1]) {
  		return 'zawgyi';
  	} else {
  		return fallback_font_type;
  	}

  } else {
    var match = {};

    for (var type in library.detect) {
  		match[type] = 0;

  		for (var i = 0; i < library.detect[type].length; i++) {
  			var rule = library.detect[type][i]
  			var m = content.match(rule);
  			match[type] += (m && m.length) || 0;
  		}
  	}

    if (match.unicode > match.zawgyi) {
  		return 'unicode';
  	} else if (match.unicode < match.zawgyi) {
  		return 'zawgyi';
  	} else {
  		return fallback_font_type;
  	}

  }
}
167061
public static function detectCharset( $body, $content_type, $default_charset )
    {
        // get character encoding from Content-Type header
        preg_match( '@([\w/+]+)(;\s+charset=(\S+))?@i', $content_type, $matches );
        $charset = isset($matches[3]) ? $matches[3] : $default_charset;

        $php_encoding = $charset ? self::getPhpEncoding($charset) : null;
        if ( !$php_encoding ){
            $html_encoded = mb_convert_encoding( strtolower($body), 'HTML-ENTITIES', 'UTF-8' );
            $doc = new \DOMDocument();
            @$doc->loadHTML( $html_encoded );
            $elements = $doc->getElementsByTagName( "meta" );

            for($i = 0; $i < $elements->length; $i++) {
                $e = $elements->item($i);

                // find like: <meta charset="utf-8"/>
                $node = $e->attributes->getNamedItem("charset");
                if($node){
                    $charset = $node->nodeValue;
                    $php_encoding = self::getPhpEncoding($charset);
                    break;
                }

                // find like: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                $node = $e->attributes->getNamedItem("http-equiv");
                if ( $node ){
                    if ( strcasecmp($node->nodeValue, 'content-type') == 0 ){
                        $node = $e->attributes->getNamedItem("content");
                        if( $node ){
                            if ( preg_match('/[\; ]charset ?\= ?([A-Za-z0-9\-\_]+)/', $node->nodeValue, $m) ){
                                $charset = $m[1];
                                $php_encoding = self::getPhpEncoding($charset);
                                break;
                            }
                        }
                    }
                }
            }
        }

        return $php_encoding;
    }
1147428
function(type, text) {
            var parser = parsers.get(type);

            return parser.parseInline(text)
                .get('content');
        }
737088
public static FontUIResource getFont(PropertyOwner propertyOwner, Map<String,Object> properties, boolean returnDefaultIfNone)
    {
        String strFontName = ScreenUtil.getPropery(ScreenUtil.FONT_NAME, propertyOwner, properties, null);
        String strFontSize = ScreenUtil.getPropery(ScreenUtil.FONT_SIZE, propertyOwner, properties, null);
        String strFontStyle = ScreenUtil.getPropery(ScreenUtil.FONT_STYLE, propertyOwner, properties, null);

        if ((strFontName == null) || (strFontName.length() == 0))
        {
            if (!returnDefaultIfNone)
                return null;
            strFontName = Font.DIALOG;  // Default font
        }
        int iSize = 18; // Default size
        if ((strFontSize != null) && (strFontSize.length() > 0))
            iSize = Integer.parseInt(strFontSize);      
        int iStyle = Font.PLAIN;
        if ((strFontStyle != null) && (strFontStyle.length() > 0))
            iStyle = Integer.parseInt(strFontStyle);
        return new FontUIResource(strFontName, iStyle, iSize);
    }
1089587
@Override
   public Typeface  resolveFont(String fontFamily, int fontWeight, String fontStyle)
   {
      Log.i(TAG, "resolveFont("+fontFamily+","+fontWeight+","+fontStyle+")");

      // Try font name with suffix ".ttf"
      try
      {
         return Typeface.createFromAsset(assetManager, fontFamily + ".ttf");
      }
      catch (RuntimeException ignored) {}

      // That failed, so try ".otf"
      try
      {
         return Typeface.createFromAsset(assetManager, fontFamily + ".otf");
      }
      catch (RuntimeException e)
      {
         return null;
      }
   }
230848
public boolean matchContentType(String contentTypeRegex) {
        if (contentTypeRegex == null) {
            return contentType() == null;
        }
        return Pattern.matches(contentTypeRegex, contentType());
    }
675904
protected Font getTextFont(byte textType) {
		try {
			return getTemplate().getTextFont(textType);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
351303
function computeFontWidthFactor (font) {
  var sum = 0;
  var digitsum = 0;
  var digits = 0;
  font.chars.map(function (ch) {
    sum += ch.xadvance;
    if (ch.id >= 48 && ch.id <= 57) {
      digits++;
      digitsum += ch.xadvance;
    }
  });
  return digits ? digitsum / digits : sum / font.chars.length;
}
78201
public static String addUtf8Charset(String contentType)
    {
        if (contentType == null)
        {
            return "text/plain;charset=utf-8";
        }

        Matcher matcher = CHARSET_PATT.matcher(contentType);
        if (matcher.find())
        {
            // replace what ever content-type with utf8
            return contentType.substring(0, matcher.start(1)) + "utf-8" + contentType.substring(matcher.end(1));
        }

        return contentType + ";charset=utf-8";
    }
669908
@Nonnull
  public ExcelStyle setFont (@Nonnull final Font aFont)
  {
    ValueEnforcer.notNull (aFont, "Font");
    return setFontIndex (aFont.getIndexAsInt ());
  }
237946
private static FontStyle stringToFontStyle(String style) {
        if (style.compareToIgnoreCase("plain") == 0) {
            return FontStyle.PLAIN;
        } else if (style.compareToIgnoreCase("bold") == 0) {
            return FontStyle.BOLD;
        } else if (style.compareToIgnoreCase("italic") == 0) {
            return FontStyle.ITALIC;
        } else {
            throw new IllegalArgumentException();
        }
    }
659786
public Font getTextFont(byte field) {
		FieldInfos fi = getFieldInfos(field);
		Vector v = new Vector(m_defaultTextFontFamilyNames.length+fi.m_fontFamilyNames.length);
        Collections.addAll(v, fi.m_fontFamilyNames);
        Collections.addAll(v, m_defaultTextFontFamilyNames);
		Iterator it = v.iterator();
		Font font = null;
		String s = "";
		int style = getTextStyle(field);
		int size = (int) getTextSize(field);
		while (it.hasNext()) {
			String fontName = (String) it.next();
			if (s.length() > 0)
				s += ", ";
			s += fontName;
			if (isFontAvailable(fontName)) {
				font = new Font(fontName, style, size);
				break;
			}
		}
		if (font == null) {
			System.err.println("None of these fonts are available: " + s);
			font = new Font("Dialog", style, size);
		}
		if (fi.m_textAttributes != null) {
			font = font.deriveFont(fi.m_textAttributes);
		}
		return font;
	}
231437
private static ByteBuffer stringAsType(String content, AbstractType<?> type)
    {
        try
        {
            return type.fromString(content);
        }
        catch (MarshalException e)
        {
            throw new RuntimeException(e.getMessage());
        }
    }
925939
private ContentType parseContentType(String value) {
        String[] pcs = value.split("\\;");
        String mimeType = pcs[0].trim();
        String charSet = "UTF-8";
        
        for (int i = 1; i < pcs.length; i++) {
            String s = pcs[i].trim().toUpperCase();
            
            if (s.startsWith("CHARSET=")) {
                charSet = s.substring(8);
                break;
            }
        }
        
        return ContentType.create(mimeType, charSet);
    }
752467
public static String getCharset(final String contentType) {
        // Default to UTF-8
        String charset = "UTF-8";
        try {
            // Content type is in the form:
            // Content-Type: text/html; charset=utf-8
            // Where they can be lots of params separated by ; characters
            if (contentType != null && !contentType.isEmpty()) {
                if (contentType.contains(";")) {
                    String[] params = contentType.substring(contentType.indexOf(";")).split(";");
                    for (String param : params) {
                        param = param.trim();
                        if (param.indexOf("=") > 0) {
                            String paramName = param.substring(0, param.indexOf("=")).trim();
                            if ("charset".equals(paramName) && param.length() > (param.indexOf("=") + 1)) {
                                String paramValue = param.substring(param.indexOf("=") + 1).trim();
                                if (paramValue != null && !paramValue.isEmpty() && Charset.isSupported(paramValue)) {
                                    charset = paramValue;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable t) {
            // Ignore, we really don't want this util killing anything!
        }
        return charset;
    }
49670
public static PDFont chooseMatchingCourier(Font font) {
		if ((font.getStyle() & (Font.ITALIC | Font.BOLD)) == (Font.ITALIC | Font.BOLD))
			return PDType1Font.COURIER_BOLD_OBLIQUE;
		if ((font.getStyle() & Font.ITALIC) == Font.ITALIC)
			return PDType1Font.COURIER_OBLIQUE;
		if ((font.getStyle() & Font.BOLD) == Font.BOLD)
			return PDType1Font.COURIER_BOLD;
		return PDType1Font.COURIER;
	}
521782
function loadFont (fontFace, callback) {
  var defaultNode;
  var testNode;
  var checkFont;

  // See if we've previously loaded it.
  if (_loadedFonts[fontFace.id]) {
    return callback(null);
  }

  // See if we've previously failed to load it.
  if (_failedFonts[fontFace.id]) {
    return callback(_failedFonts[fontFace.id]);
  }

  // System font: assume already loaded.
  if (!fontFace.url) {
    return callback(null);
  }

  // Font load is already in progress:
  if (_pendingFonts[fontFace.id]) {
    _pendingFonts[fontFace.id].callbacks.push(callback);
    return;
  }

  // Create the test <span>'s for measuring.
  defaultNode = createTestNode('Helvetica', fontFace.attributes);
  testNode = createTestNode(fontFace.family, fontFace.attributes);
  document.body.appendChild(testNode);
  document.body.appendChild(defaultNode);

  _pendingFonts[fontFace.id] = {
    startTime: Date.now(),
    defaultNode: defaultNode,
    testNode: testNode,
    callbacks: [callback]
  };

  // Font watcher
  checkFont = function () {
    var currWidth = testNode.getBoundingClientRect().width;
    var defaultWidth = defaultNode.getBoundingClientRect().width;
    var loaded = currWidth !== defaultWidth;

    if (loaded) {
      handleFontLoad(fontFace, null);
    } else {
      // Timeout?
      if (Date.now() - _pendingFonts[fontFace.id].startTime >= kFontLoadTimeout) {
        handleFontLoad(fontFace, true);
      } else {
        requestAnimationFrame(checkFont);
      }
    }
  };

  // Start watching
  checkFont();
}